Example usage for org.hibernate.criterion Projections property

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

Introduction

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

Prototype

public static PropertyProjection property(String propertyName) 

Source Link

Document

A property value projection

Usage

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@Override
public boolean hasAssignedEveryoneRole(Realm realm, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());
    criteria.setProjection(Projections.property("id"));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);/*from   w  ww .  j av  a 2 s.  com*/
    }

    criteria.add(Restrictions.eq("realm", realm));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", true));

    List<?> everyoneRoles = criteria.list();

    return everyoneRoles.size() > 0;
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  ww . ja  v  a  2 s  .c om*/
public Collection<Principal> getAssignedPrincipals(Realm realm, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    criteria.add(Restrictions.eq("realm", realm));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));

    criteria = criteria.createCriteria("principals");
    criteria.setProjection(Projections.distinct(Projections.property("id")));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);

    List<?> uniquePrincipals = criteria.list();

    if (uniquePrincipals.isEmpty()) {
        return new HashSet<Principal>();
    }
    criteria = createCriteria(Principal.class);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.in("id", uniquePrincipals));
    List<?> res = criteria.list();

    return (Collection<Principal>) res;
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@Override
public Long getAssignedPrincipalCount(Realm realm, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);//from  ww  w  .jav a2 s  .c o  m
    }

    criteria.add(Restrictions.eq("realm", realm));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));

    criteria = criteria.createCriteria("principals");
    criteria.setProjection(Projections.distinct(Projections.property("id")));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);

    List<?> uniquePrincipals = criteria.list();

    return (long) uniquePrincipals.size();

}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@Override
public Long getAssignedResourceCount(List<Principal> principals, final String searchPattern,
        CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());
    criteria.setProjection(Projections.property("id"));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);
    if (StringUtils.isNotBlank(searchPattern)) {
        criteria.add(Restrictions.ilike("name", searchPattern));
    }/*from w  w  w  .j a  va  2s  .  c  o  m*/

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    criteria.add(Restrictions.eq("realm", principals.get(0).getRealm()));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", true));

    List<?> ids = criteria.list();

    criteria = createCriteria(getResourceClass());
    criteria.setProjection(Projections.countDistinct("name"));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);
    if (StringUtils.isNotBlank(searchPattern)) {
        criteria.add(Restrictions.ilike("name", searchPattern));
    }

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    criteria.add(Restrictions.eq("realm", principals.get(0).getRealm()));
    if (ids.size() > 0) {
        criteria.add(Restrictions.not(Restrictions.in("id", ids)));
    }

    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));
    criteria = criteria.createCriteria("principals");
    List<Long> pids = new ArrayList<Long>();
    for (Principal p : principals) {
        pids.add(p.getId());
    }
    criteria.add(Restrictions.in("id", pids));

    Long count = (Long) criteria.uniqueResult();
    return count + ids.size();

}

From source file:com.hyzy.core.orm.hibernate.SimpleHibernateDao.java

License:Apache License

/**
 * // w ww.ja v a 2 s  . c  o m
 * 
 * ??
 * 
 * @author: zhaozongzhan
 * 
 * @create: 2016113 ?10:40:57
 * 
 *
 * 
 * @param showFieldNames ?
 * @param criterions
 * @return
 */
protected Criteria setShowFields(List<String> showFieldNames, final Criterion... criterions) {
    Criteria criteria = createCriteria(criterions);
    if (showFieldNames != null && showFieldNames.size() > 0) {
        ProjectionList proList = Projections.projectionList();// ?
        for (String fieldName : showFieldNames) {

            proList.add(Projections.property(fieldName));
        }
        criteria.setProjection(proList);
    }

    return criteria;
}

From source file:com.ibm.asset.trails.dao.jpa.VSoftwareLparDAOJpa.java

