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.param.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 MaskAnnotationForParameterFacetFactory extends FacetFactoryAbstract {
034
035    public MaskAnnotationForParameterFacetFactory() {
036        super(FeatureType.PARAMETERS_ONLY);
037    }
038
039    @Override
040    public void processParams(final ProcessParameterContext processParameterContext) {
041        final Class<?>[] parameterTypes = processParameterContext.getMethod().getParameterTypes();
042        if (processParameterContext.getParamNum() >= parameterTypes.length) {
043            // ignore
044            return;
045        }
046
047        final java.lang.annotation.Annotation[] parameterAnnotations = Annotations.getParameterAnnotations(processParameterContext.getMethod())[processParameterContext.getParamNum()];
048        for (int i = 0; i < parameterAnnotations.length; i++) {
049            if (parameterAnnotations[i] instanceof Mask) {
050                final Mask annotation = (Mask) parameterAnnotations[i];
051                addMaskFacetAndCorrespondingTitleFacet(processParameterContext.getFacetHolder(), annotation, parameterTypes[i]);
052                return;
053            }
054        }
055    }
056
057    private boolean addMaskFacetAndCorrespondingTitleFacet(final FacetHolder holder, final Mask annotation, final Class<?> cls) {
058        final MaskFacet maskFacet = createMaskFacet(annotation, holder);
059        if (maskFacet == null) {
060            return false;
061        }
062        FacetUtil.addFacet(maskFacet);
063
064        final ObjectSpecification type = getSpecificationLoader().loadSpecification(cls);
065        final TitleFacet underlyingTitleFacet = type.getFacet(TitleFacet.class);
066        if (underlyingTitleFacet != null) {
067            final TitleFacet titleFacet = new TitleFacetBasedOnMask(maskFacet, underlyingTitleFacet);
068            FacetUtil.addFacet(titleFacet);
069        }
070        return true;
071    }
072
073    private MaskFacet createMaskFacet(final Mask annotation, final FacetHolder holder) {
074        return annotation != null ? new MaskFacetAnnotationForParameter(annotation.value(), null, holder) : null;
075    }
076
077}