001: package org.osbl.persistence.hibernate;
002:
003: import org.osbl.persistence.*;
004: import org.hibernate.*;
005: import org.hibernate.criterion.Restrictions;
006: import org.hibernate.criterion.Order;
007:
008: import java.util.*;
009:
010: public class HibernateJoinedQueryCommand<T> extends
011: JoinedQueryCommand<T> implements HibernateCommand {
012: SessionFactory sessionFactory;
013:
014: public void setSessionFactory(SessionFactory sessionFactory) {
015: this .sessionFactory = sessionFactory;
016: }
017:
018: public List<T> execute() {
019: Session session = sessionFactory.getCurrentSession();
020:
021: Map<String, Criteria> criterias = new HashMap<String, Criteria>();
022: Criteria criteria = session.createCriteria(type);
023: for (Map.Entry<String, String> entry : joins.entrySet()) {
024: Criteria subCriteria = criteria.createCriteria(entry
025: .getKey(), entry.getValue());
026: criterias.put(entry.getValue(), subCriteria);
027: }
028:
029: for (Filter filter : filters) {
030: String property = filter.getProperty();
031: Object value = filter.getValue();
032:
033: Criteria subCriteria;
034: int pos = property.lastIndexOf('.');
035: if (pos != -1) {
036: subCriteria = criterias.get(property.substring(0, pos));
037: property = property.substring(pos + 1);
038: } else
039: subCriteria = criteria;
040:
041: switch (filter.getOperator()) {
042: case EQUAL:
043: subCriteria.add(Restrictions.eq(property, value));
044: break;
045: case NOT_EQUAL:
046: subCriteria.add(Restrictions.ne(property, value));
047: break;
048: case GREATER_OR_EQUAL:
049: subCriteria.add(Restrictions.ge(property, value));
050: break;
051: case GREATER_THAN:
052: subCriteria.add(Restrictions.gt(property, value));
053: break;
054: case LESS_OR_EQUAL:
055: subCriteria.add(Restrictions.le(property, value));
056: break;
057: case LESS_THAN:
058: subCriteria.add(Restrictions.lt(property, value));
059: break;
060: case LIKE:
061: subCriteria.add(Restrictions.like(property, value));
062: break;
063: case LIKE_IGNORE_CASE:
064: subCriteria.add(Restrictions.ilike(property, value));
065: break;
066: case NULL:
067: subCriteria.add(Restrictions.isNull(property));
068: break;
069: case NOT_NULL:
070: subCriteria.add(Restrictions.isNotNull(property));
071: break;
072: case IN:
073: criteria.add(Restrictions.in(filter.getProperty(),
074: (Collection) filter.getValue()));
075: break;
076: case NOT_IN:
077: criteria.add(Restrictions
078: .not(Restrictions.in(filter.getProperty(),
079: (Collection) filter.getValue())));
080: break;
081: }
082: }
083:
084: for (Map.Entry<String, Sort> entry : sorts.entrySet()) {
085: switch (entry.getValue()) {
086: case ASCENDING:
087: criteria.addOrder(Order.asc(entry.getKey()));
088: break;
089: case DESCENDING:
090: criteria.addOrder(Order.desc(entry.getKey()));
091: break;
092: }
093: }
094:
095: for (Map.Entry<String, Fetch> entry : fetches.entrySet()) {
096: switch (entry.getValue()) {
097: case JOIN:
098: criteria.setFetchMode(entry.getKey(), FetchMode.JOIN);
099: break;
100: case SELECT:
101: criteria.setFetchMode(entry.getKey(), FetchMode.SELECT);
102: break;
103: }
104: }
105: if (fetches.size() > 0)
106: criteria
107: .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
108:
109: return (List<T>) criteria.list();
110: }
111: }
|