Example usage for org.hibernate.criterion Restrictions isNotNull

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

Introduction

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

Prototype

public static Criterion isNotNull(String propertyName) 

Source Link

Document

Apply an "is not null" constraint to the named property

Usage

From source file:ca.myewb.build.DevDBSetup.java

License:Open Source License

@SuppressWarnings("unused")
public static void populate() throws Exception {
    UserModel randyBachman, burtonCummings, sydBarrett, rogerWaters, davidGilmour, justinTimberlake,
            singerRecipient, singerSender, singerLeader, guesswhoMember;
    GroupChapterModel theGuessWho, pinkFloyd;
    GroupModel leadSingers, songwriters, bachmanTurnerOverdrive;
    PostModel gw, gwExec, pf, pfExec, ls, sw, bto;
    PostModel gwReply, gqExecReply, pfReply, pfExecReply, lsReply, swReply, btoReply;
    EventModel liveAtParamount, rehearsal, onTheRoad, setup;

    System.out.println("Populating users...");

    //User Population
    randyBachman = UserModel.newAssociateSignUp("randy@theguesswho.ca", "Randy", "Bachman", "guesswho");
    burtonCummings = UserModel.newAssociateSignUp("burton@theguesswho.ca", "Burton", "Cummings", "guesswho");
    sydBarrett = UserModel.newAssociateSignUp("syd@pinkfloyd.co.uk", "Syd", "Barrett", "pinkfloyd");
    rogerWaters = UserModel.newAssociateSignUp("roger@pinkfloyd.co.uk", "Roger", "Waters", "pinkfloyd");
    davidGilmour = UserModel.newAssociateSignUp("david@pinkfloyd.co.uk", "David", "Gilmour", "pinkfloyd");
    justinTimberlake = UserModel.newAssociateSignUp("justin@nsuck.com", "Justin", "Timberlake", "istink");
    singerRecipient = UserModel.newMailingListSignUp("recipient@singers.org");
    singerSender = UserModel.newMailingListSignUp("sender@singers.org");
    singerLeader = UserModel.newMailingListSignUp("leader@singers.org");
    guesswhoMember = UserModel.newMailingListSignUp("guesswhomember@guesswho.ca");

    System.out.println("Populating chapters...");

    //Chapter Setup
    theGuessWho = GroupChapterModel.newChapter();
    pinkFloyd = GroupChapterModel.newChapter();

    theGuessWho.save("Guess Who Professional Chapter", "guesswho", "1 Guess St.\n\n\nWinnipeg\nMB\nH0H0H0\nCA",
            "(416) 222-3561", "(416) 222-3561", "chapter@theguesswho.ca", "http://theguesswho.ca/", false,
            true);/*w  ww .  ja v  a2  s.c  om*/
    pinkFloyd.save("Pink Floyd University Chapter", "pinkfloyd", "1 Pink Ln.\n\n\nLondon\nON\nH0H0H0\nCA",
            "(416) 222-3561", "(416) 222-3561", "chapter#pinkfloyd.co.uk", "http://pinkfloyd.co.uk/", false,
            false);

    System.out.println("Populating non-chapter lists...");

    //Group Setup
    leadSingers = GroupModel.newGroup();
    songwriters = GroupModel.newGroup();
    bachmanTurnerOverdrive = GroupModel.newGroup(theGuessWho);

    leadSingers.save("Lead Singers", "leadsingers", "Group for the lead singers of bands", false);
    songwriters.save("Songwriters", "songwriters", "A group for any songwriters", true);
    bachmanTurnerOverdrive.save("Bachman-Turner Overdrive", "BTO", "For those who went on to BTO", false);

    System.out.println("Populating chapter members...");

    //Putting people in groups
    randyBachman.joinChapter(theGuessWho);
    randyBachman.renew(randyBachman, true);
    randyBachman.upgradeToExec();
    randyBachman.setExecTitle("Exec member");
    burtonCummings.joinChapter(theGuessWho);
    sydBarrett.joinChapter(pinkFloyd);
    rogerWaters.joinChapter(pinkFloyd);
    rogerWaters.upgradeToExec();
    rogerWaters.setExecTitle("Exec member");
    davidGilmour.joinChapter(pinkFloyd);
    davidGilmour.upgradeToExec();
    davidGilmour.setExecTitle("President");
    davidGilmour.addGroup(Helpers.getGroup("UniPresidents"), 'm');
    guesswhoMember.joinChapter(theGuessWho);

    System.out.println("Populating non-chapter list members...");

    //Putting people in Lists
    davidGilmour.upgradeToListLeader(leadSingers);
    rogerWaters.subscribe(leadSingers);
    randyBachman.upgradeToListSender(leadSingers);

    randyBachman.subscribe(bachmanTurnerOverdrive);

    randyBachman.subscribe(songwriters);
    burtonCummings.upgradeToListSender(songwriters);
    davidGilmour.subscribe(songwriters);
    rogerWaters.upgradeToListLeader(songwriters);

    singerRecipient.subscribe(leadSingers);
    singerSender.upgradeToListSender(leadSingers);
    singerLeader.upgradeToListLeader(leadSingers);

    System.out.println("Fudging signup dates...");
    Criteria roleCrit = HibernateUtil.currentSession().createCriteria(RoleModel.class);
    List<RoleModel> roles = new SafeHibList<RoleModel>(roleCrit).list();
    Calendar c = GregorianCalendar.getInstance();
    c.add(Calendar.MONTH, -3);
    Date d = c.getTime();

    for (RoleModel r : roles) {
        if (r.getGroup().getAdmin()) {
            r.setStart(d);
        } else {
            c = GregorianCalendar.getInstance();
            c.add(Calendar.DATE, (int) (Math.random() * (-90)));
            r.setStart(c.getTime());
        }
    }

    System.out.println("Populating posts...");

    //Making Posts
    gw = PostModel.newPost(burtonCummings, theGuessWho, "The Guess Who", "Guess Who's First Post", "I dunno",
            "guess who, canada");
    gwExec = PostModel.newPost(randyBachman, theGuessWho.getExec(), "The Guess Who Exec",
            "Guess Who's First Exec List Post", "I do know!", "guess who, canada, exec");
    pf = PostModel.newPost(rogerWaters, pinkFloyd, "Pink Floyd", "Pink Floyd's First Post",
            "Up agains teh wall", "pink floyd, psychadelic rock");
    pfExec = PostModel.newPost(davidGilmour, pinkFloyd.getExec(), "Pink Floyd Execs", "Pink Floyd's First Post",
            "Wish you were here!", "pink floyd, psychadelic rock");
    ls = PostModel.newPost(davidGilmour, leadSingers, "Lead Singers", "Lead Singers's First Post",
            "haha. we rock", "lead singers, rock");
    sw = PostModel.newPost(rogerWaters, songwriters, "Songwriters", "Songwriters's First Post",
            "they write, they write yay", "writing, music");
    bto = PostModel.newPost(randyBachman, bachmanTurnerOverdrive, "Bachman Turner Overdrive",
            "BTO's First Post", "you ain't seen nothing yet", "bto, canada");

    System.out.println("Fudging post dates...");
    Criteria postCrit = HibernateUtil.currentSession().createCriteria(PostModel.class);
    List<PostModel> posts = new SafeHibList<PostModel>(postCrit).list();

    for (PostModel p : posts) {
        c = GregorianCalendar.getInstance();
        c.add(Calendar.DATE, (int) (Math.random() * (-90)));
        p.setDate(c.getTime());
    }

    System.out.println("Populating Replies...");
    gw.reply(randyBachman, "GW Reply", "");
    gwExec.reply(randyBachman, "GW Exec Reply", "");
    pf.reply(davidGilmour, "PF Reply", "");
    pfExec.reply(davidGilmour, "PF Exec Reply", "");
    ls.reply(davidGilmour, "Lead Singers Reply", "");
    sw.reply(rogerWaters, "Writers Reply", "");
    bto.reply(randyBachman, "BTO Reply", "");

    System.out.println("Fudging reply dates...");
    Criteria replyCrit = HibernateUtil.currentSession().createCriteria(PostModel.class);
    replyCrit.add(Restrictions.isNotNull("parent"));
    List<PostModel> replies = new SafeHibList<PostModel>(replyCrit).list();

    for (PostModel r : replies) {
        Date now = new Date();
        c.setTime(r.getParent().getDate());
        Date before = c.getTime();

        c = GregorianCalendar.getInstance();
        c.add(Calendar.DATE,
                (int) (Math.random() * (before.getTime() - now.getTime()) / (1000 * 60 * 60 * 24)));
        r.setDate(c.getTime());
    }

    System.out.println("Populating events...");

    //Creating Events
    c = GregorianCalendar.getInstance();
    c.set(2007, 6, 31, 10, 30);
    Date startDate = c.getTime();
    c.add(Calendar.DATE, 2);
    Date endDate = c.getTime();

    rehearsal = EventModel.newEvent("Band Rehearsal", startDate, endDate, "Bachman's Basement", "Garageband",
            theGuessWho, false, "");

    c.set(2007, 7, 5, 5, 23);
    startDate = c.getTime();
    c.add(Calendar.WEEK_OF_YEAR, 1);
    endDate = c.getTime();
    onTheRoad = EventModel.newEvent("Driving", startDate, endDate, "The i95", "On the road again...",
            theGuessWho, false, "");

    c.set(2007, 7, 15, 12, 49);
    startDate = c.getTime();
    c.add(Calendar.HOUR, 7);
    endDate = c.getTime();
    setup = EventModel.newEvent("Setup", startDate, endDate, "Outside Paramount", "roadies!", theGuessWho,
            false, "");

    c.set(2007, 7, 15, 20, 35);
    startDate = c.getTime();
    c.add(Calendar.HOUR, 4);
    endDate = c.getTime();
    liveAtParamount = EventModel.newEvent("Concert: Live at the Paramount", startDate, endDate,
            "Paramount Stage", "Yeah for concerts", theGuessWho, false, "");

}

