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 */
019package org.apache.isis.core.progmodel.facets.param.javaxvaldigits;
020
021import java.lang.annotation.Annotation;
022import java.lang.reflect.Method;
023import java.math.BigDecimal;
024
025import javax.validation.constraints.Digits;
026
027import org.apache.isis.core.metamodel.facetapi.FacetHolder;
028import org.apache.isis.core.metamodel.facetapi.FacetUtil;
029import org.apache.isis.core.metamodel.facetapi.FeatureType;
030import org.apache.isis.core.metamodel.facets.Annotations;
031import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
032import org.apache.isis.core.progmodel.facets.value.bigdecimal.BigDecimalValueFacet;
033
034public class BigDecimalForParameterDerivedFromJavaxValidationAnnotationFacetFactory extends FacetFactoryAbstract {
035
036    public BigDecimalForParameterDerivedFromJavaxValidationAnnotationFacetFactory() {
037        super(FeatureType.PARAMETERS_ONLY);
038    }
039
040    @Override
041    public void processParams(ProcessParameterContext processParameterContext) {
042
043        final Method method = processParameterContext.getMethod();
044        final int paramNum = processParameterContext.getParamNum();
045        
046        if(BigDecimal.class != method.getParameterTypes()[paramNum]) {
047            return;
048        }
049        final Annotation[] parameterAnnotations = Annotations.getParameterAnnotations(method)[paramNum];
050
051        for (final Annotation parameterAnnotation : parameterAnnotations) {
052            if (parameterAnnotation instanceof Digits) {
053                Digits digitsAnnotation = (Digits) parameterAnnotation;
054                FacetUtil.addFacet(create(digitsAnnotation, processParameterContext.getFacetHolder()));
055                return;
056            }
057        }
058    }
059
060    private BigDecimalValueFacet create(final Digits annotation, final FacetHolder holder) {
061        final int length = annotation.integer() + annotation.fraction();
062        final int scale = annotation.fraction();
063        return annotation == null ? null : 
064            new BigDecimalFacetForParameterFromJavaxValidationDigitsAnnotation(holder, length, scale);
065    }
066    
067}