001: /*
002: * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portlet.portletadmin;
020:
021: import java.io.IOException;
022: import java.util.HashMap;
023: import java.util.Hashtable;
024: import java.util.Vector;
025:
026: import javax.portlet.ActionRequest;
027: import javax.portlet.ActionResponse;
028: import javax.portlet.PortletException;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import com.nabhinc.core.MimeTypes;
033: import com.nabhinc.portal.config.NameType;
034: import com.nabhinc.portal.config.PortletPreferencesType;
035: import com.nabhinc.portal.config.PortletType;
036: import com.nabhinc.portal.config.PreferenceType;
037: import com.nabhinc.portal.config.ValueType;
038: import com.nabhinc.portal.config.impl.NameTypeImpl;
039: import com.nabhinc.portal.config.impl.PortletPreferencesTypeImpl;
040: import com.nabhinc.portal.config.impl.PreferenceTypeImpl;
041: import com.nabhinc.portal.config.impl.ValueTypeImpl;
042: import com.nabhinc.portal.container.PortletRequestImpl;
043: import com.nabhinc.portal.container.PortletResponseImpl;
044: import com.nabhinc.portal.core.PortalServlet;
045: import com.nabhinc.portal.core.PortletConfigInfo;
046: import com.nabhinc.portlet.portletadmin.PortletUtil;
047: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
048: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
049: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
050: import com.nabhinc.util.StringUtil;
051:
052: /**
053: *
054: *
055: * @author Padmanabh Dabke
056: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
057: * @since 2.4.1
058: */
059: public class GenericPortletCreator extends BaseRequestProcessor
060: implements ActionProcessor {
061:
062: /* (non-Javadoc)
063: * @see com.nabhinc.portlet.mvcportlet.core.ActionProcessor#process(javax.portlet.ActionRequest, javax.portlet.ActionResponse, com.nabhinc.portlet.mvcportlet.core.ActionConfig)
064: */
065: public String process(ActionRequest request,
066: ActionResponse response, ActionConfig actionConfig)
067: throws PortletException, IOException {
068:
069: final String VIEW_MODE = "view";
070: final String EDIT_MODE = "edit";
071: final String HELP_MODE = "help";
072: final String ADMIN_MODE = "admin";
073:
074: boolean isEdit = "true".equals(request.getParameter("edit"));
075: String portletIndex = request.getParameter("portlet_index");
076: int pIndex = portletIndex == null ? -1 : Integer
077: .parseInt(portletIndex);
078: String requestType = request.getParameter("request_type");
079:
080: //HTML mime type is supported by default
081: boolean isHTMLMimeTypeSupported = true;
082: boolean isHTMLEditSupported = request
083: .getParameter("html_edit_mode") != null;
084: boolean isHTMLHelpSupported = request
085: .getParameter("html_help_mode") != null;
086: boolean isHTMLAdminSupported = request
087: .getParameter("html_admin_mode") != null;
088: boolean isXHTMLMimeTypeSupported = request
089: .getParameter("xhtml_mime_type") != null;
090: boolean isWMLMimeTypeSupported = request
091: .getParameter("wml_mime_type") != null;
092:
093: String pName = request.getParameter("pname");
094: String pClass = request.getParameter("portlet_class");
095: String title = request.getParameter("title");
096: //String descr = request.getParameter("descr");
097: //String displayName = request.getParameter("display_name");
098: String keywordsStr = request.getParameter("keywords");
099: String shortTitle = request.getParameter("short_title");
100: String expCache = request.getParameter("exp_cache");
101: String resourceBundleName = request
102: .getParameter("resource_bundle");
103: String supportedLocales = request
104: .getParameter("supported_locales");
105: String prefValidatorClassName = request
106: .getParameter("pref_validator");
107:
108: String totalInitParams = request
109: .getParameter("init_params_total");
110: String totalPrefs = request.getParameter("pref_total");
111: String totalSecRoles = request.getParameter("sec_roles_total");
112: String totalDescrLang = request
113: .getParameter("descr_lang_total");
114: String totalDispLang = request
115: .getParameter("display_lang_total");
116:
117: int numOfInitParams = Integer.parseInt(totalInitParams);
118: int numOfPrefs = Integer.parseInt(totalPrefs);
119: int numOfSecRoles = Integer.parseInt(totalSecRoles);
120: int numOfDescrLangs = Integer.parseInt(totalDescrLang);
121: int numOfDispLangs = Integer.parseInt(totalDispLang);
122:
123: String[] totalPrefValues = new String[numOfPrefs];
124: int[] numOfPrefValues = new int[numOfPrefs];
125: for (int i = 0; i < numOfPrefs; i++) {
126: totalPrefValues[i] = request
127: .getParameter("pref_value_total" + i);
128: numOfPrefValues[i] = Integer.parseInt(totalPrefValues[i]);
129: }
130:
131: String portletClassName = "";
132:
133: String error = "";
134: // Check for errors
135: if (pName == null || "".equals(pName.trim())) {
136: error += "You must provide portlet name.<br/>";
137: } else if (PortalServlet.getInstance().isPortletExists(pName)) {
138: if (!isEdit
139: || !pName.equals(((PortletConfigInfo) PortalServlet
140: .getInstance().getPortletConfigInfoList()
141: .elementAt(pIndex)).name)) {
142: error += "Duplicate portlet name.<br/>";
143: }
144: }
145: if (pClass == null || "".equals(pClass.trim())) {
146: error += "You must provide portlet class name.<br/>";
147: }
148: if (title == null || "".equals(title.trim())) {
149: error += "You must provide portlet title.<br/>";
150: }
151: if (expCache != null && !expCache.trim().equals("")) {
152: try {
153: Integer.parseInt(expCache);
154: } catch (Exception ex) {
155: error += "Expiration cache must be a number.<br/>";
156: }
157: }
158:
159: if (!error.equals("")) {
160: response.setRenderParameter("error", error);
161: if (isEdit)
162: response.setRenderParameter("edit", "true");
163: return "failure";
164: }
165:
166: portletClassName = pClass
167: .substring(pClass.lastIndexOf(".") + 1);
168: PortletType pType = PortletUtil
169: .createPortletType(pName, pClass);
170: PortletUtil.setTitle(title, pType);
171: PortletUtil.setKeywords(keywordsStr, pType);
172: PortletUtil.setShortTitle(shortTitle, pType);
173: PortletUtil.setResourceBundle(resourceBundleName, pType);
174:
175: HashMap descrMap = new HashMap();
176: for (int i = 0; i < numOfDescrLangs; i++) {
177: String lang = request.getParameter("descr_lang" + i);
178: String descr = request.getParameter("descr" + i);
179: if (descrMap.containsKey(lang))
180: continue;
181: descrMap.put(lang, descr);
182: }
183: PortletUtil.setDescription(descrMap, pType);
184:
185: HashMap displayMap = new HashMap();
186: for (int i = 0; i < numOfDispLangs; i++) {
187: String lang = request.getParameter("display_lang" + i);
188: String displayName = request.getParameter("display_name"
189: + i);
190: if (displayMap.containsKey(lang))
191: continue;
192: displayMap.put(lang, displayName);
193: }
194: PortletUtil.setDisplayName(displayMap, pType);
195:
196: if (expCache != null && !expCache.trim().equals(""))
197: PortletUtil.setExpirationCache(Integer.parseInt(expCache),
198: pType);
199:
200: Hashtable initParams = new Hashtable();
201: for (int i = 0; i < numOfInitParams; i++) {
202: String initParamName = request.getParameter("init_param"
203: + i);
204: if (initParamName != null
205: && !"".equals(initParamName.trim())) {
206: initParams.put(initParamName, request
207: .getParameter("init_value" + i));
208: }
209: }
210: PortletUtil.setInitParams(initParams, pType);
211:
212: if (supportedLocales != null
213: && !"".equals(supportedLocales.trim())) {
214: String[] locales = StringUtil.split(supportedLocales, ",");
215: PortletUtil.setSupportedLocales(locales, pType);
216: }
217:
218: if (isHTMLMimeTypeSupported) {
219: Vector modeList = new Vector();
220:
221: if (isHTMLEditSupported) {
222: modeList.add(EDIT_MODE);
223: }
224: if (isHTMLHelpSupported) {
225: modeList.add(HELP_MODE);
226: }
227: if (isHTMLAdminSupported) {
228: modeList.add(ADMIN_MODE);
229: }
230:
231: String[] supportedModes = new String[modeList.size()];
232: modeList.copyInto(supportedModes);
233:
234: PortletUtil.setSupports(supportedModes, MimeTypes.HTML,
235: pType);
236: }
237:
238: if (isXHTMLMimeTypeSupported) {
239: String[] supportedModes = new String[0];
240: PortletUtil.setSupports(supportedModes, MimeTypes.XHTML_MP,
241: pType);
242: }
243:
244: if (isWMLMimeTypeSupported) {
245: String[] supportedModes = new String[0];
246: PortletUtil.setSupports(supportedModes, MimeTypes.WML,
247: pType);
248: }
249:
250: Hashtable prefMap = new Hashtable();
251: PortletPreferencesType pPrefType = new PortletPreferencesTypeImpl();
252: boolean isEmpty = true;
253: for (int i = 0; i < numOfPrefs; i++) {
254: String prefName = request.getParameter("pref_name" + i);
255: if (prefName != null && !"".equals(prefName.trim())) {
256: isEmpty = false;
257: PreferenceType prefType = new PreferenceTypeImpl();
258: NameType nameType = new NameTypeImpl();
259: nameType.setValue(prefName);
260: prefType.setName(nameType);
261:
262: Vector prefValueVec = new Vector();
263: for (int j = 0; j < numOfPrefValues[i]; j++) {
264: String prefValue = request
265: .getParameter("pref_value" + i + j);
266: if (prefValue != null && !"".equals(prefValue)) {
267: prefValueVec.add(prefValue);
268: ValueType valueType = new ValueTypeImpl();
269: valueType.setValue(prefValue);
270: prefType.getValue().add(valueType);
271: }
272: }
273: String isReadOnly = request.getParameter("read_only"
274: + i) != null ? "true" : "false";
275: prefType.setReadOnly(isReadOnly);
276:
277: pPrefType.getPreference().add(prefType);
278: }
279: }
280: if (prefValidatorClassName != null
281: && !"".equals(prefValidatorClassName))
282: pPrefType.setPreferencesValidator(prefValidatorClassName);
283:
284: if (!isEmpty)
285: pType.setPortletPreferences(pPrefType);
286:
287: HashMap secRoleMap = new HashMap();
288: for (int i = 0; i < numOfSecRoles; i++) {
289: String roleName = request.getParameter("role_name" + i);
290: if (roleName != null && !"".equals(roleName.trim())) {
291: secRoleMap.put(roleName, request
292: .getParameter("role_link" + i));
293: }
294: }
295: PortletUtil.setSecurityRoleReference(secRoleMap, pType);
296:
297: try {
298: if (isEdit) {
299: HttpServletRequest req = ((PortletRequestImpl) request)
300: .getHttpServletRequest();
301: HttpServletResponse res = ((PortletResponseImpl) response)
302: .getHttpServletResponse();
303: PortalServlet.getInstance().replacePortlet(pType,
304: pIndex, req, res);
305: } else {
306: PortalServlet.getInstance().addPortlet(pType);
307: }
308: PortalServlet.getInstance().savePortletConfiguration();
309: } catch (Exception ex) {
310: throw new PortletException("Failed to create "
311: + portletClassName + " portlet.", ex);
312: }
313:
314: return "success";
315: }
316:
317: }
|