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.param.choices.methodnum;
021
022import java.lang.reflect.Method;
023import java.util.Collections;
024import java.util.List;
025
026import org.apache.isis.core.commons.lang.ObjectExtensions;
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.CollectionUtils;
036import org.apache.isis.core.progmodel.facets.param.choices.ActionParameterChoicesFacetAbstract;
037
038public class ActionParameterChoicesFacetViaMethod extends ActionParameterChoicesFacetAbstract implements ImperativeFacet {
039
040    private final Method method;
041    private final Class<?> choicesType;
042
043    public ActionParameterChoicesFacetViaMethod(final Method method, final Class<?> choicesType, final FacetHolder holder, final SpecificationLoader specificationLookup, final AdapterManager adapterManager) {
044        super(holder, specificationLookup, adapterManager);
045        this.method = method;
046        this.choicesType = choicesType;
047    }
048
049    /**
050     * Returns a singleton list of the {@link Method} provided in the
051     * constructor.
052     */
053    @Override
054    public List<Method> getMethods() {
055        return Collections.singletonList(method);
056    }
057
058    @Override
059    public Intent getIntent(final Method method) {
060        return Intent.CHOICES_OR_AUTOCOMPLETE;
061    }
062
063    @Override
064    public boolean impliesResolve() {
065        return true;
066    }
067
068    @Override
069    public boolean impliesObjectChanged() {
070        return false;
071    }
072
073    @Override
074    public Object[] getChoices(final ObjectAdapter adapter, final List<ObjectAdapter> argumentsIfAvailable) {
075        final Object choices = AdapterInvokeUtils.invokeAutofit(method, adapter, argumentsIfAvailable, getAdapterManager());
076        if (choices == null) {
077            return new Object[0];
078        }
079        if (choices.getClass().isArray()) {
080            return ObjectExtensions.asArray(choices);
081        } else {
082            final ObjectSpecification specification = getSpecification(choicesType);
083            return CollectionUtils.getCollectionAsObjectArray(choices, specification, getAdapterManager());
084        }
085    }
086
087    @Override
088    protected String toStringValues() {
089        return "method=" + method + ",type=" + choicesType;
090    }
091
092}