List of usage examples for org.hibernate Query getQueryString
String getQueryString();
From source file:org.sakaiproject.component.app.messageforums.ui.PrivateMessageManagerImpl.java
License:Educational Community License
public List getMessagesByTypeByContext(final String typeUuid, final String contextId, final String userId, final String orderField, final String order) { if (LOG.isDebugEnabled()) { LOG.debug("getMessagesByTypeForASite(typeUuid:" + typeUuid + ")"); }/* w w w . j av a2s. com*/ HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.getNamedQuery(QUERY_MESSAGES_BY_USER_TYPE_AND_CONTEXT); Query qOrdered = session.createQuery(q.getQueryString() + " order by " + orderField + " " + order); qOrdered.setParameter("userId", userId, Hibernate.STRING); qOrdered.setParameter("typeUuid", typeUuid, Hibernate.STRING); qOrdered.setParameter("contextId", contextId, Hibernate.STRING); return qOrdered.list(); } }; return (List) getHibernateTemplate().execute(hcb); }
From source file:org.sakaiproject.sitestats.impl.event.detailed.DetailedEventsManagerImpl.java
License:Educational Community License
@Override public Optional<DetailedEvent> getDetailedEventById(final long id) { if (!statMan.isDisplayDetailedEvents()) { return Optional.empty(); }/* ww w .j ava 2 s . com*/ HibernateCallback<Optional<DetailedEvent>> hcb = session -> { Query q = session.createQuery(HQL_BY_ID); q.setLong("id", id); if (log.isDebugEnabled()) { log.debug("getDetailedEvents(): " + q.getQueryString()); } List<Object[]> records = q.list(); if (records.size() > 1) { log.error("getDetailedEvents(): query for id " + id + " returned more than one result."); return Optional.empty(); } else if (records.isEmpty()) { return Optional.empty(); } Object[] record = records.get(0); String userID = (String) record[1]; String siteID = (String) record[5]; // Only return the event if the current user is is allowed to track, and the target user is allowed to be tracked in the site if (statsAuthz.canCurrentUserTrackInSite(siteID) && statsAuthz.canUserBeTracked(siteID, userID)) { DetailedEvent de = new DetailedEventImpl(); de.setId((Long) record[0]); de.setUserId(userID); de.setEventDate((Date) record[2]); de.setEventId((String) record[3]); de.setEventRef((String) record[4]); de.setSiteId(siteID); // do not return if anonymous if (!regServ.getAnonymousEventIds().contains(de.getEventId())) { return Optional.of(de); } } return Optional.empty(); }; return getHibernateTemplate().execute(hcb); }
From source file:org.sakaiproject.sitestats.impl.StatsManagerImpl.java
License:Educational Community License
public List<Stat> getEventStats(final String siteId, final List<String> events, final Date iDate, final Date fDate, final List<String> userIds, final boolean inverseUserSelection, final PagingPosition page, final List<String> totalsBy, final String sortBy, boolean sortAscending, final int maxResults) { final Set<String> anonymousEvents = M_ers.getAnonymousEventIds(); StatsSqlBuilder sqlBuilder = new StatsSqlBuilder(getDbVendor(), Q_TYPE_EVENT, totalsBy, siteId, events, anonymousEvents, showAnonymousAccessEvents, null, null, iDate, fDate, userIds, inverseUserSelection, sortBy, sortAscending);/*from ww w . j a va 2s. c om*/ final String hql = sqlBuilder.getHQL(); final Map<Integer, Integer> columnMap = sqlBuilder.getHQLColumnMap(); // DO IT! HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(hql); if (siteId != null) { q.setString("siteid", siteId); } if (events != null) { if (events.isEmpty()) { events.add(""); } q.setParameterList("events", events); } if (userIds != null && !userIds.isEmpty()) { if (userIds.size() <= 1000) { q.setParameterList("users", userIds); } else { int nUsers = userIds.size(); int blockId = 0, startIndex = 0; int blocks = (int) (nUsers / 1000); blocks = (blocks * 1000 == nUsers) ? blocks : blocks + 1; for (int i = 0; i < blocks - 1; i++) { q.setParameterList("users" + blockId, userIds.subList(startIndex, startIndex + 1000)); blockId++; startIndex += 1000; } q.setParameterList("users" + blockId, userIds.subList(startIndex, nUsers)); } } if (iDate != null) q.setDate("idate", iDate); if (fDate != null) { // adjust final date Calendar c = Calendar.getInstance(); c.setTime(fDate); c.add(Calendar.DAY_OF_YEAR, 1); Date fDate2 = c.getTime(); q.setDate("fdate", fDate2); } if (columnMap.containsKey(StatsSqlBuilder.C_USER) && anonymousEvents != null && anonymousEvents.size() > 0) { q.setParameterList("anonymousEvents", anonymousEvents); } if (page != null) { q.setFirstResult(page.getFirst() - 1); q.setMaxResults(page.getLast() - page.getFirst() + 1); } if (maxResults > 0) { q.setMaxResults(maxResults); } LOG.debug("getEventStats(): " + q.getQueryString()); List<Object[]> records = q.list(); List<Stat> results = new ArrayList<Stat>(); Set<String> siteUserIds = null; if (inverseUserSelection) siteUserIds = getSiteUsers(siteId); if (records.size() > 0) { Calendar cal = Calendar.getInstance(); Map<String, ToolInfo> eventIdToolMap = M_ers.getEventIdToolMap(); boolean groupByTool = columnMap.containsKey(StatsSqlBuilder.C_TOOL) && !columnMap.containsKey(StatsSqlBuilder.C_EVENT); boolean hasVisitsData = columnMap.containsKey(StatsSqlBuilder.C_VISITS); for (Iterator<Object[]> iter = records.iterator(); iter.hasNext();) { if (!inverseUserSelection) { Object[] s = iter.next(); Stat c = null; String toolId = null; if (!hasVisitsData) { c = new EventStatImpl(); } else { c = new SiteVisitsImpl(); } if (columnMap.containsKey(StatsSqlBuilder.C_SITE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_SITE); c.setSiteId((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_USER)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_USER); c.setUserId((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_EVENT) && !hasVisitsData) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_EVENT); ((EventStat) c).setEventId((String) s[ix]); ToolInfo ti = eventIdToolMap.get((String) s[ix]); toolId = ti != null ? ti.getToolId() : (String) s[ix]; ((EventStat) c).setToolId(toolId); } if (columnMap.containsKey(StatsSqlBuilder.C_TOOL) && !hasVisitsData) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_TOOL); ToolInfo ti = eventIdToolMap.get((String) s[ix]); toolId = ti != null ? ti.getToolId() : (String) s[ix]; // ((EventStat) c).setToolId(toolId); } if (columnMap.containsKey(StatsSqlBuilder.C_DATE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_DATE); c.setDate((Date) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_DATEMONTH) && columnMap.containsKey(StatsSqlBuilder.C_DATEYEAR)) { int ixY = (Integer) columnMap.get(StatsSqlBuilder.C_DATEYEAR); int ixM = (Integer) columnMap.get(StatsSqlBuilder.C_DATEMONTH); int yr = 0, mo = 0; if (getDbVendor().equals("oracle")) { yr = Integer.parseInt((String) s[ixY]); mo = Integer.parseInt((String) s[ixM]) - 1; } else { yr = ((Integer) s[ixY]).intValue(); mo = ((Integer) s[ixM]).intValue() - 1; } cal.set(Calendar.YEAR, yr); cal.set(Calendar.MONTH, mo); c.setDate(cal.getTime()); } else if (columnMap.containsKey(StatsSqlBuilder.C_DATEYEAR)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_DATEYEAR); int yr = 0; if (getDbVendor().equals("oracle")) { yr = Integer.parseInt((String) s[ix]); } else { yr = ((Integer) s[ix]).intValue(); } cal.set(Calendar.YEAR, yr); c.setDate(cal.getTime()); } if (columnMap.containsKey(StatsSqlBuilder.C_TOTAL)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_TOTAL); c.setCount(c.getCount() + ((Long) s[ix]).longValue()); } if (columnMap.containsKey(StatsSqlBuilder.C_VISITS)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_VISITS); try { ((SiteVisits) c).setTotalVisits(((Long) s[ix]).longValue()); } catch (ClassCastException cce) { ((SiteVisits) c).setTotalVisits(((Integer) s[ix]).intValue()); } } if (columnMap.containsKey(StatsSqlBuilder.C_UNIQUEVISITS)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_UNIQUEVISITS); try { ((SiteVisits) c).setTotalUnique(((Long) s[ix]).longValue()); } catch (ClassCastException cce) { ((SiteVisits) c).setTotalUnique(((Integer) s[ix]).intValue()); } } if (!groupByTool) { results.add(c); } else { // Special case: // - group by tool (& event not part of grouping) boolean toolAggregated = false; for (Stat s_ : results) { EventStat es_ = (EventStat) s_; if (es_.equalExceptForCount(c)) { es_.setCount(es_.getCount() + c.getCount()); toolAggregated = true; break; } } if (!toolAggregated) { results.add(c); } } } else { if (siteUserIds != null) { siteUserIds.remove((Object) iter.next()); } } } } if (inverseUserSelection) { long id = 0; Iterator<String> iU = siteUserIds.iterator(); while (iU.hasNext()) { String userId = iU.next(); EventStat c = new EventStatImpl(); c.setId(id++); c.setUserId(userId); c.setSiteId(siteId); c.setCount(0); results.add(c); } } // hack for hibernate-oracle bug producing duplicate lines else if (getDbVendor().equals("oracle") && totalsBy.contains(T_USER) && anonymousEvents != null && anonymousEvents.size() > 0) { List<Stat> consolidated = new ArrayList<Stat>(); for (Stat s : results) { EventStat es = (EventStat) s; boolean found = false; for (Stat c : consolidated) { EventStat esc = (EventStat) c; if (esc.equalExceptForCount((Object) es)) { esc.setCount(esc.getCount() + es.getCount()); found = true; break; } } if (!found) { consolidated.add(es); } } results = consolidated; } return results; } }; return (List<Stat>) getHibernateTemplate().execute(hcb); }
From source file:org.sakaiproject.sitestats.impl.StatsManagerImpl.java
License:Educational Community License
public int getEventStatsRowCount(final String siteId, final List<String> events, final Date iDate, final Date fDate, final List<String> userIds, final boolean inverseUserSelection, final List<String> totalsBy) { final Set<String> anonymousEvents = M_ers.getAnonymousEventIds(); StatsSqlBuilder sqlBuilder = new StatsSqlBuilder(getDbVendor(), Q_TYPE_EVENT, totalsBy, siteId, events, anonymousEvents, showAnonymousAccessEvents, null, null, iDate, fDate, userIds, inverseUserSelection, null, true);/* w w w. j ava 2 s . co m*/ final String hql = sqlBuilder.getHQL(); final Map<Integer, Integer> columnMap = sqlBuilder.getHQLColumnMap(); // DO IT! HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(hql); if (siteId != null) { q.setString("siteid", siteId); } if (events != null && !events.isEmpty()) { q.setParameterList("events", events); } if (userIds != null && !userIds.isEmpty()) { if (userIds.size() <= 1000) { q.setParameterList("users", userIds); } else { int nUsers = userIds.size(); int blockId = 0, startIndex = 0; int blocks = (int) (nUsers / 1000); blocks = (blocks * 1000 == nUsers) ? blocks : blocks + 1; for (int i = 0; i < blocks - 1; i++) { q.setParameterList("users" + blockId, userIds.subList(startIndex, startIndex + 1000)); blockId++; startIndex += 1000; } q.setParameterList("users" + blockId, userIds.subList(startIndex, nUsers)); } } if (iDate != null) q.setDate("idate", iDate); if (fDate != null) { // adjust final date Calendar c = Calendar.getInstance(); c.setTime(fDate); c.add(Calendar.DAY_OF_YEAR, 1); Date fDate2 = c.getTime(); q.setDate("fdate", fDate2); } if (columnMap.containsKey(StatsSqlBuilder.C_USER) && anonymousEvents != null && anonymousEvents.size() > 0) { q.setParameterList("anonymousEvents", anonymousEvents); } LOG.debug("getEventStatsRowCount(): " + q.getQueryString()); Integer rowCount = q.list().size(); if (!inverseUserSelection) { return rowCount; } else { return getSiteUsers(siteId).size() - rowCount; } } }; return (Integer) getHibernateTemplate().execute(hcb); }
From source file:org.sakaiproject.sitestats.impl.StatsManagerImpl.java
License:Educational Community License
public List<Stat> getPresenceStats(final String siteId, final Date iDate, final Date fDate, final List<String> userIds, final boolean inverseUserSelection, final PagingPosition page, final List<String> totalsBy, final String sortBy, boolean sortAscending, final int maxResults) { StatsSqlBuilder sqlBuilder = new StatsSqlBuilder(getDbVendor(), Q_TYPE_PRESENCE, totalsBy, siteId, (Set<String>) null, null, showAnonymousAccessEvents, null, null, iDate, fDate, userIds, inverseUserSelection, sortBy, sortAscending); final String hql = sqlBuilder.getHQL(); final Map<Integer, Integer> columnMap = sqlBuilder.getHQLColumnMap(); // DO IT!/*from ww w. j a va 2s . c om*/ HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(hql); if (siteId != null) { q.setString("siteid", siteId); } if (userIds != null && !userIds.isEmpty()) { if (userIds.size() <= 1000) { q.setParameterList("users", userIds); } else { int nUsers = userIds.size(); int blockId = 0, startIndex = 0; int blocks = (int) (nUsers / 1000); blocks = (blocks * 1000 == nUsers) ? blocks : blocks + 1; for (int i = 0; i < blocks - 1; i++) { q.setParameterList("users" + blockId, userIds.subList(startIndex, startIndex + 1000)); blockId++; startIndex += 1000; } q.setParameterList("users" + blockId, userIds.subList(startIndex, nUsers)); } } if (iDate != null) q.setDate("idate", iDate); if (fDate != null) { // adjust final date Calendar c = Calendar.getInstance(); c.setTime(fDate); c.add(Calendar.DAY_OF_YEAR, 1); Date fDate2 = c.getTime(); q.setDate("fdate", fDate2); } if (page != null) { q.setFirstResult(page.getFirst() - 1); q.setMaxResults(page.getLast() - page.getFirst() + 1); } if (maxResults > 0) { q.setMaxResults(maxResults); } LOG.debug("getPresenceStats(): " + q.getQueryString()); List<Object[]> records = q.list(); List<Stat> results = new ArrayList<Stat>(); Set<String> siteUserIds = null; if (inverseUserSelection) siteUserIds = getSiteUsers(siteId); if (records.size() > 0) { Calendar cal = Calendar.getInstance(); for (Iterator<Object[]> iter = records.iterator(); iter.hasNext();) { if (!inverseUserSelection) { Object[] s = iter.next(); SitePresence c = new SitePresenceImpl(); if (columnMap.containsKey(StatsSqlBuilder.C_SITE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_SITE); c.setSiteId((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_USER)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_USER); c.setUserId((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_DATE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_DATE); c.setDate((Date) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_DATEMONTH) && columnMap.containsKey(StatsSqlBuilder.C_DATEYEAR)) { int ixY = (Integer) columnMap.get(StatsSqlBuilder.C_DATEYEAR); int ixM = (Integer) columnMap.get(StatsSqlBuilder.C_DATEMONTH); int yr = 0, mo = 0; if (getDbVendor().equals("oracle")) { yr = Integer.parseInt((String) s[ixY]); mo = Integer.parseInt((String) s[ixM]) - 1; } else { yr = ((Integer) s[ixY]).intValue(); mo = ((Integer) s[ixM]).intValue() - 1; } cal.set(Calendar.YEAR, yr); cal.set(Calendar.MONTH, mo); c.setDate(cal.getTime()); } else if (columnMap.containsKey(StatsSqlBuilder.C_DATEYEAR)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_DATEYEAR); int yr = 0; if (getDbVendor().equals("oracle")) { yr = Integer.parseInt((String) s[ix]); } else { yr = ((Integer) s[ix]).intValue(); } cal.set(Calendar.YEAR, yr); c.setDate(cal.getTime()); } { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_DURATION); c.setDuration(c.getDuration() + ((Long) s[ix]).longValue()); } results.add(c); } else { if (siteUserIds != null) { siteUserIds.remove((Object) iter.next()); } } } } if (inverseUserSelection) { long id = 0; Iterator<String> iU = siteUserIds.iterator(); while (iU.hasNext()) { String userId = iU.next(); SitePresence c = new SitePresenceImpl(); c.setId(id++); c.setUserId(userId); c.setSiteId(siteId); c.setDuration(0); c.setCount(0); results.add(c); } } return results; } }; return (List<Stat>) getHibernateTemplate().execute(hcb); }
From source file:org.sakaiproject.sitestats.impl.StatsManagerImpl.java
License:Educational Community License
public int getPresenceStatsRowCount(final String siteId, final Date iDate, final Date fDate, final List<String> userIds, final boolean inverseUserSelection, final List<String> totalsBy) { StatsSqlBuilder sqlBuilder = new StatsSqlBuilder(getDbVendor(), Q_TYPE_PRESENCE, totalsBy, null, (Set<String>) null, null, showAnonymousAccessEvents, null, null, iDate, fDate, userIds, inverseUserSelection, null, true); final String hql = sqlBuilder.getHQL(); // DO IT!// w w w .j av a2 s.c o m HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(hql); if (siteId != null) { q.setString("siteid", siteId); } if (userIds != null && !userIds.isEmpty()) { if (userIds.size() <= 1000) { q.setParameterList("users", userIds); } else { int nUsers = userIds.size(); int blockId = 0, startIndex = 0; int blocks = (int) (nUsers / 1000); blocks = (blocks * 1000 == nUsers) ? blocks : blocks + 1; for (int i = 0; i < blocks - 1; i++) { q.setParameterList("users" + blockId, userIds.subList(startIndex, startIndex + 1000)); blockId++; startIndex += 1000; } q.setParameterList("users" + blockId, userIds.subList(startIndex, nUsers)); } } if (iDate != null) q.setDate("idate", iDate); if (fDate != null) { // adjust final date Calendar c = Calendar.getInstance(); c.setTime(fDate); c.add(Calendar.DAY_OF_YEAR, 1); Date fDate2 = c.getTime(); q.setDate("fdate", fDate2); } LOG.debug("getPresenceStatsRowCount(): " + q.getQueryString()); Integer rowCount = q.list().size(); if (!inverseUserSelection) { return rowCount; } else { return getSiteUsers(siteId).size() - rowCount; } } }; return (Integer) getHibernateTemplate().execute(hcb); }
From source file:org.sakaiproject.sitestats.impl.StatsManagerImpl.java
License:Educational Community License
public Map<String, SitePresenceTotal> getPresenceTotalsForSite(final String siteId) { HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { String hql = "FROM SitePresenceTotalImpl st WHERE st.siteId = :siteId"; Query q = session.createQuery(hql); q.setString("siteId", siteId); LOG.debug("getPresenceTotalsForSite(): " + q.getQueryString()); return q.list(); }// www. j a v a 2s .c o m }; final Map<String, SitePresenceTotal> totals = new HashMap<String, SitePresenceTotal>(); List<SitePresenceTotal> siteTotals = (List<SitePresenceTotal>) getHibernateTemplate().execute(hcb); for (SitePresenceTotal total : siteTotals) { totals.put(total.getUserId(), total); } return totals; }
From source file:org.sakaiproject.sitestats.impl.StatsManagerImpl.java
License:Educational Community License
public List<Stat> getResourceStats(final String siteId, final String resourceAction, final List<String> resourceIds, final Date iDate, final Date fDate, final List<String> userIds, final boolean inverseUserSelection, final PagingPosition page, final List<String> totalsBy, final String sortBy, final boolean sortAscending, final int maxResults) { StatsSqlBuilder sqlBuilder = new StatsSqlBuilder(getDbVendor(), Q_TYPE_RESOURCE, totalsBy, siteId, (Set<String>) null, null, showAnonymousAccessEvents, resourceAction, resourceIds, iDate, fDate, userIds, inverseUserSelection, sortBy, sortAscending); final String hql = sqlBuilder.getHQL(); final Map<Integer, Integer> columnMap = sqlBuilder.getHQLColumnMap(); HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(hql); if (siteId != null) { q.setString("siteid", siteId); }/* w w w . j a v a 2 s . c o m*/ if (userIds != null && !userIds.isEmpty()) { if (userIds.size() <= 1000) { q.setParameterList("users", userIds); } else { int nUsers = userIds.size(); int blockId = 0, startIndex = 0; int blocks = (int) (nUsers / 1000); blocks = (blocks * 1000 == nUsers) ? blocks : blocks + 1; for (int i = 0; i < blocks - 1; i++) { q.setParameterList("users" + blockId, userIds.subList(startIndex, startIndex + 1000)); blockId++; startIndex += 1000; } q.setParameterList("users" + blockId, userIds.subList(startIndex, nUsers)); } } if (resourceAction != null) q.setString("action", resourceAction); if (resourceIds != null && !resourceIds.isEmpty()) { List<String> simpleResourceIds = new ArrayList<String>(); List<String> wildcardResourceIds = new ArrayList<String>(); for (String rId : resourceIds) { if (rId.endsWith("/")) { wildcardResourceIds.add(rId + "%"); } else { simpleResourceIds.add(rId); } } if (simpleResourceIds.size() > 0) { q.setParameterList("resources", resourceIds); } for (int i = 0; i < wildcardResourceIds.size(); i++) { q.setString("resource" + i, wildcardResourceIds.get(i)); } } if (iDate != null) q.setDate("idate", iDate); if (fDate != null) { // adjust final date Calendar c = Calendar.getInstance(); c.setTime(fDate); c.add(Calendar.DAY_OF_YEAR, 1); Date fDate2 = c.getTime(); q.setDate("fdate", fDate2); } if (page != null) { q.setFirstResult(page.getFirst() - 1); q.setMaxResults(page.getLast() - page.getFirst() + 1); } if (maxResults > 0) { q.setMaxResults(maxResults); } LOG.debug("getResourceStats(): " + q.getQueryString()); List<Object[]> records = q.list(); List<ResourceStat> results = new ArrayList<ResourceStat>(); Set<String> siteUserIds = null; if (inverseUserSelection) { siteUserIds = getSiteUsers(siteId); } if (records.size() > 0) { Calendar cal = Calendar.getInstance(); for (Iterator<Object[]> iter = records.iterator(); iter.hasNext();) { if (!inverseUserSelection) { Object[] s = iter.next(); ResourceStat c = new ResourceStatImpl(); if (columnMap.containsKey(StatsSqlBuilder.C_SITE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_SITE); c.setSiteId((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_USER)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_USER); c.setUserId((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_RESOURCE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_RESOURCE); c.setResourceRef((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_RESOURCE_ACTION)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_RESOURCE_ACTION); c.setResourceAction((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_DATE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_DATE); c.setDate((Date) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_DATEMONTH) && columnMap.containsKey(StatsSqlBuilder.C_DATEYEAR)) { int ixY = (Integer) columnMap.get(StatsSqlBuilder.C_DATEYEAR); int ixM = (Integer) columnMap.get(StatsSqlBuilder.C_DATEMONTH); int yr = 0, mo = 0; if (getDbVendor().equals("oracle")) { yr = Integer.parseInt((String) s[ixY]); mo = Integer.parseInt((String) s[ixM]) - 1; } else { yr = ((Integer) s[ixY]).intValue(); mo = ((Integer) s[ixM]).intValue() - 1; } cal.set(Calendar.YEAR, yr); cal.set(Calendar.MONTH, mo); c.setDate(cal.getTime()); } else if (columnMap.containsKey(StatsSqlBuilder.C_DATEYEAR)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_DATEYEAR); int yr = 0; if (getDbVendor().equals("oracle")) { yr = Integer.parseInt((String) s[ix]); } else { yr = ((Integer) s[ix]).intValue(); } cal.set(Calendar.YEAR, yr); c.setDate(cal.getTime()); } if (columnMap.containsKey(StatsSqlBuilder.C_TOTAL)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_TOTAL); c.setCount(((Long) s[ix]).longValue()); } results.add(c); } else { if (siteUserIds != null) { siteUserIds.remove((Object) iter.next()); } } } } if (inverseUserSelection) { long id = 0; Iterator<String> iU = siteUserIds.iterator(); while (iU.hasNext()) { String userId = iU.next(); ResourceStat c = new ResourceStatImpl(); c.setId(id++); c.setUserId(userId); c.setSiteId(siteId); c.setCount(0); results.add(c); } } return results; } }; return (List<Stat>) getHibernateTemplate().execute(hcb); }
From source file:org.sakaiproject.sitestats.impl.StatsManagerImpl.java
License:Educational Community License
public List<Stat> getLessonBuilderStats(final String siteId, final String pageAction, final List<String> resourceIds, final Date iDate, final Date fDate, final List<String> userIds, final boolean inverseUserSelection, final PagingPosition page, final List<String> totalsBy, final String sortBy, final boolean sortAscending, final int maxResults) { StatsSqlBuilder sqlBuilder = new StatsSqlBuilder(getDbVendor(), Q_TYPE_LESSON, totalsBy, siteId, (Set<String>) null, null, false, pageAction, resourceIds, iDate, fDate, userIds, inverseUserSelection, sortBy, sortAscending); final String hql = sqlBuilder.getHQL(); final Map<Integer, Integer> columnMap = sqlBuilder.getHQLColumnMap(); HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(hql); q.setString("siteid", siteId); if (userIds != null && !userIds.isEmpty()) { if (userIds.size() <= 1000) { q.setParameterList("users", userIds); } else { int nUsers = userIds.size(); int blockId = 0, startIndex = 0; int blocks = (int) (nUsers / 1000); blocks = (blocks * 1000 == nUsers) ? blocks : blocks + 1; for (int i = 0; i < blocks - 1; i++) { q.setParameterList("users" + blockId, userIds.subList(startIndex, startIndex + 1000)); blockId++;//from ww w. j a v a 2 s .c o m startIndex += 1000; } q.setParameterList("users" + blockId, userIds.subList(startIndex, nUsers)); } } if (pageAction != null) { q.setString("action", pageAction); } if (resourceIds != null && !resourceIds.isEmpty()) { List<String> simpleResourceIds = new ArrayList<String>(); List<String> wildcardResourceIds = new ArrayList<String>(); for (String rId : resourceIds) { if (rId.endsWith("/")) { wildcardResourceIds.add(rId + "%"); } else { simpleResourceIds.add(rId); } } if (simpleResourceIds.size() > 0) { q.setParameterList("resources", resourceIds); } for (int i = 0; i < wildcardResourceIds.size(); i++) { q.setString("resource" + i, wildcardResourceIds.get(i)); } } if (iDate != null) { q.setDate("idate", iDate); } if (fDate != null) { // adjust final date Calendar c = Calendar.getInstance(); c.setTime(fDate); c.add(Calendar.DAY_OF_YEAR, 1); Date fDate2 = c.getTime(); q.setDate("fdate", fDate2); } if (page != null) { q.setFirstResult(page.getFirst() - 1); q.setMaxResults(page.getLast() - page.getFirst() + 1); } if (maxResults > 0) { q.setMaxResults(maxResults); } if (LOG.isDebugEnabled()) { LOG.debug("getLessonBuilderStats(): " + q.getQueryString()); } List<Object[]> records = q.list(); List<LessonBuilderStat> results = new ArrayList<LessonBuilderStat>(); Set<String> siteUserIds = null; if (inverseUserSelection) { siteUserIds = getSiteUsers(siteId); } if (records.size() > 0) { Calendar cal = Calendar.getInstance(); for (Iterator<Object[]> iter = records.iterator(); iter.hasNext();) { if (!inverseUserSelection) { Object[] s = iter.next(); LessonBuilderStat stat = new LessonBuilderStatImpl(); if (columnMap.containsKey(StatsSqlBuilder.C_SITE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_SITE); stat.setSiteId((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_USER)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_USER); stat.setUserId((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_PAGE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_PAGE); stat.setPageRef((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_PAGE_ACTION)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_PAGE_ACTION); stat.setPageAction((String) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_PAGE_ID)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_PAGE_ID); stat.setPageId((Long) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_DATE)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_DATE); stat.setDate((Date) s[ix]); } if (columnMap.containsKey(StatsSqlBuilder.C_DATEMONTH) && columnMap.containsKey(StatsSqlBuilder.C_DATEYEAR)) { int ixY = (Integer) columnMap.get(StatsSqlBuilder.C_DATEYEAR); int ixM = (Integer) columnMap.get(StatsSqlBuilder.C_DATEMONTH); int yr = 0, mo = 0; if (getDbVendor().equals("oracle")) { yr = Integer.parseInt((String) s[ixY]); mo = Integer.parseInt((String) s[ixM]) - 1; } else { yr = ((Integer) s[ixY]).intValue(); mo = ((Integer) s[ixM]).intValue() - 1; } cal.set(Calendar.YEAR, yr); cal.set(Calendar.MONTH, mo); stat.setDate(cal.getTime()); } else if (columnMap.containsKey(StatsSqlBuilder.C_DATEYEAR)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_DATEYEAR); int yr = 0; if (getDbVendor().equals("oracle")) { yr = Integer.parseInt((String) s[ix]); } else { yr = ((Integer) s[ix]).intValue(); } cal.set(Calendar.YEAR, yr); stat.setDate(cal.getTime()); } if (columnMap.containsKey(StatsSqlBuilder.C_TOTAL)) { int ix = (Integer) columnMap.get(StatsSqlBuilder.C_TOTAL); Long total = (Long) s[ix]; if (total != null) { stat.setCount(total.longValue()); } } stat.setPageTitle(getLessonPageTitle(stat.getPageId())); results.add(stat); } else { if (siteUserIds != null) { siteUserIds.remove((Object) iter.next()); } } } } if (inverseUserSelection) { long id = 0; Iterator<String> iU = siteUserIds.iterator(); while (iU.hasNext()) { String userId = iU.next(); LessonBuilderStat c = new LessonBuilderStatImpl(); c.setId(id++); c.setUserId(userId); c.setSiteId(siteId); c.setCount(0); results.add(c); } } return results; } }; return (List<Stat>) getHibernateTemplate().execute(hcb); }
From source file:org.sakaiproject.sitestats.impl.StatsManagerImpl.java
License:Educational Community License
public int getResourceStatsRowCount(final String siteId, final String resourceAction, final List<String> resourceIds, final Date iDate, final Date fDate, final List<String> userIds, final boolean inverseUserSelection, final List<String> totalsBy) { StatsSqlBuilder sqlBuilder = new StatsSqlBuilder(getDbVendor(), Q_TYPE_RESOURCE, totalsBy, siteId, (Set<String>) null, null, showAnonymousAccessEvents, resourceAction, resourceIds, iDate, fDate, userIds, inverseUserSelection, null, true); final String hql = sqlBuilder.getHQL(); HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(hql); if (siteId != null) { q.setString("siteid", siteId); }/*from w w w . j a va 2 s . c o m*/ if (userIds != null && !userIds.isEmpty()) { if (userIds.size() <= 1000) { q.setParameterList("users", userIds); } else { int nUsers = userIds.size(); int blockId = 0, startIndex = 0; int blocks = (int) (nUsers / 1000); blocks = (blocks * 1000 == nUsers) ? blocks : blocks + 1; for (int i = 0; i < blocks - 1; i++) { q.setParameterList("users" + blockId, userIds.subList(startIndex, startIndex + 1000)); blockId++; startIndex += 1000; } q.setParameterList("users" + blockId, userIds.subList(startIndex, nUsers)); } } if (resourceAction != null) q.setString("action", resourceAction); if (resourceIds != null && !resourceIds.isEmpty()) q.setParameterList("resources", resourceIds); if (iDate != null) q.setDate("idate", iDate); if (fDate != null) { // adjust final date Calendar c = Calendar.getInstance(); c.setTime(fDate); c.add(Calendar.DAY_OF_YEAR, 1); Date fDate2 = c.getTime(); q.setDate("fdate", fDate2); } LOG.debug("getEventStatsRowCount(): " + q.getQueryString()); Integer rowCount = q.list().size(); if (!inverseUserSelection) { return rowCount; } else { return getSiteUsers(siteId).size() - rowCount; } } }; return (Integer) getHibernateTemplate().execute(hcb); }