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.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

public Model getModelByManufacturerModelID(Manufacturer manufacturer, String id) throws DMException {
    if (id == null || id.trim().length() == 0 || manufacturer == null) {
        return null;
    }// w ww.  j a  v  a 2 s . c  o  m
    Session session = this.getHibernateSession();
    try {
        Criteria criteria = session.createCriteria(Model.class);
        Criteria manuCriteria = criteria.createCriteria("manufacturer");
        manuCriteria.add(Expression.eq("ID", new Long(manufacturer.getID())));
        criteria.add(Expression.ilike("manufacturerModelId", id));
        List<Model> list = criteria.list();

        if (list.size() == 0) {
            return null;
        }

        if (list.size() == 1) {
            return (Model) list.get(0);
        } else {
            throw new DMException("Result is not unique, many Models have the same External ID: " + id
                    + " below manufacturer ID: " + manufacturer.getID());
        }
    } catch (HibernateException e) {
        throw new DMException(e);
    } finally {
    }
}

From source file:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

public List<Model> getAllModel(Manufacturer manufacturer) throws DMException {
    if (manufacturer == null) {
        return new ArrayList<Model>(0);
    }/*w w  w.  j a v  a 2s .c om*/
    Session session = this.getHibernateSession();
    try {
        Criteria criteria = session.createCriteria(Model.class);
        Criteria manuCriteria = criteria.createCriteria("manufacturer");
        manuCriteria.add(Expression.eq("ID", new Long(manufacturer.getID())));
        List<Model> list = criteria.list();
        return list;
    } catch (HibernateException e) {
        throw new DMException(e);
    } finally {
    }
}

From source file:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

/**
 * TAC./*from  w ww. j a v a  2s .  com*/
 * @param tacString
 * @param length
 * @return
 * @throws DMException
 */
private Model getModelByTAC(String tacString, int length) throws DMException {
    if (StringUtils.isEmpty(tacString)) {
        return null;
    }
    String tacInfo = tacString;
    if (tacString.toUpperCase().startsWith("IMEI:")) {
        tacInfo = tacString.substring(5, tacString.length());
    }
    tacInfo = tacInfo.substring(0, length);

    Session session = this.getHibernateSession();
    Criteria criteria = session.createCriteria(Model.class);
    Criteria tacCriteria = criteria.createCriteria("modelTACs");
    tacCriteria.add(Expression.eq("id.TAC", tacInfo));
    List<Model> list = this.findModel(criteria);
    if (list != null && list.size() > 0) {
        return (Model) list.get(0);
    } else {
        return null;
    }
}

From source file:com.npower.dm.hibernate.management.ProfileAssignmentManagementBeanImpl.java

License:Open Source License

public List<ProfileAssignment> findProfileAssignmentsByStatus(Device device, String status) throws DMException {
    Criteria mainCrt = this.getHibernateSession().createCriteria(ProfileAssignmentEntity.class);
    if (status != null) {
        Criteria deviceStateCrt = mainCrt.createCriteria("jobAssignmentses").createCriteria("jobState")
                .createCriteria("deviceProvisionRequests");
        deviceStateCrt.add(Expression.eq("device", device));
        deviceStateCrt.add(Expression.eq("state", status));
    }/*w  w  w.  ja  va 2  s  .com*/
    mainCrt.add(Expression.eq("device", device));
    mainCrt.addOrder(Order.asc("assignmentIndex"));
    return mainCrt.list();
}

From source file:com.npower.dm.hibernate.management.ProfileConfigManagementBeanImp.java

License:Open Source License

/**
 * Find a ProfileConfigEntity By it's name.
 * /*w w  w. jav a 2 s  .co m*/
 * @see com.npower.dm.management.ProfileConfigBean#getProfileConfigByName(java.lang.String)
 */
