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