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