Java tutorial
package com.reignite.parser; import java.beans.IntrospectionException; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.util.ArrayList; import java.util.List; import java.util.Set; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Order; import org.hibernate.criterion.ProjectionList; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hibernate.metadata.ClassMetadata; import org.hibernate.type.Type; /* Copyright (c) 2014 Surrey Hughes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The Software shall be used for Good, not Evil. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * @author Surrey * * <pre> * joins : { * id : "id", // the field used to aggregate join rows * items : [ // join added as a field * { * name : "roles", * where : {// where added as an example * "roles.id" : { * gt : 0 * } * }, * fields : [ * "roles.role" * ] * // will return an array of string with the role name * } * ] * } * </pre> */ public class Join { private Criteria criteria; private Session session; private String entity; private String joinId; private String name; private List<String> fields = new ArrayList<String>(); private List<String> expectedFields = new ArrayList<String>(); private Criterion where; private ProjectionList projections; private Integer startIndex = 0; private Integer maxResults = 1000; private List<Order> orders = new ArrayList<Order>(); public List<Order> getOrders() { return orders; } public void setOrders(List<Order> orders) { this.orders = orders; } public Integer getStartIndex() { return startIndex; } public void setStartIndex(Integer startIndex) { this.startIndex = startIndex; } public Integer getMaxResults() { return maxResults; } public void setMaxResults(Integer maxResults) { if (maxResults != null && maxResults > 0) { this.maxResults = maxResults; } } public void addField(String field) { fields.add(field); } public void addExpectedField(String field) { expectedFields.add(field); } public List<String> getExpectedFields() { return expectedFields; } public Criteria getCriteria() { return criteria; } public void setCriteria(Criteria criteria) { this.criteria = criteria; if (name != null) { createJoin(name); } if (where != null) { criteria.add(where); } } public String getJoinId() { return joinId; } public void setJoinId(String joinId) { this.joinId = joinId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getFields() { return fields; } public void setFields(List<String> fields) { this.fields = fields; } /** * @return the where */ public Criterion getWhere() { return where; } /** * @param where * the where to set */ public void setWhere(Criterion where) { this.where = where; } /** * @return the projections */ public ProjectionList getProjections() { return projections; } /** * @param projections * the projections to set */ public void setProjections(ProjectionList projections) { this.projections = projections; } private void createJoin(String join) { criteria.createCriteria(join, join); } public void applyProjections() { criteria.setProjection(projections); } /** * @return the entity */ public String getEntity() { return entity; } /** * @param entity * the entity to set */ public void setEntity(String entity) { this.entity = entity; } /** * Gets all the basic fields of the criteria entity * * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException * @throws IntrospectionException */ public void populateFields() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IntrospectionException { Class<?> clazz = Class.forName(entity); Method getter = clazz.getMethod("get" + Character.toUpperCase(name.charAt(0)) + name.substring(1)); Class<?> joined = (Class<?>) ((ParameterizedType) getter.getGenericReturnType()) .getActualTypeArguments()[0]; ClassMetadata metadata = session.getSessionFactory().getClassMetadata(joined); expectedFields.add(name + "." + metadata.getIdentifierPropertyName()); projections.add(Projections.property(name + "." + metadata.getIdentifierPropertyName())); for (String fieldName : metadata.getPropertyNames()) { Type type = metadata.getPropertyType(fieldName); if (!type.isAnyType() && !type.isAssociationType() && !type.isComponentType() && !type.isCollectionType()) { String field = name + "." + fieldName; expectedFields.add(field); projections.add(Projections.property(field)); } } } /** * @return the session */ public Session getSession() { return session; } /** * @param session * the session to set */ public void setSession(Session session) { this.session = session; } /** * Prepares the criteria for execution by adding an "in" clause using the * given ids and by setting the order for more efficient processing. * * @param ids */ public void prepare(Set<Object> ids) { this.criteria.add(Restrictions.in(joinId, ids)); if (this.orders == null || this.orders.isEmpty()) { this.criteria.addOrder(Order.asc(joinId)); } else { for (Order order : orders) { this.criteria.addOrder(order); } } } public void addOrder(String field, boolean ascending) { if (ascending) { orders.add(Order.asc(field)); } else { orders.add(Order.desc(field)); } } }