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    @Override
052    public Map<String, SubType> getAllowedSubTypes() {
053        if (type != null) {
054            return type.getAllowedSubTypes();
055        }
056
057        return null;
058    }
059
060    @Override
061    public String getCreateView() {
062        if (type != null) {
063            return type.getCreateView();
064        }
065
066        return null;
067    }
068
069    @Override
070    public String getDefaultView() {
071        if (type != null) {
072            return type.getDefaultView();
073        }
074
075        return null;
076    }
077
078    @Override
079    public String getEditView() {
080        if (type != null) {
081            return type.getEditView();
082        }
083
084        return null;
085    }
086
087    @Override
088    public String getIcon() {
089        if (type != null) {
090            return type.getIcon();
091        }
092
093        return null;
094    }
095
096    @Override
097    public String getIconExpanded() {
098        if (type != null) {
099            return type.getIconExpanded();
100        }
101
102        return null;
103    }
104
105    @Override
106    public String getBigIcon() {
107        if (type != null) {
108            return type.getBigIcon();
109        }
110        return null;
111    }
112
113    @Override
114    public String getBigIconExpanded() {
115        if (type != null) {
116            return type.getBigIconExpanded();
117        }
118        return null;
119    }
120
121    @Override
122    public String getId() {
123        if (type != null) {
124            return type.getId();
125        }
126
127        return null;
128    }
129
130    @Override
131    public String getLabel() {
132        if (type != null) {
133            return type.getLabel();
134        }
135
136        return null;
137    }
138
139    @Override
140    public String getDescription() {
141        if (type != null) {
142            return type.getDescription();
143        }
144
145        return null;
146    }
147
148    @Override
149    public String[] getLayouts(String mode) {
150        if (type != null) {
151            return type.getLayouts(mode);
152        }
153        return null;
154    }
155
156    @Override
157    public String[] getLayouts(String mode, String defaultMode) {
158        if (type != null) {
159            return type.getLayouts(mode, defaultMode);
160        }
161        return null;
162    }
163
164    @Override
165    public String getView(String viewId) {
166        if (type != null) {
167            TypeView view = type.getView(viewId);
168            if (view != null) {
169                return view.getValue();
170            }
171        }
172        return null;
173    }
174
175    @Override
176    public TypeView[] getViews() {
177        if (type != null) {
178            return type.getViews();
179        }
180
181        return null;
182    }
183
184    @Override
185    public String[] getContentViews(String category) {
186        if (type != null) {
187            return type.getContentViews(category);
188        }
189        return null;
190    }
191
192    @Override
193    public Map<String, String[]> getContentViews() {
194        if (type != null) {
195            Map<String, String[]> res = new LinkedHashMap<>();
196            Map<String, DocumentContentViews> defs = type.getContentViews();
197            if (defs != null) {
198                for (Map.Entry<String, DocumentContentViews> def : defs.entrySet()) {
199                    res.put(def.getKey(), def.getValue().getContentViewNames());
200                }
201            }
202            return res;
203        }
204        return null;
205    }
206
207    @Override
208    public Map<String, String[]> getContentViewsForExport() {
209        if (type != null) {
210            Map<String, String[]> res = new LinkedHashMap<>();
211            Map<String, DocumentContentViews> defs = type.getContentViews();
212            if (defs != null) {
213                for (Map.Entry<String, DocumentContentViews> def : defs.entrySet()) {
214                    String[] cvsByCat = def.getValue().getContentViewNamesForExport();
215                    if (cvsByCat != null && cvsByCat.length > 0) {
216                        res.put(def.getKey(), cvsByCat);
217                    }
218                }
219            }
220            return res;
221        }
222        return null;
223    }
224}