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.regexannot;
021
022 import org.apache.isis.applib.annotation.RegEx;
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.progmodel.facets.object.regex.RegExFacet;
029 import org.apache.isis.core.progmodel.facets.object.regex.TitleFacetFormattedByRegex;
030
031 public class RegExFacetAnnotationForPropertyFacetFactory extends AnnotationBasedFacetFactoryAbstract {
032
033 public RegExFacetAnnotationForPropertyFacetFactory() {
034 super(FeatureType.PROPERTIES_ONLY);
035 }
036
037 @Override
038 public void process(final ProcessMethodContext processMethodContext) {
039 final Class<?> returnType = processMethodContext.getMethod().getReturnType();
040 if (!isString(returnType)) {
041 return;
042 }
043 final RegEx annotation = getAnnotation(processMethodContext.getMethod(), RegEx.class);
044 addRegexFacetAndCorrespondingTitleFacet(processMethodContext.getFacetHolder(), annotation);
045 }
046
047 private void addRegexFacetAndCorrespondingTitleFacet(final FacetHolder holder, final RegEx annotation) {
048 final RegExFacet regexFacet = createRegexFacet(annotation, holder);
049 if (regexFacet == null) {
050 return;
051 }
052 FacetUtil.addFacet(regexFacet);
053
054 final TitleFacet titleFacet = createTitleFacet(regexFacet);
055 FacetUtil.addFacet(titleFacet);
056 }
057
058 private RegExFacet createRegexFacet(final RegEx annotation, final FacetHolder holder) {
059 if (annotation == null) {
060 return null;
061 }
062
063 final String validationExpression = annotation.validation();
064 final boolean caseSensitive = annotation.caseSensitive();
065 final String formatExpression = annotation.format();
066
067 return new RegExFacetAnnotationForProperty(validationExpression, formatExpression, caseSensitive, holder);
068 }
069
070 private TitleFacet createTitleFacet(final RegExFacet regexFacet) {
071 return new TitleFacetFormattedByRegex(regexFacet);
072 }
073
074 }