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.mandatory.staticmethod;
021
022import java.lang.reflect.Method;
023import java.util.List;
024
025import org.apache.isis.core.commons.lang.MethodExtensions;
026import org.apache.isis.core.commons.lang.StringExtensions;
027import org.apache.isis.core.metamodel.exceptions.MetaModelException;
028import org.apache.isis.core.metamodel.facetapi.Facet;
029import org.apache.isis.core.metamodel.facetapi.FacetUtil;
030import org.apache.isis.core.metamodel.facetapi.FeatureType;
031import org.apache.isis.core.metamodel.facets.FacetedMethod;
032import org.apache.isis.core.metamodel.facets.FacetedMethodParameter;
033import org.apache.isis.core.metamodel.methodutils.MethodScope;
034import org.apache.isis.core.progmodel.facets.MethodFinderUtils;
035import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract;
036import org.apache.isis.core.progmodel.facets.MethodPrefixConstants;
037
038/**
039 * Sets up all the {@link Facet}s for an action in a single shot.
040 */
041public class ActionParameterOptionalViaMethodFacetFactory extends MethodPrefixBasedFacetFactoryAbstract {
042
043    private static final String[] PREFIXES = { MethodPrefixConstants.OPTIONAL_PREFIX };
044
045    /**
046     * Note that the {@link Facet}s registered are the generic ones from
047     * noa-architecture (where they exist)
048     */
049    public ActionParameterOptionalViaMethodFacetFactory() {
050        super(FeatureType.ACTIONS_ONLY, OrphanValidation.VALIDATE, PREFIXES);
051    }
052
053    @Override
054    public void process(final ProcessMethodContext processMethodContext) {
055
056        final FacetedMethod facetedMethod = processMethodContext.getFacetHolder();
057        final List<FacetedMethodParameter> holderList = facetedMethod.getParameters();
058
059        attachMandatoryFacetForParametersIfOptionalMethodIsFound(processMethodContext, holderList);
060
061    }
062
063    private static void attachMandatoryFacetForParametersIfOptionalMethodIsFound(final ProcessMethodContext processMethodContext, final List<FacetedMethodParameter> parameters) {
064
065        if (parameters.isEmpty()) {
066            return;
067        }
068
069        final Method actionMethod = processMethodContext.getMethod();
070        final String capitalizedName = StringExtensions.asCapitalizedName(actionMethod.getName());
071
072        final Class<?> cls = processMethodContext.getCls();
073        final Method optionalMethod = MethodFinderUtils.findMethod(cls, MethodScope.CLASS, MethodPrefixConstants.OPTIONAL_PREFIX + capitalizedName, boolean[].class, new Class[0]);
074        if (optionalMethod == null) {
075            return;
076        }
077        try {
078            final boolean[] optionals = invokeOptionalsMethod(optionalMethod, parameters.size());
079
080            for (int i = 0; i < optionals.length; i++) {
081                if (optionals[i]) {
082                    // add facets directly to parameters, not to actions
083                    FacetUtil.addFacet(new MandatoryFacetOptionalViaMethodForParameter(parameters.get(i)));
084                }
085            }
086        } finally {
087            processMethodContext.removeMethod(optionalMethod);
088        }
089    }
090
091    private static boolean[] invokeOptionalsMethod(final Method optionalMethod, final int numElementsRequired) {
092        boolean[] optionals = null;
093        try {
094            optionals = (boolean[]) MethodExtensions.invokeStatic(optionalMethod, new Object[0]);
095        } catch (final ClassCastException ex) {
096            // ignore, test below
097        }
098        if (optionals == null || optionals.length != numElementsRequired) {
099            throw new MetaModelException(optionalMethod + " must return an boolean[] array of same size as number of parameters of action");
100        }
101        return optionals;
102    }
103
104}