001/*
002 * (C) Copyright 2012 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 *     ataillefer
018 */
019package org.nuxeo.ecm.diff.model.impl;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.collections.CollectionUtils;
027import org.apache.commons.collections.MapUtils;
028import org.nuxeo.ecm.diff.model.DiffBlockDefinition;
029import org.nuxeo.ecm.diff.model.DiffFieldDefinition;
030import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
031import org.nuxeo.ecm.platform.forms.layout.api.impl.WidgetDefinitionImpl;
032
033/**
034 * Default implementation of a {@link DiffBlockDefinition}.
035 *
036 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
037 * @since 5.6
038 */
039public class DiffBlockDefinitionImpl implements DiffBlockDefinition {
040
041    private static final long serialVersionUID = 511776842683091931L;
042
043    protected String name;
044
045    protected Map<String, String> templates;
046
047    protected List<DiffFieldDefinition> fields;
048
049    protected Map<String, Map<String, Serializable>> properties;
050
051    public DiffBlockDefinitionImpl(String name, Map<String, String> templates, List<DiffFieldDefinition> fields,
052            Map<String, Map<String, Serializable>> properties) {
053        this.name = name;
054        this.templates = templates;
055        if (fields == null) {
056            this.fields = new ArrayList<DiffFieldDefinition>();
057        } else {
058            this.fields = fields;
059        }
060        this.properties = properties;
061    }
062
063    public String getName() {
064        return name;
065    }
066
067    public String getTemplate(String mode) {
068        if (templates != null) {
069            String template = templates.get(mode);
070            if (template == null) {
071                template = templates.get(BuiltinModes.ANY);
072            }
073            return template;
074        }
075        return null;
076    }
077
078    public Map<String, String> getTemplates() {
079        return templates;
080    }
081
082    public List<DiffFieldDefinition> getFields() {
083        return fields;
084    }
085
086    public Map<String, Serializable> getProperties(String layoutMode) {
087        return WidgetDefinitionImpl.getProperties(properties, layoutMode);
088    }
089
090    public Map<String, Map<String, Serializable>> getProperties() {
091        return properties;
092    }
093
094    @Override
095    public boolean equals(Object other) {
096
097        if (this == other) {
098            return true;
099        }
100        if (other == null || !(other instanceof DiffBlockDefinition)) {
101            return false;
102        }
103
104        String otherName = ((DiffBlockDefinition) other).getName();
105        if (name == null && otherName == null) {
106            return true;
107        }
108        if (name == null && otherName != null || name != null && otherName == null || !name.equals(otherName)) {
109            return false;
110        }
111
112        Map<String, String> otherTemplates = ((DiffBlockDefinition) other).getTemplates();
113        List<DiffFieldDefinition> otherFields = ((DiffBlockDefinition) other).getFields();
114        Map<String, Map<String, Serializable>> otherProperties = ((DiffBlockDefinition) other).getProperties();
115        if (MapUtils.isEmpty(templates) && MapUtils.isEmpty(otherTemplates) && CollectionUtils.isEmpty(fields)
116                && CollectionUtils.isEmpty(otherFields) && MapUtils.isEmpty(properties)
117                && MapUtils.isEmpty(otherProperties)) {
118            return true;
119        }
120        if (MapUtils.isEmpty(templates) && !MapUtils.isEmpty(otherTemplates) || !MapUtils.isEmpty(templates)
121                && MapUtils.isEmpty(otherTemplates) || (templates != null && !templates.equals(otherTemplates))
122                || CollectionUtils.isEmpty(fields) && !CollectionUtils.isEmpty(otherFields)
123                || !CollectionUtils.isEmpty(fields) && CollectionUtils.isEmpty(otherFields)
124                || (fields != null && !fields.equals(otherFields)) || MapUtils.isEmpty(properties)
125                && !MapUtils.isEmpty(otherProperties) || !MapUtils.isEmpty(properties)
126                && MapUtils.isEmpty(otherProperties) || (properties != null && !properties.equals(otherProperties))) {
127            return false;
128        }
129
130        return true;
131    }
132
133    @Override
134    public String toString() {
135        return name + fields + templates + properties;
136    }
137}