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