001/*
002 * (C) Copyright 2012-2016 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.forms.layout.service;
020
021import java.io.Serializable;
022
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XObject;
025
026/**
027 * Descriptor for properties that should not be referenced using value expressions, see
028 * {@link WebLayoutManager#referencePropertyAsExpression(String, Serializable, String, String, String, String)}
029 *
030 * @since 5.6
031 */
032@XObject("disabledPropertyRef")
033public class DisabledPropertyRefDescriptor implements Serializable {
034
035    private static final long serialVersionUID = 1L;
036
037    @XNode("@name")
038    protected String name;
039
040    @XNode("@widgetType")
041    protected String widgetType;
042
043    /**
044     * @since 5.7.3
045     */
046    @XNode("@widgetTypeCategory")
047    protected String widgetTypeCategory;
048
049    @XNode("@widgetMode")
050    protected String widgetMode;
051
052    @XNode("@template")
053    protected String template;
054
055    @XNode("@enabled")
056    protected Boolean enabled = Boolean.TRUE;
057
058    public String getId() {
059        return name + "/" + widgetType + "/" + widgetTypeCategory + "/" + widgetMode + "/" + template;
060    }
061
062    public String getName() {
063        return name;
064    }
065
066    public void setName(String name) {
067        this.name = name;
068    }
069
070    public String getWidgetType() {
071        return widgetType;
072    }
073
074    public void setWidgetType(String widgetType) {
075        this.widgetType = widgetType;
076    }
077
078    public String getWidgetTypeCategory() {
079        return widgetTypeCategory;
080    }
081
082    public void setWidgetTypeCategory(String widgetTypeCategory) {
083        this.widgetTypeCategory = widgetTypeCategory;
084    }
085
086    public String getWidgetMode() {
087        return widgetMode;
088    }
089
090    public void setWidgetMode(String widgetMode) {
091        this.widgetMode = widgetMode;
092    }
093
094    public String getTemplate() {
095        return template;
096    }
097
098    public void setTemplate(String template) {
099        this.template = template;
100    }
101
102    public Boolean getEnabled() {
103        return enabled;
104    }
105
106    public void setEnabled(Boolean enabled) {
107        this.enabled = enabled;
108    }
109
110    public boolean matches(String name, String widgetType, String widgetTypeCategory, String widgetMode,
111            String template) {
112        if (name != null && name.equals(this.name)) {
113            if (matches(this.widgetType, widgetType)) {
114                if (matches(this.widgetTypeCategory, widgetTypeCategory)) {
115                    if (matches(this.widgetMode, widgetMode)) {
116                        if (matches(this.template, template)) {
117                            return true;
118                        }
119                    }
120                }
121            }
122        }
123        return false;
124    }
125
126    protected boolean matches(String value1, String value2) {
127        if ("*".equals(value1)) {
128            return true;
129        }
130        if ((value1 == null && value2 == null) || (value1 == null) || (value1 != null && value1.equals(value2))
131                || (value2 != null && value2.equals(value1))) {
132            return true;
133        }
134        return false;
135    }
136
137    @Override
138    public String toString() {
139        final StringBuilder buf = new StringBuilder();
140
141        buf.append(DisabledPropertyRefDescriptor.class.getName());
142        buf.append(" {");
143        buf.append("name=");
144        buf.append(name);
145        buf.append(", widgetType=");
146        buf.append(widgetType);
147        buf.append(", widgetTypeCategory=");
148        buf.append(widgetTypeCategory);
149        buf.append(", widgetMode=");
150        buf.append(widgetMode);
151        buf.append(", template=");
152        buf.append(template);
153        buf.append(", enabled=");
154        buf.append(enabled);
155        buf.append("}");
156
157        return buf.toString();
158    }
159
160    @Override
161    public DisabledPropertyRefDescriptor clone() {
162        DisabledPropertyRefDescriptor clone = new DisabledPropertyRefDescriptor();
163        clone.setEnabled(enabled);
164        clone.setName(name);
165        clone.setTemplate(template);
166        clone.setWidgetMode(widgetMode);
167        clone.setWidgetType(widgetType);
168        clone.setWidgetTypeCategory(widgetTypeCategory);
169        return clone;
170    }
171
172}