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.properties.validate.maskannot;
021    
022    import org.apache.isis.applib.annotation.Mask;
023    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
024    import org.apache.isis.core.metamodel.facetapi.FacetUtil;
025    import org.apache.isis.core.metamodel.facetapi.FeatureType;
026    import org.apache.isis.core.metamodel.facets.AnnotationBasedFacetFactoryAbstract;
027    import org.apache.isis.core.metamodel.facets.object.title.TitleFacet;
028    import org.apache.isis.core.metamodel.spec.ObjectSpecification;
029    import org.apache.isis.core.progmodel.facets.object.mask.MaskFacet;
030    import org.apache.isis.core.progmodel.facets.object.mask.TitleFacetBasedOnMask;
031    
032    public class MaskAnnotationForPropertyFacetFactory extends AnnotationBasedFacetFactoryAbstract {
033    
034        public MaskAnnotationForPropertyFacetFactory() {
035            super(FeatureType.PROPERTIES_ONLY);
036        }
037    
038        /**
039         * In readiness for supporting <tt>@Value</tt> in the future.
040         */
041        @Override
042        public void process(final ProcessClassContext processClassContaxt) {
043            final Mask annotation = getAnnotation(processClassContaxt.getCls(), Mask.class);
044            FacetUtil.addFacet(createMaskFacet(annotation, processClassContaxt.getFacetHolder()));
045        }
046    
047        @Override
048        public void process(final ProcessMethodContext processMethodContext) {
049            if (processMethodContext.getMethod().getReturnType() == void.class) {
050                return;
051            }
052    
053            final Mask annotation = getAnnotation(processMethodContext.getMethod(), Mask.class);
054            addMaskFacetAndCorrespondingTitleFacet(processMethodContext.getFacetHolder(), annotation, processMethodContext
055                .getMethod().getReturnType());
056        }
057    
058        @Override
059        public void processParams(final ProcessParameterContext processParameterContext) {
060            final Class<?>[] parameterTypes = processParameterContext.getMethod().getParameterTypes();
061            if (processParameterContext.getParamNum() >= parameterTypes.length) {
062                // ignore
063                return;
064            }
065    
066            final java.lang.annotation.Annotation[] parameterAnnotations =
067                getParameterAnnotations(processParameterContext.getMethod())[processParameterContext.getParamNum()];
068            for (int i = 0; i < parameterAnnotations.length; i++) {
069                if (parameterAnnotations[i] instanceof Mask) {
070                    final Mask annotation = (Mask) parameterAnnotations[i];
071                    addMaskFacetAndCorrespondingTitleFacet(processParameterContext.getFacetHolder(), annotation,
072                        parameterTypes[i]);
073                    return;
074                }
075            }
076        }
077    
078        private MaskFacet createMaskFacet(final Mask annotation, final FacetHolder holder) {
079            return annotation != null ? new MaskFacetAnnotationForProperty(annotation.value(), null, holder) : null;
080        }
081    
082        private boolean addMaskFacetAndCorrespondingTitleFacet(final FacetHolder holder, final Mask annotation,
083            final Class<?> cls) {
084            final MaskFacet maskFacet = createMaskFacet(annotation, holder);
085            if (maskFacet == null) {
086                return false;
087            }
088            FacetUtil.addFacet(maskFacet);
089    
090            final ObjectSpecification type = getSpecificationLookup().loadSpecification(cls);
091            final TitleFacet underlyingTitleFacet = type.getFacet(TitleFacet.class);
092            if (underlyingTitleFacet != null) {
093                final TitleFacet titleFacet = new TitleFacetBasedOnMask(maskFacet, underlyingTitleFacet);
094                FacetUtil.addFacet(titleFacet);
095            }
096            return true;
097        }
098    
099    }