List of usage examples for org.hibernate.criterion DetachedCriteria setProjection
public DetachedCriteria setProjection(Projection projection)
From source file:org.n52.sos.ds.hibernate.GetFeatureOfInterestDAO.java
License:Open Source License
/** * Get Detached Criteria for series concept. Criteria results are * FeatureOfInterest entities./* w ww . ja v a2 s. co m*/ * * @param req * GetFeatureOfInterest request * @param session * Hibernate Sesstion * @return Detached Criteria * @throws CodedException If an error occurs during processing */ private DetachedCriteria getDetachedCriteriaForSeriesWithProcedureObservableProperty( GetFeatureOfInterestRequest req, Session session) throws CodedException { final DetachedCriteria detachedCriteria = DetachedCriteria .forClass(EntitiyHelper.getInstance().getSeriesEntityClass()); detachedCriteria.add(Restrictions.eq(Series.DELETED, false)); // observableProperties if (req.isSetObservableProperties()) { detachedCriteria.createCriteria(Series.OBSERVABLE_PROPERTY) .add(Restrictions.in(ObservableProperty.IDENTIFIER, req.getObservedProperties())); } // procedures if (req.isSetProcedures()) { detachedCriteria.createCriteria(Series.PROCEDURE) .add(Restrictions.in(Procedure.IDENTIFIER, req.getProcedures())); } detachedCriteria.setProjection(Projections.distinct(Projections.property(Series.FEATURE_OF_INTEREST))); return detachedCriteria; }
From source file:org.n52.sos.ds.hibernate.GetFeatureOfInterestDAO.java
License:Open Source License
/** * Get Detached Criteria for SOS 1.0.0 and series concept. Criteria results * are FeatureOfInterest entities.// w w w .j a v a 2 s. c om * * @param req * GetFeatureOfInterest request * @param session * Hibernate Sesstion * @return Detached Criteria * @throws OwsExceptionReport * If an error occurs during processing */ private DetachedCriteria getDetachedCriteriaForFeautreOfInterestForSeries(GetFeatureOfInterestRequest req, Session session) throws OwsExceptionReport { final DetachedCriteria detachedCriteria = DetachedCriteria .forClass(EntitiyHelper.getInstance().getSeriesEntityClass()); detachedCriteria.add(Subqueries.propertyIn(Series.ID, getDetachedCriteriaForSeriesWithProcedureObservablePropertyTemporalFilter(req, session))); detachedCriteria.setProjection(Projections.distinct(Projections.property(Series.FEATURE_OF_INTEREST))); return detachedCriteria; }
From source file:org.n52.sos.ds.hibernate.GetFeatureOfInterestDAO.java
License:Open Source License
/** * Get Detached Criteria for SOS 1.0.0 and series concept. Criteria results * are Series entities./*ww w. java 2s.c o m*/ * * @param req * GetFeatureOfInterest request * @param session * Hibernate Sesstion * @return Detached Criteria * @throws CodedException If an error occurs during processing */ private DetachedCriteria getDetachedCriteriaForSeriesWithProcedureObservablePropertyTemporalFilter( GetFeatureOfInterestRequest req, Session session) throws CodedException { final DetachedCriteria detachedCriteria = DetachedCriteria .forClass(EntitiyHelper.getInstance().getObservationInfoEntityClass()); DetachedCriteria seriesCriteria = detachedCriteria.createCriteria(SeriesObservationInfo.SERIES); detachedCriteria.add(Restrictions.eq(Series.DELETED, false)); // observableProperties if (req.isSetObservableProperties()) { seriesCriteria.createCriteria(Series.OBSERVABLE_PROPERTY) .add(Restrictions.in(ObservableProperty.IDENTIFIER, req.getObservedProperties())); } // procedures if (req.isSetProcedures()) { seriesCriteria.createCriteria(Series.PROCEDURE) .add(Restrictions.in(Procedure.IDENTIFIER, req.getProcedures())); } // temporal filter if (req.isSetTemporalFilters()) { detachedCriteria.add(TemporalRestrictions.filter(req.getTemporalFilters())); } detachedCriteria.setProjection(Projections.distinct(Projections.property(SeriesObservationInfo.SERIES))); return detachedCriteria; }
From source file:org.onecmdb.core.internal.storage.expression.OneCMDBExpression.java
License:Open Source License
protected DetachedCriteria getAttributeSearch(DetachedCriteria ciCrit) { if (getTextMatch() != null) { DetachedCriteria attribute = DetachedCriteria.forClass(BasicAttribute.class); //ciCrit.setProjection(); Disjunction orAttribute = Restrictions.disjunction(); boolean orAttributeAdded = false; if (isTextMatchAlias()) { orAttribute.add(Expression.ilike("alias", getTextMatch(), MatchMode.ANYWHERE)); orAttributeAdded = true;//from w w w .j a va 2s . c om } if (isTextMatchDescription()) { orAttribute.add(Expression.ilike("description", getTextMatch(), MatchMode.ANYWHERE)); orAttributeAdded = true; } if (isTextMatchValue()) { orAttribute.add(Expression.ilike("valueAsString", getTextMatch(), MatchMode.ANYWHERE)); orAttributeAdded = true; } if (orAttributeAdded) { DetachedCriteria ciIdCriteria = ciCrit.setProjection(Projections.property("longId")); DetachedCriteria attributeCriteira = DetachedCriteria.forClass(BasicAttribute.class); attributeCriteira.add(Property.forName("ownerId").in(ciIdCriteria)); attributeCriteira.add(orAttribute); return (attributeCriteira); } } return (null); }
From source file:org.onecmdb.core.internal.storage.expression.OneCMDBExpression.java
License:Open Source License
protected DetachedCriteria addCISearch(DetachedCriteria ciCrit) { if (getTextMatch() != null) { Disjunction orCi = Restrictions.disjunction(); boolean orCiAdded = false; if (isTextMatchAlias()) { orCi.add(Expression.ilike("alias", getTextMatch(), MatchMode.ANYWHERE)); orCiAdded = true;/*from w w w .ja v a 2 s .c om*/ } if (isTextMatchDescription()) { orCi.add(Expression.ilike("description", getTextMatch(), MatchMode.ANYWHERE)); orCiAdded = true; } if (orCiAdded) { DetachedCriteria attributeCriteria = getAttributeSearch(ciCrit); DetachedCriteria attrOwnerIdCriteria = attributeCriteria .setProjection(Projections.property("ownerId")); orCi.add(Property.forName("longId").in(attrOwnerIdCriteria)); DetachedCriteria selectSearchCi = DetachedCriteria.forClass(ConfigurationItem.class); selectSearchCi.add(orCi); return (selectSearchCi); } } return (ciCrit); }
From source file:org.onecmdb.core.internal.storage.expression.RelationExpression.java
License:Open Source License
@Override protected DetachedCriteria getCriteria() { DetachedCriteria attrTarget = DetachedCriteria.forClass(BasicAttribute.class); attrTarget.add(Expression.eq("valueAsLong", targetId)); attrTarget.add(Expression.eq("alias", "target")); DetachedCriteria targetOwnerCrit = attrTarget.setProjection(Projections.property("ownerId")); DetachedCriteria ciRelation = DetachedCriteria.forClass(ConfigurationItem.class); ciRelation.add(Property.forName("longId").in(targetOwnerCrit)); return (ciRelation); }
From source file:org.onecmdb.core.internal.storage.expression.SourceRelationExpression.java
License:Open Source License
public DetachedCriteria getCriteria() { // Select Target/Source attribute id. DetachedCriteria attrTarget = DetachedCriteria.forClass(BasicAttribute.class); attrTarget.add(Expression.eq("valueAsLong", targetId)); attrTarget.add(Expression.eq("alias", "target")); DetachedCriteria targetOwnerCrit = attrTarget.setProjection(Projections.property("ownerId")); DetachedCriteria attrSource = DetachedCriteria.forClass(BasicAttribute.class); attrSource.add(Property.forName("ownerId").in(targetOwnerCrit)); attrSource.add(Expression.eq("alias", "source")); DetachedCriteria sourceIdCrit = attrSource.setProjection(Projections.property("valueAsLong")); //DetachedCriteria sourceCi = DetachedCriteria.forClass(BasicAttribute.class); DetachedCriteria sourceCi = DetachedCriteria.forClass(ConfigurationItem.class); if (this.sourceTemplateId != null) { sourceCi.add(Expression.eq("derivedFromId", sourceTemplateId)); } else if (this.sourceTemplatePath != null) { sourceCi.add(Expression.ilike("templatePath", sourceTemplatePath + "/%")); }/* w w w . ja v a 2 s. c o m*/ sourceCi.add(Property.forName("longId").in(sourceIdCrit)); return (sourceCi); }
From source file:org.onecmdb.core.internal.storage.expression.SourceTemplateRelationExpression.java
License:Open Source License
@Override protected DetachedCriteria getCriteria() { // Select Target/Source attribute id. DetachedCriteria attrTarget = DetachedCriteria.forClass(BasicAttribute.class); attrTarget.add(Expression.eq("valueAsLong", targetId)); attrTarget.add(Expression.eq("alias", "target")); DetachedCriteria relOwnerCrit = attrTarget.setProjection(Projections.property("ownerId")); DetachedCriteria relCi = DetachedCriteria.forClass(ConfigurationItem.class); relCi.add(Property.forName("longId").in(relOwnerCrit)); relCi.add(Expression.eq("derivedFromId", referenceTemplateId)); DetachedCriteria targetOwnerCrit = relCi.setProjection(Projections.property("longId")); DetachedCriteria attrSource = DetachedCriteria.forClass(BasicAttribute.class); attrSource.add(Expression.eq("alias", "source")); attrSource.add(Property.forName("ownerId").in(targetOwnerCrit)); DetachedCriteria sourceIdCrit = attrSource.setProjection(Projections.property("valueAsLong")); DetachedCriteria sourceCi = DetachedCriteria.forClass(ConfigurationItem.class); sourceCi.add(Property.forName("longId").in(sourceIdCrit)); return (sourceCi); }
From source file:org.onecmdb.core.internal.storage.expression.TemplateRelationExpression.java
License:Open Source License
@Override protected DetachedCriteria getCriteria() { DetachedCriteria relCrit = super.getCriteria(); DetachedCriteria instanceCrit = relCrit.setProjection(Projections.property("derivedFromId")); DetachedCriteria templateRelation = DetachedCriteria.forClass(ConfigurationItem.class); templateRelation.add(Property.forName("longId").in(instanceCrit)); return (templateRelation); }
From source file:org.onecmdb.core.internal.storage.hibernate.HibernateDao.java
License:Open Source License
public QueryResult<ICi> query(QueryCriteria criteria, boolean count) { DetachedCriteria hibCiCriteria = DetachedCriteria.forClass(ConfigurationItem.class); DetachedCriteria hibAttributeCriteria = DetachedCriteria.forClass(BasicAttribute.class); // Search in the db... if (criteria.getOffspringOfId() != null) { try {/*from w w w. j ava 2 s.c om*/ // Query for an unique id. Long longId = Long.parseLong(criteria.getOffspringOfId()); hibCiCriteria.add(Expression.eq("derivedFromId", longId)); } catch (NumberFormatException e) { log.warn("QueryCriteria contained not a long offspringId <" + criteria.getCiId()); throw new IllegalArgumentException("Not a correct long ci id <" + criteria.getCiId()); } } else if (criteria.getOffspringOfAlias() != null) { ICi ci = findCiByAlias(new Path<String>(criteria.getOffspringOfAlias())); if (criteria.getOffspringDepth() != null) { if (ci == null) { // Is an error, but we don't throw an exception, instead it will return empty/0 DetachedCriteria hibAliasCiCriteria = DetachedCriteria.forClass(ConfigurationItem.class); hibAliasCiCriteria.add(Expression.eq("alias", criteria.getOffspringOfAlias())); DetachedCriteria idCriteria = hibAliasCiCriteria.setProjection(Projections.property("longId")); hibCiCriteria.add(Property.forName("derivedFromId").in(idCriteria)); } else { // TODO: append %/%/% according to offspring depth. hibCiCriteria.add(Expression.ilike("templatePath", ci.getTemplatePath() + "/%")); } } else { if (ci != null) { hibCiCriteria.add(Expression.eq("derivedFromId", ci.getId().asLong())); } else { hibCiCriteria.add(Expression.eq("derivedFromId", new Long(0))); } } //hibAttributeCriteria.add(Expression.eq("alias", criteria.getOffspringOfAlias())); } if (criteria.getCiAlias() != null) { hibCiCriteria.add(Expression.eq("alias", criteria.getCiAlias())); } else if (criteria.getCiId() != null) { try { // Query for an unique id. Long longId = Long.parseLong(criteria.getCiId()); hibCiCriteria.add(Expression.eq("longId", longId)); } catch (NumberFormatException e) { log.warn("QueryCriteria contained not a long ci id <" + criteria.getCiId()); throw new IllegalArgumentException("Not a correct long ci id <" + criteria.getCiId()); } /* if (ci == null || ci instanceof IAttribute) { if (count) { result.setTotalHits(0); } } else { if (count) { result.setTotalHits(1); } result.add(ci); } return(result); */ } if (criteria.getMatchType() != null) { ICi type = findCiByAlias(new Path<String>(criteria.getMatchType())); if (type != null) { Disjunction orAttribute = Restrictions.disjunction(); String path = type.getTemplatePath(); String paths[] = path.split("/"); if (paths.length > 1) { for (int i = 1; i < paths.length; i++) { orAttribute.add(Expression.ilike("typeName", "%#" + paths[i], MatchMode.START)); } DetachedCriteria typeCrit = DetachedCriteria.forClass(BasicAttribute.class); typeCrit.add(Expression.isNull("derivedFromId")); typeCrit.add(orAttribute); DetachedCriteria idCrit = typeCrit.setProjection(Projections.property("ownerId")); hibCiCriteria.add(Property.forName("longId").in(idCrit)); if (criteria.getMatchCiPath() != null) { String idPath = ""; String ciPath[] = criteria.getMatchCiPath().split("/"); if (ciPath.length > 0) { for (int i = 0; i < ciPath.length; i++) { ICi ci = findCiByAlias(new Path<String>(ciPath[i])); if (ci != null) { idPath += "/" + ci.getId().asLong(); } } // TODO: append %/%/% according to offspring depth. hibCiCriteria.add(Expression.ilike("templatePath", idPath + "/%")); } } } } } if (criteria.isMatchCiTemplates() && criteria.isMatchCiInstances()) { // Search Both. } else if (criteria.isMatchCiTemplates()) { hibCiCriteria.add(Expression.eq("isBlueprint", Boolean.TRUE)); } else if (criteria.isMatchCiInstances()) { hibCiCriteria.add(Expression.eq("isBlueprint", Boolean.FALSE)); } if (criteria.isMatchAttributeTemplates() && criteria.isMatchAttributeInstances()) { // Search both } else if (criteria.isMatchAttributeTemplates()) { hibAttributeCriteria.add(Expression.eq("isBlueprint", Boolean.TRUE)); } else if (criteria.isMatchAttributeInstances()) { hibAttributeCriteria.add(Expression.eq("isBlueprint", Boolean.FALSE)); } if (criteria.getText() != null) { Disjunction orAttribute = Restrictions.disjunction(); Disjunction orCi = Restrictions.disjunction(); boolean orAttributeAdded = false; boolean orCiAdded = false; if (criteria.isTextMatchAlias()) { orCi.add(Expression.ilike("alias", criteria.getText(), MatchMode.ANYWHERE)); orAttribute.add(Expression.ilike("alias", criteria.getText(), MatchMode.ANYWHERE)); orAttributeAdded = true; orCiAdded = true; } if (criteria.isTextMatchDescription()) { orCi.add(Expression.ilike("description", criteria.getText(), MatchMode.ANYWHERE)); orAttribute.add(Expression.ilike("description", criteria.getText(), MatchMode.ANYWHERE)); orAttributeAdded = true; orCiAdded = true; } if (criteria.isTextMatchValue()) { orAttribute.add(Expression.ilike("valueAsString", criteria.getText(), MatchMode.ANYWHERE)); orAttributeAdded = true; // Enable Attribute serach.... criteria.setMatchAttribute(true); } /* DetachedCriteria idCriteria = hibAttributeCriteria.setProjection(Projections.property("ownerId")); orCi.add(Property.forName("longId").in(idCriteria)); */ if (orAttributeAdded) { if (criteria.getMatchAttributeAlias() != null) { hibAttributeCriteria.add(Expression.eq("alias", criteria.getMatchAttributeAlias())); } hibAttributeCriteria.add(orAttribute); DetachedCriteria idCriteria = hibAttributeCriteria.setProjection(Projections.property("ownerId")); orCi.add(Property.forName("longId").in(idCriteria)); orCiAdded = true; } if (orCiAdded) { hibCiCriteria.add(orCi); } } QueryResult<ICi> result = new QueryResult<ICi>(); /* if (criteria.isMatchAttribute()) { DetachedCriteria idCriteria = hibAttributeCriteria.setProjection(Projections.property("ownerId")); hibCiCriteria.add(Property.forName("longId").in(idCriteria)); } */ // Search ICi. Session session = getSession(); try { Profiler.start("QueryCi():"); if (count) { Criteria hibCriteria = hibCiCriteria.getExecutableCriteria(session); hibCriteria.setProjection(Projections.rowCount()); List list = hibCriteria.list(); if (list != null && !list.isEmpty()) { Integer itemCount = ((Integer) list.get(0)).intValue(); result.setTotalHits(itemCount); } } else { if (criteria.getOrderAttAlias() != null) { DetachedCriteria idCriteria = hibCiCriteria.setProjection(Projections.property("longId")); DetachedCriteria attr = DetachedCriteria.forClass(BasicAttribute.class); attr.add(Expression.eq("alias", criteria.getOrderAttAlias())); attr.add(Property.forName("ownerId").in(idCriteria)); if (criteria.isOrderAscending()) { attr.addOrder(Order.asc(criteria.getOrderType())); } else { attr.addOrder(Order.desc(criteria.getOrderType())); } Criteria attrCriteria = attr.getExecutableCriteria(session); if (criteria.getMaxResult() != null) { attrCriteria.setMaxResults(criteria.getMaxResult()); } if (criteria.getFirstResult() != null) { attrCriteria.setFirstResult(criteria.getFirstResult()); } List<IAttribute> attrs = attrCriteria.list(); for (IAttribute a : attrs) { result.add(a.getOwner()); } } else { hibCiCriteria.addOrder(Order.asc("alias")); Criteria hibCriteria = hibCiCriteria.getExecutableCriteria(session); if (criteria.getMaxResult() != null) { hibCriteria.setMaxResults(criteria.getMaxResult()); } if (criteria.getFirstResult() != null) { hibCriteria.setFirstResult(criteria.getFirstResult()); } List objects = hibCriteria.list(); result.addAll(objects); } } } finally { Profiler.stop(); closeSession(session); } return (result); }