001/**
002 * Copyright 2005-2017 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;
021
022import org.kuali.rice.kew.notes.Attachment;
023import org.kuali.rice.kew.notes.service.AttachmentService;
024import org.kuali.rice.kew.service.KEWServiceLocator;
025import org.springframework.core.io.Resource;
026import org.springframework.core.io.UrlResource;
027
028
029/**
030 * Implementation of the {@link AttachmentService}.
031 *
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class AttachmentServiceImpl implements AttachmentService {
035        
036        protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AttachmentServiceImpl.class);
037        
038        private static final String ATTACHMENT_PREPEND = "wf_att_";
039        
040        private String attachmentDir;
041        
042        public void persistAttachedFileAndSetAttachmentBusinessObjectValue(Attachment attachment) throws Exception {
043                createStorageDirIfNecessary();
044                String uniqueId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
045                String internalFileIndicator = attachment.getFileName().replace('.', '_');
046                String fileName = ATTACHMENT_PREPEND + attachment.getNote().getDocumentId() + "_" + internalFileIndicator + "_" + uniqueId;
047                File file = File.createTempFile(fileName, null, new File(attachmentDir));
048                LOG.info("Persisting attachment at: " + file.getAbsolutePath());
049                if (!file.canWrite()) {
050                        throw new RuntimeException("Do not have permissions to write to attachment file at: " + file.getAbsolutePath());
051                }
052                FileOutputStream streamOut = null;
053        BufferedOutputStream bufferedStreamOut = null;
054        try {
055            streamOut = new FileOutputStream(file);
056            bufferedStreamOut = new BufferedOutputStream(streamOut);
057            int c;
058            while ((c = attachment.getAttachedObject().read()) != -1) 
059                {
060                    bufferedStreamOut.write(c);
061                }
062        } finally {
063                if (bufferedStreamOut != null) {
064                        bufferedStreamOut.close();
065                }
066            if (streamOut != null) {
067                streamOut.close();
068            }
069        }
070        attachment.setFileLoc(file.getAbsolutePath());
071        }
072
073        public File findAttachedFile(Attachment attachment) throws Exception {
074                return new File(attachment.getFileLoc());
075        }
076        
077        
078        
079        @Override
080        public Resource findAttachedResource(Attachment attachment) {
081                try {
082                        return new UrlResource(findAttachedFile(attachment).toURI());
083                } catch (Exception e) {
084                        if (e instanceof RuntimeException) {
085                                throw (RuntimeException)e;
086                        }
087                        throw new RuntimeException("Failed to converted attachment file to a Resource", e);
088                }
089        }
090
091        public void deleteAttachedFile(Attachment attachment) throws Exception {
092                File file = new File(attachment.getFileLoc());
093                if (! file.delete()) {
094                        LOG.error("failed to delete file " + attachment.getFileLoc());
095                }
096        }
097        
098        private void createStorageDirIfNecessary() {
099                if (attachmentDir == null) {
100                        throw new RuntimeException("Attachment Directory was not set when configuring workflow");
101                }
102                File attachDir = new File(attachmentDir);
103                if (! attachDir.exists()) {
104                        LOG.warn("No attachment directory found.  Attempting to create directory " + attachmentDir);
105                        boolean directoriesCreated = attachDir.mkdirs();
106                        if (!directoriesCreated) {
107                                throw new RuntimeException("Failed to create directory for attachments at: " + attachDir.getAbsolutePath());
108                        }
109                }
110                if (!attachDir.canWrite()) {
111                        throw new RuntimeException("Do not have permission to write to: " + attachDir.getAbsolutePath());
112                }
113        }
114
115        public String getAttachmentDir() {
116                return attachmentDir;
117        }
118
119        public void setAttachmentDir(String attachmentDir) {
120                this.attachmentDir = attachmentDir;
121        }
122
123}