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.collections.clear;
021
022import java.lang.reflect.Method;
023
024import org.apache.isis.core.commons.lang.StringExtensions;
025import org.apache.isis.core.metamodel.adapter.ObjectDirtier;
026import org.apache.isis.core.metamodel.adapter.ObjectDirtierAware;
027import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager;
028import org.apache.isis.core.metamodel.adapter.mgr.AdapterManagerAware;
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.facets.collections.modify.CollectionClearFacet;
033import org.apache.isis.core.metamodel.methodutils.MethodScope;
034import org.apache.isis.core.progmodel.facets.MethodFinderUtils;
035import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract;
036import org.apache.isis.core.progmodel.facets.MethodPrefixConstants;
037
038public class CollectionClearFacetFactory extends MethodPrefixBasedFacetFactoryAbstract implements AdapterManagerAware, ObjectDirtierAware {
039
040    private static final String[] PREFIXES = { MethodPrefixConstants.CLEAR_PREFIX };
041
042    private AdapterManager adapterManager;
043    private ObjectDirtier objectDirtier;
044
045    public CollectionClearFacetFactory() {
046        super(FeatureType.COLLECTIONS_ONLY, OrphanValidation.VALIDATE, PREFIXES);
047    }
048
049    @Override
050    public void process(final ProcessMethodContext processMethodContext) {
051        attachCollectionClearFacets(processMethodContext);
052
053    }
054
055    private void attachCollectionClearFacets(final ProcessMethodContext processMethodContext) {
056
057        final Method getMethod = processMethodContext.getMethod();
058        final String capitalizedName = StringExtensions.asJavaBaseName(getMethod.getName());
059
060        final Class<?> cls = processMethodContext.getCls();
061        final Method method = MethodFinderUtils.findMethod(cls, MethodScope.OBJECT, MethodPrefixConstants.CLEAR_PREFIX + capitalizedName, void.class, null);
062        processMethodContext.removeMethod(method);
063
064        final FacetHolder collection = processMethodContext.getFacetHolder();
065        FacetUtil.addFacet(createCollectionClearFacet(method, getMethod, collection));
066    }
067
068    private CollectionClearFacet createCollectionClearFacet(final Method clearMethodIfAny, final Method accessorMethod, final FacetHolder collection) {
069        if (clearMethodIfAny != null) {
070            return new CollectionClearFacetViaMethod(clearMethodIfAny, collection);
071        } else {
072            return new CollectionClearFacetViaAccessor(accessorMethod, collection, getAdapterManager(), getObjectDirtier());
073        }
074    }
075
076    // ///////////////////////////////////////////////////////
077    // Dependencies (injected)
078    // ///////////////////////////////////////////////////////
079
080    protected AdapterManager getAdapterManager() {
081        return adapterManager;
082    }
083
084    @Override
085    public void setAdapterManager(final AdapterManager adapterManager) {
086        this.adapterManager = adapterManager;
087    }
088
089    protected ObjectDirtier getObjectDirtier() {
090        return objectDirtier;
091    }
092
093    @Override
094    public void setObjectDirtier(final ObjectDirtier objectDirtier) {
095        this.objectDirtier = objectDirtier;
096    }
097
098}