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.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.actions.choices.ActionChoicesFacetAbstract; 033import org.apache.isis.core.metamodel.spec.DomainModelException; 034import org.apache.isis.core.metamodel.spec.ObjectSpecification; 035import org.apache.isis.core.metamodel.spec.SpecificationLoader; 036import org.apache.isis.core.progmodel.facets.CollectionUtils; 037 038public class ActionChoicesFacetViaMethod extends ActionChoicesFacetAbstract implements ImperativeFacet { 039 040 private final Method method; 041 private final Class<?> choicesType; 042 private final SpecificationLoader specificationLookup; 043 private final AdapterManager adapterMap; 044 045 public ActionChoicesFacetViaMethod(final Method method, final Class<?> choicesType, final FacetHolder holder, final SpecificationLoader specificationLookup, final AdapterManager adapterManager) { 046 super(holder); 047 this.method = method; 048 this.choicesType = choicesType; 049 this.specificationLookup = specificationLookup; 050 this.adapterMap = adapterManager; 051 } 052 053 /** 054 * Returns a singleton list of the {@link Method} provided in the 055 * constructor. 056 */ 057 @Override 058 public List<Method> getMethods() { 059 return Collections.singletonList(method); 060 } 061 062 @Override 063 public Intent getIntent(final Method method) { 064 return Intent.CHOICES_OR_AUTOCOMPLETE; 065 } 066 067 @Override 068 public boolean impliesResolve() { 069 return true; 070 } 071 072 @Override 073 public boolean impliesObjectChanged() { 074 return false; 075 } 076 077 @Override 078 public Object[][] getChoices(final ObjectAdapter owningAdapter) { 079 final Object invoke = AdapterInvokeUtils.invoke(method, owningAdapter); 080 if (!(invoke instanceof Object[])) { 081 throw new DomainModelException("Expected an array of collections (Object[]) containing choices for all parameters, but got " + invoke + " instead. Perhaps the parameter number is missing!"); 082 } 083 final Object[] options = (Object[]) invoke; 084 final Object[][] results = new Object[options.length][]; 085 for (int i = 0; i < results.length; i++) { 086 if (options[i] == null) { 087 results[i] = null; 088 } else if (options[i].getClass().isArray()) { 089 results[i] = ObjectExtensions.asArray(options[i]); 090 } else { 091 final ObjectSpecification specification = getSpecificationLookup().loadSpecification(choicesType); 092 results[i] = CollectionUtils.getCollectionAsObjectArray(options[i], specification, getAdapterMap()); 093 } 094 } 095 return results; 096 } 097 098 @Override 099 protected String toStringValues() { 100 return "method=" + method + ",type=" + choicesType; 101 } 102 103 // /////////////////////////////////////////////////////// 104 // Dependencies 105 // /////////////////////////////////////////////////////// 106 107 private SpecificationLoader getSpecificationLookup() { 108 return specificationLookup; 109 } 110 111 private AdapterManager getAdapterMap() { 112 return adapterMap; 113 } 114 115}