View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.core.execution.tasks;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.UUID;
23  
24  import org.argeo.slc.SlcException;
25  import org.argeo.slc.core.attachment.Attachment;
26  import org.argeo.slc.core.attachment.AttachmentUploader;
27  import org.argeo.slc.core.attachment.AttachmentsEnabled;
28  import org.springframework.core.io.Resource;
29  
30  public class UploadAttachments implements Runnable {
31  	private AttachmentUploader attachmentUploader;
32  	private Attachment attachment = null;
33  	private Resource resource = null;
34  	private Map<Attachment, Resource> attachments = new HashMap<Attachment, Resource>();
35  	private List<AttachmentsEnabled> attachTo = new ArrayList<AttachmentsEnabled>();
36  	private Boolean newUuidPerExecution = true;
37  
38  	public void run() {
39  		if (attachment != null) {
40  			if (resource == null)
41  				throw new SlcException("A resource must be specified.");
42  			uploadAndAdd(attachment, resource);
43  		}
44  
45  		for (Attachment attachmentT : attachments.keySet()) {
46  			Resource resourceT = attachments.get(attachmentT);
47  			uploadAndAdd(attachmentT, resourceT);
48  		}
49  
50  	}
51  
52  	protected void uploadAndAdd(Attachment attachment, Resource resource) {
53  		if (newUuidPerExecution)
54  			attachment.setUuid(UUID.randomUUID().toString());
55  		attachmentUploader.upload(attachment, resource);
56  		for (AttachmentsEnabled attachmentsEnabled : attachTo) {
57  			attachmentsEnabled.addAttachment(attachment);
58  		}
59  	}
60  
61  	public void setAttachmentUploader(AttachmentUploader attachmentUploader) {
62  		this.attachmentUploader = attachmentUploader;
63  	}
64  
65  	public void setAttachments(Map<Attachment, Resource> attachments) {
66  		this.attachments = attachments;
67  	}
68  
69  	public void setAttachTo(List<AttachmentsEnabled> attachTo) {
70  		this.attachTo = attachTo;
71  	}
72  
73  	public void setAttachment(Attachment attachment) {
74  		this.attachment = attachment;
75  	}
76  
77  	public void setResource(Resource resource) {
78  		this.resource = resource;
79  	}
80  
81  	public void setNewUuidPerExecution(Boolean newUuidPerExecution) {
82  		this.newUuidPerExecution = newUuidPerExecution;
83  	}
84  
85  }