Example usage for org.hibernate.criterion Restrictions in

List of usage examples for org.hibernate.criterion Restrictions in

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions in.

Prototype

public static Criterion in(String propertyName, Collection values) 

Source Link

Document

Apply an "in" constraint to the named property.

Usage

From source file:com.denimgroup.threadfix.data.dao.hibernate.VulnerabilitySearchCriteriaConstructor.java

License:Mozilla Public License

private void addDefectOptions() {
    boolean showDefectPresent = parameters.getShowDefectPresent() != null && parameters.getShowDefectPresent();
    boolean showDefectNotPresent = parameters.getShowDefectNotPresent() != null
            && parameters.getShowDefectNotPresent();
    boolean showDefectOpen = parameters.getShowDefectOpen() != null && parameters.getShowDefectOpen();
    boolean showDefectClosed = parameters.getShowDefectClosed() != null && parameters.getShowDefectClosed();

    Criterion defectRestrictions = null;
    boolean defectAliasCreated = false;

    if (showDefectPresent) {
        defectRestrictions = disjoinRestrictions(defectRestrictions, Restrictions.isNotNull("defect"));
        LOG.debug("Adding defect not null restriction");
    }/*  ww  w .j  av a2  s  .c  o  m*/

    if (showDefectNotPresent) {
        defectRestrictions = disjoinRestrictions(defectRestrictions, Restrictions.isNull("defect"));
        LOG.debug("Adding defect is null restriction");
    }

    if (showDefectOpen) {
        if (!defectAliasCreated) {
            criteria.createAlias("defect", "d", Criteria.LEFT_JOIN);
            defectAliasCreated = true;
        }
        defectRestrictions = disjoinRestrictions(defectRestrictions,
                Restrictions.in("d.status", Defect.OPEN_CODES));
        LOG.debug("Adding defect is open restriction");
    }

    if (showDefectClosed) {
        if (!defectAliasCreated) {
            criteria.createAlias("defect", "d", Criteria.LEFT_JOIN);
            defectAliasCreated = true;
        }
        defectRestrictions = disjoinRestrictions(defectRestrictions,
                Restrictions.in("d.status", Defect.CLOSED_CODES));
        LOG.debug("Adding defect is closed restriction");
    }

    if (defectRestrictions != null) {
        criteria.add(defectRestrictions);
    }
}

From source file:com.denimgroup.threadfix.data.dao.hibernate.VulnerabilitySearchCriteriaConstructor.java

License:Mozilla Public License

private void addSeverities() {
    if (parameters.getGenericSeverities() != null && !parameters.getGenericSeverities().isEmpty()) {
        List<Integer> severityIntValues = list();
        for (GenericSeverity severity : parameters.getGenericSeverities()) {
            severityIntValues.add(severity.getIntValue());
        }//from w  w w.j  av a  2  s  . co  m
        if (!severityIntValues.isEmpty()) {
            LOG.debug("Restricting severity ID to one of " + severityIntValues);
            criteria.add(Restrictions.in("severity.intValue", severityIntValues));
        }
    }
}

From source file:com.denimgroup.threadfix.data.dao.hibernate.VulnerabilitySearchCriteriaConstructor.java

License:Mozilla Public License

private void addChannelTypeRestrictions() {

    // Limit scanner if present
    if (parameters.getChannelTypes() != null && !parameters.getChannelTypes().isEmpty()) {
        List<Integer> channelTypeIds = list();
        for (ChannelType channelType : parameters.getChannelTypes()) {
            if (channelType.getId() != null) {
                channelTypeIds.add(channelType.getId());
            }//from w  w w  .j  av  a  2  s  .com
        }

        if (!channelTypeIds.isEmpty()) {
            Criteria subCriteria = session.createCriteria(Finding.class);

            subCriteria.createAlias("scan", "myScan");
            subCriteria.createAlias("myScan.applicationChannel", "myApplicationChannel");
            subCriteria.createAlias("myApplicationChannel.channelType", "myChannelType");

            subCriteria.add(Restrictions.in("myChannelType.id", channelTypeIds));

            subCriteria.setProjection(Projections.property("vulnerability.id"));

            List<Integer> ids = (List<Integer>) subCriteria.list();

            if (ids.isEmpty()) {
                LOG.debug("Got no valid Scanner type IDs.");
                ids.add(0); // should yield no results
                // throw an exception here?
            } else {
                LOG.debug("Adding scanner restriction: " + channelTypeIds);
            }

            criteria.add(Restrictions.in("id", ids));
        }
    }
}

From source file:com.denimgroup.threadfix.data.dao.hibernate.VulnerabilitySearchCriteriaConstructor.java

License:Mozilla Public License

