GenericPortletCreator.java :  » Portal » stringbeans-3.5 » com » nabhinc » portlet » portletadmin » Java Open Source

Java Open Source » Portal » stringbeans 3.5 
stringbeans 3.5 » com » nabhinc » portlet » portletadmin » GenericPortletCreator.java
/* 
 * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc. 
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */
package com.nabhinc.portlet.portletadmin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nabhinc.core.MimeTypes;
import com.nabhinc.portal.config.NameType;
import com.nabhinc.portal.config.PortletPreferencesType;
import com.nabhinc.portal.config.PortletType;
import com.nabhinc.portal.config.PreferenceType;
import com.nabhinc.portal.config.ValueType;
import com.nabhinc.portal.config.impl.NameTypeImpl;
import com.nabhinc.portal.config.impl.PortletPreferencesTypeImpl;
import com.nabhinc.portal.config.impl.PreferenceTypeImpl;
import com.nabhinc.portal.config.impl.ValueTypeImpl;
import com.nabhinc.portal.container.PortletRequestImpl;
import com.nabhinc.portal.container.PortletResponseImpl;
import com.nabhinc.portal.core.PortalServlet;
import com.nabhinc.portal.core.PortletConfigInfo;
import com.nabhinc.portlet.portletadmin.PortletUtil;
import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
import com.nabhinc.util.StringUtil;

/**
 * 
 *
 * @author Padmanabh Dabke
 * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
 * @since 2.4.1
 */
