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.progmodel.facets.properties.defaults.fromtype;
021
022import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager;
023import org.apache.isis.core.metamodel.adapter.mgr.AdapterManagerAware;
024import org.apache.isis.core.metamodel.facetapi.FacetUtil;
025import org.apache.isis.core.metamodel.facetapi.FeatureType;
026import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
027import org.apache.isis.core.metamodel.facets.properties.defaults.PropertyDefaultFacet;
028import org.apache.isis.core.metamodel.spec.ObjectSpecification;
029import org.apache.isis.core.progmodel.facets.object.defaults.DefaultedFacet;
030
031public class PropertyDefaultDerivedFromTypeFacetFactory extends FacetFactoryAbstract implements AdapterManagerAware {
032
033    private AdapterManager adapterManager;
034
035    public PropertyDefaultDerivedFromTypeFacetFactory() {
036        super(FeatureType.PROPERTIES_ONLY);
037    }
038
039    /**
040     * If there is a {@link DefaultedFacet} on the properties return type, then
041     * installs a {@link PropertyDefaultFacet} for the property with the same
042     * default.
043     */
044    @Override
045    public void process(final ProcessMethodContext processMethodContext) {
046        // don't overwrite any defaults that might already picked up
047        final PropertyDefaultFacet existingDefaultFacet = processMethodContext.getFacetHolder().getFacet(PropertyDefaultFacet.class);
048        if (existingDefaultFacet != null && !existingDefaultFacet.isNoop()) {
049            return;
050        }
051
052        // try to infer defaults from the underlying return type
053        final Class<?> returnType = processMethodContext.getMethod().getReturnType();
054        final DefaultedFacet returnTypeDefaultedFacet = getDefaultedFacet(returnType);
055        if (returnTypeDefaultedFacet != null) {
056            final PropertyDefaultFacetDerivedFromDefaultedFacet propertyFacet = new PropertyDefaultFacetDerivedFromDefaultedFacet(returnTypeDefaultedFacet, processMethodContext.getFacetHolder(), getAdapterManager());
057            FacetUtil.addFacet(propertyFacet);
058        }
059    }
060
061    private DefaultedFacet getDefaultedFacet(final Class<?> paramType) {
062        final ObjectSpecification paramTypeSpec = getSpecificationLoader().loadSpecification(paramType);
063        return paramTypeSpec.getFacet(DefaultedFacet.class);
064    }
065
066    // ////////////////////////////////////////////////////////////////////
067    // Injected
068    // ////////////////////////////////////////////////////////////////////
069
070    public AdapterManager getAdapterManager() {
071        return adapterManager;
072    }
073
074    @Override
075    public void setAdapterManager(final AdapterManager adapterMap) {
076        this.adapterManager = adapterMap;
077    }
078
079}