KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > function > Attach


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.cms.applications.workflowtool.function;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.infoglue.cms.applications.workflowtool.condition.InfoglueCondition;
33 import org.infoglue.cms.applications.workflowtool.function.email.Attachment;
34 import org.infoglue.cms.controllers.kernel.impl.simple.DigitalAssetController;
35 import org.infoglue.cms.entities.content.DigitalAssetVO;
36 import org.infoglue.cms.util.CmsPropertyHandler;
37
38 import com.opensymphony.workflow.WorkflowException;
39
40 /**
41  *
42  */

43 public class Attach extends ContentFunction
44 {
45     private final static Logger logger = Logger.getLogger(Attach.class.getName());
46
47     /**
48      *
49      */

50     private static final String JavaDoc ATTACHMENTS_PARAMETER = "attachments";
51
52     /**
53      *
54      */

55     private Collection JavaDoc attachments;
56     
57     /**
58      * Default constructor.
59      */

60     public Attach()
61     {
62         super();
63     }
64
65     /**
66      *
67      */

68     protected void execute() throws WorkflowException
69     {
70         if(getContentVersion() != null)
71         {
72             try
73             {
74                 for(final Iterator JavaDoc i = attachments.iterator(); i.hasNext(); )
75                 {
76                     final Attachment attachment = (Attachment) i.next();
77                     logger.debug("Adding attachment : " + attachment);
78                     DigitalAssetController.create(createDigitalAssetVO(attachment), getInputStream(attachment), getContentVersion(), getDatabase());
79                 }
80             }
81             catch(Exception JavaDoc e)
82             {
83                 throwException(e);
84             }
85         }
86     }
87
88     /**
89      *
90      */

91     private DigitalAssetVO createDigitalAssetVO(final Attachment attachment)
92     {
93         final DigitalAssetVO digitalAssetVO = new DigitalAssetVO();
94         digitalAssetVO.setAssetContentType(attachment.getContentType());
95         digitalAssetVO.setAssetKey(attachment.getName());
96         digitalAssetVO.setAssetFileName(attachment.getName());
97         digitalAssetVO.setAssetFilePath(getDigitalAssetsDirectory());
98         digitalAssetVO.setAssetFileSize(new Integer JavaDoc(attachment.getSize()));
99         return digitalAssetVO;
100     }
101     
102     /**
103      *
104      */

105     private InputStream JavaDoc getInputStream(final Attachment attachment)
106     {
107         return new ByteArrayInputStream JavaDoc(attachment.getBytes());
108     }
109     
110     /**
111      *
112      */

113     private String JavaDoc getDigitalAssetsDirectory()
114     {
115         return CmsPropertyHandler.getDigitalAssetPath();
116     }
117
118     /**
119      * Method used for initializing the function; will be called before <code>execute</code> is called.
120      * <p><strong>Note</strong>! You must call <code>super.initialize()</code> first.</p>
121      *
122      * @throws WorkflowException if an error occurs during the initialization.
123      */

124     protected void initialize() throws WorkflowException
125     {
126         super.initialize();
127         this.attachments = (Collection JavaDoc) getParameter(ATTACHMENTS_PARAMETER, new ArrayList JavaDoc());
128     }
129 }
130
Popular Tags