1 package org.kuali.ole.docstore.document;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.util.Calendar;
8
9 import org.apache.commons.io.FileUtils;
10 import org.kuali.ole.docstore.OleDocStoreException;
11 import org.kuali.ole.docstore.model.enums.DocFormat;
12 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
13 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
14 import org.kuali.ole.docstore.repository.CustomNodeManager;
15 import org.kuali.ole.docstore.repository.WorkBibNodeManager;
16 import org.kuali.ole.docstore.repository.WorkLicenseNodeManager;
17 import org.kuali.ole.pojo.OleException;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import javax.jcr.Node;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Session;
24
25
26
27
28
29
30
31
32
33 public class WorkLicenseDocumentManager
34 extends AbstractDocumentManager {
35 private static WorkLicenseDocumentManager ourInstance = null;
36 private static final Logger LOG = LoggerFactory
37 .getLogger(WorkLicenseDocumentManager.class);
38
39 public static WorkLicenseDocumentManager getInstance() {
40 if (null == ourInstance) {
41 ourInstance = new WorkLicenseDocumentManager();
42 }
43 return ourInstance;
44 }
45
46 protected WorkLicenseDocumentManager() {
47 super();
48 this.nodeManager = WorkLicenseNodeManager.getInstance();
49 }
50
51 @Override
52 public boolean isVersioningEnabled() {
53 return true;
54 }
55
56 @Override
57 protected void modifyAdditionalAttributes(AdditionalAttributes additionalAttributes,
58 RequestDocument requestDocument) {
59 if (!DocFormat.ONIXPL.isEqualTo(requestDocument.getFormat())) {
60 String docName = new File(requestDocument.getDocumentName()).getName();
61 additionalAttributes.setAttribute("dateLoaded", Calendar.getInstance().toString());
62 additionalAttributes.setAttribute("fileName", docName);
63 additionalAttributes.setAttribute("owner", requestDocument.getUser());
64 }
65 }
66
67 @Override
68 protected byte[] convertContentToBytes(RequestDocument reqDoc) throws OleDocStoreException {
69 String charset = "UTF-8";
70 byte[] documentBytes = new byte[0];
71 try {
72 if (reqDoc.getDocumentName() != null && reqDoc.getDocumentName().trim().length() != 0) {
73 documentBytes = FileUtils.readFileToByteArray(new File(reqDoc.getDocumentName()));
74 }
75 else if (reqDoc.getContent().getContent() != null) {
76 documentBytes = reqDoc.getContent().getContent().getBytes(charset);
77 }
78 }
79 catch (Exception e) {
80 LOG.info("Failed to convert input string to byte[] with charset " + e);
81 throw new OleDocStoreException(e.getMessage());
82 }
83 return documentBytes;
84 }
85
86 @Override
87 protected String checkOutContent(Node nodeByUUID, String format, String operation) throws OleDocStoreException {
88 String outputFilePath = null;
89 if (!operation.equalsIgnoreCase("RestWebUser") && (format.equals(DocFormat.PDF.getCode()) || format
90 .equals(DocFormat.DOC.getCode()))) {
91 try {
92 FileOutputStream fos = null;
93 File output = File.createTempFile("checkout.", ".output");
94 FileUtils.deleteQuietly(output);
95 output.mkdirs();
96
97 String fileNameAndExtension = null;
98 fileNameAndExtension = nodeByUUID.getIdentifier() + format;
99 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 }