private void addVulnCommentRestrictions() {
    if ((parameters.getShowCommentPresent() == null || !parameters.getShowCommentPresent())
            && (parameters.getCommentTags() == null || parameters.getCommentTags().isEmpty()))
        return;//from   w  w  w.j a  v  a2 s . c o m
    criteria.createAlias("vulnerabilityComments", "commentAlias");

    List<Integer> tagIds = list();

    if (parameters.getCommentTags() != null) {
        for (Tag tag : parameters.getCommentTags()) {
            if (tag.getId() != null) {
                tagIds.add(tag.getId());
            }
        }
    }

    if (tagIds.isEmpty()) {
        LOG.debug("No vulnerability comment tag IDs found in parameters.");
    } else {
        criteria.createAlias("vulnerabilityComments.tags", "tagsAlias");
        criteria.add(Restrictions.in("tagsAlias.id", tagIds));
        LOG.debug("Added vulnerability comment tags with IDs " + tagIds);
    }

}

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   ww w .j  a  v a  2  s  .  c  om
                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();
}

From source file:com.duroty.application.admin.manager.AdminManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*from   ww  w  . j  a v  a2s  . co m*/
 *
 * @param hsession DOCUMENT ME!
 * @param userObj DOCUMENT ME!
 *
 * @throws AdminException DOCUMENT ME!
 */
