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.regexannot;
021
022 import java.lang.annotation.Annotation;
023
024 import org.apache.isis.applib.annotation.RegEx;
025 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
026 import org.apache.isis.core.metamodel.facetapi.FacetUtil;
027 import org.apache.isis.core.metamodel.facetapi.FeatureType;
028 import org.apache.isis.core.metamodel.facets.AnnotationBasedFacetFactoryAbstract;
029 import org.apache.isis.core.metamodel.facets.object.title.TitleFacet;
030 import org.apache.isis.core.progmodel.facets.object.regex.RegExFacet;
031 import org.apache.isis.core.progmodel.facets.object.regex.TitleFacetFormattedByRegex;
032
033 public class RegExFacetAnnotationForParameterFacetFactory extends AnnotationBasedFacetFactoryAbstract {
034
035 public RegExFacetAnnotationForParameterFacetFactory() {
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 if (!isString(parameterTypes[processParameterContext.getParamNum()])) {
047 return;
048 }
049
050 final Annotation[] parameterAnnotations =
051 getParameterAnnotations(processParameterContext.getMethod())[processParameterContext.getParamNum()];
052 for (final Annotation parameterAnnotation : parameterAnnotations) {
053 if (parameterAnnotation instanceof RegEx) {
054 final RegEx annotation = (RegEx) parameterAnnotation;
055 addRegexFacetAndCorrespondingTitleFacet(processParameterContext.getFacetHolder(), annotation);
056 return;
057 }
058 }
059 }
060
061 private void addRegexFacetAndCorrespondingTitleFacet(final FacetHolder holder, final RegEx annotation) {
062 final RegExFacet regexFacet = createRegexFacet(annotation, holder);
063 if (regexFacet == null) {
064 return;
065 }
066 FacetUtil.addFacet(regexFacet);
067
068 final TitleFacet titleFacet = createTitleFacet(regexFacet);
069 FacetUtil.addFacet(titleFacet);
070 }
071
072 private RegExFacet createRegexFacet(final RegEx annotation, final FacetHolder holder) {
073 if (annotation == null) {
074 return null;
075 }
076
077 final String validationExpression = annotation.validation();
078 final boolean caseSensitive = annotation.caseSensitive();
079 final String formatExpression = annotation.format();
080
081 return new RegExFacetAnnotationForParameter(validationExpression, formatExpression, caseSensitive, holder);
082 }
083
084 private TitleFacet createTitleFacet(final RegExFacet regexFacet) {
085 return new TitleFacetFormattedByRegex(regexFacet);
086 }
087
088 }