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.actions.defaults.method;
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.facetapi.Facet;
029 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
030 import org.apache.isis.core.metamodel.facetapi.FacetUtil;
031 import org.apache.isis.core.metamodel.facetapi.FeatureType;
032 import org.apache.isis.core.metamodel.methodutils.MethodScope;
033 import org.apache.isis.core.progmodel.facets.MethodFinderUtils;
034 import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract;
035 import org.apache.isis.core.progmodel.facets.MethodPrefixConstants;
036
037 /**
038 * Sets up all the {@link Facet}s for an action in a single shot.
039 */
040 public class ActionDefaultsFacetFactory extends MethodPrefixBasedFacetFactoryAbstract implements AdapterMapAware {
041
042 private static final String[] PREFIXES = { MethodPrefixConstants.DEFAULT_PREFIX };
043
044 private AdapterMap adapterMap;
045
046 /**
047 * Note that the {@link Facet}s registered are the generic ones from noa-architecture (where they exist)
048 */
049 public ActionDefaultsFacetFactory() {
050 super(FeatureType.ACTIONS_ONLY, PREFIXES);
051 }
052
053 // ///////////////////////////////////////////////////////
054 // Actions
055 // ///////////////////////////////////////////////////////
056
057 @Override
058 public void process(final ProcessMethodContext processMethodContext) {
059
060 attachActionDefaultsFacetIfParameterDefaultsMethodIsFound(processMethodContext);
061 }
062
063 private static void attachActionDefaultsFacetIfParameterDefaultsMethodIsFound(
064 final ProcessMethodContext processMethodContext) {
065
066 Method defaultsMethod = findDefaultsMethodReturning(processMethodContext, Object[].class);
067 if (defaultsMethod == null) {
068 defaultsMethod = findDefaultsMethodReturning(processMethodContext, List.class);
069 }
070 if (defaultsMethod == null) {
071 return;
072 }
073
074 processMethodContext.removeMethod(defaultsMethod);
075
076 final FacetHolder facetedMethod = processMethodContext.getFacetHolder();
077 final ActionDefaultsFacetViaMethod facet = new ActionDefaultsFacetViaMethod(defaultsMethod, facetedMethod);
078 FacetUtil.addFacet(facet);
079 }
080
081 private static Method findDefaultsMethodReturning(final ProcessMethodContext processMethodContext,
082 final Class<?> returnType) {
083
084 final Method actionMethod = processMethodContext.getMethod();
085 final String capitalizedName = NameUtils.capitalizeName(actionMethod.getName());
086 final String name = MethodPrefixConstants.DEFAULT_PREFIX + capitalizedName;
087
088 final Class<?> cls = processMethodContext.getCls();
089 return MethodFinderUtils.findMethod(cls, MethodScope.OBJECT, name, returnType, new Class[0]);
090 }
091
092 // ///////////////////////////////////////////////////////////////
093 // Dependencies
094 // ///////////////////////////////////////////////////////////////
095
096 @Override
097 public void setAdapterMap(final AdapterMap adapterMap) {
098 this.adapterMap = adapterMap;
099 }
100
101 private AdapterMap getAdapterMap() {
102 return adapterMap;
103 }
104
105 }