001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.notes;
018:
019: import java.io.BufferedOutputStream;
020: import java.io.File;
021: import java.io.FileOutputStream;
022:
023: import edu.iu.uis.eden.KEWServiceLocator;
024:
025: /**
026: * Implementation of the {@link AttachmentService}.
027: *
028: * @author rkirkend
029: */
030: public class AttachmentServiceImpl implements AttachmentService {
031:
032: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
033: .getLogger(AttachmentServiceImpl.class);
034:
035: private static final String ATTACHMENT_PREPEND = "wf_att_";
036:
037: private String attachmentDir;
038:
039: public void persistAttachedFileAndSetAttachmentBusinessObjectValue(
040: Attachment attachment) throws Exception {
041: createStorageDirIfNecessary();
042: Long uniqueId = KEWServiceLocator.getResponsibilityIdService()
043: .getNewResponsibilityId();
044: String internalFileIndicator = attachment.getFileName()
045: .replace('.', '_');
046: String fileName = ATTACHMENT_PREPEND
047: + attachment.getNote().getRouteHeaderId() + "_"
048: + internalFileIndicator + "_" + uniqueId;
049:
050: File file = File.createTempFile(fileName, null, new File(
051: attachmentDir));
052: FileOutputStream streamOut = null;
053: BufferedOutputStream bufferedStreamOut = null;
054: try {
055: streamOut = new FileOutputStream(file);
056: bufferedStreamOut = new BufferedOutputStream(streamOut);
057: int c;
058: while ((c = attachment.getAttachedObject().read()) != -1) {
059: bufferedStreamOut.write(c);
060: }
061: } finally {
062: if (bufferedStreamOut != null) {
063: bufferedStreamOut.close();
064: }
065: if (streamOut != null) {
066: streamOut.close();
067: }
068: }
069: attachment.setFileLoc(file.getAbsolutePath());
070: }
071:
072: public File findAttachedFile(Attachment attachment)
073: throws Exception {
074: return new File(attachment.getFileLoc());
075: }
076:
077: public void deleteAttachedFile(Attachment attachment)
078: throws Exception {
079: File file = new File(attachment.getFileLoc());
080: if (!file.delete()) {
081: LOG.error("failed to delete file "
082: + attachment.getFileLoc());
083: }
084: }
085:
086: private void createStorageDirIfNecessary() {
087: if (attachmentDir == null) {
088: throw new RuntimeException(
089: "Attachment Directory was not set when configuring workflow");
090: }
091: File attachDir = new File(attachmentDir);
092: if (!attachDir.exists()) {
093: LOG
094: .warn("No attachment directory found. Attempting to create directory "
095: + attachmentDir);
096: attachDir.mkdirs();
097: }
098: }
099:
100: public String getAttachmentDir() {
101: return attachmentDir;
102: }
103:
104: public void setAttachmentDir(String attachmentDir) {
105: this.attachmentDir = attachmentDir;
106: }
107:
108: }
|