Example usage for org.hibernate.criterion Projections sum

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

Introduction

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

Prototype

public static AggregateProjection sum(String propertyName) 

Source Link

Document

A property value sum projection

Usage

From source file:com.ut.tekir.report.ProductStatusReportBean.java

License:LGPL

public DetachedCriteria buildCriteria() {

    DetachedCriteria crit = DetachedCriteria.forClass(ProductTxn.class);

    crit.createAlias("product", "product");
    crit.createAlias("warehouse", "warehouse");

    //TODO: Unit Price'n farkl dvizlerde olma sorunu zle...
    ProjectionList pl = Projections.projectionList();
    pl.add(Projections.groupProperty("product.code"), "prodcode")
            .add(Projections.groupProperty("product.name"), "prodname") //.add( Projections.groupProperty("action"), "action" )
            //.add( Projections.groupProperty("amount.currency"), "currency" )
            .add(Projections.groupProperty("product.group"), "group")
            .add(Projections.groupProperty("product.barcode1"), "barcode")
            .add(Projections.sum("quantity.value"), "quantity")
            .add(Projections.avg("unitPrice.value"), "unitPrice")
            .add(Projections.sqlGroupProjection("{alias}.UNIT as unit, " +
    //"sum( case {alias}.finance_action when 0 then {alias}.QUANTITY else 0 end ) as INQTY, " +
    //"sum( case {alias}.finance_action when 0 then 0 else {alias}.QUANTITY end ) as OUTQTY",
                    "sum( case {alias}.trade_action when 0 then {alias}.QUANTITY else 0 end ) as INQTY, "
                    + "sum( case {alias}.trade_action when 1 then {alias}.QUANTITY else 0 end ) as OUTQTY , "
                    + "sum( case {alias}.trade_action when 2 then {alias}.QUANTITY else 0 end ) as BUYRETQTY, "
                    + "sum( case {alias}.trade_action when 3 then {alias}.QUANTITY else 0 end ) as SELLRETQTY, "
                    + "sum( case {alias}.trade_action when 6 then {alias}.QUANTITY else 0 end ) as RESQTY , "
                    + "sum( case {alias}.trade_action when 7 then {alias}.QUANTITY else 0 end ) as DELQTY ",
                    "UNIT",
                    new String[] { "unit", "inqty", "outqty", "buyretqty", "sellretqty", "resqty", "delqty" },
                    new Type[] { Hibernate.STRING, Hibernate.DOUBLE, Hibernate.DOUBLE, Hibernate.DOUBLE,
                            Hibernate.DOUBLE, Hibernate.DOUBLE, Hibernate.DOUBLE }));

    if (warehouseBase) {
        //Depo krlml
        pl.add(Projections.groupProperty("warehouse.code"), "warecode")
                .add(Projections.groupProperty("warehouse.name"), "warename");
        crit.addOrder(Order.asc("warehouse.code"));
    }//ww  w  .  j  a  v a2 s. c  o  m

    crit.setProjection(pl);

    crit.add(Restrictions.eq("active", true));

    if (code != null && code.length() > 0) {
        crit.add(Restrictions.ilike("product.code", code, MatchMode.START));
    }

    if (name != null && name.length() > 0) {
        crit.add(Restrictions.ilike("product.name", name, MatchMode.START));
    }

    if (productType != null) {

        if (getProductType() == 1) {
            crit.add(Restrictions.like("this.productType", ProductType.Product));
        } else if (getProductType() == 2) {
            crit.add(Restrictions.like("this.productType", ProductType.Service));

        }
    }

    if (barcode != null && barcode.length() > 0) {
        Criterion criteria1 = Restrictions.like("product.barcode1", barcode, MatchMode.START);
        Criterion criteria2 = Restrictions.like("product.barcode2", barcode, MatchMode.START);
        Criterion criteria3 = Restrictions.like("product.barcode3", barcode, MatchMode.START);

        crit.add(Restrictions.or(criteria1, Restrictions.or(criteria2, criteria3)));
    }

    if (group != null) {
        crit.add(Restrictions.eq("product.group", group));
    }

    if (warehouse != null) {
        crit.add(Restrictions.eq("warehouse", warehouse));
    }

    if (beginDate != null) {
        crit.add(Restrictions.ge("date", beginDate));
    }

    if (endDate != null) {
        crit.add(Restrictions.le("date", endDate));
    }

    if (category != null) {
        crit.add(Restrictions.eq("product.category", category));
    }
    if (docCode != null && docCode.length() > 0) {
        crit.add(Restrictions.ilike("code", docCode, MatchMode.START));
    }

    if (getDocumentType() != null && getDocumentType() != DocumentType.Unknown) {
        crit.add(Restrictions.eq("this.documentType", getDocumentType()));
    }

    if (getWorkBunch() != null) {
        crit.add(Restrictions.eq("this.workBunch", getWorkBunch()));
    }

    /*
    crit.addOrder( Order.asc("date"));
    crit.addOrder( Order.asc("serial"));
     * 
     */

    crit.addOrder(Order.asc("product.name"));

    log.debug("Sonu : #0", crit);

    return crit;
}

