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