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.base.BasePortlet;
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.portlet.table.TablePortlet;
043: import com.nabhinc.util.StringUtil;
044:
045: /**
046: *
047: *
048: * @author Padmanabh Dabke
049: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
050: * @since 2.4.1
051: */
052: public class TablePortletCreator extends BaseRequestProcessor implements
053: ActionProcessor {
054:
055: /* (non-Javadoc)
056: * @see com.nabhinc.portlet.mvcportlet.core.ActionProcessor#process(javax.portlet.ActionRequest, javax.portlet.ActionResponse, com.nabhinc.portlet.mvcportlet.core.ActionConfig)
057: */
058: public String process(ActionRequest request,
059: ActionResponse response, ActionConfig actionConfig)
060: throws PortletException, IOException {
061:
062: final String PORTLET_TYPE = "com.nabhinc.portlet.table.TablePortlet";
063:
064: final String VIEW_MODE = "view";
065: final String EDIT_MODE = "edit";
066: final String HELP_MODE = "help";
067:
068: boolean isEdit = "true".equals(request.getParameter("edit"));
069: String portletIndex = request.getParameter("portlet_index");
070: int pIndex = portletIndex == null ? -1 : Integer
071: .parseInt(portletIndex);
072: String requestType = request.getParameter("request_type");
073:
074: //HTML mime type is supported by default
075: boolean isHTMLMimeTypeSupported = true; //rReq.getParameter("html_mime_type") != null;
076: boolean isHTMLEditSupported = request
077: .getParameter("html_edit_mode") != null;
078: boolean isHTMLHelpSupported = request
079: .getParameter("html_help_mode") != null;
080: boolean isXHTMLMimeTypeSupported = true;
081: boolean isWMLMimeTypeSupported = false;
082:
083: boolean isInsertUser = false;
084: boolean isIncludeName = false;
085: boolean isAscending = true;
086:
087: final String[] SQL_TYPES = { "", "VARCHAR", "INTEGER",
088: "DECIMAL", "NUMERIC", "BOOLEAN", "SMALLINT", "DATE",
089: "TIME", "TIMESTAMP", "FLOAT", "DOUBLE", "ARRAY",
090: "BIGINT", "BINARY", "BIT", "BLOB", "CHAR", "CLOB",
091: "LONGVARBINARY", "LONGVARCHAR", "JAVA_OBJECT", "OTHER" };
092: final String[] SQL_TYPE_LABELS = { "Select sql type",
093: "VARCHAR", "INTEGER", "DECIMAL", "NUMERIC", "BOOLEAN",
094: "SMALLINT", "DATE", "TIME", "TIMESTAMP", "FLOAT",
095: "DOUBLE", "ARRAY", "BIGINT", "BINARY", "BIT", "BLOB",
096: "CHAR", "CLOB", "LONG VARBINARY", "LONG VARCHAR",
097: "Java Object", "Other Type" };
098:
099: String pName = request.getParameter("pname");
100: String title = request.getParameter("title");
101: String keywordsStr = request.getParameter("keywords");
102: String shortTitle = request.getParameter("short_title");
103: String expCache = request.getParameter("exp_cache");
104: String resourceBundleName = request
105: .getParameter("resource_bundle");
106: String supportedLocales = request
107: .getParameter("supported_locales");
108:
109: String totalSecRoles = request.getParameter("sec_roles_total");
110: String totalDescrLang = request
111: .getParameter("descr_lang_total");
112: String totalDispLang = request
113: .getParameter("display_lang_total");
114:
115: int numOfSecRoles = Integer.parseInt(totalSecRoles);
116: int numOfDescrLangs = Integer.parseInt(totalDescrLang);
117: int numOfDispLangs = Integer.parseInt(totalDispLang);
118:
119: //datasource is the default datasource
120: String dataSource = request.getParameter("data_source");
121:
122: String listSQL = request.getParameter("list_sql");
123: String countSQL = request.getParameter("count_sql");
124: String headers = request.getParameter("headers");
125: String columnNames = request.getParameter("column_names");
126: String columnTypes = request.getParameter("column_types");
127: String columnFormats = request.getParameter("column_formats");
128: String actionSQL = request.getParameter("action_sql");
129: String actionLabel = request.getParameter("action_label");
130: String actionRestrictSQL = request
131: .getParameter("action_restrict_sql");
132: String actionRestrictMsg = request
133: .getParameter("action_restrict_msg");
134: String emptyTableMsg = request.getParameter("empty_table_msg");
135: String sortColumn = request.getParameter("sort_column");
136:
137: String sortType = request.getParameter("sort_type");
138:
139: String linkColumnIndex = request
140: .getParameter("link_column_index");
141: String primaryKeyIndex = request
142: .getParameter("primary_key_index");
143: String primaryKeyType = request
144: .getParameter("primary_key_type");
145: String rowDisplayPagePath = request
146: .getParameter("row_display_page_path");
147: String rowDisplayPageExt = request
148: .getParameter("row_display_page_ext");
149: String createPagePath = request
150: .getParameter("create_page_path");
151: String createLabel = request.getParameter("create_label");
152: String searchPagePath = request
153: .getParameter("search_page_path");
154: String searchLabel = request.getParameter("search_label");
155:
156: //preference
157: String rowsPerPage = request.getParameter("rows_per_page");
158:
159: //check box
160: String insertUser = request.getParameter("insert_user");
161:
162: String insertParamName = request
163: .getParameter("insert_param_name");
164: String insertParamValue = request
165: .getParameter("insert_param_value");
166: String insertParamType = request
167: .getParameter("insert_param_type");
168:
169: String includeName = request.getParameter("include_name");
170:
171: String linkIDParamName = request
172: .getParameter("link_id_param_name");
173: String linkNameParamName = request
174: .getParameter("link_name_param_name");
175: String linkExcludedValues = request
176: .getParameter("link_excluded_values");
177: String linkNewWindow = request.getParameter("link_new_window");
178: String linkWindowHeight = request
179: .getParameter("link_window_height");
180: String linkWindowWidth = request
181: .getParameter("link_window_width");
182: String rowStateColumnIndex = request
183: .getParameter("row_state_column_index");
184: String tableDisplayPage = request
185: .getParameter("table_display_page");
186: String errorPage = request.getParameter("error_page");
187: String setIDPage = request.getParameter("set_id_page");
188: String imageFolder = request.getParameter("image_folder");
189:
190: String editPath = request.getParameter("edit_path");
191: String helpPath = request.getParameter("help_path");
192:
193: String error = "";
194:
195: if (pName == null || "".equals(pName.trim())) {
196: error += "You must provide portlet name.<br/>";
197: } else if (PortalServlet.getInstance().isPortletExists(pName)) {
198: if (!isEdit
199: || !pName.equals(((PortletConfigInfo) PortalServlet
200: .getInstance().getPortletConfigInfoList()
201: .elementAt(pIndex)).name)) {
202: error += "Duplicate portlet name.<br/>";
203: }
204: }
205: if (title == null || "".equals(title.trim())) {
206: error += "You must provide portlet title.<br/>";
207: }
208: if (listSQL == null || listSQL.trim().equals("")) {
209: error += "Missing list SQL.<br/>";
210: }
211: if (countSQL == null || countSQL.trim().equals("")) {
212: error += "Missing count SQL.<br/>";
213: }
214: if (headers == null || headers.trim().equals("")) {
215: error += "Missing table headers.<br/>";
216: }
217: if (dataSource == null || dataSource.trim().equals("")) {
218: error += "Missing data source.<br/>";
219: }
220:
221: if (linkColumnIndex != null
222: && !linkColumnIndex.trim().equals("")) {
223: try {
224: Integer.parseInt(linkColumnIndex);
225:
226: } catch (Exception ex) {
227: error += "Link column index has to be a number.<br/>";
228: }
229: }
230:
231: if (primaryKeyIndex != null
232: && !primaryKeyIndex.trim().equals("")) {
233: try {
234: Integer.parseInt(primaryKeyIndex);
235:
236: } catch (Exception ex) {
237: error += "Primary Key index has to be a number.<br/>";
238: }
239: }
240:
241: if (rowsPerPage != null && !rowsPerPage.trim().equals("")) {
242: try {
243: Integer.parseInt(rowsPerPage);
244:
245: } catch (Exception ex) {
246: error += "Rows per page has to be a number.<br/>";
247: }
248: }
249: if (expCache != null && !expCache.trim().equals("")) {
250: try {
251: Integer.parseInt(expCache);
252: } catch (Exception ex) {
253: error += "Expiration cache must be a number.<br/>";
254: }
255: }
256:
257: if (!error.equals("")) {
258: response.setRenderParameter("error", error);
259: if (isEdit)
260: response.setRenderParameter("edit", "true");
261: return "failure";
262: }
263:
264: PortletType pType = PortletUtil.createPortletType(pName,
265: PORTLET_TYPE);
266: PortletUtil.setTitle(title, pType);
267: PortletUtil.setShortTitle(shortTitle, pType);
268: PortletUtil.setResourceBundle(resourceBundleName, pType);
269:
270: HashMap descrMap = new HashMap();
271: for (int i = 0; i < numOfDescrLangs; i++) {
272: String lang = request.getParameter("descr_lang" + i);
273: String descr = request.getParameter("descr" + i);
274: if (descrMap.containsKey(lang))
275: continue;
276: descrMap.put(lang, descr);
277: }
278: PortletUtil.setDescription(descrMap, pType);
279:
280: HashMap displayMap = new HashMap();
281: for (int i = 0; i < numOfDispLangs; i++) {
282: String lang = request.getParameter("display_lang" + i);
283: String displayName = request.getParameter("display_name"
284: + i);
285: if (displayMap.containsKey(lang))
286: continue;
287: displayMap.put(lang, displayName);
288: }
289: PortletUtil.setDisplayName(displayMap, pType);
290:
291: if (expCache != null && !expCache.trim().equals(""))
292: PortletUtil.setExpirationCache(Integer.parseInt(expCache),
293: pType);
294:
295: Hashtable initParams = new Hashtable();
296:
297: if (editPath != null && !editPath.trim().equals("")) {
298: if (editPath.indexOf("://") == -1
299: && !editPath.startsWith("/")) {
300: editPath = "/" + editPath;
301: }
302: initParams.put(BasePortlet.EDIT_JSP_PARAM, editPath);
303: isHTMLEditSupported = true;
304: }
305:
306: if (helpPath != null && !helpPath.trim().equals("")) {
307: if (helpPath.indexOf("://") == -1
308: && !helpPath.startsWith("/")) {
309: helpPath = "/" + helpPath;
310: }
311: initParams.put(BasePortlet.HELP_JSP_PARAM, helpPath);
312: isHTMLHelpSupported = true;
313: }
314:
315: initParams.put(TablePortlet.LIST_SQL_INIT_PARAM, listSQL);
316: initParams.put(TablePortlet.COUNT_SQL_INIT_PARAM, countSQL);
317: initParams.put(TablePortlet.HEADERS_INIT_PARAM, headers);
318: initParams.put(TablePortlet.DATA_SOURCE_INIT_PARAM, dataSource);
319: addInitParameter(initParams,
320: TablePortlet.COLUMN_NAMES_INIT_PARAM, columnNames);
321: addInitParameter(initParams,
322: TablePortlet.COLUMN_TYPES_INIT_PARAM, columnTypes);
323: addInitParameter(initParams,
324: TablePortlet.COLUMN_FORMATS_INIT_PARAM, columnFormats);
325: addInitParameter(initParams,
326: TablePortlet.ACTION_SQL_INIT_PARAM, actionSQL);
327: addInitParameter(initParams,
328: TablePortlet.ACTION_LABEL_INIT_PARAM, actionLabel);
329: addInitParameter(initParams,
330: TablePortlet.ACTION_RESTRICT_SQL_INIT_PARAM,
331: actionRestrictSQL);
332: addInitParameter(initParams,
333: TablePortlet.ACTION_RESTRICT_MSG_INIT_PARAM,
334: actionRestrictMsg);
335: addInitParameter(initParams,
336: TablePortlet.EMPTY_TABLE_MSG_INIT_PARAM, emptyTableMsg);
337: addInitParameter(initParams,
338: TablePortlet.SORT_COLUMN_INIT_PARAM, sortColumn);
339: if (isAscending)
340: initParams.put(TablePortlet.SORT_TYPE_INIT_PARAM,
341: TablePortlet.SORT_TYPE_ASCENDING);
342: else
343: initParams.put(TablePortlet.SORT_TYPE_INIT_PARAM,
344: TablePortlet.SORT_TYPE_DESCENDING);
345: addInitParameter(initParams,
346: TablePortlet.LINK_COLUMN_INDEX_INIT_PARAM,
347: linkColumnIndex);
348: addInitParameter(initParams,
349: TablePortlet.PRIMARY_KEY_INDEX_INIT_PARAM,
350: primaryKeyIndex);
351: addInitParameter(initParams,
352: TablePortlet.PRIMARY_KEY_TYPE_INIT_PARAM,
353: primaryKeyType);
354: addInitParameter(initParams,
355: TablePortlet.ROW_DISPLAY_PAGE_INIT_PARAM,
356: rowDisplayPagePath);
357: addInitParameter(initParams,
358: TablePortlet.ROW_DISPLAY_PAGE_EXT_INIT_PARAM,
359: rowDisplayPageExt);
360: addInitParameter(initParams,
361: TablePortlet.CREATE_PAGE_INIT_PARAM, createPagePath);
362: addInitParameter(initParams,
363: TablePortlet.CREATE_LABEL_INIT_PARAM, createLabel);
364: addInitParameter(initParams,
365: TablePortlet.SEARCH_PAGE_INIT_PARAM, searchPagePath);
366: addInitParameter(initParams,
367: TablePortlet.SEARCH_LABEL_INIT_PARAM, searchLabel);
368: if (isInsertUser)
369: initParams.put(TablePortlet.INSERT_USER_INIT_PARAM, "true");
370: addInitParameter(initParams,
371: TablePortlet.INSERT_PARAM_NAME_INIT_PARAM,
372: insertParamName);
373: addInitParameter(initParams,
374: TablePortlet.INSERT_PARAM_VALUE_INIT_PARAM,
375: insertParamValue);
376: addInitParameter(initParams,
377: TablePortlet.INSERT_PARAM_TYPE_INIT_PARAM,
378: insertParamType);
379: if (isIncludeName)
380: initParams
381: .put(TablePortlet.INCLUDE_NAME_INIT_PARAM, "true");
382: addInitParameter(initParams,
383: TablePortlet.LINK_ID_PARAM_NAME_INIT_PARAM,
384: linkIDParamName);
385: addInitParameter(initParams,
386: TablePortlet.LINK_NAME_PARAM_NAME_INIT_PARAM,
387: linkNameParamName);
388: addInitParameter(initParams,
389: TablePortlet.LINK_EXCLUDED_VALUES_INIT_PARAM,
390: linkExcludedValues);
391: addInitParameter(initParams,
392: TablePortlet.LINK_NEW_WINDOW_INIT_PARAM, linkNewWindow);
393: addInitParameter(initParams,
394: TablePortlet.LINK_WINDOW_HEIGHT_INIT_PARAM,
395: linkWindowHeight);
396: addInitParameter(initParams,
397: TablePortlet.LINK_WINDOW_WIDTH_INIT_PARAM,
398: linkWindowWidth);
399: addInitParameter(initParams,
400: TablePortlet.ROW_STATE_COLUMN_INDEX_INIT_PARAM,
401: rowStateColumnIndex);
402: addInitParameter(initParams,
403: TablePortlet.TABLE_DISPLAY_PAGE_INIT_PARAM,
404: tableDisplayPage);
405: addInitParameter(initParams,
406: TablePortlet.ERROR_PAGE_INIT_PARAM, errorPage);
407: addInitParameter(initParams,
408: TablePortlet.SET_ID_PAGE_INIT_PARAM, setIDPage);
409: addInitParameter(initParams,
410: TablePortlet.IMAGE_FOLDER_INIT_PARAM, imageFolder);
411:
412: PortletUtil.setInitParams(initParams, pType);
413:
414: Hashtable prefMap = new Hashtable();
415: if (rowsPerPage != null && !"".equals(rowsPerPage.trim())) {
416: ArrayList valueList1 = new ArrayList();
417:
418: valueList1.add(rowsPerPage);
419: prefMap.put(TablePortlet.ROWS_PER_PAGE_PREF, valueList1);
420: }
421:
422: PortletUtil.setPreferences(prefMap, pType);
423: PortletUtil.setKeywords(keywordsStr, pType);
424:
425: if (isHTMLMimeTypeSupported) {
426: String[] supportedModes = null;
427: if (isHTMLEditSupported && isHTMLHelpSupported) {
428: supportedModes = new String[] { EDIT_MODE, HELP_MODE };
429:
430: } else if (isHTMLEditSupported) {
431: supportedModes = new String[] { EDIT_MODE };
432:
433: } else if (isHTMLHelpSupported) {
434: supportedModes = new String[] { HELP_MODE };
435:
436: }
437: PortletUtil.setSupports(supportedModes, MimeTypes.HTML,
438: pType);
439: }
440:
441: if (isXHTMLMimeTypeSupported) {
442: String[] supportedModes = null;
443: PortletUtil.setSupports(supportedModes, MimeTypes.XHTML_MP,
444: pType);
445: }
446:
447: if (isWMLMimeTypeSupported) {
448: String[] supportedModes = null;
449: PortletUtil.setSupports(supportedModes, MimeTypes.WML,
450: pType);
451: }
452:
453: if (supportedLocales != null
454: && !"".equals(supportedLocales.trim())) {
455: String[] locales = StringUtil.split(supportedLocales, ",");
456: PortletUtil.setSupportedLocales(locales, pType);
457: }
458:
459: HashMap secRoleMap = new HashMap();
460: for (int i = 0; i < numOfSecRoles; i++) {
461: String roleName = request.getParameter("role_name" + i);
462: if (roleName != null && !"".equals(roleName.trim())) {
463: secRoleMap.put(roleName, request
464: .getParameter("role_link" + i));
465: }
466: }
467: PortletUtil.setSecurityRoleReference(secRoleMap, pType);
468:
469: try {
470: if (isEdit) {
471: HttpServletRequest req = ((PortletRequestImpl) request)
472: .getHttpServletRequest();
473: HttpServletResponse res = ((PortletResponseImpl) response)
474: .getHttpServletResponse();
475: PortalServlet.getInstance().replacePortlet(pType,
476: pIndex, req, res);
477: } else {
478: PortalServlet.getInstance().addPortlet(pType);
479: }
480: PortalServlet.getInstance().savePortletConfiguration();
481:
482: } catch (Exception ex) {
483: throw new PortletException(
484: "Failed to create Table portlet.", ex);
485: }
486: return "success";
487: }
488:
489: private void addInitParameter(Hashtable table, String paramName,
490: String paramValue) {
491: if (paramValue != null) {
492: paramValue = paramValue.trim();
493: if (!paramValue.equals("")) {
494: table.put(paramName, paramValue);
495: }
496: }
497: }
498:
499: }
|