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 *     qlamerand
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.annotations.gwt.client.configuration;
023
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.platform.annotations.gwt.client.annotea.RDFConstant;
029import org.nuxeo.ecm.platform.annotations.gwt.client.model.Annotation;
030
031import com.google.gwt.user.client.rpc.IsSerializable;
032
033public class AnnotationFilter implements IsSerializable {
034    private String name;
035
036    private String icon;
037
038    private String type;
039
040    private String author;
041
042    private Map<String, String> fields;
043
044    private List<String> parameters = new ArrayList<String>();
045
046    public AnnotationFilter() {
047    }
048
049    public AnnotationFilter(String name, String icon, String type, String author, Map<String, String> fields) {
050        this.name = name;
051        this.icon = icon;
052
053        if ("".equals(type)) {
054            parameters.add(RDFConstant.R_TYPE);
055        } else {
056            this.type = type;
057        }
058
059        if ("".equals(author)) {
060            parameters.add(RDFConstant.D_CREATOR);
061        } else {
062            this.author = author;
063        }
064
065        this.fields = fields;
066        if (fields != null) {
067            List<String> fieldNames = new ArrayList<String>(fields.keySet());
068            for (String fieldName : fieldNames) {
069                if ("".equals(fields.get(fieldName))) {
070                    parameters.add(fieldName);
071                    fields.remove(fieldName);
072                }
073            }
074        }
075    }
076
077    public boolean accept(Annotation annotation) {
078        boolean accept = true;
079
080        if (type != null) {
081            accept &= type.equals(annotation.getShortType());
082        }
083
084        if (author != null) {
085            accept &= author.equals(annotation.getAuthor());
086        }
087
088        if (fields != null) {
089            for (String name : fields.keySet()) {
090                accept &= fields.get(name).equals(annotation.getFields().get(name));
091            }
092        }
093
094        return accept;
095    }
096
097    public String getName() {
098        return name;
099    }
100
101    public String getIcon() {
102        return icon;
103    }
104
105    public String getType() {
106        return type;
107    }
108
109    public void setType(String type) {
110        this.type = type;
111    }
112
113    public String getAuthor() {
114        return author;
115    }
116
117    public void setAuthor(String author) {
118        this.author = author;
119    }
120
121    public Map<String, String> getFields() {
122        return fields;
123    }
124
125    public String getField(String name) {
126        return fields.get(name);
127    }
128
129    public void setFields(Map<String, String> fields) {
130        this.fields = fields;
131    }
132
133    public void setField(String name, String value) {
134        fields.put(name, value);
135    }
136
137    public void removeField(String name) {
138        fields.remove(name);
139    }
140
141    public List<String> getParameters() {
142        return parameters;
143    }
144
145    public boolean hasParameters() {
146        return !parameters.isEmpty();
147    }
148
149}