001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.metamodel.specloader.specimpl;
021
022import org.apache.isis.core.commons.exceptions.NotYetImplementedException;
023import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
024import org.apache.isis.core.metamodel.facetapi.FeatureType;
025import org.apache.isis.core.metamodel.facets.FacetedMethod;
026import org.apache.isis.core.metamodel.facets.mandatory.MandatoryFacet;
027import org.apache.isis.core.metamodel.facets.notpersisted.NotPersistedFacet;
028import org.apache.isis.core.metamodel.facets.properties.choices.PropertyChoicesFacet;
029import org.apache.isis.core.metamodel.spec.ObjectSpecification;
030import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
031import org.apache.isis.core.metamodel.spec.feature.ObjectMemberContext;
032
033// TODO need to pull up the common methods. like getName(), from subclasses
034public abstract class ObjectAssociationAbstract extends ObjectMemberAbstract implements ObjectAssociation {
035
036    private final ObjectSpecification specification;
037
038    public ObjectAssociationAbstract(final FacetedMethod facetedMethod, final FeatureType featureType, final ObjectSpecification specification, final ObjectMemberContext parameterObject) {
039        super(facetedMethod, featureType, parameterObject);
040        if (specification == null) {
041            throw new IllegalArgumentException("field type for '" + getId() + "' must exist");
042        }
043        this.specification = specification;
044    }
045
046    @Override
047    public abstract ObjectAdapter get(final ObjectAdapter fromObject);
048
049    /**
050     * Return the specification of the object (or objects) that this field
051     * holds. For a value are one-to-one reference this will be type that the
052     * accessor returns. For a collection it will be the type of element, not
053     * the type of collection.
054     */
055    @Override
056    public ObjectSpecification getSpecification() {
057        return specification;
058    }
059
060    @Override
061    public boolean isNotPersisted() {
062        return containsFacet(NotPersistedFacet.class);
063    }
064
065    @Override
066    public boolean hasChoices() {
067        return containsFacet(PropertyChoicesFacet.class);
068    }
069
070    @Override
071    public boolean isMandatory() {
072        final MandatoryFacet mandatoryFacet = getFacet(MandatoryFacet.class);
073        return mandatoryFacet != null && !mandatoryFacet.isInvertedSemantics();
074    }
075
076    @Override
077    public abstract boolean isEmpty(final ObjectAdapter adapter);
078
079    @Override
080    public boolean isOneToOneAssociation() {
081        return !isOneToManyAssociation();
082    }
083
084    @Override
085    public String getBusinessKeyName() {
086        throw new NotYetImplementedException();
087    }
088
089}