From source file:com.ut.tekir.stock.ProductTxnReportBean.java

License:LGPL

public DetachedCriteria buildCriteriaForWarehouse() {

    DetachedCriteria crit = DetachedCriteria.forClass(ProductTxn.class);

    crit.createAlias("product", "product");
    crit.createAlias("warehouse", "warehouse");

    ProjectionList pl = Projections.projectionList();
    pl.add(Projections.groupProperty("product.code"), "prodcode")
            .add(Projections.groupProperty("product.name"), "prodname")
            .add(Projections.groupProperty("product.group"), "group")
            .add(Projections.groupProperty("product.barcode1"), "barcode")
            .add(Projections.groupProperty("warehouse.code"), "warecode")
            .add(Projections.groupProperty("warehouse.name"), "warename")
            .add(Projections.sum("quantity.value"), "quantity")
            .add(Projections.avg("unitPrice.value"), "unitPrice")
            .add(Projections.sqlGroupProjection("{alias}.UNIT as unit, "
                    + "sum( case {alias}.trade_action when 0 then {alias}.QUANTITY else 0 end ) as INQTY, "
                    + "sum( case {alias}.trade_action when 1 then {alias}.QUANTITY else 0 end ) as OUTQTY , "
                    + "sum( case {alias}.trade_action when 2 then {alias}.QUANTITY else 0 end ) as BUYRETQTY, "
                    + "sum( case {alias}.trade_action when 3 then {alias}.QUANTITY else 0 end ) as SELLRETQTY, "
                    + "sum( case {alias}.trade_action when 6 then {alias}.QUANTITY else 0 end ) as RESQTY , "
                    + "sum( case {alias}.trade_action when 7 then {alias}.QUANTITY else 0 end ) as DELQTY ",
                    "UNIT",
                    new String[] { "unit", "inqty", "outqty", "buyretqty", "sellretqty", "resqty", "delqty" },
                    new Type[] { Hibernate.STRING, Hibernate.DOUBLE, Hibernate.DOUBLE, Hibernate.DOUBLE,
                            Hibernate.DOUBLE, Hibernate.DOUBLE, Hibernate.DOUBLE }));

    crit.setProjection(pl);//www . j a va2  s  .  c  om

    crit.add(Restrictions.eq("active", true));

    if (filterModel.getBarcode() != null && filterModel.getBarcode().length() > 0) {
        Criterion criteria1 = Restrictions.eq("product.barcode1", filterModel.getBarcode());
        Criterion criteria2 = Restrictions.eq("product.barcode2", filterModel.getBarcode());
        Criterion criteria3 = Restrictions.eq("product.barcode3", filterModel.getBarcode());

        crit.add(Restrictions.or(criteria1, Restrictions.or(criteria2, criteria3)));
    }

    if (filterModel.getProduct() != null) {
        crit.add(Restrictions.eq("product", filterModel.getProduct()));
    }
    crit.addOrder(Order.asc("product.name"));

    return crit;
}

From source file:DAO.EJBDAO.java

public void getAmountOfProductInDifferentStocks() {

    Criteria criteria = session.createCriteria(StockedProduct.class, "sp").createAlias("sp.product", "pr")
            .add(Restrictions.eq("pr.name", "laban")).setProjection((Projections.sum("amount")));
    Long resultCount = (Long) criteria.uniqueResult();
    System.out.println(resultCount);
}

