001/*
002 * (C) Copyright 2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: DirectoryTreeDescriptor.java 29556 2008-01-23 00:59:39Z jcarsique $
020 */
021package org.nuxeo.ecm.webapp.directory;
022
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XNodeList;
029import org.nuxeo.common.xmap.annotation.XObject;
030import org.nuxeo.ecm.directory.DirectoryException;
031import org.nuxeo.ecm.platform.actions.Action;
032import org.nuxeo.ecm.platform.actions.ActionPropertiesDescriptor;
033
034@XObject(value = "directoryTree")
035public class DirectoryTreeDescriptor {
036
037    /**
038     * @since 6.0
039     */
040    public static final String ACTION_ID_PREFIX = "dirtree_";
041
042    /**
043     * @since 6.0
044     */
045    public static final String NAV_ACTION_CATEGORY = "TREE_EXPLORER";
046
047    /**
048     * @since 6.0
049     */
050    public static final String DIR_ACTION_CATEGORY = "DIRECTORY_TREE_EXPLORER";
051
052    @XNode("@name")
053    protected String name;
054
055    @XNode("@enabled")
056    protected Boolean enabled = true;
057
058    @XNode("@isNavigationTree")
059    protected boolean isNavigationTree = true;
060
061    /**
062     * Label to be displayed as the root of the tree (description field).
063     */
064    @XNode("@label")
065    protected String label;
066
067    /**
068     * Content view to be updated on node selection
069     */
070    @XNode("@contentView")
071    protected String contentView;
072
073    /**
074     * Name of the QueryModel field that will be used updated on node selection.
075     */
076    @XNode("@field")
077    protected String fieldName;
078
079    /**
080     * Name of the QueryModel schema for the field that will be used updated on node selection.
081     */
082    @XNode("@schema")
083    protected String schemaName;
084
085    /**
086     * Id of the faces navigation case to return on node selection.
087     */
088    @XNode("@outcome")
089    protected String outcome;
090
091    /**
092     * Allows the selection of several nodes of the tree.
093     */
094    @XNode("@multiselect")
095    protected Boolean multiselect;
096
097    /**
098     * List of directories ids used to build the classification tree.
099     */
100    protected String[] directories;
101
102    @XNodeList(value = "directory", componentType = String.class, type = String[].class)
103    public void setDirectories(String[] directories) throws DirectoryException {
104        this.directories = directories;
105    }
106
107    /**
108     * @since 6.0
109     */
110    @XNode("@order")
111    protected Integer order;
112
113    public String getFieldName() {
114        return fieldName;
115    }
116
117    public String getName() {
118        return name;
119    }
120
121    public String[] getDirectories() {
122        return directories;
123    }
124
125    public String getLabel() {
126        return label;
127    }
128
129    public boolean isMultiselect() {
130        if (multiselect == null) {
131            return false;
132        }
133        return multiselect;
134    }
135
136    public String getOutcome() {
137        return outcome;
138    }
139
140    public String getContentView() {
141        return contentView;
142    }
143
144    public String getSchemaName() {
145        return schemaName;
146    }
147
148    public Boolean getEnabled() {
149        return enabled;
150    }
151
152    public boolean isNavigationTree() {
153        return isNavigationTree;
154    }
155
156    public boolean hasContentViewSupport() {
157        return contentView != null;
158    }
159
160    /**
161     * @since 6.0
162     */
163    public Integer getOrder() {
164        return order;
165    }
166
167    public void merge(DirectoryTreeDescriptor other) {
168        if (other.schemaName != null) {
169            this.schemaName = other.schemaName;
170        }
171        if (other.contentView != null) {
172            this.contentView = other.contentView;
173        }
174        if (other.outcome != null) {
175            this.outcome = other.outcome;
176        }
177        if (other.multiselect != null) {
178            this.multiselect = other.multiselect;
179        }
180        if (other.label != null) {
181            this.label = other.label;
182        }
183        if (other.directories != null) {
184            this.directories = other.directories;
185        }
186        if (other.fieldName != null) {
187            this.fieldName = other.fieldName;
188        }
189        this.enabled = other.enabled;
190        this.isNavigationTree = other.isNavigationTree;
191        if (other.order != null) {
192            this.order = other.order;
193        }
194    }
195
196    public DirectoryTreeDescriptor clone() {
197        DirectoryTreeDescriptor clone = new DirectoryTreeDescriptor();
198        clone.name = name;
199        clone.enabled = enabled;
200        clone.isNavigationTree = isNavigationTree;
201        clone.label = label;
202        clone.contentView = contentView;
203        clone.fieldName = fieldName;
204        clone.schemaName = schemaName;
205        clone.outcome = outcome;
206        clone.multiselect = multiselect;
207        if (directories != null) {
208            clone.directories = directories.clone();
209        }
210        clone.order = order;
211        return clone;
212    }
213
214    /**
215     * Helper to register a simple action based on the given descriptor
216     *
217     * @since 6.0
218     */
219    protected Action getAction() {
220        String[] cats;
221        if (isNavigationTree()) {
222            cats = new String[] { NAV_ACTION_CATEGORY, DIR_ACTION_CATEGORY };
223        } else {
224            cats = new String[] { DIR_ACTION_CATEGORY };
225        }
226        Action a = new Action(ACTION_ID_PREFIX + getName(), cats);
227        a.setType("rest_document_link");
228        a.setLabel(getLabel());
229        Map<String, String> props = new HashMap<String, String>();
230        props.put("ajaxSupport", "true");
231        props.put("link", "/incl/single_directory_tree_explorer.xhtml");
232        ActionPropertiesDescriptor pdesc = new ActionPropertiesDescriptor();
233        pdesc.setProperties(props);
234        a.setPropertiesDescriptor(pdesc);
235        Integer order = getOrder();
236        if (order != null) {
237            a.setOrder(order.intValue());
238        } else {
239            // use a default high default order for directory trees so that
240            // they're displayed after standard navigation trees
241            a.setOrder(1000);
242        }
243        a.setIcon("/img/" + getName() + ".png");
244        // need to set a non-empty list
245        a.setEnabled(Boolean.TRUE.equals(getEnabled()));
246        a.setFilterIds(new ArrayList<String>());
247        return a;
248    }
249
250}