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