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.choices.method;
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.properties.choices.PropertyChoicesFacetAbstract;
037
038public class PropertyChoicesFacetViaMethod extends PropertyChoicesFacetAbstract implements ImperativeFacet {
039
040    private final Method method;
041    private final Class<?> choicesClass;
042
043    private final AdapterManager adapterManager;
044
045    public PropertyChoicesFacetViaMethod(final Method method, final Class<?> choicesClass, final FacetHolder holder, final SpecificationLoader specificationLookup, final AdapterManager adapterManager) {
046        super(holder, specificationLookup);
047        this.method = method;
048        this.choicesClass = choicesClass;
049        this.adapterManager = adapterManager;
050    }
051
052    /**
053     * Returns a singleton list of the {@link Method} provided in the
054     * constructor.
055     */
056    @Override
057    public List<Method> getMethods() {
058        return Collections.singletonList(method);
059    }
060
061    @Override
062    public Intent getIntent(final Method method) {
063        return Intent.CHOICES_OR_AUTOCOMPLETE;
064    }
065
066    @Override
067    public boolean impliesResolve() {
068        return true;
069    }
070
071    @Override
072    public boolean impliesObjectChanged() {
073        return false;
074    }
075
076    @Override
077    public Object[] getChoices(final ObjectAdapter owningAdapter, final SpecificationLoader specificationLookup) {
078        final Object options = AdapterInvokeUtils.invoke(method, owningAdapter);
079        if (options == null) {
080            return null;
081        }
082        if (options.getClass().isArray()) {
083            return ObjectExtensions.asArray(options);
084        }
085        final ObjectSpecification specification = specificationLookup.loadSpecification(choicesClass);
086        return CollectionUtils.getCollectionAsObjectArray(options, specification, getAdapterManager());
087    }
088
089    @Override
090    protected String toStringValues() {
091        return "method=" + method + ",class=" + choicesClass;
092    }
093
094    // ////////////////////////////////////////////
095    // Dependencies
096    // ////////////////////////////////////////////
097
098    protected AdapterManager getAdapterManager() {
099        return adapterManager;
100    }
101
102}