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.ecm.webui.component.admin.script;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.jcr.Node;
23  import javax.jcr.NodeIterator;
24  
25  import org.exoplatform.ecm.webui.component.admin.script.UIScriptList.ScriptData;
26  import org.exoplatform.ecm.webui.utils.Utils;
27  import org.exoplatform.services.cms.scripts.ScriptService;
28  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
29  import org.exoplatform.webui.config.annotation.ComponentConfig;
30  import org.exoplatform.webui.core.UIContainer;
31  import org.exoplatform.webui.core.UIPopupWindow;
32  import org.exoplatform.webui.core.lifecycle.UIContainerLifecycle;
33  import org.exoplatform.webui.core.model.SelectItemOption;
34  
35  /**
36   * Created by The eXo Platform SARL
37   * Author : pham tuan
38   *          phamtuanchip@yahoo.de
39   * September 27, 2006
40   * 09:22:15 AM
41   */
42  @ComponentConfig(lifecycle = UIContainerLifecycle.class)
43  
44  public class UIECMScripts extends UIContainer {
45    public static String SCRIPTLIST_NAME = "ecmScriptList" ;
46    public static String SCRIPTFORM_NAME = "ecmScriptForm" ;
47    public static String SCRIPT_PAGE =  "PageIterator" ;
48  
49    public UIECMScripts() throws Exception {
50      addChild(UIECMFilterForm.class, null, null) ;
51      UIScriptList list = addChild(UIScriptList.class, null, SCRIPTLIST_NAME) ;
52      list.getUIPageIterator().setId(SCRIPTLIST_NAME + SCRIPT_PAGE) ;
53    }
54  
55    private List<SelectItemOption<String>> getECMCategoryOptions() throws Exception {
56      List<SelectItemOption<String>> ecmOptions = new ArrayList<SelectItemOption<String>>() ;
57      Node ecmScriptHome = 
58        getApplicationComponent(ScriptService.class).getECMScriptHome(WCMCoreUtils.getSystemSessionProvider());
59      NodeIterator categories = ecmScriptHome.getNodes() ;
60      while(categories.hasNext()) {
61        Node script = categories.nextNode() ;
62        ecmOptions.add(new SelectItemOption<String>(script.getName(),script.getName())) ;
63      }
64      return ecmOptions ;
65    }
66  
67    public void refresh(int currentPage) throws Exception {
68      UIECMFilterForm ecmFilterForm = getChild(UIECMFilterForm.class) ;
69      String categoryName =
70        ecmFilterForm.getUIFormSelectBox(UIECMFilterForm.FIELD_SELECT_SCRIPT).getValue() ;
71      if (categoryName == null)  {
72        ecmFilterForm.setOptions(getECMCategoryOptions()) ;
73        categoryName = ecmFilterForm.getUIFormSelectBox(UIECMFilterForm.FIELD_SELECT_SCRIPT).getValue() ;
74      }
75      UIScriptList uiScriptList = getChildById(SCRIPTLIST_NAME);
76      uiScriptList.updateGrid(getECMScript(categoryName), currentPage);
77    }
78  
79    public List<ScriptData> getECMScript(String name) throws Exception {
80      List <ScriptData> scriptData = new ArrayList <ScriptData>() ;
81      List<Node> scripts = new ArrayList<Node> () ;
82      if(name.equals("action")) {
83        scripts = getApplicationComponent(ScriptService.class).getECMActionScripts(WCMCoreUtils.getSystemSessionProvider());
84      }else if(name.equals("widget")){
85        scripts = getApplicationComponent(ScriptService.class).getECMWidgetScripts(WCMCoreUtils.getSystemSessionProvider());
86      }else if(name.equals("interceptor")) {
87        scripts = 
88          getApplicationComponent(ScriptService.class).getECMInterceptorScripts(WCMCoreUtils.getSystemSessionProvider());
89      }
90      for(Node scriptNode : scripts) {
91        String version = "" ;
92        if(scriptNode.isNodeType(Utils.MIX_VERSIONABLE) && !scriptNode.isNodeType(Utils.NT_FROZEN)){
93          version = scriptNode.getBaseVersion().getName();
94        }
95        scriptData.add(new ScriptData(scriptNode.getName(), scriptNode.getPath(), version)) ;
96      }
97      return scriptData ;
98    }
99    
100   public void initFormPopup(String id) throws Exception {
101     removeChildById(id);
102     UIPopupWindow uiPopup = addChild(UIPopupWindow.class, null, id);
103     uiPopup.setShowMask(true);
104     uiPopup.setWindowSize(600, 250);
105     UIScriptForm uiForm = createUIComponent(UIScriptForm.class, null, null);
106     uiPopup.setUIComponent(uiForm);
107     uiPopup.setRendered(true);
108     uiPopup.setShow(true);
109     uiPopup.setResizable(true);
110   }
111 }