public class GenericPortletCreator extends BaseRequestProcessor implements
    ActionProcessor {

  /* (non-Javadoc)
   * @see com.nabhinc.portlet.mvcportlet.core.ActionProcessor#process(javax.portlet.ActionRequest, javax.portlet.ActionResponse, com.nabhinc.portlet.mvcportlet.core.ActionConfig)
   */
  public String process(ActionRequest request, ActionResponse response,
      ActionConfig actionConfig) throws PortletException, IOException {
      
      final String VIEW_MODE = "view";
      final String EDIT_MODE = "edit";
      final String HELP_MODE = "help";
      final String ADMIN_MODE ="admin";
      
      boolean isEdit = "true".equals(request.getParameter("edit"));
      String portletIndex = request.getParameter("portlet_index");
      int pIndex = portletIndex == null ? -1 : Integer.parseInt(portletIndex) ; 
      String requestType = request.getParameter("request_type");
    
      //HTML mime type is supported by default
      boolean isHTMLMimeTypeSupported = true;    
      boolean isHTMLEditSupported = request.getParameter("html_edit_mode") != null;
      boolean isHTMLHelpSupported = request.getParameter("html_help_mode") != null;
      boolean isHTMLAdminSupported = request.getParameter("html_admin_mode") != null;
      boolean isXHTMLMimeTypeSupported = request.getParameter("xhtml_mime_type") != null;
      boolean isWMLMimeTypeSupported = request.getParameter("wml_mime_type") != null;

      String pName = request.getParameter("pname");
    String pClass = request.getParameter("portlet_class");      
      String title = request.getParameter("title");
      //String descr = request.getParameter("descr");
      //String displayName = request.getParameter("display_name");
      String keywordsStr = request.getParameter("keywords");
      String shortTitle = request.getParameter("short_title");
      String expCache = request.getParameter("exp_cache");
      String resourceBundleName = request.getParameter("resource_bundle");
      String supportedLocales = request.getParameter("supported_locales");
      String prefValidatorClassName = request.getParameter ("pref_validator");
      
      String totalInitParams = request.getParameter("init_params_total");
      String totalPrefs = request.getParameter("pref_total");
      String totalSecRoles = request.getParameter("sec_roles_total");
      String totalDescrLang = request.getParameter("descr_lang_total");
      String totalDispLang = request.getParameter("display_lang_total");
      
      int numOfInitParams = Integer.parseInt(totalInitParams);
      int numOfPrefs = Integer.parseInt(totalPrefs);
      int numOfSecRoles = Integer.parseInt(totalSecRoles);
      int numOfDescrLangs = Integer.parseInt(totalDescrLang);
      int numOfDispLangs = Integer.parseInt(totalDispLang);
      
      String[] totalPrefValues = new String[numOfPrefs];
      int[] numOfPrefValues = new int [numOfPrefs];  
      for (int i = 0; i < numOfPrefs; i++) {
          totalPrefValues[i] = request.getParameter("pref_value_total"+i);
          numOfPrefValues[i] = Integer.parseInt(totalPrefValues[i]);
      }
      
      String portletClassName = "";
      
      String error = "";
        // Check for errors
        if (pName == null || "".equals(pName.trim())) {
            error += "You must provide portlet name.<br/>";
        } else if (PortalServlet.getInstance().isPortletExists(pName)) {
          if (!isEdit || 
            !pName.equals(((PortletConfigInfo)PortalServlet.getInstance().
                getPortletConfigInfoList().elementAt(pIndex)).name)) {  
            error += "Duplicate portlet name.<br/>";
         }
        }
        if (pClass == null || "".equals(pClass.trim())) {
            error += "You must provide portlet class name.<br/>";
        }        
        if (title == null || "".equals(title.trim())) {
            error += "You must provide portlet title.<br/>";
        }
        if (expCache != null && !expCache.trim().equals("")) {
            try {
                Integer.parseInt(expCache);
            } catch (Exception ex) {
                error += "Expiration cache must be a number.<br/>";
            }
        }
        
        if (! error.equals("")) {
          response.setRenderParameter("error", error);
          if (isEdit) response.setRenderParameter("edit", "true");
          return "failure";
        }
        
        portletClassName = pClass.substring(pClass.lastIndexOf(".")+1);
        PortletType pType = PortletUtil.createPortletType(pName, pClass);
        PortletUtil.setTitle(title, pType);
        PortletUtil.setKeywords(keywordsStr, pType);
        PortletUtil.setShortTitle(shortTitle, pType);
        PortletUtil.setResourceBundle(resourceBundleName, pType);
        
        HashMap descrMap = new HashMap();
        for (int i=0; i<numOfDescrLangs; i++) {
          String lang = request.getParameter("descr_lang"+i);
          String descr = request.getParameter("descr"+i);
          if (descrMap.containsKey(lang)) continue;
          descrMap.put(lang, descr);
        }
        PortletUtil.setDescription(descrMap, pType);

        HashMap displayMap = new HashMap();
        for (int i=0; i<numOfDispLangs; i++) {
          String lang = request.getParameter("display_lang"+i);
          String displayName = request.getParameter("display_name"+i);
          if (displayMap.containsKey(lang)) continue;
          displayMap.put(lang, displayName);
        }
        PortletUtil.setDisplayName(displayMap, pType);
        
        if (expCache != null && !expCache.trim().equals(""))
          PortletUtil.setExpirationCache(Integer.parseInt(expCache), pType);
        
      Hashtable initParams = new Hashtable();
      for (int i = 0; i < numOfInitParams; i++) {
        String initParamName = request.getParameter("init_param"+i);
          if (initParamName != null && !"".equals(initParamName.trim())){
            initParams.put(initParamName, request.getParameter("init_value"+i));
          }
      }
        PortletUtil.setInitParams(initParams, pType);           

        if (supportedLocales != null && !"".equals(supportedLocales.trim())) {
          String[] locales = StringUtil.split(supportedLocales, ",");
          PortletUtil.setSupportedLocales(locales, pType);
        }
      
        if (isHTMLMimeTypeSupported) {
          Vector modeList = new Vector();
   
            if (isHTMLEditSupported) {
              modeList.add(EDIT_MODE);
            } 
            if (isHTMLHelpSupported) {
              modeList.add(HELP_MODE);
            }
            if (isHTMLAdminSupported) {
              modeList.add(ADMIN_MODE);
            }
            
            String[] supportedModes = new String[modeList.size()];
            modeList.copyInto(supportedModes);
            
            PortletUtil.setSupports(supportedModes, MimeTypes.HTML, pType);
        }
                                            
        if (isXHTMLMimeTypeSupported) {
            String[] supportedModes = new String[0];
            PortletUtil.setSupports(supportedModes, MimeTypes.XHTML_MP, pType);
        }            
        
        if (isWMLMimeTypeSupported) {
            String[] supportedModes = new String[0];
            PortletUtil.setSupports(supportedModes, MimeTypes.WML, pType);
        }            
        
      Hashtable prefMap = new Hashtable();
      PortletPreferencesType pPrefType = new PortletPreferencesTypeImpl();
      boolean isEmpty = true;
      for (int i = 0; i < numOfPrefs; i++) {
          String prefName = request.getParameter("pref_name"+i);
        if (prefName != null && ! "".equals(prefName.trim())) {
          isEmpty = false;
          PreferenceType prefType = new PreferenceTypeImpl();
              NameType nameType = new NameTypeImpl();
              nameType.setValue(prefName);
              prefType.setName(nameType);
              
          Vector prefValueVec = new Vector();
          for (int j=0; j < numOfPrefValues[i]; j++) {
            String prefValue = request.getParameter("pref_value"+i+j);
            if (prefValue != null && !"".equals(prefValue)) {
              prefValueVec.add(prefValue);
                    ValueType valueType = new ValueTypeImpl();
                    valueType.setValue(prefValue);
                    prefType.getValue().add(valueType);
            }
          }                    
          String isReadOnly = request.getParameter("read_only"+i) != null ? "true" : "false";
          prefType.setReadOnly(isReadOnly);
          
          pPrefType.getPreference().add(prefType);          
        }
      }
      if (prefValidatorClassName != null && !"".equals(prefValidatorClassName))
        pPrefType.setPreferencesValidator(prefValidatorClassName);
      
      if (!isEmpty) pType.setPortletPreferences(pPrefType);      
      
      HashMap secRoleMap = new HashMap();
      for (int i = 0; i < numOfSecRoles; i++) {
        String roleName = request.getParameter("role_name"+i);
          if (roleName != null && !"".equals(roleName.trim())){
            secRoleMap.put(roleName, request.getParameter("role_link"+i));
          }
      }
        PortletUtil.setSecurityRoleReference(secRoleMap, pType);
      
        try {
          if (isEdit) {
        HttpServletRequest req = ((PortletRequestImpl) request).getHttpServletRequest();
        HttpServletResponse res = ((PortletResponseImpl) response).getHttpServletResponse();
              PortalServlet.getInstance().replacePortlet(pType, pIndex, req, res);
           } else {
              PortalServlet.getInstance().addPortlet(pType);
          }
          PortalServlet.getInstance().savePortletConfiguration();  
        } catch (Exception ex) {
          throw new PortletException("Failed to create " + portletClassName + " portlet.", ex);
        }
    
    return "success";
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.