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 *     Antoine Taillefer
016 */
017package org.nuxeo.ecm.diff.model.impl;
018
019import java.io.Serializable;
020
021import org.nuxeo.ecm.core.api.NuxeoException;
022import org.nuxeo.ecm.core.io.ExportConstants;
023
024/**
025 * Implementation of a content property (blob), defined by the {@code content} low level complex type.
026 *
027 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
028 * @since 5.6
029 */
030public class ContentProperty implements Serializable {
031
032    private static final long serialVersionUID = 4258464052505119020L;
033
034    protected String encoding;
035
036    protected String mimeType;
037
038    protected String filename;
039
040    protected String digest;
041
042    public ContentProperty() {
043    }
044
045    public ContentProperty(String encoding, String mimeType, String filename, String digest) {
046        this.encoding = encoding;
047        this.mimeType = mimeType;
048        this.filename = filename;
049        this.digest = digest;
050    }
051
052    public String getEncoding() {
053        return encoding;
054    }
055
056    public void setEncoding(String encoding) {
057        this.encoding = encoding;
058    }
059
060    public String getMimeType() {
061        return mimeType;
062    }
063
064    public void setMimeType(String mimeType) {
065        this.mimeType = mimeType;
066    }
067
068    public String getFilename() {
069        return filename;
070    }
071
072    public void setFilename(String filename) {
073        this.filename = filename;
074    }
075
076    public String getDigest() {
077        return digest;
078    }
079
080    public void setDigest(String digest) {
081        this.digest = digest;
082    }
083
084    public void setSubProperty(String subPropertyName, String subPropertyValue) {
085        if (ExportConstants.BLOB_ENCODING.equals(subPropertyName)) {
086            setEncoding(subPropertyValue);
087        } else if (ExportConstants.BLOB_MIME_TYPE.equals(subPropertyName)) {
088            setMimeType(subPropertyValue);
089        } else if (ExportConstants.BLOB_FILENAME.equals(subPropertyName)) {
090            setFilename(subPropertyValue);
091        } else if (ExportConstants.BLOB_DIGEST.equals(subPropertyName)) {
092            setDigest(subPropertyValue);
093        } else if (ExportConstants.BLOB_DATA.equals(subPropertyName)) {
094            // Nothing to do here, the data property is not interesting for the
095            // content diff
096        } else {
097            throw new NuxeoException(
098                    String.format(
099                            "Error while trying to set sub property '%s' on an object of type ContentProperty: no such sub property.",
100                            subPropertyName));
101        }
102    }
103
104    @Override
105    public boolean equals(Object other) {
106
107        if (this == other) {
108            return true;
109        }
110        if (!(other instanceof ContentProperty)) {
111            return false;
112        }
113
114        String otherEncoding = ((ContentProperty) other).getEncoding();
115        String otherMimeType = ((ContentProperty) other).getMimeType();
116        String otherFilename = ((ContentProperty) other).getFilename();
117        String otherDigest = ((ContentProperty) other).getDigest();
118
119        if (encoding == null && otherEncoding == null && mimeType == null && otherMimeType == null && filename == null
120                && otherFilename == null && digest == null && otherDigest == null) {
121            return true;
122        }
123
124        if (notEquals(encoding, otherEncoding) || notEquals(mimeType, otherMimeType)
125                || notEquals(filename, otherFilename) || notEquals(digest, otherDigest)) {
126            return false;
127        }
128
129        return true;
130    }
131
132    @Override
133    public String toString() {
134        StringBuilder sb = new StringBuilder();
135        sb.append("filename=");
136        sb.append(filename);
137        sb.append("; encoding=");
138        sb.append(encoding);
139        sb.append("; mimeType=");
140        sb.append(mimeType);
141        sb.append("; digest=");
142        sb.append(digest);
143        return sb.toString();
144    }
145
146    protected boolean notEquals(String s1, String s2) {
147        return s1 == null && s2 != null || s1 != null && s2 == null || s1 != null && !s1.equals(s2);
148    }
149}