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