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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.views;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.xmap.annotation.XNode;
030import org.nuxeo.common.xmap.annotation.XNodeList;
031import org.nuxeo.common.xmap.annotation.XObject;
032import org.nuxeo.theme.Manager;
033import org.nuxeo.theme.elements.ElementType;
034import org.nuxeo.theme.formats.FormatType;
035import org.nuxeo.theme.models.ModelType;
036import org.nuxeo.theme.templates.TemplateEngineType;
037import org.nuxeo.theme.types.Type;
038import org.nuxeo.theme.types.TypeFamily;
039
040@XObject("view")
041public final class ViewType implements Type {
042
043    private static final Log log = LogFactory.getLog(ViewType.class);
044
045    @XNode("@name")
046    public String viewName = "*";
047
048    @XNode("engine")
049    public String engineName = "default";
050
051    @XNode("@template-engine")
052    private String templateEngine;
053
054    @XNode("@merge")
055    private boolean merge = false;
056
057    @XNode("mode")
058    public String mode = "*";
059
060    @XNode("icon")
061    public String icon;
062
063    @XNode("element-type")
064    public String elementTypeName = "*";
065
066    @XNode("format-type")
067    public String formatTypeName = "*";
068
069    @XNode("model-type")
070    public String modelTypeName = "*";
071
072    @XNode("class")
073    public String className;
074
075    @XNode("template")
076    public String template;
077
078    @XNodeList(value = "resource", type = ArrayList.class, componentType = String.class)
079    public List<String> resources = new ArrayList<String>();
080
081    private View view;
082
083    public ViewType() {
084    }
085
086    public ViewType(final String viewName, final String className, final String engineName,
087            final String templateEngine, final String mode, final String elementTypeName, final String modelTypeName,
088            final String formatTypeName, final String template, final List<String> resources) {
089        this.viewName = viewName;
090        this.elementTypeName = elementTypeName;
091        this.modelTypeName = modelTypeName;
092        this.formatTypeName = formatTypeName;
093        this.engineName = engineName;
094        this.templateEngine = templateEngine;
095        this.mode = mode;
096        this.className = className;
097        this.template = template;
098        this.resources = resources;
099    }
100
101    public String getTypeName() {
102        return computeName(formatTypeName, elementTypeName, viewName, modelTypeName, engineName, mode, templateEngine);
103    }
104
105    public static String computeName(final String formatTypeName, final String elementTypeName, final String viewName,
106            final String modelTypeName, final String engineName, final String mode, final String templateEngineName) {
107
108        return String.format("%s/%s/%s/%s/%s/%s/%s", formatTypeName, elementTypeName, viewName, modelTypeName,
109                engineName, mode, templateEngineName);
110    }
111
112    public TypeFamily getTypeFamily() {
113        return TypeFamily.VIEW;
114    }
115
116    public String getViewName() {
117        return viewName;
118    }
119
120    public View getView() {
121        if (view != null) {
122            return view;
123        }
124        if (className == null) {
125            className = ((TemplateEngineType) Manager.getTypeRegistry().lookup(TypeFamily.TEMPLATE_ENGINE,
126                    templateEngine)).getTemplateView();
127        }
128        try {
129            view = (View) Class.forName(className).newInstance();
130            view.setViewType(this);
131        } catch (ReflectiveOperationException e) {
132            log.error("Could not create view for: " + className);
133            return null;
134        }
135        return view;
136    }
137
138    public ElementType getElementType() {
139        return (ElementType) Manager.getTypeRegistry().lookup(TypeFamily.ELEMENT, elementTypeName);
140    }
141
142    public ModelType getModelType() {
143        return (ModelType) Manager.getTypeRegistry().lookup(TypeFamily.MODEL, modelTypeName);
144    }
145
146    public FormatType getFormatType() {
147        return (FormatType) Manager.getTypeRegistry().lookup(TypeFamily.FORMAT, formatTypeName);
148    }
149
150    public String getTemplate() {
151        return template;
152    }
153
154    public void setTemplate(final String template) {
155        this.template = template;
156    }
157
158    public List<String> getResources() {
159        return resources;
160    }
161
162    public void setResources(final List<String> resources) {
163        this.resources = resources;
164    }
165
166    public String getMode() {
167        return mode;
168    }
169
170    public void setMode(String mode) {
171        this.mode = mode;
172    }
173
174    public String getIcon() {
175        return icon;
176    }
177
178    public void setIcon(String icon) {
179        this.icon = icon;
180    }
181
182    public String getTemplateEngine() {
183        return templateEngine;
184    }
185
186    public void setTemplateEngine(String templateEngine) {
187        this.templateEngine = templateEngine;
188    }
189
190    public boolean isMerge() {
191        return merge;
192    }
193
194    public void setMerge(boolean merge) {
195        this.merge = merge;
196    }
197
198    public void addResource(String resource) {
199        if (!resources.contains(resource)) {
200            resources.add(resource);
201        }
202    }
203}