Example usage for org.hibernate.criterion Projections projectionList

List of usage examples for org.hibernate.criterion Projections projectionList

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections projectionList.

Prototype

public static ProjectionList projectionList() 

Source Link

Document

Create a new projection list.

Usage

From source file:com.silverpeas.mailinglist.service.model.dao.MessageDaoImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Activity> listActivity(String componentId) {
    Criteria criteria = getSession().createCriteria(Message.class);
    criteria.add(Restrictions.eq("componentId", componentId));
    criteria.add(Restrictions.eq("moderated", Boolean.TRUE));
    criteria.setProjection(Projections.projectionList().add(Projections.rowCount(), "nb")
            .add(Projections.groupProperty("year"), "year").add(Projections.groupProperty("month"), "month"));
    List result = criteria.list();
    List<Activity> activities;
    if (result != null && !result.isEmpty()) {
        activities = new ArrayList<Activity>(result.size());
        for (Object aResult : result) {
            Object[] line = (Object[]) aResult;
            Activity activity = new Activity();
            activity.setNbMessages(((Long) line[0]).intValue());
            activity.setYear((Integer) line[1]);
            activity.setMonth((Integer) line[2]);
            activities.add(activity);/*from  w w w  .  j  a va 2s  . com*/
        }
    } else {
        activities = new ArrayList<Activity>();
    }
    return activities;
}

From source file:com.smartitengineering.dao.impl.hibernate.AbstractDAO.java

License:Open Source License

private void setProjection(Criteria criteria, final Projection projection) {
    ProjectionList currentProjections = projections.get(criteria);
    if (currentProjections == null) {
        currentProjections = Projections.projectionList();
        projections.put(criteria, currentProjections);
        criteria.setProjection(currentProjections);
    }/*from w  ww.j  a v a  2s . co  m*/

    currentProjections.add(projection);
}

From source file:com.square.adherent.noyau.dao.implementations.contrat.ContratDaoImpl.java

License:Open Source License

@Override
public List<RatioPrestationCotisationDto> getRatioPrestationCotisationPersonne(Long uidPersonne) {
    final Criteria crit = createCriteria(Ratio.class);
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("ratioPrestaSurCotis"));
    projectionList.add(Projections.property("annee"));
    crit.setProjection(projectionList);//from w w  w . j  av a 2 s  .  com
    crit.add(Restrictions.eq("uidPersonne", uidPersonne));
    final List result = crit.list();

    final Iterator iterator = result.iterator();

    if (result == null) {
        return null;
    } else {
        double ratio = -1;
        int annee = -1;

        final List<RatioPrestationCotisationDto> listRatioDto = new ArrayList<RatioPrestationCotisationDto>();

        while (iterator.hasNext()) {
            final Object[] objects = (Object[]) iterator.next();

            ratio = ((Double) objects[0]).doubleValue();
            annee = ((Integer) objects[1]).intValue();

            final RatioPrestationCotisationDto ratioDto = new RatioPrestationCotisationDto();
            ratioDto.setAnnee(annee);
            ratioDto.setRatioPrestationCotisation(ratio);
            final IdentifiantLibelleDto personneDto = new IdentifiantLibelleDto(uidPersonne);
            ratioDto.setPersonne(personneDto);

            listRatioDto.add(ratioDto);
        }

        return listRatioDto;
    }
}

