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