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