public void addUser(Session hsession, UserObj userObj) throws AdminException {
    try {
        Users users = new Users();
        users.setUseActive(userObj.isActive());
        users.setUseEmail(userObj.getEmail());
        users.setUseLanguage(userObj.getLanguage());
        users.setUseName(userObj.getName());
        users.setUsePassword(userObj.getPassword());
        users.setUseRegisterDate(userObj.getRegisterDate());
        users.setUseUsername(userObj.getUsername());

        if ((userObj.getRoles() != null) && (userObj.getRoles().length > 0)) {
            Criteria crit = hsession.createCriteria(Roles.class);
            crit.add(Restrictions.in("rolIdint", userObj.getRoles()));

            ScrollableResults scroll = crit.scroll();

            while (scroll.next()) {
                UserRole userRole = new UserRole(new UserRoleId(users, (Roles) scroll.get(0)));
                users.addUserRole(userRole);
            }
        }

        hsession.save(users);
        hsession.flush();

        MailPreferences mailPreferences = new MailPreferences();
        mailPreferences.setUsers(users);
        mailPreferences.setMaprHtmlMessage(userObj.isHtmlMessages());
        mailPreferences.setMaprMessagesByPage(userObj.getMessagesByPage());
        mailPreferences.setMaprQuotaSize(userObj.getQuotaSize());

        if (userObj.getSignature() != null) {
            mailPreferences.setMaprSignature("\n" + userObj.getSignature().replaceAll("'", "\\\\'"));
        }

        if (userObj.isSpamTolerance()) {
            mailPreferences.setMaprSpamTolerance(100);
        } else {
            mailPreferences.setMaprSpamTolerance(-1);
        }

        mailPreferences.setMaprVacationActive(userObj.isVactionActive());
        mailPreferences.setMaprVacationBody(userObj.getVacationBody());
        mailPreferences.setMaprVacationSubject(userObj.getVacationSubject());

        hsession.save(mailPreferences);
        hsession.flush();

        Identity identity = new Identity();
        identity.setIdeActive(true);
        identity.setIdeCode(RandomStringUtils.randomAlphanumeric(25));
        identity.setIdeDefault(true);
        identity.setIdeEmail(userObj.getEmailIdentity());
        identity.setIdeName(userObj.getName());
        identity.setIdeReplyTo(userObj.getEmailIdentity());
        identity.setUsers(users);

        hsession.save(identity);
        hsession.flush();
    } catch (Exception ex) {
        throw new AdminException(ex);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.admin.manager.AdminManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*from   w ww . j ava  2  s. c  o m*/
 *
 * @param hsession DOCUMENT ME!
 * @param userObj DOCUMENT ME!
 *
 * @throws AdminException DOCUMENT ME!
 */
public void updateUser(Session hsession, UserObj userObj) throws AdminException {
    try {
        Criteria crit1 = hsession.createCriteria(Users.class);
        crit1.add(Restrictions.eq("useIdint", new Integer(userObj.getIdint())));

        Users users = (Users) crit1.uniqueResult();

        if (users == null) {
            throw new AdminException("The user don't exist");
        }

        Set roles = users.getUserRoles();
        Iterator it = roles.iterator();

        while (it.hasNext()) {
            UserRole ur = (UserRole) it.next();

            //roles.remove(ur);
            hsession.delete(ur);
        }

        users.setUserRoles(new HashSet(0));
        hsession.update(users);
        hsession.flush();

        users.setUseActive(userObj.isActive());
        users.setUseEmail(userObj.getEmail());
        users.setUseLanguage(userObj.getLanguage());
        users.setUseName(userObj.getName());

        if (!StringUtils.isBlank(userObj.getPassword())) {
            users.setUsePassword(userObj.getPassword());
        }

        if ((userObj.getRoles() != null) && (userObj.getRoles().length > 0)) {
            Criteria crit2 = hsession.createCriteria(Roles.class);
            crit2.add(Restrictions.in("rolIdint", userObj.getRoles()));

            Set set = new HashSet();
            ScrollableResults scroll = crit2.scroll();

            while (scroll.next()) {
                UserRole userRole = new UserRole(new UserRoleId(users, (Roles) scroll.get(0)));
                set.add(userRole);
            }

            users.setUserRoles(set);

            hsession.update(users);
            hsession.flush();
        }

        it = users.getMailPreferenceses().iterator();

        MailPreferences mailPreferences = (MailPreferences) it.next();
        mailPreferences.setUsers(users);
        mailPreferences.setMaprHtmlMessage(userObj.isHtmlMessages());
        mailPreferences.setMaprMessagesByPage(userObj.getMessagesByPage());
        mailPreferences.setMaprQuotaSize(userObj.getQuotaSize());

        if (userObj.getSignature() != null) {
            mailPreferences.setMaprSignature("\n" + userObj.getSignature().replaceAll("'", "\\\\'"));
        }

        if (userObj.isSpamTolerance()) {
            mailPreferences.setMaprSpamTolerance(100);
        } else {
            mailPreferences.setMaprSpamTolerance(-1);
        }

        mailPreferences.setMaprVacationActive(userObj.isVactionActive());
        mailPreferences.setMaprVacationBody(userObj.getVacationBody());
        mailPreferences.setMaprVacationSubject(userObj.getVacationSubject());

        hsession.update(mailPreferences);
        hsession.flush();
    } catch (Exception ex) {
        throw new AdminException(ex);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.admin.manager.AdminManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*from  w  ww.  j  a  v a  2 s . c  o m*/
 *
 * @param hsession DOCUMENT ME!
 * @param idint DOCUMENT ME!
 *
 * @throws AdminException DOCUMENT ME!
 */
public void deleteUsers(Session hsession, Integer[] idints) throws AdminException {
    try {
        Criteria crit = hsession.createCriteria(Users.class);
        crit.add(Restrictions.in("useIdint", idints));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            Users users = (Users) scroll.get(0);

            if (users != null) {
                hsession.delete(users);
                hsession.flush();
            }
        }
    } catch (Exception ex) {
        throw new AdminException(ex);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!//from ww w.ja v a2s.  c  o  m
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param mids DOCUMENT ME!
 * @param folder DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public void moveFiles(Session hsession, String repositoryName, String[] mids, String folder)
        throws FilesException {
    if ((mids == null) || (mids.length == 0)) {
        throw new FilesException("ErrorMessages.messages.selection.null");
    }

    try {
        folder = parseFolder(folder);

        if (folder.equals(this.folderAll)) {
            folder = this.folderHidden;
        }

        Criteria crit = hsession.createCriteria(Message.class);
        crit.add(Restrictions.in("mesName", mids));
        crit.add(Restrictions.eq("users", getUser(hsession, repositoryName)));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            message.setMesBox(folder);

            hsession.update(message);
            hsession.flush();
        }
    } catch (Exception ex) {
        throw new FilesException(ex);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.mail.manager.MailManager.java

License:Open Source License

/**
 * DOCUMENT ME!//w ww  .j  av a2  s . c  om
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param mids DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
public void applyLabel(Session hsession, String repositoryName, Integer label, String[] mids)
        throws MailException {
    if ((mids == null) || (mids.length == 0)) {
        throw new MailException("ErrorMessages.messages.selection.null");
    }

    try {
        if ((label == null) || (label.intValue() <= 0)) {
            return;
        }

        Users user = getUser(hsession, repositoryName);

        Criteria crit = hsession.createCriteria(Label.class);
        crit.add(Restrictions.eq("labIdint", label));
        crit.add(Restrictions.eq("users", user));

        Label hlabel = (Label) crit.uniqueResult();

        crit = hsession.createCriteria(Message.class);
        crit.add(Restrictions.in("mesName", mids));
        crit.add(Restrictions.eq("users", getUser(hsession, repositoryName)));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            LabMes labMes = new LabMes(new LabMesId(message, hlabel));
            hsession.saveOrUpdate(labMes);
            hsession.flush();
        }

        hsession.flush();
    } catch (Exception e) {
        throw new MailException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}