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.views;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.MissingResourceException;
24  import java.util.ResourceBundle;
25  
26  import javax.jcr.Node;
27  import javax.jcr.NodeIterator;
28  import javax.jcr.RepositoryException;
29  import javax.jcr.version.VersionHistory;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.exoplatform.ecm.jcr.model.VersionNode;
33  import org.exoplatform.ecm.webui.form.validator.ECMNameValidator;
34  import org.exoplatform.ecm.webui.selector.UISelectable;
35  import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
36  import org.exoplatform.ecm.webui.utils.Utils;
37  import org.exoplatform.services.cms.BasePath;
38  import org.exoplatform.services.cms.views.ManageViewService;
39  import org.exoplatform.services.cms.views.ViewConfig;
40  import org.exoplatform.services.cms.views.ViewConfig.Tab;
41  import org.exoplatform.services.wcm.core.NodeLocation;
42  import org.exoplatform.services.wcm.core.NodetypeConstant;
43  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
44  import org.exoplatform.web.application.ApplicationMessage;
45  import org.exoplatform.web.application.RequestContext;
46  import org.exoplatform.webui.application.WebuiRequestContext;
47  import org.exoplatform.webui.core.UIApplication;
48  import org.exoplatform.webui.core.UIPopupWindow;
49  import org.exoplatform.webui.core.model.SelectItemOption;
50  import org.exoplatform.webui.event.Event;
51  import org.exoplatform.webui.event.EventListener;
52  import org.exoplatform.webui.exception.MessageException;
53  import org.exoplatform.webui.form.UIForm;
54  import org.exoplatform.webui.form.UIFormSelectBox;
55  import org.exoplatform.webui.form.UIFormStringInput;
56  import org.exoplatform.webui.form.input.UICheckBoxInput;
57  import org.exoplatform.webui.form.validator.MandatoryValidator;
58  
59  /**
60   * Created by The eXo Platform SARL
61   * Author : Tran The Trong
62   *          trongtt@yahoo.com
63   * Jun 28, 2006
64   */
65  
66  public class UIViewForm extends UIForm implements UISelectable {
67  
68    final static public String FIELD_VERSION = "version" ;
69    final static public String FIELD_NAME = "viewName" ;  
70    final static public String FIELD_TABS = "tabs" ;
71    final static public String FIELD_TEMPLATE = "template" ;
72    final static public String FIELD_ENABLEVERSION = "enableVersion" ;
73    final static public String FIELD_PERMISSION = "permission" ;
74    final static public String FIELD_HIDE_EXPLORER_PANEL = "hideExplorerPanel" ;
75  
76    private boolean isView_ = true ;
77    private NodeLocation views_;
78    private HashMap<String, Tab> tabMap_ = new HashMap<String, Tab>() ;
79    private ManageViewService vservice_ = null ;
80    private String viewName_ = null;
81    private String permission = StringUtils.EMPTY;
82    private List<String> listVersion = new ArrayList<String>() ;
83    private String baseVersionName_;
84    private VersionNode selectedVersion_;
85    private VersionNode rootVersionNode;
86    Map<String, String> templateMap = new HashMap<String, String>();
87    Map<String, String> tempMap = new HashMap<String, String>();
88  
89    public String getViewName() {
90      return viewName_;
91    }
92  
93    public void setViewName(String viewName) {
94      this.viewName_ = viewName;
95    }
96  
97    public String getPermission() {
98      return permission;
99    }
100 
101   public String[] getActions() { return new String[] {}; }
102 
103   public void setPermission(String permission) {
104     this.permission = permission;
105   }
106 
107   public UIViewForm() throws Exception {
108     this("UIViewForm");  	
109   }  
110 
111   public UIViewForm(String name) throws Exception {
112     setComponentConfig(getClass(), null) ;
113     List<SelectItemOption<String>> options = new ArrayList<SelectItemOption<String>>() ;
114     UIFormSelectBox versions = new UIFormSelectBox(FIELD_VERSION , FIELD_VERSION, options) ;
115     versions.setOnChange("ChangeVersion");
116     versions.setRendered(false) ;
117     addUIFormInput(versions) ;
118     addUIFormInput(new UIFormStringInput(FIELD_NAME, FIELD_NAME, null).addValidator(MandatoryValidator.class)
119                    .addValidator(ECMNameValidator.class)) ;
120     vservice_ = getApplicationComponent(ManageViewService.class) ;
121     Node ecmTemplateHome = vservice_.getTemplateHome(BasePath.ECM_EXPLORER_TEMPLATES, WCMCoreUtils.getSystemSessionProvider());
122     List<SelectItemOption<String>> temp = new ArrayList<SelectItemOption<String>>() ;
123     if(ecmTemplateHome != null) {
124       NodeIterator iter = ecmTemplateHome.getNodes() ;
125       while(iter.hasNext()) {
126         Node tempNode = iter.nextNode() ;
127         temp.add(new SelectItemOption<String>(tempNode.getName(),tempNode.getName())) ;
128         templateMap.put(tempNode.getName(), tempNode.getPath());
129         tempMap.put(tempNode.getPath(), tempNode.getName());
130       }
131     }
132     addUIFormInput(new UIFormSelectBox(FIELD_TEMPLATE,FIELD_TEMPLATE, temp)) ;
133     UICheckBoxInput enableVersion = new UICheckBoxInput(FIELD_ENABLEVERSION, FIELD_ENABLEVERSION, null) ;
134     enableVersion.setRendered(true) ;
135     addUIFormInput(enableVersion) ;
136     //prefernce: is show side bar
137     UICheckBoxInput hideExplorerPanel = 
138         new UICheckBoxInput(FIELD_HIDE_EXPLORER_PANEL, FIELD_HIDE_EXPLORER_PANEL, false);
139     hideExplorerPanel.setRendered(false);
140     addUIFormInput(hideExplorerPanel);
141   }
142 
143   public void processRender(WebuiRequestContext context) throws Exception {
144     super.processRender(context) ;
145   }
146 
147   public void doSelect(String selectField, Object value) {
148     UIFormStringInput uiStringInput = getUIStringInput(selectField);
149     uiStringInput.setValue(value.toString());
150   }
151 
152   public boolean isView() { return isView_ ; }
153 
154   public Node getViews() {
155     return NodeLocation.getNodeByLocation(views_);
156   }
157 
158   public boolean canEnableVersionning(Node node) throws Exception {
159     return node.canAddMixin(Utils.MIX_VERSIONABLE);
160   }
161 
162   private boolean isVersioned(Node node) throws RepositoryException {
163     return node.isNodeType(Utils.MIX_VERSIONABLE);
164   }
165 
166   private VersionNode getRootVersion(Node node) throws Exception{
167     VersionHistory vH = node.getVersionHistory() ;
168     if(vH != null) return new VersionNode(vH.getRootVersion(), node.getSession()) ;
169     return null ;
170   }
171 
172   private List<String> getNodeVersions(List<VersionNode> children) throws Exception {
173     List<VersionNode> child = new ArrayList<VersionNode>() ;
174     for(VersionNode vNode : children){
175       listVersion.add(vNode.getName());
176       child = vNode.getChildren() ;
177       if (!child.isEmpty()) getNodeVersions(child) ;
178     }
179     return listVersion ;
180   }
181 
182   private List<SelectItemOption<String>> getVersionValues(Node node) throws Exception {
183     List<SelectItemOption<String>> options = new ArrayList<SelectItemOption<String>>() ;
184     List<VersionNode> children = getRootVersion(node).getChildren() ;
185     listVersion.clear() ;
186     List<String> versionList = getNodeVersions(children) ;
187     for(int i = 0; i < versionList.size(); i++) {
188       for(int j = i + 1; j < versionList.size(); j ++) {
189         if( Integer.parseInt(versionList.get(j)) < Integer.parseInt(versionList.get(i))) {
190           String temp = versionList.get(i) ;
191           versionList.set(i, versionList.get(j))  ;
192           versionList.set(j, temp) ;
193         }
194       }
195       options.add(new SelectItemOption<String>(versionList.get(i), versionList.get(i))) ;
196     }
197     return options ;
198   }
199 
200   public HashMap<String, Tab> getTabMap() {
201     return tabMap_;
202   }
203 
204   public void addTab(String tabName, String buttons){
205     Tab tab = new Tab() ;
206     tab.setTabName(tabName) ;
207     tab.setButtons(buttons) ;
208     tab.setLocalizeButtons(getLocalizationButtons(buttons));
209     tabMap_.put(tabName, tab) ;
210   }
211 
212   public String getLocalizationButtons(String buttons) {
213     StringBuilder localizationButtons = new StringBuilder();
214     RequestContext context = RequestContext.getCurrentInstance();
215     ResourceBundle res = context.getApplicationResourceBundle();
216     if(buttons.contains(";")) {
217       String[] arrButtons = buttons.split(";");
218       for(int i = 0; i < arrButtons.length; i++) {
219         try {
220           localizationButtons.append(res.getString("UITabForm.label." + arrButtons[i].trim()));
221         } catch(MissingResourceException mre) {
222           localizationButtons.append(arrButtons[i]);
223         }
224         if(i < arrButtons.length - 1) {
225           localizationButtons.append(", ");
226         }
227       }
228     } else {
229       try {
230         localizationButtons.append(res.getString("UITabForm.label." + buttons.trim()));
231       } catch(MissingResourceException mre) {
232         localizationButtons.append(buttons.trim());
233       }
234     }
235     return localizationButtons.toString();
236   }  
237 
238   public String getTabList() throws Exception {
239     StringBuilder result = new StringBuilder() ;
240     List<Tab> tabList = new ArrayList<Tab>(tabMap_.values());
241     if(result != null) {
242       for(Tab tab : tabList) {
243         if(result.length() > 0) result.append(",") ;
244         result.append(tab.getTabName()) ;
245       }
246     }
247     return result.toString() ;
248   }
249 
250   public List<Tab> getTabs() throws Exception {
251     return new ArrayList<Tab>(tabMap_.values());
252   }  
253 
254   public void refresh(boolean isAddNew) throws Exception {
255     getUIFormSelectBox(FIELD_VERSION).setRendered(!isAddNew) ;
256     getUIFormSelectBox(FIELD_VERSION).setDisabled(!isAddNew) ;
257     getUIStringInput(FIELD_NAME).setDisabled(!isAddNew).setValue(null) ;
258     getUIFormSelectBox(FIELD_TEMPLATE).setValue(null) ;
259     getUIFormSelectBox(FIELD_TEMPLATE).setDisabled(!isAddNew) ;
260     getUICheckBoxInput(FIELD_ENABLEVERSION).setRendered(!isAddNew) ;
261     getUICheckBoxInput(FIELD_HIDE_EXPLORER_PANEL).setRendered(!isAddNew);
262     setViewName("");
263     if(isAddNew) {
264       tabMap_.clear() ;
265       views_ = null ;
266       getUICheckBoxInput(FIELD_HIDE_EXPLORER_PANEL).setValue(false);
267     }
268     selectedVersion_ = null ;
269     baseVersionName_ = null ;
270   }
271 
272   public void update(Node viewNode, boolean isView, VersionNode selectedVersion) throws Exception {
273     isView_ = isView ;
274     if(viewNode != null) {
275       setPermission(viewNode.getProperty("exo:accessPermissions").getString());
276       views_ = NodeLocation.getNodeLocationByNode(viewNode);
277       if(isVersioned(viewNode)) baseVersionName_ = viewNode.getBaseVersion().getName();
278       tabMap_.clear() ;
279       for(NodeIterator iter = viewNode.getNodes(); iter.hasNext(); ) {
280         Node tab = iter.nextNode() ;
281         String buttons = tab.getProperty("exo:buttons").getString() ;
282         Tab tabObj = new Tab() ;
283         tabObj.setTabName(tab.getName()) ;
284         tabObj.setButtons(buttons) ;
285         tabObj.setLocalizeButtons(getLocalizationButtons(buttons));
286         tabMap_.put(tab.getName(), tabObj) ;
287       }
288 
289       getUICheckBoxInput(FIELD_ENABLEVERSION).setRendered(true) ;
290       if (isVersioned(viewNode)) {
291         rootVersionNode = getRootVersion(viewNode);
292         getUIFormSelectBox(FIELD_VERSION).setOptions(getVersionValues(viewNode)).setRendered(true) ;
293         getUIFormSelectBox(FIELD_VERSION).setValue(baseVersionName_) ;
294         getUICheckBoxInput(FIELD_ENABLEVERSION).setChecked(true) ;
295         getUICheckBoxInput(FIELD_ENABLEVERSION).setDisabled(false);
296       } else if (!isVersioned(viewNode)) {
297         getUIFormSelectBox(FIELD_VERSION).setRendered(false) ;
298         getUICheckBoxInput(FIELD_ENABLEVERSION).setChecked(false) ;
299         getUICheckBoxInput(FIELD_ENABLEVERSION).setDisabled(false);
300       }
301       //pref is show side bar
302       getUICheckBoxInput(FIELD_HIDE_EXPLORER_PANEL).setRendered(true);
303       getUICheckBoxInput(FIELD_HIDE_EXPLORER_PANEL).setValue(
304                                                              viewNode.getProperty(NodetypeConstant.EXO_HIDE_EXPLORER_PANEL).getBoolean());
305     }
306     //---------------------
307     Node viewsNode = NodeLocation.getNodeByLocation(views_);
308     if (selectedVersion != null) {
309       viewsNode.restore(selectedVersion.getName(), false) ;
310       viewsNode.checkout() ;
311       tabMap_.clear() ;
312       for(NodeIterator iter = viewsNode.getNodes(); iter.hasNext(); ) {
313         Node tab = iter.nextNode() ;
314         String buttons = tab.getProperty("exo:buttons").getString() ;
315         Tab tabObj = new Tab() ;
316         tabObj.setTabName(tab.getName()) ;
317         tabObj.setButtons(buttons) ;
318         tabMap_.put(tab.getName(), tabObj) ;
319       }
320       selectedVersion_ = selectedVersion;
321     }
322     if(viewsNode != null) {
323       getUIStringInput(FIELD_NAME).setDisabled(true).setValue(viewsNode.getName()) ;
324       getUIFormSelectBox(FIELD_TEMPLATE).setValue(tempMap.get(viewsNode.getProperty("exo:template").getString()));
325     }
326   }
327 
328   public void save() throws Exception {
329     String viewName = getUIStringInput(FIELD_NAME).getValue();
330     ApplicationMessage message ;
331     if(viewName == null || viewName.trim().length() == 0){
332       throw new MessageException(new ApplicationMessage("UIViewForm.msg.view-name-invalid", null,
333                                                         ApplicationMessage.WARNING)) ;
334     }
335     viewName = viewName.trim();
336     
337     boolean isEnableVersioning = getUICheckBoxInput(FIELD_ENABLEVERSION).isChecked() ;
338     boolean hideExplorerPanel = getUICheckBoxInput(FIELD_HIDE_EXPLORER_PANEL).isChecked();
339     List<ViewConfig> viewList = vservice_.getAllViews() ;
340     UIPopupWindow uiPopup = getAncestorOfType(UIPopupWindow.class) ;
341     uiPopup.setShowMask(true);
342     if(uiPopup.getId().equals(UIViewList.ST_ADD)) {
343       for(ViewConfig view : viewList) {
344         if(view.getName().equals(viewName) && !isEnableVersioning) {
345           message = new ApplicationMessage("UIViewForm.msg.view-exist", null,
346                                            ApplicationMessage.WARNING) ;
347           throw new MessageException(message) ;
348         }
349       }
350     }
351 
352     if(tabMap_.size() < 1 ){
353       message = new ApplicationMessage("UIViewForm.msg.mustbe-add-tab", null,
354                                        ApplicationMessage.WARNING) ;
355       throw new MessageException(message) ;
356     }
357     if(permission == null || permission.length() == 0){
358       message = new ApplicationMessage("UIViewForm.msg.mustbe-add-permission", null,
359                                        ApplicationMessage.WARNING) ;
360       throw new MessageException(message) ;
361     }    
362     String template = templateMap.get(getUIFormSelectBox(FIELD_TEMPLATE).getValue());
363 
364     List<Tab> tabList = new ArrayList<Tab>(tabMap_.values());
365     Node viewNode = NodeLocation.getNodeByLocation(views_);
366     if(views_ == null || !isEnableVersioning) {
367       vservice_.addView(viewName, permission, hideExplorerPanel, template, tabList) ;
368       if(viewNode != null) {
369         for(NodeIterator iter = viewNode.getNodes(); iter.hasNext(); ) {
370           Node tab = iter.nextNode() ;
371           if(!tabMap_.containsKey(tab.getName())) tab.remove() ;
372         }
373         viewNode.save() ;
374       }
375     } else {
376       if (!isVersioned(viewNode)) {
377         viewNode.addMixin(Utils.MIX_VERSIONABLE);
378         viewNode.save();
379       } else {
380         viewNode.checkout() ;
381       }
382       for(NodeIterator iter = viewNode.getNodes(); iter.hasNext(); ) {
383         Node tab = iter.nextNode() ;
384         if(!tabMap_.containsKey(tab.getName())) tab.remove() ;
385       }
386       vservice_.addView(viewName, permission, hideExplorerPanel, template, tabList) ;
387       try {
388         viewNode.save() ;
389         viewNode.checkin();
390       } catch (Exception e) {
391         UIApplication uiApp = getAncestorOfType(UIApplication.class) ;
392         JCRExceptionManager.process(uiApp, e);
393         return ;
394       }
395     }
396     UIViewList uiViewList = getAncestorOfType(UIViewContainer.class).getChild(UIViewList.class);
397     uiViewList.refresh(uiViewList.getUIPageIterator().getCurrentPage());
398     refresh(true) ;
399   }
400 
401   public void editTab(String tabName) throws Exception {
402     UIViewFormTabPane viewTabPane = getParent() ;
403     UITabForm tabForm = viewTabPane.getChild(UITabForm.class) ;
404     tabForm.update(tabMap_.get(tabName), isView_) ;
405     viewTabPane.setSelectedTab(tabForm.getId()) ;
406   }
407 
408   public void deleteTab(String tabName) throws Exception {
409     UIViewFormTabPane viewTabPane = getParent() ;
410     //String permLastest = viewTabPane.getUIStringInput(UIViewForm.FIELD_PERMISSION).getValue();
411     tabMap_.remove(tabName) ;
412     update(null, false, null) ;
413     UIViewContainer uiViewContainer = getAncestorOfType(UIViewContainer.class) ;
414     UIViewList uiViewList = uiViewContainer.getChild(UIViewList.class) ;
415     uiViewList.refresh(uiViewList.getUIPageIterator().getCurrentPage());
416     UIViewForm uiViewForm = viewTabPane.getChild(UIViewForm.class) ;
417     viewTabPane.setSelectedTab(uiViewForm.getId()) ;
418   }
419 
420   public void changeVersion() throws Exception {
421     String path = NodeLocation.getNodeByLocation(views_)
422         .getVersionHistory()
423         .getVersion(getUIFormSelectBox(FIELD_VERSION).getValue())
424         .getPath();
425     VersionNode selectedVesion = rootVersionNode.findVersionNode(path);
426     update(null, false, selectedVesion) ;
427   }
428 
429   public void revertVersion() throws Exception {
430     if (selectedVersion_ != null && !selectedVersion_.getName().equals(baseVersionName_)) {
431       NodeLocation.getNodeByLocation(views_).restore(baseVersionName_, true);
432     }
433   }
434 
435   static public class ChangeVersionActionListener extends EventListener<UIViewForm> {
436     public void execute(Event<UIViewForm> event) throws Exception {
437       UIViewFormTabPane uiFormTab = event.getSource().getParent();
438       UIViewForm uiForm = uiFormTab.getChild(UIViewForm.class);
439       uiForm.changeVersion();
440       UIViewContainer uiViewContainer = uiFormTab.getAncestorOfType(UIViewContainer.class) ;
441       event.getRequestContext().addUIComponentToUpdateByAjax(uiViewContainer) ;
442     }
443   }
444 }