001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     troger
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.client.configuration;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.HashSet;
026import java.util.List;
027import java.util.Map;
028import java.util.Set;
029
030import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.filter.AnnotationDefinitionFilter;
031
032import com.google.gwt.user.client.rpc.IsSerializable;
033
034/**
035 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
036 */
037public class WebConfiguration implements IsSerializable {
038
039    public static final WebConfiguration DEFAULT_WEB_CONFIGURATION;
040
041    static {
042        DEFAULT_WEB_CONFIGURATION = new WebConfiguration();
043        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
044                "http://www.w3.org/2000/10/annotationType#Example", "Example", "icons/annotate.png", "local"));
045        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
046                "http://www.w3.org/2000/10/annotationType#Comment", "Comment", "icons/annotate.png", "local"));
047        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
048                "http://www.w3.org/2000/10/annotationType#SeeAlso", "SeeAlso", "icons/annotate.png", "local"));
049        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
050                "http://www.w3.org/2000/10/annotationType#Question", "Question", "icons/annotate.png", "local"));
051        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
052                "http://www.w3.org/2000/10/annotationType#Explanation", "Explanation", "icons/annotate.png", "local"));
053        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
054                "http://www.w3.org/2000/10/annotationType#Change", "Change", "icons/annotate.png", "local"));
055        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
056                "http://www.w3.org/2000/10/annotationType#Advice", "Advice", "icons/annotate.png", "local"));
057    }
058
059    private Map<String, AnnotationDefinition> annotationDefinitions = new HashMap<String, AnnotationDefinition>();
060
061    private Map<String, String> userInfo = new HashMap<String, String>();
062
063    private List<AnnotationFilter> filters = new ArrayList<AnnotationFilter>();
064
065    private Set<String> displayedFields = new HashSet<String>();
066
067    private Map<String, String> fieldLabels;
068
069    private boolean canAnnotate = true;
070
071    public void addAnnotationDefinition(AnnotationDefinition annotationDefinition) {
072        annotationDefinitions.put(annotationDefinition.getName(), annotationDefinition);
073    }
074
075    public void removeAnnotationDefinition(AnnotationDefinition annotationDefinition) {
076        annotationDefinitions.remove(annotationDefinition);
077    }
078
079    public List<AnnotationDefinition> getAnnotationDefinitions() {
080        List<AnnotationDefinition> list = new ArrayList<AnnotationDefinition>(annotationDefinitions.values());
081        return Collections.unmodifiableList(list);
082    }
083
084    public List<AnnotationDefinition> getAnnotationDefinitions(AnnotationDefinitionFilter filter) {
085        List<AnnotationDefinition> types = new ArrayList<AnnotationDefinition>();
086        for (AnnotationDefinition type : annotationDefinitions.values()) {
087            if (filter.accept(type)) {
088                types.add(type);
089            }
090        }
091        return types;
092    }
093
094    public Map<String, AnnotationDefinition> getAnnotationDefinitionsMap() {
095        return annotationDefinitions;
096    }
097
098    public AnnotationDefinition getAnnotationDefinition(String name) {
099        AnnotationDefinition def = annotationDefinitions.get(name);
100        return def != null ? def : getFirsTannotationDefinition();
101    }
102
103    private AnnotationDefinition getFirsTannotationDefinition() {
104        List<AnnotationDefinition> l = new ArrayList<AnnotationDefinition>(annotationDefinitions.values());
105        return l.isEmpty() ? null : l.get(0);
106    }
107
108    public void setUserInfo(Map<String, String> userInfo) {
109        this.userInfo = userInfo;
110    }
111
112    public Map<String, String> getUserInfo() {
113        return userInfo;
114    }
115
116    private String getValue(String v) {
117        if (v != null && v.startsWith("${") && v.endsWith("}")) {
118            v = userInfo.get(v.substring(2, v.length() - 1));
119        }
120        return v;
121    }
122
123    public void addFilter(int order, String name, String icon, String type, String author, Map<String, String> fields) {
124        Map<String, String> newFields = new HashMap<String, String>();
125        for (String fieldName : fields.keySet()) {
126            String value = getValue(fields.get(fieldName));
127            if (value != null) {
128                newFields.put(fieldName, value);
129            }
130        }
131        if (order < filters.size()) {
132            filters.add(order, new AnnotationFilter(name, icon, getValue(type), getValue(author), newFields));
133        } else {
134            filters.add(new AnnotationFilter(name, icon, getValue(type), getValue(author), newFields));
135        }
136    }
137
138    public List<AnnotationFilter> getFilters() {
139        return filters;
140    }
141
142    public void setFilters(List<AnnotationFilter> filters) {
143        this.filters = filters;
144    }
145
146    public Set<String> getDisplayedFields() {
147        return displayedFields;
148    }
149
150    public void setDisplayedFields(Set<String> fields) {
151        this.displayedFields = fields;
152    }
153
154    public void setFieldLabels(Map<String, String> fieldLabels) {
155        this.fieldLabels = fieldLabels;
156    }
157
158    public Map<String, String> getFieldLabels() {
159        return fieldLabels;
160    }
161
162    public void setCanAnnotate(boolean canAnnotate) {
163        this.canAnnotate = canAnnotate;
164    }
165
166    public boolean canAnnotate() {
167        return canAnnotate;
168    }
169
170}