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 *     Antoine Taillefer
018 */
019package org.nuxeo.ecm.diff.model.impl;
020
021import org.nuxeo.ecm.diff.model.DifferenceType;
022import org.nuxeo.ecm.diff.model.PropertyDiff;
023import org.nuxeo.ecm.diff.model.PropertyType;
024
025/**
026 * Implementation of {@link PropertyDiff} for a content property (blob).
027 *
028 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
029 * @since 5.6
030 */
031public class ContentPropertyDiff extends PropertyDiff {
032
033    private static final long serialVersionUID = -1030336913574192065L;
034
035    protected ContentProperty leftContent;
036
037    protected ContentProperty rightContent;
038
039    /**
040     * Instantiates a new content property diff with the {@link PropertyType#CONTENT} property type.
041     *
042     * @param propertyType the property type
043     */
044    public ContentPropertyDiff() {
045        this.propertyType = PropertyType.CONTENT;
046    }
047
048    /**
049     * Instantiates a new content property diff with a difference type.
050     *
051     * @param propertyType the property type
052     */
053    public ContentPropertyDiff(DifferenceType differenceType) {
054        this();
055        this.differenceType = differenceType;
056    }
057
058    /**
059     * Instantiates a new content property diff with the {@link PropertyType#CONTENT} property type, the
060     * {@link DifferenceType#different} difference type, a left content and right content.
061     *
062     * @param leftContent the left content
063     * @param rightContent the right content
064     */
065    public ContentPropertyDiff(ContentProperty leftContent, ContentProperty rightContent) {
066
067        this(DifferenceType.different, leftContent, rightContent);
068    }
069
070    /**
071     * Instantiates a new content property diff with the {@link PropertyType#CONTENT} property type, a difference type,
072     * a left content and right content.
073     *
074     * @param differenceType the difference type
075     * @param leftContent the left content
076     * @param rightContent the right content
077     */
078    public ContentPropertyDiff(DifferenceType differenceType, ContentProperty leftContent, ContentProperty rightContent) {
079
080        this(PropertyType.CONTENT, differenceType, leftContent, rightContent);
081    }
082
083    /**
084     * Instantiates a new content property diff with a property type, difference type, left content and right content.
085     *
086     * @param propertyType the property type
087     * @param differenceType the difference type
088     * @param leftContent the left content
089     * @param rightContent the right content
090     */
091    public ContentPropertyDiff(String propertyType, DifferenceType differenceType, ContentProperty leftContent,
092            ContentProperty rightContent) {
093
094        this.propertyType = propertyType;
095        this.differenceType = differenceType;
096        this.leftContent = leftContent;
097        this.rightContent = rightContent;
098    }
099
100    @Override
101    public boolean equals(Object other) {
102
103        if (!super.equals(other)) {
104            return false;
105        }
106        if (this == other) {
107            return true;
108        }
109        if (!(other instanceof ContentPropertyDiff)) {
110            return false;
111        }
112
113        ContentProperty otherLeftContent = ((ContentPropertyDiff) other).getLeftContent();
114        ContentProperty otherRightContent = ((ContentPropertyDiff) other).getRightContent();
115
116        return (leftContent == null && otherLeftContent == null && rightContent == null && otherRightContent == null)
117                || (leftContent == null && otherLeftContent == null && rightContent != null && rightContent.equals(otherRightContent))
118                || (rightContent == null && otherRightContent == null && leftContent != null && leftContent.equals(otherLeftContent))
119                || (leftContent != null && rightContent != null && leftContent.equals(otherLeftContent) && rightContent.equals(otherRightContent));
120    }
121
122    @Override
123    public String toString() {
124
125        StringBuilder sb = new StringBuilder();
126
127        sb.append(leftContent);
128        sb.append(" --> ");
129        sb.append(rightContent);
130        sb.append(super.toString());
131
132        return sb.toString();
133    }
134
135    public ContentProperty getLeftContent() {
136        return leftContent;
137    }
138
139    public void setLeftContent(ContentProperty leftContent) {
140        this.leftContent = leftContent;
141    }
142
143    public ContentProperty getRightContent() {
144        return rightContent;
145    }
146
147    public void setRightContent(ContentProperty rightContent) {
148        this.rightContent = rightContent;
149    }
150}