001/*
002 * (C) Copyright 2010 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.types;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeList;
027import org.nuxeo.common.xmap.annotation.XObject;
028
029/**
030 * Content view descriptor put on document types.
031 *
032 * @author Anahide Tchertchian
033 * @since 5.4
034 */
035@XObject("contentViews")
036public class DocumentContentViews implements Serializable {
037
038    private static final long serialVersionUID = 1L;
039
040    @XNode("@append")
041    boolean append = false;
042
043    @XNodeList(value = "contentView", type = DocumentContentView[].class, componentType = DocumentContentView.class)
044    DocumentContentView[] contentViews = new DocumentContentView[0];
045
046    public DocumentContentView[] getContentViews() {
047        return contentViews;
048    }
049
050    public boolean getAppend() {
051        return append;
052    }
053
054    public String[] getContentViewNames() {
055        if (contentViews != null) {
056            String[] res = new String[contentViews.length];
057            for (int i = 0; i < contentViews.length; i++) {
058                res[i] = contentViews[i].getContentViewName();
059            }
060            return res;
061        }
062        return null;
063    }
064
065    public String[] getContentViewNamesForExport() {
066        List<String> res = new ArrayList<String>();
067        if (contentViews != null) {
068            for (DocumentContentView contentView : contentViews) {
069                if (contentView.getShowInExportView()) {
070                    res.add(contentView.getContentViewName());
071                }
072            }
073        }
074        return res.toArray(new String[] {});
075    }
076
077    /**
078     * Clone to handle hot reload
079     *
080     * @since 5.6
081     */
082    @Override
083    public DocumentContentViews clone() {
084        DocumentContentViews clone = new DocumentContentViews();
085        clone.append = getAppend();
086        DocumentContentView[] cvs = getContentViews();
087        if (cvs != null) {
088            DocumentContentView[] ccvs = new DocumentContentView[cvs.length];
089            for (int i = 0; i < cvs.length; i++) {
090                ccvs[i] = cvs[i].clone();
091            }
092            clone.contentViews = ccvs;
093        }
094        return clone;
095    }
096
097}