List of usage examples for org.hibernate Query setDate
@Deprecated @SuppressWarnings("unchecked") default Query<R> setDate(String name, Date val)
From source file:org.unitime.timetable.model.Location.java
License:Open Source License
public static Hashtable<Long, Set<Long>> findClassLocationTable(Long sessionId, Set<Long> permanentIds, int startSlot, int length, Vector<Date> dates) { if (permanentIds.isEmpty() || dates.isEmpty()) return new Hashtable<Long, Set<Long>>(); EventDateMapping.Class2EventDateMap class2eventMap = EventDateMapping.getMapping(sessionId); String datesStr = ""; for (int i = 0; i < dates.size(); i++) { if (i > 0) datesStr += ", "; datesStr += ":date" + i; }// w w w. j a v a 2 s. c o m Hashtable<Long, Set<Long>> table = new Hashtable<Long, Set<Long>>(); Iterator<Long> permanentIdIterator = permanentIds.iterator(); while (permanentIdIterator.hasNext()) { String permIds = ""; Long permanentId; int cntPermIds = 0; while (permanentIdIterator.hasNext() && cntPermIds < 1000) { permanentId = permanentIdIterator.next(); if (permIds.length() > 0) permIds += ","; permIds += permanentId; cntPermIds++; } Query q = LocationDAO.getInstance().getSession() .createQuery("select distinct m.locationPermanentId, e.clazz.uniqueId from " + "ClassEvent e inner join e.meetings m where " + "m.locationPermanentId in (" + permIds + ") and " + "m.stopPeriod>:startSlot and :endSlot>m.startPeriod and " + // meeting time within given time period "m.meetingDate in (" + datesStr + ")") // and date .setInteger("startSlot", startSlot).setInteger("endSlot", startSlot + length); for (int i = 0; i < dates.size(); i++) { q.setDate("date" + i, class2eventMap.getEventDate(dates.elementAt(i))); } for (Iterator i = q.setCacheable(true).list().iterator(); i.hasNext();) { Object[] o = (Object[]) i.next(); Set<Long> ids = table.get((Long) o[0]); if (ids == null) { ids = new HashSet<Long>(); table.put((Long) o[0], ids); } ids.add((Long) o[1]); } } return table; }
From source file:org.unitime.timetable.model.Location.java
License:Open Source License
public static Hashtable<Long, Set<Event>> findEventTable(Long sessionId, Set<Long> permanentIds, int startSlot, int length, Vector<Date> dates) { if (permanentIds.isEmpty() || dates.isEmpty()) return new Hashtable<Long, Set<Event>>(); EventDateMapping.Class2EventDateMap class2eventMap = EventDateMapping.getMapping(sessionId); String datesStr = ""; for (int i = 0; i < dates.size(); i++) { if (i > 0) datesStr += ", "; datesStr += ":date" + i; }/*from w w w . j a va2 s . com*/ Hashtable<Long, Set<Event>> table = new Hashtable<Long, Set<Event>>(); Iterator<Long> permanentIdIterator = permanentIds.iterator(); while (permanentIdIterator.hasNext()) { String permIds = ""; Long permanentId; int cntPermIds = 0; while (permanentIdIterator.hasNext() && cntPermIds < 1000) { permanentId = permanentIdIterator.next(); if (permIds.length() > 0) permIds += ","; permIds += permanentId; cntPermIds++; } Query q = LocationDAO.getInstance().getSession() .createQuery("select distinct m.locationPermanentId, e from " + "Event e inner join e.meetings m where " + "e.class!=ClassEvent and " + "m.locationPermanentId in (" + permIds + ") and " + "m.stopPeriod>:startSlot and :endSlot>m.startPeriod and " + // meeting time within given time period "m.meetingDate in (" + datesStr + ") and m.approvalStatus = 1") // and date .setInteger("startSlot", startSlot).setInteger("endSlot", startSlot + length); for (int i = 0; i < dates.size(); i++) { q.setDate("date" + i, class2eventMap.getEventDate(dates.elementAt(i))); } for (Iterator i = q.setCacheable(true).list().iterator(); i.hasNext();) { Object[] o = (Object[]) i.next(); Set<Event> events = table.get((Long) o[0]); if (events == null) { events = new HashSet<Event>(); table.put((Long) o[0], events); } events.add((Event) o[1]); } } return table; }
From source file:org.unitime.timetable.model.Student.java
License:Open Source License
public static Hashtable<Long, Set<Long>> findConflictingStudents(Long classId, int startSlot, int length, Vector<Date> dates) { Hashtable<Long, Set<Long>> table = new Hashtable(); if (dates.isEmpty()) return table; String datesStr = ""; for (int i = 0; i < dates.size(); i++) { if (i > 0) datesStr += ", "; datesStr += ":date" + i; }// w w w .j av a2 s . com Query q = LocationDAO.getInstance().getSession() .createQuery("select distinct e.clazz.uniqueId, e.student.uniqueId " + "from StudentClassEnrollment e, ClassEvent c inner join c.meetings m, StudentClassEnrollment x " + "where x.clazz.uniqueId=:classId and x.student=e.student and " + // only look among students of the given class "e.clazz=c.clazz and " + // link ClassEvent c with StudentClassEnrollment e "m.stopPeriod>:startSlot and :endSlot>m.startPeriod and " + // meeting time within given time period "m.meetingDate in (" + datesStr + ")") .setLong("classId", classId).setInteger("startSlot", startSlot) .setInteger("endSlot", startSlot + length); for (int i = 0; i < dates.size(); i++) { q.setDate("date" + i, dates.elementAt(i)); } for (Iterator i = q.setCacheable(true).list().iterator(); i.hasNext();) { Object[] o = (Object[]) i.next(); Set<Long> set = table.get((Long) o[0]); if (set == null) { set = new HashSet<Long>(); table.put((Long) o[0], set); } set.add((Long) o[1]); } return table; }
From source file:org.unitime.timetable.webutil.WebEventTableBuilder.java
License:Open Source License
protected List loadEvents(EventListForm form) { boolean conf = (form.getMode() == EventListForm.sModeAllConflictingEvents); String query = "select distinct e from Event e inner join e.meetings m where e.class in ("; if (conf) {/*from w w w.ja v a 2 s . c o m*/ query = "select distinct e from Event e inner join e.meetings m, Meeting mx where " + "mx.uniqueId!=m.uniqueId and m.meetingDate=mx.meetingDate and m.startPeriod < mx.stopPeriod and m.stopPeriod > mx.startPeriod and " + "m.locationPermanentId = mx.locationPermanentId and e.class in ("; } for (int i = 0; i < form.getEventTypes().length; i++) { if (i > 0) query += ","; switch (form.getEventTypes()[i].intValue()) { case Event.sEventTypeClass: query += "ClassEvent"; break; case Event.sEventTypeFinalExam: query += "FinalExamEvent"; break; case Event.sEventTypeMidtermExam: query += "MidtermExamEvent"; break; case Event.sEventTypeCourse: query += "CourseEvent"; break; case Event.sEventTypeSpecial: query += "SpecialEvent"; break; } //query += form.getEventTypes()[i]; } query += ")"; if (form.getEventNameSubstring() != null && form.getEventNameSubstring().trim().length() > 0) { query += " and upper(e.eventName) like :eventNameSubstring"; } if (form.getEventDateFrom() != null && form.getEventDateFrom().trim().length() > 0) { query += " and m.meetingDate>=:eventDateFrom"; } if (form.getEventDateTo() != null && form.getEventDateTo().trim().length() > 0) { query += " and m.meetingDate<=:eventDateTo"; } if (form.getEventMainContactSubstring() != null && form.getEventMainContactSubstring().trim().length() > 0) { for (StringTokenizer s = new StringTokenizer(form.getEventMainContactSubstring().trim(), ", "); s .hasMoreTokens();) { String token = s.nextToken().toUpperCase(); query += " and (upper(e.mainContact.firstName) like '%" + token + "%' or upper(e.mainContact.middleName) like '%" + token + "%' or upper(e.mainContact.lastName) like '%" + token + "%')"; } } switch (form.getMode()) { case EventListForm.sModeMyEvents: query += " and e.mainContact.externalUniqueId = :userId"; break; case EventListForm.sModeAllApprovedEvents: query += " and m.approvedDate is not null"; break; case EventListForm.sModeAllEventsWaitingApproval: query += " and m.approvedDate is null"; break; case EventListForm.sModeEvents4Approval: query += " and m.approvedDate is null"; break; case EventListForm.sModeAllEvents: break; } if (form.getSponsoringOrganization() != null && form.getSponsoringOrganization() >= 0) { query += " and e.sponsoringOrganization.uniqueId=:sponsorOrgId"; } if (form.getStartTime() >= 0) { query += " and m.stopPeriod > " + form.getStartTime(); } if (form.getStopTime() >= 0) { query += " and m.startPeriod < " + form.getStopTime(); } if (form.isDayMon() || form.isDayTue() || form.isDayWed() || form.isDayThu() || form.isDayFri() || form.isDaySat() || form.isDaySun()) { String dow = ""; if (form.isDayMon()) { if (!dow.isEmpty()) dow += ","; dow += "2"; } if (form.isDayTue()) { if (!dow.isEmpty()) dow += ","; dow += "3"; } if (form.isDayWed()) { if (!dow.isEmpty()) dow += ","; dow += "4"; } if (form.isDayThu()) { if (!dow.isEmpty()) dow += ","; dow += "5"; } if (form.isDayFri()) { if (!dow.isEmpty()) dow += ","; dow += "6"; } if (form.isDaySat()) { if (!dow.isEmpty()) dow += ","; dow += "7"; } if (form.isDaySun()) { if (!dow.isEmpty()) dow += ","; dow += "1"; } if (dow.indexOf(',') >= 0) query += " and " + HibernateUtil.dayOfWeek("m.meetingDate") + " in (" + dow + ")"; else query += " and " + HibernateUtil.dayOfWeek("m.meetingDate") + " = " + dow; } query += " order by e.eventName, e.uniqueId"; Query hibQuery = new EventDAO().getSession().createQuery(query); hibQuery.setFetchSize(getMaxResults() + 1); if (form.getMode() != EventListForm.sModeEvents4Approval) hibQuery.setMaxResults(getMaxResults() + 1); if (form.getEventNameSubstring() != null && form.getEventNameSubstring().trim().length() > 0) { hibQuery.setString("eventNameSubstring", "%" + form.getEventNameSubstring().toUpperCase().trim() + "%"); } if (form.getEventDateFrom() != null && form.getEventDateFrom().trim().length() > 0) { try { hibQuery.setDate("eventDateFrom", new SimpleDateFormat("MM/dd/yyyy").parse(form.getEventDateFrom())); } catch (ParseException ex) { hibQuery.setDate("eventDateFrom", new Date()); } } if (form.getEventDateTo() != null && form.getEventDateTo().trim().length() > 0) { try { hibQuery.setDate("eventDateTo", new SimpleDateFormat("MM/dd/yyyy").parse(form.getEventDateTo())); } catch (ParseException ex) { hibQuery.setDate("eventDateTo", new Date()); } } if (form.getSponsoringOrganization() != null && form.getSponsoringOrganization() >= 0) { hibQuery.setLong("sponsorOrgId", form.getSponsoringOrganization()); } switch (form.getMode()) { case EventListForm.sModeMyEvents: hibQuery.setString("userId", form.getUserId()); break; case EventListForm.sModeAllApprovedEvents: case EventListForm.sModeAllEventsWaitingApproval: case EventListForm.sModeEvents4Approval: break; } return hibQuery.setCacheable(true).list(); }
From source file:org.unitime.timetable.webutil.WebEventTableBuilder.java
License:Open Source License
protected List<Meeting> loadMeetings(MeetingListForm form) { boolean conf = (form.getMode() == EventListForm.sModeAllConflictingEvents); String query = "select m from Event e inner join e.meetings m where e.class in ("; if (conf) {/*from w w w .j av a 2s . c o m*/ query = "select m from Event e inner join e.meetings m, Meeting mx where " + "mx.uniqueId!=m.uniqueId and m.meetingDate=mx.meetingDate and m.startPeriod < mx.stopPeriod and m.stopPeriod > mx.startPeriod and " + "m.locationPermanentId = mx.locationPermanentId and e.class in ("; } for (int i = 0; i < form.getEventTypes().length; i++) { if (i > 0) query += ","; switch (form.getEventTypes()[i].intValue()) { case Event.sEventTypeClass: query += "ClassEvent"; break; case Event.sEventTypeFinalExam: query += "FinalExamEvent"; break; case Event.sEventTypeMidtermExam: query += "MidtermExamEvent"; break; case Event.sEventTypeCourse: query += "CourseEvent"; break; case Event.sEventTypeSpecial: query += "SpecialEvent"; break; } //query += form.getEventTypes()[i]; } query += ")"; if (form.getEventNameSubstring() != null && form.getEventNameSubstring().trim().length() > 0) { query += " and upper(e.eventName) like :eventNameSubstring"; } if (form.getEventDateFrom() != null && form.getEventDateFrom().trim().length() > 0) { query += " and m.meetingDate>=:eventDateFrom"; } if (form.getEventDateTo() != null && form.getEventDateTo().trim().length() > 0) { query += " and m.meetingDate<=:eventDateTo"; } if (form.getEventMainContactSubstring() != null && form.getEventMainContactSubstring().trim().length() > 0) { for (StringTokenizer s = new StringTokenizer(form.getEventMainContactSubstring().trim(), ", "); s .hasMoreTokens();) { String token = s.nextToken().toUpperCase(); query += " and (upper(e.mainContact.firstName) like '%" + token + "%' or upper(e.mainContact.middleName) like '%" + token + "%' or upper(e.mainContact.lastName) like '%" + token + "%')"; } } if (form.getLocation() != null && form.getLocation().trim().length() > 0) { query += " and (" + "(select count(r) from Room as r where r.permanentId = m.locationPermanentId and upper(r.building.abbreviation) like :bldgSubstr and upper(r.roomNumber) like :roomSubstr) > 0 or " + "(select count(nul) from NonUniversityLocation as nul where nul.permanentId = m.locationPermanentId and upper(nul.name) like :nameStr) > 0) "; } switch (form.getMode()) { case EventListForm.sModeMyEvents: query += " and e.mainContact.externalUniqueId = :userId"; break; case EventListForm.sModeAllApprovedEvents: query += " and m.approvedDate is not null"; break; case EventListForm.sModeAllEventsWaitingApproval: query += " and m.approvedDate is null"; break; case EventListForm.sModeEvents4Approval: query += " and m.approvedDate is null"; break; case EventListForm.sModeAllEvents: break; } if (form.getSponsoringOrganization() != null && form.getSponsoringOrganization() >= 0) { query += " and e.sponsoringOrganization.uniqueId=:sponsorOrgId"; } if (form.getStartTime() >= 0) { query += " and m.stopPeriod > " + form.getStartTime(); } if (form.getStopTime() >= 0) { query += " and m.startPeriod < " + form.getStopTime(); } if (form.isDayMon() || form.isDayTue() || form.isDayWed() || form.isDayThu() || form.isDayFri() || form.isDaySat() || form.isDaySun()) { String dow = ""; if (form.isDayMon()) { if (!dow.isEmpty()) dow += ","; dow += "2"; } if (form.isDayTue()) { if (!dow.isEmpty()) dow += ","; dow += "3"; } if (form.isDayWed()) { if (!dow.isEmpty()) dow += ","; dow += "4"; } if (form.isDayThu()) { if (!dow.isEmpty()) dow += ","; dow += "5"; } if (form.isDayFri()) { if (!dow.isEmpty()) dow += ","; dow += "6"; } if (form.isDaySat()) { if (!dow.isEmpty()) dow += ","; dow += "7"; } if (form.isDaySun()) { if (!dow.isEmpty()) dow += ","; dow += "1"; } if (dow.indexOf(',') >= 0) query += " and " + HibernateUtil.dayOfWeek("m.meetingDate") + " in (" + dow + ")"; else query += " and " + HibernateUtil.dayOfWeek("m.meetingDate") + " = " + dow; } Query hibQuery = new EventDAO().getSession().createQuery(query); hibQuery.setFetchSize(getMaxResults() + 1); if (form.getMode() != EventListForm.sModeEvents4Approval) hibQuery.setMaxResults(getMaxResults() + 1); if (form.getEventNameSubstring() != null && form.getEventNameSubstring().trim().length() > 0) { hibQuery.setString("eventNameSubstring", "%" + form.getEventNameSubstring().toUpperCase().trim() + "%"); } if (form.getEventDateFrom() != null && form.getEventDateFrom().trim().length() > 0) { try { hibQuery.setDate("eventDateFrom", new SimpleDateFormat("MM/dd/yyyy").parse(form.getEventDateFrom())); } catch (ParseException ex) { hibQuery.setDate("eventDateFrom", new Date()); } } if (form.getEventDateTo() != null && form.getEventDateTo().trim().length() > 0) { try { hibQuery.setDate("eventDateTo", new SimpleDateFormat("MM/dd/yyyy").parse(form.getEventDateTo())); } catch (ParseException ex) { hibQuery.setDate("eventDateTo", new Date()); } } if (form.getSponsoringOrganization() != null && form.getSponsoringOrganization() >= 0) { hibQuery.setLong("sponsorOrgId", form.getSponsoringOrganization()); } if (form.getLocation() != null && form.getLocation().trim().length() > 0) { String bldgSubstr = null; String roomSubstr = null; String nameStr = "%" + form.getLocation().toUpperCase() + "%"; int indexOfFirstSpace = form.getLocation().indexOf(' '); if (indexOfFirstSpace == 0) { bldgSubstr = "%"; roomSubstr = "%" + form.getLocation().substring(0, indexOfFirstSpace).toUpperCase() + "%"; } else if (indexOfFirstSpace < 0) { bldgSubstr = "%" + form.getLocation().toUpperCase() + "%"; roomSubstr = "%"; } else { bldgSubstr = "%" + form.getLocation().substring(0, indexOfFirstSpace).toUpperCase() + "%"; if (indexOfFirstSpace == form.getLocation().length() - 1) { roomSubstr = "%"; } else { roomSubstr = "%" + form.getLocation() .substring(indexOfFirstSpace + 1, form.getLocation().length()).toUpperCase() + "%"; } } hibQuery.setString("bldgSubstr", bldgSubstr); hibQuery.setString("roomSubstr", roomSubstr); hibQuery.setString("nameStr", nameStr); } switch (form.getMode()) { case EventListForm.sModeMyEvents: hibQuery.setString("userId", form.getUserId()); break; case EventListForm.sModeAllApprovedEvents: case EventListForm.sModeAllEventsWaitingApproval: case EventListForm.sModeEvents4Approval: break; } List meetings = hibQuery.setCacheable(true).list(); if (form.getMode() == EventListForm.sModeEvents4Approval || (form.getLocation() != null && form.getLocation().trim().length() > 0)) { for (Iterator it = meetings.iterator(); it.hasNext();) { Meeting meeting = (Meeting) it.next(); if (form.getMode() == EventListForm.sModeEvents4Approval) { if (meeting.getApprovedDate() != null || meeting.getLocation() == null || !form .getManagingDepartments().contains(meeting.getLocation().getControllingDepartment())) { it.remove(); continue; } } if (meeting.getLocation() == null || !match(form.getLocation(), meeting.getLocation().getLabel())) { it.remove(); continue; } } } Comparator<Meeting> cmp = null; if (MeetingListForm.sOrderByName.equals(form.getOrderBy())) { cmp = new Comparator<Meeting>() { public int compare(Meeting m1, Meeting m2) { int cmp = m1.getEvent().getEventName().compareToIgnoreCase(m2.getEvent().getEventName()); if (cmp != 0) return cmp; cmp = m1.getEvent().getUniqueId().compareTo(m2.getEvent().getUniqueId()); if (cmp != 0) return cmp; return m1.compareTo(m2); } }; } else if (MeetingListForm.sOrderByLocation.equals(form.getOrderBy())) { cmp = new Comparator<Meeting>() { public int compare(Meeting m1, Meeting m2) { String l1 = (m1.getLocation() == null ? "" : m1.getLocation().getLabel()); String l2 = (m2.getLocation() == null ? "" : m2.getLocation().getLabel()); int cmp = l1.compareToIgnoreCase(l2); if (cmp != 0) return cmp; return m1.compareTo(m2); } }; } else if (MeetingListForm.sOrderByTime.equals(form.getOrderBy())) { cmp = new Comparator<Meeting>() { public int compare(Meeting m1, Meeting m2) { return m1.compareTo(m2); } }; } if (cmp != null) Collections.sort(meetings, cmp); return meetings; }
From source file:org.webcurator.domain.TargetInstanceDAOImpl.java
License:Apache License
@SuppressWarnings("unchecked") public List<QueuedTargetInstanceDTO> getQueueForTarget(final Long targetOid) { final TargetInstanceCriteria criteria = new TargetInstanceCriteria(); Set<String> states = new HashSet<String>(); states.add(TargetInstance.STATE_SCHEDULED); states.add(TargetInstance.STATE_QUEUED); criteria.setStates(states);/*from www .ja v a 2s. c o m*/ criteria.setTo(new Date()); return (List) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) { StringBuffer q = new StringBuffer(); q.append( "select new org.webcurator.domain.model.dto.QueuedTargetInstanceDTO(ti.oid, ti.scheduledTime, ti.priority, ti.state, ti.bandwidthPercent, ti.owner.agency.name) "); q.append("from TargetInstance ti where ti.scheduledTime > :ed "); q.append("and ti.state in ('Scheduled', 'Queued') and ti.target.oid = :toid "); q.append("order by ti.priority asc, ti.scheduledTime asc, ti.oid asc "); Query query = session.createQuery(q.toString()); query.setDate("ed", new Date()); query.setLong("toid", targetOid); return query.list(); } }); }
From source file:org.webcurator.domain.TargetInstanceDAOImpl.java
License:Apache License
public Long countQueueLengthForTarget(final Long targetOid) { final TargetInstanceCriteria criteria = new TargetInstanceCriteria(); Set<String> states = new HashSet<String>(); states.add(TargetInstance.STATE_SCHEDULED); states.add(TargetInstance.STATE_QUEUED); criteria.setStates(states);/* www . j av a2 s .c om*/ criteria.setTo(new Date()); return (Long) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) { StringBuffer q = new StringBuffer(); q.append("select count(*) "); q.append("from TargetInstance ti where ti.scheduledTime > :ed "); q.append("and ti.state in ('Scheduled', 'Queued') and ti.target.oid = :toid "); Query query = session.createQuery(q.toString()); query.setDate("ed", new Date()); query.setLong("toid", targetOid); return ((Integer) query.list().get(0)).longValue(); } }); }
From source file:pl.surreal.finance.transaction.db.TransactionDAO.java
License:Apache License
public List<Transaction> findByLabel(Label label, Date dateFrom, Date dateTo) { Objects.requireNonNull(label); Query query; if (dateFrom == null && dateTo == null) { query = namedQuery(findByLabelQuery); } else if (dateFrom != null && dateTo != null) { query = namedQuery(findByLabelFromToQuery); query.setDate("dateFrom", dateFrom); query.setDate("dateTo", dateTo); } else if (dateFrom != null && dateTo == null) { query = namedQuery(findByLabelFromQuery); query.setDate("dateFrom", dateFrom); } else {//from w w w . j a va 2 s . c o m query = namedQuery(findByLabelToQuery); query.setDate("dateTo", dateTo); } query.setParameter("label", Objects.requireNonNull(label)); return list(query); }
From source file:ru.codemine.ccms.dao.ActionEventDAOImpl.java
License:Open Source License
@Override public List<ActionEvent> getActiveEvents() { Query query = getSession() .createQuery("FROM ActionEvent a " + "WHERE a.startDate <= :now " + "AND a.endDate >= :now"); query.setDate("now", LocalDate.now().toDate()); return query.list(); }
From source file:ru.codemine.ccms.dao.ActionEventDAOImpl.java
License:Open Source License
@Override public List<ActionEvent> getActiveEvents(Shop shop) { Query query = getSession().createQuery("FROM ActionEvent a " + "WHERE a.startDate <= :now " + "AND a.endDate >= :now " + "AND :shop IN ELEMENTS(a.affectedShops)"); query.setDate("now", LocalDate.now().toDate()); query.setParameter("shop", shop); return query.list(); }