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.drives;
18  
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.MissingResourceException;
24  import java.util.ResourceBundle;
25  import java.util.Set;
26  
27  import javax.jcr.Node;
28  import javax.jcr.Session;
29  import javax.jcr.nodetype.NodeType;
30  
31  import org.exoplatform.ecm.webui.comparator.ItemOptionNameComparator;
32  import org.exoplatform.ecm.webui.form.UIFormInputSetWithAction;
33  import org.exoplatform.ecm.webui.form.validator.DrivePermissionValidator;
34  import org.exoplatform.ecm.webui.form.validator.ECMNameValidator;
35  import org.exoplatform.ecm.webui.utils.Utils;
36  import org.exoplatform.services.cms.drives.DriveData;
37  import org.exoplatform.services.cms.templates.TemplateService;
38  import org.exoplatform.services.jcr.RepositoryService;
39  import org.exoplatform.services.jcr.ext.common.SessionProvider;
40  import org.exoplatform.services.jcr.impl.Constants;
41  import org.exoplatform.services.jcr.impl.core.nodetype.NodeTypeImpl;
42  import org.exoplatform.services.log.ExoLogger;
43  import org.exoplatform.services.log.Log;
44  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
45  import org.exoplatform.web.application.RequestContext;
46  import org.exoplatform.webui.config.annotation.ComponentConfig;
47  import org.exoplatform.webui.core.model.SelectItemOption;
48  import org.exoplatform.webui.form.UIFormSelectBox;
49  import org.exoplatform.webui.form.UIFormStringInput;
50  import org.exoplatform.webui.form.input.UICheckBoxInput;
51  import org.exoplatform.webui.form.validator.MandatoryValidator;
52  
53  /**
54   * Created by The eXo Platform SARL
55   * Author : Tran The Trong
56   *          trongtt@gmail.com
57   * Jun 28, 2006
58   */
59  @ComponentConfig(template = "classpath:groovy/ecm/webui/form/UIFormInputSetWithAction.gtmpl")
60  public class UIDriveInputSet extends UIFormInputSetWithAction {
61    final static public String FIELD_NAME = "name";
62    final static public String FIELD_WORKSPACE = "workspace";
63    final static public String FIELD_HOMEPATH = "homePath";
64    final static public String FIELD_WORKSPACEICON = "icon";
65    final static public String FIELD_PERMISSION = "permissions";
66    final static public String FIELD_ALLOW_NODETYPES_ON_TREE = "allowNodeTypesOnTree";
67    final static public String FIELD_VIEWPREFERENCESDOC = "viewPreferences";
68    final static public String FIELD_VIEWNONDOC = "viewNonDocument";
69    final static public String FIELD_VIEWSIDEBAR = "viewSideBar";
70    final static public String FIELD_FOLDER_ONLY = "Folder";
71    final static public String FIELD_BOTH = "Both";
72    final static public String FIELD_UNSTRUCTURED_ONLY = "Unstructured folder";
73    final static public String FIELD_ALLOW_CREATE_FOLDERS = "allowCreateFolders";
74    final static public String SHOW_HIDDEN_NODE = "showHiddenNode";
75  
76    private final static Log  LOG  = ExoLogger.getLogger(UIDriveInputSet.class.getName());
77  
78    public String bothLabel_;
79    public String folderOnlyLabel_;
80    public String unstructuredFolderLabel_;
81    protected Set<String> setFoldertypes;
82    protected TemplateService templateService;
83  
84    public UIDriveInputSet(String name) throws Exception {
85      super(name);
86      setComponentConfig(getClass(), null);
87  
88      addUIFormInput(new UIFormStringInput(FIELD_NAME, FIELD_NAME, null).
89                         addValidator(MandatoryValidator.class).addValidator(ECMNameValidator.class));
90      addUIFormInput(new UIFormSelectBox(FIELD_WORKSPACE, FIELD_WORKSPACE, null));
91      UIFormStringInput homePathField = new UIFormStringInput(FIELD_HOMEPATH, FIELD_HOMEPATH, null);
92      homePathField.setValue("/");
93      homePathField.setDisabled(true);
94      addUIFormInput(homePathField);
95      addUIFormInput(new UIFormStringInput(FIELD_WORKSPACEICON, FIELD_WORKSPACEICON, null).setDisabled(true));
96      UIFormStringInput permissonSelectField = new UIFormStringInput(FIELD_PERMISSION , FIELD_PERMISSION , null);
97      permissonSelectField.addValidator(MandatoryValidator.class);
98      permissonSelectField.addValidator(DrivePermissionValidator.class);
99      permissonSelectField.setDisabled(false);
100     addUIFormInput(permissonSelectField);
101     addUIFormInput(new UICheckBoxInput(FIELD_VIEWPREFERENCESDOC, FIELD_VIEWPREFERENCESDOC, null));
102     addUIFormInput(new UICheckBoxInput(FIELD_VIEWNONDOC, FIELD_VIEWNONDOC, null));
103     addUIFormInput(new UICheckBoxInput(FIELD_VIEWSIDEBAR, FIELD_VIEWSIDEBAR, null));
104     addUIFormInput(new UICheckBoxInput(SHOW_HIDDEN_NODE, SHOW_HIDDEN_NODE, null));
105 
106     addUIFormInput(new UIFormSelectBox(FIELD_ALLOW_CREATE_FOLDERS, FIELD_ALLOW_CREATE_FOLDERS, null));
107     UIFormStringInput filterNodeTypes =
108       new UIFormStringInput(FIELD_ALLOW_NODETYPES_ON_TREE , FIELD_ALLOW_NODETYPES_ON_TREE , null);
109     addUIFormInput(filterNodeTypes);
110     setActionInfo(FIELD_ALLOW_NODETYPES_ON_TREE, new String[] {"ChooseNodeType", "RemoveNodeType"});
111     setActionInfo(FIELD_PERMISSION, new String[] {"AddPermission", "RemovePermission"});
112     setActionInfo(FIELD_HOMEPATH, new String[] {"AddPath"});
113     setActionInfo(FIELD_WORKSPACEICON, new String[] {"AddIcon"});
114     templateService = getApplicationComponent(TemplateService.class);
115     setFoldertypes = templateService.getAllowanceFolderType();
116   }
117 
118   public void update(DriveData drive) throws Exception {
119     String[] wsNames = getApplicationComponent(RepositoryService.class)
120                       .getCurrentRepository().getWorkspaceNames();
121 
122 
123     List<SelectItemOption<String>> workspace = new ArrayList<SelectItemOption<String>>();
124 
125     List<SelectItemOption<String>> foldertypeOptions = new ArrayList<SelectItemOption<String>>();
126     for(String wsName : wsNames) {
127       workspace.add(new SelectItemOption<String>(wsName,  wsName));
128     }
129 
130     RequestContext context = RequestContext.getCurrentInstance();
131     ResourceBundle res = context.getApplicationResourceBundle();
132 
133     for (String foldertype : setFoldertypes) {
134       try {
135         foldertypeOptions.add(new SelectItemOption<String>(res.getString(getId() + ".label."
136             + foldertype.replace(":", "_")), foldertype));
137       } catch (MissingResourceException mre) {
138         foldertypeOptions.add(new SelectItemOption<String>(foldertype, foldertype));
139       }
140     }
141     getUIFormSelectBox(FIELD_WORKSPACE).setOptions(workspace);
142     Collections.sort(foldertypeOptions, new ItemOptionNameComparator());
143     getUIFormSelectBox(FIELD_ALLOW_CREATE_FOLDERS).setOptions(foldertypeOptions);
144     getUIFormSelectBox(FIELD_ALLOW_CREATE_FOLDERS).setMultiple(true);
145     if(drive != null) {
146 
147       // Begin of update
148       UIDriveForm uiDriveForm =  getAncestorOfType(UIDriveForm.class);
149       String selectedWorkspace = drive.getWorkspace();
150       String wsInitRootNodeType = uiDriveForm.getWorkspaceEntries(selectedWorkspace);
151       // End of update
152 
153       invokeGetBindingField(drive);
154       //Set value for multi-value select box
155       String foldertypes = drive.getAllowCreateFolders();
156       String selectedFolderTypes[];
157       if (foldertypes.contains(",")) {
158         selectedFolderTypes = foldertypes.split(",");
159       } else {
160         selectedFolderTypes = new String[] {foldertypes};
161       }
162       List<SelectItemOption<String>> folderOptions = new ArrayList<SelectItemOption<String>>();
163       if(wsInitRootNodeType != null && wsInitRootNodeType.equals(Utils.NT_FOLDER)) {
164         folderOptions.add(new SelectItemOption<String>(UIDriveInputSet.FIELD_FOLDER_ONLY, Utils.NT_FOLDER));
165       } else {
166         folderOptions.addAll(foldertypeOptions);
167       }
168       Collections.sort(folderOptions, new ItemOptionNameComparator());
169       getUIFormSelectBox(FIELD_ALLOW_CREATE_FOLDERS).setOptions(folderOptions);
170       getUIFormSelectBox(FIELD_ALLOW_CREATE_FOLDERS).setSelectedValues(selectedFolderTypes);
171       getUIStringInput(FIELD_NAME).setDisabled(true);
172       return;
173     }
174     getUIStringInput(FIELD_NAME).setDisabled(false);
175     reset();
176     getUICheckBoxInput(FIELD_VIEWPREFERENCESDOC).setChecked(false);
177     getUICheckBoxInput(FIELD_VIEWNONDOC).setChecked(false);
178     getUICheckBoxInput(FIELD_VIEWSIDEBAR).setChecked(false);
179     getUICheckBoxInput(SHOW_HIDDEN_NODE).setChecked(false);
180   }
181 
182   public void updateFolderAllowed(String path) {
183     UIFormSelectBox sltWorkspace =  getChildById(UIDriveInputSet.FIELD_WORKSPACE);
184     String strWorkspace = sltWorkspace.getSelectedValues()[0];
185     SessionProvider sessionProvider = WCMCoreUtils.getSystemSessionProvider();
186     try {
187       Session session = sessionProvider.getSession(strWorkspace,
188                                 getApplicationComponent(RepositoryService.class).getCurrentRepository());
189       Node rootNode = (Node)session.getItem(path);
190       List<SelectItemOption<String>> foldertypeOptions = new ArrayList<SelectItemOption<String>>();
191       RequestContext context = RequestContext.getCurrentInstance();
192       ResourceBundle res = context.getApplicationResourceBundle();
193       for (String foldertype : setFoldertypes) {
194         if (isChildNodePrimaryTypeAllowed(rootNode, foldertype) ){
195           try {
196               foldertypeOptions.add(new SelectItemOption<String>(res.getString(getId() + ".label."
197                   + foldertype.replace(":", "_")), foldertype));
198           } catch (MissingResourceException mre) {
199             foldertypeOptions.add(new SelectItemOption<String>(foldertype, foldertype));
200           }
201         }
202       }
203       Collections.sort(foldertypeOptions, new ItemOptionNameComparator());
204       getUIFormSelectBox(FIELD_ALLOW_CREATE_FOLDERS).setOptions(foldertypeOptions);
205     } catch (Exception e) {
206       if (LOG.isErrorEnabled()) {
207         LOG.error("Unexpected problem occurs while updating", e);
208       }
209     }
210   }
211   private boolean isChildNodePrimaryTypeAllowed(Node parent, String childNodeTypeName) throws Exception{
212     NodeType childNodeType = parent.getSession().getWorkspace().getNodeTypeManager().getNodeType(childNodeTypeName);
213     //In some cases, the child node is mixins type of a nt:file example
214     if(childNodeType.isMixin()) return true;
215     List<NodeType> allNodeTypes = new ArrayList<NodeType>();
216     allNodeTypes.add(parent.getPrimaryNodeType());
217     for(NodeType mixin: parent.getMixinNodeTypes()) {
218       allNodeTypes.add(mixin);
219     }
220     for (NodeType nodetype:allNodeTypes) {
221       if (((NodeTypeImpl)nodetype).isChildNodePrimaryTypeAllowed(Constants.JCR_ANY_NAME,
222                                                                  ((NodeTypeImpl)childNodeType).getQName())) {
223         return true;
224       }
225     }
226     return false;
227   }
228 }