001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036:
037: package com.sun.portal.app.communityportlets.faces;
038:
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.ArrayList;
042: import java.util.Set;
043: import java.util.Locale;
044: import java.util.logging.Logger;
045: import java.util.logging.Level;
046:
047: import java.net.URI;
048: import java.net.URISyntaxException;
049:
050: import com.sun.portal.log.common.PortalLogger;
051:
052: import com.sun.portal.providers.window.WindowProvider;
053:
054: import com.sun.data.provider.impl.ObjectArrayDataProvider;
055:
056: import com.sun.portal.community.template.TemplateManagerFactory;
057: import com.sun.portal.community.template.TemplateManager;
058: import com.sun.portal.community.template.TemplateDescriptor;
059: import com.sun.portal.community.template.TemplateException;
060:
061: public class TemplateHandler extends CommunityBaseHandler {
062:
063: private static TemplateManager templateManager;
064: private String defaultTemplateId;
065: private TemplateBean[] templateBeans;
066: private ObjectArrayDataProvider templateData;
067: private int templateCount;
068: private static Logger logger = PortalLogger
069: .getLogger(TemplateHandler.class);
070:
071: static {
072: try {
073: templateManager = TemplateManagerFactory.getInstance()
074: .getTemplateManager();
075:
076: } catch (TemplateException te) {
077: logger.log(Level.SEVERE, "PSCPL_CSPACF00400", te);
078: throw new RuntimeException("TemplateHandler.static: ", te);
079: }
080: }
081:
082: public String getDefaultTemplateId() {
083:
084: if (defaultTemplateId == null) {
085: // pick the first one from the list
086: try {
087: defaultTemplateId = (String) templateManager
088: .getAllTemplateIds().iterator().next();
089: } catch (TemplateException te) {
090: logger.log(Level.SEVERE, "PSCPL_CSPACF00401", te);
091: throw new RuntimeException(
092: "TemplateHandler.getDefaultTemplateId(): ", te);
093: }
094: }
095:
096: return defaultTemplateId;
097: }
098:
099: public TemplateBean[] getTemplateBeans() {
100:
101: if (templateBeans == null) {
102:
103: //
104: // get all template id's
105: //
106: Set tids;
107: try {
108: tids = templateManager.getAllTemplateIds();
109: } catch (TemplateException te) {
110: throw new RuntimeException(
111: "TemplateHandler.getTemplateChoices(): failed to get all template ID's",
112: te);
113: }
114:
115: //
116: // create TemplateBeans array
117: //
118: templateBeans = new TemplateBean[tids.size()];
119: int j = 0;
120: for (Iterator i = tids.iterator(); i.hasNext(); j++) {
121: String tid = (String) i.next();
122: TemplateDescriptor td;
123: try {
124: td = templateManager.getTemplateDescriptor(tid,
125: getUserLocale());
126: } catch (TemplateException te) {
127: throw new RuntimeException(
128: "TemplateHandler.getTemplateChoices(): failed to fetch template tid="
129: + tid, te);
130: }
131:
132: String imageURI = normalizeURI(td.getPreviewImageURI());
133:
134: templateBeans[j] = new TemplateBean(td.getId(), td
135: .getName(), td.getDescription(), imageURI);
136: }
137: }
138:
139: return templateBeans;
140: }
141:
142: public ObjectArrayDataProvider getTemplateData() {
143:
144: if (templateData == null) {
145:
146: templateData = new ObjectArrayDataProvider(
147: getTemplateBeans());
148: }
149:
150: return templateData;
151: }
152:
153: public int getTemplateCount() {
154:
155: return getTemplateBeans().length;
156: }
157:
158: public static String getTemplateName(String templateId,
159: Locale locale) {
160:
161: TemplateDescriptor td;
162: try {
163: td = templateManager.getTemplateDescriptor(templateId,
164: locale);
165: } catch (TemplateException te) {
166: throw new RuntimeException(
167: "TemplateHandler.getTemplateName(): failed to retrieve template descriptor for tid="
168: + templateId);
169: }
170:
171: if (td == null) {
172: throw new RuntimeException(
173: "TemplateHandler.getTemplateName(): failed to retrieve template descriptor for tid="
174: + templateId);
175: }
176:
177: return td.getName();
178: }
179:
180: public static String getTemplateDescription(String templateId,
181: Locale locale) {
182:
183: TemplateDescriptor td;
184: try {
185: td = templateManager.getTemplateDescriptor(templateId,
186: locale);
187: } catch (TemplateException te) {
188: throw new RuntimeException(
189: "TemplateHandler.getTemplateDescription(): failed to retrieve template descriptor for tid="
190: + templateId);
191: }
192:
193: if (td == null) {
194: throw new RuntimeException(
195: "TemplateHandler.getTemplateDescription(): failed to retrieve template descriptor for tid="
196: + templateId);
197: }
198:
199: return td.getDescription();
200: }
201:
202: public static String getTemplatePreviewImageURI(String templateId,
203: Locale locale) {
204:
205: TemplateDescriptor td;
206: try {
207: td = templateManager.getTemplateDescriptor(templateId,
208: locale);
209: } catch (TemplateException te) {
210: throw new RuntimeException(
211: "TemplateHandler.getTemplatePreviewImageURI(): failed to retrieve template descriptor for tid="
212: + templateId);
213: }
214:
215: if (td == null) {
216: throw new RuntimeException(
217: "TemplateHandler.getTemplatePreviewImageURI(): failed to retrieve template descriptor for tid="
218: + templateId);
219: }
220:
221: return normalizeURI(td.getPreviewImageURI());
222: }
223:
224: /**
225: * returns the URI for accessing static resources contained in the
226: * portal web-app (i.e. images)
227: * - for absolute URI, do nothing
228: * - for relative URI, prepend "/.." + portal context
229: */
230: private static String normalizeURI(String uri) {
231:
232: URI u = null;
233: try {
234: u = new URI(uri);
235: } catch (URISyntaxException ue) {
236: throw new RuntimeException(
237: "TemplateHander.normalizeURI(): failed to construct URI from uri="
238: + uri, ue);
239: }
240:
241: // if uri is not absolute, qualify it by prepending
242: // "/.." + portal contextpath
243: if (!u.isAbsolute()) {
244: String portalCxt = (String) getHttpServletRequest()
245: .getAttribute(WindowProvider.PORTAL_CONTEXT_PATH);
246: uri = "/.." + portalCxt + (uri.startsWith("/") ? "" : "/")
247: + uri;
248: }
249: return uri;
250: }
251:
252: public class TemplateBean {
253:
254: private String id;
255: private String name;
256: private String description;
257: private String previewImageURI;
258:
259: protected TemplateBean(String id, String name,
260: String description, String previewImageURI) {
261: this .id = id;
262: this .name = name;
263: this .description = description;
264: this .previewImageURI = previewImageURI;
265: }
266:
267: public String getId() {
268: return id;
269: }
270:
271: public String getName() {
272: return name;
273: }
274:
275: public String getDescription() {
276: return description;
277: }
278:
279: public String getPreviewImageURI() {
280: return previewImageURI;
281: }
282: }
283: }
|