Coverage Report - br.com.arsmachina.tapestrycrud.services.impl.ActivationContextEncoderSourceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActivationContextEncoderSourceImpl
0%
0/29
0%
0/16
0
 
 1  
 // Copyright 2008 Thiago H. de Paula Figueiredo
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package br.com.arsmachina.tapestrycrud.services.impl;
 16  
 
 17  
 import java.util.HashMap;
 18  
 import java.util.Map;
 19  
 
 20  
 import org.apache.tapestry5.PrimaryKeyEncoder;
 21  
 import org.apache.tapestry5.ioc.util.StrategyRegistry;
 22  
 
 23  
 import br.com.arsmachina.tapestrycrud.encoder.ActivationContextEncoder;
 24  
 import br.com.arsmachina.tapestrycrud.services.ActivationContextEncoderSource;
 25  
 import br.com.arsmachina.tapestrycrud.services.EncoderSource;
 26  
 import br.com.arsmachina.tapestrycrud.services.PrimaryKeyEncoderSource;
 27  
 import br.com.arsmachina.tapestrycrud.services.PrimaryKeyTypeService;
 28  
 
 29  
 /**
 30  
  * {@link ActivationContextEncoderSource} implementation.
 31  
  * 
 32  
  * @author Thiago H. de Paula Figueiredo
 33  
  */
 34  
 public class ActivationContextEncoderSourceImpl implements ActivationContextEncoderSource {
 35  
 
 36  
         @SuppressWarnings("unchecked")
 37  
         final private StrategyRegistry<ActivationContextEncoder> registry;
 38  
 
 39  
         final private EncoderSource encoderSource;
 40  
         
 41  
         final private PrimaryKeyEncoderSource primaryKeyEncoderSource;
 42  
         
 43  
         final private PrimaryKeyTypeService primaryKeyTypeService;
 44  
 
 45  
         @SuppressWarnings("unchecked")
 46  0
         final private Map<Class, ActivationContextEncoder> additionalEncoders = new HashMap<Class, ActivationContextEncoder>();
 47  
 
 48  
         /**
 49  
          * Single constructor.
 50  
          * 
 51  
          * @param registrations
 52  
          */
 53  
         @SuppressWarnings("unchecked")
 54  0
         public ActivationContextEncoderSourceImpl(Map<Class, ActivationContextEncoder> registrations,
 55  
                         EncoderSource encoderSource, PrimaryKeyEncoderSource primaryKeyEncoderSource,
 56  
                         PrimaryKeyTypeService primaryKeyTypeService) {
 57  
 
 58  0
                 if (registrations == null) {
 59  0
                         throw new IllegalArgumentException("Parameter registrations cannot be null");
 60  
                 }
 61  
 
 62  0
                 if (encoderSource == null) {
 63  0
                         throw new IllegalArgumentException("Parameter encoderSource cannot be null");
 64  
                 }
 65  
                 
 66  0
                 if (primaryKeyTypeService == null) {
 67  0
                         throw new IllegalArgumentException("Parameter primaryKeyTypeService cannot be null");
 68  
                 }
 69  
                 
 70  0
                 registry = StrategyRegistry.newInstance(ActivationContextEncoder.class, registrations, true);
 71  
 
 72  0
                 this.encoderSource = encoderSource;
 73  0
                 this.primaryKeyEncoderSource = primaryKeyEncoderSource;
 74  0
                 this.primaryKeyTypeService = primaryKeyTypeService;
 75  
                 
 76  
 
 77  0
         }
 78  
 
 79  
         @SuppressWarnings("unchecked")
 80  
         public <T> ActivationContextEncoder<T> get(Class<T> clasz) {
 81  
 
 82  0
                 ActivationContextEncoder<T> encoder = registry.get(clasz);
 83  
 
 84  0
                 if (encoder == null) {
 85  0
                         encoder = encoderSource.get(clasz);
 86  
                 }
 87  
 
 88  0
                 if (encoder == null) {
 89  
 
 90  0
                         encoder = additionalEncoders.get(clasz);
 91  
                         
 92  0
                         if (encoder == null) {
 93  
                                 
 94  0
                                 PrimaryKeyEncoder<?, T> primaryKeyEncoder = primaryKeyEncoderSource.get(clasz);
 95  
                                 
 96  0
                                 if (primaryKeyEncoder != null) {
 97  
                                         
 98  0
                                         Class primaryKeyType = primaryKeyTypeService.getPrimaryKeyType(clasz);
 99  0
                                         encoder = 
 100  0
                                                 new PrimaryKeyEncoderActivationContextEncoder(primaryKeyEncoder, primaryKeyType);
 101  
                                         
 102  0
                                         additionalEncoders.put(clasz, encoder);
 103  
                                         
 104  
                                 }
 105  
                                 
 106  
                         }
 107  
                         
 108  
                 }
 109  
 
 110  0
                 if (encoder == null) {
 111  0
                         throw new IllegalArgumentException(
 112  0
                                         "There is no ActivationContextEncoder configured for class " + clasz.getName());
 113  
                 }
 114  
 
 115  0
                 return encoder;
 116  
 
 117  
         }
 118  
 
 119  
 }