From source file:ca.myewb.controllers.mailing.AvailableLists.java

License:Open Source License

public void handle(Context ctx) throws Exception {
    List<GroupModel> currentLists = currentUser.getGroups();
    GroupChapterModel chapter = currentUser.getChapter();
    Criteria crit = null;/*from   w w w  . j  a  v a  2s.  co  m*/
    Hashtable<String, List<GroupModel>> hash = new Hashtable<String, List<GroupModel>>();
    List<String> names = new Vector<String>();

    //chapter lists
    crit = hibernateSession.createCriteria(GroupChapterModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("visible", new Boolean(true)));
    crit.addOrder(Order.asc("name"));

    List<GroupModel> chapterLists = (new SafeHibList<GroupModel>(crit)).list();
    chapterLists.removeAll(currentLists);

    //general public lists
    crit = hibernateSession.createCriteria(GroupModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("admin", new Boolean(false)));
    crit.add(Restrictions.eq("visible", new Boolean(true)));

    crit.add(Restrictions.isNull("parent"));
    crit.add(Restrictions.eq("public", new Boolean(true)));

    List<GroupModel> generalPublicLists = (new SafeHibList<GroupModel>(crit)).list();
    generalPublicLists.removeAll(currentLists);
    generalPublicLists.removeAll(chapterLists);

    log.debug("Populating available lists:");

    if (currentUser.getUsername().equals("guest")) {
        generalPublicLists.add(0, Helpers.getGroup("Org"));
        log.debug("Global list, for the guest");
    }

    if (!generalPublicLists.isEmpty()) {
        hash.put("General Public Lists", generalPublicLists);
        names.add("General Public Lists");
        log.debug("General public lists");
    }

    if (currentUser.isMember("Exec")) {
        // admin level lists
        List<GroupModel> adminLists = Helpers.getNationalRepLists(true, true);
        adminLists.add(0, Helpers.getGroup("ProChaptersExec"));
        adminLists.add(0, Helpers.getGroup("UniChaptersExec"));
        adminLists.add(0, Helpers.getGroup("Exec"));

        adminLists.removeAll(currentLists);

        if (!adminLists.isEmpty()) {
            hash.put("Exec and Natl Rep Lists", adminLists);
            names.add("Exec and Natl Rep Lists");
        }
    }

    if (currentUser.isAdmin()) {

        {
            //general private lists
            crit = hibernateSession.createCriteria(GroupModel.class);
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            crit.add(Restrictions.eq("admin", new Boolean(false)));
            crit.add(Restrictions.eq("visible", new Boolean(true)));

            crit.add(Restrictions.isNull("parent"));
            crit.add(Restrictions.eq("public", new Boolean(false)));

            List<GroupModel> generalPrivateLists = (new SafeHibList<GroupModel>(crit)).list();
            generalPrivateLists.removeAll(currentLists);

            if (!generalPrivateLists.isEmpty()) {
                hash.put("General Private Lists", generalPrivateLists);
                names.add("General Private Lists");
                log.debug("General private lists");
            } else {
                log.debug("General private lists was empty");
            }
        }

        {
            //all chapter public lists
            crit = hibernateSession.createCriteria(GroupModel.class);
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            crit.add(Restrictions.eq("admin", new Boolean(false)));
            crit.add(Restrictions.eq("visible", new Boolean(true)));

            crit.add(Restrictions.isNotNull("parent"));
            crit.add(Restrictions.eq("public", new Boolean(true)));
            crit.addOrder(Order.asc("parent"));

            List<GroupModel> chapterPublicLists = (new SafeHibList<GroupModel>(crit)).list();
            chapterPublicLists.removeAll(currentLists);

            if (!chapterPublicLists.isEmpty()) {
                hash.put("Chapter Public Lists (any chapter)", chapterPublicLists);
                names.add("Chapter Public Lists (any chapter)");
                log.debug("Chapter public lists for admin");
            } else {
                log.debug("Chapter public lists for admin; empty");
            }
        }

        {
            //all chapter private lists
            crit = hibernateSession.createCriteria(GroupModel.class);
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            crit.add(Restrictions.eq("admin", new Boolean(false)));
            crit.add(Restrictions.eq("visible", new Boolean(true)));

            crit.add(Restrictions.isNotNull("parent"));
            crit.add(Restrictions.eq("public", new Boolean(false)));
            crit.addOrder(Order.asc("parent"));

            List<GroupModel> chapterPrivateLists = (new SafeHibList<GroupModel>(crit)).list();
            chapterPrivateLists.removeAll(currentLists);

            if (!chapterPrivateLists.isEmpty()) {
                hash.put("Chapter Private Lists (any chapter)", chapterPrivateLists);
                names.add("Chapter Private Lists (any chapter)");
                log.debug("Chapter private lists, admin");
            } else {
                log.debug("Chapter private lists, admin, empty");
            }
        }
    } else {
        if (chapter != null) {
            //chapter public lists
            crit = hibernateSession.createCriteria(GroupModel.class);
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            crit.add(Restrictions.eq("admin", new Boolean(false)));
            crit.add(Restrictions.eq("visible", new Boolean(true)));

            crit.add(Restrictions.eq("parent", chapter));
            crit.add(Restrictions.eq("public", new Boolean(true)));

            List<GroupModel> chapterPublicLists = (new SafeHibList<GroupModel>(crit)).list();
            chapterPublicLists.removeAll(currentLists);

            if (!chapterPublicLists.isEmpty()) {
                hash.put("Chapter Public Lists", chapterPublicLists);
                names.add("Chapter Public Lists");
                log.debug("Chapter public lists");
            } else {
                log.debug("Chapter public lists was empty");
            }

            if (currentUser.isLeader(chapter, false)) {
                //own chapter's private lists
                crit = hibernateSession.createCriteria(GroupModel.class);
                crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
                crit.add(Restrictions.eq("admin", new Boolean(false)));
                crit.add(Restrictions.eq("visible", new Boolean(true)));

                crit.add(Restrictions.eq("parent", chapter));
                crit.add(Restrictions.eq("public", new Boolean(false)));

                List<GroupModel> chapterPrivateLists = (new SafeHibList<GroupModel>(crit)).list();
                chapterPrivateLists.removeAll(currentLists);

                if (!chapterPrivateLists.isEmpty()) {
                    hash.put("Chapter Private Lists", chapterPrivateLists);
                    names.add("Chapter Private Lists");
                    log.debug("Chapter private lists");
                } else {
                    log.debug("Chapter private lists was empty");
                }
            }
        }
    }

    if (!chapterLists.isEmpty()) {
        hash.put("Chapter Lists", chapterLists);
        names.add("Chapter Lists");
        log.debug("Chapter lists");
    }

    // Stick it all in the context
    ctx.put("names", names);
    ctx.put("names2", names);
    ctx.put("hash", hash);
}

