001 package org.kuali.ole.docstore.document;
002
003 import java.io.File;
004 import java.io.FileNotFoundException;
005 import java.io.FileOutputStream;
006 import java.io.IOException;
007 import java.util.Calendar;
008
009 import org.apache.commons.io.FileUtils;
010 import org.kuali.ole.docstore.OleDocStoreException;
011 import org.kuali.ole.docstore.model.enums.DocFormat;
012 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
013 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
014 import org.kuali.ole.docstore.repository.CustomNodeManager;
015 import org.kuali.ole.docstore.repository.WorkBibNodeManager;
016 import org.kuali.ole.docstore.repository.WorkLicenseNodeManager;
017 import org.kuali.ole.pojo.OleException;
018 import org.slf4j.Logger;
019 import org.slf4j.LoggerFactory;
020
021 import javax.jcr.Node;
022 import javax.jcr.RepositoryException;
023 import javax.jcr.Session;
024
025 /**
026 * Implements the DocumentManager interface for [Work-License-*] documents.
027 *
028 * @version %I%, %G%
029 * @author: tirumalesh.b
030 * Date: 31/8/12 Time: 7:04 PM
031 */
032
033 public class WorkLicenseDocumentManager
034 extends AbstractDocumentManager {
035 private static WorkLicenseDocumentManager ourInstance = null;
036 private static final Logger LOG = LoggerFactory
037 .getLogger(WorkLicenseDocumentManager.class);
038
039 public static WorkLicenseDocumentManager getInstance() {
040 if (null == ourInstance) {
041 ourInstance = new WorkLicenseDocumentManager();
042 }
043 return ourInstance;
044 }
045
046 protected WorkLicenseDocumentManager() {
047 super();
048 this.nodeManager = WorkLicenseNodeManager.getInstance();
049 }
050
051 @Override
052 public boolean isVersioningEnabled() {
053 return true;
054 }
055
056 @Override
057 protected void modifyAdditionalAttributes(AdditionalAttributes additionalAttributes,
058 RequestDocument requestDocument) {
059 if (!DocFormat.ONIXPL.isEqualTo(requestDocument.getFormat())) {
060 String docName = new File(requestDocument.getDocumentName()).getName();
061 additionalAttributes.setAttribute("dateLoaded", Calendar.getInstance().toString());
062 additionalAttributes.setAttribute("fileName", docName);
063 additionalAttributes.setAttribute("owner", requestDocument.getUser());
064 }
065 }
066
067 @Override
068 protected byte[] convertContentToBytes(RequestDocument reqDoc) throws OleDocStoreException {
069 String charset = "UTF-8";
070 byte[] documentBytes = new byte[0];
071 try {
072 if (reqDoc.getDocumentName() != null && reqDoc.getDocumentName().trim().length() != 0) {
073 documentBytes = FileUtils.readFileToByteArray(new File(reqDoc.getDocumentName()));
074 }
075 else if (reqDoc.getContent().getContent() != null) {
076 documentBytes = reqDoc.getContent().getContent().getBytes(charset);
077 }
078 }
079 catch (Exception e) {
080 LOG.info("Failed to convert input string to byte[] with charset " + e);
081 throw new OleDocStoreException(e.getMessage());
082 }
083 return documentBytes;
084 }
085
086 @Override
087 protected String checkOutContent(Node nodeByUUID, String format, String operation) throws OleDocStoreException {
088 String outputFilePath = null;
089 if (!operation.equalsIgnoreCase("RestWebUser") && (format.equals(DocFormat.PDF.getCode()) || format
090 .equals(DocFormat.DOC.getCode()))) {
091 try {
092 FileOutputStream fos = null;
093 File output = File.createTempFile("checkout.", ".output");
094 FileUtils.deleteQuietly(output);
095 output.mkdirs();
096
097 String fileNameAndExtension = null;
098 fileNameAndExtension = nodeByUUID.getIdentifier() + format;
099 outputFilePath = output.getAbsolutePath() + File.separator + fileNameAndExtension;
100 byte[] binaryContent = nodeManager.getBinaryData(nodeByUUID);
101 fos = new FileOutputStream(outputFilePath);
102 fos.write(binaryContent);
103 fos.close();
104 LOG.info("path-->" + output.getAbsolutePath());
105 }
106 catch (Exception e) {
107 LOG.info("Checking out file from JCR" + e.getMessage(), e);
108 throw new OleDocStoreException(e);
109 }
110 }
111 else {
112 try {
113 outputFilePath = nodeManager.getData(nodeByUUID);
114 }
115 catch (Exception e) {
116 LOG.error(e.getMessage(), e);
117 throw new OleDocStoreException(e.getMessage(), e);
118 }
119 }
120
121 return outputFilePath;
122 }
123 }