001/*
002 * (C) Copyright 2006-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: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.types;
023
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.nuxeo.common.xmap.annotation.XNode;
031import org.nuxeo.common.xmap.annotation.XNodeList;
032import org.nuxeo.common.xmap.annotation.XNodeMap;
033import org.nuxeo.common.xmap.annotation.XObject;
034import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
035
036@XObject("type")
037public class Type {
038
039    public static final String[] EMPTY_ACTIONS = new String[0];
040
041    @XNode("@id")
042    protected String id;
043
044    @XNode("icon")
045    protected String icon;
046
047    @XNode("icon-expanded")
048    protected String iconExpanded;
049
050    @XNode("bigIcon")
051    protected String bigIcon;
052
053    @XNode("bigIcon-expanded")
054    protected String bigIconExpanded;
055
056    @XNode("label")
057    protected String label;
058
059    protected Type(String id) {
060        this.id = id;
061    }
062
063    public Type() {
064
065    }
066
067    protected Map<String, SubType> allowedSubTypes = new HashMap<>();
068
069    @XNodeList(value = "subtypes/type", type = ArrayList.class, componentType = SubType.class)
070    public void addSubType(List<SubType> subTypes) {
071        if (allowedSubTypes == null) {
072            allowedSubTypes = new HashMap<>();
073        }
074
075        for (SubType currentSubType : subTypes) {
076            SubType subTypeToMerge = allowedSubTypes.get(currentSubType.name);
077            if (subTypeToMerge == null) {
078                allowedSubTypes.put(currentSubType.name, currentSubType);
079            } else {
080                List<String> currentSubTypeHidden = currentSubType.getHidden();
081                List<String> subTypeToMergeHidden = subTypeToMerge.getHidden();
082                for (String hidden : currentSubTypeHidden) {
083                    if (!subTypeToMergeHidden.contains(hidden)) {
084                        subTypeToMergeHidden.add(hidden);
085                    }
086                }
087            }
088        }
089    }
090
091    @XNodeList(value = "deniedSubtypes/type", type = String[].class, componentType = String.class)
092    protected String[] deniedSubTypes;
093
094    @XNode("default-view")
095    protected String defaultView;
096
097    @XNode("create-view")
098    protected String createView;
099
100    @XNode("edit-view")
101    protected String editView;
102
103    @XNode("description")
104    protected String description;
105
106    @XNode("category")
107    protected String category;
108
109    protected Map<String, TypeView> views;
110
111    @XNodeList(value = "actions/action", type = String[].class, componentType = String.class)
112    protected String[] actions;
113
114    @XNodeMap(value = "layouts", key = "@mode", type = HashMap.class, componentType = Layouts.class)
115    Map<String, Layouts> layouts;
116
117    @XNodeMap(value = "contentViews", key = "@category", type = HashMap.class, componentType = DocumentContentViews.class)
118    protected Map<String, DocumentContentViews> contentViews;
119
120    // for bundle update::
121    @XNode("@remove")
122    protected boolean remove = false;
123
124    public String[] getActions() {
125        return actions;
126    }
127
128    public void setActions(String[] actions) {
129        this.actions = actions;
130    }
131
132    public String getIcon() {
133        return icon;
134    }
135
136    public void setIcon(String icon) {
137        this.icon = icon;
138    }
139
140    public String getBigIcon() {
141        return bigIcon;
142    }
143
144    public void setBigIcon(String bigIcon) {
145        this.bigIcon = bigIcon;
146    }
147
148    public String getBigIconExpanded() {
149        return bigIconExpanded;
150    }
151
152    public void setBigIconExpanded(String bigIconExpanded) {
153        this.bigIconExpanded = bigIconExpanded;
154    }
155
156    public String getLabel() {
157        return label;
158    }
159
160    public void setLabel(String label) {
161        this.label = label;
162    }
163
164    public String getId() {
165        return id;
166    }
167
168    public void setId(String id) {
169        this.id = id;
170    }
171
172    public String getDescription() {
173        return description;
174    }
175
176    public void setDescription(String description) {
177        this.description = description;
178    }
179
180    public String getCategory() {
181        return category;
182    }
183
184    public void setCategory(String category) {
185        this.category = category;
186    }
187
188    /**
189     * Returns layout names given a mode.
190     */
191    public String[] getLayouts(String mode) {
192        // default to mode ANY
193        return getLayouts(mode, BuiltinModes.ANY);
194    }
195
196    public String[] getLayouts(String mode, String defaultMode) {
197        if (layouts != null) {
198            Layouts layouts = this.layouts.get(mode);
199            if (layouts == null && defaultMode != null) {
200                layouts = this.layouts.get(defaultMode);
201            }
202            if (layouts != null) {
203                return layouts.getLayouts();
204            }
205        }
206        return null;
207    }
208
209    /**
210     * Returns the layouts map
211     */
212    public Map<String, Layouts> getLayouts() {
213        return Collections.unmodifiableMap(layouts);
214    }
215
216    public void setLayouts(Map<String, Layouts> layouts) {
217        this.layouts = layouts;
218    }
219
220    public String getDefaultView() {
221        return defaultView;
222    }
223
224    public void setDefaultView(String defaultView) {
225        this.defaultView = defaultView;
226    }
227
228    public String getCreateView() {
229        return createView;
230    }
231
232    public void setCreateView(String createView) {
233        this.createView = createView;
234    }
235
236    public String getEditView() {
237        return editView;
238    }
239
240    public void setEditView(String editView) {
241        this.editView = editView;
242    }
243
244    public TypeView[] getViews() {
245        return views.values().toArray(new TypeView[views.size()]);
246    }
247
248    @XNodeList(value = "views/view", type = TypeView[].class, componentType = TypeView.class)
249    public void setViews(TypeView[] views) {
250        this.views = new HashMap<>();
251        for (TypeView view : views) {
252            this.views.put(view.getId(), view);
253        }
254    }
255
256    public TypeView getView(String viewId) {
257        return views.get(viewId);
258    }
259
260    public void setView(TypeView view) {
261        views.put(view.getId(), view);
262    }
263
264    public String[] getDeniedSubTypes() {
265        return deniedSubTypes;
266    }
267
268    public void setDeniedSubTypes(String[] deniedSubTypes) {
269        this.deniedSubTypes = deniedSubTypes;
270    }
271
272    public Map<String, SubType> getAllowedSubTypes() {
273        return allowedSubTypes;
274    }
275
276    public void setAllowedSubTypes(Map<String, SubType> allowedSubTypes) {
277        this.allowedSubTypes = allowedSubTypes;
278    }
279
280    public boolean getRemove() {
281        return remove;
282    }
283
284    public void setRemove(boolean remove) {
285        this.remove = remove;
286    }
287
288    @Override
289    public String toString() {
290        final StringBuilder sb = new StringBuilder();
291        sb.append(Type.class.getSimpleName());
292        sb.append(" {");
293        sb.append("id: ");
294        sb.append(id);
295        sb.append('}');
296        return sb.toString();
297    }
298
299    public String getIconExpanded() {
300        return iconExpanded;
301    }
302
303    public void setIconExpanded(String iconExpanded) {
304        this.iconExpanded = iconExpanded;
305    }
306
307    /**
308     * Return content views defined on this document type for given category
309     *
310     * @since 5.4
311     */
312    public String[] getContentViews(String category) {
313        if (contentViews != null) {
314            DocumentContentViews cv = contentViews.get(category);
315            if (cv != null) {
316                return cv.getContentViewNames();
317            }
318        }
319        return null;
320    }
321
322    public Map<String, DocumentContentViews> getContentViews() {
323        return Collections.unmodifiableMap(contentViews);
324    }
325
326    public void setContentViews(Map<String, DocumentContentViews> contentViews) {
327        this.contentViews = contentViews;
328    }
329
330    /**
331     * Clone method to handle hot reload
332     *
333     * @since 5.6
334     */
335    @Override
336    public Type clone() {
337        Type clone = new Type();
338        clone.setId(getId());
339        clone.setIcon(getIcon());
340        clone.setIconExpanded(getIconExpanded());
341        clone.setBigIcon(getBigIcon());
342        clone.setBigIconExpanded(getBigIconExpanded());
343        clone.setLabel(getLabel());
344        Map<String, SubType> subs = getAllowedSubTypes();
345        if (subs != null) {
346            Map<String, SubType> csubs = new HashMap<>();
347            for (Map.Entry<String, SubType> item : subs.entrySet()) {
348                csubs.put(item.getKey(), item.getValue().clone());
349            }
350            clone.setAllowedSubTypes(csubs);
351        }
352        String[] denied = getDeniedSubTypes();
353        if (denied != null) {
354            clone.setDeniedSubTypes(denied.clone());
355        }
356        clone.setDefaultView(getDefaultView());
357        clone.setCreateView(getCreateView());
358        clone.setEditView(getEditView());
359        clone.setDescription(getDescription());
360        clone.setCategory(getCategory());
361        if (views != null) {
362            Map<String, TypeView> cviews = new HashMap<>();
363            for (Map.Entry<String, TypeView> item : views.entrySet()) {
364                cviews.put(item.getKey(), item.getValue().clone());
365            }
366            clone.views = cviews;
367        }
368        String[] actions = getActions();
369        if (actions != null) {
370            clone.setActions(actions.clone());
371        }
372        // do not clone old layout definition, nobody's using it anymore
373        Map<String, Layouts> layouts = getLayouts();
374        if (layouts != null) {
375            Map<String, Layouts> clayouts = new HashMap<>();
376            for (Map.Entry<String, Layouts> item : layouts.entrySet()) {
377                clayouts.put(item.getKey(), item.getValue().clone());
378            }
379            clone.setLayouts(clayouts);
380        }
381        Map<String, DocumentContentViews> cvs = getContentViews();
382        if (cvs != null) {
383            Map<String, DocumentContentViews> ccvs = new HashMap<>();
384            for (Map.Entry<String, DocumentContentViews> item : cvs.entrySet()) {
385                ccvs.put(item.getKey(), item.getValue().clone());
386            }
387            clone.setContentViews(ccvs);
388        }
389        clone.setRemove(getRemove());
390        return clone;
391    }
392
393}