Example usage for org.hibernate Query setBoolean

List of usage examples for org.hibernate Query setBoolean

Introduction

In this page you can find the example usage for org.hibernate Query setBoolean.

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setBoolean(String name, boolean val) 

Source Link

Document

Bind a named boolean-valued parameter.

Usage

From source file:br.edu.ifes.sr.dw.persistencia.CupomDaoHibernate.java

@Override
public List<Cupom> buscarPorCliente(String email) {
    session.getTransaction().begin();//from   w w w .  j a  v  a 2 s.  c o m
    String hql = "select c from Cupom c inner join c.cliente cli where cli.email = :email and c.resgatado = :resgatado";
    Query consulta = session.createQuery(hql);
    consulta.setString("email", email);
    consulta.setBoolean("resgatado", false);
    List<Cupom> cupons = (List<Cupom>) consulta.list();
    session.getTransaction().commit();
    session.close();
    return cupons;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private List<WorkflowTask> findActiveTaskInstances(final String authority, JbpmContext context) {
    Session session = context.getSession();
    Query query = session.getNamedQuery("org.alfresco.repo.workflow.findTaskInstancesByActorId");
    query.setString("actorId", authority);
    query.setBoolean("true", true);
    List<WorkflowTask> workflowTasks = getWorkflowTasks(session, query.list());
    // Do we need to clear a session here? It takes 3 seconds with 2000 workflows.
    // session.clear();
    return workflowTasks;
}

From source file:com.cloud.bridge.util.QueryHelper.java

License:Open Source License

public static void bindParameters(Query query, Object[] params) {
    int pos = 0;//  www.  j  a  v a2s  .c  om
    if (params != null && params.length > 0) {
        for (Object param : params) {
            if (param instanceof Byte)
                query.setByte(pos++, ((Byte) param).byteValue());
            else if (param instanceof Short)
                query.setShort(pos++, ((Short) param).shortValue());
            else if (param instanceof Integer)
                query.setInteger(pos++, ((Integer) param).intValue());
            else if (param instanceof Long)
                query.setLong(pos++, ((Long) param).longValue());
            else if (param instanceof Float)
                query.setFloat(pos++, ((Float) param).floatValue());
            else if (param instanceof Double)
                query.setDouble(pos++, ((Double) param).doubleValue());
            else if (param instanceof Boolean)
                query.setBoolean(pos++, ((Boolean) param).booleanValue());
            else if (param instanceof Character)
                query.setCharacter(pos++, ((Character) param).charValue());
            else if (param instanceof Date)
                query.setDate(pos++, (Date) param);
            else if (param instanceof Calendar)
                query.setCalendar(pos++, (Calendar) param);
            else if (param instanceof CalendarDateParam)
                query.setCalendarDate(pos++, ((CalendarDateParam) param).dateValue());
            else if (param instanceof TimestampParam)
                query.setTimestamp(pos++, ((TimestampParam) param).timestampValue());
            else if (param instanceof TimeParam)
                query.setTime(pos++, ((TimeParam) param).timeValue());
            else if (param instanceof String)
                query.setString(pos++, (String) param);
            else if (param instanceof TextParam)
                query.setText(pos++, ((TextParam) param).textValue());
            else if (param instanceof byte[])
                query.setBinary(pos++, (byte[]) param);
            else if (param instanceof BigDecimal)
                query.setBigDecimal(pos++, (BigDecimal) param);
            else if (param instanceof BigInteger)
                query.setBigInteger(pos++, (BigInteger) param);
            else if (param instanceof Locale)
                query.setLocale(pos++, (Locale) param);
            else if (param instanceof EntityParam)
                query.setEntity(pos++, ((EntityParam) param).entityValue());
            else if (param instanceof Serializable)
                query.setSerializable(pos++, (Serializable) param);
            else
                query.setEntity(pos++, param);
        }
    }
}

From source file:com.duroty.task.IMAPServiceTask.java

License:Open Source License

/**
 * DOCUMENT ME!/*  ww w. j a v a2  s  . c o m*/
 */
private void flush() {
    setInit(true);

    SessionFactory hfactory = null;
    Session hsession = null;
    javax.mail.Session msession = null;
    Store store = null;
    Folder rootFolder = null;
    Folder folder = null;

    try {
        hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory);
        hsession = hfactory.openSession();
        msession = (javax.mail.Session) ctx.lookup(durotyMailFactory);
        store = msession.getStore("imap");

        String imapHost = msession.getProperty("mail.imap.host");

        Query query = hsession.getNamedQuery("users-mail");
        query.setBoolean("active", true);
        query.setString("role", "mail");

        ScrollableResults scroll = query.scroll();

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

            String repositoryName = user.getUseUsername();

            store.connect(imapHost, repositoryName, user.getUsePassword());

            rootFolder = store.getDefaultFolder();

            if ((rootFolder.getType() & Folder.HOLDS_FOLDERS) != 0) {
                folder = store.getFolder(this.imapInbox);

                Message[] messages = getMimeMessages(folder, new Flags(Flags.Flag.SEEN), false);
                MailUtilities.setFolderOpenAndReady(folder, Folder.READ_WRITE);

                if ((messages != null) && (messages.length > 0)) {
                    for (int i = 0; i < messages.length; i++) {
                        if (pool.size() >= poolSize) {
                            DLog.log(DLog.WARN, this.getClass(), "PoolSize " + pool.size());

                            break;
                        }

                        MimeMessage mime = null;

                        if (messages[i] instanceof MimeMessage) {
                            mime = new MimeMessage((MimeMessage) messages[i]);

                            String messageName = null;

                            if (folder instanceof IMAPFolder) {
                                messageName = String.valueOf(((IMAPFolder) folder).getUID(messages[i]));
                            } else {
                                messageName = String.valueOf(messages[i].getMessageNumber());
                            }

                            messageName += this.imapInbox;

                            boolean existMessage = existMessageName(hfactory.openSession(), user, messageName);

                            if (existMessage) {
                                messages[i].setFlag(Flags.Flag.SEEN, true);
                            } else {
                                String key = messageName + "--" + repositoryName;

                                if (!poolContains(key)) {
                                    addPool(key);

                                    if (!isSpam(user, mime)) {
                                        Mailet mailet = new Mailet(this, messageName, repositoryName, mime);

                                        Thread thread = new Thread(mailet, key);
                                        thread.start();

                                        //cal controlar si el borrem
                                        messages[i].setFlag(Flags.Flag.SEEN, true);
                                    } else {
                                        messages[i].setFlag(Flags.Flag.SEEN, true);
                                        messages[i].setFlag(Flags.Flag.DELETED, true);
                                    }
                                }
                            }

                            Thread.sleep(100);
                        }
                    }
                }

                MailUtilities.setFolderClose(folder, true);
                folder = null;
                messages = null;

                folder = store.getFolder(this.imapSent);
                messages = getMimeMessages(folder, new Flags(Flags.Flag.SEEN), false);
                MailUtilities.setFolderOpenAndReady(folder, Folder.READ_WRITE);

                if (!folder.exists()) {
                    folder.create(Folder.HOLDS_MESSAGES);
                }

                if (!folder.isSubscribed()) {
                    folder.setSubscribed(true);
                }

                if ((messages != null) && (messages.length > 0)) {
                    for (int i = 0; i < messages.length; i++) {
                        if (pool.size() >= poolSize) {
                            DLog.log(DLog.WARN, this.getClass(), "PoolSize " + pool.size());

                            break;
                        }

                        MimeMessage mime = null;

                        if (messages[i] instanceof MimeMessage) {
                            mime = new MimeMessage((MimeMessage) messages[i]);

                            mime.addHeader("X-DBox", "SENT");

                            String messageName = null;

                            if (folder instanceof IMAPFolder) {
                                messageName = String.valueOf(((IMAPFolder) folder).getUID(messages[i]));
                            } else {
                                messageName = String.valueOf(messages[i].getMessageNumber());
                            }

                            messageName += this.imapSent;

                            boolean existMessage = existMessageName(hfactory.openSession(), user, messageName);

                            if (existMessage) {
                                messages[i].setFlag(Flags.Flag.SEEN, true);
                            } else {
                                String key = messageName + "--" + repositoryName;

                                if (!poolContains(key)) {
                                    addPool(key);

                                    if (!isSpam(user, mime)) {
                                        Mailet mailet = new Mailet(this, messageName, repositoryName, mime);

                                        Thread thread = new Thread(mailet, key);
                                        thread.start();

                                        //cal controlar si el borrem
                                        messages[i].setFlag(Flags.Flag.SEEN, true);
                                    } else {
                                        messages[i].setFlag(Flags.Flag.SEEN, true);
                                        messages[i].setFlag(Flags.Flag.DELETED, true);
                                    }
                                }
                            }

                            Thread.sleep(100);
                        }
                    }
                }

                MailUtilities.setFolderClose(folder, true);
                folder = null;
                messages = null;

                folder = store.getFolder(this.imapDraft);
                messages = getMimeMessages(folder, new Flags(Flags.Flag.SEEN), false);
                MailUtilities.setFolderOpenAndReady(folder, Folder.READ_WRITE);

                if (!folder.exists()) {
                    folder.create(Folder.HOLDS_MESSAGES);
                }

                if (!folder.isSubscribed()) {
                    folder.setSubscribed(true);
                }

                if ((messages != null) && (messages.length > 0)) {
                    for (int i = 0; i < messages.length; i++) {
                        if (pool.size() >= poolSize) {
                            DLog.log(DLog.WARN, this.getClass(), "PoolSize " + pool.size());

                            break;
                        }

                        MimeMessage mime = null;

                        if (messages[i] instanceof MimeMessage) {
                            mime = new MimeMessage((MimeMessage) messages[i]);

                            mime.addHeader("X-DBox", "DRAFT");

                            String messageName = null;

                            if (folder instanceof IMAPFolder) {
                                messageName = String.valueOf(((IMAPFolder) folder).getUID(messages[i]));
                            } else {
                                messageName = String.valueOf(messages[i].getMessageNumber());
                            }

                            messageName += this.imapDraft;

                            boolean existMessage = existMessageName(hfactory.openSession(), user, messageName);

                            if (existMessage) {
                                messages[i].setFlag(Flags.Flag.SEEN, true);
                            } else {
                                String key = messageName + "--" + repositoryName;

                                if (!poolContains(key)) {
                                    addPool(key);

                                    if (!isSpam(user, mime)) {
                                        Mailet mailet = new Mailet(this, messageName, repositoryName, mime);

                                        Thread thread = new Thread(mailet, key);
                                        thread.start();

                                        //cal controlar si el borrem
                                        messages[i].setFlag(Flags.Flag.SEEN, true);
                                    } else {
                                        messages[i].setFlag(Flags.Flag.SEEN, true);
                                        messages[i].setFlag(Flags.Flag.DELETED, true);
                                    }
                                }
                            }

                            Thread.sleep(100);
                        }
                    }
                }

                MailUtilities.setFolderClose(folder, true);
                folder = null;
            }

            MailUtilities.setFolderClose(rootFolder, true);
            MailUtilities.setStoreClose(store);
        }
    } catch (Exception e) {
        System.gc();
        pool.clear();

        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        DLog.log(DLog.ERROR, this.getClass(), writer.toString());
    } catch (OutOfMemoryError e) {
        System.gc();
        pool.clear();

        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        DLog.log(DLog.ERROR, this.getClass(), writer.toString());
    } catch (Throwable e) {
        System.gc();
        pool.clear();

        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        DLog.log(DLog.ERROR, this.getClass(), writer.toString());
    } finally {
        System.gc();

        GeneralOperations.closeMailFolder(folder, false);
        GeneralOperations.closeMailFolder(rootFolder, false);
        GeneralOperations.closeMailStore(store);
        GeneralOperations.closeHibernateSession(hsession);

        setInit(false);
    }
}

