Example usage for org.hibernate Criteria createCriteria

List of usage examples for org.hibernate Criteria createCriteria

Introduction

In this page you can find the example usage for org.hibernate Criteria createCriteria.

Prototype

public Criteria createCriteria(String associationPath) throws HibernateException;

Source Link

Document

Create a new Criteria, "rooted" at the associated entity.

Usage

From source file:com.ar.dev.tierra.api.dao.impl.StockDAOImpl.java

@Override
public List<StockLibertador> searchByFacturaStockLibertador(int idFactura) {
    Criteria criteria = getSession().createCriteria(StockLibertador.class);
    criteria.add(Restrictions.eq("estado", true));
    Criteria producto = criteria.createCriteria("idProducto");
    Criteria factura = producto.createCriteria("facturaProducto");
    factura.add(Restrictions.eq("idFacturaProducto", idFactura));
    criteria.addOrder(Order.asc("idStock"));
    List<StockLibertador> list = criteria.list();
    return list;//  w  ww .ja va 2 s .com
}

From source file:com.ar.dev.tierra.api.dao.impl.StockDAOImpl.java

@Override
@SuppressWarnings("null")
public List<WrapperStock> searchByBarcodeInStock(int sucursal, String barcode) {
    Criteria criteria = null;
    switch (sucursal) {
    case 1://  w  ww  .j  a va  2s.c  o  m
        criteria = getSession().createCriteria(StockTierra.class);
        break;
    case 2:
        criteria = getSession().createCriteria(StockBebelandia.class);
        break;
    case 3:
        criteria = getSession().createCriteria(StockLibertador.class);
        break;
    }
    criteria.add(Restrictions.eq("estado", true));
    Criteria producto = criteria.createCriteria("idProducto");
    producto.add(Restrictions.ilike("codigoProducto", barcode, MatchMode.START));
    criteria.add(Restrictions.gt("cantidad", 0));
    criteria.addOrder(Order.desc("idStock"));
    List<WrapperStock> list = new ArrayList<>();
    switch (sucursal) {
    case 1:
        List<StockTierra> tierraList = criteria.list();
        for (StockTierra stockTierra : tierraList) {
            WrapperStock wrapperTierra = new WrapperStock();
            wrapperTierra.setStockTierra(stockTierra);
            list.add(wrapperTierra);
        }
        break;
    case 2:
        List<StockBebelandia> bebeList = criteria.list();
        for (StockBebelandia stockBebelandia : bebeList) {
            WrapperStock wrapperBebelandia = new WrapperStock();
            wrapperBebelandia.setStockBebelandia(stockBebelandia);
            list.add(wrapperBebelandia);
        }
        break;
    case 3:
        List<StockLibertador> libertadorList = criteria.list();
        for (StockLibertador stockLibertador : libertadorList) {
            WrapperStock wrapperLibertador = new WrapperStock();
            wrapperLibertador.setStockLibertador(stockLibertador);
            list.add(wrapperLibertador);
        }
        break;
    }
    return list;
}

From source file:com.ar.dev.tierra.api.dao.impl.TarjetaDAOimpl.java

@Override
public List<Tarjeta> searchEntidadMedio(int idEntidad, int idMedio) {
    Criteria criteria = getSession().createCriteria(Tarjeta.class);
    criteria.add(Restrictions.eq("estadoTarjeta", true));
    Criteria entidadTarjeta = criteria.createCriteria("entidadBancaria");
    entidadTarjeta.add(Restrictions.eq("idEntidadMonetaria", idEntidad));
    Criteria medioTarjeta = criteria.createCriteria("medioPago");
    medioTarjeta.add(Restrictions.eq("idMedioPago", idMedio));
    List<Tarjeta> list = criteria.list();
    return list;/* w  w w.  ja  v  a 2s.c  o m*/
}

From source file:com.ar.dev.tierra.api.dao.impl.UsuariosDAOImpl.java

