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