001/*
002 * (C) Copyright 2006-2016 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 */
019package org.nuxeo.ecm.platform.types.adapter;
020
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.platform.types.DocumentContentViews;
026import org.nuxeo.ecm.platform.types.SubType;
027import org.nuxeo.ecm.platform.types.Type;
028import org.nuxeo.ecm.platform.types.TypeManager;
029import org.nuxeo.ecm.platform.types.TypeView;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
034 */
035public class TypeInfoAdapter implements TypeInfo {
036
037    private final Type type;
038
039    public TypeInfoAdapter(DocumentModel doc) {
040        TypeManager mgr = Framework.getService(TypeManager.class);
041        type = mgr.getType(doc.getType());
042    }
043
044    public String[] getActions() {
045        if (type != null) {
046            return type.getActions();
047        }
048        return null;
049    }
050
051    public Map<String, SubType> getAllowedSubTypes() {
052        if (type != null) {
053            return type.getAllowedSubTypes();
054        }
055
056        return null;
057    }
058
059    public String getCreateView() {
060        if (type != null) {
061            return type.getCreateView();
062        }
063
064        return null;
065    }
066
067    public String getDefaultView() {
068        if (type != null) {
069            return type.getDefaultView();
070        }
071
072        return null;
073    }
074
075    public String getEditView() {
076        if (type != null) {
077            return type.getEditView();
078        }
079
080        return null;
081    }
082
083    public String getIcon() {
084        if (type != null) {
085            return type.getIcon();
086        }
087
088        return null;
089    }
090
091    public String getIconExpanded() {
092        if (type != null) {
093            return type.getIconExpanded();
094        }
095
096        return null;
097    }
098
099    public String getBigIcon() {
100        if (type != null) {
101            return type.getBigIcon();
102        }
103        return null;
104    }
105
106    public String getBigIconExpanded() {
107        if (type != null) {
108            return type.getBigIconExpanded();
109        }
110        return null;
111    }
112
113    public String getId() {
114        if (type != null) {
115            return type.getId();
116        }
117
118        return null;
119    }
120
121    public String getLabel() {
122        if (type != null) {
123            return type.getLabel();
124        }
125
126        return null;
127    }
128
129    public String getDescription() {
130        if (type != null) {
131            return type.getDescription();
132        }
133
134        return null;
135    }
136
137    public String[] getLayouts(String mode) {
138        if (type != null) {
139            return type.getLayouts(mode);
140        }
141        return null;
142    }
143
144    public String[] getLayouts(String mode, String defaultMode) {
145        if (type != null) {
146            return type.getLayouts(mode, defaultMode);
147        }
148        return null;
149    }
150
151    public String getView(String viewId) {
152        if (type != null) {
153            TypeView view = type.getView(viewId);
154            if (view != null) {
155                return view.getValue();
156            }
157        }
158        return null;
159    }
160
161    public TypeView[] getViews() {
162        if (type != null) {
163            return type.getViews();
164        }
165
166        return null;
167    }
168
169    public String[] getContentViews(String category) {
170        if (type != null) {
171            return type.getContentViews(category);
172        }
173        return null;
174    }
175
176    @Override
177    public Map<String, String[]> getContentViews() {
178        if (type != null) {
179            Map<String, String[]> res = new LinkedHashMap<>();
180            Map<String, DocumentContentViews> defs = type.getContentViews();
181            if (defs != null) {
182                for (Map.Entry<String, DocumentContentViews> def : defs.entrySet()) {
183                    res.put(def.getKey(), def.getValue().getContentViewNames());
184                }
185            }
186            return res;
187        }
188        return null;
189    }
190
191    @Override
192    public Map<String, String[]> getContentViewsForExport() {
193        if (type != null) {
194            Map<String, String[]> res = new LinkedHashMap<>();
195            Map<String, DocumentContentViews> defs = type.getContentViews();
196            if (defs != null) {
197                for (Map.Entry<String, DocumentContentViews> def : defs.entrySet()) {
198                    String[] cvsByCat = def.getValue().getContentViewNamesForExport();
199                    if (cvsByCat != null && cvsByCat.length > 0) {
200                        res.put(def.getKey(), cvsByCat);
201                    }
202                }
203            }
204            return res;
205        }
206        return null;
207    }
208}