001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.contentview.jsf;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XNodeList;
025import org.nuxeo.common.xmap.annotation.XObject;
026import org.nuxeo.ecm.platform.query.core.CoreQueryPageProviderDescriptor;
027import org.nuxeo.ecm.platform.query.core.GenericPageProviderDescriptor;
028import org.nuxeo.ecm.platform.query.core.ReferencePageProviderDescriptor;
029
030/**
031 * Descriptor for content view registration.
032 *
033 * @author Anahide Tchertchian
034 * @since 5.4
035 */
036@XObject("contentView")
037public class ContentViewDescriptor {
038
039    @XNode("@name")
040    String name;
041
042    @XNode("@enabled")
043    boolean enabled = true;
044
045    @XNode("title")
046    String title;
047
048    @XNode("translateTitle")
049    Boolean translateTitle;
050
051    @XNode("emptySentence")
052    String emptySentence;
053
054    /**
055     * @since 7.4
056     */
057    @XNode("waitForExecutionSentence")
058    String waitForExecutionSentence;
059
060    @XNode("translateEmptySentence")
061    Boolean translateEmptySentence;
062
063    @XNode("iconPath")
064    String iconPath;
065
066    @XNode("coreQueryPageProvider")
067    CoreQueryPageProviderDescriptor coreQueryPageProvider;
068
069    @XNode("genericPageProvider")
070    GenericPageProviderDescriptor genericPageProvider;
071
072    @XNode("pageProvider")
073    ReferencePageProviderDescriptor referencePageProvider;
074
075    @XNode("selectionList")
076    String selectionList;
077
078    @XNode("pagination")
079    String pagination;
080
081    @XNodeList(value = "actions@category", type = ArrayList.class, componentType = String.class)
082    List<String> actionCategories;
083
084    @XNode("searchDocument")
085    String searchDocument;
086
087    @XNode("searchLayout")
088    ContentViewLayoutImpl searchLayout;
089
090    @XNode("resultLayouts@append")
091    Boolean appendResultLayouts;
092
093    @XNodeList(value = "resultLayouts/layout", type = ArrayList.class, componentType = ContentViewLayoutImpl.class)
094    List<ContentViewLayout> resultLayouts;
095
096    @XNode("resultColumns")
097    String resultColumns;
098
099    /**
100     * @since 6.0
101     */
102    @XNode("resultLayout")
103    String resultLayout;
104
105    @XNodeList(value = "flags/flag", type = ArrayList.class, componentType = String.class)
106    List<String> flags;
107
108    @XNode("cacheKey")
109    String cacheKey;
110
111    @XNode("cacheSize")
112    Integer cacheSize;
113
114    @XNode("useGlobalPageSize")
115    Boolean useGlobalPageSize;
116
117    @XNode("showTitle")
118    Boolean showTitle;
119
120    @XNode("showPageSizeSelector")
121    Boolean showPageSizeSelector;
122
123    @XNode("showRefreshCommand")
124    Boolean showRefreshCommand;
125
126    @XNode("showFilterForm")
127    Boolean showFilterForm;
128
129    /**
130     * @since 7.4
131     */
132    @XNode("waitForExecution")
133    Boolean waitForExecution;
134
135    @XNodeList(value = "refresh/event", type = ArrayList.class, componentType = String.class)
136    List<String> refreshEventNames;
137
138    @XNodeList(value = "reset/event", type = ArrayList.class, componentType = String.class)
139    List<String> resetEventNames;
140
141    public String getName() {
142        return name;
143    }
144
145    public CoreQueryPageProviderDescriptor getCoreQueryPageProvider() {
146        return coreQueryPageProvider;
147    }
148
149    public GenericPageProviderDescriptor getGenericPageProvider() {
150        return genericPageProvider;
151    }
152
153    public ReferencePageProviderDescriptor getReferencePageProvider() {
154        return referencePageProvider;
155    }
156
157    // @since 6.0
158    protected String pageProviderName;
159
160    // @since 6.0
161    protected Map<String, String> pageProviderProperties;
162
163    public String getSelectionListName() {
164        return selectionList;
165    }
166
167    public String getPagination() {
168        return pagination;
169    }
170
171    public List<String> getActionCategories() {
172        return actionCategories;
173    }
174
175    public ContentViewLayoutImpl getSearchLayout() {
176        return searchLayout;
177    }
178
179    public Boolean getAppendResultLayouts() {
180        return appendResultLayouts;
181    }
182
183    public List<ContentViewLayout> getResultLayouts() {
184        return resultLayouts;
185    }
186
187    public String getCacheKey() {
188        return cacheKey;
189    }
190
191    public Integer getCacheSize() {
192        return cacheSize;
193    }
194
195    public List<String> getRefreshEventNames() {
196        return refreshEventNames;
197    }
198
199    public List<String> getResetEventNames() {
200        return resetEventNames;
201    }
202
203    public Boolean getUseGlobalPageSize() {
204        return useGlobalPageSize;
205    }
206
207    public String getIconPath() {
208        return iconPath;
209    }
210
211    public String getTitle() {
212        return title;
213    }
214
215    public Boolean getTranslateTitle() {
216        return translateTitle;
217    }
218
219    public String getSearchDocumentBinding() {
220        return searchDocument;
221    }
222
223    public String getResultColumnsBinding() {
224        return resultColumns;
225    }
226
227    /**
228     * @since 6.0
229     */
230    public String getResultLayoutBinding() {
231        return resultLayout;
232    }
233
234    public List<String> getFlags() {
235        return flags;
236    }
237
238    public boolean isEnabled() {
239        return enabled;
240    }
241
242    public void setEnabled(boolean enabled) {
243        this.enabled = enabled;
244    }
245
246    /**
247     * @since 5.4.2
248     */
249    public Boolean getShowTitle() {
250        return showTitle;
251    }
252
253    /**
254     * @since 5.4.2
255     */
256    public Boolean getShowPageSizeSelector() {
257        if (showPageSizeSelector == null) {
258            // default value
259            return Boolean.TRUE;
260        }
261        return showPageSizeSelector;
262    }
263
264    /**
265     * @since 5.4.2
266     */
267    public Boolean getShowRefreshCommand() {
268        if (showRefreshCommand == null) {
269            // default value
270            return Boolean.TRUE;
271        }
272        return showRefreshCommand;
273    }
274
275    /**
276     * @since 5.4.2
277     */
278    public Boolean getShowFilterForm() {
279        return showFilterForm;
280    }
281
282    /**
283     * @since 5.4.2
284     */
285    public String getEmptySentence() {
286        return emptySentence;
287    }
288
289    /**
290     * @since 7.4
291     */
292    public String getWaitForExecutionSentence() {
293        return waitForExecutionSentence;
294    }
295
296    /**
297     * @since 5.4.2
298     */
299    public Boolean getTranslateEmptySentence() {
300        return translateEmptySentence;
301    }
302
303    /**
304     * @since 6.0
305     */
306    public String getPageProviderName() {
307        if (pageProviderName == null) {
308            if (referencePageProvider != null && referencePageProvider.isEnabled()) {
309                pageProviderName = referencePageProvider.getName();
310            } else if (coreQueryPageProvider != null && coreQueryPageProvider.isEnabled()
311                    && coreQueryPageProvider.getName() != null) {
312                pageProviderName = coreQueryPageProvider.getName();
313            } else if (genericPageProvider != null && genericPageProvider.isEnabled()
314                    && genericPageProvider.getName() != null) {
315                pageProviderName = genericPageProvider.getName();
316            } else {
317                pageProviderName = getName();
318            }
319        }
320        return pageProviderName;
321    }
322
323    /**
324     * @since 6.0
325     */
326    public Map<String, String> getPageProviderProperties() {
327        if (pageProviderProperties == null) {
328            if (referencePageProvider != null && referencePageProvider.isEnabled()) {
329                pageProviderProperties = referencePageProvider.getProperties();
330            } else if (coreQueryPageProvider != null && coreQueryPageProvider.isEnabled()) {
331                pageProviderProperties = coreQueryPageProvider.getProperties();
332
333            } else if (genericPageProvider != null && genericPageProvider.isEnabled()) {
334                pageProviderProperties = genericPageProvider.getProperties();
335            }
336        }
337        return pageProviderProperties;
338    }
339
340    /**
341     * @since 7.4
342     */
343    public Boolean getWaitForExecution() {
344        return waitForExecution;
345    }
346
347    /**
348     * @since 7.4
349     */
350    public void merge(ContentViewDescriptor newDesc) {
351        this.setEnabled(newDesc.isEnabled());
352
353        String title = newDesc.getTitle();
354        if (title != null) {
355            this.title = title;
356        }
357
358        Boolean translateTitle = newDesc.getTranslateTitle();
359        if (translateTitle != null) {
360            this.translateTitle = translateTitle;
361        }
362
363        String emptySentence = newDesc.getEmptySentence();
364        if (emptySentence != null) {
365            this.emptySentence = emptySentence;
366        }
367
368        String waitForExecutionSentence = newDesc.getEmptySentence();
369        if (waitForExecutionSentence != null) {
370            this.waitForExecutionSentence = waitForExecutionSentence;
371        }
372
373        Boolean translateEmptySentence = newDesc.getTranslateEmptySentence();
374        if (translateEmptySentence != null) {
375            this.translateEmptySentence = translateEmptySentence;
376        }
377
378        String iconPath = newDesc.getIconPath();
379        if (iconPath != null) {
380            this.iconPath = iconPath;
381        }
382
383        List<String> actions = newDesc.getActionCategories();
384        if (actions != null && !actions.isEmpty()) {
385            this.actionCategories = actions;
386        }
387
388        String cacheKey = newDesc.getCacheKey();
389        if (cacheKey != null) {
390            this.cacheKey = cacheKey;
391        }
392
393        Integer cacheSize = newDesc.getCacheSize();
394        if (cacheSize != null) {
395            this.cacheSize = cacheSize;
396        }
397
398        CoreQueryPageProviderDescriptor coreDesc = newDesc.getCoreQueryPageProvider();
399        if (coreDesc != null && coreDesc.isEnabled()) {
400            this.coreQueryPageProvider = coreDesc;
401            // make sure other page providers are reset
402            this.genericPageProvider = null;
403            this.referencePageProvider = null;
404        }
405
406        GenericPageProviderDescriptor genDesc = newDesc.getGenericPageProvider();
407        if (genDesc != null && genDesc.isEnabled()) {
408            this.genericPageProvider = genDesc;
409            // make sure other page providers are reset
410            this.coreQueryPageProvider = null;
411            this.referencePageProvider = null;
412        }
413
414        ReferencePageProviderDescriptor refDesc = newDesc.getReferencePageProvider();
415        if (refDesc != null && refDesc.isEnabled()) {
416            this.referencePageProvider = refDesc;
417            // make sure other page providers are reset
418            this.coreQueryPageProvider = null;
419            this.genericPageProvider = null;
420        }
421
422        String pagination = newDesc.getPagination();
423        if (pagination != null) {
424            this.pagination = pagination;
425        }
426
427        List<String> events = newDesc.getRefreshEventNames();
428        if (events != null && !events.isEmpty()) {
429            this.refreshEventNames = events;
430        }
431        events = newDesc.getResetEventNames();
432        if (events != null && !events.isEmpty()) {
433            this.resetEventNames = events;
434        }
435
436        ContentViewLayoutImpl searchLayout = newDesc.getSearchLayout();
437        if (searchLayout != null) {
438            this.searchLayout = searchLayout;
439        }
440
441        List<ContentViewLayout> resultLayouts = newDesc.getResultLayouts();
442        if (resultLayouts != null) {
443            Boolean appendResultLayout = newDesc.getAppendResultLayouts();
444            if (Boolean.TRUE.equals(appendResultLayout) || resultLayouts.isEmpty()) {
445                List<ContentViewLayout> allLayouts = new ArrayList<ContentViewLayout>();
446                if (this.resultLayouts != null) {
447                    allLayouts.addAll(this.resultLayouts);
448                }
449                allLayouts.addAll(resultLayouts);
450                this.resultLayouts = allLayouts;
451            } else {
452                this.resultLayouts = resultLayouts;
453            }
454        }
455
456        List<String> flags = newDesc.getFlags();
457        if (flags != null && !flags.isEmpty()) {
458            this.flags = flags;
459        }
460
461        String selectionList = newDesc.getSelectionListName();
462        if (selectionList != null) {
463            this.selectionList = selectionList;
464        }
465
466        Boolean useGlobalPageSize = newDesc.getUseGlobalPageSize();
467        if (useGlobalPageSize != null) {
468            this.useGlobalPageSize = useGlobalPageSize;
469        }
470
471        Boolean showTitle = newDesc.getShowTitle();
472        if (showTitle != null) {
473            this.showTitle = showTitle;
474        }
475
476        // avoid override when setting the default value => use the field, not
477        // the API, for merge
478        Boolean showPageSizeSelector = newDesc.showPageSizeSelector;
479        if (showPageSizeSelector != null) {
480            this.showPageSizeSelector = showPageSizeSelector;
481        }
482
483        Boolean showRefreshCommand = newDesc.showRefreshCommand;
484        if (showRefreshCommand != null) {
485            this.showRefreshCommand = showRefreshCommand;
486        }
487
488        Boolean showFilterForm = newDesc.getShowFilterForm();
489        if (showFilterForm != null) {
490            this.showFilterForm = showFilterForm;
491        }
492
493        String searchDocument = newDesc.getSearchDocumentBinding();
494        if (searchDocument != null) {
495            this.searchDocument = searchDocument;
496        }
497
498        String resultCols = newDesc.getResultColumnsBinding();
499        if (resultCols != null) {
500            this.resultColumns = resultCols;
501        }
502
503        String resultLayout = newDesc.getResultLayoutBinding();
504        if (resultLayout != null) {
505            this.resultLayout = resultLayout;
506        }
507
508        Boolean waitForFilter = newDesc.getWaitForExecution();
509        if (waitForFilter != null) {
510            this.waitForExecution = waitForFilter;
511        }
512
513    }
514
515    @Override
516    public ContentViewDescriptor clone() {
517        ContentViewDescriptor clone = new ContentViewDescriptor();
518        clone.name = getName();
519        clone.enabled = isEnabled();
520        clone.title = getTitle();
521        clone.translateTitle = getTranslateTitle();
522        clone.emptySentence = getEmptySentence();
523        clone.waitForExecutionSentence = getWaitForExecutionSentence();
524        clone.translateEmptySentence = getTranslateEmptySentence();
525        clone.iconPath = getIconPath();
526        CoreQueryPageProviderDescriptor cpp = getCoreQueryPageProvider();
527        if (cpp != null) {
528            clone.coreQueryPageProvider = cpp.clone();
529        }
530        GenericPageProviderDescriptor gpp = getGenericPageProvider();
531        if (gpp != null) {
532            clone.genericPageProvider = gpp.clone();
533        }
534        ReferencePageProviderDescriptor rpp = getReferencePageProvider();
535        if (rpp != null) {
536            clone.referencePageProvider = rpp.clone();
537        }
538        clone.selectionList = getSelectionListName();
539        clone.pagination = getPagination();
540        List<String> actionCats = getActionCategories();
541        if (actionCats != null) {
542            clone.actionCategories = new ArrayList<String>();
543            clone.actionCategories.addAll(actionCats);
544        }
545        clone.searchDocument = getSearchDocumentBinding();
546        ContentViewLayoutImpl searchLayout = getSearchLayout();
547        if (searchLayout != null) {
548            clone.searchLayout = searchLayout.clone();
549        }
550        clone.appendResultLayouts = getAppendResultLayouts();
551        List<ContentViewLayout> resultLayouts = getResultLayouts();
552        if (resultLayouts != null) {
553            clone.resultLayouts = new ArrayList<ContentViewLayout>();
554            for (ContentViewLayout item : resultLayouts) {
555                clone.resultLayouts.add(item.clone());
556            }
557        }
558        clone.resultColumns = getResultColumnsBinding();
559        clone.resultLayout = getResultLayoutBinding();
560        List<String> flags = getFlags();
561        if (flags != null) {
562            clone.flags = new ArrayList<String>();
563            clone.flags.addAll(flags);
564        }
565        clone.cacheKey = getCacheKey();
566        clone.cacheSize = getCacheSize();
567        clone.useGlobalPageSize = getUseGlobalPageSize();
568        clone.showTitle = getShowTitle();
569        clone.showPageSizeSelector = getShowPageSizeSelector();
570        clone.showRefreshCommand = getShowRefreshCommand();
571        clone.showFilterForm = getShowFilterForm();
572        List<String> refresh = getRefreshEventNames();
573        if (refresh != null) {
574            clone.refreshEventNames = new ArrayList<String>();
575            clone.refreshEventNames.addAll(refresh);
576        }
577        List<String> reset = getResetEventNames();
578        if (reset != null) {
579            clone.resetEventNames = new ArrayList<String>();
580            clone.resetEventNames.addAll(reset);
581        }
582        clone.waitForExecution = getWaitForExecution();
583        return clone;
584    }
585}