public ProfileConfig getProfileConfigByName(String carrierExtID, String categoryName, String name)
        throws DMException {
    if (name == null || name.trim().length() == 0) {
        return null;
    }
    if (carrierExtID == null || carrierExtID.trim().length() == 0) {
        throw new DMException("Missing carrierExtID to find a profile config.");
    }
    if (categoryName == null || categoryName.trim().length() == 0) {
        throw new DMException("Missing categoryName to find a profile config.");
    }
    ManagementBeanFactory factory = this.getManagementBeanFactory();
    CarrierBean carrierBean = factory.createCarrierBean();
    ProfileTemplateBean templateBean = factory.createProfileTemplateBean();

    Session session = this.getHibernateSession();
    try {
        ProfileCategory category = templateBean.getProfileCategoryByName(categoryName);
        if (category == null) {
            throw new DMException(
                    "Missing Profile Category to find a profile config, could not found category: "
                            + categoryName);
        }
        Carrier carrier = carrierBean.getCarrierByExternalID(carrierExtID);
        if (carrier == null) {
            return null;
            //throw new DMException("Missing Carrier to find a profile config, could not found carrier: " + carrierExtID);
        }

        Criteria criteria = session.createCriteria(ProfileConfigEntity.class);

        Criteria subCriteria = criteria.createCriteria("profileTemplate");
        subCriteria.add(Expression.eq("profileCategory", category));
        criteria.add(Expression.eq("carrier", carrier));
        criteria.add(Expression.eq("name", name));

        List<ProfileConfig> list = criteria.list();

        if (list.size() == 0) {
            return null;
        }

        if (list.size() == 1) {
            return (ProfileConfig) list.get(0);
        } else {
            throw new DMException("Result is not unique, more ProfileConfigEntity have the same name: " + name
                    + "[Category: " + categoryName + ", carrier: " + carrierExtID + "]");
        }
    } catch (HibernateException e) {
        throw new DMException(e);
    } finally {
    }
}

From source file:com.npower.dm.hibernate.management.ProfileTemplateManagementBeanImpl.java

License:Open Source License

public ProfileAttribute getProfileAttributeByName(String templateName, String name) throws DMException {
    if (templateName == null || templateName.trim().length() == 0 || name == null
            || name.trim().length() == 0) {
        return null;
    }/*from w  w w  .  jav a 2 s .  c  om*/
    Session session = this.getHibernateSession();
    try {
        ProfileTemplate template = this.getTemplateByName(templateName);
        if (template == null) {
            return null;
        }
        Criteria criteria = session.createCriteria(ProfileAttribute.class);
        Criteria templateCriteria = criteria.createCriteria("profileTemplate");
        criteria.add(Expression.eq("name", name));
        templateCriteria.add(Expression.eq("name", templateName));

        List<ProfileAttribute> list = criteria.list();

        if (list.size() == 0) {
            return null;
        }

        if (list.size() == 1) {
            return (ProfileAttribute) list.get(0);
        } else {
            throw new DMException(
                    "Result is not unique, many ProfileAttributeEntity have the same name: " + name);
        }
    } catch (HibernateException e) {
        throw new DMException(e);
    } finally {
    }
}

From source file:com.npower.dm.hibernate.management.ProvisionJobManagementBeanImpl.java

License:Open Source License

/**
 * @param jobID//from www.  j  ava 2  s .co m
 * @param newState
 * @throws DMException
 */