From source file:com.square.core.dao.implementations.CommuneDaoImplementation.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  w  ww. j  a v a 2  s . c  o  m*/
public List<IdentifiantLibelleCodePostalCommuneDto> rechercherCodesPostauxCommunes(
        DimensionCriteresRechercheDto criteres) {
    final Criteria criteria = createCriteria(CodePostalCommune.class);
    criteria.createAlias("commune", "commune");
    criteria.createAlias("codePostal", "codePostal");
    criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("commune.id"))
            .add(Projections.groupProperty("commune.libelle")).add(Projections.groupProperty("codePostal.id"))
            .add(Projections.groupProperty("codePostal.codePostal"))
            .add(Projections.groupProperty("commune.ordre"))
            .add(Projections.groupProperty("codePostal.ordre")));

    // Critre sur l'identifiant
    if (criteres.getId() != null) {
        criteria.add(Restrictions.eq("commune.id", criteres.getId()));
    }
    // Critre sur le libelle
    if (criteres.getLibelle() != null && !criteres.getLibelle().equals("")) {
        criteria.add(Restrictions.ilike("codePostal.codePostal", criteres.getLibelle().toLowerCase() + "%"));
    }
    // Critre sur la visibilit
    if (criteres.getVisible() != null) {
        criteria.add(Restrictions.eq("commune.visible", criteres.getVisible()));
        criteria.add(Restrictions.eq("codePostal.visible", criteres.getVisible()));
    }
    // Maxresults
    if (criteres.getMaxResults() != null) {
        criteria.setFirstResult(0);
        criteria.setMaxResults(criteres.getMaxResults());
    }
    // Ordonner les lments
    criteria.addOrder(Order.asc("commune.ordre"));
    criteria.addOrder(Order.asc("commune.libelle"));
    criteria.addOrder(Order.asc("codePostal.ordre"));

    final List<Object[]> resultat = criteria.list();
    final List<IdentifiantLibelleCodePostalCommuneDto> list = new ArrayList<IdentifiantLibelleCodePostalCommuneDto>();
    for (Object[] row : resultat) {
        final IdentifiantLibelleCodePostalCommuneDto identifiantLibelleCodePostalCommuneDto = new IdentifiantLibelleCodePostalCommuneDto();
        identifiantLibelleCodePostalCommuneDto.setIdCodePostal((Long) row[2]);
        identifiantLibelleCodePostalCommuneDto.setIdCommune((Long) row[0]);
        identifiantLibelleCodePostalCommuneDto.setLibelleCodePostal(String.valueOf(row[3]));
        identifiantLibelleCodePostalCommuneDto.setLibelleCommune(String.valueOf(row[1]));
        list.add(identifiantLibelleCodePostalCommuneDto);
    }
    return list;
}

From source file:com.square.core.dao.implementations.CommuneDaoImplementation.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*  w  ww  . ja v a 2s  . c  om*/
public List<IdentifiantLibelleCodePostalCommuneDto> rechercherCommunesCodesPostaux(
        DimensionCriteresRechercheDto criteres) {
    final Criteria criteria = createCriteria(CodePostalCommune.class);
    criteria.createAlias("commune", "commune");
    criteria.createAlias("codePostal", "codePostal");
    criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("commune.id"))
            .add(Projections.groupProperty("commune.libelle")).add(Projections.groupProperty("codePostal.id"))
            .add(Projections.groupProperty("codePostal.codePostal"))
            .add(Projections.groupProperty("commune.ordre"))
            .add(Projections.groupProperty("codePostal.ordre")));

    // Critre sur l'identifiant
    if (criteres.getId() != null) {
        criteria.add(Restrictions.eq("commune.id", criteres.getId()));
    }
    // Critre sur le libelle
    if (criteres.getLibelle() != null && !criteres.getLibelle().equals("")) {
        criteria.add(Restrictions.ilike("commune.libelle", criteres.getLibelle().toLowerCase() + "%"));
    }
    // Critre sur la visibilit
    if (criteres.getVisible() != null) {
        criteria.add(Restrictions.eq("commune.visible", criteres.getVisible()));
        criteria.add(Restrictions.eq("codePostal.visible", criteres.getVisible()));
    }
    // Maxresults
    if (criteres.getMaxResults() != null) {
        criteria.setFirstResult(0);
        criteria.setMaxResults(criteres.getMaxResults());
    }
    // Ordonner les lments
    criteria.addOrder(Order.asc("commune.ordre"));
    criteria.addOrder(Order.asc("commune.libelle"));
    criteria.addOrder(Order.asc("codePostal.ordre"));

    final List<Object[]> resultat = criteria.list();
    final List<IdentifiantLibelleCodePostalCommuneDto> list = new ArrayList<IdentifiantLibelleCodePostalCommuneDto>();
    for (Object[] row : resultat) {
        final IdentifiantLibelleCodePostalCommuneDto identifiantLibelleCodePostalCommuneDto = new IdentifiantLibelleCodePostalCommuneDto();
        identifiantLibelleCodePostalCommuneDto.setIdCodePostal((Long) row[2]);
        identifiantLibelleCodePostalCommuneDto.setIdCommune((Long) row[0]);
        identifiantLibelleCodePostalCommuneDto.setLibelleCodePostal(String.valueOf(row[3]));
        identifiantLibelleCodePostalCommuneDto.setLibelleCommune(String.valueOf(row[1]));
        list.add(identifiantLibelleCodePostalCommuneDto);
    }
    return list;
}

