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.drives;
18  import java.io.Serializable;
19  import java.util.*;
20  import java.util.regex.Pattern;
21  
22  /**
23   * Created by The eXo Platform SARL
24   * Author : Nguyen Quang Hung
25   *          nguyenkequanghung@yahoo.com
26   * Feb 27, 2006
27   */
28  public class DriveData implements Comparable<DriveData>, Serializable {
29  
30    /**
31     * 
32     */
33    private static final long serialVersionUID = -1483463576328793278L;
34    private String name ;
35    private String label ;
36    private String workspace ;
37    private String permissions ;
38    private String homePath ;
39    private String icon ;
40    private String views ;
41    private boolean viewPreferences ;
42    private boolean viewNonDocument ;
43    private boolean viewSideBar ;
44    private boolean showHiddenNode ;
45    private String allowCreateFolders ;
46    private String allowNodeTypesOnTree;
47    private Map<String, String> parameters = new HashMap<>();
48  
49    public  DriveData(){}
50  
51    /**
52     * Clone object
53     *
54     * @return cloned object
55     */
56    public DriveData clone() {
57      DriveData driveData = new DriveData();
58      driveData.setAllowCreateFolders(getAllowCreateFolders());
59      driveData.setAllowNodeTypesOnTree(getAllowNodeTypesOnTree());
60      driveData.setHomePath(getHomePath());
61      driveData.setIcon(getIcon());
62      driveData.setName(getName());
63      driveData.setPermissions(getPermissions());
64      driveData.setShowHiddenNode(getShowHiddenNode());
65      driveData.setViewNonDocument(getViewNonDocument());
66      driveData.setViewPreferences(getViewPreferences());
67      driveData.setViewSideBar(getViewSideBar());
68      driveData.setViews(getViews());
69      driveData.setWorkspace(getWorkspace());
70      driveData.setParameters(getParameters());
71      return driveData;
72    }
73  
74    /**
75     *
76     * @return the name of drive
77     */
78    public String getName() { return name ; }
79    /**
80     * Register drive name
81     * @param name  the name of DriveData
82     */
83    public void setName(String name) { this.name = name ; }
84  
85    /**
86     *
87     * @return the name of workspace
88     */
89    public String getWorkspace() { return workspace ; }
90    /**
91     * Register workspace to drive
92     * @param ws the workspace name
93     */
94    public void setWorkspace(String ws) { workspace = ws ; }
95  
96    /**
97     *
98     * @return the permissions of drive
99     */
100   public String getPermissions() { return this.permissions ; }
101   /**
102    * Register permission to drive
103    * @param permissions
104    */
105   public void setPermissions(String permissions) { this.permissions = permissions ; }
106 
107   public void removePermission(String permission) {
108     if (permissions == null) return;
109 
110     String[] perms = permissions.split(",");
111     StringBuilder newsPermissions = new StringBuilder();
112     for (String perm : perms) {
113       if (perm.equals(permission)) continue;
114 
115       if(newsPermissions.length() > 0) newsPermissions.append(",");
116       newsPermissions.append(perm);
117     }
118 
119     permissions = newsPermissions.toString();
120   }
121   /**
122    *
123    * @return the home path of drive
124    */
125   public String getHomePath() { return homePath ; }
126   /**
127    * Register home path to drive
128    * @param path the home path of drive
129    */
130   public void setHomePath(String path) { homePath = path ; }
131 
132   /**
133    *
134    * @return icon path
135    */
136   public String getIcon() { return icon ; }
137   /**
138    * Register icon to drive
139    * @param ico icon path
140    */
141   public void setIcon(String ico) { icon = ico ; }
142   /**
143    * @return the label
144    */
145   public String getLabel() {
146     return label;
147   }
148 
149   /**
150    * @param label the label to set
151    */
152   public void setLabel(String label) {
153     this.label= label;
154   }
155   /**
156    *
157    * @return the folder type of drive
158    */
159   public String getAllowCreateFolders() { return allowCreateFolders ; }
160   /**
161    * Register folder type to drive
162    * @param allowCreateFolders folder type
163    */
164   public void setAllowCreateFolders(String allowCreateFolders) { this.allowCreateFolders = allowCreateFolders ; }
165 
166   public String getAllowNodeTypesOnTree() { return allowNodeTypesOnTree ; }
167 
168   public void setAllowNodeTypesOnTree(String allowNodeTypesOnTree) { this.allowNodeTypesOnTree = allowNodeTypesOnTree ; }
169 
170   /**
171    *
172    * @return  the views of drive
173    */
174   public String getViews() { return views ; }
175   /**
176    * Register views to drive
177    * @param v view name
178    */
179   public void setViews(String v) { views = v ; }
180 
181   /**
182    *
183    * @return the state of view preference drive
184    */
185   public boolean getViewPreferences() { return viewPreferences ; }
186 
187   /**
188    * Register the state of view preference to drive
189    * @param b  the state of view preference
190    */
191   public void setViewPreferences(boolean b) { viewPreferences = b ; }
192 
193   /**
194    *
195    * @return the state of view non document node type of drive
196    */
197   public boolean getViewNonDocument() { return viewNonDocument ; }
198   /**
199    * Register state of view non document to drive
200    * @param b the state of view non document node type
201    */
202   public void setViewNonDocument(boolean b) { viewNonDocument = b ; }
203   /**
204    *
205    * @return the state of view side bar of drive
206    */
207   public boolean getViewSideBar() { return viewSideBar ; }
208   /**
209    * Register state of view side bar to drive
210    * @param b state of view side bar
211    */
212   public void setViewSideBar(boolean b) { viewSideBar = b ; }
213 
214   /**
215    *
216    * @return the state of show hidden node of drive
217    */
218   public boolean getShowHiddenNode() { return showHiddenNode ; }
219   /**
220    * Register state of show hidden node to drive
221    * @param b state of show hidden node
222    */
223   public void setShowHiddenNode(boolean b) { showHiddenNode = b ; }
224 
225   public Map<String, String> getParameters() {
226     return parameters;
227   }
228 
229   public void setParameters(Map<String, String> parameters) {
230     this.parameters = parameters;
231   }
232 
233   /**
234    *
235    * @return  the array of permission
236    */
237   public String[] getAllPermissions() {
238     return permissions.split(",") ;
239   }
240 
241   /**
242    * Check the state of permission is existing or not
243    * @param allPermissions  the string array permission of drive
244    * @param permission  permission name
245    * @return the state of permission is existing or not.
246    */
247   public boolean hasPermission(String[] allPermissions, String permission) {
248     List<String> permissionList = new ArrayList<String>() ;
249     for(String per : allPermissions){
250       permissionList.add(per.trim()) ;
251     }
252     if(permission == null) return false ;
253     if(permission.indexOf(":/") > -1){
254       String[] array = permission.split(":/") ;
255       if(array == null || array.length < 2) return false ;
256       if(permissionList.contains("*:/"+array[1])) return true ;
257       if(array[0].equals("*")) {
258         String[] arrPer = {};
259         for(String per : permissionList) {
260           arrPer = per.split(":/");
261           if(arrPer.length == 2 && arrPer[1].equals(array[1])) return true;
262         }
263       }
264     }
265     return permissionList.contains(permission) ;
266   }
267 
268   public String getResolvedHomePath() {
269     String resolvedHomePath = homePath;
270     if (parameters != null) {
271       if (parameters.containsKey("userId") && homePath.contains("${userId}")) {
272         resolvedHomePath = resolvedHomePath.replaceAll(Pattern.quote("${userId}"), parameters.get("userId"));
273       }
274       if (parameters.containsKey("groupId") && homePath.contains("${groupId}")) {
275         resolvedHomePath = resolvedHomePath.replaceAll(Pattern.quote("${groupId}"), parameters.get("groupId"));
276       }
277     }
278     return resolvedHomePath;
279   }
280 
281   public int compareTo(DriveData arg) {
282     return name.compareToIgnoreCase(arg.getName()) ;
283   }
284 
285   @Override
286   public boolean equals(Object o) {
287     if (this == o) return true;
288     if (o == null || getClass() != o.getClass()) return false;
289     DriveData driveData = (DriveData) o;
290     return viewPreferences == driveData.viewPreferences &&
291             viewNonDocument == driveData.viewNonDocument &&
292             viewSideBar == driveData.viewSideBar &&
293             showHiddenNode == driveData.showHiddenNode &&
294             Objects.equals(name, driveData.name) &&
295             Objects.equals(workspace, driveData.workspace) &&
296             Objects.equals(permissions, driveData.permissions) &&
297             Objects.equals(homePath, driveData.homePath) &&
298             Objects.equals(icon, driveData.icon) &&
299             Objects.equals(views, driveData.views) &&
300             Objects.equals(allowCreateFolders, driveData.allowCreateFolders) &&
301             Objects.equals(allowNodeTypesOnTree, driveData.allowNodeTypesOnTree) &&
302             Objects.equals(parameters, driveData.parameters);
303   }
304 
305   @Override
306   public int hashCode() {
307     return Objects.hash(name, workspace, permissions, homePath, icon, views, viewPreferences,
308             viewNonDocument, viewSideBar, showHiddenNode, allowCreateFolders, allowNodeTypesOnTree, parameters);
309   }
310 }