001/**
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.isis.core.metamodel.specloader.specimpl;
018
019import java.util.Arrays;
020import java.util.Collections;
021import java.util.List;
022
023import com.google.common.collect.Lists;
024
025import org.apache.isis.core.commons.lang.ListExtensions;
026import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
027import org.apache.isis.core.metamodel.facets.object.bounded.ChoicesFacetUtils;
028import org.apache.isis.core.metamodel.facets.param.choices.ActionParameterChoicesFacet;
029import org.apache.isis.core.metamodel.facets.param.defaults.ActionParameterDefaultsFacet;
030import org.apache.isis.core.metamodel.spec.feature.ObjectActionParameter;
031
032/**
033 * REVIEW: this is a bit hacky having 'Contributed' subtypes of both {@link ObjectActionParameterParseable} and also
034 * {@link OneToOneActionParameterImpl}.  However, the {@link ObjectActionParameterParseable parseable} version
035 * only seems to be used by the DnD viewer; Scimpi, Wicket and RO do not.  So, we could hopefully simplify the
036 * hierarchy at some point.
037 */
038public class ObjectActionParameterParseableContributee extends ObjectActionParameterParseable implements ObjectActionParameterContributee {
039
040    private final ObjectAdapter serviceAdapter;
041    @SuppressWarnings("unused")
042    private final ObjectActionImpl serviceAction;
043    private final ObjectActionParameter serviceActionParameter;
044    @SuppressWarnings("unused")
045    private final int serviceParamNumber;
046    @SuppressWarnings("unused")
047    private final int contributeeParamNumber;
048    private final ObjectActionContributee contributeeAction;
049
050    public ObjectActionParameterParseableContributee(
051            final ObjectAdapter serviceAdapter,
052            final ObjectActionImpl serviceAction,
053            final ObjectActionParameterAbstract serviceActionParameter,
054            final int serviceParamNumber,
055            final int contributeeParamNumber,
056            final ObjectActionContributee contributeeAction) {
057        super(contributeeParamNumber, contributeeAction, serviceActionParameter.getPeer());
058        this.serviceAdapter = serviceAdapter;
059        this.serviceAction = serviceAction;
060        this.serviceActionParameter = serviceActionParameter;
061        this.serviceParamNumber = serviceParamNumber;
062        this.contributeeParamNumber = contributeeParamNumber;
063        this.contributeeAction = contributeeAction;
064    }
065
066    @Override
067    public ObjectAdapter[] getAutoComplete(ObjectAdapter adapter, String searchArg) {
068        return serviceActionParameter.getAutoComplete(serviceAdapter, searchArg);
069    }
070
071    protected ObjectAdapter targetForDefaultOrChoices(ObjectAdapter adapter, final List<ObjectAdapter> argumentsIfAvailable) {
072        return serviceAdapter;
073    }
074
075    protected List<ObjectAdapter> argsForDefaultOrChoices(final ObjectAdapter contributee, final List<ObjectAdapter> argumentsIfAvailable) {
076
077        final List<ObjectAdapter> suppliedArgs = ListExtensions.mutableCopy(argumentsIfAvailable);
078        
079        final int contributeeParam = contributeeAction.getContributeeParam();
080        ListExtensions.insert(suppliedArgs, contributeeParam, contributee);
081        
082        return suppliedArgs;
083    }
084
085    
086}