1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.krad.service.impl; |
18 | |
|
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.log4j.Logger; |
21 | |
import org.kuali.rice.core.api.config.property.ConfigurationService; |
22 | |
import org.kuali.rice.krad.bo.Attachment; |
23 | |
import org.kuali.rice.krad.bo.Note; |
24 | |
import org.kuali.rice.krad.bo.PersistableBusinessObject; |
25 | |
import org.kuali.rice.krad.dao.AttachmentDao; |
26 | |
import org.kuali.rice.krad.document.Document; |
27 | |
import org.kuali.rice.krad.service.AttachmentService; |
28 | |
import org.kuali.rice.krad.util.KRADConstants; |
29 | |
import org.springframework.transaction.annotation.Transactional; |
30 | |
|
31 | |
import java.io.BufferedInputStream; |
32 | |
import java.io.BufferedOutputStream; |
33 | |
import java.io.File; |
34 | |
import java.io.FileInputStream; |
35 | |
import java.io.FileOutputStream; |
36 | |
import java.io.IOException; |
37 | |
import java.io.InputStream; |
38 | |
import java.util.UUID; |
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 ConfigurationService 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 = UUID.randomUUID().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 | |
} |
112 | |
finally { |
113 | 0 | bufferedStreamOut.close(); |
114 | 0 | streamOut.close(); |
115 | 0 | } |
116 | 0 | } |
117 | |
|
118 | |
public void moveAttachmentWherePending(Note note) { |
119 | 0 | if (note == null) { |
120 | 0 | throw new IllegalArgumentException("Note must be non-null"); |
121 | |
} |
122 | 0 | if (StringUtils.isBlank(note.getObjectId())) { |
123 | 0 | throw new IllegalArgumentException("Note does not have a valid object id, object id was null or empty"); |
124 | |
} |
125 | 0 | Attachment attachment = note.getAttachment(); |
126 | 0 | if(attachment!=null){ |
127 | |
try { |
128 | 0 | moveAttachmentFromPending(attachment, note.getObjectId()); |
129 | |
} |
130 | 0 | catch (IOException e) { |
131 | 0 | throw new RuntimeException("Problem moving pending attachment to final directory"); |
132 | 0 | } |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | |
private void moveAttachmentFromPending(Attachment attachment, String objectId) throws IOException { |
137 | |
|
138 | 0 | String fullPendingFileName = getPendingDirectory() + File.separator + attachment.getAttachmentIdentifier(); |
139 | 0 | File pendingFile = new File(fullPendingFileName); |
140 | |
|
141 | 0 | if(pendingFile.exists()) { |
142 | 0 | BufferedInputStream bufferedStream = null; |
143 | 0 | FileInputStream oldFileStream = null; |
144 | 0 | String fullPathNewFile = getDocumentDirectory(objectId) + File.separator + attachment.getAttachmentIdentifier(); |
145 | |
try { |
146 | 0 | oldFileStream = new FileInputStream(pendingFile); |
147 | 0 | bufferedStream = new BufferedInputStream(oldFileStream); |
148 | 0 | writeInputStreamToFileStorage(bufferedStream,fullPathNewFile); |
149 | |
} |
150 | |
finally { |
151 | |
|
152 | 0 | bufferedStream.close(); |
153 | 0 | oldFileStream.close(); |
154 | |
|
155 | 0 | pendingFile.delete(); |
156 | |
|
157 | 0 | } |
158 | |
} |
159 | |
|
160 | 0 | } |
161 | |
|
162 | |
public void deleteAttachmentContents(Attachment attachment) { |
163 | 0 | if (attachment.getNote() == null) throw new RuntimeException("Attachment.note must be set in order to delete the attachment"); |
164 | 0 | String fullPathUniqueFileName = getDocumentDirectory(attachment.getNote().getRemoteObjectIdentifier()) + File.separator + attachment.getAttachmentIdentifier(); |
165 | 0 | File attachmentFile = new File(fullPathUniqueFileName); |
166 | 0 | attachmentFile.delete(); |
167 | 0 | } |
168 | |
private String getPendingDirectory() { |
169 | 0 | return this.getDocumentDirectory(""); |
170 | |
} |
171 | |
|
172 | |
private String getDocumentDirectory(String objectId) { |
173 | |
|
174 | 0 | File documentDirectory = new File(getDocumentFileStorageLocation(objectId)); |
175 | 0 | if (!documentDirectory.exists()) { |
176 | 0 | boolean success = documentDirectory.mkdirs(); |
177 | 0 | if (!success) { |
178 | 0 | throw new RuntimeException("Could not generate directory for File at: " + documentDirectory.getAbsolutePath()); |
179 | |
} |
180 | |
} |
181 | 0 | return documentDirectory.getAbsolutePath(); |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public InputStream retrieveAttachmentContents(Attachment attachment) throws IOException { |
190 | |
|
191 | 0 | if(attachment.getNoteIdentifier()!=null) { |
192 | 0 | attachment.refreshNonUpdateableReferences(); |
193 | |
} |
194 | |
|
195 | 0 | String parentDirectory = ""; |
196 | 0 | if(attachment.getNote()!=null) { |
197 | 0 | parentDirectory = attachment.getNote().getRemoteObjectIdentifier(); |
198 | |
} |
199 | |
|
200 | 0 | return new BufferedInputStream(new FileInputStream(getDocumentDirectory(parentDirectory) + File.separator + attachment.getAttachmentIdentifier())); |
201 | |
} |
202 | |
|
203 | |
private String getDocumentFileStorageLocation(String objectId) { |
204 | 0 | String location = null; |
205 | 0 | if(StringUtils.isEmpty(objectId)) { |
206 | 0 | location = kualiConfigurationService.getPropertyString(KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY); |
207 | |
} else { |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | 0 | char[] chars = objectId.toUpperCase().replace(" ", "").toCharArray(); |
215 | 0 | int count = chars.length < MAX_DIR_LEVELS ? chars.length : MAX_DIR_LEVELS; |
216 | |
|
217 | 0 | StringBuffer prefix = new StringBuffer(); |
218 | 0 | for ( int i = 0; i < count; i++ ) |
219 | 0 | prefix.append(File.separator + chars[i]); |
220 | |
|
221 | 0 | location = kualiConfigurationService.getPropertyString(KRADConstants.ATTACHMENTS_DIRECTORY_KEY) + prefix + File.separator + objectId; |
222 | |
} |
223 | 0 | return location; |
224 | |
} |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public void deletePendingAttachmentsModifiedBefore(long modificationTime) { |
230 | 0 | String pendingAttachmentDirName = getPendingDirectory(); |
231 | 0 | if (StringUtils.isBlank(pendingAttachmentDirName)) { |
232 | 0 | throw new RuntimeException("Blank pending attachment directory name"); |
233 | |
} |
234 | 0 | File pendingAttachmentDir = new File(pendingAttachmentDirName); |
235 | 0 | if (!pendingAttachmentDir.exists()) { |
236 | 0 | throw new RuntimeException("Pending attachment directory does not exist"); |
237 | |
} |
238 | 0 | if (!pendingAttachmentDir.isDirectory()) { |
239 | 0 | throw new RuntimeException("Pending attachment directory is not a directory! " + pendingAttachmentDir.getAbsolutePath()); |
240 | |
} |
241 | |
|
242 | 0 | File[] files = pendingAttachmentDir.listFiles(); |
243 | 0 | for (File file : files) { |
244 | 0 | if (!file.getName().equals("placeholder.txt")) { |
245 | 0 | if (file.lastModified() < modificationTime) { |
246 | 0 | file.delete(); |
247 | |
} |
248 | |
} |
249 | |
} |
250 | |
|
251 | 0 | } |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
public void setAttachmentDao(AttachmentDao d) { |
260 | 0 | this.attachmentDao = d; |
261 | 0 | } |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
public AttachmentDao getAttachmentDao() { |
267 | 0 | return attachmentDao; |
268 | |
} |
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public ConfigurationService getKualiConfigurationService() { |
275 | 0 | return kualiConfigurationService; |
276 | |
} |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
public void setKualiConfigurationService(ConfigurationService configService) { |
283 | 0 | this.kualiConfigurationService = configService; |
284 | 0 | } |
285 | |
} |