/** * Return statistics for given user * * @param userBean user * @return statistics * @throws org.trecena.ApplicationException when failed */ public UserSummaryDTO getStatisticsForUser(UserBean userBean) throws ApplicationException { UserSummaryDTO reslt = new UserSummaryDTO(); try { Session session = sessionFactory.getCurrentSession(); // make projection list to count hidden and not hidden tasks ProjectionList projList = Projections.projectionList(); projList.add(Projections.groupProperty("hidden")); projList.add(Projections.rowCount()); // count tasks List<Object[]> tasks = session .createCriteria(TaskBean.class) .add(Restrictions.eq("user", userBean)) .setProjection(projList) .list(); // reset task counter, as we will be adding later on. reslt.setTotaltasks(0L); // process results for (Object[] obj : tasks) { if ((Boolean) obj[0]) { reslt.setTotaltasks(reslt.getTotaltasks() + (Long) obj[1]); reslt.setTotalhiddentasks((Long) obj[1]); } else { reslt.setTotaltasks(reslt.getTotaltasks() + (Long) obj[1]); } } // count today activities projList = Projections.projectionList(); projList.add(Projections.rowCount()); projList.add( Projections.sqlProjection( "SUM(TIME_TO_SEC(TIMEDIFF({alias}.`stop`, {alias}.`start`))) as workedtime", new String[] {"workedtime"}, new Type[] {StandardBasicTypes.LONG})); Calendar cal = Calendar.getInstance(Locale.getDefault()); // get day start time cal.add( Calendar.MILLISECOND, -(cal.get(Calendar.HOUR) * 1000 * 60 * 60 + cal.get(Calendar.MINUTE) * 1000 * 60 + cal.get(Calendar.SECOND) * 1000 + cal.get(Calendar.MILLISECOND) + 1)); Date dayStart = cal.getTime(); // get day end time cal.add(Calendar.DAY_OF_YEAR, 1); cal.add(Calendar.MILLISECOND, 2); Date dayEnd = cal.getTime(); // count activities and time List<Object[]> activities = session .createCriteria(ActivityBean.class) .add(Restrictions.eq("user", userBean)) .add(Restrictions.between("start", dayStart, dayEnd)) .setProjection(projList) .list(); // process results for (Object[] obj : activities) { reslt.setTodayactivities((null != obj[0] ? (Long) obj[0] : 0)); reslt.setTodayworkedtime((null != obj[1] ? (long) Math.round((Long) obj[1] / 60) : 0)); } // return DTO return reslt; } catch (Exception ex) { logger.error("[STATMNGR00001] Unable to return system statistics for the user.", ex); throw new ApplicationException( "STATMNGR00001", "Unable to return system statistics for the user.", ex); } }