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.method;
021
022import java.lang.reflect.Method;
023import java.util.Collections;
024import java.util.List;
025
026import org.apache.isis.core.commons.exceptions.UnknownTypeException;
027import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
028import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager;
029import org.apache.isis.core.metamodel.adapter.util.AdapterInvokeUtils;
030import org.apache.isis.core.metamodel.facetapi.FacetHolder;
031import org.apache.isis.core.metamodel.facets.ImperativeFacet;
032import org.apache.isis.core.metamodel.facets.ImperativeFacet.Intent;
033import org.apache.isis.core.metamodel.spec.ObjectSpecification;
034import org.apache.isis.core.metamodel.spec.SpecificationLoader;
035import org.apache.isis.core.progmodel.facets.properties.defaults.PropertyDefaultFacetAbstract;
036
037public class PropertyDefaultFacetViaMethod extends PropertyDefaultFacetAbstract implements ImperativeFacet {
038
039    private final Method method;
040    private final SpecificationLoader specificationLookup;
041    private final AdapterManager adapterManager;
042
043    public PropertyDefaultFacetViaMethod(final Method method, final FacetHolder holder, final SpecificationLoader specificationLookup, final AdapterManager adapterManager) {
044        super(holder);
045        this.method = method;
046        this.specificationLookup = specificationLookup;
047        this.adapterManager = adapterManager;
048    }
049
050    /**
051     * Returns a singleton list of the {@link Method} provided in the
052     * constructor.
053     */
054    @Override
055    public List<Method> getMethods() {
056        return Collections.singletonList(method);
057    }
058
059    @Override
060    public Intent getIntent(final Method method) {
061        return Intent.DEFAULTS;
062    }
063
064    @Override
065    public boolean impliesResolve() {
066        return true;
067    }
068
069    @Override
070    public boolean impliesObjectChanged() {
071        return false;
072    }
073
074    @Override
075    public ObjectAdapter getDefault(final ObjectAdapter owningAdapter) {
076        final Object result = AdapterInvokeUtils.invoke(method, owningAdapter);
077        if (result == null) {
078            return null;
079        }
080        return createAdapter(method.getReturnType(), result);
081    }
082
083    private ObjectAdapter createAdapter(final Class<?> type, final Object object) {
084        final ObjectSpecification specification = getSpecificationLookup().loadSpecification(type);
085        if (specification.isNotCollection()) {
086            return getAdapterManager().adapterFor(object);
087        } else {
088            throw new UnknownTypeException("not an object, is this a collection?");
089        }
090    }
091
092    @Override
093    protected String toStringValues() {
094        return "method=" + method;
095    }
096
097    // //////////////////////////////////////////////////////////////////
098    // Dependencies (from constructor)
099    // //////////////////////////////////////////////////////////////////
100
101    private SpecificationLoader getSpecificationLookup() {
102        return specificationLookup;
103    }
104
105    protected AdapterManager getAdapterManager() {
106        return adapterManager;
107    }
108
109}