Coverage Report - org.kuali.rice.kew.notes.service.impl.AttachmentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AttachmentServiceImpl
0%
0/41
0%
0/18
3.167
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.notes.service.impl;
 17  
 
 18  
 import java.io.BufferedOutputStream;
 19  
 import java.io.File;
 20  
 import java.io.FileOutputStream;
 21  
 import java.util.logging.Logger;
 22  
 
 23  
 import org.kuali.rice.kew.notes.Attachment;
 24  
 import org.kuali.rice.kew.notes.service.AttachmentService;
 25  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 26  
 
 27  
 
 28  
 /**
 29  
  * Implementation of the {@link AttachmentService}.
 30  
  *
 31  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 32  
  */
 33  0
 public class AttachmentServiceImpl implements AttachmentService {
 34  
         
 35  0
         protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AttachmentServiceImpl.class);
 36  
         
 37  
         private static final String ATTACHMENT_PREPEND = "wf_att_";
 38  
         
 39  
         private String attachmentDir;
 40  
 
 41  
         public void persistAttachedFileAndSetAttachmentBusinessObjectValue(Attachment attachment) throws Exception {
 42  0
                 createStorageDirIfNecessary();
 43  0
                 String uniqueId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
 44  0
                 String internalFileIndicator = attachment.getFileName().replace('.', '_');
 45  0
                 String fileName = ATTACHMENT_PREPEND + attachment.getNote().getDocumentId() + "_" + internalFileIndicator + "_" + uniqueId;
 46  0
                 File file = File.createTempFile(fileName, null, new File(attachmentDir));
 47  0
                 LOG.info("Persisting attachment at: " + file.getAbsolutePath());
 48  0
                 if (!file.canWrite()) {
 49  0
                         throw new RuntimeException("Do not have permissions to write to attachment file at: " + file.getAbsolutePath());
 50  
                 }
 51  0
                 FileOutputStream streamOut = null;
 52  0
         BufferedOutputStream bufferedStreamOut = null;
 53  
         try {
 54  0
             streamOut = new FileOutputStream(file);
 55  0
             bufferedStreamOut = new BufferedOutputStream(streamOut);
 56  
             int c;
 57  0
             while ((c = attachment.getAttachedObject().read()) != -1) 
 58  
                 {
 59  0
                     bufferedStreamOut.write(c);
 60  
                 }
 61  
         } finally {
 62  0
                 if (bufferedStreamOut != null) {
 63  0
                         bufferedStreamOut.close();
 64  
                 }
 65  0
             if (streamOut != null) {
 66  0
                     streamOut.close();
 67  
             }
 68  
         }
 69  0
         attachment.setFileLoc(file.getAbsolutePath());
 70  0
         }
 71  
 
 72  
         public File findAttachedFile(Attachment attachment) throws Exception {
 73  0
                 return new File(attachment.getFileLoc());
 74  
         }
 75  
         
 76  
         public void deleteAttachedFile(Attachment attachment) throws Exception {
 77  0
                 File file = new File(attachment.getFileLoc());
 78  0
                 if (! file.delete()) {
 79  0
                         LOG.error("failed to delete file " + attachment.getFileLoc());
 80  
                 }
 81  0
         }
 82  
         
 83  
         private void createStorageDirIfNecessary() {
 84  0
                 if (attachmentDir == null) {
 85  0
                         throw new RuntimeException("Attachment Directory was not set when configuring workflow");
 86  
                 }
 87  0
                 File attachDir = new File(attachmentDir);
 88  0
                 if (! attachDir.exists()) {
 89  0
                         LOG.warn("No attachment directory found.  Attempting to create directory " + attachmentDir);
 90  0
                         boolean directoriesCreated = attachDir.mkdirs();
 91  0
                         if (!directoriesCreated) {
 92  0
                                 throw new RuntimeException("Failed to create directory for attachments at: " + attachDir.getAbsolutePath());
 93  
                         }
 94  
                 }
 95  0
                 if (!attachDir.canWrite()) {
 96  0
                         throw new RuntimeException("Do not have permission to write to: " + attachDir.getAbsolutePath());
 97  
                 }
 98  0
         }
 99  
 
 100  
         public String getAttachmentDir() {
 101  0
                 return attachmentDir;
 102  
         }
 103  
 
 104  
         public void setAttachmentDir(String attachmentDir) {
 105  0
                 this.attachmentDir = attachmentDir;
 106  0
         }
 107  
         
 108  
 }