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.autocomplete; 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.ImperativeFacet.Intent; 033import org.apache.isis.core.metamodel.spec.ObjectSpecification; 034import org.apache.isis.core.metamodel.spec.SpecificationLoader; 035import org.apache.isis.core.progmodel.facets.CollectionUtils; 036 037public class ActionParameterAutoCompleteFacetViaMethod extends ActionParameterAutoCompleteFacetAbstract implements ImperativeFacet { 038 039 private final Method method; 040 private final Class<?> choicesType; 041 private final int minLength; 042 043 public ActionParameterAutoCompleteFacetViaMethod(final Method method, final Class<?> choicesType, final FacetHolder holder, final SpecificationLoader specificationLookup, final AdapterManager adapterManager) { 044 super(holder, specificationLookup, adapterManager); 045 this.method = method; 046 this.choicesType = choicesType; 047 this.minLength = MinLengthUtil.determineMinLength(method); 048 } 049 050 /** 051 * Returns a singleton list of the {@link Method} provided in the 052 * constructor. 053 */ 054 @Override 055 public List<Method> getMethods() { 056 return Collections.singletonList(method); 057 } 058 059 @Override 060 public Intent getIntent(final Method method) { 061 return Intent.CHOICES_OR_AUTOCOMPLETE; 062 } 063 064 @Override 065 public int getMinLength() { 066 return minLength; 067 } 068 069 @Override 070 public boolean impliesResolve() { 071 return true; 072 } 073 074 @Override 075 public boolean impliesObjectChanged() { 076 return false; 077 } 078 079 @Override 080 public Object[] autoComplete(ObjectAdapter owningAdapter, String searchArg) { 081 final Object options = AdapterInvokeUtils.invoke(method, owningAdapter, searchArg); 082 if (options == null) { 083 return new Object[0]; 084 } 085 if (options.getClass().isArray()) { 086 return ObjectExtensions.asArray(options); 087 } else { 088 final ObjectSpecification specification = getSpecification(choicesType); 089 return CollectionUtils.getCollectionAsObjectArray(options, specification, getAdapterManager()); 090 } 091 } 092 093 @Override 094 protected String toStringValues() { 095 return "method=" + method + ",type=" + choicesType; 096 } 097 098 099}