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.actions.defaults.method;
021
022import java.lang.reflect.Method;
023import java.util.Collections;
024import java.util.List;
025
026import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
027import org.apache.isis.core.metamodel.adapter.util.AdapterInvokeUtils;
028import org.apache.isis.core.metamodel.facetapi.Facet;
029import org.apache.isis.core.metamodel.facetapi.FacetHolder;
030import org.apache.isis.core.metamodel.facets.ImperativeFacet;
031import org.apache.isis.core.metamodel.facets.ImperativeFacet.Intent;
032import org.apache.isis.core.metamodel.facets.actions.defaults.ActionDefaultsFacetAbstract;
033import org.apache.isis.core.metamodel.facets.actions.invoke.ActionInvocationFacet;
034import org.apache.isis.core.progmodel.facets.actions.invoke.ActionInvocationFacetViaMethod;
035
036public class ActionDefaultsFacetViaMethod extends ActionDefaultsFacetAbstract implements ImperativeFacet {
037
038    private final Method defaultMethod;
039
040    @SuppressWarnings("unused")
041    private final Method actionMethod;
042
043    public ActionDefaultsFacetViaMethod(final Method defaultMethod, final FacetHolder holder) {
044        super(holder, Derivation.NOT_DERIVED);
045        this.defaultMethod = defaultMethod;
046        this.actionMethod = determineActionMethod(holder);
047    }
048
049    private static Method determineActionMethod(final FacetHolder holder) {
050        Method method2;
051        final Facet actionInvocationFacet = holder.getFacet(ActionInvocationFacet.class);
052        if (actionInvocationFacet instanceof ActionInvocationFacetViaMethod) {
053            final ActionInvocationFacetViaMethod facetViaMethod = (ActionInvocationFacetViaMethod) actionInvocationFacet;
054            method2 = facetViaMethod.getMethods().get(0);
055        } else {
056            method2 = null;
057        }
058        return method2;
059    }
060
061    /**
062     * Returns a singleton list of the {@link Method} provided in the
063     * constructor.
064     */
065    @Override
066    public List<Method> getMethods() {
067        return Collections.singletonList(defaultMethod);
068    }
069
070    @Override
071    public Intent getIntent(final Method method) {
072        return Intent.DEFAULTS;
073    }
074
075    @Override
076    public boolean impliesResolve() {
077        return true;
078    }
079
080    @Override
081    public boolean impliesObjectChanged() {
082        return false;
083    }
084
085    @Override
086    public Object[] getDefaults(final ObjectAdapter owningAdapter) {
087        return (Object[]) AdapterInvokeUtils.invoke(defaultMethod, owningAdapter);
088    }
089
090    @Override
091    protected String toStringValues() {
092        return "method=" + defaultMethod;
093    }
094
095}