private EcmDocument readDocumentByNode(Node node, int readType) throws EcmException { EcmDocument doc = null; try { if (node == null) { return doc; } doc = new EcmDocument(); Node resNode = node.getNode(Property.JCR_CONTENT); if (resNode == null) { return doc; } /** Load the document meta data */ DocumentMetadata meta = this.loadDocumentMetadata(resNode); meta.setFileName(node.getName()); meta.setIdentifier(node.getIdentifier()); String createdBy = node.getProperty(Property.JCR_CREATED_BY).getString(); Calendar createdDate = node.getProperty(Property.JCR_CREATED).getDate(); meta.setCreated(createdDate.getTime()); meta.setCreatedBy(createdBy); meta.setCheckedOut(node.isCheckedOut()); meta.setFullPath(node.getPath()); doc.setMetadata(meta); doc.setFileName(meta.getFileName()); doc.setParentFolder(node.getParent().getName()); /** Load the file content */ Property dataProperty = resNode.getProperty(Property.JCR_DATA); if (dataProperty != null && readType == 0) { doc.setInputStream(dataProperty.getBinary().getStream()); doc.getMetadata().setSize(dataProperty.getBinary().getSize()); } else if (dataProperty != null && readType == 1) { byte[] contents = convertToByteArray(dataProperty.getBinary()); doc.setContent(contents); doc.getMetadata().setSize(dataProperty.getBinary().getSize()); } } catch (PathNotFoundException e) { throw new EcmException( "Fail to read document from repository.", e, ErrorCodes.REPOSITROY_ERR_INVALID_PATH); } catch (RepositoryException e) { throw new EcmException( "Fail to read document from repository.", e, ErrorCodes.REPOSITROY_ERR_GENERIC); } catch (IOException e) { throw new EcmException( "Fail to read document from repository.", e, ErrorCodes.REPOSITROY_ERR_FAIL_TO_READ_CONTENT); } return doc; }
/** * Add a new version-able <code>EcmDocument</code> under the parent folder name provided * * @param pFolderPath the name of the parent folder * @param document the <code>EcmDocument</code> object * @throws EcmException if there is any error */ public void addNewVersionableDocumentToFolder(String pFolderPath, EcmDocument document) throws EcmException { logger.debug("Adding versionable document to folder:" + pFolderPath); try { Node parentNode = this.getNode(pFolderPath); Node fileNode = parentNode.addNode(document.getMetadata().getFileName(), NodeType.NT_FILE); fileNode.addMixin(NodeType.MIX_VERSIONABLE); this.createFileContentNode(fileNode, document); this.session.save(); /** Get the version manager */ VersionManager vm = this.session.getWorkspace().getVersionManager(); /** Check in the changes as a new version */ vm.checkin(fileNode.getPath()); this.session.save(); } catch (RepositoryException e) { e.printStackTrace(); } logger.debug("A document has been added to the folder:" + pFolderPath); }
/** * @param resNode * @param document * @throws ValueFormatException * @throws VersionException * @throws LockException * @throws ConstraintViolationException * @throws UnsupportedRepositoryOperationException * @throws RepositoryException */ private void updateResouceNodePropertyxxx(Node resNode, EcmDocument document) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, UnsupportedRepositoryOperationException, RepositoryException { resNode.setProperty(Property.JCR_MIMETYPE, document.getMetadata().getMimeType()); resNode.setProperty(Property.JCR_ENCODING, document.getMetadata().getEncoding()); resNode.setProperty(Property.JCR_DATA, convertToBinary(document.getContent())); /** Set last updated date time */ Calendar now = Calendar.getInstance(); now.setTime(new Date()); resNode.setProperty(Property.JCR_LAST_MODIFIED, now); /** Update the user information */ String userName = this.getSession().getUserID(); resNode.setProperty(Property.JCR_LAST_MODIFIED_BY, userName); }
/** * Load the <code>EcmDocument</code> provided by version no * * @param versionIdentifier the version no of the document * @return the generated <code>EcmDocument</code> * @throws EcmException if there is any error */ public EcmDocument loadDocumentVersion(String versionIdentifier) throws EcmException { logger.debug("Loading document version by identifier:" + versionIdentifier); EcmDocument doc = null; try { Node node = this.session.getNodeByIdentifier(versionIdentifier); if (node == null) { return doc; } doc = new EcmDocument(); Version v = (Version) node; DocumentMetadata metaData = this.loadFileVersionMetadata(v); doc.setMetadata(metaData); Node nContent = this.loadDocumentContentNode(v); Property dataProperty = nContent.getProperty(Property.JCR_DATA); if (dataProperty != null) { byte[] contents = convertToByteArray(dataProperty.getBinary()); doc.setContent(contents); } } catch (RepositoryException e) { throw new EcmException( "Fail to retrieve document version", e, ErrorCodes.REPOSITROY_ERR_GENERIC); } catch (IOException e) { throw new EcmException( "Fail to read document from repository.", e, ErrorCodes.REPOSITROY_ERR_FAIL_TO_READ_CONTENT); } logger.debug("Version loaded:" + versionIdentifier); return doc; }
@Override public void updateDocumentToFolder(String folderPath, EcmDocument document) throws EcmException { logger.debug("Updating document to folder:" + folderPath); try { Node fileNode = this.getNode(folderPath + "/" + document.getFileName()); this.updateFileContentNode(fileNode, document); this.session.save(); } catch (RepositoryException e) { throw new EcmException( "Fail to update document to the folder," + folderPath, e, ErrorCodes.REPOSITROY_ERR_GENERIC); } }
/** * Add an non version-able <code>EcmDocument</code> into the folder name specified * * @param folderPath the name of the folder * @param document the EcmDocument need to add * @throws EcmException if an error occurs. */ public void addDocumentToFolder(String folderPath, EcmDocument document) throws EcmException { logger.debug("Adding document to folder:" + folderPath); try { Node parentNode = this.getNode(folderPath); this.validateNodeInstance(parentNode); Node fileNode = parentNode.addNode(document.getMetadata().getFileName(), NodeType.NT_FILE); this.createFileContentNode(fileNode, document); this.session.save(); } catch (RepositoryException e) { throw new EcmException( "Fail to add document to the folder," + folderPath, e, ErrorCodes.REPOSITROY_ERR_GENERIC); } logger.debug("A file has been added to the folder:" + folderPath); }