Java tutorial
/** * * APDPlat - Application Product Development Platform * Copyright (c) 2013, ??, yang-shangchuan@qq.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package org.apdplat.platform.dao; import org.apdplat.platform.common.DataPrivilegeControl; import org.apdplat.platform.criteria.Order; import org.apdplat.platform.criteria.OrderCriteria; import org.apdplat.platform.criteria.PageCriteria; import org.apdplat.platform.criteria.PropertyCriteria; import org.apdplat.platform.criteria.PropertyEditor; import org.apdplat.platform.criteria.Sequence; import org.apdplat.platform.log.APDPlatLogger; import org.apdplat.platform.model.Model; import org.apdplat.platform.result.Page; import org.apdplat.platform.util.ReflectionUtils; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.apache.commons.lang.StringUtils; import org.apdplat.platform.log.APDPlatLoggerFactory; import org.compass.core.Compass; import org.compass.core.CompassHighlighter; import org.compass.core.CompassHits; import org.compass.core.CompassSession; import org.compass.core.CompassTemplate; /** * DAO?? * @author ?? * */ public abstract class DaoSupport extends DataPrivilegeControl { protected final APDPlatLogger LOG = APDPlatLoggerFactory.getAPDPlatLogger(getClass()); protected static final OrderCriteria defaultOrderCriteria = new OrderCriteria(); static { defaultOrderCriteria.addOrder(new Order("id", Sequence.DESC)); } public DaoSupport(MultiDatabase multiDatabase) { super(multiDatabase); } @Resource(name = "compassTemplate") protected CompassTemplate compassTemplate; protected <T extends Model> Page<T> queryData(Class<T> modelClass, PageCriteria pageCriteria, PropertyCriteria propertyCriteria, OrderCriteria sortCriteria) { /* //?? User user=UserHolder.getCurrentLoginUser(); //??? if(user!=null && !user.isSuperManager() && needPrivilege(modelClass)){ user=em.find(User.class, user.getId()); Org org=user.getOrg(); List<Integer> child=OrgService.getChildIds(org); if(propertyCriteria==null){ propertyCriteria=new PropertyCriteria(); } //??? if(child.isEmpty()){ propertyCriteria.addPropertyEditor(new PropertyEditor("ownerUser.id", Operator.eq, user.getId())); } //?????? else{ PropertyEditor pe=new PropertyEditor(Criteria.or); pe.addSubPropertyEditor(new PropertyEditor("ownerUser.id", Operator.eq, user.getId())); //???? for(Integer orgID : child){ pe.addSubPropertyEditor(new PropertyEditor("ownerUser.org.id", Operator.eq,"Integer", orgID)); } propertyCriteria.addPropertyEditor(pe); } } * */ //?????jpql? StringBuilder jpql = new StringBuilder("select o from "); jpql.append(getEntityName(modelClass)).append(" o ").append(buildPropertyCriteria(propertyCriteria)) .append(buildOrderCriteria(sortCriteria)); LOG.debug("jpql:" + jpql); Query query = getEntityManager().createQuery(jpql.toString()); //? bindingPropertyCriteria(query, propertyCriteria); //???query? buildPageCriteria(pageCriteria, query); setQueryCache(query); Page<T> page = new Page<>(); List<T> models = query.getResultList(); if (models != null) { page.setModels(models); //???? page.setTotalRecords(getCount(modelClass, propertyCriteria)); } return page; } private void setQueryCache(Query query) { if (query instanceof org.hibernate.ejb.QueryImpl) { ((org.hibernate.ejb.QueryImpl) query).getHibernateQuery().setCacheable(true); } } private void bindingPropertyCriteria(Query query, PropertyCriteria propertyCriteria) { if (query != null && propertyCriteria != null) { List<PropertyEditor> propertyEditors = propertyCriteria.getPropertyEditors(); int len = propertyEditors.size(); for (int i = 0; i < len; i++) { PropertyEditor propertyEditor = propertyEditors.get(i); List<PropertyEditor> subPropertyEditor = propertyEditor.getSubPropertyEditor(); if (subPropertyEditor == null || subPropertyEditor.isEmpty()) { query.setParameter(propertyEditor.getProperty().getNameParameter(), propertyEditor.getProperty().getValue()); } else { binding(query, propertyEditor, 1); } } } } private void binding(Query query, PropertyEditor propertyEditor, int level) { List<PropertyEditor> subPropertyEditor = propertyEditor.getSubPropertyEditor(); int l = subPropertyEditor.size(); for (int j = 0; j < l; j++) { PropertyEditor p = subPropertyEditor.get(j); List<PropertyEditor> ss = p.getSubPropertyEditor(); if (ss == null || ss.isEmpty()) { query.setParameter(p.getProperty().getNameParameter() + "_" + level + "_" + j, p.getProperty().getValue()); } else { binding(query, p, ++level); } } } /** * ?? * @param pageCriteria * @param query */ private void buildPageCriteria(PageCriteria pageCriteria, Query query) { if (query != null && pageCriteria != null) { int firstindex = (pageCriteria.getPage() - 1) * pageCriteria.getSize(); int maxresult = pageCriteria.getSize(); query.setFirstResult(firstindex).setMaxResults(maxresult); } } /** * where ? * @param propertyCriteria * @return */ private <T extends Model> String buildPropertyCriteria(PropertyCriteria propertyCriteria) { StringBuilder wherejpql = new StringBuilder(""); String result = ""; if (propertyCriteria != null && propertyCriteria.getPropertyEditors().size() > 0) { //??? if (propertyCriteria.getCollection() != null && propertyCriteria.getObject() != null) { wherejpql.append(" join o.").append(propertyCriteria.getCollection()).append(" ") .append(propertyCriteria.getObject()); } wherejpql.append(" where "); List<PropertyEditor> propertyEditors = propertyCriteria.getPropertyEditors(); int len = propertyEditors.size(); for (int i = 0; i < len; i++) { PropertyEditor propertyEditor = propertyEditors.get(i); List<PropertyEditor> subPropertyEditor = propertyEditor.getSubPropertyEditor(); //???? if (subPropertyEditor == null || subPropertyEditor.isEmpty()) { //??? if (propertyCriteria.getCollection() != null && propertyCriteria.getObject() != null && propertyEditor.getProperty().getName().startsWith(propertyCriteria.getObject())) { wherejpql.append(" "); } else { wherejpql.append(" o."); } wherejpql.append(propertyEditor.getProperty().getName()).append(" ") .append(propertyEditor.getPropertyOperator().getSymbol()).append(" ").append(":") .append(propertyEditor.getProperty().getNameParameter()); if (i < len - 1) { wherejpql.append(" "); wherejpql.append(propertyCriteria.getCriteria().name()); wherejpql.append(" "); } } else { wherejpql.append(dealWithSubPropertyEditor(propertyEditor, 1)); } } result = wherejpql.toString(); } return result; } private String dealWithSubPropertyEditor(PropertyEditor propertyEditor, int level) { StringBuilder wherejpql = new StringBuilder(); List<PropertyEditor> subPropertyEditor = propertyEditor.getSubPropertyEditor(); wherejpql.append(" ( "); for (int j = 0; j < subPropertyEditor.size(); j++) { PropertyEditor sub = subPropertyEditor.get(j); List<PropertyEditor> ss = sub.getSubPropertyEditor(); //???? if (ss != null && !ss.isEmpty()) { wherejpql.append(dealWithSubPropertyEditor(sub, ++level)); } else { wherejpql.append(" o.").append(sub.getProperty().getName()).append(" ") .append(sub.getPropertyOperator().getSymbol()).append(" ").append(":") .append(sub.getProperty().getNameParameter()).append("_").append(level).append("_") .append(j); } if (j < subPropertyEditor.size() - 1) { wherejpql.append(" ").append(propertyEditor.getCriteria().name()).append(" "); } } wherejpql.append(" ) "); return wherejpql.toString(); } /** * order by? * @param sortCriteria * @return */ private String buildOrderCriteria(OrderCriteria sortCriteria) { StringBuilder orderbyql = new StringBuilder(""); if (sortCriteria != null && sortCriteria.getOrders().size() > 0) { orderbyql.append(" order by "); for (Order order : sortCriteria.getOrders()) { orderbyql.append("o.").append(order.getPropertyName()).append(" ") .append(order.getSequence().getValue()).append(","); } orderbyql.deleteCharAt(orderbyql.length() - 1); } return orderbyql.toString(); } /** * ???? * @param clazz * @param propertyCriteria * @return */ private Long getCount(Class<? extends Model> clazz, PropertyCriteria propertyCriteria) { Query query = getEntityManager().createQuery("select count(o.id) from " + getEntityName(clazz) + " o " + buildPropertyCriteria(propertyCriteria)); //? bindingPropertyCriteria(query, propertyCriteria); setQueryCache(query); return (Long) query.getSingleResult(); } public Long getCount(Class<? extends Model> clazz) { Query query = getEntityManager().createQuery("select count(o.id) from " + getEntityName(clazz) + " o "); return (Long) query.getSingleResult(); } public <T extends Model> Page<T> search(String queryString, PageCriteria pageCriteria, Class<T> modelClass) { List<T> result = new ArrayList<>(); Compass compass = compassTemplate.getCompass(); CompassSession session = compass.openSession(); CompassHits hits = session.find(queryString); LOG.info(":" + hits.getLength()); LOG.info(":" + queryString); if (pageCriteria != null) { int start = (pageCriteria.getPage() - 1) * pageCriteria.getSize(); int end = (pageCriteria.getPage() - 1) * pageCriteria.getSize() + pageCriteria.getSize(); if (end > hits.getLength()) { end = hits.getLength(); } for (int i = start; i < end; i++) { if (hits.data(i).getClass() == modelClass) { //T t=(T)hits.data(i); try { T t = hightlight(modelClass, hits, i); result.add(t); } catch (Exception e) { result.add((T) hits.data(i)); } } } } else { for (int i = 0; i < hits.getLength(); i++) { if (hits.data(i).getClass() == modelClass) { //T t=(T)hits.data(i); try { T t = hightlight(modelClass, hits, i); result.add(t); } catch (Exception e) { result.add((T) hits.data(i)); } } } } session.close(); //????,??? //Comparator comparter=new BeanComparator("id"); //Collections.sort(result, comparter); //Collections.reverse(result); //? Page<T> page = new Page<>(); page.setModels(result); page.setTotalRecords(hits.getLength()); return page; } private <T extends Model> T hightlight(Class<T> modelClass, CompassHits hits, int i) { T model = (T) hits.data(i); //?? model = getEntityManager().find(modelClass, model.getId()); //? getEntityManager().detach(model); CompassHighlighter highlighter = hits.highlighter(i); //? for (String searchProperty : model.getSearchProperties()) { try { String value = highlighter.fragment(searchProperty); if (StringUtils.isNotBlank(value)) { try { ReflectionUtils.setFieldValue(model, searchProperty, value); LOG.debug("?" + model.getMetaData() + " : " + model.getId() + "?" + searchProperty + "?"); } catch (Exception e) { LOG.debug("?" + model.getMetaData() + "?" + searchProperty + ",:?" + value + ""); } } } catch (Exception e) { LOG.debug("?" + searchProperty + ""); } } return model; } }