View Javadoc
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  
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.exoplatform.commons.utils.IOUtil;
25  import org.exoplatform.container.component.BaseComponentPlugin;
26  import org.exoplatform.container.configuration.ConfigurationManager;
27  import org.exoplatform.container.xml.InitParams;
28  import org.exoplatform.container.xml.ObjectParameter;
29  
30  /**
31   * Created by The eXo Platform SAS
32   * Author : Hoa Pham
33   * hoa.phamvu@exoplatform.com
34   * Oct 15, 2008
35   */
36  public class PortletTemplatePlugin extends BaseComponentPlugin{
37  
38    private ConfigurationManager configurationManager;
39    private InitParams initParams;
40    private String portletName;
41  
42    /**
43     * Instantiates a new portlet template plugin.
44     *
45     * @param initParams the init params
46     * @param configurationManager the configuration manager
47     */
48    public PortletTemplatePlugin(InitParams initParams, ConfigurationManager configurationManager) {
49      this.initParams = initParams;
50      this.configurationManager = configurationManager;
51      portletName = this.initParams.getValueParam("portletName").getValue();
52    }
53  
54    /**
55     * Gets the portlet name.
56     *
57     * @return the portlet name
58     */
59    public String getPortletName() { return portletName; }
60  
61    /**
62     * Retrieves all portlet template config for a portlet in plugin
63     *
64     * @return the portlet template configs
65     *
66     * @throws Exception the exception
67     */
68    public List<PortletTemplateConfig> getPortletTemplateConfigs() throws Exception {
69      List<PortletTemplateConfig> list = new ArrayList<PortletTemplateConfig>();
70      String configPath = initParams.getValueParam("portlet.template.path").getValue();
71      Iterator<ObjectParameter> iterator = initParams.getObjectParamIterator();
72      for(;iterator.hasNext();) {
73        Object obj = iterator.next().getObject();
74        PortletTemplateConfig config = PortletTemplateConfig.class.cast(obj);
75        String templateFile = configPath + "/" + config.getCategory() + "/" + config.getTemplateName();
76        InputStream input = configurationManager.getInputStream(templateFile);
77        String templateData = IOUtil.getStreamContentAsString(input);
78        config.setTemplateData(templateData);
79        list.add(config);
80      }
81      return list;
82    }
83  
84    /**
85     * The Class PortletTemplateConfig.
86     */
87    public static class PortletTemplateConfig {
88      private String category;
89      private ArrayList<String> accessPermissions;
90      private ArrayList<String> editPermissions;
91      private String templateName;
92      private String title;
93      private String templateData;
94  
95      /**
96       * Gets the category.
97       *
98       * @return the category
99       */
100     public String getCategory() {
101       return category;
102     }
103 
104     /**
105      * Sets the category.
106      *
107      * @param category the new category
108      */
109     public void setCategory(String category) {
110       this.category = category;
111     }
112 
113     /**
114      * Gets the access permissions.
115      *
116      * @return the access permissions
117      */
118     public ArrayList<String> getAccessPermissions() {
119       return accessPermissions;
120     }
121 
122     /**
123      * Sets the access permissions.
124      *
125      * @param accessPermissions the new access permissions
126      */
127     public void setAccessPermissions(ArrayList<String> accessPermissions) {
128       this.accessPermissions = accessPermissions;
129     }
130 
131     /**
132      * Gets the edits the permissions.
133      *
134      * @return the edits the permissions
135      */
136     public ArrayList<String> getEditPermissions() {
137       return editPermissions;
138     }
139 
140     /**
141      * Sets the edits the permissions.
142      *
143      * @param editPermissions the new edits the permissions
144      */
145     public void setEditPermissions(ArrayList<String> editPermissions) {
146       this.editPermissions = editPermissions;
147     }
148 
149     /**
150      * Gets the template name.
151      *
152      * @return the template name
153      */
154     public String getTemplateName() {
155       return templateName;
156     }
157 
158     /**
159      * Sets the template name.
160      *
161      * @param templateName the new template name
162      */
163     public void setTemplateName(String templateName) {
164       this.templateName = templateName;
165     }
166     
167     /**
168      * Gets the full text template name.
169      *
170      * @return the template name
171      */
172     public String getTitle() {
173       return title;
174     }
175 
176     /**
177      * Sets the full text template title.
178      *
179      * @param title the new template title
180      */
181     public void setTitle(String title) {
182       this.title = title;
183     }    
184 
185     /**
186      * Gets the template data.
187      *
188      * @return the template data
189      */
190     public String getTemplateData() {
191       return templateData;
192     }
193 
194     /**
195      * Sets the template data.
196      *
197      * @param templateData the new template data
198      */
199     public void setTemplateData(String templateData) {
200       this.templateData = templateData;
201     }
202   }
203 }