1 package org.kuali.ole.docstore.document.jcr;
2
3 import org.apache.commons.io.FileUtils;
4 import org.kuali.ole.docstore.OleDocStoreException;
5 import org.kuali.ole.docstore.model.enums.DocFormat;
6 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
7 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
8 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
9 import org.kuali.ole.docstore.repository.WorkLicenseNodeManager;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import javax.jcr.Node;
14 import java.io.File;
15 import java.io.FileOutputStream;
16 import java.util.Calendar;
17 import java.util.List;
18
19
20
21
22
23
24
25
26
27 public class JcrWorkLicenseDocumentManager
28 extends JcrAbstractDocumentManager {
29 private static JcrWorkLicenseDocumentManager ourInstance = null;
30 private static final Logger LOG = LoggerFactory
31 .getLogger(JcrWorkLicenseDocumentManager.class);
32
33 public static JcrWorkLicenseDocumentManager getInstance() {
34 if (null == ourInstance) {
35 ourInstance = new JcrWorkLicenseDocumentManager();
36 }
37 return ourInstance;
38 }
39
40 protected JcrWorkLicenseDocumentManager() {
41 super();
42 this.nodeManager = WorkLicenseNodeManager.getInstance();
43 }
44
45 @Override
46 public boolean isVersioningEnabled() {
47 return true;
48 }
49
50 @Override
51 protected void modifyAdditionalAttributes(RequestDocument requestDocument, Node node) {
52 if (!DocFormat.ONIXPL.isEqualTo(requestDocument.getFormat())) {
53 AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
54 String docName = new File(requestDocument.getDocumentName()).getName();
55 additionalAttributes.setAttribute("dateEntered", Calendar.getInstance().toString());
56 additionalAttributes.setAttribute("fileName", docName);
57 additionalAttributes.setAttribute("owner", requestDocument.getUser());
58 }
59 }
60
61 @Override
62 protected byte[] convertContentToBytes(RequestDocument reqDoc) throws OleDocStoreException {
63 String charset = "UTF-8";
64 byte[] documentBytes = new byte[0];
65 try {
66 if (reqDoc.getDocumentName() != null && reqDoc.getDocumentName().trim().length() != 0) {
67 documentBytes = FileUtils.readFileToByteArray(new File(reqDoc.getDocumentName()));
68 } else if (reqDoc.getContent().getContent() != null) {
69 documentBytes = reqDoc.getContent().getContent().getBytes(charset);
70 }
71 } catch (Exception e) {
72 LOG.info("Failed to convert input string to byte[] with charset " + e);
73 throw new OleDocStoreException(e.getMessage());
74 }
75 return documentBytes;
76 }
77
78 @Override
79 protected String checkOutContent(Node nodeByUUID, String format, String user) throws OleDocStoreException {
80 String outputFilePath = null;
81 if (!user.equalsIgnoreCase("RestWebUser") && (format.equals(DocFormat.PDF.getCode()) || format
82 .equals(DocFormat.DOC.getCode()))) {
83 try {
84 FileOutputStream fos = null;
85 File output = File.createTempFile("checkout.", ".output");
86 FileUtils.deleteQuietly(output);
87 output.mkdirs();
88
89 String fileNameAndExtension = null;
90 fileNameAndExtension = nodeByUUID.getIdentifier() + format;
91 outputFilePath = output.getAbsolutePath() + File.separator + fileNameAndExtension;
92 byte[] binaryContent = nodeManager.getBinaryData(nodeByUUID);
93 fos = new FileOutputStream(outputFilePath);
94 fos.write(binaryContent);
95 fos.close();
96 LOG.info("path-->" + output.getAbsolutePath());
97 } catch (Exception e) {
98 LOG.info("Checking out file from JCR" + e.getMessage(), e);
99 throw new OleDocStoreException(e);
100 }
101 } else {
102 try {
103 outputFilePath = nodeManager.getData(nodeByUUID);
104 } catch (Exception e) {
105 LOG.error(e.getMessage(), e);
106 throw new OleDocStoreException(e.getMessage(), e);
107 }
108 }
109
110 return outputFilePath;
111 }
112
113 @Override
114 public ResponseDocument delete(RequestDocument requestDocument, Object object) throws Exception {
115 return null;
116 }
117
118 @Override
119 public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
120
121 }
122
123 @Override
124 public List<ResponseDocument> deleteVerify(List<RequestDocument> requestDocument, Object object) {
125 return null;
126 }
127
128 @Override
129 public ResponseDocument deleteVerify(RequestDocument requestDocument, Object object) throws Exception {
130 return null;
131 }
132 }