List of usage examples for org.hibernate Criteria setProjection
public Criteria setProjection(Projection projection);
From source file:com.abiquo.abiserver.commands.UserCommand.java
License:Mozilla Public License
/** * Gets the List of enterprises from the Data Base. Enterprises marked as deleted will not be * returned//from ww w . j av a 2s.co m * * @param userSession A UserSession object containing the information of the user that called * this method * @param enterpriseListOptions an UserListOptions object containing the options to retrieve the * list of users * @return A DataResult object containing an EnterpriseListResult object */ @SuppressWarnings("unchecked") protected DataResult<EnterpriseListResult> getEnterprises(UserSession userSession, EnterpriseListOptions enterpriseListOptions) { DataResult<EnterpriseListResult> dataResult = new DataResult<EnterpriseListResult>(); Session session = null; Transaction transaction = null; try { session = HibernateUtil.getSession(); transaction = session.beginTransaction(); ArrayList<Enterprise> enterpriseList = new ArrayList<Enterprise>(); Integer totalEnterprises = 0; // Getting the user that called this method UserHB userHB = (UserHB) session.createCriteria(UserHB.class) .add(Restrictions.eq("user", userSession.getUser())).uniqueResult(); // getEnterprises has two different authorization resources // If the user who called this method, does not match the security level for the // authorization resource // ENTERPRISE_GET_ALL_ENTERPRISES, this method will only return the enterprise to which // the user belongs // Getting the authorization resources ENTERPRISE_GET_ALL_ENTERPRISES AuthResourceHB authResourceHB = (AuthResourceHB) session.createCriteria(AuthResourceHB.class) .add(Restrictions.eq("name", "ENTERPRISE_GET_ALL_ENTERPRISES")).uniqueResult(); // Checking if the user has a security level equal or higher than the necessary for // retrieve the whole list of // enterprises. Tip: in securityLevel scale, 1 is the greater level of security, and 99 // the lowest if (authResourceHB.getRoleHB().getSecurityLevel() .compareTo(userHB.getRoleHB().getSecurityLevel()) > -1) { // GRANTED! This User can view other Enterprises than the one to he belongs // Creating enterprises criteria Criteria enterprisesListCriteria = session.createCriteria(EnterpriseHB.class); Criteria enterprisesCountCriteria = session.createCriteria(EnterpriseHB.class); // Removing the enterprises that are deleted enterprisesListCriteria.add(Restrictions.eq("deleted", 0)); enterprisesCountCriteria.add(Restrictions.eq("deleted", 0)); // Adding filter text enterprisesListCriteria .add(Restrictions.like("name", '%' + enterpriseListOptions.getFilter() + '%')); enterprisesCountCriteria .add(Restrictions.like("name", '%' + enterpriseListOptions.getFilter() + '%')); // Adding order enterprisesListCriteria.addOrder(Order.asc("name")); // Once we have the criteria... // 1. Getting the total number of enterprises that match enterpriseListOptions totalEnterprises = (Integer) enterprisesCountCriteria.setProjection(Projections.rowCount()) .uniqueResult(); // 2. Getting the list of enterprises, applying an offset and a max of results ArrayList<EnterpriseHB> enterpriseHBList = (ArrayList<EnterpriseHB>) enterprisesListCriteria .setFirstResult(enterpriseListOptions.getOffset()) .setMaxResults(enterpriseListOptions.getLength()).list(); // Building result for (EnterpriseHB enterpriseHB : enterpriseHBList) { enterpriseList.add((Enterprise) enterpriseHB.toPojo()); } } else { // NOT GRANTED! This User can only view the Enterprise to which he belongs enterpriseList.add((Enterprise) userHB.getEnterpriseHB().toPojo()); totalEnterprises = 1; } transaction.commit(); EnterpriseListResult enterpriseListResult = new EnterpriseListResult(); enterpriseListResult.setEnterprisesList(enterpriseList); enterpriseListResult.setTotalEnterprises(totalEnterprises); dataResult.setSuccess(true); dataResult.setData(enterpriseListResult); dataResult.setMessage(UserCommand.resourceManager.getMessage("getEnterprises.success")); } catch (Exception e) { if (transaction != null && transaction.isActive()) transaction.rollback(); this.errorManager.reportError(resourceManager, dataResult, "getEnterprises", e); } return dataResult; }
From source file:com.abiquo.server.core.common.persistence.DefaultDAOBase.java
License:Open Source License
protected Long count(final Criteria criteria) { criteria.setProjection(Projections.rowCount()); return (Long) criteria.uniqueResult(); }
From source file:com.abiquo.server.core.infrastructure.network.IpPoolManagementDAO.java
License:Open Source License
public Collection<String> getAllMacs() { Criteria criteria = getSession().createCriteria(IpPoolManagement.class); ProjectionList projList = Projections.projectionList(); projList.add(Projections.property(IpPoolManagement.MAC_PROPERTY)); criteria.setProjection(projList); return criteria.list(); }
From source file:com.abiquo.server.core.infrastructure.RepositoryDAO.java
License:Open Source License
public boolean existRepositoryInOtherDatacenter(Datacenter datacenter, String repositoryLocation) { Criterion notDatacenter = Restrictions.not(thisDatacenter(datacenter)); Criteria criteria = createCriteria(notDatacenter, thisLocation(repositoryLocation)); criteria.setProjection(Projections.projectionList().add(Projections.rowCount())); Long count = (Long) criteria.uniqueResult(); return count != null && count.intValue() > 0; }
From source file:com.abiquo.server.core.infrastructure.RepositoryDAO.java
License:Open Source License
public boolean existRepositoryInSameDatacenter(Datacenter datacenter, String repositoryLocation) { Criteria criteria = createCriteria(thisDatacenter(datacenter), thisLocation(repositoryLocation)); criteria.setProjection(Projections.projectionList().add(Projections.rowCount())); Long count = (Long) criteria.uniqueResult(); return count != null && count.intValue() > 0; }
From source file:com.abiquo.server.core.statistics.CloudUsageDAO.java
License:Open Source License
public CloudUsage sumTotalCloudUsage() { // TODO: Include aggregators functionality in bzengine? Session ses = HibernateEntityManagerHelper.getSession(getEntityManager()); Criteria crit = ses.createCriteria(CloudUsage.class); ProjectionList proList = Projections.projectionList(); proList.add(Projections.sum(CloudUsage.SERVERS_TOTAL_PROPERTY)); proList.add(Projections.sum(CloudUsage.SERVERS_RUNNING_PROPERTY)); proList.add(Projections.sum(CloudUsage.STORAGE_TOTAL_PROPERTY)); // proList.add(Projections.sum(CloudUsage.STORAGE_RESERVED_PROPERTY)); proList.add(Projections.sum(CloudUsage.STORAGE_USED_PROPERTY)); proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_TOTAL_PROPERTY)); // proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_RESERVED_PROPERTY)); proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_USED_PROPERTY)); proList.add(Projections.sum(CloudUsage.V_MACHINES_TOTAL_PROPERTY)); proList.add(Projections.sum(CloudUsage.V_MACHINES_RUNNING_PROPERTY)); proList.add(Projections.sum(CloudUsage.V_CPU_TOTAL_PROPERTY)); // proList.add(Projections.sum(CloudUsage.V_CPU_RESERVED_PROPERTY)); proList.add(Projections.sum(CloudUsage.V_CPU_USED_PROPERTY)); proList.add(Projections.sum(CloudUsage.V_MEMORY_TOTAL_PROPERTY)); // proList.add(Projections.sum(CloudUsage.V_MEMORY_RESERVED_PROPERTY)); proList.add(Projections.sum(CloudUsage.V_MEMORY_USED_PROPERTY)); proList.add(Projections.sum(CloudUsage.V_STORAGE_TOTAL_PROPERTY)); // proList.add(Projections.sum(CloudUsage.V_STORAGE_RESERVED_PROPERTY)); proList.add(Projections.sum(CloudUsage.V_STORAGE_USED_PROPERTY)); proList.add(Projections.sum(CloudUsage.NUM_USERS_CREATED_PROPERTY)); proList.add(Projections.sum(CloudUsage.NUM_VDC_CREATED_PROPERTY)); proList.add(Projections.sum(CloudUsage.NUM_ENTERPRISES_CREATED_PROPERTY)); crit.setProjection(proList); Object[] obj = (Object[]) crit.uniqueResult(); // Returns Object[] -> CloudUsage result = new CloudUsage(); int cont = 0; result.setServersTotal((Long) obj[cont++]); result.setServersRunning((Long) obj[cont++]); result.setStorageTotal((Long) obj[cont++]); // result.setStorageReserved((Long) obj[cont++]); result.setStorageUsed((Long) obj[cont++]); result.setPublicIPsTotal((Long) obj[cont++]); // result.setPublicIPsReserved((Long) obj[cont++]); result.setPublicIPsUsed((Long) obj[cont++]); result.setVirtualMachinesTotal((Long) obj[cont++]); result.setVirtualMachinesRunning((Long) obj[cont++]); result.setVirtualCpuTotal((Long) obj[cont++]); // result.setVirtualCpuReserved((Long) obj[cont++]); result.setVirtualCpuUsed((Long) obj[cont++]); result.setVirtualMemoryTotal((Long) obj[cont++]); // result.setVirtualMemoryReserved((Long) obj[cont++]); result.setVirtualMemoryUsed((Long) obj[cont++]); result.setVirtualStorageTotal((Long) obj[cont++]); // result.setVirtualStorageReserved((Long) obj[cont++]); result.setVirtualStorageUsed((Long) obj[cont++]); result.setNumUsersCreated((Long) obj[cont++]); result.setNumVdcCreated((Long) obj[cont++]); result.setNumEnterprisesCreated((Long) obj[cont++]); return result; }
From source file:com.abssh.util.GenericDao.java
License:Apache License
/** * countCriteria.//from ww w . ja v a 2 s . c o m */ @SuppressWarnings("unchecked") protected int countCriteriaResult(final Criteria c) { CriteriaImpl impl = (CriteriaImpl) c; // Projection?ResultTransformer?OrderBy??,??Count? Projection projection = impl.getProjection(); ResultTransformer transformer = impl.getResultTransformer(); List<CriteriaImpl.OrderEntry> orderEntries = null; try { orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries"); ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList()); } catch (Exception e) { logger.error("can not throw Exception:{}", e.getMessage()); } // Count Long ct = (Long) c.setProjection(Projections.rowCount()).uniqueResult(); int totalCount = ct == null ? 0 : ct.intValue(); // ?Projection,ResultTransformerOrderBy?? c.setProjection(projection); if (projection == null) { c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); } if (transformer != null) { c.setResultTransformer(transformer); } try { ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries); } catch (Exception e) { logger.error("can not throw Exception:{}", e.getMessage()); } return totalCount; }
From source file:com.abssh.util.GenericDao.java
License:Apache License
/** * countCriteria.//from w w w . ja va 2 s . co m */ @SuppressWarnings("unchecked") protected int sumCriteriaResult(final String propertyName, final Criteria c) { CriteriaImpl impl = (CriteriaImpl) c; // Projection?ResultTransformer?OrderBy??,??Sum? Projection projection = impl.getProjection(); ResultTransformer transformer = impl.getResultTransformer(); List<CriteriaImpl.OrderEntry> orderEntries = null; try { orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries"); ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList()); } catch (Exception e) { logger.error("can not throw Exception:{}", e.getMessage()); } // Sum Integer sum = (Integer) c.setProjection(Projections.sum(propertyName)).uniqueResult(); // ?Projection,ResultTransformerOrderBy?? c.setProjection(projection); if (projection == null) { c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); } if (transformer != null) { c.setResultTransformer(transformer); } try { ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries); } catch (Exception e) { logger.error("can not throw Exception:{}", e.getMessage()); } return sum == null ? 0 : sum; }
From source file:com.all.rds.service.impl.TrackServiceImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from w w w . j av a 2s.c o m*/ public List<String> filterTracksByAvailability(final List<String> trackIds) { return ht.executeFind(new HibernateCallback<List<String>>() { @Override public List<String> doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = session.createCriteria(TrackUploadStatus.class); criteria.setProjection(Projections.property("trackId")); criteria.add(Restrictions.in("trackId", trackIds)); criteria.add(Restrictions.eq("trackStatus", TrackStatus.Status.UPLOADED)); return criteria.list(); } }); }
From source file:com.app.gpo.dao.OrderItemDAO.java
License:Open Source License
public boolean isInDbByOrderNumber(String orderNumber) { Criteria criteria = getSession().createCriteria(OrderItem.class); criteria.add(Restrictions.like("orderNumber", orderNumber + "%")); criteria.setProjection(Projections.rowCount()); long count = (Long) criteria.uniqueResult(); logger.info("DB count " + count + " for order items with order number " + orderNumber); if (count != 0) { logger.info("In DB exist" + count + " order items with order number " + orderNumber); return true; } else {/*from w w w. jav a 2 s . c o m*/ logger.info("In DB no exist order items with order number " + orderNumber); return false; } }