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