Coverage Report - org.kuali.rice.kew.notes.service.impl.AttachmentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AttachmentServiceImpl
0%
0/36
0%
0/12
2.167
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.notes.service.impl;
 18  
 
 19  
 import java.io.BufferedOutputStream;
 20  
 import java.io.File;
 21  
 import java.io.FileOutputStream;
 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
                 Long uniqueId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
 44  0
                 String internalFileIndicator = attachment.getFileName().replace('.', '_');
 45  0
                 String fileName = ATTACHMENT_PREPEND + attachment.getNote().getRouteHeaderId() + "_" + internalFileIndicator + "_" + uniqueId;
 46  
                 
 47  0
                 File file = File.createTempFile(fileName, null, new File(attachmentDir));
 48  0
         FileOutputStream streamOut = null;
 49  0
         BufferedOutputStream bufferedStreamOut = null;
 50  
         try {
 51  0
             streamOut = new FileOutputStream(file);
 52  0
             bufferedStreamOut = new BufferedOutputStream(streamOut);
 53  
             int c;
 54  0
             while ((c = attachment.getAttachedObject().read()) != -1) 
 55  
                 {
 56  0
                     bufferedStreamOut.write(c);
 57  
                 }
 58  0
         } finally {
 59  0
                 if (bufferedStreamOut != null) {
 60  0
                         bufferedStreamOut.close();
 61  
                 }
 62  0
             if (streamOut != null) {
 63  0
                     streamOut.close();
 64  
             }
 65  0
         }
 66  0
         attachment.setFileLoc(file.getAbsolutePath());
 67  0
         }
 68  
 
 69  
         public File findAttachedFile(Attachment attachment) throws Exception {
 70  0
                 return new File(attachment.getFileLoc());
 71  
         }
 72  
         
 73  
         public void deleteAttachedFile(Attachment attachment) throws Exception {
 74  0
                 File file = new File(attachment.getFileLoc());
 75  0
                 if (! file.delete()) {
 76  0
                         LOG.error("failed to delete file " + attachment.getFileLoc());
 77  
                 }
 78  0
         }
 79  
         
 80  
         private void createStorageDirIfNecessary() {
 81  0
                 if (attachmentDir == null) {
 82  0
                         throw new RuntimeException("Attachment Directory was not set when configuring workflow");
 83  
                 }
 84  0
                 File attachDir = new File(attachmentDir);
 85  0
                 if (! attachDir.exists()) {
 86  0
                         LOG.warn("No attachment directory found.  Attempting to create directory " + attachmentDir);
 87  0
                         attachDir.mkdirs();
 88  
                 }
 89  0
         }
 90  
 
 91  
         public String getAttachmentDir() {
 92  0
                 return attachmentDir;
 93  
         }
 94  
 
 95  
         public void setAttachmentDir(String attachmentDir) {
 96  0
                 this.attachmentDir = attachmentDir;
 97  0
         }
 98  
         
 99  
 }