// 默认顺序createtime desc public List<T> findByMapWithCond(Map<String, Object> params, String orderBy, Boolean asc) { StringBuilder hql = new StringBuilder("from " + className); String orderStr; if (null == orderBy) { orderStr = " order by createtime desc"; } else { String ascStr = asc ? " asc" : " desc"; orderStr = " order by " + orderBy + ascStr; } Map<String, Object> alterParams = new HashMap<>(); Query query; if (null != params && !params.isEmpty()) { hql.append(" where "); for (String field : params.keySet()) { int i = field.indexOf(" "); Assert.isTrue(i != -1, "Wrong condition, must have space inside!"); String ramdonName = "_" + Utils.getRandomString(8); hql.append(field).append(" :").append(ramdonName).append(" and "); if (field.contains("like")) { String likeStr = "%" + params.get(field) + "%"; alterParams.put(ramdonName, likeStr); } else alterParams.put(ramdonName, params.get(field)); } hql.append("1=1"); query = getSession().createQuery(hql.append(orderStr).toString()); for (String field : alterParams.keySet()) query.setParameter(field, alterParams.get(field)); } else query = getSession().createQuery(hql.append(orderStr).toString()); return query.list(); }
/* (non-Javadoc) * @see org.openiam.idm.srvc.auth.login.LoginDAO#findInactiveUsers(int, int) */ public List<Login> findInactiveUsers(int startDays, int endDays, String managedSysId) { log.debug("findInactiveUsers called."); log.debug("Start days=" + startDays); log.debug("End days=" + endDays); boolean start = false; long curTimeMillis = System.currentTimeMillis(); Date startDate = new Date(curTimeMillis); Date endDate = new Date(curTimeMillis); StringBuilder sql = new StringBuilder( " from org.openiam.idm.srvc.auth.dto.Login l where " + " l.id.managedSysId = :managedSys and "); if (startDays != 0) { sql.append(" l.lastLogin <= :startDate "); start = true; Calendar c = Calendar.getInstance(); c.setTime(startDate); c.add(Calendar.DAY_OF_YEAR, (-1 * startDays)); startDate.setTime(c.getTimeInMillis()); log.debug("starDate = " + startDate.toString()); } if (endDays != 0) { if (start) { sql.append(" and "); } sql.append(" l.lastLogin >= :endDate "); Calendar c = Calendar.getInstance(); c.setTime(endDate); c.add(Calendar.DAY_OF_YEAR, (-1 * endDays)); endDate.setTime(c.getTimeInMillis()); log.debug("endDate = " + endDate.toString()); } Session session = sessionFactory.getCurrentSession(); Query qry = session.createQuery(sql.toString()); qry.setString("managedSys", managedSysId); if (startDays != 0) { qry.setDate("startDate", startDate); } if (endDays != 0) { qry.setDate("endDate", endDate); } List<Login> results = (List<Login>) qry.list(); if (results == null) { return (new ArrayList<Login>()); } return results; }
/** * @param session the hibernate session * @return a string in HTML listing the different subscribers present in the database */ @SuppressWarnings("unchecked") public String toHTML(Session session) { List<Media> medias = (List<Media>) session.createQuery("from Media").list(); if (medias.size() <= 0) return "No media present in the library."; StringBuilder sb = new StringBuilder(); sb.append("<p>List of medias (" + medias.size() + ") present in the library:</p>"); sb.append(Media.toHTML(medias)); return sb.toString(); }
private String getFolio(Almacen almacen) { Query query = currentSession() .createQuery( "select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId"); query.setString("nombre", "FACTURA"); query.setLong("almacenId", almacen.getId()); query.setLockOptions(LockOptions.UPGRADE); Folio folio = (Folio) query.uniqueResult(); if (folio == null) { folio = new Folio("FACTURA"); folio.setAlmacen(almacen); currentSession().save(folio); return getFolio(almacen); } folio.setValor(folio.getValor() + 1); java.text.NumberFormat nf = java.text.DecimalFormat.getInstance(); nf.setGroupingUsed(false); nf.setMinimumIntegerDigits(9); nf.setMaximumIntegerDigits(9); nf.setMaximumFractionDigits(0); StringBuilder sb = new StringBuilder(); sb.append("FA-"); sb.append(almacen.getEmpresa().getOrganizacion().getCodigo()); sb.append(almacen.getEmpresa().getCodigo()); sb.append(almacen.getCodigo()); sb.append(nf.format(folio.getValor())); return sb.toString(); }
public Map<String, Object> findByMapWithCond( final Map<String, Object> params, int page, int rows, String order, boolean asc) throws HibernateException { StringBuilder hql = new StringBuilder("from " + className); Query query, countQuery; Long total; Map<String, Object> alterParams = new HashMap<>(); if (null != params && !params.isEmpty()) { hql.append(" where "); for (String field : params.keySet()) { int i = field.indexOf(" "); Assert.isTrue(i != -1, "Wrong condition, must have space inside!"); String ramdonName = "_" + Utils.getRandomString(8); hql.append(field).append(" :").append(ramdonName).append(" and "); alterParams.put(ramdonName, params.get(field)); } hql.append("1=1"); countQuery = getSession().createQuery("SELECT count(*) " + hql.toString()); if (null != order) { hql.append(" order by ").append(order); if (asc) hql.append(" asc"); else hql.append(" desc"); } query = getSession().createQuery(hql.toString()); for (String field : alterParams.keySet()) { query.setParameter(field, alterParams.get(field)); countQuery.setParameter(field, alterParams.get(field)); } } else { countQuery = getSession().createQuery("SELECT count(*) " + hql.toString()); if (null != order) { hql.append(" order by ").append(order); if (asc) hql.append(" asc"); else hql.append(" desc"); } query = getSession().createQuery(hql.toString()); } total = (Long) countQuery.uniqueResult(); if (page > 0 && rows > 0) { query.setFirstResult((page - 1) * rows).setMaxResults(rows); } Map<String, Object> result = new HashMap<String, Object>(); result.put("total", total); result.put("rows", query.list()); return result; }