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