From source file:de.forsthaus.backend.dao.impl.OrderDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  www  .  j a v a 2 s . c o  m*/
public BigDecimal getOrderSum(Order order) {
    DetachedCriteria criteria = DetachedCriteria.forClass(Orderposition.class);
    criteria.add(Restrictions.eq("order", order));
    criteria.setProjection(Projections.sum("aupGesamtwert"));

    BigDecimal sumResult = (BigDecimal) DataAccessUtils
            .uniqueResult(getHibernateTemplate().findByCriteria(criteria));

    return sumResult;
}

From source file:de.sub.goobi.forms.ProjekteForm.java

License:Open Source License

/**
 * generates values for count of volumes and images for statistics.
 */// w  ww.  j  ava 2s .c o m
@SuppressWarnings("rawtypes")
public void GenerateValuesForStatistics() {
    Criteria crit = Helper.getHibernateSession().createCriteria(Process.class)
            .add(Restrictions.eq("projekt", this.myProjekt));
    ProjectionList pl = Projections.projectionList();
    pl.add(Projections.sum("sortHelperImages"));
    pl.add(Projections.count("sortHelperImages"));
    crit.setProjection(pl);
    List list = crit.list();
    Long images = 0l;
    Long volumes = 0l;
    for (Object obj : list) {
        Object[] row = (Object[]) obj;
        images = (Long) row[0];
        volumes = (Long) row[1];
    }
    this.myProjekt.setNumberOfPages(images.intValue());
    this.myProjekt.setNumberOfVolumes(volumes.intValue());
}

From source file:de.sub.goobi.helper.ProjectHelper.java

License:Open Source License

/**
 * static to reduce load/*from ww w.  ja  v a 2  s . com*/
 *
 * @param project
 *            object
 * @return a GoobiCollection of the following structure: GoobiCollection 1-n
 *         representing the steps each step has the following properties @
 *         stepTitle, stepOrder,
 *         stepCount,stepImageCount,totalProcessCount,totalImageCount which
 *         can get extracted by the IGoobiCollection Interface using the
 *         getItem(<name>) method standard workflow of the project
 *         according to the definition that only steps shared by all
 *         processes are returned. The workflow order is returned according
 *         to the average order return by a grouping by step title consider
 *         workflow structure to be a prototype, it would probably make
 *         things easier, to either assemble the underlying construction in
 *         separate classes or to create a new class with these properties
 */

@SuppressWarnings("unchecked")
public static synchronized List<StepInformation> getProjectWorkFlowOverview(Project project) {
    Long totalNumberOfProc = 0l;
    Long totalNumberOfImages = 0l;

    Session session = Helper.getHibernateSession();

    Criteria critTotals = session.createCriteria(Process.class, "proc");
    critTotals.add(Restrictions.eq("proc.template", Boolean.FALSE));
    critTotals.add(Restrictions.eq("proc.project", project));

    ProjectionList proList = Projections.projectionList();

    proList.add(Projections.count("proc.id"));
    proList.add(Projections.sum("proc.sortHelperImages"));

    critTotals.setProjection(proList);

    List<Object> list = critTotals.list();

    for (Object obj : list) {
        Object[] row = (Object[]) obj;

        totalNumberOfProc = (Long) row[FieldList.totalProcessCount.fieldLocation];
        totalNumberOfImages = (Long) row[FieldList.totalImageCount.fieldLocation];
        ;
    }

    proList = null;
    list = null;

    Criteria critSteps = session.createCriteria(Task.class);

    critSteps.createCriteria("process", "proc");
    critSteps.addOrder(Order.asc("ordering"));

    critSteps.add(Restrictions.eq("proc.template", Boolean.FALSE));
    critSteps.add(Restrictions.eq("proc.project", project));

    proList = Projections.projectionList();

    proList.add(Projections.groupProperty(("title")));
    proList.add(Projections.count("id"));
    proList.add(Projections.avg("ordering"));

    critSteps.setProjection(proList);

    // now we have to discriminate the hits where the max number of hits
    // doesn't reach numberOfProcs
    // and extract a workflow, which is the workflow common for all
    // processes according to its titel
    // the position will be calculated by the average of 'reihenfolge' of
    // steps

    list = critSteps.list();

    String title;
    Double averageStepOrder;
    Long numberOfSteps;
    Long numberOfImages;

    List<StepInformation> workFlow = new ArrayList<>();

    for (Object obj : list) {
        Object[] row = (Object[]) obj;

        title = (String) (row[FieldList.stepName.fieldLocation]);
        numberOfSteps = (Long) (row[FieldList.stepCount.fieldLocation]);
        averageStepOrder = (Double) (row[FieldList.stepOrder.fieldLocation]);

        // in this step we only take the steps which are present in each of
        // the workflows
        if (numberOfSteps.equals(totalNumberOfProc)) {
            StepInformation newStep = new StepInformation(title, averageStepOrder);
            newStep.setNumberOfTotalImages(totalNumberOfImages.intValue());
            newStep.setNumberOfTotalSteps(totalNumberOfProc.intValue());
            workFlow.add(newStep);
        }
    }

    Criteria critStepDone = session.createCriteria(Task.class, "step");

    critStepDone.createCriteria("process", "proc");

    critStepDone.add(Restrictions.eq("step.processingStatus", TaskStatus.DONE.getValue()));
    critStepDone.add(Restrictions.eq("proc.template", Boolean.FALSE));
    critStepDone.add(Restrictions.eq("proc.project", project));

    ProjectionList proCount = Projections.projectionList();

    proCount.add(Projections.groupProperty(("step.title")));
    proCount.add(Projections.count("proc.id"));
    proCount.add(Projections.sum("proc.sortHelperImages"));

    critStepDone.setProjection(proCount);

    list = critStepDone.list();

    for (Object obj : list) {

        Object[] row = (Object[]) obj;

        title = (String) (row[FieldList.stepName.fieldLocation]);
        numberOfSteps = (Long) (row[FieldList.stepCount.fieldLocation]);
        numberOfImages = (Long) (row[FieldList.imageCount.fieldLocation]);

        // getting from the workflow collection the collection which
        // represents step <title>
        // we only created one for each step holding the counts of processes
        for (StepInformation currentStep : workFlow) {
            if (currentStep.getTitle().equals(title)) {
                currentStep.setNumberOfStepsDone(numberOfSteps.intValue());
                currentStep.setNumberOfImagesDone(numberOfImages.intValue());
            }
        }
    }
    Comparator<StepInformation> comp = new compareWorkflowSteps();
    Collections.sort(workFlow, comp);
    return workFlow;
}

