2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.core
.execution
.tasks
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
23 import java
.util
.UUID
;
25 import org
.argeo
.slc
.SlcException
;
26 import org
.argeo
.slc
.core
.attachment
.Attachment
;
27 import org
.argeo
.slc
.core
.attachment
.AttachmentUploader
;
28 import org
.argeo
.slc
.core
.attachment
.AttachmentsEnabled
;
29 import org
.springframework
.core
.io
.Resource
;
31 public class UploadAttachments
implements Runnable
{
32 private AttachmentUploader attachmentUploader
;
33 private Attachment attachment
= null;
34 private Resource resource
= null;
35 private Map
<Attachment
, Resource
> attachments
= new HashMap
<Attachment
, Resource
>();
36 private List
<AttachmentsEnabled
> attachTo
= new ArrayList
<AttachmentsEnabled
>();
37 private Boolean newUuidPerExecution
= true;
40 if (attachment
!= null) {
42 throw new SlcException("A resource must be specified.");
43 uploadAndAdd(attachment
, resource
);
46 for (Attachment attachmentT
: attachments
.keySet()) {
47 Resource resourceT
= attachments
.get(attachmentT
);
48 uploadAndAdd(attachmentT
, resourceT
);
53 protected void uploadAndAdd(Attachment attachment
, Resource resource
) {
54 if (newUuidPerExecution
)
55 attachment
.setUuid(UUID
.randomUUID().toString());
56 attachmentUploader
.upload(attachment
, resource
);
57 for (AttachmentsEnabled attachmentsEnabled
: attachTo
) {
58 attachmentsEnabled
.addAttachment(attachment
);
62 public void setAttachmentUploader(AttachmentUploader attachmentUploader
) {
63 this.attachmentUploader
= attachmentUploader
;
66 public void setAttachments(Map
<Attachment
, Resource
> attachments
) {
67 this.attachments
= attachments
;
70 public void setAttachTo(List
<AttachmentsEnabled
> attachTo
) {
71 this.attachTo
= attachTo
;
74 public void setAttachment(Attachment attachment
) {
75 this.attachment
= attachment
;
78 public void setResource(Resource resource
) {
79 this.resource
= resource
;
82 public void setNewUuidPerExecution(Boolean newUuidPerExecution
) {
83 this.newUuidPerExecution
= newUuidPerExecution
;