Example usage for org.hibernate Session load

List of usage examples for org.hibernate Session load

Introduction

In this page you can find the example usage for org.hibernate Session load.

Prototype

void load(Object object, Serializable id);

Source Link

Document

Read the persistent state associated with the given identifier into the given transient instance.

Usage

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

License:Open Source License

/**
 * Test for locked status/* w  w  w  .jav a 2 s. com*/
 */
public NodeLock getLock(String uuid) throws PathNotFoundException, LockException, DatabaseException {
    log.debug("getLock({})", uuid);
    Session session = null;

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

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

        NodeLock nLock = nDoc.getLock();

        if (nLock == null) {
            throw new LockException("Node not locked: " + uuid);
        }

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

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

License:Open Source License

/**
 * Helps on extracting text from documents
 */// w  ww  .j  a va  2  s. co m
public void textExtractorHelper(TextExtractorWork work) throws DatabaseException, FileNotFoundException {
    log.debug("textExtractorHelper({})", work);
    Session session = null;
    Transaction tx = null;
    InputStream isContent = null;

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

        if (FsDataStore.DATASTORE_BACKEND_FS.equals(Config.REPOSITORY_DATASTORE_BACKEND)) {
            isContent = FsDataStore.read(work.getDocVerUuid());
        } else {
            NodeDocumentVersion nDocVer = (NodeDocumentVersion) session.load(NodeDocumentVersion.class,
                    work.getDocVerUuid());
            isContent = new ByteArrayInputStream(nDocVer.getContent());
        }

        NodeDocument nDoc = (NodeDocument) session.load(NodeDocument.class, work.getDocUuid());

        try {
            // AUTOMATION - PRE
            Map<String, Object> env = new HashMap<String, Object>();
            env.put(AutomationUtils.DOCUMENT_NODE, nDoc);
            AutomationManager.getInstance().fireEvent(AutomationRule.EVENT_TEXT_EXTRACTOR,
                    AutomationRule.AT_PRE, env);

            String textExtracted = RegisteredExtractors.getText(work.getDocPath(), nDoc.getMimeType(), null,
                    isContent);

            // AUTOMATION - POST
            env.put(AutomationUtils.TEXT_EXTRACTED, textExtracted);
            AutomationManager.getInstance().fireEvent(AutomationRule.EVENT_TEXT_EXTRACTOR,
                    AutomationRule.AT_POST, env);
            textExtracted = (String) env.get(AutomationUtils.TEXT_EXTRACTED);

            // Need to replace 0x00 because PostgreSQL does not accept string containing 0x00
            textExtracted = FormatUtil.fixUTF8(textExtracted);

            // Need to remove Unicode surrogate because of MySQL => SQL Error: 1366, SQLState: HY000
            textExtracted = FormatUtil.trimUnicodeSurrogates(textExtracted);

            textExtracted = textExtracted.replaceAll("[^\\w\\s\\-_]", "");

            nDoc.setText(textExtracted);

            try {
                Detector lt = DetectorFactory.create();
                lt.append(textExtracted);
                nDoc.setLanguage(lt.detect());
            } catch (LangDetectException e) {
                log.warn("Language detection problem: {}", e.getMessage(), e);
            }
        } catch (Exception e) {
            try {
                String docPath = NodeBaseDAO.getInstance().getPathFromUuid(nDoc.getUuid());
                log.warn("There was a problem extracting text from '{}': {}", docPath, e.getMessage());
                UserActivity.log(Config.SYSTEM_USER, "MISC_TEXT_EXTRACTION_FAILURE", nDoc.getUuid(), docPath,
                        e.getMessage());
            } catch (PathNotFoundException pnfe) {
                log.warn("Item not found: {}", nDoc.getUuid());
            }
        }

        nDoc.setTextExtracted(true);
        session.update(nDoc);
        HibernateUtil.commit(tx);
        log.debug("textExtractorHelper: void");
    } catch (HibernateException e) {
        HibernateUtil.rollback(tx);
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(isContent);
        HibernateUtil.close(session);
    }

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

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

License:Open Source License

/**
 * Get extracted text./* w  ww .j ava 2  s  . com*/
 */
public String getExtractedText(Session session, String uuid) throws PathNotFoundException, DatabaseException {
    // Security Check
    NodeDocument nDoc = (NodeDocument) session.load(NodeDocument.class, uuid);
    SecurityHelper.checkRead(nDoc);

    return nDoc.getText();
}

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

License:Open Source License

/**
 * Find by parent//w  w  w  .java2 s  . com
 */
@SuppressWarnings("unchecked")
public List<NodeDocumentVersion> findByParent(String docUuid) throws PathNotFoundException, DatabaseException {
    log.debug("findByParent({})", docUuid);
    String qs = "from NodeDocumentVersion ndv where ndv.parent=:parent order by ndv.created";
    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);

        Query q = session.createQuery(qs);
        q.setString("parent", docUuid);
        List<NodeDocumentVersion> ret = q.list();
        HibernateUtil.commit(tx);
        log.debug("findByParent: {}", 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

/**
 * Find current document version/*from  www .j  ava 2  s  .  co  m*/
 */
public NodeDocumentVersion findCurrentVersion(String docUuid) throws PathNotFoundException, DatabaseException {
    log.debug("findCurrentVersion({})", docUuid);
    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);

        NodeDocumentVersion currentVersion = findCurrentVersion(session, docUuid);
        HibernateUtil.commit(tx);
        log.debug("findCurrentVersion: {}", currentVersion);
        return currentVersion;
    } 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  . j a va  2 s.  c om*/
 * 
 * @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  2 s .c o  m
 */
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/*ww  w  . j a  va2s . co 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  w w  w .j  av a 2 s.com
 */
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.//from   w ww .j  a va 2 s. c  o 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);
    }
}