DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Tapestry Grid & Hibernate Criteria

Tapestry Grid & Hibernate Criteria

Taha Siddiqi user avatar by
Taha Siddiqi
·
Jun. 01, 12 · Java Zone · Interview
Like (0)
Save
Tweet
3.49K Views

Join the DZone community and get the full member experience.

Join For Free

Tapestry Grid is a very powerful tool for displaying a list of entities. It expects a GridDataSource as source parameter. A useful tool can be a TypeCoercer which converts a Criteria to GridDataSource.

Here is a simple implementation

public class Criteria2GridDataSourceCoercion implements Coercion<Criteria, GridDataSource> {

    @Override
    public GridDataSource coerce(final Criteria criteria) {
        return new GridDataSource() {
            private List<?> cache;

            private int startIndex;

            @Override
            public int getAvailableRows() {
                setupCriteria(Projections.rowCount(), 0, 1);
                long count = ((Long) criteria.uniqueResult());
                return (int) count;
            }

            @Override
            public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) {
                setupCriteria(null, startIndex, endIndex - startIndex + 1);
                criteria.setResultTransformer(Criteria.ROOT_ENTITY);

                if (sortConstraints.size() != 0) {
                    for (SortConstraint sortConstraint : sortConstraints) {
                        String propertyName = sortConstraint.getPropertyModel().getPropertyName();
                        criteria.addOrder(
                                sortConstraint.getColumnSort() == ColumnSort.ASCENDING ?
                                        Order.asc(propertyName) : Order.desc(propertyName));
                    }
                }

                cache = criteria.list();
                this.startIndex = startIndex;
            }

            @Override
            public Object getRowValue (int index) {
                return cache.get(index - startIndex);
            }

            @Override
            public Class getRowType() {
                if (cache == null) {
                    setupCriteria(null, 0, 1);
                    criteria.setResultTransformer(Criteria.ROOT_ENTITY);
                    cache = criteria.list();
                }

                if (cache.size() != 0) {
                    return cache.get(0).getClass();
                }

                return null;
            }

            private void setupCriteria(Projection projection, int firstResult, int maxResults) {
                criteria.setProjection(projection).setFirstResult(firstResult).setMaxResults
                        (maxResults);
            }
        };
    }
}

and in your Module class

@Contribute(TypeCoercer.class)
 public static void addCoercion(Configuration<CoercionTuple> types) {
     types.add(new CoercionTuple(Criteria.class, GridDataSource.class,
             new Criteria2GridDataSourceCoercion()));
 }

Usage

public class SamplePage {

    @Inject
    private Session session;

    public Criteria getCriteria() {
        Criteria criteria = session.createCriteria(MyEntity.class);
        return criteria;
    }
}

<html xmlns:t='http://tapestry.apache.org/schema/tapestry_5_3.xsd'>
   <div t:type='Grid' t:source='criteria'>
   </div>
</html>

Dragan came up with another useful Coercer to convert Criteria to Long

public static void contributeTypeCoercer(Configuration configuration) {
    configuration.add(CoercionTuple.create(Criteria.class, Long.class,
            new Coercion() {
                @Override
                public Long coerce(Criteria input) {
                    input = input.setProjection(Projections.rowCount());
                    input = input.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
                    return (Long) input.uniqueResult();
                }
            }));

    configuration.add(CoercionTuple.create(Criteria.class, List.class,
            new Coercion() {
                @Override
                public List coerce(Criteria input) {
                    return input.list();
                }
            }));
}

 

 

 

 

 

Hibernate Convert (command) entity Implementation

Published at DZone with permission of Taha Siddiqi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Stupid Things Orgs Do That Kill Productivity w/ Netflix, FloSports & Refactoring.club
  • The End of the Beginning for Apache Cassandra
  • Why Performance Projects Fail
  • What Are Cookies in Servlets?

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo