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