List of usage examples for org.hibernate.criterion Projections id
public static IdentifierProjection id()
From source file:org.rebioma.server.services.OccurrenceDbImpl.java
License:Apache License
private List<Integer> findIds(OccurrenceQuery query, Set<OccurrenceFilter> filters, User user, int tryCount) throws Exception { log.debug("finding Occurrence instances by query."); try {/*from ww w . ja v a2 s . com*/ Session session = ManagedSession.createNewSessionAndTransaction(); List<Integer> results = null; Criteria criteria = session.createCriteria(Occurrence.class).setProjection(Projections.id()); OccurrenceFilter userReviewFilter = null; for (OccurrenceFilter filter : filters) { if (filter.column.equals(filter.getPropertyName("userReviewed"))) { userReviewFilter = filter; } } filters.remove(userReviewFilter); List<OrderKey> orderingMap = query.getOrderingMap(); for (OrderKey orderKey : orderingMap) { String property = orderKey.getAttributeName(); if (orderKey.isAsc()) { //criteria.addOrder(Order.asc(getOccurrencePropertyName(property))); } else { //criteria.addOrder(Order.desc(getOccurrencePropertyName(property))); } } // Sets the start, limit, and order by accepted species: criteria.setFirstResult(query.getStart()); if (query.getLimit() != OccurrenceQuery.UNLIMITED) { criteria.setMaxResults(query.getLimit()); } OccurrenceFilter idsFilter = null; if (userReviewFilter != null) { Boolean reviewed = null; if (userReviewFilter.operator == Operator.EQUAL) { reviewed = (Boolean) userReviewFilter.getValue(); } List<Integer> occIds = recordReviewDb.getRecordReviewOccIds(user.getId(), reviewed); System.out.println(occIds.size()); if (occIds.isEmpty()) { occIds.add(0); } idsFilter = new OccurrenceFilter("id", Operator.IN, occIds); filters.add(idsFilter); } log.info("find filters: " + addCreterionByFilters(criteria, user, filters, query.getResultFilter(), tryCount)); if (userReviewFilter != null) { filters.remove(idsFilter); filters.add(userReviewFilter); } results = criteria.list(); if (query.isCountTotalResults()) { criteria.setFirstResult(0); criteria.setProjection(Projections.count("id")); Integer count = (Integer) criteria.uniqueResult(); if (count != null) { query.setCount(count); } } else { query.setCount(-1); } // filters.addAll(removedFilters); log.debug("find by example successful, result size: " + results.size()); ManagedSession.commitTransaction(session); return results; } catch (RuntimeException re) { log.error("find by example failed", re); re.printStackTrace(); throw re; } catch (Exception e) { log.error("unexpected error: ", e); e.printStackTrace(); throw e; } }
From source file:org.shredzone.cilla.ws.assembler.CategoryAssembler.java
License:Open Source License
@Override public ProjectionList projection() { ProjectionList projection = Projections.projectionList(); projection.add(Projections.id(), "id"); projection.add(Property.forName("name").as("name")); projection.add(Property.forName("title").as("title")); projection.add(Property.forName("icon").as("icon")); projection.add(Property.forName("caption.text").as("caption")); projection.add(Property.forName("caption.format").as("captionFormat")); return projection; }
From source file:org.shredzone.cilla.ws.assembler.CommentAssembler.java
License:Open Source License
@Override public ProjectionList projection() { ProjectionList projection = Projections.projectionList(); projection.add(Projections.id(), "id"); projection.add(Property.forName("name").as("name")); projection.add(Property.forName("mail").as("mail")); projection.add(Property.forName("url").as("url")); projection.add(Property.forName("text.text").as("text")); projection.add(Property.forName("text.format").as("textFormat")); projection.add(Property.forName("creation").as("creation")); projection.add(Property.forName("published").as("published")); projection.add(Property.forName("replyTo.id").as("replyToId")); projection.add(Property.forName("t.id").as("threadId")); projection.add(Property.forName("c.id").as("creatorId")); projection.add(Property.forName("c.login").as("creatorLogin")); projection.add(Property.forName("c.name").as("creatorName")); return projection; }
From source file:org.shredzone.cilla.ws.assembler.HeaderAssembler.java
License:Open Source License
@Override public ProjectionList projection() { ProjectionList projection = Projections.projectionList(); projection.add(Projections.id(), "id"); projection.add(Property.forName("name").as("name")); projection.add(Property.forName("caption").as("caption")); projection.add(Property.forName("description.text").as("description")); projection.add(Property.forName("description.format").as("descriptionFormat")); projection.add(Property.forName("creation").as("creation")); projection.add(Property.forName("enabled").as("enabled")); projection.add(Property.forName("location.longitude").as("longitude")); projection.add(Property.forName("location.latitude").as("latitude")); projection.add(Property.forName("location.altitude").as("altitude")); projection.add(Property.forName("t.commentable").as("commentable")); projection.add(Property.forName("c.id").as("creatorId")); projection.add(Property.forName("c.login").as("creatorLogin")); projection.add(Property.forName("c.name").as("creatorName")); return projection; }
From source file:org.shredzone.cilla.ws.assembler.LanguageAssembler.java
License:Open Source License
@Override public ProjectionList projection() { ProjectionList projection = Projections.projectionList(); projection.add(Projections.id(), "id"); projection.add(Property.forName("name").as("name")); projection.add(Property.forName("locale").as("locale")); return projection; }
From source file:org.shredzone.cilla.ws.assembler.PageAssembler.java
License:Open Source License
/** * {@inheritDoc}//from w w w . ja v a 2s. c o m * <p> * Only creates a projection for {@link PageInfoDto}. */ @Override public ProjectionList projection() { ProjectionList projection = Projections.projectionList(); projection.add(Projections.id(), "id"); projection.add(Property.forName("name").as("name")); projection.add(Property.forName("title").as("title")); projection.add(Property.forName("subject").as("subject")); projection.add(Property.forName("creation").as("creation")); projection.add(Property.forName("modification").as("modification")); projection.add(Property.forName("publication").as("publication")); projection.add(Property.forName("expiration").as("expiration")); projection.add(Property.forName("sticky").as("sticky")); projection.add(Property.forName("hidden").as("hidden")); projection.add(Property.forName("donatable").as("donatable")); projection.add(Property.forName("promoted").as("promoted")); projection.add(Property.forName("c.id").as("creatorId")); projection.add(Property.forName("c.login").as("creatorLogin")); projection.add(Property.forName("c.name").as("creatorName")); projection.add(Property.forName("t.commentable").as("commentable")); projection.add(Property.forName("language.id").as("languageId")); return projection; }
From source file:org.shredzone.cilla.ws.assembler.UserAssembler.java
License:Open Source License
@Override public ProjectionList projection() { ProjectionList projection = Projections.projectionList(); projection.add(Projections.id(), "id"); projection.add(Property.forName("login").as("login")); projection.add(Property.forName("name").as("name")); projection.add(Property.forName("mail").as("mail")); projection.add(Property.forName("timeZone").as("timeZone")); projection.add(Property.forName("r.name").as("roleName")); projection.add(Property.forName("l.id").as("languageId")); projection.add(Property.forName("l.locale").as("language")); return projection; }
From source file:org.tonguetied.keywordmanagement.KeywordRepositoryImpl.java
License:Apache License
public PaginatedList<Keyword> findKeywords(Keyword keyword, final boolean ignoreCase, final Order order, final Integer firstResult, final Integer maxResults) throws IllegalArgumentException { if (keyword == null) { throw new IllegalArgumentException("keyword cannot be null"); }//from w w w . ja v a2 s .co m final MatchMode matchMode = MatchMode.ANYWHERE; Example criterionKeyword = Example.create(keyword); criterionKeyword.enableLike(matchMode); if (ignoreCase) { criterionKeyword.ignoreCase(); } // Normally, Hibernate performs a left-outer join, when searching for // an object with collections using Criteria. This returns a ResultSet // that contains duplicate objects. In order to get a unique list of // Keywords with paginated support, we need to a nested query to find // distinct matching ids, then get the Keywords. The end result is a // subselect in the main query, but only one query is sent. DetachedCriteria dc = DetachedCriteria.forClass(Keyword.class); dc.add(criterionKeyword); dc.setResultTransformer(DISTINCT_ROOT_ENTITY); Conjunction conjunction = createTranslationConditions(keyword.getTranslations(), ignoreCase, matchMode); if (conjunction != null) dc.createCriteria(FIELD_TRANSLATIONS).add(conjunction); dc.setProjection(Projections.id()); Criteria criteria = getSession().createCriteria(Keyword.class); criteria.add(Subqueries.propertyIn(FIELD_ID, dc)); if (Order.desc == order) criteria.addOrder(desc(FIELD_KEYWORD)); else criteria.addOrder(asc(FIELD_KEYWORD)); if (firstResult != null) criteria.setFirstResult(firstResult); if (maxResults != null) criteria.setMaxResults(maxResults); final List<Keyword> criteriaList = criteria.list(); int maxListSize = 0; if (criteriaList.size() > 0) { maxListSize = calculateMaxListSize(criterionKeyword, conjunction); } return new PaginatedList<Keyword>(criteriaList, maxListSize); }
From source file:ro.cs.om.model.dao.impl.DaoOOOImpl.java
License:Open Source License
/** * Searches for out of office profiles after criterion from searchOOOBean * @author alu//from w w w.j a v a 2s. co m * @return A list of ooo beans * @throws ParseException */ public List getOOOBeanFromSearch(SearchOOOBean searchOOOBean, boolean isDeleteAction) throws ParseException { logger.debug("getOOOBeanFromSearch - START"); /*Once a Projection is being set to a Detached Criteria object, it cannot be removed anymore, so two identical DetachedCriteria objects must be created: -dcCount ( on which the projection is being set )used to retrieve the number of distinct results which is set when the request didn't come from the pagination area and needed further more to set the current page after a delete action; -dc used to retrieve the result set after the current page has been set in case of a delete action */ // set search criterion DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.oooAllEntity); DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.oooAllEntity); dc.createAlias("person", "person"); dcCount.createAlias("person", "person"); if (searchOOOBean.getOrganisationId() != -1) { dc.createCriteria("person.depts") .add(Restrictions.eq("organisationId", searchOOOBean.getOrganisationId())); dcCount.createCriteria("person.depts") .add(Restrictions.eq("organisationId", searchOOOBean.getOrganisationId())); } if (searchOOOBean.getOwnerFirstName() != null && !"".equals(searchOOOBean.getOwnerFirstName())) { dc.add(Restrictions.eq("person.firstName", searchOOOBean.getOwnerFirstName())); dcCount.add(Restrictions.eq("person.firstName", searchOOOBean.getOwnerFirstName())); logger.debug("Owner first name: ".concat(searchOOOBean.getOwnerFirstName())); } if (searchOOOBean.getOwnerLastName() != null && !"".equals(searchOOOBean.getOwnerLastName())) { dc.add(Restrictions.eq("person.lastName", searchOOOBean.getOwnerLastName())); dcCount.add(Restrictions.eq("person.lastName", searchOOOBean.getOwnerLastName())); logger.debug("Owner last name: ".concat(searchOOOBean.getOwnerLastName())); } if (searchOOOBean.getReplacementFirstName() != null && !"".equals(searchOOOBean.getReplacementFirstName()) && searchOOOBean.getReplacementLastName() != null && !"".equals(searchOOOBean.getReplacementLastName())) { dc.createCriteria("personReplacement") .add(Restrictions.eq("firstName", searchOOOBean.getReplacementFirstName())) .add(Restrictions.eq("lastName", searchOOOBean.getReplacementLastName())); dcCount.createCriteria("personReplacement") .add(Restrictions.eq("firstName", searchOOOBean.getReplacementFirstName())) .add(Restrictions.eq("lastName", searchOOOBean.getReplacementLastName())); } if (searchOOOBean.getStartPeriod() != null) { dc.add(Expression.ge("startPeriod", searchOOOBean.getStartPeriod())); dcCount.add(Expression.ge("startPeriod", searchOOOBean.getStartPeriod())); } if (searchOOOBean.getEndPeriod() != null) { dc.add(Expression.le("endPeriod", searchOOOBean.getEndPeriod())); dcCount.add(Expression.le("endPeriod", searchOOOBean.getEndPeriod())); } dc.setProjection(Projections.id()); dcCount.setProjection(Projections.id()); // until here, I've created the subquery // now, it's time to retrive all the profiles that are in the list of the subquery DetachedCriteria dc1 = DetachedCriteria.forEntityName(IModelConstant.oooAllEntity); dc1.createAlias("person", "person"); dc1.add(Subqueries.propertyIn("outOfOfficeId", dc)); // check if I have to order the results if (searchOOOBean.getSortParam() != null && !"".equals(searchOOOBean.getSortParam())) { logger.debug("Add sorting ! 234234"); // if I have to, check if I have to order them ascending or descending if (searchOOOBean.getSortDirection() == -1) { // ascending dc1.addOrder(Order.asc(searchOOOBean.getSortParam())); } else { // descending dc1.addOrder(Order.desc(searchOOOBean.getSortParam())); } } // if the request didn't come from the pagination area, // it means that I have to set the number of results and pages if (isDeleteAction || searchOOOBean.getNbrOfResults() == -1) { boolean isSearch = false; if (searchOOOBean.getNbrOfResults() == -1) { isSearch = true; } // set the count(*) restriction dcCount.setProjection(Projections.countDistinct("outOfOfficeId")); //findByCriteria must be called with firstResult and maxResults parameters; the default findByCriteria(DetachedCriteria criteria) implementation //sets firstResult and maxResults to -1, which kills the countDistinct Projection int nbrOfResults = ((Integer) getHibernateTemplate().findByCriteria(dcCount, 0, 0).get(0)).intValue(); logger.debug("search results: ".concat(String.valueOf(nbrOfResults))); searchOOOBean.setNbrOfResults(nbrOfResults); // get the number of pages if (nbrOfResults % searchOOOBean.getResultsPerPage() == 0) { searchOOOBean.setNbrOfPages(nbrOfResults / searchOOOBean.getResultsPerPage()); } else { searchOOOBean.setNbrOfPages(nbrOfResults / searchOOOBean.getResultsPerPage() + 1); } // after an ooo profile is deleted, the same page has to be displayed; //only when all the ooo profiles from last page are deleted, the previous page will be shown if (isDeleteAction && (searchOOOBean.getCurrentPage() > searchOOOBean.getNbrOfPages())) { searchOOOBean.setCurrentPage(searchOOOBean.getNbrOfPages()); } else if (isSearch) { searchOOOBean.setCurrentPage(1); } } List res = getHibernateTemplate().findByCriteria(dc1, (searchOOOBean.getCurrentPage() - 1) * searchOOOBean.getResultsPerPage(), searchOOOBean.getResultsPerPage()); logger.debug("getOOOBeanFromSearch - END results size : ".concat(String.valueOf(res.size()))); return res; }
From source file:ro.cs.om.model.dao.impl.DaoPersonImpl.java
License:Open Source License
/** * Searches for persons after criterion from searchPersonBean. * /*from w ww .j av a2 s. c om*/ * @author alu * * @return A list of log beans * @throws ParseException */ public List<Person> getFromSearch(SearchPersonBean searchPersonBean, boolean isDeleteAction, List<Integer> orgs) throws ParseException { logger.debug("getFromSearch - START"); /*Once a Projection is being set to a Detached Criteria object, it cannot be removed anymore, so two identical DetachedCriteria objects must be created: -dcCount ( on which the projection is being set )used to retrieve the number of distinct results which is set when the request didn't come from the pagination area and needed further more to set the current page after a delete action; -dc used to retrieve the result set after the current page has been set in case of a delete action */ // set search criterion DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity); DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity); // Status dc.add(Restrictions.ne("status", IConstant.NOM_PERSON_STATUS_DELETED)); dcCount.add(Restrictions.ne("status", IConstant.NOM_PERSON_STATUS_DELETED)); if (Tools.getInstance().stringNotEmpty(searchPersonBean.getFirstName())) { dc.add(Restrictions.ilike("firstName", "%".concat(searchPersonBean.getFirstName()).concat("%"))); dcCount.add(Restrictions.ilike("firstName", "%".concat(searchPersonBean.getFirstName()).concat("%"))); logger.debug("firstName: " + searchPersonBean.getFirstName()); } if (Tools.getInstance().stringNotEmpty(searchPersonBean.getLastName())) { dc.add(Restrictions.ilike("lastName", "%".concat(searchPersonBean.getLastName()).concat("%"))); dcCount.add(Restrictions.ilike("lastName", "%".concat(searchPersonBean.getLastName()).concat("%"))); logger.debug("lastName: " + searchPersonBean.getLastName()); } if (Tools.getInstance().stringNotEmpty(searchPersonBean.getUsername())) { dc.add(Restrictions.ilike("username", "%".concat(searchPersonBean.getUsername()).concat("%"))); dcCount.add(Restrictions.ilike("username", "%".concat(searchPersonBean.getUsername()).concat("%"))); logger.debug("username: " + searchPersonBean.getUsername()); } if (searchPersonBean.getDepartmentId() > 0) { dc.createCriteria("depts") .add(Restrictions.eq("departmentId", new Integer(searchPersonBean.getDepartmentId()))); dcCount.createCriteria("depts") .add(Restrictions.eq("departmentId", new Integer(searchPersonBean.getDepartmentId()))); logger.debug("departmentId: " + searchPersonBean.getDepartmentId()); } else if (searchPersonBean.getOrganisationId() > 0) { //if the search is in all branches we use it if (orgs != null) { dc.createCriteria("depts").add(Restrictions.in("organisationId", orgs)); dcCount.createCriteria("depts").add(Restrictions.in("organisationId", orgs)); logger.debug("number of branches: " + orgs.size()); } else { dc.createCriteria("depts") .add(Restrictions.eq("organisationId", new Integer(searchPersonBean.getOrganisationId()))); dcCount.createCriteria("depts") .add(Restrictions.eq("organisationId", new Integer(searchPersonBean.getOrganisationId()))); logger.debug("organisationId: " + searchPersonBean.getOrganisationId()); } } if (searchPersonBean.getSex() != null) { if (searchPersonBean.getSex().equals(IConstant.NOM_PERSON_SEX_F) || searchPersonBean.getSex().equals(IConstant.NOM_PERSON_SEX_M)) { dc.add(Restrictions.eq("sex", searchPersonBean.getSex())); dcCount.add(Restrictions.eq("sex", searchPersonBean.getSex())); logger.debug("sex: " + searchPersonBean.getSex()); } } dc.setProjection(Projections.id()); dcCount.setProjection(Projections.id()); // until here, I've created the subquery // now, it's time to retrive all the persons that are in the list of the subquery DetachedCriteria dc1 = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity); dc1.add(Subqueries.propertyIn("personId", dc)); // check if I have to order the results if (searchPersonBean.getSortParam() != null && StringUtils.hasLength(searchPersonBean.getSortParam())) { // if I have to, check if I have to order them ascending or descending if (searchPersonBean.getSortDirection() == IConstant.ASCENDING) { // ascending dc1.addOrder(Order.asc(searchPersonBean.getSortParam())); } else { // descending dc1.addOrder(Order.desc(searchPersonBean.getSortParam())); } } // if the request didn't come from the pagination area, // it means that I have to set the number of result and pages if (isDeleteAction || searchPersonBean.getNbrOfResults() == -1) { boolean isSearch = false; if (searchPersonBean.getNbrOfResults() == -1) { isSearch = true; } // set the countDistinct restriction dcCount.setProjection(Projections.countDistinct("personId")); int nbrOfResults = ((Integer) getHibernateTemplate().findByCriteria(dcCount, 0, 0).get(0)).intValue(); searchPersonBean.setNbrOfResults(nbrOfResults); logger.debug("NbrOfResults " + searchPersonBean.getNbrOfResults()); logger.debug("----> searchPersonBean.getResults " + searchPersonBean.getResultsPerPage()); // get the number of pages if (nbrOfResults % searchPersonBean.getResultsPerPage() == 0) { searchPersonBean.setNbrOfPages(nbrOfResults / searchPersonBean.getResultsPerPage()); } else { searchPersonBean.setNbrOfPages(nbrOfResults / searchPersonBean.getResultsPerPage() + 1); } // after a person is deleted, the same page has to be displayed; //only when all the persons from last page are deleted, the previous page will be shown if (isDeleteAction && (searchPersonBean.getCurrentPage() > searchPersonBean.getNbrOfPages())) { searchPersonBean.setCurrentPage(searchPersonBean.getNbrOfPages()); } else if (isSearch) { searchPersonBean.setCurrentPage(1); } } List<Person> res = getHibernateTemplate().findByCriteria(dc1, (searchPersonBean.getCurrentPage() - 1) * searchPersonBean.getResultsPerPage(), searchPersonBean.getResultsPerPage()); logger.debug("Res " + res.size()); logger.debug("getFromSearch - END - results size : ".concat(String.valueOf(res.size()))); return res; }