001/*
002 * ModeShape (http://www.modeshape.org)
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 */
016package org.modeshape.jdbc.rest;
017
018import javax.jcr.version.OnParentVersionAction;
019import org.codehaus.jettison.json.JSONObject;
020import org.modeshape.common.annotation.Immutable;
021
022/**
023 * An {@link javax.jcr.nodetype.ItemDefinition} implementation for the ModeShape client.
024 */
025@Immutable
026public abstract class ItemDefinition implements javax.jcr.nodetype.ItemDefinition {
027
028    private final String declaringNodeTypeName;
029    private final boolean isAutoCreated;
030    private final boolean isMandatory;
031    private final boolean isProtected;
032    private final int onParentVersion;
033    private final NodeTypes nodeTypes;
034
035    protected ItemDefinition( String declaringNodeTypeName,
036                              JSONObject json,
037                              NodeTypes nodeTypes ) {
038        this.declaringNodeTypeName = declaringNodeTypeName;
039        this.nodeTypes = nodeTypes;
040        this.isAutoCreated = JSONHelper.valueFrom(json, "jcr:autoCreated", false);
041        this.isMandatory = JSONHelper.valueFrom(json, "jcr:mandatory", false);
042        this.isProtected = JSONHelper.valueFrom(json, "jcr:protected", false);
043        this.onParentVersion = OnParentVersionAction.valueFromName(JSONHelper.valueFrom(json, "jcr:onParentVersion"));
044    }
045
046    protected NodeTypes nodeTypes() {
047        return nodeTypes;
048    }
049
050    @Override
051    public NodeType getDeclaringNodeType() {
052        return nodeTypes.getNodeType(declaringNodeTypeName);
053    }
054
055    @Override
056    public int getOnParentVersion() {
057        return onParentVersion;
058    }
059
060    @Override
061    public boolean isAutoCreated() {
062        return isAutoCreated;
063    }
064
065    @Override
066    public boolean isMandatory() {
067        return isMandatory;
068    }
069
070    @Override
071    public boolean isProtected() {
072        return isProtected;
073    }
074
075}