1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.service.impl; |
17 | |
|
18 | |
import java.io.BufferedInputStream; |
19 | |
import java.io.BufferedOutputStream; |
20 | |
import java.io.File; |
21 | |
import java.io.FileInputStream; |
22 | |
import java.io.FileOutputStream; |
23 | |
import java.io.IOException; |
24 | |
import java.io.InputStream; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.commons.lang.StringUtils; |
28 | |
import org.apache.log4j.Logger; |
29 | |
import org.kuali.rice.kns.bo.Attachment; |
30 | |
import org.kuali.rice.kns.bo.Note; |
31 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
32 | |
import org.kuali.rice.kns.dao.AttachmentDao; |
33 | |
import org.kuali.rice.kns.dao.NoteDao; |
34 | |
import org.kuali.rice.kns.service.AttachmentService; |
35 | |
import org.kuali.rice.kns.service.KualiConfigurationService; |
36 | |
import org.kuali.rice.kns.util.Guid; |
37 | |
import org.kuali.rice.kns.util.KNSConstants; |
38 | |
import org.springframework.transaction.annotation.Transactional; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
@Transactional |
44 | 0 | public class AttachmentServiceImpl implements AttachmentService { |
45 | |
private static final int MAX_DIR_LEVELS = 6; |
46 | 0 | private static Logger LOG = Logger.getLogger(AttachmentServiceImpl.class); |
47 | |
|
48 | |
private KualiConfigurationService kualiConfigurationService; |
49 | |
private AttachmentDao attachmentDao; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public Attachment getAttachmentByNoteId(Long noteId) { |
56 | 0 | return attachmentDao.getAttachmentByNoteId(noteId); |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public Attachment createAttachment(PersistableBusinessObject parent, String uploadedFileName, String mimeType, int fileSize, InputStream fileContents, String attachmentTypeCode) throws IOException { |
64 | 0 | if ( LOG.isDebugEnabled() ) { |
65 | 0 | LOG.debug("starting to create attachment for document: " + parent.getObjectId()); |
66 | |
} |
67 | 0 | if (parent == null) { |
68 | 0 | throw new IllegalArgumentException("invalid (null or uninitialized) document"); |
69 | |
} |
70 | 0 | if (StringUtils.isBlank(uploadedFileName)) { |
71 | 0 | throw new IllegalArgumentException("invalid (blank) fileName"); |
72 | |
} |
73 | 0 | if (StringUtils.isBlank(mimeType)) { |
74 | 0 | throw new IllegalArgumentException("invalid (blank) mimeType"); |
75 | |
} |
76 | 0 | if (fileSize <= 0) { |
77 | 0 | throw new IllegalArgumentException("invalid (non-positive) fileSize"); |
78 | |
} |
79 | 0 | if (fileContents == null) { |
80 | 0 | throw new IllegalArgumentException("invalid (null) inputStream"); |
81 | |
} |
82 | |
|
83 | 0 | String uniqueFileNameGuid = new Guid().toString(); |
84 | 0 | String fullPathUniqueFileName = getDocumentDirectory(parent.getObjectId()) + File.separator + uniqueFileNameGuid; |
85 | |
|
86 | 0 | writeInputStreamToFileStorage(fileContents, fullPathUniqueFileName); |
87 | |
|
88 | |
|
89 | 0 | Attachment attachment = new Attachment(); |
90 | 0 | attachment.setAttachmentIdentifier(uniqueFileNameGuid); |
91 | 0 | attachment.setAttachmentFileName(uploadedFileName); |
92 | 0 | attachment.setAttachmentFileSize(new Long(fileSize)); |
93 | 0 | attachment.setAttachmentMimeTypeCode(mimeType); |
94 | 0 | attachment.setAttachmentTypeCode(attachmentTypeCode); |
95 | |
|
96 | 0 | LOG.debug("finished creating attachment for document: " + parent.getObjectId()); |
97 | 0 | return attachment; |
98 | |
} |
99 | |
|
100 | |
private void writeInputStreamToFileStorage(InputStream fileContents, String fullPathUniqueFileName) throws IOException { |
101 | 0 | File fileOut = new File(fullPathUniqueFileName); |
102 | 0 | FileOutputStream streamOut = null; |
103 | 0 | BufferedOutputStream bufferedStreamOut = null; |
104 | |
try { |
105 | 0 | streamOut = new FileOutputStream(fileOut); |
106 | 0 | bufferedStreamOut = new BufferedOutputStream(streamOut); |
107 | |
int c; |
108 | 0 | while ((c = fileContents.read()) != -1) { |
109 | 0 | bufferedStreamOut.write(c); |
110 | |
} |
111 | 0 | } |
112 | |
finally { |
113 | 0 | bufferedStreamOut.close(); |
114 | 0 | streamOut.close(); |
115 | 0 | } |
116 | 0 | } |
117 | |
|
118 | |
public void moveAttachmentsWherePending(List notes, String objectId) { |
119 | 0 | for (Object obj : notes) { |
120 | 0 | Note note = (Note)obj; |
121 | 0 | Attachment attachment = note.getAttachment(); |
122 | 0 | if(attachment!=null){ |
123 | |
try { |
124 | 0 | moveAttachmentFromPending(attachment, objectId); |
125 | |
} |
126 | 0 | catch (IOException e) { |
127 | 0 | throw new RuntimeException("Problem moving pending attachment to final directory"); |
128 | |
|
129 | 0 | } |
130 | |
} |
131 | 0 | } |
132 | 0 | } |
133 | |
|
134 | |
private void moveAttachmentFromPending(Attachment attachment, String objectId) throws IOException { |
135 | |
|
136 | 0 | String fullPendingFileName = getPendingDirectory() + File.separator + attachment.getAttachmentIdentifier(); |
137 | 0 | File pendingFile = new File(fullPendingFileName); |
138 | |
|
139 | 0 | if(pendingFile.exists()) { |
140 | 0 | BufferedInputStream bufferedStream = null; |
141 | 0 | FileInputStream oldFileStream = null; |
142 | 0 | String fullPathNewFile = getDocumentDirectory(objectId) + File.separator + attachment.getAttachmentIdentifier(); |
143 | |
try { |
144 | 0 | oldFileStream = new FileInputStream(pendingFile); |
145 | 0 | bufferedStream = new BufferedInputStream(oldFileStream); |
146 | 0 | writeInputStreamToFileStorage(bufferedStream,fullPathNewFile); |
147 | 0 | } |
148 | |
finally { |
149 | |
|
150 | 0 | bufferedStream.close(); |
151 | 0 | oldFileStream.close(); |
152 | |
|
153 | 0 | pendingFile.delete(); |
154 | |
|
155 | 0 | } |
156 | |
} |
157 | |
|
158 | 0 | } |
159 | |
|
160 | |
public void deleteAttachmentContents(Attachment attachment) { |
161 | 0 | if (attachment.getNote() == null) throw new RuntimeException("Attachment.note must be set in order to delete the attachment"); |
162 | 0 | String fullPathUniqueFileName = getDocumentDirectory(attachment.getNote().getRemoteObjectIdentifier()) + File.separator + attachment.getAttachmentIdentifier(); |
163 | 0 | File attachmentFile = new File(fullPathUniqueFileName); |
164 | 0 | attachmentFile.delete(); |
165 | 0 | } |
166 | |
private String getPendingDirectory() { |
167 | 0 | return this.getDocumentDirectory(""); |
168 | |
} |
169 | |
|
170 | |
private String getDocumentDirectory(String objectId) { |
171 | |
|
172 | 0 | File documentDirectory = new File(getDocumentFileStorageLocation(objectId)); |
173 | 0 | if (!documentDirectory.exists()) { |
174 | 0 | boolean success = documentDirectory.mkdirs(); |
175 | 0 | if (!success) { |
176 | 0 | throw new RuntimeException("Could not generate directory for File at: " + documentDirectory.getAbsolutePath()); |
177 | |
} |
178 | |
} |
179 | 0 | return documentDirectory.getAbsolutePath(); |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public InputStream retrieveAttachmentContents(Attachment attachment) throws IOException { |
188 | |
|
189 | 0 | if(attachment.getNoteIdentifier()!=null) { |
190 | 0 | attachment.refreshNonUpdateableReferences(); |
191 | |
} |
192 | |
|
193 | 0 | String parentDirectory = ""; |
194 | 0 | if(attachment.getNote()!=null) { |
195 | 0 | parentDirectory = attachment.getNote().getRemoteObjectIdentifier(); |
196 | |
} |
197 | |
|
198 | 0 | return new BufferedInputStream(new FileInputStream(getDocumentDirectory(parentDirectory) + File.separator + attachment.getAttachmentIdentifier())); |
199 | |
} |
200 | |
|
201 | |
private String getDocumentFileStorageLocation(String objectId) { |
202 | 0 | String location = null; |
203 | 0 | if(StringUtils.isEmpty(objectId)) { |
204 | 0 | location = kualiConfigurationService.getPropertyString(KNSConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY); |
205 | |
} else { |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | 0 | char[] chars = objectId.toUpperCase().replace(" ", "").toCharArray(); |
213 | 0 | int count = chars.length < MAX_DIR_LEVELS ? chars.length : MAX_DIR_LEVELS; |
214 | |
|
215 | 0 | StringBuffer prefix = new StringBuffer(); |
216 | 0 | for ( int i = 0; i < count; i++ ) |
217 | 0 | prefix.append(File.separator + chars[i]); |
218 | |
|
219 | 0 | location = kualiConfigurationService.getPropertyString(KNSConstants.ATTACHMENTS_DIRECTORY_KEY) + prefix + File.separator + objectId; |
220 | |
} |
221 | 0 | return location; |
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public void deletePendingAttachmentsModifiedBefore(long modificationTime) { |
228 | 0 | String pendingAttachmentDirName = getPendingDirectory(); |
229 | 0 | if (StringUtils.isBlank(pendingAttachmentDirName)) { |
230 | 0 | throw new RuntimeException("Blank pending attachment directory name"); |
231 | |
} |
232 | 0 | File pendingAttachmentDir = new File(pendingAttachmentDirName); |
233 | 0 | if (!pendingAttachmentDir.exists()) { |
234 | 0 | throw new RuntimeException("Pending attachment directory does not exist"); |
235 | |
} |
236 | 0 | if (!pendingAttachmentDir.isDirectory()) { |
237 | 0 | throw new RuntimeException("Pending attachment directory is not a directory! " + pendingAttachmentDir.getAbsolutePath()); |
238 | |
} |
239 | |
|
240 | 0 | File[] files = pendingAttachmentDir.listFiles(); |
241 | 0 | for (File file : files) { |
242 | 0 | if (!file.getName().equals("placeholder.txt")) { |
243 | 0 | if (file.lastModified() < modificationTime) { |
244 | 0 | file.delete(); |
245 | |
} |
246 | |
} |
247 | |
} |
248 | |
|
249 | 0 | } |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
public void setAttachmentDao(AttachmentDao d) { |
258 | 0 | this.attachmentDao = d; |
259 | 0 | } |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
public AttachmentDao getAttachmentDao() { |
265 | 0 | return attachmentDao; |
266 | |
} |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
public KualiConfigurationService getKualiConfigurationService() { |
273 | 0 | return kualiConfigurationService; |
274 | |
} |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
public void setKualiConfigurationService(KualiConfigurationService configService) { |
281 | 0 | this.kualiConfigurationService = configService; |
282 | 0 | } |
283 | |
} |