@Override
public List<Usuarios> getVendedores(int idSucursal) {
    Criteria criteria = getSession().createCriteria(Usuarios.class);
    criteria.add(Restrictions.eq("estado", true));
    Criteria roles = criteria.createCriteria("roles");
    roles.add(Restrictions.or(Restrictions.eq("idRol", 2), Restrictions.eq("idRol", 3),
            Restrictions.eq("idRol", 6)));
    Criteria sucursal = criteria.createCriteria("usuarioSucursal");
    sucursal.add(Restrictions.eq("idSucursal", idSucursal));
    criteria.addOrder(Order.asc("idUsuario"));
    List<Usuarios> us = criteria.list();
    return us;/*from  w  w w .  j a  v a 2  s .c om*/
}

From source file:com.bluexml.side.Framework.alfresco.jbpm.CustomJBPMEngine.java

License:Open Source License

@SuppressWarnings("unchecked")
private List<ProcessInstance> getProcessInstances(final Long processDefId, final Boolean active) {
    return (List<ProcessInstance>) jbpmTemplate.execute(new JbpmCallback() {
        public Object doInJbpm(JbpmContext context) {
            Session session = context.getSession();
            Criteria criteria = session.createCriteria(ProcessInstance.class);
            if (processDefId != null) {
                Criteria definitionCriteria = criteria.createCriteria("processDefinition");
                definitionCriteria.add(Restrictions.eq("id", processDefId));
            }//from w  ww. j av a  2 s . c om
            if (Boolean.TRUE.equals(active)) {
                criteria.add(Restrictions.isNull("end"));
            } else if (Boolean.FALSE.equals(active)) {
                criteria.add(Restrictions.isNotNull("end"));
            }
            return criteria.list();
        }
    });
}

From source file:com.bluexml.side.Framework.alfresco.jbpm.CustomJBPMEngine.java

License:Open Source License

/**
 * Construct a JBPM Hibernate query based on the Task Query provided
 * /*  w  ww .j a  v a  2s .  com*/
 * @param session
 * @param query
 * @return  jbpm hiberate query criteria
 */
