Example usage for org.hibernate Query setMaxResults

List of usage examples for org.hibernate Query setMaxResults

Introduction

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

Prototype

@Override
    Query<R> setMaxResults(int maxResult);

Source Link

Usage

From source file:com.ikon.dao.NodeDocumentVersionDAO.java

License:Open Source License

/**
 * Get document version content//w  w  w.  j av a2 s  .  c  o  m
 * 
 * @param docUuid Id of the document to get the content.
 * This is used to enable the document preview.
 */
public InputStream getCurrentContentByParent(String docUuid) throws PathNotFoundException,
        AccessDeniedException, DatabaseException, FileNotFoundException, IOException {
    log.debug("getContent({})", docUuid);
    String qs = "from NodeDocumentVersion ndv where ndv.parent=:parent and ndv.current=:current";
    Session session = null;
    Transaction tx = null;
    InputStream ret = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        // Security Check
        NodeDocument nDoc = (NodeDocument) session.load(NodeDocument.class, docUuid);
        SecurityHelper.checkRead(nDoc);

        Query q = session.createQuery(qs);
        q.setString("parent", docUuid);
        q.setBoolean("current", true);
        NodeDocumentVersion nDocVer = (NodeDocumentVersion) q.setMaxResults(1).uniqueResult();

        if (FsDataStore.DATASTORE_BACKEND_FS.equals(Config.REPOSITORY_DATASTORE_BACKEND)) {
            ret = FsDataStore.read(nDocVer.getUuid());
        } else {
            ret = new ByteArrayInputStream(nDocVer.getContent());
        }

        HibernateUtil.commit(tx);
        log.debug("getContent: {}", ret);
        return ret;
    } catch (PathNotFoundException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (DatabaseException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (HibernateException e) {
        HibernateUtil.rollback(tx);
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.ikon.dao.NodeDocumentVersionDAO.java

License:Open Source License

/**
 * Get document version content/*from   w  w w  .ja  va  2s  .  com*/
 */
public InputStream getVersionContentByParent(String docUuid, String name)
        throws PathNotFoundException, DatabaseException, FileNotFoundException, IOException {
    log.debug("getContent({})", docUuid);
    String qs = "from NodeDocumentVersion ndv where ndv.parent=:parent and ndv.name=:name";
    Session session = null;
    Transaction tx = null;
    InputStream ret = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        // Security Check
        NodeDocument nDoc = (NodeDocument) session.load(NodeDocument.class, docUuid);
        SecurityHelper.checkRead(nDoc);

        Query q = session.createQuery(qs);
        q.setString("parent", docUuid);
        q.setString("name", name);
        NodeDocumentVersion nDocVer = (NodeDocumentVersion) q.setMaxResults(1).uniqueResult();

        if (FsDataStore.DATASTORE_BACKEND_FS.equals(Config.REPOSITORY_DATASTORE_BACKEND)) {
            ret = FsDataStore.read(nDocVer.getUuid());
        } else {
            ret = new ByteArrayInputStream(nDocVer.getContent());
        }

        HibernateUtil.commit(tx);
        log.debug("getContent: {}", ret);
        return ret;
    } catch (PathNotFoundException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (DatabaseException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (HibernateException e) {
        HibernateUtil.rollback(tx);
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.ikon.dao.NodeDocumentVersionDAO.java

License:Open Source License

/**
 * Create or update dummy version/*  w w  w.  java 2  s.  c o  m*/
 */
public NodeDocumentVersion checkin(String user, String comment, String docUuid, InputStream is, long size)
        throws IOException, PathNotFoundException, AccessDeniedException, LockException, DatabaseException {
    log.debug("checkin({}, {}, {}, {}, {})", new Object[] { user, comment, docUuid, is, size });
    String qs = "from NodeDocumentVersion ndv where ndv.parent=:parent and ndv.current=:current";
    NodeDocumentVersion newDocVersion = new NodeDocumentVersion();
    Session session = null;
    Transaction tx = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        // Security Check
        NodeDocument nDoc = (NodeDocument) session.load(NodeDocument.class, docUuid);
        SecurityHelper.checkRead(nDoc);
        SecurityHelper.checkWrite(nDoc);

        // Lock Check
        LockHelper.checkWriteLock(user, nDoc);

        Query q = session.createQuery(qs);
        q.setString("parent", docUuid);
        q.setBoolean("current", true);
        NodeDocumentVersion curDocVersion = (NodeDocumentVersion) q.setMaxResults(1).uniqueResult();
        VersionNumerationAdapter verNumAdapter = VersionNumerationFactory.getVersionNumerationAdapter();
        String nextVersionNumber = verNumAdapter.getNextVersionNumber(session, nDoc, curDocVersion);

        // Make current version obsolete
        curDocVersion.setCurrent(false);
        session.update(curDocVersion);

        // New document version
        newDocVersion.setUuid(UUID.randomUUID().toString());
        newDocVersion.setParent(docUuid);
        newDocVersion.setName(nextVersionNumber);
        newDocVersion.setAuthor(user);
        newDocVersion.setComment(comment);
        newDocVersion.setCurrent(true);
        newDocVersion.setCreated(Calendar.getInstance());
        newDocVersion.setSize(size);
        newDocVersion.setMimeType(curDocVersion.getMimeType());
        newDocVersion.setPrevious(curDocVersion.getUuid());

        // Persist file in datastore
        FsDataStore.persist(newDocVersion, is);

        session.save(newDocVersion);

        // Set document checkout status to false
        nDoc.setLastModified(newDocVersion.getCreated());
        nDoc.setCheckedOut(false);

        // Text extraction
        nDoc.setText("");
        nDoc.setTextExtracted(false);

        // Remove lock
        NodeDocumentDAO.getInstance().unlock(session, user, nDoc, false);

        session.update(nDoc);
        HibernateUtil.commit(tx);

        log.debug("checkin: {}", newDocVersion);
        return newDocVersion;
    } catch (PathNotFoundException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (AccessDeniedException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (DatabaseException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (LockException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (HibernateException e) {
        HibernateUtil.rollback(tx);

        // What happen when create fails? This datastore file should be deleted!
        FsDataStore.delete(newDocVersion.getUuid());
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.ikon.dao.NodeDocumentVersionDAO.java

License:Open Source License

/**
 * Set version content./*from  ww  w  .  ja  va2 s.c  o  m*/
 */
public void setContent(String docUuid, InputStream is, long size)
        throws IOException, PathNotFoundException, AccessDeniedException, LockException, DatabaseException {
    log.debug("setContent({}, {}, {})", new Object[] { docUuid, is, size });
    String qs = "from NodeDocumentVersion ndv where ndv.parent=:parent and ndv.current=:current";
    Session session = null;
    Transaction tx = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        // Security Check
        NodeDocument nDoc = (NodeDocument) session.load(NodeDocument.class, docUuid);
        SecurityHelper.checkRead(nDoc);
        SecurityHelper.checkWrite(nDoc);

        // Lock Check
        LockHelper.checkWriteLock(nDoc);

        Query q = session.createQuery(qs);
        q.setString("parent", docUuid);
        q.setBoolean("current", true);

        // Text extraction
        nDoc.setText("");
        nDoc.setTextExtracted(false);
        session.update(nDoc);

        // Update version content
        NodeDocumentVersion curDocVersion = (NodeDocumentVersion) q.setMaxResults(1).uniqueResult();
        curDocVersion.setText("");
        curDocVersion.setSize(size);
        session.update(curDocVersion);

        // Persist file in datastore
        FsDataStore.persist(curDocVersion, is);

        HibernateUtil.commit(tx);
        log.debug("setContent: void");
    } catch (PathNotFoundException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (AccessDeniedException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (DatabaseException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (LockException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (HibernateException e) {
        HibernateUtil.rollback(tx);
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.ikon.dao.NodeDocumentVersionDAO.java

License:Open Source License

/**
 * Set a document version as current.//  w w w.  j  a  v  a  2s  . co  m
 */
public void restoreVersion(String docUuid, String versionId)
        throws PathNotFoundException, AccessDeniedException, LockException, DatabaseException {
    log.debug("restoreVersion({}, {})", new Object[] { docUuid, versionId });
    String qsCurrent = "from NodeDocumentVersion ndv where ndv.parent=:parent and ndv.current=:current";
    String qsName = "from NodeDocumentVersion ndv where ndv.parent=:parent and ndv.name=:name";
    Session session = null;
    Transaction tx = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        // Security Check
        NodeDocument nDoc = (NodeDocument) session.load(NodeDocument.class, docUuid);
        SecurityHelper.checkRead(nDoc);
        SecurityHelper.checkWrite(nDoc);

        // Lock Check
        LockHelper.checkWriteLock(nDoc);

        Query qCurrent = session.createQuery(qsCurrent);
        qCurrent.setString("parent", docUuid);
        qCurrent.setBoolean("current", true);

        Query qName = session.createQuery(qsName);
        qName.setString("parent", docUuid);
        qName.setString("name", versionId);

        // Update current version
        NodeDocumentVersion curDocVersion = (NodeDocumentVersion) qCurrent.setMaxResults(1).uniqueResult();
        NodeDocumentVersion namDocVersion = (NodeDocumentVersion) qName.setMaxResults(1).uniqueResult();
        curDocVersion.setCurrent(false);
        namDocVersion.setCurrent(true);
        session.update(namDocVersion);
        session.update(curDocVersion);

        // Text extraction
        nDoc.setText(namDocVersion.getText());
        session.update(nDoc);

        HibernateUtil.commit(tx);
        log.debug("restoreVersion: void");
    } catch (PathNotFoundException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (AccessDeniedException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (DatabaseException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (LockException e) {
        HibernateUtil.rollback(tx);
        throw e;
    } catch (HibernateException e) {
        HibernateUtil.rollback(tx);
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.ikon.dao.NodeFolderDAO.java

License:Open Source License

/**
 * Find by pk//  w w w .  j a  v a2s  .  com
 */
public NodeFolder findByPk(String uuid) throws PathNotFoundException, DatabaseException {
    log.debug("findByPk({})", uuid);
    String qs = "from NodeFolder nf where nf.uuid=:uuid";
    Session session = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Query q = session.createQuery(qs);
        q.setString("uuid", uuid);
        NodeFolder nFld = (NodeFolder) q.setMaxResults(1).uniqueResult();

        if (nFld == null) {
            throw new PathNotFoundException(uuid);
        }

        // Security Check
        SecurityHelper.checkRead(nFld);

        initialize(nFld);
        log.debug("findByPk: {}", nFld);
        return nFld;
    } catch (HibernateException e) {
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.ikon.dao.NodeMailDAO.java

License:Open Source License

/**
 * Find by path//from w ww . ja  va2  s .c o  m
 */
public NodeMail findByPk(String uuid) throws PathNotFoundException, DatabaseException {
    log.debug("findByPk({})", uuid);
    String qs = "from NodeMail nm where nm.uuid=:uuid";
    Session session = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Query q = session.createQuery(qs);
        q.setString("uuid", uuid);
        NodeMail nMail = (NodeMail) q.setMaxResults(1).uniqueResult();

        if (nMail == null) {
            throw new PathNotFoundException(uuid);
        }

        // Security Check
        SecurityHelper.checkRead(nMail);

        initialize(nMail);
        log.debug("findByPk: {}", nMail);
        return nMail;
    } catch (HibernateException e) {
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.ikon.dao.NodeNoteDAO.java

License:Open Source License

/**
 * Get parent node/*  w  w  w. ja v a 2s.  c om*/
 */
private NodeBase getParentNode(Session session, String uuid) throws HibernateException {
    log.debug("getParentNode({}, {})", session, uuid);
    String qs = "select nn.parent from NodeNote nn where nn.uuid=:uuid";
    Query q = session.createQuery(qs);
    q.setString("uuid", uuid);
    String parentUuid = (String) q.setMaxResults(1).uniqueResult();
    NodeBase parentNode = (NodeBase) session.load(NodeBase.class, parentUuid);
    log.debug("getParentNode: {}", parentNode);
    return parentNode;
}

From source file:com.ikon.dao.OmrDAO.java

License:Open Source License

/**
 * Update template//from   w  w  w. j a  v a  2s .  co m
 */
public void updateTemplate(Omr om) throws DatabaseException {
    log.debug("updateTemplate({})", om);
    String qs = "select om.templateFileContent, om.templateFileName, templateFileMime, "
            + "om.ascFileContent, om.ascFileName, ascFileMime, "
            + "om.configFileContent, om.configFileName, configFileMime, "
            + "om.fieldsFileContent, om.fieldsFileName, fieldsFileMime " + "from Omr om where om.id=:id";
    Session session = null;
    Transaction tx = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        Query q = session.createQuery(qs);
        q.setParameter("id", om.getId());
        Object[] data = (Object[]) q.setMaxResults(1).uniqueResult();

        if (om.getTemplateFileContent() == null || om.getTemplateFileContent().length == 0) {
            om.setTemplateFilContent((byte[]) data[0]);
            om.setTemplateFileName((String) data[1]);
            om.setTemplateFileMime((String) data[2]);
        }

        if (om.getAscFileContent() == null || om.getAscFileContent().length == 0) {
            om.setAscFileContent((byte[]) data[3]);
            om.setAscFileName((String) data[4]);
            om.setAscFileMime((String) data[5]);
        }

        if (om.getConfigFileContent() == null || om.getConfigFileContent().length == 0) {
            om.setConfigFileContent((byte[]) data[6]);
            om.setConfigFileName((String) data[7]);
            om.setConfigFileMime((String) data[8]);
        }

        if (om.getFieldsFileContent() == null || om.getFieldsFileContent().length == 0) {
            om.setFieldsFileContent((byte[]) data[9]);
            om.setFieldsFileName((String) data[10]);
            om.setFieldsFileMime((String) data[11]);
        }

        session.update(om);
        HibernateUtil.commit(tx);
    } catch (HibernateException e) {
        HibernateUtil.rollback(tx);
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }

    log.debug("updateTemplate: void");
}

From source file:com.ikon.dao.OmrDAO.java

License:Open Source License

/**
 * Find by pk//from  www  .  j  av a  2 s .c om
 */
public Omr findByPk(long omId) throws DatabaseException {
    log.debug("findByPk({})", omId);
    String qs = "from Omr om where om.id=:id";
    Session session = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Query q = session.createQuery(qs);
        q.setLong("id", omId);
        Omr ret = (Omr) q.setMaxResults(1).uniqueResult();
        initializeOMR(ret);
        log.debug("findByPk: {}", ret);
        return ret;
    } catch (HibernateException e) {
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}