From source file:ca.myewb.frame.Cron.java

License:Open Source License

private static void doTwoWeekWarnings(Logger log, Session session) throws Exception {
    // 2-week warning
    log.info("----- 14-day warning");

    Calendar calendar = Calendar.getInstance();
    Criteria crit = session.createCriteria(UserModel.class);
    crit.add(Restrictions.isNotNull("email"));
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    calendar.add(Calendar.DAY_OF_YEAR, 14);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    crit.add(Restrictions.eq("expiry", calendar.getTime()));

    Iterator it = crit.list().iterator();

    while (it.hasNext()) {
        UserModel u = (UserModel) it.next();
        log.info(u.getFirstname() + " " + u.getLastname() + ": " + u.getEmail());

        VelocityContext mailCtx = new VelocityContext();
        mailCtx.put("helpers", new Helpers());
        mailCtx.put("name", u.getFirstname());
        mailCtx.put("numdays", "14 days");
        mailCtx.put("expiry", formatter.format(calendar.getTime()));

        Template template = Velocity.getTemplate("emails/expirywarning.vm");
        StringWriter writer = new StringWriter();
        template.merge(mailCtx, writer);

        EmailModel.sendEmail(u.getEmail(), writer.toString());
    }/*from  www.  j  av  a 2 s  .  c  o m*/
}

