KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > jsptag > ItemPreviewTag


1 /*
2  * ItemPreviewTag.java
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2006/03/10 20:22:52 $
7  *
8  * Copyright (c) 2002-2006, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.jsptag;
41
42 import org.dspace.app.webui.util.UIUtil;
43
44 import org.dspace.content.Bitstream;
45 import org.dspace.content.Bundle;
46 import org.dspace.content.DCValue;
47 import org.dspace.content.Item;
48
49 import org.dspace.core.ConfigurationManager;
50 import org.dspace.core.Constants;
51 import org.dspace.core.Utils;
52
53 import java.io.IOException JavaDoc;
54 import java.sql.SQLException JavaDoc;
55
56 import javax.servlet.http.HttpServletRequest JavaDoc;
57 import javax.servlet.jsp.JspException JavaDoc;
58 import javax.servlet.jsp.JspWriter JavaDoc;
59 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
60
61 /**
62  * <p>
63  * JSP tag for displaying a preview version of an item. For this tag to
64  * output anything, the preview feature must be activated in DSpace.
65  * </p>
66  *
67  * @author Scott Yeadon
68  * @version $Revision: 1.1 $
69  */

70 public class ItemPreviewTag extends TagSupport JavaDoc
71 {
72     /** Item to display */
73     private Item item;
74
75     public ItemPreviewTag()
76     {
77         super();
78     }
79
80     public int doStartTag() throws JspException JavaDoc
81     {
82         if (!ConfigurationManager.getBooleanProperty("webui.preview.enabled"))
83         {
84             return SKIP_BODY;
85         }
86         try
87         {
88             showPreview();
89         }
90         catch (SQLException JavaDoc sqle)
91         {
92             throw new JspException JavaDoc(sqle);
93         }
94         catch (IOException JavaDoc ioe)
95         {
96             throw new JspException JavaDoc(ioe);
97         }
98
99         return SKIP_BODY;
100     }
101
102     public void setItem(Item itemIn)
103     {
104         item = itemIn;
105     }
106     
107     private void showPreview() throws SQLException JavaDoc, IOException JavaDoc
108     {
109         JspWriter JavaDoc out = pageContext.getOut();
110         
111         // Only shows 1 preview image at the moment (the first encountered) regardless
112
// of the number of bundles/bitstreams of this type
113
Bundle[] bundles = item.getBundles("BRANDED_PREVIEW");
114         
115         if (bundles.length > 0)
116         {
117             Bitstream[] bitstreams = bundles[0].getBitstreams();
118             
119             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
120             out.println("<br/><p align=\"center\">");
121             out.println("<img SRC=\""
122                         + request.getContextPath() + "/retrieve/"
123                         + bitstreams[0].getID() + "/"
124                         + UIUtil.encodeBitstreamName(bitstreams[0].getName(),
125                                   Constants.DEFAULT_ENCODING)
126                         + "\"/>");
127             
128             // Currently only one metadata item supported. Only the first match is taken
129
String JavaDoc s = ConfigurationManager.getProperty("webui.preview.dc");
130             if (s != null)
131             {
132                 DCValue[] dcValue;
133                 
134                 int i = s.indexOf('.');
135                 
136                 if (i == -1)
137                 {
138                     dcValue = item.getDC(s, Item.ANY, Item.ANY);
139                 }
140                 else
141                 {
142                     dcValue = item.getDC(s.substring(0,1), s.substring(i + 1), Item.ANY);
143                 }
144                 
145                 if (dcValue.length > 0)
146                 {
147                     out.println("<br/>" + dcValue[0].value);
148                 }
149             }
150             
151             out.println("</p>");
152         }
153     }
154
155     public void release()
156     {
157         item = null;
158     }
159 }
160
Popular Tags