List of usage examples for org.hibernate.criterion Restrictions in
public static Criterion in(String propertyName, Collection values)
From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.JobAttributeReportHelper.java
License:Apache License
private void initDate() { totalCells = new TreeMap<Integer, List<Integer>>(); Session session = HibernateUtil.getSession(); Criteria c = session.createCriteria(JobImpl.class); c.add(Restrictions.in("createUserId", submitIds)); c.add(Restrictions.in("state", status)); if (startDate != null) { c.add(Restrictions.gt("createDate", startDate)); }/*from w ww . j av a 2 s . co m*/ if (endDate != null) { c.add(Restrictions.le("createDate", endDate)); } jobAttributes = c.list(); for (int i = jobAttributes.size() - 1; i >= 0; i--) { JobImpl job = jobAttributes.get(i); long projectId = job.getL10nProfile().getProject().getId(); if (projectIds.indexOf(projectId) < 0) { jobAttributes.remove(i); continue; } boolean found = false; for (Workflow w : job.getWorkflows()) { String targetLang = Long.toString(w.getTargetLocale().getId()); if (trgLocaleList.contains(targetLang)) { found = true; break; } } if (!found) { jobAttributes.remove(i); continue; } } SortUtil.sort(jobAttributes, getComparator()); }
From source file:com.grand.ids.jdbc.knowledgemodule.id.IdKnowledgeModule.java
License:Apache License
protected Graph getGraph(final Table table, ResultSet resultSet, final UserId userId) throws Exception { final List<List<Object>> ids = getObjectIds(resultSet, table); final Graph graph = new Graph(ids.size()); doInTransaction(new Action() { public void doAction(Session session) { Set<String> idSet = new HashSet<String>(); Map<String, Integer> nodesNum = new HashMap<String, Integer>(); int num = 0; for (List<Object> id : ids) { String stringId = getStringIdPresentation(id); idSet.add(stringId);/*from www . j av a 2s . c o m*/ nodesNum.put(stringId, num++); } Criteria criteria = session.createCriteria(RecordInfo.class); criteria.add(Restrictions.in("recordId", idSet)); criteria.add(Restrictions.eq("tableName", table.getName())); criteria.add(Restrictions.eq("userId", userId.getUserId())); criteria.setFetchMode("friendlyRecordIds", FetchMode.JOIN); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); List<RecordInfo> recordInfos = criteria.list(); Set<String> edges = new HashSet<String>(); for (RecordInfo recordInfoA : recordInfos) { Integer nodeId = nodesNum.get(recordInfoA.getRecordId()); Set<String> friendIds = recordInfoA.getFriendlyRecordIds(); for (RecordInfo recordInfoB : recordInfos) { Integer friendNodeId = nodesNum.get(recordInfoB.getRecordId()); String edge = String.format("%d-%d", friendNodeId, nodeId); if (!edges.contains(edge)) { if (friendIds.contains(recordInfoB.getRecordId())) { graph.addEdge(nodeId, friendNodeId, 1.0); } else { graph.addEdge(nodeId, friendNodeId, getLinkPredictionScore(table, userId, session, recordInfoA, recordInfoB)); } edges.add(String.format("%d-%d", nodeId, friendNodeId)); } } } } }); return graph; }
From source file:com.grand.ids.jdbc.knowledgemodule.id.IdKnowledgeModule.java
License:Apache License
private double getLinkPredictionScore(Table table, UserId userId, Session session, RecordInfo recordInfoA, RecordInfo recordInfoB) {// ww w . j a v a 2s.co m if (linkPredictionMode == LinkPredictionMode.ADAMIC_ADAR) { Set<String> intersection = new HashSet<String>(recordInfoA.getFriendlyRecordIds()); intersection.retainAll(recordInfoB.getFriendlyRecordIds()); if (intersection.size() > 0) { Criteria criteria = session.createCriteria(RecordInfo.class); criteria.add(Restrictions.in("recordId", intersection)); criteria.add(Restrictions.eq("tableName", table.getName())); criteria.add(Restrictions.eq("userId", userId.getUserId())); criteria.setFetchMode("friendlyRecordIds", FetchMode.JOIN); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); List<RecordInfo> recordInfos = criteria.list(); double sum = 0; for (RecordInfo recordInfo : recordInfos) { sum += Math.log(2) / Math.log(recordInfo.getFriendlyRecordIds().size()); } double score = sum / intersection.size(); if ((score > 1.0) || (score < 0.0)) { throw new RuntimeException("Error!"); } return score; } else { return 0.0; } } else if (linkPredictionMode == LinkPredictionMode.JACCARD) { Set<String> friendsA = recordInfoA.getFriendlyRecordIds(); Set<String> friendsB = recordInfoB.getFriendlyRecordIds(); Set<String> union = new HashSet<String>(friendsA); union.addAll(friendsB); Set<String> intersection = new HashSet<String>(friendsA); intersection.retainAll(friendsB); System.out.println(intersection.size() + " " + union.size() + " : " + ((union.size() > 0) ? (double) intersection.size() / union.size() : 0.0)); return (union.size() > 0) ? ((double) intersection.size()) / union.size() : 0.0; } return 0.0; }
From source file:com.grand.ids.jdbc.knowledgemodule.id.IdKnowledgeModule.java
License:Apache License
private void updateRecordInfos(final String tableName, final List<List<Object>> ids, final UserId userId) { doInTransaction(new Action() { public void doAction(Session session) { Set<String> idSet = new HashSet<String>(); for (List<Object> id : ids) { idSet.add(getStringIdPresentation(id)); }//from w w w . ja v a2 s . com Criteria criteria = session.createCriteria(RecordInfo.class); criteria.add(Restrictions.in("recordId", idSet)); criteria.add(Restrictions.eq("tableName", tableName)); criteria.add(Restrictions.eq("userId", userId.getUserId())); criteria.setFetchMode("friendlyRecordIds", FetchMode.JOIN); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); List<RecordInfo> recordInfos = criteria.list(); Set<String> nonExistIds = new HashSet<String>(idSet); for (RecordInfo recordInfo : recordInfos) { recordInfo.getFriendlyRecordIds().addAll(idSet); recordInfo.setHits(recordInfo.getHits() + 1); session.update(recordInfo); nonExistIds.remove(recordInfo.getRecordId()); } for (String id : nonExistIds) { RecordInfo recordInfo = new RecordInfo(); recordInfo.setRecordId(id); recordInfo.setTableName(tableName); recordInfo.setFriendlyRecordIds(idSet); recordInfo.setHits(1L); recordInfo.setUserId(userId.getUserId()); session.save(recordInfo); } } }); }
From source file:com.green.common.service.BaseService.java
License:Open Source License
/** * ?/* w w w. ja va 2 s .c o m*/ * @param user ?UserUtils.getUser()?? * @param officeAlias ??dc.createAlias("office", "office"); * @param userAlias ??? * @return ? */ protected static Junction dataScopeFilter(User user, String officeAlias, String userAlias) { // ???? List<String> dataScope = Lists.newArrayList(); Junction junction = Restrictions.disjunction(); // ??? if (!user.isAdmin()) { for (Role r : user.getRoleList()) { if (!dataScope.contains(r.getDataScope()) && StringUtils.isNotBlank(officeAlias)) { boolean isDataScopeAll = false; if (Role.DATA_SCOPE_ALL.equals(r.getDataScope())) { isDataScopeAll = true; } else if (Role.DATA_SCOPE_COMPANY_AND_CHILD.equals(r.getDataScope())) { junction.add(Restrictions.eq(officeAlias + ".id", user.getCompany().getId())); junction.add(Restrictions.like(officeAlias + ".parentIds", user.getCompany().getParentIds() + user.getCompany().getId() + ",%")); } else if (Role.DATA_SCOPE_COMPANY.equals(r.getDataScope())) { junction.add(Restrictions.eq(officeAlias + ".id", user.getCompany().getId())); junction.add(Restrictions.and( Restrictions.eq(officeAlias + ".parent.id", user.getCompany().getId()), Restrictions.eq(officeAlias + ".type", "2"))); // ? } else if (Role.DATA_SCOPE_OFFICE_AND_CHILD.equals(r.getDataScope())) { junction.add(Restrictions.eq(officeAlias + ".id", user.getOffice().getId())); junction.add(Restrictions.like(officeAlias + ".parentIds", user.getOffice().getParentIds() + user.getOffice().getId() + ",%")); } else if (Role.DATA_SCOPE_OFFICE.equals(r.getDataScope())) { junction.add(Restrictions.eq(officeAlias + ".id", user.getOffice().getId())); } else if (Role.DATA_SCOPE_CUSTOM.equals(r.getDataScope())) { junction.add(Restrictions.in(officeAlias + ".id", r.getOfficeIdList())); } //else if (Role.DATA_SCOPE_SELF.equals(r.getDataScope())){ if (!isDataScopeAll) { if (StringUtils.isNotBlank(userAlias)) { junction.add(Restrictions.eq(userAlias + ".id", user.getId())); } else { junction.add(Restrictions.isNull(officeAlias + ".id")); } } else { // ????? junction = Restrictions.disjunction(); break; } dataScope.add(r.getDataScope()); } } } return junction; }
From source file:com.green.modules.cms.service.CategoryService.java
License:Open Source License
@SuppressWarnings("unchecked") public List<Category> findByUser(boolean isCurrentSite, String module) { List<Category> list = (List<Category>) UserUtils.getCache(CACHE_CATEGORY_LIST); if (list == null) { User user = UserUtils.getUser(); DetachedCriteria dc = categoryDao.createDetachedCriteria(); dc.createAlias("office", "office").createAlias("createBy", "user"); dc.add(dataScopeFilter(user, "office", "user")); // dc.add(Restrictions.or(Restrictions.isNull("href"),Restrictions.eq("href", ""))); dc.add(Restrictions.eq("delFlag", Category.DEL_FLAG_NORMAL)); dc.addOrder(Order.asc("site.id")).addOrder(Order.asc("sort")); list = categoryDao.find(dc);//w w w . ja v a 2s . c o m // Set<String> parentIdSet = Sets.newHashSet(); for (Category e : list) { if (e.getParent() != null && StringUtils.isNotBlank(e.getParent().getId())) { boolean isExistParent = false; for (Category e2 : list) { if (e.getParent().getId().equals(e2.getId())) { isExistParent = true; break; } } if (!isExistParent) { parentIdSet.add(e.getParent().getId()); } } } if (parentIdSet.size() > 0) { dc = categoryDao.createDetachedCriteria(); dc.add(Restrictions.in("id", parentIdSet)); dc.add(Restrictions.eq("delFlag", Category.DEL_FLAG_NORMAL)); dc.addOrder(Order.asc("site.id")).addOrder(Order.asc("sort")); list.addAll(0, categoryDao.find(dc)); } UserUtils.putCache(CACHE_CATEGORY_LIST, list); } if (isCurrentSite) { List<Category> categoryList = Lists.newArrayList(); for (Category e : list) { if (Category.isRoot(e.getId()) || (e.getSite() != null && e.getSite().getId() != null && e.getSite().getId().equals(Site.getCurrentSiteId()))) { if (StringUtils.isNotEmpty(module)) { if (module.equals(e.getModule()) || "".equals(e.getModule())) { categoryList.add(e); } } else { categoryList.add(e); } } } return categoryList; } return list; }
From source file:com.griffinslogistics.book.BookDAO.java
@Override public List<Book> getAllById(List<Long> selectedBookIds) { return this.sessionFactory.getCurrentSession().createCriteria(Book.class) .setFetchMode(Bookspackage.class.getName(), FetchMode.JOIN) .add(Restrictions.in("id", selectedBookIds)).list(); }
From source file:com.griffinslogistics.book.BookService.java
@Override public List<BookLabelModel> getLabelInfoForBooks(List<Long> selectedBookIds) { DetachedCriteria criteria = DetachedCriteria.forClass(Book.class) .setFetchMode(Bookspackage.class.getName(), FetchMode.JOIN) .add(Restrictions.in("id", selectedBookIds)); List<Book> books = (List<Book>) this.dao.getAllByDetachedCriteria(criteria); List<BookLabelModel> result = new ArrayList<BookLabelModel>(); if (books != null && !books.isEmpty()) { Transport transport = (Transport) books.get(0).getTransport(); for (Book book : books) { BookLabelModel model = new BookLabelModel(book.getDeliveryAddress(), book.getBookspackage().getPostalCode(), book.getTitle(), book.getBookNumber(), book.getBookspackage().getClient(), transport.getWeekNumber() + "/" + transport.getYear(), (long) book.getCount(), book.getBookspackage().getPackageNumber(), book.getISBN()); result.add(model);/* w w w. j a v a2s.c o m*/ } } return result; }
From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java
License:Apache License
/** * Element Start SAX ContentHandler Method. * @param uri// ww w . j ava 2s .c o m * @param localName * @param qName * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) * @throws SAXException * TODO: Document supported tags in javadoc. */ @SuppressWarnings({ "unchecked" }) public void startElement(String uri, String localNameX, String qName, Attributes attrs) throws SAXException { if ("Query".equalsIgnoreCase(qName)) { queryName = attrs.getValue("name"); rootElementName = queryName; } else if ("Class".equalsIgnoreCase(qName)) { processCriteria(attrs); } else if ("Union".equalsIgnoreCase(qName)) { inDetached = true; DetachedCriteria dc = DetachedCriteria.forEntityName(className); criteriaStack.push(dc); } else if ("NamedQuery".equalsIgnoreCase(qName)) { isNamedQuery = true; namedQuery = attrs.getValue("name"); rootElementName = namedQuery; try { query = session.getNamedQuery(namedQuery); } catch (HibernateException he) { throw new SAXException("Failed to retrieve named query[" + namedQuery + "]", he); } } else if ("qparam".equalsIgnoreCase(qName)) { processQueryBind(attrs); } else if ("Join".equalsIgnoreCase(qName)) { processCriteria(attrs); } else if ("Projections".equalsIgnoreCase(qName)) { startProjection(attrs); } else if ("Projection".equalsIgnoreCase(qName)) { addProjection(attrs); } else if ("Order".equalsIgnoreCase(qName)) { if (isRowCountOnly() == false) { try { String name = attrs.getValue("name"); String type = attrs.getValue("type"); ((Criteria) criteriaStack.peek()) .addOrder(type.equalsIgnoreCase("asc") ? Order.asc(name) : Order.desc(name)); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThan:" + attrs.getValue("name"), e); } } } else if ("GreaterThan".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.gt(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThan:" + attrs.getValue("name"), e); } } else if ("GreaterThanOrEqual".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.ge(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThanOrEqual:" + attrs.getValue("name"), e); } } else if ("LessThan".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.lt(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse LessThan:" + attrs.getValue("name"), e); } } else if ("LessThanOrEqual".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.le(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse LessThanOrEqual:" + attrs.getValue("name"), e); } } else if ("Equals".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); if ("true".equalsIgnoreCase(attrs.getValue("not"))) { addCriterion(Restrictions.not(Restrictions.eq(operator.getName(), operator.getNativeValue()))); } else { addCriterion(Restrictions.eq(operator.getName(), operator.getNativeValue())); } } catch (Exception e) { throw new SAXException("Unable To Parse Equals:" + attrs.getValue("name"), e); } } else if ("Alias".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); ((Criteria) criteriaStack.peek()).createAlias(operator.getName(), operator.getValue()); } catch (Exception e) { throw new SAXException("Unable To Create Alias:" + attrs.getValue("name"), e); } } else if ("GreaterThanProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.gtProperty(operator.getName(), operator.getName2())); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThanProperty:" + attrs.getValue("name"), e); } } else if ("GreaterThanOrEqualProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.geProperty(operator.getName(), operator.getName2())); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThanOrEqualProperty:" + attrs.getValue("name"), e); } } else if ("LessThanProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.ltProperty(operator.getName(), operator.getName2())); } catch (Exception e) { throw new SAXException("Unable To Parse LessThanProperty:" + attrs.getValue("name"), e); } } else if ("LessThanOrEqualProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.leProperty(operator.getName(), operator.getName2())); } catch (Exception e) { throw new SAXException("Unable To Parse LessThanOrEqualProperty:" + attrs.getValue("name"), e); } } else if ("EqualsProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); if ("true".equalsIgnoreCase(attrs.getValue("not"))) { addCriterion( Restrictions.not(Restrictions.eqProperty(operator.getName(), operator.getName2()))); } else { addCriterion(Restrictions.eqProperty(operator.getName(), operator.getName2())); } } catch (Exception e) { throw new SAXException("Unable To Parse EqualsProperty:" + attrs.getValue("name"), e); } } else if ("Like".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.like(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse Like:" + attrs.getValue("name"), e); } } else if ("Between".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.between(operator.getName(), operator.getNativeValue(), operator.getNativeValue2())); } catch (Exception e) { throw new SAXException("Unable To Parse Between:" + attrs.getValue("name"), e); } } else if ("IsEmpty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.isEmpty(operator.getName())); } catch (Exception e) { throw new SAXException("Unable To Parse IsEmpty:" + attrs.getValue("name"), e); } } else if ("IsNotEmpty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.isNotEmpty(operator.getName())); } catch (Exception e) { throw new SAXException("Unable To Parse IsNotEmpty:" + attrs.getValue("name"), e); } } else if ("IsNull".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.isNull(operator.getName())); } catch (Exception e) { throw new SAXException("Unable To Parse IsNull:" + attrs.getValue("name"), e); } } else if ("IsNotNull".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.isNotNull(operator.getName())); } catch (Exception e) { throw new SAXException("Unable To Parse IsNotNull:" + attrs.getValue("name"), e); } } else if ("In".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); if (operator.isLiteral()) { addCriterion(new LiteralInExpression(operator.getName(), (String) operator.getNativeValue())); } else { addCriterion(Restrictions.in(operator.getName(), (Collection) operator.getNativeValue())); } } catch (Exception e) { throw new SAXException("Unable To Parse In:" + attrs.getValue("name"), e); } } else if ("SizeEquals".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); int i = ((Integer) operator.getNativeValue()).intValue(); addCriterion(Restrictions.sizeEq(operator.getName(), i)); } catch (Exception e) { throw new SAXException("Unable To Parse SizeEquals:" + attrs.getValue("name"), e); } } else if ("Not".equalsIgnoreCase(qName)) { notStack.push(new Object()); } else if ("Or".equalsIgnoreCase(qName)) { opStack.push(Restrictions.disjunction()); } else if ("And".equalsIgnoreCase(qName)) { opStack.push(Restrictions.conjunction()); } else { throw new SAXException("Element Name[" + qName + "] Not Recognized."); } }
From source file:com.heroespersist.sports.implementation.match.MatchDaoImpl.java
License:Open Source License
public List<Match> allMatches(boolean showCriteria) { Session session = utilityDao.getSession(); List<Match> matches;/*from ww w . j av a 2 s .c om*/ try { if (showCriteria) { matches = session.createCriteria(Match.class) .add(Restrictions.in("game.id", utilityDao.getGameIds())).add(Restrictions.eq("show", true)) .addOrder(Order.desc("round")).addOrder(Order.asc("id")).setCacheable(true).list(); } else { matches = session.createCriteria(Match.class) .add(Restrictions.in("game.id", utilityDao.getGameIds())).addOrder(Order.desc("round")) .addOrder(Order.asc("id")).setCacheable(true).list(); } } catch (HibernateException e) { throw new SportsException("OOPS! There seems to be a problem with the database"); } for (Match match : matches) { match.getGame().getName(); match.getTeams().size(); } return matches; }