From source file:ca.myewb.frame.Cron.java

License:Open Source License

private static void doOneWeekWarnings(Logger log, Session session) throws Exception {
    // 1-week warning
    log.info("----- 7-day warning");

    Calendar calendar = Calendar.getInstance();
    Criteria crit = session.createCriteria(UserModel.class);
    crit.add(Restrictions.isNotNull("email"));
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    calendar.add(Calendar.DAY_OF_YEAR, 7);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    crit.add(Restrictions.eq("expiry", calendar.getTime()));

    Iterator it = crit.list().iterator();

    while (it.hasNext()) {
        UserModel u = (UserModel) it.next();
        log.info(u.getFirstname() + " " + u.getLastname() + ": " + u.getEmail());

        VelocityContext mailCtx = new VelocityContext();
        mailCtx.put("helpers", new Helpers());
        mailCtx.put("name", u.getFirstname());
        mailCtx.put("numdays", "7 days");
        mailCtx.put("expiry", formatter.format(calendar.getTime()));

        Template template = Velocity.getTemplate("emails/expirywarning.vm");
        StringWriter writer = new StringWriter();
        template.merge(mailCtx, writer);

        EmailModel.sendEmail(u.getEmail(), writer.toString());
    }// w  w  w .ja  v  a  2s .  c o m
}

