PortletTemplatePlugin.java

  1. /*
  2.  * Copyright (C) 2003-2008 eXo Platform SAS.
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Affero General Public License
  6.  * as published by the Free Software Foundation; either version 3
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, see<http://www.gnu.org/licenses/>.
  16.  */
  17. package org.exoplatform.services.cms.views;

  18. import java.io.InputStream;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;

  22. import org.exoplatform.commons.utils.IOUtil;
  23. import org.exoplatform.container.component.BaseComponentPlugin;
  24. import org.exoplatform.container.configuration.ConfigurationManager;
  25. import org.exoplatform.container.xml.InitParams;
  26. import org.exoplatform.container.xml.ObjectParameter;

  27. /**
  28.  * Created by The eXo Platform SAS
  29.  * Author : Hoa Pham
  30.  * hoa.phamvu@exoplatform.com
  31.  * Oct 15, 2008
  32.  */
  33. public class PortletTemplatePlugin extends BaseComponentPlugin{

  34.   private ConfigurationManager configurationManager;
  35.   private InitParams initParams;
  36.   private String portletName;

  37.   /**
  38.    * Instantiates a new portlet template plugin.
  39.    *
  40.    * @param initParams the init params
  41.    * @param configurationManager the configuration manager
  42.    */
  43.   public PortletTemplatePlugin(InitParams initParams, ConfigurationManager configurationManager) {
  44.     this.initParams = initParams;
  45.     this.configurationManager = configurationManager;
  46.     portletName = this.initParams.getValueParam("portletName").getValue();
  47.   }

  48.   /**
  49.    * Gets the portlet name.
  50.    *
  51.    * @return the portlet name
  52.    */
  53.   public String getPortletName() { return portletName; }

  54.   /**
  55.    * Retrieves all portlet template config for a portlet in plugin
  56.    *
  57.    * @return the portlet template configs
  58.    *
  59.    * @throws Exception the exception
  60.    */
  61.   public List<PortletTemplateConfig> getPortletTemplateConfigs() throws Exception {
  62.     List<PortletTemplateConfig> list = new ArrayList<PortletTemplateConfig>();
  63.     String configPath = initParams.getValueParam("portlet.template.path").getValue();
  64.     Iterator<ObjectParameter> iterator = initParams.getObjectParamIterator();
  65.     for(;iterator.hasNext();) {
  66.       Object obj = iterator.next().getObject();
  67.       PortletTemplateConfig config = PortletTemplateConfig.class.cast(obj);
  68.       String templateFile = configPath + "/" + config.getCategory() + "/" + config.getTemplateName();
  69.       InputStream input = configurationManager.getInputStream(templateFile);
  70.       String templateData = IOUtil.getStreamContentAsString(input);
  71.       config.setTemplateData(templateData);
  72.       list.add(config);
  73.     }
  74.     return list;
  75.   }

  76.   /**
  77.    * The Class PortletTemplateConfig.
  78.    */
  79.   public static class PortletTemplateConfig {
  80.     private String category;
  81.     private ArrayList<String> accessPermissions;
  82.     private ArrayList<String> editPermissions;
  83.     private String templateName;
  84.     private String title;
  85.     private String templateData;

  86.     /**
  87.      * Gets the category.
  88.      *
  89.      * @return the category
  90.      */
  91.     public String getCategory() {
  92.       return category;
  93.     }

  94.     /**
  95.      * Sets the category.
  96.      *
  97.      * @param category the new category
  98.      */
  99.     public void setCategory(String category) {
  100.       this.category = category;
  101.     }

  102.     /**
  103.      * Gets the access permissions.
  104.      *
  105.      * @return the access permissions
  106.      */
  107.     public ArrayList<String> getAccessPermissions() {
  108.       return accessPermissions;
  109.     }

  110.     /**
  111.      * Sets the access permissions.
  112.      *
  113.      * @param accessPermissions the new access permissions
  114.      */
  115.     public void setAccessPermissions(ArrayList<String> accessPermissions) {
  116.       this.accessPermissions = accessPermissions;
  117.     }

  118.     /**
  119.      * Gets the edits the permissions.
  120.      *
  121.      * @return the edits the permissions
  122.      */
  123.     public ArrayList<String> getEditPermissions() {
  124.       return editPermissions;
  125.     }

  126.     /**
  127.      * Sets the edits the permissions.
  128.      *
  129.      * @param editPermissions the new edits the permissions
  130.      */
  131.     public void setEditPermissions(ArrayList<String> editPermissions) {
  132.       this.editPermissions = editPermissions;
  133.     }

  134.     /**
  135.      * Gets the template name.
  136.      *
  137.      * @return the template name
  138.      */
  139.     public String getTemplateName() {
  140.       return templateName;
  141.     }

  142.     /**
  143.      * Sets the template name.
  144.      *
  145.      * @param templateName the new template name
  146.      */
  147.     public void setTemplateName(String templateName) {
  148.       this.templateName = templateName;
  149.     }
  150.    
  151.     /**
  152.      * Gets the full text template name.
  153.      *
  154.      * @return the template name
  155.      */
  156.     public String getTitle() {
  157.       return title;
  158.     }

  159.     /**
  160.      * Sets the full text template title.
  161.      *
  162.      * @param title the new template title
  163.      */
  164.     public void setTitle(String title) {
  165.       this.title = title;
  166.     }    

  167.     /**
  168.      * Gets the template data.
  169.      *
  170.      * @return the template data
  171.      */
  172.     public String getTemplateData() {
  173.       return templateData;
  174.     }

  175.     /**
  176.      * Sets the template data.
  177.      *
  178.      * @param templateData the new template data
  179.      */
  180.     public void setTemplateData(String templateData) {
  181.       this.templateData = templateData;
  182.     }
  183.   }
  184. }