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.members.disabled.annotation;
021
022import java.util.Properties;
023
024import org.apache.isis.applib.annotation.Disabled;
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.Annotations;
029import org.apache.isis.core.metamodel.facets.ContributeeMemberFacetFactory;
030import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
031import org.apache.isis.core.progmodel.facets.members.disabled.DisabledFacet;
032import org.apache.isis.core.progmodel.facets.members.hidden.annotation.HiddenFacetOnMemberFromProperties;
033
034public class DisabledFacetFactory extends FacetFactoryAbstract 
035        implements ContributeeMemberFacetFactory {
036
037    public DisabledFacetFactory() {
038        super(FeatureType.MEMBERS);
039    }
040
041    @Override
042    public void process(final ProcessMethodContext processMethodContext) {
043        DisabledFacet disabledFacet = createFromMetadataPropertiesIfPossible(processMethodContext);
044        if(disabledFacet == null) {
045            disabledFacet = createFromAnnotationIfPossible(processMethodContext);
046        }
047        // no-op if null
048        FacetUtil.addFacet(disabledFacet);
049    }
050
051    @Override
052    public void process(ProcessContributeeMemberContext processMemberContext) {
053        DisabledFacet disabledFacet = createFromMetadataPropertiesIfPossible(processMemberContext);
054        // no-op if null
055        FacetUtil.addFacet(disabledFacet);
056    }
057
058    private static DisabledFacet createFromAnnotationIfPossible(final ProcessMethodContext processMethodContext) {
059        final Disabled annotation = Annotations.getAnnotation(processMethodContext.getMethod(), Disabled.class);
060        return annotation != null 
061                ? new DisabledFacetAnnotation(annotation.when(), annotation.where(), annotation.reason(), processMethodContext.getFacetHolder()) 
062                : null;
063    }
064
065    private static DisabledFacet createFromMetadataPropertiesIfPossible(
066            final ProcessContextWithMetadataProperties<? extends FacetHolder> pcwmp) {
067        
068        final FacetHolder holder = pcwmp.getFacetHolder();
069        
070        final Properties properties = pcwmp.metadataProperties("disabled");
071        return properties != null ? new DisabledFacetFromProperties(properties, holder) : null;
072    }
073
074
075}