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