private void updateJobState(long jobID, String newState) throws DMException {
    try {
        ProvisionJob job = this.loadJobByID(jobID);
        job.setState(newState);
        Session session = this.getHibernateSession();
        session.saveOrUpdate(job);

        Criteria mainCriteria = session.createCriteria(DeviceProvisionRequestEntity.class);
        Criteria subCriteria = mainCriteria.createCriteria("provisionElement");
        subCriteria.add(Expression.eq("provisionRequest", job));
        List<DeviceProvisionRequestEntity> deviceStatusList = subCriteria.list();
        for (int i = 0; i < deviceStatusList.size(); i++) {
            DeviceProvisionRequestEntity deviceStatus = (DeviceProvisionRequestEntity) deviceStatusList.get(i);
            String state = deviceStatus.getState();
            // Where the device's status is not done, broadcast the provisionjob's status to device' status.
            if (!state.equalsIgnoreCase(ProvisionJobStatus.DEVICE_JOB_STATE_DONE)) {
                if (newState.equalsIgnoreCase(ProvisionJob.JOB_STATE_CANCELLED)) {
                    deviceStatus.setState(ProvisionJobStatus.DEVICE_JOB_STATE_CANCELLED);
                } else if (newState.equalsIgnoreCase(ProvisionJob.JOB_STATE_DISABLE)) {
                    deviceStatus.setState(ProvisionJobStatus.DEVICE_JOB_STATE_CANCELLED);
                } else if (newState.equalsIgnoreCase(ProvisionJob.JOB_STATE_APPLIED)) {
                    deviceStatus.setState(ProvisionJobStatus.DEVICE_JOB_STATE_READY);
                }
            }
            //session.saveOrUpdate(deviceStatus);
            session.merge(deviceStatus);
        }
    } catch (HibernateException e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.ProvisionJobManagementBeanImpl.java

License:Open Source License

/**
 * Find Linked Profile Assignment by linkedCategory name.
 * MMS Profile Template, Proxy Template, 
 * Proxy TemplateNAP Template. , MMS Profile
 * ProxyNAP Profile Assignment.//from   w  w w. ja v  a 2  s  . c  o  m
 * <pre>
 * proxyAssignment = this.getLinkedProfileAssignment(mmsAssgingment, ProfileCategory.PROXY_CATEGORY_NAME);
 * napAssignment = this.getLinkedProfileAssignment(mmsAssgingment, ProfileCategory.NAP_CATEGORY_NAME);
 * </pre>
 * 
 * @param assignment
 * @param linkedCategory
 * @return
 * @throws DMException
 */
private ProfileAssignment getLinkedProfileAssignment(ProfileAssignment assignment, String linkedCategory)
        throws DMException {
    ProfileConfig config = assignment.getProfileConfig();
    Device device = assignment.getDevice();

    ProfileAssignment linkedAssignment = null;
    ProfileConfig linkedProfile = null;
    if (StringUtils.isEmpty(linkedCategory)) {
        if (config.getProfileTemplate().getNeedsNap()) {
            linkedProfile = config.getNAPProfile();
        } else if (config.getProfileTemplate().getNeedsProxy()) {
            linkedProfile = config.getProxyProfile();
        }
    } else {
        if (linkedCategory.equalsIgnoreCase(ProfileCategory.PROXY_CATEGORY_NAME)
                && config.getProfileTemplate().getNeedsProxy()) {
            // Case#1 :LinkedProfileCategoryProxy, ProfileProxy Profile
            linkedProfile = config.getProxyProfile();
        } else if (linkedCategory.equalsIgnoreCase(ProfileCategory.NAP_CATEGORY_NAME)) {
            if (config.getProfileTemplate().getNeedsNap()) {
                // Case#2 :LinkedProfileCategoryNAP, ProfileNAP Profile
                linkedProfile = config.getNAPProfile();
            } else {
                // Case#3 :LinkedProfileCategoryNAP, ProfileNAP Profile, 
                if (config.getProfileTemplate().getNeedsProxy()) {
                    ProfileConfig linkedProxyProfile = config.getProxyProfile();
                    if (linkedProxyProfile != null && linkedProxyProfile.getProfileTemplate().getNeedsNap()) {
                        linkedProfile = linkedProxyProfile.getNAPProfile();
                    }
                }
            }
        }
    }

    // Retrieve Linked ProfileAssigment
    if (linkedProfile != null) {
        // Found linked assignment
        // 
        // 1. LinkedProfileAssignment
        // 2. LinkedProfileAssignmentLinked Profile Assignment.
        ManagementBeanFactory factory = this.getManagementBeanFactory();
        SearchBean searchBean = factory.createSearchBean();
        Criteria mainCrt = searchBean.newCriteriaInstance(ProfileAssignmentEntity.class);
        Criteria deviceStateCrt = mainCrt.createCriteria("jobAssignmentses").createCriteria("jobState")
                .createCriteria("deviceProvisionRequests");
        deviceStateCrt.add(Expression.eq("device", device));
        deviceStateCrt.add(Expression.eq("state", ProvisionJobStatus.DEVICE_JOB_STATE_DONE));
        mainCrt.add(Expression.eq("profileConfig", linkedProfile));
        mainCrt.add(Expression.eq("device", device));
        // 
        mainCrt.addOrder(Order.desc("lastSentToDevice"));
        List<ProfileAssignment> linkedAssignments = mainCrt.list();
        if (linkedAssignments.size() > 0) {
            linkedAssignment = linkedAssignments.get(0);
        }
    }
    return linkedAssignment;
}

From source file:com.npower.dm.hibernate.management.ProvisionJobManagementBeanImpl.java

License:Open Source License

public ProvisionJob newJob4Assignment(ProfileAssignment assignment) throws DMException {
    assert (assignment != null) : "Could not assign a NULL profile";
    assert (assignment.getDevice() != null) : "The device related with the profile is NULL!";
    try {/* ww  w  .  j  av  a 2  s  .co  m*/
        Session session = this.getHibernateSession();
        boolean resend = false;
        // Update ProfileAssignment
        session.saveOrUpdate(assignment);

        if (assignment.getID() == 0) {
            resend = true;
        } else {
            {
                // Checking the old assignment
                Criteria mainCriteria = session.createCriteria(DeviceProvisionRequestEntity.class);
                Criteria subCriteria = mainCriteria.createCriteria("jobState");
                Criteria subCriteria4Assi = subCriteria.createCriteria("jobAssignmentses");
                Criteria sub = subCriteria4Assi.createCriteria("profileAssignment");
                sub.add(Expression.eq("ID", new Long(assignment.getID())));
                // ReadyJob
                mainCriteria
                        .add(Expression.or(Expression.eq("state", ProvisionJobStatus.DEVICE_JOB_STATE_READY),
                                Expression.or(
                                        Expression.eq("state", ProvisionJobStatus.DEVICE_JOB_STATE_NOTIFIED),
                                        Expression.eq("state", ProvisionJobStatus.DEVICE_JOB_STATE_DOING))));
                List<DeviceProvisionRequestEntity> list = mainCriteria.list();
                if (list.size() > 0) {
                    ProvisionJobStatus status = (ProvisionJobStatus) list.get(0);
                    return status.getProvisionElement().getProvisionRequest();
                }
            }
            {
                // Re-Assign. 
                // ProfileAssignment ID, ProfileAssignment, ProfileAssingmentRe-Assign
                // : DONEError, .
                Criteria mainCriteria = session.createCriteria(DeviceProvisionRequestEntity.class);
                Criteria subCriteria = mainCriteria.createCriteria("jobState");
                Criteria subCriteria4Assi = subCriteria.createCriteria("jobAssignmentses");
                Criteria sub = subCriteria4Assi.createCriteria("profileAssignment");
                sub.add(Expression.eq("ID", new Long(assignment.getID())));
                mainCriteria.add(Expression.or(Expression.eq("state", ProvisionJobStatus.DEVICE_JOB_STATE_DONE),
                        Expression.eq("state", ProvisionJobStatus.DEVICE_JOB_STATE_ERROR)));
                List<DeviceProvisionRequestEntity> list = mainCriteria.list();
                resend = (list.size() > 0) ? true : false;
            }
        }

        // Create a ProvisionRequest
        ProvisionRequest request = createDefaultProvisionJob();
        if (resend) {
            request.setJobType(ProvisionJob.JOB_TYPE_RE_ASSIGN_PROFILE);
        } else {
            request.setJobType(ProvisionJob.JOB_TYPE_ASSIGN_PROFILE);
        }
        request.setJobMode(ProvisionJob.JOB_MODE_DM);
        request.setScheduledTime(new Date());
        request.setState(ProvisionJob.JOB_STATE_APPLIED);
        request.setTargetType(ProvisionJob.JOB_ELEMENT_TYPE_MULTIPLE);
        request.setLastUpdatedTime(new Date());
        session.saveOrUpdate(request);

        // Create Job_State
        JobState jobState = new JobState();
        jobState.setAskBeforeApply(request.getAskBeforeApply());
        jobState.setAskBeforeDown(request.getAskBeforeDown());
        jobState.setAskCount(request.getAskCount());
        jobState.setAskInterval(request.getAskInterval());
        jobState.setDevice(assignment.getDevice());
        //jobState.setAskUser();
        //jobState.setInstallationState();
        //jobState.setJobAdapter();
        jobState.setJobStateType(request.getJobType());
        jobState.setProvisionRequest(request);
        jobState.setUiMode(request.getUiMode());
        session.saveOrUpdate(jobState);

        // Create a DeviceGroup
        DeviceBean deviceBean = this.getManagementBeanFactory().createDeviceBean();
        DeviceGroup group = deviceBean.newDeviceGroup();
        deviceBean.add(group, assignment.getDevice());
        session.saveOrUpdate(group);

        // Create a Element4Provision
        Element4Provision element = new Element4Provision(group, request);
        //elements.setSourceImage();
        //elements.setState();
        session.saveOrUpdate(element);

        Set<Device> devices = group.getDevices();
        for (Iterator<Device> i = devices.iterator(); i.hasNext();) {
            Device device = (Device) i.next();
            ProvisionJobStatus dpRequest = new DeviceProvisionRequestEntity(element, device);
            dpRequest.setJobState(jobState);

            copyTo(request, dpRequest);
            session.saveOrUpdate(dpRequest);

            // Link to Element3Provision
            element.getDeviceProvReqs().add(dpRequest);

            // Link to Device
            if (device instanceof DeviceEntity) {
                ((DeviceEntity) device).getProvisionJobStatus().add(dpRequest);
            }
            // Link to Job_State
            jobState.getDeviceProvisionRequests().add(dpRequest);

        }

        // Create Job_Assignment
        JobAssignmentsID jobAsisgnmentID = new JobAssignmentsID();
        jobAsisgnmentID.setAssignmentIndex(System.currentTimeMillis());
        jobAsisgnmentID.setJobStateId(jobState.getID());
        jobAsisgnmentID.setProfileAssignmentId(assignment.getID());
        JobAssignments jobAssignment = new JobAssignments(jobAsisgnmentID, assignment, jobState);
        session.saveOrUpdate(jobAssignment);

        return request;

    } catch (DMException e) {
        throw e;
    }
}

From source file:com.npower.dm.hibernate.management.ProvisionJobManagementBeanImpl.java

License:Open Source License

public List<ProvisionJob> findJobs(String jobType, String jobState) throws DMException {
    try {/*from   w ww. ja  v  a 2 s. co  m*/
        Session session = this.getHibernateSession();
        Criteria mainCriteria = session.createCriteria(ProvisionRequest.class);
        mainCriteria.addOrder(Order.asc("scheduledTime"));
        mainCriteria.addOrder(Order.asc("ID"));
        mainCriteria.add(Expression.eq("jobMode", ProvisionJob.JOB_MODE_DM));

        if (StringUtils.isNotEmpty(jobType)) {
            mainCriteria.add(Expression.eq("jobType", jobType));
        }
        if (StringUtils.isNotEmpty(jobState)) {
            Criteria elementCriteria = mainCriteria.createCriteria("provisionElements");

            // Set status criteria
            Criteria statusCriteria = elementCriteria.createCriteria("deviceProvReqs");
            statusCriteria.add(Expression.eq("state", jobState));
        }
        return mainCriteria.list();
    } catch (HibernateException ex) {
        throw new DMException(ex);
    }
}