public void paginatedList(DisplayTagList data, Account account, ReconSetting reconSetting, int startIndex,
        int objectsPerPage, String sort, String dir) {
    Criteria criteria = getHibernateSessionCriteria();

    criteria.createAlias("hardwareLpar", "hl")
            .createAlias("hl.hardwareLparEff", "hle", CriteriaSpecification.LEFT_JOIN)
            .createAlias("hl.hardware", "h").createAlias("h.machineType", "mt")
            .createAlias("installedSoftwares", "is")
            .createAlias("is.scheduleF", "sf", CriteriaSpecification.LEFT_JOIN)
            .createAlias("sf.scope", "scope", CriteriaSpecification.LEFT_JOIN)
            .createAlias("is.softwareLpar", "sl").createAlias("is.alert", "aus")
            .createAlias("aus.reconcile", "r", CriteriaSpecification.LEFT_JOIN)
            .createAlias("r.usedLicenses", "ul", CriteriaSpecification.LEFT_JOIN)
            .createAlias("ul.license", "license", CriteriaSpecification.LEFT_JOIN)
            .createAlias("r.reconcileType", "rt", CriteriaSpecification.LEFT_JOIN)
            .createAlias("is.software", "sw")
            .createAlias("sw.manufacturer", "mf", CriteriaSpecification.LEFT_JOIN)
            .add(Restrictions.eq("account", account));

    if (reconSetting.getReconcileType() != null) {
        criteria.add(Restrictions.eq("rt.id", reconSetting.getReconcileType()));
    }//from  w w  w  .  j a  va 2  s.co  m

    if (StringUtils.isNotBlank(reconSetting.getAlertStatus())) {
        boolean open = false;
        if (reconSetting.getAlertStatus().equals("OPEN")) {
            open = true;
            criteria.add(Restrictions.eq("aus.open", open));
        } else {
            criteria.add(Restrictions.and(Restrictions.eq("aus.open", false),
                    Restrictions.eqProperty("is.id", "r.installedSoftware.id")));
        }

    } else {
        criteria.add(Restrictions.or(Restrictions.eq("aus.open", true),
                Restrictions.and(Restrictions.eq("aus.open", false),
                        Restrictions.eqProperty("is.id", "r.installedSoftware.id"))));
    }

    if (null != reconSetting.getAlertFrom() && reconSetting.getAlertFrom().intValue() >= 0) {
        criteria.add(Restrictions.ge("aus.alertAge", reconSetting.getAlertFrom()));
    }
    if (null != reconSetting.getAlertTo() && reconSetting.getAlertTo().intValue() >= 0) {
        criteria.add(Restrictions.le("aus.alertAge", reconSetting.getAlertTo()));
    }

    if (StringUtils.isNotBlank(reconSetting.getAssigned())) {
        if (reconSetting.getAssigned().equals("Assigned")) {
            criteria.add(Restrictions.ne("aus.remoteUser", "STAGING"));
        }
        if (reconSetting.getAssigned().equals("Unassigned")) {
            criteria.add(Restrictions.eq("aus.remoteUser", "STAGING"));
        }
    }

    if (StringUtils.isNotBlank(reconSetting.getAssignee())) {
        criteria.add(Restrictions.eq("aus.remoteUser", reconSetting.getAssignee()).ignoreCase());
    }

    if (StringUtils.isNotBlank(reconSetting.getOwner())) {
        if (reconSetting.getOwner().equalsIgnoreCase("IBM")) {
            criteria.add(Restrictions.eq("h.owner", reconSetting.getOwner()).ignoreCase());
        } else if (reconSetting.getOwner().equalsIgnoreCase("Customer")) {
            ArrayList<String> lalOwner = new ArrayList<String>();

            lalOwner.add("CUST");
            lalOwner.add("CUSTO");
            criteria.add(Restrictions.in("h.owner", lalOwner));
        }
    }

    // I'm not sure why the heck we aren't just getting a list of strings?
    if (reconSetting.getCountries().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getCountries().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getCountries()[i])) {
                list.add(reconSetting.getCountries()[i].toUpperCase());
            }
        }

        if (list.size() > 0) {
            criteria.add(Restrictions.in("h.country", list));
        }
    }

    if (reconSetting.getNames().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getNames().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getNames()[i])) {
                list.add(reconSetting.getNames()[i].toUpperCase());
            }
        }

        if (list.size() > 0) {
            criteria.add(Restrictions.in("hl.name", list));
        }
    }

    if (reconSetting.getSwcmIDs().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getSwcmIDs().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getSwcmIDs()[i])) {
                list.add(reconSetting.getSwcmIDs()[i].toUpperCase());
            }
        }
        if (list.size() > 0) {
            criteria.add(Restrictions.in("license.extSrcId", list));
        }
    }

    if (reconSetting.getSerialNumbers().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getSerialNumbers().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getSerialNumbers()[i])) {
                list.add(reconSetting.getSerialNumbers()[i].toUpperCase());
            }
        }

        if (list.size() > 0) {
            criteria.add(Restrictions.in("h.serial", list));
        }
    }

    if (reconSetting.getProductInfoNames().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getProductInfoNames().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getProductInfoNames()[i])) {
                list.add(reconSetting.getProductInfoNames()[i]);
            }
        }

        if (list.size() > 0) {
            criteria.add(Restrictions.in("sw.softwareName", list));
        }
    }

    if (StringUtils.isNotBlank(reconSetting.getScope())) {
        if ("Not specified".equalsIgnoreCase(reconSetting.getScope())) {
            criteria.add(Restrictions.isNull("scope.description"));
        } else {
            criteria.add(Restrictions.eq("scope.description", reconSetting.getScope()));
        }
    }

    if (StringUtils.isNotBlank(reconSetting.getFinanResp())) {
        if ("Not Specified".trim().equalsIgnoreCase(reconSetting.getFinanResp())) {
            criteria.add(Restrictions.isNull("sf.SWFinanceResp"));
        } else {
            criteria.add(Restrictions.eq("sf.SWFinanceResp", reconSetting.getFinanResp()));
        }
    }

    criteria.setProjection(Projections.projectionList().add(Projections.property("aus.id").as("alertId"))
            .add(Projections.property("r.id").as("reconcileId"))
            .add(Projections.property("aus.alertAge").as("alertAgeI"))
            .add(Projections.property("is.id").as("installedSoftwareId"))
            .add(Projections.property("hl.name").as("hostname"))
            .add(Projections.property("sl.name").as("sl_hostname"))
            .add(Projections.property("hl.spla").as("spla"))
            .add(Projections.property("hl.sysplex").as("sysplex"))
            .add(Projections.property("hl.internetIccFlag").as("internetIccFlag"))
            .add(Projections.property("h.serial").as("serial"))
            .add(Projections.property("h.country").as("country"))
            .add(Projections.property("h.owner").as("owner"))
            .add(Projections.property("h.mastProcessorType").as("mastProcessorType"))
            .add(Projections.property("h.processorManufacturer").as("processorManufacturer"))
            .add(Projections.property("h.mastProcessorModel").as("mastProcessorModel"))
            .add(Projections.property("h.nbrCoresPerChip").as("nbrCoresPerChip"))
            .add(Projections.property("h.nbrOfChipsMax").as("nbrOfChipsMax"))
            .add(Projections.property("h.cpuLsprMips").as("cpuLsprMips"))
            .add(Projections.property("h.cpuIfl").as("cpuIFL"))
            .add(Projections.property("hl.partLsprMips").as("partLsprMips"))
            .add(Projections.property("h.cpuGartnerMips").as("cpuGartnerMips"))
            .add(Projections.property("hl.partGartnerMips").as("partGartnerMips"))
            .add(Projections.property("hl.effectiveThreads").as("effectiveThreads"))
            .add(Projections.property("hl.vcpu").as("vcpu")).add(Projections.property("h.cpuMsu").as("cpuMsu"))
            .add(Projections.property("hl.partMsu").as("partMsu"))
            .add(Projections.property("hl.serverType").as("lparServerType"))
            .add(Projections.property("h.shared").as("shared"))
            .add(Projections.property("h.multi_tenant").as("multi_tenant"))
            .add(Projections.property("mt.type").as("assetType"))
            .add(Projections.property("mt.name").as("assetName"))
            .add(Projections.property("h.hardwareStatus").as("hardwareStatus"))
            .add(Projections.property("hl.lparStatus").as("lparStatus"))
            .add(Projections.property("processorCount").as("processorCount"))
            .add(Projections.property("sw.softwareName").as("productInfoName"))
            .add(Projections.property("sw.softwareId").as("productInfoId"))
            .add(Projections.property("sw.pid").as("pid"))
            .add(Projections.property("mf.manufacturerName").as("manufacturerName"))
            .add(Projections.property("rt.name").as("reconcileTypeName"))
            .add(Projections.property("rt.id").as("reconcileTypeId"))
            .add(Projections.property("aus.remoteUser").as("assignee"))
            .add(Projections.property("h.processorCount").as("hardwareProcessorCount"))
            .add(Projections.property("hle.processorCount").as("hwLparEffProcessorCount"))
            .add(Projections.property("hl.osType").as("osType"))
            .add(Projections.property("hle.status").as("hwLparEffProcessorStatus"))
            .add(Projections.property("h.chips").as("chips")));
    criteria.setResultTransformer(new AliasToBeanResultTransformer(ReconWorkspace.class));

    criteria.addOrder(Order.desc("aus.open"));

    if (dir.equalsIgnoreCase("ASC")) {
        criteria.addOrder(Order.asc(sort));
    } else {
        criteria.addOrder(Order.desc(sort));
    }

    ArrayList<ReconWorkspace> list = new ArrayList<ReconWorkspace>();

    ScrollableResults itemCursor = criteria.scroll();
    itemCursor.beforeFirst();
    if (itemCursor.next()) {
        itemCursor.scroll(startIndex);
        int i = 0;

        while (objectsPerPage > i++) {
            ReconWorkspace rw = (ReconWorkspace) itemCursor.get(0);
            if (null != rw.getHwLparEffProcessorStatus()
                    && rw.getHwLparEffProcessorStatus().equalsIgnoreCase("INACTIVE")) {
                rw.setHwLparEffProcessorCount(0);
            }
            list.add(rw);
            if (!itemCursor.next())
                break;
        }

        data.setList(list);
        itemCursor.last();
        data.setFullListSize(itemCursor.getRowNumber() + 1);
        itemCursor.close();

        addSchedulef2List(account, data.getList());
    } else {
        data.setList(null);
        data.setFullListSize(0);
        itemCursor.close();
    }

}