From source file:com.square.core.dao.implementations.ModeleEmailDaoImplementation.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  ww  w. j a  va 2s  .  c o  m
public List<IdentifiantLibelleDto> rechercherListeModelesEmails(DimensionCriteresRechercheDto criteres) {

    final Criteria criteria = createCriteria(ModeleEmail.class);
    criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("id"))
            .add(Projections.groupProperty("libelle")).add(Projections.groupProperty("ordre")));

    // Critre sur l'identifiant
    if (criteres.getId() != null) {
        criteria.add(Restrictions.eq("id", criteres.getId()));
    }
    // Critre sur le libelle
    if (criteres.getLibelle() != null && !criteres.getLibelle().equals("")) {
        criteria.add(Restrictions.ilike("libelle", criteres.getLibelle().toLowerCase() + "%"));
    }
    // Critre sur la visibilit
    if (criteres.getVisible() != null) {
        criteria.add(Restrictions.eq("visible", criteres.getVisible()));
    }
    // Maxresults
    if (criteres.getMaxResults() != null) {
        criteria.setFirstResult(0);
        criteria.setMaxResults(criteres.getMaxResults());
    }
    // Ordonner les lments
    criteria.addOrder(Order.asc("ordre"));
    criteria.addOrder(Order.asc("libelle"));

    final List<Object[]> resultat = criteria.list();
    final List<IdentifiantLibelleDto> listeModeles = new ArrayList<IdentifiantLibelleDto>();
    for (Object[] row : resultat) {
        listeModeles.add(new IdentifiantLibelleDto((Long) row[0], (String) row[1]));
    }
    return listeModeles;
}

From source file:com.square.core.dao.implementations.PersonneDaoImplementation.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from ww w.  j ava2s.co m
public List<AdresseSimpleDto> rechercherAdressesSimplesParIdPersonne(Long idPersonne) {
    // Recherche des adresses de la personne
    final Criteria criteria = createCriteria(Personne.class);
    criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("adr.dateDebut"))
            .add(Projections.groupProperty("adr.dateFin")).add(Projections.groupProperty("adr.nature"))
            .add(Projections.groupProperty("nature.ordre")));

    criteria.add(Restrictions.eq("id", idPersonne));
    criteria.createAlias("adresses", "adr");
    criteria.createAlias("adr.nature", "nature");
    criteria.addOrder(Order.asc("nature.ordre"));
    criteria.addOrder(Order.asc("adr.dateDebut"));
    criteria.addOrder(Order.asc("adr.dateFin"));

    // Transformation

    final List<Object[]> resultatRequete = criteria.list();
    final List<AdresseSimpleDto> resultat = new ArrayList<AdresseSimpleDto>();

    if (resultatRequete != null) {
        for (Object[] row : resultatRequete) {
            final AdresseSimpleDto adresseSimpleDto = new AdresseSimpleDto();
            adresseSimpleDto.setDateDebut((Calendar) row[0]);
            adresseSimpleDto.setDateFin((Calendar) row[1]);
            final AdresseNature adresseNature = (AdresseNature) row[2];
            if (adresseNature != null) {
                adresseSimpleDto.setTypeAdresse(
                        new IdentifiantLibelleDto(adresseNature.getId(), adresseNature.getLibelle()));
            }

            resultat.add(adresseSimpleDto);
        }

    } else {
        return null;
    }

    return resultat;
}

From source file:com.square.core.dao.implementations.PersonneMoraleDaoImplementation.java

License:Open Source License

@Override
public PersonneMoraleSimpleDto rechercherPersonneMoraleSimpleParId(Long identifiant) {

    final Criteria criteria = createCriteria(PersonneMorale.class);
    criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("raisonSociale"))
            .add(Projections.groupProperty("numSiret")).add(Projections.groupProperty("naturePersonneMorale"))
            .add(Projections.groupProperty("num")));
    criteria.add(Restrictions.eq("id", identifiant));

    // Transformation de la PersonneMorale morale en personneMoraleSimpleDto
    final Object[] row = (Object[]) criteria.uniqueResult();
    final PersonneMoraleSimpleDto personneMoraleSimpleDto = new PersonneMoraleSimpleDto();
    personneMoraleSimpleDto.setRaisonSociale(String.valueOf(row[0]));
    personneMoraleSimpleDto.setNumeroEntreprise(String.valueOf(row[3]));

    PersonneMoraleNature personneMoraleNature = null;
    if (row[2] instanceof PersonneMoraleNature) {
        personneMoraleNature = (PersonneMoraleNature) row[2];
        personneMoraleSimpleDto.setNature(
                new IdentifiantLibelleDto(personneMoraleNature.getId(), personneMoraleNature.getLibelle()));
    }/*  w w  w  .  j a v  a 2 s.co m*/

    personneMoraleSimpleDto.setNum(String.valueOf(row[3]));

    return personneMoraleSimpleDto;
}

