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