From source file:ca.myewb.frame.Cron.java

License:Open Source License

private static void doOneDayWarnings(Logger log, Session session) throws Exception {
    // 1-day warning
    log.info("----- 1-day warning");

    Calendar calendar = Calendar.getInstance();
    Criteria crit = session.createCriteria(UserModel.class);
    crit.add(Restrictions.isNotNull("email"));
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    crit.add(Restrictions.eq("expiry", calendar.getTime()));

    Iterator it = crit.list().iterator();

    while (it.hasNext()) {
        UserModel u = (UserModel) it.next();
        log.info(u.getFirstname() + " " + u.getLastname() + ": " + u.getEmail());

        VelocityContext mailCtx = new VelocityContext();
        mailCtx.put("helpers", new Helpers());
        mailCtx.put("name", u.getFirstname());
        mailCtx.put("numdays", "1 day");
        mailCtx.put("expiry", formatter.format(calendar.getTime()));

        Template template = Velocity.getTemplate("emails/expirywarning.vm");
        StringWriter writer = new StringWriter();
        template.merge(mailCtx, writer);

        EmailModel.sendEmail(u.getEmail(), writer.toString());
    }//w ww.j a  v  a2 s  . co  m
}

From source file:ca.myewb.frame.Cron.java

License:Open Source License

