/* (non-Javadoc)
  * @see fr.certu.chouette.dao.IDaoTemplate#getAll()
  */
 public List<T> getAll() {
   logger.debug("invoke getAll on " + type.getSimpleName());
   // return getHibernateTemplate().loadAll(type);
   // wrong call, may contains duplicate entry if join clause
   Filter f = Filter.getNewEmptyFilter();
   return select(f);
 }
Esempio n. 2
0
  @Transactional
  @Override
  public void removeAll(User user, List<Company> companies, boolean propagate)
      throws ChouetteException {
    for (Company company : companies) {

      INeptuneManager<Line> lineManager = (INeptuneManager<Line>) getManager(Line.class);
      INeptuneManager<VehicleJourney> vjManager =
          (INeptuneManager<VehicleJourney>) getManager(VehicleJourney.class);
      Filter filter = Filter.getNewEqualsFilter("company.id", company.getId());
      List<Line> lines = lineManager.getAll(user, filter);
      List<VehicleJourney> vehicleJourneys = vjManager.getAll(user, filter);
      if (propagate) {
        lineManager.removeAll(user, lines, propagate);
        vjManager.removeAll(user, vehicleJourneys, propagate);
      } else {
        for (Line line : lines) {
          line.setCompany(null);
          lineManager.update(user, line);
        }
        for (VehicleJourney vehicleJourney : vehicleJourneys) {
          vehicleJourney.setCompany(null);
          vjManager.update(user, vehicleJourney);
        }
      }
    }
    super.removeAll(user, companies, propagate);
  }
 @Override
 public long count(Filter clause) {
   if (clause == null) clause = Filter.getNewEmptyFilter();
   Session session = getSession();
   FilterToHibernateClauseTranslator translator = new FilterToHibernateClauseTranslator();
   String hql = translator.translateToHQLCount(clause, getSessionFactory().getClassMetadata(type));
   logger.debug("hql = " + hql);
   if (translator.getValues().isEmpty())
     return ((Long) session.createQuery(hql).uniqueResult()).longValue();
   else {
     Query query = session.createQuery(hql);
     int pos = 0;
     for (Object value : translator.getValues()) {
       query.setParameter(pos++, value);
     }
     return ((Long) query.uniqueResult()).longValue();
   }
 }
  /* (non-Javadoc)
   * @see fr.certu.chouette.dao.IDaoTemplate#select(fr.certu.chouette.filter.Filter)
   */
  @SuppressWarnings("unchecked")
  public List<T> select(final Filter filter) {
    logger.debug("invoke select on " + type.getSimpleName());

    Session session = getSession();

    Criteria criteria = session.createCriteria(type);

    // DetachedCriteria criteria = DetachedCriteria.forClass(type);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    if (!filter.isEmpty()) {
      FilterToHibernateClauseTranslator translator = new FilterToHibernateClauseTranslator();
      criteria.add(
          translator.translate(filter, criteria, getSessionFactory().getClassMetadata(type)));
    }
    if (filter.getOrderList() != null) {
      for (FilterOrder order : filter.getOrderList()) {
        switch (order.getType()) {
          case ASC:
            criteria.addOrder(Order.asc(order.getAttribute()));
            break;
          case DESC:
            criteria.addOrder(Order.desc(order.getAttribute()));
            break;

          default:
            break;
        }
      }
    }
    // HibernateTemplate ht = getHibernateTemplate();
    List<T> beans = null;
    if (filter.getLimit() > 0 || filter.getStart() > 0) {
      logger.debug("call with start and/or limit");
      criteria.setFirstResult(filter.getStart());
      criteria.setMaxResults(filter.getLimit());
      beans = criteria.list(); // ht.findByCriteria(criteria,filter.getStart(),filter.getLimit());
    } else {
      beans = criteria.list(); // ht.findByCriteria(criteria);
    }
    logger.debug(type.getSimpleName() + " founds = " + beans.size());

    return beans;
  }