From source file:de.tuclausthal.submissioninterface.persistence.dao.impl.PointCategoryDAO.java

License:Open Source License

@Override
public int countPoints(Task task) {
    Integer ret = (Integer) session.createCriteria(PointCategory.class).add(Restrictions.eq("task", task))
            .add(Restrictions.eq("optional", false)).setProjection(Projections.sum("points")).uniqueResult();
    if (ret == null) {
        return 0;
    }//w  w w.j a  v a  2 s  . c  o m
    return ret;
}

From source file:es.sm2.openppm.core.dao.BusinessdriverDAO.java

License:Open Source License

/**
 * get sum relative priorization/* w ww  .j av a  2 s  .c o m*/
 * @param businessdriverset
 * @return
 */
public double getTotalPriority(Businessdriverset businessdriverset) {
    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setProjection(Projections.sum(Businessdriver.RELATIVEPRIORIZATION))
            .add(Restrictions.eq(Businessdriver.BUSINESSDRIVERSET, businessdriverset));

    Double total = (Double) crit.uniqueResult();
    return (total == null ? 0.0 : total);
}

From source file:es.sm2.openppm.core.dao.ChargescostsDAO.java

License:Open Source License

public double getSumCosts(Project project, int type) {

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setProjection(Projections.sum(Chargescosts.COST))
            .add(Restrictions.eq(Chargescosts.PROJECT, project))
            .add(Restrictions.eq(Chargescosts.IDCHARGETYPE, type));

    Double cost = (Double) crit.uniqueResult();
    return (cost == null ? 0 : cost);
}

From source file:es.sm2.openppm.core.dao.ChargescostsDAO.java

License:Open Source License

/**
 * Sum costs by currency/* w  ww. j av  a  2 s . c  om*/
 * @param project
 * @param nameCurrency
 * @return
 */
public double getSumCosts(Project project, String nameCurrency) {

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setProjection(Projections.sum(Chargescosts.COST))
            .add(Restrictions.eq(Chargescosts.PROJECT, project)).createCriteria(Chargescosts.CURRENCY)
            .add(Restrictions.eq(Currency.NAME, nameCurrency));

    Double cost = (Double) crit.uniqueResult();
    return (cost == null ? 0 : cost);
}