List of usage examples for org.hibernate.criterion Restrictions and
public static LogicalExpression and(Criterion lhs, Criterion rhs)
From source file:net.firejack.platform.core.store.AbstractStore.java
License:Apache License
protected Criteria prepareCriteria(Session session, LinkedList<Criterion> criterionList, Map<String, String> aliases, Paging paging, boolean isOr, boolean isLeft) { Criteria criteria = session.createCriteria(clazz); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); if (aliases != null && !aliases.isEmpty()) { for (Map.Entry<String, String> alias : aliases.entrySet()) { criteria.createAlias(alias.getKey(), alias.getValue(), isLeft ? CriteriaSpecification.LEFT_JOIN : CriteriaSpecification.INNER_JOIN); }//from w w w . j av a2s . c o m } if (criterionList != null) { Criterion left = null; for (Criterion criterion : criterionList) { left = criterionList.getFirst() == criterion ? criterion : isOr ? Restrictions.or(left, criterion) : Restrictions.and(left, criterion); } if (left != null) criteria.add(left); } if (paging != null) { if (paging.getLimit() != null && paging.getLimit() > -1) { criteria.setMaxResults(paging.getLimit()); } if (paging.getOffset() != null && paging.getOffset() > -1) { criteria.setFirstResult(paging.getOffset()); } if (paging.getSortFields() != null) { for (SortField sortField : paging.getSortFields()) { if (sortField.getSortDirection().equals(SortOrder.ASC)) { criteria.addOrder(Order.asc(sortField.getSortColumn())); } else { criteria.addOrder(Order.desc(sortField.getSortColumn())); } } } } return criteria; }
From source file:net.firejack.platform.core.store.AbstractStore.java
License:Apache License
protected Integer searchCount(final LinkedList<Criterion> criterions, final Map<String, String> aliases, final boolean isOr, final boolean isLeft) { return getHibernateTemplate().execute(new HibernateCallback<Integer>() { @Override/*from w w w. j a va 2s. c o m*/ public Integer doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = session.createCriteria(clazz); if (aliases != null && !aliases.isEmpty()) { for (Map.Entry<String, String> alias : aliases.entrySet()) { criteria.createAlias(alias.getKey(), alias.getValue(), isLeft ? CriteriaSpecification.LEFT_JOIN : CriteriaSpecification.INNER_JOIN); } } if (criterions != null) { Criterion left = null; for (Criterion criterion : criterions) { left = criterions.getFirst() == criterion ? criterion : isOr ? Restrictions.or(left, criterion) : Restrictions.and(left, criterion); } if (left != null) criteria.add(left); } return ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue(); } }); }
From source file:net.firejack.platform.core.store.AbstractStore.java
License:Apache License
@SuppressWarnings("unchecked") protected Criterion parseAdvancedSearchRequest(List<SearchQuery> searchQueries, Map<String, String> aliases, boolean skipNotValidValues) { int index = 0; LinkedList<Criterion> criterions = new LinkedList<Criterion>(); for (SearchQuery searchQuery : searchQueries) { Criterion criterion;/*from w ww . j a va 2s .c o m*/ String field = searchQuery.getField(); if (field == null) { criterions.add(Restrictions.sqlRestriction("1 = 1")); } else { String[] fieldNames = field.split("\\."); if (fieldNames.length == 1) { String fieldName = fieldNames[0]; PropertyDescriptor propertyDescriptor = ClassUtils.getPropertyDescriptor(getClazz(), fieldName); if (propertyDescriptor != null) { Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod != null) { Class<?> returnType = readMethod.getReturnType(); try { criterion = getRestrictions(searchQuery, returnType); criterions.add(criterion); } catch (IllegalArgumentException e) { if (!skipNotValidValues) { throw new BusinessFunctionException( "The field '" + fieldName + "' has type '" + returnType.getName() + "', but value '" + searchQuery.getValue() + "' is incorrect"); } } } else { throw new BusinessFunctionException("The field '" + fieldName + "' has not read method in class '" + getClazz().getName() + "'"); } } else { throw new BusinessFunctionException("The field '" + fieldName + "' does not exist in class '" + getClazz().getName() + "'"); } } else { Class<E> aClass = getClazz(); String indexedFieldName = null; for (int i = 0; i < fieldNames.length; i++) { String fieldName = fieldNames[i]; PropertyDescriptor propertyDescriptor = ClassUtils.getPropertyDescriptor(aClass, fieldName); if (propertyDescriptor != null) { Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod != null) { Class<?> returnType = readMethod.getReturnType(); if (Collection.class.isAssignableFrom(returnType)) { returnType = (Class<?>) ((ParameterizedTypeImpl) readMethod .getGenericReturnType()).getActualTypeArguments()[0]; } if (AbstractModel.class.isAssignableFrom(returnType)) { aClass = (Class) returnType; String alias = i == 0 ? fieldName : indexedFieldName + "." + fieldName; indexedFieldName = aliases.get(alias); if (indexedFieldName == null) { indexedFieldName = fieldName + index++; aliases.put(alias, indexedFieldName); } } else { if (i == (fieldNames.length - 1)) { String queryFieldName = indexedFieldName + "." + fieldName; try { criterion = getRestrictions(new SearchQuery(queryFieldName, searchQuery.getOperation(), searchQuery.getValue()), returnType); criterions.add(criterion); } catch (IllegalArgumentException e) { if (!skipNotValidValues) { throw new BusinessFunctionException("The field '" + fieldName + "' has type '" + returnType.getName() + "', but value '" + searchQuery.getValue() + "' is incorrect"); } } } else { throw new BusinessFunctionException("Field name: '" + fieldName + "' is not correct in query: '" + field + "'"); } } } else { throw new BusinessFunctionException("The field '" + fieldName + "' has not read method in class '" + aClass + "'"); } } else { throw new BusinessFunctionException( "The field '" + fieldName + "' does not exist in class '" + aClass + "'"); } } } } } Criterion andCriterion = null; for (Criterion criterion : criterions) { andCriterion = criterions.getFirst() == criterion ? criterion : Restrictions.and(andCriterion, criterion); } return andCriterion; }
From source file:net.firejack.platform.core.store.AbstractStore.java
License:Apache License
protected LinkedList<Criterion> addFilterCriterion(LinkedList<Criterion> criterionList, SpecifiedIdsFilter filter) {/* w w w. j a va2s .c om*/ LinkedList<Criterion> resultCriterionList; Criterion filterCriterion = createFilterCriterion(filter); if (criterionList.isEmpty()) { criterionList.add(filterCriterion); resultCriterionList = criterionList; } else { resultCriterionList = new LinkedList<Criterion>(); Criterion criterion = null; for (Criterion cr : criterionList) { criterion = criterion == null ? cr : Restrictions.or(criterion, cr); } resultCriterionList.add(Restrictions.and(filterCriterion, criterion)); } return resultCriterionList; }
From source file:net.firejack.platform.core.store.process.CaseStore.java
License:Apache License
@Override @Transactional//from w w w . ja v a 2 s . c o m public CaseModel moveCaseToActivity(Long entityId, Long activityActionId, Long assigneeId, Long currentUserId, String comment) { CaseModel processCase; if (entityId == null || activityActionId == null || currentUserId == null) { processCase = null; } else { UserModel currentUser = userStore.findById(currentUserId); LinkedList<Criterion> restrictions = new LinkedList<Criterion>(); restrictions.add(Restrictions.idEq(activityActionId)); Map<String, String> aliases = new HashMap<String, String>(); aliases.put("activityFrom", "from"); aliases.put("activityTo", "to"); aliases.put("status", "status"); List<ActivityActionModel> foundActions = activityActionStore.search(restrictions, aliases, null); ActivityActionModel activityAction = foundActions.get(0); UserModel assignee = assigneeId == null ? null : userStore.findById(assigneeId); restrictions.clear(); restrictions.add(Restrictions.idEq(activityAction.getActivityFrom().getId())); aliases.clear(); aliases.put("parent", "parent"); List<ProcessModel> processList = activityStore.searchWithProjection(restrictions, Projections.property("parent"), aliases, null); CaseObjectModel caseObjectModel; if (processList.isEmpty()) { processCase = null; caseObjectModel = null; } else { ProcessModel processModel = processList.get(0); EntityModel entityModel = getHibernateTemplate().get(EntityModel.class, processModel.getMain().getId()); restrictions.clear(); restrictions.add(Restrictions.eq("entityType", entityModel.getLookup())); restrictions.add(Restrictions.eq("entityId", entityId)); restrictions.add(Restrictions.eq("case.process.id", processModel.getId())); aliases.clear(); aliases.put("case", "case"); List<CaseObjectModel> caseObjects = caseObjectStore.search(restrictions, aliases, null, false); caseObjectModel = caseObjects.isEmpty() ? null : caseObjects.get(0); processCase = caseObjectModel == null ? null : caseObjectModel.getCase(); } if (processCase != null) { restrictions.clear(); restrictions.add(Restrictions.and(Restrictions.eq("case.id", processCase.getId()), Restrictions.eq("active", Boolean.TRUE))); List<TaskModel> currentActiveTasks = taskStore.search(restrictions, null); TaskModel oldActiveTaskModel; if (currentActiveTasks.isEmpty()) { oldActiveTaskModel = null; } else { oldActiveTaskModel = currentActiveTasks.get(0); for (TaskModel activeTask : currentActiveTasks) { activeTask.setActive(Boolean.FALSE); } taskStore.saveOrUpdateAll(currentActiveTasks); } StatusModel status = activityAction.getStatus(); ActivityModel toActivity = activityAction.getActivityTo(); boolean isNotFinalStep = !status.getName().equals(StatusModel.STATUS_FINISHED) && toActivity.getActivityOrder() != ActivityOrder.END; String taskDescription = StringUtils.isBlank(toActivity.getDescription()) ? processCase.getDescription() : toActivity.getDescription(); Date updateDate = new Date(System.currentTimeMillis()); TaskModel nextTaskModel = new TaskModel(); nextTaskModel.setDescription(taskDescription); nextTaskModel.setActivity(toActivity); nextTaskModel.setCase(processCase); nextTaskModel.setUpdateDate(updateDate); nextTaskModel.setAssignee(assignee); nextTaskModel.setActive(isNotFinalStep); taskStore.saveOrUpdate(nextTaskModel); processCase.setStatus(status); processCase.setActive(isNotFinalStep); saveOrUpdate(processCase); caseObjectModel.setTask(nextTaskModel); caseObjectModel.setStatus(status); caseObjectModel.setUpdateDate(updateDate); caseObjectModel.setUpdatedBy(currentUser); caseObjectStore.saveOrUpdate(caseObjectModel); if (oldActiveTaskModel != null) { CaseActionModel caseAction = new CaseActionModel(); caseAction.setPerformedOn(updateDate); caseAction.setType(CaseActionType.PERFORM_ACTIVITY); caseAction.setCase(processCase); caseAction.setUser(currentUser); caseAction.setTaskModel(oldActiveTaskModel); if (StringUtils.isNotBlank(comment)) { CaseNoteModel caseNote = new CaseNoteModel(); caseNote.setProcessCase(processCase); caseNote.setText(comment); caseNote.setUser(currentUser); caseNoteStore.saveOrUpdate(caseNote); caseAction.setCaseNote(caseNote); } caseActionStore.saveOrUpdate(caseAction); } } } return processCase; }
From source file:net.firejack.platform.core.store.process.TaskStore.java
License:Apache License
private LinkedList<Criterion> addCriteriaByType(String type, LinkedList<Criterion> criterions, Map<String, String> aliases) { LinkedList<Criterion> newCriterions = new LinkedList<Criterion>(); if ("MY".equals(type)) { Long userId = OPFContext.getContext().getPrincipal().getUserInfoProvider().getId(); for (Criterion criterion : criterions) { newCriterions.add(Restrictions.and(criterion, Restrictions.eq("assignee.id", userId))); }/* w w w.j ava 2 s .c o m*/ } else if ("TEAM".equals(type)) { Long userId = OPFContext.getContext().getPrincipal().getUserInfoProvider().getId(); aliases.put("activity", "activity"); aliases.put("activity.actor", "actor"); DetachedCriteria actorSubCriteria = DetachedCriteria.forClass(ActorModel.class, "a") .createAlias("a.userActors", "userActor").add(Restrictions.eq("userActor.user.id", userId)) .setProjection(Projections.property("a.id")); Criterion actorCriteria = Property.forName("actor.id").in(actorSubCriteria); for (Criterion criterion : criterions) { newCriterions.add(Restrictions.and(criterion, actorCriteria)); } } else { newCriterions = criterions; } return newCriterions; }
From source file:net.firejack.platform.core.store.registry.ConfigStore.java
License:Apache License
@Override @Transactional(readOnly = true)// w w w. ja va 2 s . c o m public List<ConfigModel> findAllBySearchTermWithFilter(List<Long> registryNodeIds, String term, SpecifiedIdsFilter<Long> filter) { List<Criterion> criterions = new ArrayList<Criterion>(); Criterion registryNodeIdCriterion = Restrictions.in("parent.id", registryNodeIds); Criterion nameCriterion = Restrictions.like("lookup", "%" + term + "%"); LogicalExpression expressionAll = Restrictions.and(registryNodeIdCriterion, nameCriterion); criterions.add(expressionAll); return findAllWithFilter(criterions, filter); }
From source file:net.firejack.platform.core.store.registry.GroupStore.java
License:Apache License
@Override public List<GroupModel> findAllBySearchTermWithFilter(List<Long> registryNodeIds, String term, SpecifiedIdsFilter<Long> filter) { List<Criterion> criterions = new ArrayList<Criterion>(); Criterion registryNodeIdCriterion = Restrictions.in("parent.id", registryNodeIds); if (term != null) { Criterion nameCriterion = Restrictions.like("lookup", "%" + term + "%"); LogicalExpression expressionAll = Restrictions.and(registryNodeIdCriterion, nameCriterion); criterions.add(expressionAll);/*from w ww. j a v a2 s . c o m*/ } else { criterions.add(registryNodeIdCriterion); } return findAllWithFilter(criterions, filter); }
From source file:net.firejack.platform.core.store.registry.PermissionStore.java
License:Apache License
@Override public List<PermissionModel> findAllBySearchTermWithFilter(List<Long> registryNodeIds, String term, SpecifiedIdsFilter<Long> filter, Paging paging) { List<Criterion> criterions = new ArrayList<Criterion>(); Criterion registryNodeIdCriterion = Restrictions.in("parent.id", registryNodeIds); Criterion nameCriterion = Restrictions.like("lookup", '%' + term + '%'); LogicalExpression expressionAll = Restrictions.and(registryNodeIdCriterion, nameCriterion); criterions.add(expressionAll);/*from ww w . j a v a2 s . c om*/ return findAllWithFilter(criterions, filter, paging); }
From source file:net.firejack.platform.core.store.registry.resource.ResourceStore.java
License:Apache License
@Override @Transactional(readOnly = true)/*from w w w. java 2 s . c o m*/ public List<R> findAllBySearchTermWithFilter(List<Long> registryNodeIds, String term, SpecifiedIdsFilter<Long> filter) { List<Criterion> criterions = new ArrayList<Criterion>(); Criterion registryNodeIdCriterion = Restrictions.in("parent.id", registryNodeIds); Criterion nameCriterion = Restrictions.like("name", "%" + term + "%"); LogicalExpression expressionAll = Restrictions.and(registryNodeIdCriterion, nameCriterion); criterions.add(expressionAll); return findAllWithFilter(criterions, filter); }