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        if (value == null) {
093            return;
094        }
095        throw new CmisConstraintException("Read-only property: " + propertyDefinition.getId());
096    }
097
098    @Override
099    public List<CmisExtensionElement> getExtensions() {
100        return null;
101    }
102
103    @Override
104    public void setExtensions(List<CmisExtensionElement> extensions) {
105        throw new UnsupportedOperationException();
106    }
107
108    /**
109     * A fixed property (whose value cannot be changed).
110     */
111    public static abstract class NuxeoPropertyDataFixed<T> extends NuxeoPropertyDataBase<T> {
112
113        protected final T value;
114
115        protected NuxeoPropertyDataFixed(PropertyDefinition<T> propertyDefinition, T value) {
116            super(propertyDefinition, null);
117            this.value = value;
118        }
119
120        @Override
121        public T getFirstValue() {
122            return value;
123        }
124    }
125
126    /**
127     * A fixed multi-valued property (whose value cannot be changed).
128     */
129    public static abstract class NuxeoPropertyMultiDataFixed<T> extends NuxeoPropertyDataBase<T> {
130
131        protected final List<T> value;
132
133        protected NuxeoPropertyMultiDataFixed(PropertyDefinition<T> propertyDefinition, List<T> value) {
134            super(propertyDefinition, null);
135            this.value = value;
136        }
137
138        @Override
139        @SuppressWarnings("unchecked")
140        public <U> U getValue() {
141            return (U) getValues();
142        }
143
144        @Override
145        public T getFirstValue() {
146            return value.size() == 0 ? null : value.get(0);
147        }
148
149        @Override
150        public List<T> getValues() {
151            return value;
152        }
153    }
154
155    /**
156     * A fixed ID property.
157     */
158    public static class NuxeoPropertyIdDataFixed extends NuxeoPropertyDataFixed<String> implements PropertyId {
159
160        protected NuxeoPropertyIdDataFixed(PropertyDefinition<String> propertyDefinition, String value) {
161            super(propertyDefinition, value);
162        }
163    }
164
165    /**
166     * A fixed multi-ID property.
167     */
168    public static class NuxeoPropertyIdMultiDataFixed extends NuxeoPropertyMultiDataFixed<String> implements PropertyId {
169
170        protected NuxeoPropertyIdMultiDataFixed(PropertyDefinition<String> propertyDefinition, List<String> value) {
171            super(propertyDefinition, value);
172        }
173    }
174
175    /**
176     * A fixed String property.
177     */
178    public static class NuxeoPropertyStringDataFixed extends NuxeoPropertyDataFixed<String> implements PropertyString {
179
180        protected NuxeoPropertyStringDataFixed(PropertyDefinition<String> propertyDefinition, String value) {
181            super(propertyDefinition, value);
182        }
183    }
184
185    /**
186     * A fixed Boolean property.
187     */
188    public static class NuxeoPropertyBooleanDataFixed extends NuxeoPropertyDataFixed<Boolean> implements
189            PropertyBoolean {
190
191        protected NuxeoPropertyBooleanDataFixed(PropertyDefinition<Boolean> propertyDefinition, Boolean value) {
192            super(propertyDefinition, value);
193        }
194    }
195
196    /**
197     * A fixed Integer property.
198     */
199    public static class NuxeoPropertyIntegerDataFixed extends NuxeoPropertyDataFixed<BigInteger> implements
200            PropertyInteger {
201
202        protected NuxeoPropertyIntegerDataFixed(PropertyDefinition<BigInteger> propertyDefinition, Long value) {
203            super(propertyDefinition, value == null ? null : BigInteger.valueOf(value.longValue()));
204        }
205    }
206
207}