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.method; 021 022import java.lang.reflect.Method; 023import java.util.Collection; 024 025import org.apache.isis.core.commons.lang.StringExtensions; 026import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager; 027import org.apache.isis.core.metamodel.adapter.mgr.AdapterManagerAware; 028import org.apache.isis.core.metamodel.facetapi.Facet; 029import org.apache.isis.core.metamodel.facetapi.FacetHolder; 030import org.apache.isis.core.metamodel.facetapi.FacetUtil; 031import org.apache.isis.core.metamodel.facetapi.FeatureType; 032import org.apache.isis.core.metamodel.methodutils.MethodScope; 033import org.apache.isis.core.progmodel.facets.MethodFinderUtils; 034import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract; 035import org.apache.isis.core.progmodel.facets.MethodPrefixConstants; 036 037public class ActionChoicesFacetFactory extends MethodPrefixBasedFacetFactoryAbstract implements AdapterManagerAware { 038 039 private static final String[] PREFIXES = { MethodPrefixConstants.CHOICES_PREFIX }; 040 041 private AdapterManager adapterManager; 042 043 /** 044 * Note that the {@link Facet}s registered are the generic ones from 045 * noa-architecture (where they exist) 046 */ 047 public ActionChoicesFacetFactory() { 048 super(FeatureType.ACTIONS_ONLY, OrphanValidation.VALIDATE, PREFIXES); 049 } 050 051 // /////////////////////////////////////////////////////// 052 // Actions 053 // /////////////////////////////////////////////////////// 054 055 @Override 056 public void process(final ProcessMethodContext processMethodContext) { 057 attachActionChoicesFacetIfParameterChoicesMethodIsFound(processMethodContext); 058 } 059 060 private void attachActionChoicesFacetIfParameterChoicesMethodIsFound(final ProcessMethodContext processMethodContext) { 061 062 final Method actionMethod = processMethodContext.getMethod(); 063 final Class<?>[] actionParamTypes = actionMethod.getParameterTypes(); 064 065 if (actionParamTypes.length <= 0) { 066 return; 067 } 068 069 Method choicesMethod = null; 070 if (choicesMethod == null) { 071 choicesMethod = findChoicesMethodReturning(processMethodContext, Object[][].class); 072 } 073 if (choicesMethod == null) { 074 choicesMethod = findChoicesMethodReturning(processMethodContext, Object[].class); 075 } 076 if (choicesMethod == null) { 077 choicesMethod = findChoicesMethodReturning(processMethodContext, Collection.class); 078 } 079 if (choicesMethod == null) { 080 return; 081 } 082 processMethodContext.removeMethod(choicesMethod); 083 084 final Class<?> returnType = actionMethod.getReturnType(); 085 final FacetHolder action = processMethodContext.getFacetHolder(); 086 final ActionChoicesFacetViaMethod facet = new ActionChoicesFacetViaMethod(choicesMethod, returnType, action, getSpecificationLoader(), getAdapterManager()); 087 FacetUtil.addFacet(facet); 088 } 089 090 protected Method findChoicesMethodReturning(final ProcessMethodContext processMethodContext, final Class<?> returnType2) { 091 Method choicesMethod; 092 final Class<?> cls = processMethodContext.getCls(); 093 094 final Method actionMethod = processMethodContext.getMethod(); 095 final MethodScope methodScope = MethodScope.scopeFor(actionMethod); 096 final String capitalizedName = StringExtensions.asCapitalizedName(actionMethod.getName()); 097 098 final String name = MethodPrefixConstants.CHOICES_PREFIX + capitalizedName; 099 choicesMethod = MethodFinderUtils.findMethod(cls, methodScope, name, returnType2, new Class[0]); 100 return choicesMethod; 101 } 102 103 // /////////////////////////////////////////////////////////////// 104 // Dependencies 105 // /////////////////////////////////////////////////////////////// 106 107 @Override 108 public void setAdapterManager(final AdapterManager adapterMap) { 109 this.adapterManager = adapterMap; 110 } 111 112 protected AdapterManager getAdapterManager() { 113 return adapterManager; 114 } 115}