private static void doMembershipExpiry(Logger log, Session session) throws Exception {
    // expiry//from w  ww .  jav a 2  s  .  c o  m
    log.info("----- Expiry");

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    Criteria crit = session.createCriteria(UserModel.class);
    crit.add(Restrictions.isNotNull("email"));
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.le("expiry", calendar.getTime()));

    Iterator it = crit.list().iterator();

    while (it.hasNext()) {
        UserModel u = (UserModel) it.next();
        log.info(u.getFirstname() + " " + u.getLastname() + ": " + u.getEmail());

        u.expire();

        VelocityContext mailCtx = new VelocityContext();
        mailCtx.put("helpers", new Helpers());
        mailCtx.put("name", u.getFirstname());

        Template template = Velocity.getTemplate("emails/expiry.vm");
        StringWriter writer = new StringWriter();
        template.merge(mailCtx, writer);

        EmailModel.sendEmail(u.getEmail(), writer.toString());
    }
}

From source file:ca.myewb.logic.PlacementLogic.java

License:Open Source License

public static List<PlacementModel> getActivePlacements() {
    Criteria crit = HibernateUtil.currentSession().createCriteria(PlacementModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("active", new Boolean(true)));
    crit.add(Restrictions.eq("deleted", new Boolean(false)));
    crit.add(Restrictions.isNotNull("ov"));
    crit.addOrder(Order.asc("name"));
    return (new SafeHibList<PlacementModel>(crit)).list();
}

From source file:ca.myewb.logic.PlacementLogic.java

License:Open Source License

public static Object getInactivePlacements() {
    Criteria crit = HibernateUtil.currentSession().createCriteria(PlacementModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("active", new Boolean(false)));
    crit.add(Restrictions.eq("deleted", new Boolean(false)));
    crit.add(Restrictions.isNotNull("ov"));
    crit.addOrder(Order.asc("name"));
    return (new SafeHibList<PlacementModel>(crit)).list();
}

From source file:cms.service.account.MemberManager.java

License:Apache License

@Transactional
public List<Member> listPhoneUser() {
    List<Member> memberList = memberDao.find(Restrictions.isNotNull("phone"));
    return memberList;
}

From source file:com.abiquo.server.core.appslibrary.VirtualMachineTemplateDAO.java

License:Open Source License

private static Criterion statefulVirtualMachineTemplate(final StatefulInclusion stateful,
        final Criteria criteria) {
    Criterion cri = Restrictions.eq(VirtualMachineTemplate.STATEFUL_PROPERTY, true);

    switch (stateful) {
    case ALL:/*from   w w w  . j  a va 2s.co  m*/
        Restrictions.and(cri, Restrictions.isNotNull(VirtualMachineTemplate.VOLUME_PROPERTY));
        break;
    case USED:
        // use function criteriaWithStatefulNavigation before
        return Restrictions.and(cri,
                Restrictions.eq("vl." + VolumeManagement.STATE_PROPERTY, VolumeState.ATTACHED));
    case NOTUSED:
        // use function criteriaWithStatefulNavigation before
        return Restrictions.and(cri,
                Restrictions.eq("vl." + VolumeManagement.STATE_PROPERTY, VolumeState.DETACHED));
    }
    return cri;
}