001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.themes;
016
017import java.net.MalformedURLException;
018import java.net.URL;
019import java.util.Date;
020import java.util.List;
021
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XObject;
024import org.nuxeo.runtime.model.RuntimeContext;
025import org.nuxeo.theme.types.Type;
026import org.nuxeo.theme.types.TypeFamily;
027
028@XObject("theme")
029public class ThemeDescriptor implements Type {
030
031    protected RuntimeContext ctx;
032
033    /**
034     * Is the theme configured as an runtime contribution?
035     */
036    private boolean configured = false;
037
038    /**
039     * Is the theme customized?
040     */
041    private boolean customized = false;
042
043    /**
044     * Is the theme a customization of another theme?
045     */
046    private boolean customization = false;
047
048    private Date lastSaved;
049
050    private Date lastLoaded;
051
052    private boolean loadingFailed = true;
053
054    private String name;
055
056    private URL url;
057
058    private List<String> templateEngines;
059
060    private String resourceBankName;
061
062    @XNode("src")
063    public String src = "";
064
065    public void setContext(RuntimeContext ctx) {
066        this.ctx = ctx;
067    }
068
069    public RuntimeContext getContext() {
070        return ctx;
071    }
072
073    @Override
074    public TypeFamily getTypeFamily() {
075        return TypeFamily.THEME;
076    }
077
078    @Override
079    public String getTypeName() {
080        return src;
081    }
082
083    private URL getUrl() {
084        if (url != null) {
085            return url;
086        }
087        try {
088            url = new URL(src);
089        } catch (MalformedURLException e) {
090            return null;
091        }
092        return url;
093    }
094
095    public void setConfigured(boolean configured) {
096        this.configured = configured;
097    }
098
099    public boolean isCustom() {
100        return !isXmlConfigured();
101    }
102
103    public boolean isLoaded() {
104        return lastLoaded != null;
105    }
106
107    public boolean isXmlConfigured() {
108        return configured;
109    }
110
111    public boolean isWritable() {
112        if (getUrl() == null) {
113            // themes with missing definition are not writable
114            return false;
115        }
116        final String protocol = getUrl().getProtocol();
117        return protocol.equals("ftp") || protocol.equals("file");
118    }
119
120    public boolean isLoadable() {
121        return !isLoaded();
122    }
123
124    public boolean isReloadable() {
125        return isLoaded() && !isCustomized();
126    }
127
128    public boolean isSaveable() {
129        return isWritable() && (isLoaded() || isCustom()) && !isCustomized();
130    }
131
132    public boolean isExportable() {
133        return (isCustom() || isLoaded()) && !isCustomized();
134    }
135
136    public boolean isRepairable() {
137        return (isCustom() || isLoaded()) && !isCustomized();
138    }
139
140    public boolean isLoadingFailed() {
141        return loadingFailed;
142    }
143
144    public void setLoadingFailed(boolean loadingFailed) {
145        this.loadingFailed = loadingFailed;
146    }
147
148    public String getSrc() {
149        return src;
150    }
151
152    public void setSrc(String src) {
153        this.src = src;
154    }
155
156    public boolean isCustomized() {
157        return customized;
158    }
159
160    public void setCustomized(boolean customized) {
161        this.customized = customized;
162    }
163
164    public Date getLastLoaded() {
165        return lastLoaded;
166    }
167
168    public void setLastLoaded(Date lastLoaded) {
169        this.lastLoaded = lastLoaded;
170    }
171
172    public Date getLastSaved() {
173        return lastSaved;
174    }
175
176    public void setLastSaved(Date lastSaved) {
177        this.lastSaved = lastSaved;
178    }
179
180    public Date getLastModified() {
181        if (lastSaved != null && lastSaved.after(lastLoaded)) {
182            return lastSaved;
183        }
184        return lastLoaded;
185    }
186
187    public String getName() {
188        return name;
189    }
190
191    public void setName(String name) {
192        this.name = name;
193    }
194
195    public List<String> getTemplateEngines() {
196        return templateEngines;
197    }
198
199    public void setTemplateEngines(List<String> templateEngines) {
200        this.templateEngines = templateEngines;
201    }
202
203    public boolean isCompatibleWith(final String templateEngine) {
204        if (templateEngines == null || templateEngines.isEmpty() || templateEngines.contains(templateEngine)) {
205            return true;
206        }
207        return false;
208    }
209
210    public String getResourceBankName() {
211        return resourceBankName;
212    }
213
214    public void setResourceBankName(String resourceBankName) {
215        this.resourceBankName = resourceBankName;
216    }
217
218    public boolean isCustomizable() {
219        return (isXmlConfigured() && !isCustomized());
220    }
221
222    public boolean isCustomization() {
223        return customization;
224    }
225
226    public void setCustomization(boolean customization) {
227        this.customization = customization;
228    }
229
230}