From source file:com.thesett.catalogue.impl.standalone.CatalogueManagerServiceImpl.java

License:Apache License

/** {@inheritDoc} */
public PagingResult executePagedQuery(int from, int number, String databaseEntityName, String entityTypeName,
        String viewTypeName, Criterion criterion, Map<String, Criterion> joins) {
    log.debug("public PagingResult executePagedQuery(int from = " + from + ", int number = " + number
            + ", String databaseEntityName = " + databaseEntityName + ", String entityTypeName = "
            + entityTypeName + ", String viewTypeName = " + viewTypeName + ", Criterion criterion, "
            + "Map<String, Criterion> joins): called");

    Session session = HibernateUtil.getCurrentSession();
    HibernateUtil.beginTransaction();/*from   w ww . j  a va2  s.com*/

    // Project the id and external id properties and just the remaining properties that are required to project
    // the results onto the specified view type.
    ProjectionList properties = Projections.projectionList().add(Projections.id())
            .add(Property.forName("externalId"));

    ViewType viewType = getCatalogue().getViewType(viewTypeName);

    for (String fieldName : viewType.getAllPropertyTypes().keySet()) {
        properties.add(Property.forName(fieldName));
    }

    // Create the selection criteria for the block.
    Criteria selectCriteria = session.createCriteria(databaseEntityName);

    if (criterion != null) {
        selectCriteria.add(criterion);
    }

    if (joins != null) {
        for (Map.Entry<String, Criterion> entry : joins.entrySet()) {
            String joinEntity = entry.getKey();
            Criterion joinCriterion = entry.getValue();

            selectCriteria.createCriteria(joinEntity).add(joinCriterion);
        }
    }

    selectCriteria.setProjection(properties).setFirstResult(from).setMaxResults(number)
            .setResultTransformer(new ViewInstanceTransformer(viewType, entityTypeName));

    // Create the count criteria.
    Criteria countCriteria = session.createCriteria(databaseEntityName);

    if (criterion != null) {
        countCriteria.add(criterion);
    }

    if (joins != null) {
        for (Map.Entry<String, Criterion> entry : joins.entrySet()) {
            String joinEntity = entry.getKey();
            Criterion joinCriterion = entry.getValue();

            countCriteria.createCriteria(joinEntity).add(joinCriterion);
        }
    }

    countCriteria.setProjection(Projections.rowCount());

    // Run a query to find out how many results there will be and update the list size.
    int count = (Integer) countCriteria.uniqueResult();

    // Execute the query to get the block.
    List<ViewInstance> results = selectCriteria.list();

    return new PagingResult(count, results);
}

From source file:com.thoughtworks.go.server.persistence.MaterialRepository.java

License:Apache License

private void checkAndRemoveDuplicates(MaterialInstance materialInstance, List<Modification> newChanges,
        ArrayList<Modification> list) {
    if (!new SystemEnvironment().get(SystemEnvironment.CHECK_AND_REMOVE_DUPLICATE_MODIFICATIONS)) {
        return;//  w w w  .  j  a  v a  2 s. co m
    }
    DetachedCriteria criteria = DetachedCriteria.forClass(Modification.class);
    criteria.setProjection(Projections.projectionList().add(Projections.property("revision")));
    criteria.add(Restrictions.eq("materialInstance.id", materialInstance.getId()));
    ArrayList<String> revisions = new ArrayList<>();
    for (Modification modification : newChanges) {
        revisions.add(modification.getRevision());
    }
    criteria.add(Restrictions.in("revision", revisions));
    List<String> matchingRevisionsFromDb = (List<String>) getHibernateTemplate().findByCriteria(criteria);
    if (!matchingRevisionsFromDb.isEmpty()) {
        for (final String revision : matchingRevisionsFromDb) {
            Modification modification = list.stream().filter(item -> item.getRevision().equals(revision))
                    .findFirst().orElse(null);
            list.remove(modification);
        }
    }
    if (!newChanges.isEmpty() && list.isEmpty()) {
        LOGGER.debug("All modifications already exist in db [{}]", revisions);
    }
    if (!matchingRevisionsFromDb.isEmpty()) {
        LOGGER.info("Saving revisions for material [{}] after removing the following duplicates {}",
                materialInstance.toOldMaterial(null, null, null).getLongDescription(), matchingRevisionsFromDb);
    }

}