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.resolve;
021
022import java.util.Properties;
023
024import org.apache.isis.applib.annotation.Render;
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.metamodel.facets.members.resolve.RenderFacet;
032
033public class RenderOrResolveFacetFactory extends FacetFactoryAbstract 
034        implements ContributeeMemberFacetFactory {
035
036    public RenderOrResolveFacetFactory() {
037        super(FeatureType.MEMBERS);
038    }
039
040    @Override
041    public void process(final ProcessMethodContext processMethodContext) {
042        
043        RenderFacet renderFacet = createFromMetadataPropertiesIfPossible(processMethodContext);
044        if(renderFacet == null) {
045            renderFacet = createFromRenderAnnotationIfPossible(processMethodContext);
046        }
047        if(renderFacet == null) {
048            renderFacet = createFromResolveAnnotationIfPossible(processMethodContext);
049        }
050
051        // no-op if null
052        FacetUtil.addFacet(renderFacet);
053    }
054
055    @Override
056    public void process(ProcessContributeeMemberContext processMemberContext) {
057        RenderFacet renderFacet = createFromMetadataPropertiesIfPossible(processMemberContext);
058        // no-op if null
059        FacetUtil.addFacet(renderFacet);
060    }
061
062    private static RenderFacet createFromMetadataPropertiesIfPossible(
063            final ProcessContextWithMetadataProperties<? extends FacetHolder> pcwmp) {
064        
065        final FacetHolder holder = pcwmp.getFacetHolder();
066        
067        final Properties properties = pcwmp.metadataProperties("render");
068        return properties != null ? new RenderFacetProperties(properties, holder) : null;
069    }
070
071    private static RenderFacet createFromRenderAnnotationIfPossible(final ProcessMethodContext processMethodContext) {
072        final Render renderAnnotation = Annotations.getAnnotation(processMethodContext.getMethod(), Render.class);
073        return renderAnnotation == null ? null : new RenderFacetAnnotation(processMethodContext.getFacetHolder(), renderAnnotation.value());
074    }
075
076    // @Render was originally called @Resolve, so look for that annotation instead.
077    @SuppressWarnings("deprecation")
078    private static RenderFacet createFromResolveAnnotationIfPossible(final ProcessMethodContext processMethodContext) {
079        final org.apache.isis.applib.annotation.Resolve resolveAnnotation = 
080        Annotations.getAnnotation(processMethodContext.getMethod(), org.apache.isis.applib.annotation.Resolve.class);
081        return resolveAnnotation == null ? null : new RenderFacetViaResolveAnnotation(processMethodContext.getFacetHolder(), resolveAnnotation.value());
082    }
083}