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