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    
020    package org.apache.isis.core.progmodel.facets.param.named.staticmethod;
021    
022    import java.lang.reflect.Method;
023    import java.util.List;
024    
025    import org.apache.isis.core.commons.lang.NameUtils;
026    import org.apache.isis.core.metamodel.adapter.map.AdapterMap;
027    import org.apache.isis.core.metamodel.adapter.map.AdapterMapAware;
028    import org.apache.isis.core.metamodel.adapter.util.InvokeUtils;
029    import org.apache.isis.core.metamodel.exceptions.MetaModelException;
030    import org.apache.isis.core.metamodel.facetapi.Facet;
031    import org.apache.isis.core.metamodel.facetapi.FacetUtil;
032    import org.apache.isis.core.metamodel.facetapi.FeatureType;
033    import org.apache.isis.core.metamodel.facets.FacetedMethod;
034    import org.apache.isis.core.metamodel.facets.FacetedMethodParameter;
035    import org.apache.isis.core.metamodel.methodutils.MethodScope;
036    import org.apache.isis.core.progmodel.facets.MethodFinderUtils;
037    import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract;
038    import org.apache.isis.core.progmodel.facets.MethodPrefixConstants;
039    import org.apache.isis.core.progmodel.facets.members.named.staticmethod.NamedFacetViaMethod;
040    
041    /**
042     * Sets up all the {@link Facet}s for an action in a single shot.
043     */
044    public class ActionParameterNamesMethodFacetFactory extends MethodPrefixBasedFacetFactoryAbstract implements
045        AdapterMapAware {
046    
047        private static final String[] PREFIXES = { MethodPrefixConstants.NAME_PREFIX };
048    
049        private AdapterMap adapterMap;
050    
051        /**
052         * Note that the {@link Facet}s registered are the generic ones from noa-architecture (where they exist)
053         */
054        public ActionParameterNamesMethodFacetFactory() {
055            super(FeatureType.ACTIONS_ONLY, PREFIXES);
056        }
057    
058        // ///////////////////////////////////////////////////////
059        // Actions
060        // ///////////////////////////////////////////////////////
061    
062        @Override
063        public void process(final ProcessMethodContext processMethodContext) {
064    
065            final FacetedMethod facetedMethod = processMethodContext.getFacetHolder();
066            final List<FacetedMethodParameter> holderList = facetedMethod.getParameters();
067    
068            attachNamedFacetForParametersIfParameterNamesMethodIsFound(processMethodContext, holderList);
069        }
070    
071        private static void attachNamedFacetForParametersIfParameterNamesMethodIsFound(
072            final ProcessMethodContext processMethodContext, final List<FacetedMethodParameter> parameters) {
073    
074            if (parameters.isEmpty()) {
075                return;
076            }
077    
078            final Method actionMethod = processMethodContext.getMethod();
079            final String capitalizedName = NameUtils.capitalizeName(actionMethod.getName());
080    
081            final Class<?> cls = processMethodContext.getCls();
082            final Method namesMethod =
083                MethodFinderUtils.findMethod(cls, MethodScope.CLASS, MethodPrefixConstants.NAME_PREFIX + capitalizedName,
084                    String[].class, new Class[0]);
085            if (namesMethod == null) {
086                return;
087            }
088            try {
089                final String[] names = invokeNamesMethod(namesMethod, parameters.size());
090                for (int i = 0; i < names.length; i++) {
091                    // add facets directly to parameters, not to actions
092                    FacetUtil.addFacet(new NamedFacetViaMethod(names[i], namesMethod, parameters.get(i)));
093                }
094            } finally {
095                processMethodContext.removeMethod(namesMethod);
096            }
097        }
098    
099        private static String[] invokeNamesMethod(final Method namesMethod, final int numElementsRequired) {
100            String[] names = null;
101            try {
102                names = (String[]) InvokeUtils.invokeStatic(namesMethod, new Object[0]);
103            } catch (final ClassCastException ex) {
104                // ignore
105            }
106            if (names == null || names.length != numElementsRequired) {
107                throw new MetaModelException(namesMethod
108                    + " must return an String[] array of same size as number of parameters of action");
109            }
110            return names;
111        }
112    
113        // ///////////////////////////////////////////////////////////////
114        // Dependencies
115        // ///////////////////////////////////////////////////////////////
116    
117        @Override
118        public void setAdapterMap(final AdapterMap adapterMap) {
119            this.adapterMap = adapterMap;
120        }
121    
122        private AdapterMap getAdapterMap() {
123            return adapterMap;
124        }
125    
126    }