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