From source file:com.duroty.task.POP3ServiceTask.java

License:Open Source License

/**
 * DOCUMENT ME!/*ww w.ja  va 2 s  .  c o m*/
 */
private void flush() {
    setInit(true);

    SessionFactory hfactory = null;
    Session hsession = null;
    javax.mail.Session msession = null;

    try {
        hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory);
        hsession = hfactory.openSession();
        msession = (javax.mail.Session) ctx.lookup(durotyMailFactory);

        String pop3Host = msession.getProperty("mail.pop3.host");

        int port = 0;

        try {
            port = Integer.parseInt(msession.getProperty("mail.pop3.port"));
        } catch (Exception ex) {
            port = 0;
        }

        Query query = hsession.getNamedQuery("users-mail");
        query.setBoolean("active", true);
        query.setString("role", "mail");

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            POP3Client client = new POP3Client();

            try {
                if (port > 0) {
                    client.connect(pop3Host, port);
                } else {
                    client.connect(pop3Host);
                }

                client.setState(POP3Client.AUTHORIZATION_STATE);

                //client.setDefaultTimeout()
                Users user = (Users) scroll.get(0);

                String repositoryName = user.getUseUsername();

                if (client.login(repositoryName, user.getUsePassword())) {
                    POP3MessageInfo[] info = client.listUniqueIdentifiers();

                    if ((info != null) && (info.length > 0)) {
                        for (int i = 0; i < info.length; i++) {
                            if (pool.size() >= poolSize) {
                                break;
                            }

                            Reader reader = client.retrieveMessage(info[i].number);

                            boolean existMessage = existMessageName(hfactory.openSession(), user,
                                    info[i].identifier);

                            String key = info[i].identifier + "--" + repositoryName;

                            if (existMessage) {
                                client.deleteMessage(info[i].number);
                            } else {
                                if (!poolContains(key)) {
                                    addPool(key);

                                    MimeMessage mime = buildMimeMessage(info[i].identifier, reader, user);

                                    if (!isSpam(user, mime)) {
                                        client.deleteMessage(info[i].number);

                                        Mailet mailet = new Mailet(this, info[i].identifier, repositoryName,
                                                mime);

                                        Thread thread = new Thread(mailet, key);
                                        thread.start();
                                    } else {
                                        client.deleteMessage(info[i].number);
                                    }
                                }
                            }

                            Thread.sleep(100);
                        }
                    }
                } else {
                }
            } catch (Exception e) {
            } finally {
                System.gc();

                try {
                    client.logout();
                    client.disconnect();
                } catch (Exception e) {
                }
            }
        }
    } catch (Exception e) {
        System.gc();
        pool.clear();
        DLog.log(DLog.ERROR, this.getClass(), e.getMessage());
    } catch (OutOfMemoryError e) {
        System.gc();
        pool.clear();
        DLog.log(DLog.ERROR, this.getClass(), e.getMessage());
    } catch (Throwable e) {
        System.gc();
        pool.clear();
        DLog.log(DLog.ERROR, this.getClass(), e.getMessage());
    } finally {
        System.gc();

        GeneralOperations.closeHibernateSession(hsession);

        setInit(false);
    }
}

