001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.notes.service.impl;
017
018import java.io.BufferedOutputStream;
019import java.io.File;
020import java.io.FileOutputStream;
021import java.util.logging.Logger;
022
023import org.kuali.rice.kew.notes.Attachment;
024import org.kuali.rice.kew.notes.service.AttachmentService;
025import org.kuali.rice.kew.service.KEWServiceLocator;
026
027
028/**
029 * Implementation of the {@link AttachmentService}.
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class AttachmentServiceImpl implements AttachmentService {
034        
035        protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AttachmentServiceImpl.class);
036        
037        private static final String ATTACHMENT_PREPEND = "wf_att_";
038        
039        private String attachmentDir;
040
041        public void persistAttachedFileAndSetAttachmentBusinessObjectValue(Attachment attachment) throws Exception {
042                createStorageDirIfNecessary();
043                String uniqueId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
044                String internalFileIndicator = attachment.getFileName().replace('.', '_');
045                String fileName = ATTACHMENT_PREPEND + attachment.getNote().getDocumentId() + "_" + internalFileIndicator + "_" + uniqueId;
046                File file = File.createTempFile(fileName, null, new File(attachmentDir));
047                LOG.info("Persisting attachment at: " + file.getAbsolutePath());
048                if (!file.canWrite()) {
049                        throw new RuntimeException("Do not have permissions to write to attachment file at: " + file.getAbsolutePath());
050                }
051                FileOutputStream streamOut = null;
052        BufferedOutputStream bufferedStreamOut = null;
053        try {
054            streamOut = new FileOutputStream(file);
055            bufferedStreamOut = new BufferedOutputStream(streamOut);
056            int c;
057            while ((c = attachment.getAttachedObject().read()) != -1) 
058                {
059                    bufferedStreamOut.write(c);
060                }
061        } finally {
062                if (bufferedStreamOut != null) {
063                        bufferedStreamOut.close();
064                }
065            if (streamOut != null) {
066                streamOut.close();
067            }
068        }
069        attachment.setFileLoc(file.getAbsolutePath());
070        }
071
072        public File findAttachedFile(Attachment attachment) throws Exception {
073                return new File(attachment.getFileLoc());
074        }
075        
076        public void deleteAttachedFile(Attachment attachment) throws Exception {
077                File file = new File(attachment.getFileLoc());
078                if (! file.delete()) {
079                        LOG.error("failed to delete file " + attachment.getFileLoc());
080                }
081        }
082        
083        private void createStorageDirIfNecessary() {
084                if (attachmentDir == null) {
085                        throw new RuntimeException("Attachment Directory was not set when configuring workflow");
086                }
087                File attachDir = new File(attachmentDir);
088                if (! attachDir.exists()) {
089                        LOG.warn("No attachment directory found.  Attempting to create directory " + attachmentDir);
090                        boolean directoriesCreated = attachDir.mkdirs();
091                        if (!directoriesCreated) {
092                                throw new RuntimeException("Failed to create directory for attachments at: " + attachDir.getAbsolutePath());
093                        }
094                }
095                if (!attachDir.canWrite()) {
096                        throw new RuntimeException("Do not have permission to write to: " + attachDir.getAbsolutePath());
097                }
098        }
099
100        public String getAttachmentDir() {
101                return attachmentDir;
102        }
103
104        public void setAttachmentDir(String attachmentDir) {
105                this.attachmentDir = attachmentDir;
106        }
107        
108}