001/*
002 * (C) Copyright 2006-2008 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 *     troger
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.annotations.gwt.client.configuration;
023
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.HashSet;
028import java.util.List;
029import java.util.Map;
030import java.util.Set;
031
032import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.filter.AnnotationDefinitionFilter;
033
034import com.google.gwt.user.client.rpc.IsSerializable;
035
036/**
037 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
038 */
039public class WebConfiguration implements IsSerializable {
040
041    public static final WebConfiguration DEFAULT_WEB_CONFIGURATION;
042
043    static {
044        DEFAULT_WEB_CONFIGURATION = new WebConfiguration();
045        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
046                "http://www.w3.org/2000/10/annotationType#Example", "Example", "icons/annotate.png", "local"));
047        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
048                "http://www.w3.org/2000/10/annotationType#Comment", "Comment", "icons/annotate.png", "local"));
049        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
050                "http://www.w3.org/2000/10/annotationType#SeeAlso", "SeeAlso", "icons/annotate.png", "local"));
051        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
052                "http://www.w3.org/2000/10/annotationType#Question", "Question", "icons/annotate.png", "local"));
053        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
054                "http://www.w3.org/2000/10/annotationType#Explanation", "Explanation", "icons/annotate.png", "local"));
055        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
056                "http://www.w3.org/2000/10/annotationType#Change", "Change", "icons/annotate.png", "local"));
057        DEFAULT_WEB_CONFIGURATION.addAnnotationDefinition(new AnnotationDefinition(
058                "http://www.w3.org/2000/10/annotationType#Advice", "Advice", "icons/annotate.png", "local"));
059    }
060
061    private Map<String, AnnotationDefinition> annotationDefinitions = new HashMap<String, AnnotationDefinition>();
062
063    private Map<String, String> userInfo = new HashMap<String, String>();
064
065    private List<AnnotationFilter> filters = new ArrayList<AnnotationFilter>();
066
067    private Set<String> displayedFields = new HashSet<String>();
068
069    private Map<String, String> fieldLabels;
070
071    private boolean canAnnotate = true;
072
073    public void addAnnotationDefinition(AnnotationDefinition annotationDefinition) {
074        annotationDefinitions.put(annotationDefinition.getName(), annotationDefinition);
075    }
076
077    public void removeAnnotationDefinition(AnnotationDefinition annotationDefinition) {
078        annotationDefinitions.remove(annotationDefinition);
079    }
080
081    public List<AnnotationDefinition> getAnnotationDefinitions() {
082        List<AnnotationDefinition> list = new ArrayList<AnnotationDefinition>(annotationDefinitions.values());
083        return Collections.unmodifiableList(list);
084    }
085
086    public List<AnnotationDefinition> getAnnotationDefinitions(AnnotationDefinitionFilter filter) {
087        List<AnnotationDefinition> types = new ArrayList<AnnotationDefinition>();
088        for (AnnotationDefinition type : annotationDefinitions.values()) {
089            if (filter.accept(type)) {
090                types.add(type);
091            }
092        }
093        return types;
094    }
095
096    public Map<String, AnnotationDefinition> getAnnotationDefinitionsMap() {
097        return annotationDefinitions;
098    }
099
100    public AnnotationDefinition getAnnotationDefinition(String name) {
101        AnnotationDefinition def = annotationDefinitions.get(name);
102        return def != null ? def : getFirsTannotationDefinition();
103    }
104
105    private AnnotationDefinition getFirsTannotationDefinition() {
106        List<AnnotationDefinition> l = new ArrayList<AnnotationDefinition>(annotationDefinitions.values());
107        return l.isEmpty() ? null : l.get(0);
108    }
109
110    public void setUserInfo(Map<String, String> userInfo) {
111        this.userInfo = userInfo;
112    }
113
114    public Map<String, String> getUserInfo() {
115        return userInfo;
116    }
117
118    private String getValue(String v) {
119        if (v != null && v.startsWith("${") && v.endsWith("}")) {
120            v = userInfo.get(v.substring(2, v.length() - 1));
121        }
122        return v;
123    }
124
125    public void addFilter(int order, String name, String icon, String type, String author, Map<String, String> fields) {
126        Map<String, String> newFields = new HashMap<String, String>();
127        for (String fieldName : fields.keySet()) {
128            String value = getValue(fields.get(fieldName));
129            if (value != null) {
130                newFields.put(fieldName, value);
131            }
132        }
133        if (order < filters.size()) {
134            filters.add(order, new AnnotationFilter(name, icon, getValue(type), getValue(author), newFields));
135        } else {
136            filters.add(new AnnotationFilter(name, icon, getValue(type), getValue(author), newFields));
137        }
138    }
139
140    public List<AnnotationFilter> getFilters() {
141        return filters;
142    }
143
144    public void setFilters(List<AnnotationFilter> filters) {
145        this.filters = filters;
146    }
147
148    public Set<String> getDisplayedFields() {
149        return displayedFields;
150    }
151
152    public void setDisplayedFields(Set<String> fields) {
153        this.displayedFields = fields;
154    }
155
156    public void setFieldLabels(Map<String, String> fieldLabels) {
157        this.fieldLabels = fieldLabels;
158    }
159
160    public Map<String, String> getFieldLabels() {
161        return fieldLabels;
162    }
163
164    public void setCanAnnotate(boolean canAnnotate) {
165        this.canAnnotate = canAnnotate;
166    }
167
168    public boolean canAnnotate() {
169        return canAnnotate;
170    }
171
172}