From source file:com.dz.module.contract.ContractDaoImpl.java

@Override
public int contractSearchConditionTotal(Integer contractid, String contractor, Long rent, Boolean isabandoned,
        String sort, String desc) throws HibernateException {
    Session session = null;/*from  w w  w .j  a  v a2  s.  co m*/
    Transaction tx = null;
    int c = 0;
    try {
        session = HibernateSessionFactory.getSession();
        tx = (Transaction) session.beginTransaction();
        String sql = "select count(*) from Contract";
        if (contractid == null && StringUtils.isEmpty(contractor) && rent == null) {
            sql += " where 1=1";
        } else {
            sql += " where 0=1";
        }
        if (contractid != null) {
            sql += " or contractId=" + contractid;
        }
        if (!StringUtils.isEmpty(contractor)) {
            sql += " or contractorName=" + contractor;
        }
        if (rent != null) {
            sql += " or rent=" + rent;
        }
        sql += " and isAbandoned=: isabandoned";
        if (sort.equals("0")) {
            sql += " order by contractId";
        } else if (sort.equals("1")) {
            sql += " order by rent";
        }
        if (desc.equals("0")) {
        } else if (desc.equals("1")) {
            sql += " desc";
        }
        System.out.println(sql);
        Query query = session.createQuery(sql);
        query.setBoolean("isabandoned", isabandoned);
        c = Integer.parseInt(query.uniqueResult().toString());
        query = null;
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return c;
}

From source file:com.dz.module.contract.ContractDaoImpl.java

@SuppressWarnings("unchecked")
@Override//from  w ww  .  ja  v a  2  s .com
public List<Contract> contractSearchCondition(Page page, Integer contractid, String contractor, Long rent,
        Boolean isabandoned, String sort, String desc) throws HibernateException {
    List<Contract> l = new ArrayList<Contract>();
    Session session = null;
    Transaction tx = null;
    try {
        session = HibernateSessionFactory.getSession();
        tx = (Transaction) session.beginTransaction();
        String sql = "from Contract";
        if (contractid == null && StringUtils.isEmpty(contractor) && rent == null) {
            sql += " where 1=1";
        } else {
            sql += " where 0=1";
        }
        if (contractid != null) {
            sql += " or contractId=" + contractid;
        }
        if (!StringUtils.isEmpty(contractor)) {
            sql += " or contractorName=" + contractor;
        }
        if (rent != null) {
            sql += " or rent=" + rent;
        }
        sql += " and isAbandoned=: isabandoned";
        if (sort.equals("0")) {
            sql += " order by contractId";
        } else if (sort.equals("1")) {
            sql += " order by rent";
        }
        if (desc.equals("0")) {
        } else if (desc.equals("1")) {
            sql += " desc";
        }
        System.out.println(sql);
        Query query = session.createQuery(sql);
        query.setBoolean("isabandoned", isabandoned);
        query.setMaxResults(15);
        query.setFirstResult(page.getBeginIndex());
        l = query.list();
        query = null;
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return l;
}

From source file:com.dz.module.driver.DriverDaoImpl.java

@SuppressWarnings("unchecked")
public List<Driver> driverSearchCondition(Page page, String idNum, Date beginDate, Date endDate,
        Boolean isInCar, Triplet<String, String, Object>... conditions) throws HibernateException {
    List<Driver> l = new ArrayList<Driver>();
    Session session = null;/*from w  ww  . j  a  va2 s .  co m*/
    try {
        session = HibernateSessionFactory.getSession();
        String sql = "from Driver where 1=1 ";

        if (!StringUtils.isEmpty(idNum)) {
            sql += "and idNum like :idNum ";
        }

        if (beginDate != null) {
            sql += "and applyTime>:beginDate ";
        }
        if (endDate != null) {
            sql += "and applyTime<:endDate ";
        }

        if (isInCar != null)
            sql += "and isInCar=:isInCar";

        for (Triplet<String, String, Object> condition : conditions) {
            if (condition != null) {
                sql += String.format("and %s %s :%s ", condition.getValue0(), condition.getValue1(),
                        condition.getValue0());
            }
        }

        System.out.println(sql);
        Query query = session.createQuery(sql);

        for (Triplet<String, String, Object> condition : conditions) {
            if (condition != null) {
                query.setParameter(condition.getValue0(), condition.getValue2());
            }
        }

        if (!StringUtils.isEmpty(idNum)) {
            query.setString("idNum", "%" + idNum + "%");
        }

        if (beginDate != null) {
            query.setDate("beginDate", beginDate);
        }

        if (endDate != null) {
            query.setDate("endDate", endDate);
        }

        if (isInCar != null)
            query.setBoolean("isInCar", isInCar);

        if (page != null) {
            query.setMaxResults(page.getEveryPage());
            query.setFirstResult(page.getBeginIndex());
        }

        l = query.list();
        query = null;
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return l;
}

From source file:com.dz.module.driver.DriverDaoImpl.java

public int driverSearchConditionTotal(String idNum, Date beginDate, Date endDate, Boolean isInCar,
        Triplet<String, String, Object>... conditions) throws HibernateException {
    Session session = null;/* ww w .ja v a2 s .  c  o  m*/
    int c = 0;
    try {
        session = HibernateSessionFactory.getSession();
        String sql = "select count(*) from Driver where 1=1 ";

        if (!StringUtils.isEmpty(idNum)) {
            sql += "and idNum like :idNum ";
        }

        if (beginDate != null) {
            sql += "and applyTime>:beginDate ";
        }
        if (endDate != null) {
            sql += "and applyTime<:endDate ";
        }

        if ((isInCar) != null)
            sql += "and isInCar=:isInCar";

        for (Triplet<String, String, Object> condition : conditions) {
            if (condition != null) {
                sql += String.format("and %s %s :%s ", condition.getValue0(), condition.getValue1(),
                        condition.getValue0());
            }
        }

        System.out.println(sql);
        Query query = session.createQuery(sql);
        for (Triplet<String, String, Object> condition : conditions) {
            if (condition != null) {
                query.setParameter(condition.getValue0(), condition.getValue2());
            }
        }
        if (!StringUtils.isEmpty(idNum)) {
            query.setString("idNum", "%" + idNum + "%");
        }

        if (beginDate != null) {
            query.setDate("beginDate", beginDate);
        }

        if (endDate != null) {
            query.setDate("endDate", endDate);
        }

        if (isInCar != null)
            query.setBoolean("isInCar", isInCar);

        c = Integer.parseInt(query.uniqueResult().toString());
        query = null;
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return c;
}

From source file:com.dz.module.driver.DriverDaoImpl.java

@Override
public int selectDriverInCarByConditionCount(Date beginDate, Date endDate, Vehicle vehicle, Driver driver,
        String operation, Boolean finished) {
    Session session = null;/*from w  w  w.ja  va 2  s.  co  m*/
    try {
        session = HibernateSessionFactory.getSession();
        String sql = "select count(*) from Driverincar where 1=1 ";

        if (beginDate != null) {
            sql += "and opeTime>:beginDate ";
        }
        if (endDate != null) {
            sql += "and opeTime<:endDate ";
        }

        if (!StringUtils.isEmpty(vehicle.getCarframeNum())) {
            sql += "and carframeNum like :carframeNum ";
        }

        if (!StringUtils.isEmpty(vehicle.getLicenseNum())) {
            sql += "and carframeNum in (select carframeNum from Vehicle where licenseNum like :licenseNum ) ";
        }

        if (!StringUtils.isEmpty(driver.getIdNum())) {
            sql += "and idNumber like :idNum ";
        }

        if (!StringUtils.isEmpty(operation)) {
            sql += "and operation like :operation ";
        }

        if (finished != null) {
            sql += "and finished = :finished ";
        }

        Query query = session.createQuery(sql);

        if (!StringUtils.isEmpty(vehicle.getCarframeNum())) {
            query.setString("carframeNum", "%" + vehicle.getCarframeNum() + "%");
        }

        if (!StringUtils.isEmpty(vehicle.getLicenseNum())) {
            query.setString("licenseNum", "%" + vehicle.getLicenseNum() + "%");
        }

        if (!StringUtils.isEmpty(driver.getIdNum())) {
            query.setString("idNum", "%" + driver.getIdNum() + "%");
        }

        if (beginDate != null) {
            query.setDate("beginDate", beginDate);
        }
        if (endDate != null) {
            query.setDate("endDate", endDate);
        }

        if (!StringUtils.isEmpty(operation)) {
            query.setString("operation", "%" + operation + "%");
        }

        if (finished != null) {
            query.setBoolean("finished", finished);
            sql += "and finished = :finished ";
        }

        return Integer.parseInt(query.uniqueResult().toString());
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}