From source file:com.ihsolution.hqipo.dao.utils.QueryHelper.java

License:Open Source License

/**
 * convert the InputDTO to QueryHelper to get the count (omitting sort order)
 *//*from   w  w w  . j  a  v a 2 s .  co m*/
@SuppressWarnings("deprecation")
public QueryHelper convertDtoToQhelperForCount(InputDTO dto) {
    if (dto == null)
        return this;
    this.dto = dto;
    int ind = 0;
    try {
        Junction j = null;
        Junction conj = Restrictions.conjunction();
        Junction disj = Restrictions.disjunction();
        String operator = "";
        boolean disjB = false, conjB = false;
        if (dto.getFetchMode() != null) {
            for (Map.Entry<String, String> entry : dto.getFetchMode().entrySet()) {
                FetchMode fmode = null;
                logger.debug("FetchMode key=" + entry.getKey() + " val=" + entry.getValue());
                if (entry.getValue().equals("join"))
                    fmode = FetchMode.JOIN;
                else if (entry.getValue().equals("eager"))
                    fmode = FetchMode.EAGER;
                else if (entry.getValue().equals("lazy"))
                    fmode = FetchMode.LAZY;
                else
                    fmode = FetchMode.LAZY;
                this.detCriteria.setFetchMode(entry.getKey(), fmode);
            }
        }
        for (String field : dto.getFields()) {
            operator = dto.getOperators().get(ind);
            if ("or".equals(operator)) {
                j = disj;
                disjB = true;
            } else {
                j = conj;
                conjB = true;
            }
            this.addFieldAndVal(createAliases(field), dto.getValues().get(ind), dto.getOperations().get(ind),
                    j);
            ind++;
        }

        if (dto.getExpressions() != null) {
            for (String expr : dto.getExpressions()) {
                j.add(Expression.sql(expr));
            }
        }
        if (dto.getFieldsToSelect() != null && dto.getFieldsToSelect().length > 0) {
            ProjectionList prList = Projections.projectionList();
            Projection projection = null;

            for (String fld : dto.getFieldsToSelect()) {
                String als = this.createAliases(fld);
                prList.add(Projections.property(als));
            }
            if (dto.isDistinct()) {
                projection = Projections.distinct(prList);
            } else {
                projection = prList;
            }
            this.detCriteria.setProjection(projection);

        } else {
            this.fldSelectedSet = false;
        }

        if (disjB)
            detCriteria.add(disj);
        if (conjB)
            detCriteria.add(conj);
        if (logger.isDebugEnabled()) {
            if (conjB)
                logger.debug("conjuction=" + conj.toString());
            if (disjB)
                logger.debug("disjunction=" + disj.toString());
        }
        if (dto.isDistinct())
            detCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e);
    }
    return this;
}

