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 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.api.model.impl;
023
024import java.io.Serializable;
025
026import org.nuxeo.common.utils.Path;
027import org.nuxeo.ecm.core.api.PropertyException;
028import org.nuxeo.ecm.core.api.model.DocumentPart;
029import org.nuxeo.ecm.core.api.model.Property;
030import org.nuxeo.ecm.core.api.model.PropertyDiff;
031import org.nuxeo.ecm.core.api.model.PropertyVisitor;
032import org.nuxeo.ecm.core.schema.SchemaManager;
033import org.nuxeo.ecm.core.schema.types.Field;
034import org.nuxeo.ecm.core.schema.types.Schema;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040public class DocumentPartImpl extends ComplexProperty implements DocumentPart {
041
042    private static final long serialVersionUID = 1L;
043
044    protected Schema schema;
045
046    protected boolean clearComplexPropertyBeforeSet;
047
048    public DocumentPartImpl(Schema schema) {
049        super(null);
050        this.schema = schema;
051        // we pre-read this flag only once to avoid looking up and calling the SchemaManager many times
052        clearComplexPropertyBeforeSet = Framework.getService(SchemaManager.class).getClearComplexPropertyBeforeSet();
053    }
054
055    @Override
056    public void internalSetValue(Serializable value) throws PropertyException {
057    }
058
059    @Override
060    public boolean isContainer() {
061        return true;
062    }
063
064    @Override
065    public Schema getSchema() {
066        return schema;
067    }
068
069    @Override
070    public String getName() {
071        return schema.getName();
072    }
073
074    @Override
075    public Schema getType() {
076        return schema;
077    }
078
079    @Override
080    public Field getField() {
081        throw new UnsupportedOperationException("Document parts are not bound to schema fields");
082    }
083
084    @Override
085    public Path collectPath(Path path) {
086        return path;
087    }
088
089    @Override
090    public Object clone() throws CloneNotSupportedException {
091        return super.clone();
092    }
093
094    @Override
095    public void accept(PropertyVisitor visitor, Object arg) throws PropertyException {
096        visitChildren(visitor, arg);
097    }
098
099    @Override
100    public Property createProperty(Property parent, Field field) {
101        return createProperty(parent, field, 0);
102    }
103
104    @Override
105    public Property createProperty(Property parent, Field field, int flags) {
106        return PropertyFactory.createProperty(parent, field, flags);
107    }
108
109    @Override
110    public boolean getClearComplexPropertyBeforeSet() {
111        return clearComplexPropertyBeforeSet;
112    }
113
114    @Override
115    public PropertyDiff exportDiff() {
116        return null;
117    }
118
119    @Override
120    public void importDiff(PropertyDiff diff) {
121    }
122
123    public boolean isSameAs(DocumentPart dp) {
124        if (dp == null) {
125            return false;
126        }
127        if (dp instanceof ComplexProperty) {
128            return getNonPhantomChildren().equals(((ComplexProperty) dp).getNonPhantomChildren());
129        }
130        return false;
131    }
132
133    @Override
134    public String toString() {
135        return getClass().getSimpleName() + '(' + getName() + (isDirty() ? "*" : "") + ", " + children + ')';
136    }
137
138}