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