From source file:com.ihsolution.hqipo.dao.utils.QueryHelper.java

License:Open Source License

protected String createProjection(String field) {
    String[] fStrs = field.split("\\.");
    String tmpField = "";
    int len = fStrs.length;
    ProjectionList prList = Projections.projectionList();

    if (len > 1) {
        for (int i = 0; len - i != 1; i++) {
            if (i == 0) {
                if (!projections.contains(fStrs[i])) {
                    prList.add(Projections.property(fStrs[i]));
                    projections.add(fStrs[i]);
                }/* w  ww  . ja  v a  2  s .  c o m*/
            } else {
                if (!projections.contains(fStrs[i])) {
                    String str = fStrs[i - 1] + "." + fStrs[i];
                    prList.add(Projections.property(str), fStrs[i]);
                    projections.add(fStrs[i]);
                }
            }
        }
        detCriteria.setProjection(prList);
        tmpField = fStrs[len - 1];
    } else
        tmpField = field;

    return tmpField;
}

From source file:com.indicator_engine.dao.GLACategoryDaoImpl.java

License:Open Source License

/**
 * Loads all the Minors Present in the Database.
 * @return List of Minors present in DB.
 *///from   w  w  w  . jav  a  2 s . c o  m
@Override
@Transactional(readOnly = true)
public List<String> selectAllMinors() {
    Session session = factory.getCurrentSession();
    Criteria criteria = session.createCriteria(GLACategory.class).setProjection(Projections.property("minor"));
    return criteria.list();
}

From source file:com.indicator_engine.dao.GLAEntityDaoImpl.java

License:Open Source License

/**
 * Loads all GLA Entity Objects present in the Database based on a specific Category ID.
 * @param categoryID Category ID used for selecting relevant GLA Entity Objects.
 * @return Returns all GLA Entity Objects present in Database and matching the specified Category ID.
 **//*from  w w  w .ja va  2  s  . c  o m*/
@Override
@Transactional(readOnly = true)
public List<String> loadEntitiesByCategoryID(Long categoryID) {
    Session session = factory.getCurrentSession();
    Criteria criteria = session.createCriteria(GLAEntity.class);
    criteria.setProjection(Projections.distinct(Projections.property("key")));
    criteria.createAlias("glaEvent", "events");
    criteria.setFetchMode("events", FetchMode.JOIN);
    criteria.createAlias("events.glaCategory", "category");
    criteria.setFetchMode("category", FetchMode.JOIN);
    criteria.add(Restrictions.eq("category.id", categoryID));
    return criteria.list();

}