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.object.disabled.method;
021
022import java.lang.reflect.Method;
023
024import org.apache.isis.applib.Identifier;
025import org.apache.isis.core.metamodel.facetapi.FacetHolder;
026import org.apache.isis.core.metamodel.facetapi.FacetUtil;
027import org.apache.isis.core.metamodel.facetapi.FeatureType;
028import org.apache.isis.core.metamodel.facets.FacetedMethod;
029import org.apache.isis.core.metamodel.methodutils.MethodScope;
030import org.apache.isis.core.metamodel.spec.ObjectSpecification;
031import org.apache.isis.core.metamodel.spec.feature.ObjectMember;
032import org.apache.isis.core.progmodel.facets.MethodFinderUtils;
033import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract;
034import org.apache.isis.core.progmodel.facets.object.disabled.DisabledObjectFacet;
035
036/**
037 * Installs the {@link DisabledObjectFacetViaDisabledMethod} on the
038 * {@link ObjectSpecification}, and copies this facet onto each
039 * {@link ObjectMember}.
040 * 
041 * <p>
042 * This two-pass design is required because, at the time that the
043 * {@link #process(org.apache.isis.core.metamodel.facets.FacetFactory.ProcessClassContext)
044 * class is being processed}, the {@link ObjectMember member}s for the
045 * {@link ObjectSpecification spec} are not known.
046 */
047public class DisabledObjectViaDisabledMethodFacetFactory extends MethodPrefixBasedFacetFactoryAbstract {
048
049    private static final String DISABLED_PREFIX = "disabled";
050
051    private static final String[] PREFIXES = { DISABLED_PREFIX, };
052
053    public DisabledObjectViaDisabledMethodFacetFactory() {
054        super(FeatureType.EVERYTHING_BUT_PARAMETERS, OrphanValidation.VALIDATE, PREFIXES);
055    }
056
057    @Override
058    public void process(final ProcessClassContext processClassContext) {
059        final Class<?> cls = processClassContext.getCls();
060        final FacetHolder facetHolder = processClassContext.getFacetHolder();
061        final Class<?>[] params = new Class<?>[1];
062        params[0] = Identifier.Type.class;// String.class;
063
064        final Method method = MethodFinderUtils.findMethod(cls, MethodScope.OBJECT, DISABLED_PREFIX, String.class, params);
065        if (method == null) {
066            return;
067        }
068        FacetUtil.addFacet(new DisabledObjectFacetViaDisabledMethod(method, facetHolder));
069        processClassContext.removeMethod(method);
070    }
071
072    @Override
073    public void process(final ProcessMethodContext processMethodContext) {
074        final FacetedMethod member = processMethodContext.getFacetHolder();
075        final Class<?> owningClass = processMethodContext.getCls();
076        final ObjectSpecification owningSpec = getSpecificationLoader().loadSpecification(owningClass);
077        final DisabledObjectFacet facet = owningSpec.getFacet(DisabledObjectFacet.class);
078        if (facet != null) {
079            facet.copyOnto(member);
080        }
081    }
082
083}