| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 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 |  |  import java.util.logging.Logger; | 
  | 23 |  |   | 
  | 24 |  |  import org.kuali.rice.kew.notes.Attachment; | 
  | 25 |  |  import org.kuali.rice.kew.notes.service.AttachmentService; | 
  | 26 |  |  import org.kuali.rice.kew.service.KEWServiceLocator; | 
  | 27 |  |   | 
  | 28 |  |   | 
  | 29 |  |   | 
  | 30 |  |   | 
  | 31 |  |   | 
  | 32 |  |   | 
  | 33 |  |   | 
  | 34 | 0 |  public class AttachmentServiceImpl implements AttachmentService { | 
  | 35 |  |           | 
  | 36 | 0 |          protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AttachmentServiceImpl.class); | 
  | 37 |  |           | 
  | 38 |  |          private static final String ATTACHMENT_PREPEND = "wf_att_"; | 
  | 39 |  |           | 
  | 40 |  |          private String attachmentDir; | 
  | 41 |  |   | 
  | 42 |  |          public void persistAttachedFileAndSetAttachmentBusinessObjectValue(Attachment attachment) throws Exception { | 
  | 43 | 0 |                  createStorageDirIfNecessary(); | 
  | 44 | 0 |                  Long uniqueId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId(); | 
  | 45 | 0 |                  String internalFileIndicator = attachment.getFileName().replace('.', '_'); | 
  | 46 | 0 |                  String fileName = ATTACHMENT_PREPEND + attachment.getNote().getDocumentId() + "_" + internalFileIndicator + "_" + uniqueId; | 
  | 47 | 0 |                  File file = File.createTempFile(fileName, null, new File(attachmentDir)); | 
  | 48 | 0 |                  LOG.info("Persisting attachment at: " + file.getAbsolutePath()); | 
  | 49 | 0 |                  if (!file.canWrite()) { | 
  | 50 | 0 |                          throw new RuntimeException("Do not have permissions to write to attachment file at: " + file.getAbsolutePath()); | 
  | 51 |  |                  } | 
  | 52 | 0 |                  FileOutputStream streamOut = null; | 
  | 53 | 0 |          BufferedOutputStream bufferedStreamOut = null; | 
  | 54 |  |          try { | 
  | 55 | 0 |              streamOut = new FileOutputStream(file); | 
  | 56 | 0 |              bufferedStreamOut = new BufferedOutputStream(streamOut); | 
  | 57 |  |              int c; | 
  | 58 | 0 |              while ((c = attachment.getAttachedObject().read()) != -1)  | 
  | 59 |  |                  { | 
  | 60 | 0 |                      bufferedStreamOut.write(c); | 
  | 61 |  |                  } | 
  | 62 |  |          } finally { | 
  | 63 | 0 |                  if (bufferedStreamOut != null) { | 
  | 64 | 0 |                          bufferedStreamOut.close(); | 
  | 65 |  |                  } | 
  | 66 | 0 |              if (streamOut != null) { | 
  | 67 | 0 |                      streamOut.close(); | 
  | 68 |  |              } | 
  | 69 |  |          } | 
  | 70 | 0 |          attachment.setFileLoc(file.getAbsolutePath()); | 
  | 71 | 0 |          } | 
  | 72 |  |   | 
  | 73 |  |          public File findAttachedFile(Attachment attachment) throws Exception { | 
  | 74 | 0 |                  return new File(attachment.getFileLoc()); | 
  | 75 |  |          } | 
  | 76 |  |           | 
  | 77 |  |          public void deleteAttachedFile(Attachment attachment) throws Exception { | 
  | 78 | 0 |                  File file = new File(attachment.getFileLoc()); | 
  | 79 | 0 |                  if (! file.delete()) { | 
  | 80 | 0 |                          LOG.error("failed to delete file " + attachment.getFileLoc()); | 
  | 81 |  |                  } | 
  | 82 | 0 |          } | 
  | 83 |  |           | 
  | 84 |  |          private void createStorageDirIfNecessary() { | 
  | 85 | 0 |                  if (attachmentDir == null) { | 
  | 86 | 0 |                          throw new RuntimeException("Attachment Directory was not set when configuring workflow"); | 
  | 87 |  |                  } | 
  | 88 | 0 |                  File attachDir = new File(attachmentDir); | 
  | 89 | 0 |                  if (! attachDir.exists()) { | 
  | 90 | 0 |                          LOG.warn("No attachment directory found.  Attempting to create directory " + attachmentDir); | 
  | 91 | 0 |                          boolean directoriesCreated = attachDir.mkdirs(); | 
  | 92 | 0 |                          if (!directoriesCreated) { | 
  | 93 | 0 |                                  throw new RuntimeException("Failed to create directory for attachments at: " + attachDir.getAbsolutePath()); | 
  | 94 |  |                          } | 
  | 95 |  |                  } | 
  | 96 | 0 |                  if (!attachDir.canWrite()) { | 
  | 97 | 0 |                          throw new RuntimeException("Do not have permission to write to: " + attachDir.getAbsolutePath()); | 
  | 98 |  |                  } | 
  | 99 | 0 |          } | 
  | 100 |  |   | 
  | 101 |  |          public String getAttachmentDir() { | 
  | 102 | 0 |                  return attachmentDir; | 
  | 103 |  |          } | 
  | 104 |  |   | 
  | 105 |  |          public void setAttachmentDir(String attachmentDir) { | 
  | 106 | 0 |                  this.attachmentDir = attachmentDir; | 
  | 107 | 0 |          } | 
  | 108 |  |           | 
  | 109 |  |  } |