KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > taglib > content > DigitalAssetParameterTag


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.deliver.taglib.content;
24
25 import java.io.File JavaDoc;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.JspTagException JavaDoc;
29
30 import org.apache.commons.fileupload.FileItem;
31 import org.infoglue.cms.io.FileHelper;
32 import org.infoglue.cms.webservices.elements.RemoteAttachment;
33 import org.infoglue.deliver.taglib.AbstractTag;
34
35 /**
36  * This class implements the <content:digitalAssetParameter> tag, which adds an asset
37  * to the list of assets in the contentVersion.
38  *
39  * Note! This tag must have a <content:contentVersionParameter> ancestor.
40  */

41
42 public class DigitalAssetParameterTag extends AbstractTag
43 {
44     /**
45      * The universal version identifier.
46      */

47     private static final long serialVersionUID = 4482006814634520239L;
48
49     /**
50      * The assetKey of the parameter.
51      */

52     private String JavaDoc assetKey;
53
54     /**
55      * The fileName of the parameter.
56      */

57     private String JavaDoc fileName;
58
59     /**
60      * The filePath of the parameter.
61      */

62     private String JavaDoc filePath;
63
64     /**
65      * The contentType of the parameter.
66      */

67     private String JavaDoc contentType;
68
69     /**
70      * The bytes of the parameter.
71      */

72     private byte[] bytes;
73
74     /**
75      * The file.
76      */

77     private File JavaDoc file;
78     
79     /**
80      * The file item.
81      */

82     private FileItem fileItem;
83     
84     /**
85      * Default constructor.
86      */

87     public DigitalAssetParameterTag()
88     {
89         super();
90     }
91
92     /**
93      * Adds a parameter with the specified name and value to the parameters
94      * of the parent tag.
95      *
96      * @return indication of whether to continue evaluating the JSP page.
97      * @throws JspException if an error occurred while processing this tag.
98      */

99     public int doEndTag() throws JspException JavaDoc
100     {
101         addDigitalAsset();
102         return EVAL_PAGE;
103     }
104     
105     /**
106      * Adds the digital asset to the ancestor tag.
107      *
108      * @throws JspException if the ancestor tag isn't a content version tag.
109      */

110     protected void addDigitalAsset() throws JspException JavaDoc
111     {
112         final ContentVersionParameterInterface parent = (ContentVersionParameterInterface) findAncestorWithClass(this, ContentVersionParameterInterface.class);
113         if(parent == null)
114         {
115             throw new JspTagException JavaDoc("DigitalAssetParameterTag must have a ContentVersionParameterInterface ancestor.");
116         }
117         
118         if(bytes != null)
119         {
120         }
121         else if(file != null)
122         {
123             try
124             {
125                 this.bytes = FileHelper.getFileBytes(file);
126             }
127             catch (Exception JavaDoc e)
128             {
129                 e.printStackTrace();
130             }
131         }
132         else if(fileItem != null)
133         {
134             try
135             {
136                 this.bytes = fileItem.get();
137             }
138             catch (Exception JavaDoc e)
139             {
140                 e.printStackTrace();
141             }
142         }
143         else
144         {
145             throw new JspException JavaDoc("Must state either bytes, a file or a fileItem");
146         }
147         
148         RemoteAttachment attachment = new RemoteAttachment(this.assetKey, this.fileName, this.filePath, this.contentType, this.bytes);
149         
150         ((ContentVersionParameterInterface) parent).addDigitalAsset(attachment);
151     }
152     
153     /**
154      * Sets the assetKey attribute.
155      *
156      * @param assetKey the assetKey to use.
157      * @throws JspException if an error occurs while evaluating name parameter.
158      */

159     public void setAssetKey(final String JavaDoc assetKey) throws JspException JavaDoc
160     {
161         this.assetKey = evaluateString("parameter", "assetKey", assetKey);
162     }
163
164     /**
165      * Sets the fileName attribute.
166      *
167      * @param fileName the fileName to use.
168      * @throws JspException if an error occurs while evaluating name parameter.
169      */

170     public void setFileName(final String JavaDoc fileName) throws JspException JavaDoc
171     {
172         this.fileName = evaluateString("parameter", "fileName", fileName);
173     }
174
175     /**
176      * Sets the filePath attribute.
177      *
178      * @param filePath the filePath to use.
179      * @throws JspException if an error occurs while evaluating name parameter.
180      */

181     public void setFilePath(final String JavaDoc filePath) throws JspException JavaDoc
182     {
183         this.filePath = evaluateString("parameter", "filePath", filePath);
184     }
185
186     /**
187      * Sets the contentType attribute.
188      *
189      * @param contentType the contentType to use.
190      * @throws JspException if an error occurs while evaluating name parameter.
191      */

192     public void setContentType(final String JavaDoc contentType) throws JspException JavaDoc
193     {
194         this.contentType = evaluateString("parameter", "contentType", contentType);
195     }
196
197     /**
198      * Sets the bytes attribute to the specified evaluated bytes.
199      *
200      * @param name the contents of the attachment.
201      * @throws JspException if an error occurs while evaluating the bytes.
202      */

203     public void setBytes(final String JavaDoc bytes) throws JspException JavaDoc
204     {
205         this.bytes = (byte[]) evaluate("addAttachment", "bytes", bytes, Object JavaDoc.class);
206     }
207
208     /**
209      * Sets the file attribute to the specified evaluated file.
210      *
211      * @param file the contents of the attachment.
212      * @throws JspException if an error occurs while evaluating the file.
213      */

214     public void setFile(final String JavaDoc file) throws JspException JavaDoc
215     {
216         this.file = (File JavaDoc) evaluate("addAttachment", "file", file, Object JavaDoc.class);
217     }
218
219     /**
220      * Sets the FileItem attribute to the specified evaluated fileItem.
221      *
222      * @param fileItem the contents of the attachment.
223      * @throws JspException if an error occurs while evaluating the fileItem.
224      */

225     public void setFileItem(final String JavaDoc fileItem) throws JspException JavaDoc
226     {
227         this.fileItem = (FileItem) evaluate("addAttachment", "fileItem", fileItem, Object JavaDoc.class);
228     }
229
230 }
231
Popular Tags