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.community.template.test;
038:
039: import java.util.Iterator;
040: import java.util.Set;
041: import java.util.Map;
042: import java.util.HashMap;
043: import java.util.Properties;
044: import java.util.Locale;
045: import java.util.StringTokenizer;
046:
047: import com.sun.portal.community.mc.CMCRolePrincipal;
048:
049: import com.sun.portal.community.template.Template;
050: import com.sun.portal.community.template.TemplateDescriptor;
051: import com.sun.portal.community.template.TemplateManager;
052: import com.sun.portal.community.template.TemplateManagerFactory;
053: import com.sun.portal.community.template.impl.CommunityTemplateManagerImpl;
054:
055: public class Test {
056:
057: public static final String COMMUNITY_CONTAINER_SUFFIX = "_Container";
058:
059: public static final String TOKEN_COMMUNITY_NAME = "%COMMUNITY_NAME%";
060: public static final String TOKEN_COMMUNITY_DESCRIPTION = "%COMMUNITY_DESCRIPTION%";
061: public static final String TOKEN_COMMUNITY_CONTAINER = "%COMMUNITY_CONTAINER%";
062: public static final String TOKEN_COMMUNITY_DP_PRIORITY = "%COMMUNITY_DP_PRIORITY%";
063:
064: private TemplateManager _tm = null;
065:
066: /** Creates a new instance of Test */
067: public Test() throws Exception {
068:
069: //
070: // get a handle on the TemplateManager
071: //
072: _tm = new CommunityTemplateManagerImpl();
073: _tm.init("myPortal");
074:
075: }
076:
077: protected Set getTemplateIds() throws Exception {
078:
079: return _tm.getAllTemplateIds();
080: }
081:
082: protected String getTemplates(String containerName)
083: throws Exception {
084:
085: StringBuffer buf = new StringBuffer();
086: Set templateIds = _tm.getAllTemplateIds();
087: for (Iterator i = templateIds.iterator(); i.hasNext();) {
088: String templateId = (String) i.next();
089: buf.append("----------------------\n");
090: buf.append(templateId + "\n");
091: buf.append("----------------------\n");
092: buf.append(getTemplate(containerName, templateId));
093: buf.append("\n\n");
094: }
095:
096: return buf.toString();
097: }
098:
099: protected String getTemplate(String containerName, String templateId)
100: throws Exception {
101:
102: Map tokenMappings = getTokenMappings(containerName,
103: "some description");
104: Template t = _tm.getTemplate(templateId, tokenMappings);
105:
106: return t.toString();
107:
108: }
109:
110: protected String getTemplate(String containerName,
111: String templateId, String localeStr) throws Exception {
112:
113: Locale locale = getLocale(localeStr);
114:
115: Map tokenMappings = getTokenMappings(containerName,
116: "some description");
117: Template t = _tm.getTemplate(templateId, tokenMappings);
118: TemplateDescriptor td = _tm.getTemplateDescriptor(templateId,
119: locale);
120:
121: StringBuffer buf = new StringBuffer();
122: buf.append("----------------------\n");
123: buf.append("ID: " + templateId + "\n");
124: buf.append("NAME: " + td.getName() + "\n");
125: buf.append("DESC: " + td.getDescription() + "\n");
126: buf.append("TOKENS: " + td.getTokens() + "\n");
127: buf.append("PREVIEW: " + td.getPreviewImageURI() + "\n");
128: buf.append("----------------------\n");
129: buf.append(t);
130: buf.append("\n\n");
131:
132: return buf.toString();
133:
134: }
135:
136: protected String getRoleTemplate(String containerName,
137: String templateId, String role) throws Exception {
138:
139: Map tokenMappings = getTokenMappings(containerName,
140: "some description");
141: Template t = _tm.getTemplate(templateId, tokenMappings);
142: Map roleTemplates = t.getRoleTemplates();
143: CMCRolePrincipal rPrincipal = CMCRolePrincipal.valueOf(role);
144: if (rPrincipal == null) {
145: throw new Exception("Test.getRoleTemplate(): invalid role="
146: + role);
147: }
148:
149: return (String) roleTemplates.get(rPrincipal);
150: }
151:
152: protected Map getTokenMappings(String name, String description) {
153:
154: Map tokenMappings = new HashMap();
155:
156: Properties tokenMapping = new Properties();
157: tokenMapping.setProperty(TOKEN_COMMUNITY_NAME, name);
158: tokenMapping.setProperty(TOKEN_COMMUNITY_DESCRIPTION,
159: description);
160: tokenMapping.setProperty(TOKEN_COMMUNITY_CONTAINER, name
161: + COMMUNITY_CONTAINER_SUFFIX);
162:
163: tokenMapping.setProperty(TOKEN_COMMUNITY_DP_PRIORITY, "1000");
164: tokenMappings.put(CMCRolePrincipal.VISITOR_ROLE.toString(),
165: tokenMapping);
166:
167: tokenMapping = (Properties) tokenMapping.clone();
168: tokenMapping.setProperty(TOKEN_COMMUNITY_DP_PRIORITY, "1005");
169: tokenMappings.put(CMCRolePrincipal.MEMBER_ROLE.toString(),
170: tokenMapping);
171:
172: tokenMapping = (Properties) tokenMapping.clone();
173: tokenMapping.setProperty(TOKEN_COMMUNITY_DP_PRIORITY, "1010");
174: tokenMappings.put(CMCRolePrincipal.OWNER_ROLE.toString(),
175: tokenMapping);
176:
177: return tokenMappings;
178: }
179:
180: protected Locale getLocale(String localeStr) {
181:
182: Locale locale = null;
183: StringTokenizer st = new StringTokenizer(localeStr, "_");
184:
185: if (st.hasMoreTokens()) {
186: String lang = st.nextToken();
187: if (st.hasMoreTokens()) {
188: String country = st.nextToken();
189: if (st.hasMoreTokens()) {
190: String variant = st.nextToken();
191: locale = new Locale(lang, country, variant);
192: } else {
193: locale = new Locale(lang, country);
194: }
195: } else {
196: locale = new Locale(lang);
197: }
198: }
199:
200: return locale;
201: }
202:
203: protected static void printUsage() {
204: System.out
205: .println("Usage: test [<cotainer_name>] [<template_id>] [<locale>] [<role>]");
206: }
207:
208: /**
209: * @param args the command line arguments
210: */
211: public static void main(String[] args) {
212:
213: try {
214: if (args.length == 0) {
215: Test t = new Test();
216: System.out.println(t.getTemplateIds());
217: } else if (args.length == 1) {
218: if (args[0].equals("help") || args[0].equals("-h")
219: || args[0].equals("-help")) {
220: printUsage();
221: } else {
222: Test t = new Test();
223: System.out.println(t.getTemplates(args[0]));
224: }
225: } else if (args.length == 2) {
226: Test t = new Test();
227: System.out.println(t.getTemplate(args[0], args[1]));
228: } else if (args.length == 3) {
229: Test t = new Test();
230: System.out.println(t.getTemplate(args[0], args[1],
231: args[2]));
232: } else if (args.length == 4) {
233: Test t = new Test();
234: System.out.println(t.getRoleTemplate(args[0], args[1],
235: args[3]));
236: } else {
237: printUsage();
238: }
239: } catch (Exception ex) {
240: ex.printStackTrace();
241: }
242: }
243:
244: }
|