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.ArrayList;
023: import java.util.HashMap;
024: import java.util.Hashtable;
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.PortletType;
034: import com.nabhinc.portal.container.PortletRequestImpl;
035: import com.nabhinc.portal.container.PortletResponseImpl;
036: import com.nabhinc.portal.core.PortalServlet;
037: import com.nabhinc.portal.core.PortletConfigInfo;
038: import com.nabhinc.portlet.portletadmin.PortletUtil;
039: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
040: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
041: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
042: import com.nabhinc.util.StringUtil;
043:
044: /**
045: *
046: *
047: * @author Padmanabh Dabke
048: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
049: * @since 2.4.1
050: */
051: public class InlinePortletCreator extends BaseRequestProcessor
052: implements ActionProcessor {
053:
054: /* (non-Javadoc)
055: * @see com.nabhinc.portlet.mvcportlet.core.ActionProcessor#process(javax.portlet.ActionRequest, javax.portlet.ActionResponse, com.nabhinc.portlet.mvcportlet.core.ActionConfig)
056: */
057: public String process(ActionRequest request,
058: ActionResponse response, ActionConfig actionConfig)
059: throws PortletException, IOException {
060:
061: final String PORTLET_TYPE = "com.nabhinc.portlet.inline.InlinePortlet";
062: final String VIEW_MODE = "view";
063: //final String EDIT_MODE = "edit"; //not supported
064: final String HELP_MODE = "help";
065:
066: boolean isEdit = "true".equals(request.getParameter("edit"));
067: String portletIndex = request.getParameter("portlet_index");
068: int pIndex = portletIndex == null ? -1 : Integer
069: .parseInt(portletIndex);
070: String requestType = request.getParameter("request_type");
071:
072: //HTML mime type is supported by default
073: boolean isHTMLMimeTypeSupported = true; //rReq.getParameter("html_mime_type") != null;
074: boolean isHTMLHelpSupported = request
075: .getParameter("html_help_mode") != null;
076: boolean isXHTMLMimeTypeSupported = true;
077: boolean isWMLMimeTypeSupported = true;
078:
079: String pName = request.getParameter("pname");
080: String title = request.getParameter("title");
081: String keywordsStr = request.getParameter("keywords");
082: String shortTitle = request.getParameter("short_title");
083: String expCache = request.getParameter("exp_cache");
084: String resourceBundleName = request
085: .getParameter("resource_bundle");
086: String supportedLocales = request
087: .getParameter("supported_locales");
088:
089: String totalSecRoles = request.getParameter("sec_roles_total");
090: String totalDescrLang = request
091: .getParameter("descr_lang_total");
092: String totalDispLang = request
093: .getParameter("display_lang_total");
094:
095: int numOfSecRoles = Integer.parseInt(totalSecRoles);
096: int numOfDescrLangs = Integer.parseInt(totalDescrLang);
097: int numOfDispLangs = Integer.parseInt(totalDispLang);
098:
099: String content = request.getParameter("content");
100: String help = request.getParameter("help");
101:
102: String error = "";
103: if (pName == null || "".equals(pName.trim())) {
104: error += "You must provide portlet name.<br/>";
105: } else if (PortalServlet.getInstance().isPortletExists(pName)) {
106: if (!isEdit
107: || !pName.equals(((PortletConfigInfo) PortalServlet
108: .getInstance().getPortletConfigInfoList()
109: .elementAt(pIndex)).name)) {
110: error += "Duplicate portlet name.<br/>";
111: }
112: }
113: if (title == null || "".equals(title.trim())) {
114: error += "You must provide portlet title.<br/>";
115: }
116: if (content == null || "".equals(content.trim())) {
117: error += "You must provide content for this portlet.<br/>";
118: }
119: if (expCache != null && !expCache.trim().equals("")) {
120: try {
121: Integer.parseInt(expCache);
122: } catch (Exception ex) {
123: error += "Expiration cache must be a number.<br/>";
124: }
125: }
126:
127: if (!error.equals("")) {
128: response.setRenderParameter("error", error);
129: if (isEdit)
130: response.setRenderParameter("edit", "true");
131: return "failure";
132: }
133:
134: PortletType pType = PortletUtil.createPortletType(pName,
135: PORTLET_TYPE);
136: PortletUtil.setTitle(title, pType);
137: PortletUtil.setKeywords(keywordsStr, pType);
138: PortletUtil.setShortTitle(shortTitle, pType);
139: PortletUtil.setResourceBundle(resourceBundleName, pType);
140:
141: HashMap descrMap = new HashMap();
142: for (int i = 0; i < numOfDescrLangs; i++) {
143: String lang = request.getParameter("descr_lang" + i);
144: String descr = request.getParameter("descr" + i);
145: if (descrMap.containsKey(lang))
146: continue;
147: descrMap.put(lang, descr);
148: }
149:
150: PortletUtil.setDescription(descrMap, pType);
151:
152: HashMap displayMap = new HashMap();
153: for (int i = 0; i < numOfDispLangs; i++) {
154: String lang = request.getParameter("display_lang" + i);
155: String displayName = request.getParameter("display_name"
156: + i);
157: if (displayMap.containsKey(lang))
158: continue;
159: displayMap.put(lang, displayName);
160: }
161: PortletUtil.setDisplayName(displayMap, pType);
162:
163: if (expCache != null && !expCache.trim().equals(""))
164: PortletUtil.setExpirationCache(Integer.parseInt(expCache),
165: pType);
166:
167: Hashtable prefMap = new Hashtable();
168:
169: ArrayList valueList = new ArrayList();
170: valueList.add(content);
171: prefMap.put("content", valueList);
172:
173: if (help != null && !"".equals(help.trim())) {
174: ArrayList helpValue = new ArrayList();
175:
176: helpValue.add(help);
177: prefMap.put("help", helpValue);
178: isHTMLHelpSupported = true;
179: }
180:
181: PortletUtil.setPreferences(prefMap, pType);
182:
183: if (supportedLocales != null
184: && !"".equals(supportedLocales.trim())) {
185: String[] locales = StringUtil.split(supportedLocales, ",");
186: PortletUtil.setSupportedLocales(locales, pType);
187: }
188:
189: if (isHTMLMimeTypeSupported) {
190: String[] supportedModes = null;
191: if (isHTMLHelpSupported) {
192: supportedModes = new String[] { HELP_MODE };
193: }
194:
195: PortletUtil.setSupports(supportedModes, MimeTypes.HTML,
196: pType);
197: }
198:
199: if (isXHTMLMimeTypeSupported) {
200: String[] supportedModes = null;
201: PortletUtil.setSupports(supportedModes, MimeTypes.XHTML_MP,
202: pType);
203: }
204:
205: if (isWMLMimeTypeSupported) {
206: String[] supportedModes = null;
207: PortletUtil.setSupports(supportedModes, MimeTypes.WML,
208: pType);
209: }
210:
211: HashMap secRoleMap = new HashMap();
212: for (int i = 0; i < numOfSecRoles; i++) {
213: String roleName = request.getParameter("role_name" + i);
214: if (roleName != null && !"".equals(roleName.trim())) {
215: secRoleMap.put(roleName, request
216: .getParameter("role_link" + i));
217: }
218: }
219: PortletUtil.setSecurityRoleReference(secRoleMap, pType);
220:
221: try {
222: if (isEdit) {
223: HttpServletRequest req = ((PortletRequestImpl) request)
224: .getHttpServletRequest();
225: HttpServletResponse res = ((PortletResponseImpl) response)
226: .getHttpServletResponse();
227: PortalServlet.getInstance().replacePortlet(pType,
228: pIndex, req, res);
229: } else {
230: PortalServlet.getInstance().addPortlet(pType);
231: }
232: PortalServlet.getInstance().savePortletConfiguration();
233: } catch (Exception ex) {
234: throw new PortletException(
235: "Failed to create Inline portlet.", ex);
236: }
237: return "success";
238: }
239:
240: }
|