View Javadoc
1   /*
2    * Copyright (C) 2003-2007 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.impl;
18  
19  import org.exoplatform.container.component.BaseComponentPlugin;
20  import org.exoplatform.container.configuration.ConfigurationManager;
21  import org.exoplatform.container.xml.InitParams;
22  import org.exoplatform.container.xml.ObjectParameter;
23  import org.exoplatform.container.xml.ValueParam;
24  import org.exoplatform.services.cms.BasePath;
25  import org.exoplatform.services.cms.impl.DMSConfiguration;
26  import org.exoplatform.services.cms.impl.DMSRepositoryConfiguration;
27  import org.exoplatform.services.cms.impl.Utils;
28  import org.exoplatform.services.cms.templates.TemplateService;
29  import org.exoplatform.services.cms.views.TemplateConfig;
30  import org.exoplatform.services.cms.views.ViewConfig;
31  import org.exoplatform.services.cms.views.ViewConfig.Tab;
32  import org.exoplatform.services.jcr.RepositoryService;
33  import org.exoplatform.services.jcr.core.ManageableRepository;
34  import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
35  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
36  
37  import javax.jcr.Node;
38  import javax.jcr.Session;
39  import java.io.InputStream;
40  import java.util.HashSet;
41  import java.util.Iterator;
42  import java.util.Set;
43  
44  public class ManageViewPlugin extends BaseComponentPlugin {
45  
46    private static String ECM_EXPLORER_TEMPLATE = "ecmExplorerTemplate" ;
47    public static final String EDITED_CONFIGURED_VIEWS = "EditedConfiguredViews";
48    public static final String EDITED_CONFIGURED_VIEWS_TEMPLATES = "EditedConfiguredViewsTemplate";
49    private InitParams params_ ;
50    private RepositoryService repositoryService_ ;
51    private NodeHierarchyCreator nodeHierarchyCreator_ ;
52    private ConfigurationManager cservice_ ;
53    private boolean autoCreateInNewRepository_ = false ;
54    private String predefinedViewsLocation_ = "war:/conf/dms/artifacts";
55    private DMSConfiguration dmsConfiguration_;
56    private TemplateService templateService;
57    private Set<String> configuredTemplate_;
58    private Set<String> configuredViews_;
59  
60    public ManageViewPlugin(RepositoryService repositoryService, InitParams params, ConfigurationManager cservice,
61        NodeHierarchyCreator nodeHierarchyCreator, DMSConfiguration dmsConfiguration) throws Exception {
62      params_ = params ;
63      repositoryService_ = repositoryService ;
64      nodeHierarchyCreator_ = nodeHierarchyCreator ;
65      cservice_ = cservice ;
66      ValueParam autoInitParam = params.getValueParam("autoCreateInNewRepository") ;
67      if(autoInitParam !=null) {
68        autoCreateInNewRepository_ = Boolean.parseBoolean(autoInitParam.getValue()) ;
69      }
70      ValueParam predefinedViewLocation = params.getValueParam("predefinedViewsLocation");
71      if(predefinedViewLocation != null) {
72        predefinedViewsLocation_ = predefinedViewLocation.getValue();
73      }
74      dmsConfiguration_ = dmsConfiguration;
75      templateService = WCMCoreUtils.getService(TemplateService.class);
76    }
77  
78    public void init() throws Exception {
79      if(autoCreateInNewRepository_) {
80        importPredefineViews() ;
81        return ;
82      }
83      return;
84    }
85  
86    private void importPredefineViews() throws Exception {
87      configuredTemplate_ = new HashSet<String>();
88      configuredViews_ = new HashSet<String>();
89      Iterator<ObjectParameter> it = params_.getObjectParamIterator() ;
90      String viewsPath = nodeHierarchyCreator_.getJcrPath(BasePath.CMS_VIEWS_PATH);
91      String templatesPath = nodeHierarchyCreator_.getJcrPath(BasePath.CMS_VIEWTEMPLATES_PATH);
92      String warViewPath = predefinedViewsLocation_ + templatesPath.substring(templatesPath.lastIndexOf("exo:ecm") + 7) ;
93      ManageableRepository manageableRepository = repositoryService_.getCurrentRepository();
94      DMSRepositoryConfiguration dmsRepoConfig = dmsConfiguration_.getConfig();
95      Session session = manageableRepository.getSystemSession(dmsRepoConfig.getSystemWorkspace()) ;
96      ViewConfig viewObject = null ;
97      TemplateConfig templateObject = null ;
98      Node viewHomeNode = (Node)session.getItem(viewsPath) ;
99      while(it.hasNext()) {
100       Object object = it.next().getObject();
101       if(object instanceof ViewConfig) {
102         viewObject = (ViewConfig)object ;
103         String viewNodeName = viewObject.getName();
104         configuredViews_.add(viewNodeName);
105         if(viewHomeNode.hasNode(viewNodeName) || Utils.getAllEditedConfiguredData(
106           this.getClass().getSimpleName(), EDITED_CONFIGURED_VIEWS, true).contains(viewNodeName)) continue ;
107         Node viewNode = addView(viewHomeNode,viewNodeName,viewObject.getPermissions(),
108                                 viewObject.isHideExplorerPanel(), viewObject.getTemplate()) ;
109         for(Tab tab:viewObject.getTabList()) {
110           addTab(viewNode,tab.getTabName(),tab.getButtons()) ;
111         }
112       }else if(object instanceof TemplateConfig) {
113         templateObject = (TemplateConfig) object;
114         addTemplate(templateObject,session,warViewPath);
115         configuredTemplate_.add(templateObject.getName());
116       }
117     }
118     session.save();
119     session.logout();
120   }
121 
122   private Node addView(Node viewManager, String viewName, String permissions, boolean hideExplorerPanel, String template) 
123   throws Exception {
124     Node contentNode = viewManager.addNode(viewName, "exo:view");
125     contentNode.setProperty("exo:accessPermissions", permissions);
126     contentNode.setProperty("exo:template", template);
127     if(contentNode.hasProperty("exo:hideExplorerPanel")) {
128       contentNode.setProperty("exo:hideExplorerPanel", hideExplorerPanel);
129     }
130     viewManager.save() ;
131     return contentNode ;
132   }
133 
134   private void addTab(Node view, String tabName, String buttons) throws Exception {
135     Node tab ;
136     if(view.hasNode(tabName)){
137       tab = view.getNode(tabName) ;
138     }else {
139       tab = view.addNode(tabName, "exo:tab");
140     }
141     tab.setProperty("exo:buttons", buttons);
142     view.save() ;
143   }
144 
145   private void addTemplate(TemplateConfig tempObject, Session session, String warViewPath) throws Exception {
146     String type = tempObject.getTemplateType() ;
147     String alias = "" ;
148     if(type.equals(ECM_EXPLORER_TEMPLATE)) {
149       alias = BasePath.ECM_EXPLORER_TEMPLATES ;
150     }
151     String templateHomePath = nodeHierarchyCreator_.getJcrPath(alias) ;
152     Node templateHomeNode = (Node)session.getItem(templateHomePath) ;
153     String templateName = tempObject.getName() ;
154     if(templateHomeNode.hasNode(templateName) || Utils.getAllEditedConfiguredData(
155       this.getClass().getSimpleName(), EDITED_CONFIGURED_VIEWS_TEMPLATES, true).contains(templateName)) return;
156     String warPath = warViewPath + tempObject.getWarPath() ;
157     InputStream in = cservice_.getInputStream(warPath) ;
158     templateService.createTemplate(templateHomeNode, templateName, templateName, in, new String[] {"*"});
159   }
160   
161   public Set<String> getConfiguredTemplates() {
162     return configuredTemplate_;
163   }
164   
165   public Set<String> getConfiguredViews() {
166     return configuredViews_;
167   }
168   
169 }