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