001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 */
011package org.nuxeo.ecm.core.opencmis.impl.server;
012
013import java.io.Serializable;
014import java.math.BigInteger;
015import java.util.Collections;
016import java.util.List;
017
018import org.apache.chemistry.opencmis.commons.data.CmisExtensionElement;
019import org.apache.chemistry.opencmis.commons.data.PropertyBoolean;
020import org.apache.chemistry.opencmis.commons.data.PropertyData;
021import org.apache.chemistry.opencmis.commons.data.PropertyId;
022import org.apache.chemistry.opencmis.commons.data.PropertyInteger;
023import org.apache.chemistry.opencmis.commons.data.PropertyString;
024import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
025import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
026import org.nuxeo.ecm.core.api.DocumentModel;
027
028/**
029 * Base abstract class for a live property of an object.
030 * <p>
031 * Concrete classes must also implement one of {@link PropertyId}, {@link PropertyString}, ...
032 *
033 * @see NuxeoPropertyData
034 */
035public abstract class NuxeoPropertyDataBase<T> implements PropertyData<T> {
036
037    protected final PropertyDefinition<T> propertyDefinition;
038
039    protected final DocumentModel doc;
040
041    public NuxeoPropertyDataBase(PropertyDefinition<T> propertyDefinition, DocumentModel doc) {
042        this.propertyDefinition = propertyDefinition;
043        this.doc = doc;
044    }
045
046    public PropertyDefinition<T> getPropertyDefinition() {
047        return propertyDefinition;
048    }
049
050    @Override
051    public String getId() {
052        return propertyDefinition.getId();
053    }
054
055    @Override
056    public String getLocalName() {
057        return propertyDefinition.getLocalName();
058    }
059
060    @Override
061    public String getDisplayName() {
062        return propertyDefinition.getDisplayName();
063    }
064
065    @Override
066    public String getQueryName() {
067        return propertyDefinition.getQueryName();
068    }
069
070    @SuppressWarnings("unchecked")
071    public <U> U getValue() {
072        return (U) getFirstValue();
073    }
074
075    @Override
076    public abstract T getFirstValue();
077
078    @Override
079    public List<T> getValues() {
080        T v = getFirstValue();
081        return v == null ? Collections.<T> emptyList() : Collections.singletonList(v);
082    }
083
084    public void setValue(Object value) {
085        Serializable old = null;
086        if (value == null && old == null) {
087            return;
088        }
089        if (value != null && value.equals(old)) {
090            return;
091        }
092        throw new CmisConstraintException("Read-only property: " + propertyDefinition.getId());
093    }
094
095    @Override
096    public List<CmisExtensionElement> getExtensions() {
097        return null;
098    }
099
100    @Override
101    public void setExtensions(List<CmisExtensionElement> extensions) {
102        throw new UnsupportedOperationException();
103    }
104
105    /**
106     * A fixed property (whose value cannot be changed).
107     */
108    public static abstract class NuxeoPropertyDataFixed<T> extends NuxeoPropertyDataBase<T> {
109
110        protected final T value;
111
112        protected NuxeoPropertyDataFixed(PropertyDefinition<T> propertyDefinition, T value) {
113            super(propertyDefinition, null);
114            this.value = value;
115        }
116
117        @Override
118        public T getFirstValue() {
119            return value;
120        }
121    }
122
123    /**
124     * A fixed multi-valued property (whose value cannot be changed).
125     */
126    public static abstract class NuxeoPropertyMultiDataFixed<T> extends NuxeoPropertyDataBase<T> {
127
128        protected final List<T> value;
129
130        protected NuxeoPropertyMultiDataFixed(PropertyDefinition<T> propertyDefinition, List<T> value) {
131            super(propertyDefinition, null);
132            this.value = value;
133        }
134
135        @Override
136        @SuppressWarnings("unchecked")
137        public <U> U getValue() {
138            return (U) getValues();
139        }
140
141        @Override
142        public T getFirstValue() {
143            return value.size() == 0 ? null : value.get(0);
144        }
145
146        @Override
147        public List<T> getValues() {
148            return value;
149        }
150    }
151
152    /**
153     * A fixed ID property.
154     */
155    public static class NuxeoPropertyIdDataFixed extends NuxeoPropertyDataFixed<String> implements PropertyId {
156
157        protected NuxeoPropertyIdDataFixed(PropertyDefinition<String> propertyDefinition, String value) {
158            super(propertyDefinition, value);
159        }
160    }
161
162    /**
163     * A fixed multi-ID property.
164     */
165    public static class NuxeoPropertyIdMultiDataFixed extends NuxeoPropertyMultiDataFixed<String> implements PropertyId {
166
167        protected NuxeoPropertyIdMultiDataFixed(PropertyDefinition<String> propertyDefinition, List<String> value) {
168            super(propertyDefinition, value);
169        }
170    }
171
172    /**
173     * A fixed String property.
174     */
175    public static class NuxeoPropertyStringDataFixed extends NuxeoPropertyDataFixed<String> implements PropertyString {
176
177        protected NuxeoPropertyStringDataFixed(PropertyDefinition<String> propertyDefinition, String value) {
178            super(propertyDefinition, value);
179        }
180    }
181
182    /**
183     * A fixed Boolean property.
184     */
185    public static class NuxeoPropertyBooleanDataFixed extends NuxeoPropertyDataFixed<Boolean> implements
186            PropertyBoolean {
187
188        protected NuxeoPropertyBooleanDataFixed(PropertyDefinition<Boolean> propertyDefinition, Boolean value) {
189            super(propertyDefinition, value);
190        }
191    }
192
193    /**
194     * A fixed Integer property.
195     */
196    public static class NuxeoPropertyIntegerDataFixed extends NuxeoPropertyDataFixed<BigInteger> implements
197            PropertyInteger {
198
199        protected NuxeoPropertyIntegerDataFixed(PropertyDefinition<BigInteger> propertyDefinition, Long value) {
200            super(propertyDefinition, value == null ? null : BigInteger.valueOf(value.longValue()));
201        }
202    }
203
204}