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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: FilterRule.java 30476 2008-02-22 09:13:23Z bstefanescu $
020 */
021
022package org.nuxeo.ecm.platform.actions;
023
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XNodeList;
026import org.nuxeo.common.xmap.annotation.XObject;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 * @author <a href="mailto:rspivak@nuxeo.com">Ruslan Spivak</a>
031 */
032@XObject("rule")
033public class FilterRule {
034
035    // These instance variables are package-private because there are no
036    // accessors (for now?).
037
038    @XNode(value = "@grant")
039    boolean grant = false; // DENY
040
041    @XNodeList(value = "permission", type = String[].class, componentType = String.class)
042    String[] permissions;
043
044    @XNodeList(value = "facet", type = String[].class, componentType = String.class)
045    String[] facets;
046
047    @XNodeList(value = "type", type = String[].class, componentType = String.class)
048    String[] types;
049
050    @XNodeList(value = "schema", type = String[].class, componentType = String.class)
051    String[] schemas;
052
053    @XNodeList(value = "group", type = String[].class, componentType = String.class)
054    String[] groups;
055
056    String[] conditions;
057
058    protected String cacheKey;
059
060    public FilterRule() {
061    }
062
063    public FilterRule(boolean grant, String[] permissions, String[] facets, String[] conditions, String[] types,
064            String[] schemas) {
065        this.grant = grant;
066        this.permissions = permissions;
067        this.facets = facets;
068        this.conditions = conditions;
069        this.types = types;
070        this.schemas = schemas;
071    }
072
073    @XNodeList(value = "condition", type = String[].class, componentType = String.class)
074    public void setConditions(String[] conditions) {
075        this.conditions = conditions;
076    }
077
078    public String getCacheKey() {
079        if (cacheKey == null) {
080            StringBuffer sb = new StringBuffer();
081            sb.append("grant:");
082            sb.append(grant);
083            if (permissions != null && permissions.length > 0) {
084                sb.append(":permissions:");
085                for (String perm : permissions) {
086                    sb.append(perm);
087                    sb.append(",");
088                }
089            }
090            if (facets != null && facets.length > 0) {
091                sb.append(":facets:");
092                for (String facet : facets) {
093                    sb.append(facet);
094                    sb.append(",");
095                }
096            }
097            if (conditions != null && conditions.length > 0) {
098                sb.append(":conditions:");
099                for (String cond : conditions) {
100                    sb.append(cond);
101                    sb.append(",");
102                }
103            }
104            if (types != null && types.length > 0) {
105                sb.append(":types:");
106                for (String typ : types) {
107                    sb.append(typ);
108                    sb.append(",");
109                }
110            }
111            if (schemas != null && schemas.length > 0) {
112                sb.append(":schemas:");
113                for (String schem : schemas) {
114                    sb.append(schem);
115                    sb.append(",");
116                }
117            }
118
119            if (groups != null && groups.length > 0) {
120                sb.append(":groups:");
121                for (String group : groups) {
122                    sb.append(group);
123                    sb.append(",");
124                }
125            }
126            cacheKey = sb.toString();
127        }
128        return cacheKey;
129    }
130
131    @Override
132    public String toString() {
133        return getCacheKey();
134    }
135
136    @Override
137    public boolean equals(Object obj) {
138        if (obj == this) {
139            return true;
140        }
141        if (!(obj instanceof FilterRule)) {
142            return false;
143        }
144        return getCacheKey().equals(((FilterRule) obj).getCacheKey());
145    }
146
147    @Override
148    public int hashCode() {
149        return getCacheKey().hashCode();
150    }
151
152    public FilterRule clone() {
153        FilterRule clone = new FilterRule();
154        clone.grant = grant;
155        if (permissions != null) {
156            clone.permissions = permissions.clone();
157        }
158        if (facets != null) {
159            clone.facets = facets.clone();
160        }
161        if (types != null) {
162            clone.types = types.clone();
163        }
164        if (schemas != null) {
165            clone.schemas = schemas.clone();
166        }
167        if (groups != null) {
168            clone.groups = groups.clone();
169        }
170        if (conditions != null) {
171            clone.conditions = conditions.clone();
172        }
173        clone.cacheKey = cacheKey;
174        return clone;
175    }
176
177}