| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.service.impl; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.apache.log4j.Logger; |
| 20 | |
import org.kuali.rice.core.api.config.property.ConfigurationService; |
| 21 | |
import org.kuali.rice.krad.bo.Attachment; |
| 22 | |
import org.kuali.rice.krad.bo.Note; |
| 23 | |
import org.kuali.rice.krad.bo.PersistableBusinessObject; |
| 24 | |
import org.kuali.rice.krad.dao.AttachmentDao; |
| 25 | |
import org.kuali.rice.krad.document.Document; |
| 26 | |
import org.kuali.rice.krad.service.AttachmentService; |
| 27 | |
import org.kuali.rice.krad.util.KRADConstants; |
| 28 | |
import org.springframework.transaction.annotation.Transactional; |
| 29 | |
|
| 30 | |
import java.io.BufferedInputStream; |
| 31 | |
import java.io.BufferedOutputStream; |
| 32 | |
import java.io.File; |
| 33 | |
import java.io.FileInputStream; |
| 34 | |
import java.io.FileOutputStream; |
| 35 | |
import java.io.IOException; |
| 36 | |
import java.io.InputStream; |
| 37 | |
import java.util.UUID; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
@Transactional |
| 43 | 0 | public class AttachmentServiceImpl implements AttachmentService { |
| 44 | |
private static final int MAX_DIR_LEVELS = 6; |
| 45 | 0 | private static Logger LOG = Logger.getLogger(AttachmentServiceImpl.class); |
| 46 | |
|
| 47 | |
private ConfigurationService kualiConfigurationService; |
| 48 | |
private AttachmentDao attachmentDao; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public Attachment getAttachmentByNoteId(Long noteId) { |
| 55 | 0 | return attachmentDao.getAttachmentByNoteId(noteId); |
| 56 | |
} |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public Attachment createAttachment(PersistableBusinessObject parent, String uploadedFileName, String mimeType, int fileSize, InputStream fileContents, String attachmentTypeCode) throws IOException { |
| 63 | 0 | if ( LOG.isDebugEnabled() ) { |
| 64 | 0 | LOG.debug("starting to create attachment for document: " + parent.getObjectId()); |
| 65 | |
} |
| 66 | 0 | if (parent == null) { |
| 67 | 0 | throw new IllegalArgumentException("invalid (null or uninitialized) document"); |
| 68 | |
} |
| 69 | 0 | if (StringUtils.isBlank(uploadedFileName)) { |
| 70 | 0 | throw new IllegalArgumentException("invalid (blank) fileName"); |
| 71 | |
} |
| 72 | 0 | if (StringUtils.isBlank(mimeType)) { |
| 73 | 0 | throw new IllegalArgumentException("invalid (blank) mimeType"); |
| 74 | |
} |
| 75 | 0 | if (fileSize <= 0) { |
| 76 | 0 | throw new IllegalArgumentException("invalid (non-positive) fileSize"); |
| 77 | |
} |
| 78 | 0 | if (fileContents == null) { |
| 79 | 0 | throw new IllegalArgumentException("invalid (null) inputStream"); |
| 80 | |
} |
| 81 | |
|
| 82 | 0 | String uniqueFileNameGuid = UUID.randomUUID().toString(); |
| 83 | 0 | String fullPathUniqueFileName = getDocumentDirectory(parent.getObjectId()) + File.separator + uniqueFileNameGuid; |
| 84 | |
|
| 85 | 0 | writeInputStreamToFileStorage(fileContents, fullPathUniqueFileName); |
| 86 | |
|
| 87 | |
|
| 88 | 0 | Attachment attachment = new Attachment(); |
| 89 | 0 | attachment.setAttachmentIdentifier(uniqueFileNameGuid); |
| 90 | 0 | attachment.setAttachmentFileName(uploadedFileName); |
| 91 | 0 | attachment.setAttachmentFileSize(new Long(fileSize)); |
| 92 | 0 | attachment.setAttachmentMimeTypeCode(mimeType); |
| 93 | 0 | attachment.setAttachmentTypeCode(attachmentTypeCode); |
| 94 | |
|
| 95 | 0 | LOG.debug("finished creating attachment for document: " + parent.getObjectId()); |
| 96 | 0 | return attachment; |
| 97 | |
} |
| 98 | |
|
| 99 | |
private void writeInputStreamToFileStorage(InputStream fileContents, String fullPathUniqueFileName) throws IOException { |
| 100 | 0 | File fileOut = new File(fullPathUniqueFileName); |
| 101 | 0 | FileOutputStream streamOut = null; |
| 102 | 0 | BufferedOutputStream bufferedStreamOut = null; |
| 103 | |
try { |
| 104 | 0 | streamOut = new FileOutputStream(fileOut); |
| 105 | 0 | bufferedStreamOut = new BufferedOutputStream(streamOut); |
| 106 | |
int c; |
| 107 | 0 | while ((c = fileContents.read()) != -1) { |
| 108 | 0 | bufferedStreamOut.write(c); |
| 109 | |
} |
| 110 | |
} |
| 111 | |
finally { |
| 112 | 0 | bufferedStreamOut.close(); |
| 113 | 0 | streamOut.close(); |
| 114 | 0 | } |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
public void moveAttachmentWherePending(Note note) { |
| 118 | 0 | if (note == null) { |
| 119 | 0 | throw new IllegalArgumentException("Note must be non-null"); |
| 120 | |
} |
| 121 | 0 | if (StringUtils.isBlank(note.getObjectId())) { |
| 122 | 0 | throw new IllegalArgumentException("Note does not have a valid object id, object id was null or empty"); |
| 123 | |
} |
| 124 | 0 | Attachment attachment = note.getAttachment(); |
| 125 | 0 | if(attachment!=null){ |
| 126 | |
try { |
| 127 | 0 | moveAttachmentFromPending(attachment, note.getRemoteObjectIdentifier()); |
| 128 | |
} |
| 129 | 0 | catch (IOException e) { |
| 130 | 0 | throw new RuntimeException("Problem moving pending attachment to final directory"); |
| 131 | 0 | } |
| 132 | |
} |
| 133 | 0 | } |
| 134 | |
|
| 135 | |
private void moveAttachmentFromPending(Attachment attachment, String objectId) throws IOException { |
| 136 | |
|
| 137 | 0 | String fullPendingFileName = getPendingDirectory() + File.separator + attachment.getAttachmentIdentifier(); |
| 138 | 0 | File pendingFile = new File(fullPendingFileName); |
| 139 | |
|
| 140 | 0 | if(pendingFile.exists()) { |
| 141 | 0 | BufferedInputStream bufferedStream = null; |
| 142 | 0 | FileInputStream oldFileStream = null; |
| 143 | 0 | String fullPathNewFile = getDocumentDirectory(objectId) + File.separator + attachment.getAttachmentIdentifier(); |
| 144 | |
try { |
| 145 | 0 | oldFileStream = new FileInputStream(pendingFile); |
| 146 | 0 | bufferedStream = new BufferedInputStream(oldFileStream); |
| 147 | 0 | writeInputStreamToFileStorage(bufferedStream,fullPathNewFile); |
| 148 | |
} |
| 149 | |
finally { |
| 150 | |
|
| 151 | 0 | bufferedStream.close(); |
| 152 | 0 | oldFileStream.close(); |
| 153 | |
|
| 154 | 0 | pendingFile.delete(); |
| 155 | |
|
| 156 | 0 | } |
| 157 | |
} |
| 158 | |
|
| 159 | 0 | } |
| 160 | |
|
| 161 | |
public void deleteAttachmentContents(Attachment attachment) { |
| 162 | 0 | if (attachment.getNote() == null) throw new RuntimeException("Attachment.note must be set in order to delete the attachment"); |
| 163 | 0 | String fullPathUniqueFileName = getDocumentDirectory(attachment.getNote().getRemoteObjectIdentifier()) + File.separator + attachment.getAttachmentIdentifier(); |
| 164 | 0 | File attachmentFile = new File(fullPathUniqueFileName); |
| 165 | 0 | attachmentFile.delete(); |
| 166 | 0 | } |
| 167 | |
private String getPendingDirectory() { |
| 168 | 0 | return this.getDocumentDirectory(""); |
| 169 | |
} |
| 170 | |
|
| 171 | |
private String getDocumentDirectory(String objectId) { |
| 172 | |
|
| 173 | 0 | File documentDirectory = new File(getDocumentFileStorageLocation(objectId)); |
| 174 | 0 | if (!documentDirectory.exists()) { |
| 175 | 0 | boolean success = documentDirectory.mkdirs(); |
| 176 | 0 | if (!success) { |
| 177 | 0 | throw new RuntimeException("Could not generate directory for File at: " + documentDirectory.getAbsolutePath()); |
| 178 | |
} |
| 179 | |
} |
| 180 | 0 | return documentDirectory.getAbsolutePath(); |
| 181 | |
} |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
public InputStream retrieveAttachmentContents(Attachment attachment) throws IOException { |
| 189 | |
|
| 190 | 0 | if(attachment.getNoteIdentifier()!=null) { |
| 191 | 0 | attachment.refreshNonUpdateableReferences(); |
| 192 | |
} |
| 193 | |
|
| 194 | 0 | String parentDirectory = ""; |
| 195 | 0 | if(attachment.getNote()!=null && attachment.getNote().getRemoteObjectIdentifier() != null) { |
| 196 | 0 | parentDirectory = attachment.getNote().getRemoteObjectIdentifier(); |
| 197 | |
} |
| 198 | |
|
| 199 | 0 | return new BufferedInputStream(new FileInputStream(getDocumentDirectory(parentDirectory) + File.separator + attachment.getAttachmentIdentifier())); |
| 200 | |
} |
| 201 | |
|
| 202 | |
private String getDocumentFileStorageLocation(String objectId) { |
| 203 | 0 | String location = null; |
| 204 | 0 | if(StringUtils.isEmpty(objectId)) { |
| 205 | 0 | location = kualiConfigurationService.getPropertyValueAsString( |
| 206 | |
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.getPropertyValueAsString(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 | |
} |