private Criteria createTaskQueryCriteria(Session session, WorkflowTaskQuery query) {
    Criteria task = session.createCriteria(TaskInstance.class);

    // task id
    if (query.getTaskId() != null) {
        task.add(Restrictions.eq("id", getJbpmId(query.getTaskId())));
    }

    // task state
    if (query.getTaskState() != null) {
        WorkflowTaskState state = query.getTaskState();
        if (state == WorkflowTaskState.IN_PROGRESS) {
            task.add(Restrictions.eq("isOpen", true));
            task.add(Restrictions.isNull("end"));
        } else if (state == WorkflowTaskState.COMPLETED) {
            task.add(Restrictions.eq("isOpen", false));
            task.add(Restrictions.isNotNull("end"));
        }
    }

    // task name
    if (query.getTaskName() != null) {
        task.add(Restrictions.eq("name", query.getTaskName().toPrefixString(namespaceService)));
    }

    // task actor
    if (query.getActorId() != null) {
        task.add(Restrictions.eq("actorId", query.getActorId()));
    }

    // task custom properties
    if (query.getTaskCustomProps() != null) {
        Map<QName, Object> props = query.getTaskCustomProps();
        if (props.size() > 0) {
            Criteria variables = task.createCriteria("variableInstances");
            Disjunction values = Restrictions.disjunction();
            for (Map.Entry<QName, Object> prop : props.entrySet()) {
                Conjunction value = Restrictions.conjunction();
                value.add(Restrictions.eq("name", factory.mapQNameToName(prop.getKey())));
                value.add(Restrictions.eq("value", prop.getValue().toString()));
                values.add(value);
            }
            variables.add(values);
        }
    }

    // process criteria
    Criteria process = createProcessCriteria(task, query);

    // process custom properties
    if (query.getProcessCustomProps() != null) {
        // TODO: Due to Hibernate bug
        // http://opensource.atlassian.com/projects/hibernate/browse/HHH-957
        // it's not possible to perform a sub-select with the criteria api.
        // For now issue a
        //       secondary query and create an IN clause.
        Map<QName, Object> props = query.getProcessCustomProps();
        if (props.size() > 0) {
            // create criteria for process variables
            Criteria variables = session.createCriteria(VariableInstance.class);
            variables.setProjection(Projections.distinct(Property.forName("processInstance")));
            Disjunction values = Restrictions.disjunction();
            for (Map.Entry<QName, Object> prop : props.entrySet()) {
                Conjunction value = Restrictions.conjunction();
                value.add(Restrictions.eq("name", factory.mapQNameToName(prop.getKey())));
                value.add(Restrictions.eq("value", prop.getValue().toString()));
                values.add(value);
            }
            variables.add(values);

            // note: constrain process variables to same criteria as tasks
            createProcessCriteria(variables, query);

            Disjunction processIdCriteria = createProcessIdCriteria(variables);

            // constrain tasks by process list
            process = (process == null) ? task.createCriteria("processInstance") : process;
            process.add(processIdCriteria);
        }
    }

    // order by
    if (query.getOrderBy() != null) {
        WorkflowTaskQuery.OrderBy[] orderBy = query.getOrderBy();
        for (WorkflowTaskQuery.OrderBy orderByPart : orderBy) {
            if (orderByPart == WorkflowTaskQuery.OrderBy.TaskActor_Asc) {
                task.addOrder(Order.asc("actorId"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskActor_Desc) {
                task.addOrder(Order.desc("actorId"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskCreated_Asc) {
                task.addOrder(Order.asc("create"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskCreated_Desc) {
                task.addOrder(Order.desc("create"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskDue_Asc) {
                task.addOrder(Order.asc("dueDate"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskDue_Desc) {
                task.addOrder(Order.desc("dueDate"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskId_Asc) {
                task.addOrder(Order.asc("id"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskId_Desc) {
                task.addOrder(Order.desc("id"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskName_Asc) {
                task.addOrder(Order.asc("name"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskName_Desc) {
                task.addOrder(Order.desc("name"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskState_Asc) {
                task.addOrder(Order.asc("end"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskState_Desc) {
                task.addOrder(Order.desc("end"));
            }
        }
    }

    // limit results
    if (query.getLimit() != -1) {
        task.setMaxResults(query.getLimit());
    }

    return task;
}

From source file:com.bluexml.side.Framework.alfresco.jbpm.CustomJBPMEngine.java

License:Open Source License

/**
 * Create process-specific query criteria
 * //  ww  w.j a  v a  2 s.  co  m
 * @param root
 * @param query
 * @return
 */
private Criteria createProcessCriteria(Criteria root, WorkflowTaskQuery query) {
    Criteria process = null;

    // process active?
    if (query.isActive() != null) {
        process = root.createCriteria("processInstance");
        if (query.isActive()) {
            process.add(Restrictions.isNull("end"));
        } else {
            process.add(Restrictions.isNotNull("end"));
        }
    }

    // process id
    if (query.getProcessId() != null) {
        process = (process == null) ? root.createCriteria("processInstance") : process;
        process.add(Restrictions.eq("id", getJbpmId(query.getProcessId())));
    }

    // process definition name
    String definitionName = query.getWorkflowDefinitionName();
    if (definitionName != null) {
        definitionName = createLocalId(definitionName);
    }
    if (definitionName == null) {
        QName qName = query.getProcessName();
        definitionName = qName == null ? null : qName.toPrefixString(namespaceService);
    }
    if (definitionName != null) {
        process = (process == null) ? root.createCriteria("processInstance") : process;
        Criteria processDef = process.createCriteria("processDefinition");
        String processName = tenantService.getName(definitionName);
        processDef.add(Restrictions.eq("name", processName));
    }

    return process;
}

From source file:com.bluexml.side.Framework.alfresco.jbpm.CustomJBPMEngine.java

License:Open Source License

public WorkflowTask getStartTask(final String workflowInstanceId) {
    try {/*from  w w  w .  j a v a  2s. c  o  m*/
        return (WorkflowTask) jbpmTemplate.execute(new JbpmCallback() {
            public Object doInJbpm(JbpmContext context) {
                // retrieve process instance
                GraphSession graphSession = context.getGraphSession();
                ProcessInstance processInstance = getProcessInstanceIfExists(graphSession, workflowInstanceId);
                if (processInstance == null) {
                    return null;
                }
                Task startTask = processInstance.getProcessDefinition().getTaskMgmtDefinition().getStartTask();

                // retrieve task
                Session session = context.getSession();
                Criteria taskCriteria = session.createCriteria(TaskInstance.class);
                taskCriteria.add(Restrictions.eq("name", startTask.getName()));
                Criteria process = taskCriteria.createCriteria("processInstance");
                process.add(Restrictions.eq("id", processInstance.getId()));
                TaskInstance taskInstance = (TaskInstance) taskCriteria.uniqueResult();
                return createWorkflowTask(taskInstance);
            }
        });
    } catch (JbpmException e) {
        String msg = messageService.getMessage(ERR_GET_START_TASK, workflowInstanceId);
        throw new WorkflowException(msg, e);
    }
}

From source file:com.cubeia.backoffice.users.dao.UserDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w w  w  . j av a2 s  . c om*/
public Collection<User> findUsersByExample(UserInformation ui, Long operatorId) {
    Session hbs = getHibernateSession();
    Criteria uc = hbs.createCriteria(User.class);
    uc.add(Restrictions.ne("status", REMOVED));
    uc.add(Restrictions.eq("userType", UserType.USER));

    if (operatorId != null && operatorId >= 0) {
        uc.add(Restrictions.eq("operatorId", operatorId));
    }

    Example ex = Example.create(ui);
    uc.createCriteria("information").add(ex);
    return uc.list();
}

From source file:com.devnexus.ting.repository.jpa.PresentationRepositoryImpl.java

License:Apache License

@Override
public List<Presentation> findPresentations(PresentationSearchQuery presentationSearchQuery) {

    Session session = (Session) entityManager.getDelegate();

    final Criteria rootCriteria = session.createCriteria(Presentation.class);
    final Criteria eventCriteria = rootCriteria.createCriteria("event");

    if (presentationSearchQuery.getEvent() != null
            && presentationSearchQuery.getEvent().getEventKey() != null) {
        eventCriteria.add(/*from   w  w w .j  av a  2  s .  c  o m*/
                Restrictions.eq("eventKey", presentationSearchQuery.getEvent().getEventKey()).ignoreCase());
    }

    if (presentationSearchQuery.getTrack() != null) {
        if (presentationSearchQuery.getTrack().getId() != null) {
            rootCriteria.createAlias("track", "t", JoinType.INNER_JOIN,
                    Restrictions.eq("t.id", presentationSearchQuery.getTrack().getId()));
        } else if (presentationSearchQuery.getTrack().getName() != null) {
            rootCriteria.createAlias("track", "t", JoinType.INNER_JOIN,
                    Restrictions.eq("t.name", presentationSearchQuery.getTrack().getName()).ignoreCase());
        }
    } else {
        rootCriteria.createAlias("track", "t", JoinType.RIGHT_OUTER_JOIN);
    }

    if (!presentationSearchQuery.getPresentationTags().isEmpty()) {
        final Criteria tagsCriteria = rootCriteria.createCriteria("presentationTags");
        tagsCriteria.add(Restrictions.in("name", presentationSearchQuery.getPresentationTagNames()));
    }

    return rootCriteria.list();
}