View Javadoc

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  	final private Map<Class, ActivationContextEncoder> additionalEncoders = new HashMap<Class, ActivationContextEncoder>();
47  
48  	/**
49  	 * Single constructor.
50  	 * 
51  	 * @param registrations
52  	 */
53  	@SuppressWarnings("unchecked")
54  	public ActivationContextEncoderSourceImpl(Map<Class, ActivationContextEncoder> registrations,
55  			EncoderSource encoderSource, PrimaryKeyEncoderSource primaryKeyEncoderSource,
56  			PrimaryKeyTypeService primaryKeyTypeService) {
57  
58  		if (registrations == null) {
59  			throw new IllegalArgumentException("Parameter registrations cannot be null");
60  		}
61  
62  		if (encoderSource == null) {
63  			throw new IllegalArgumentException("Parameter encoderSource cannot be null");
64  		}
65  		
66  		if (primaryKeyTypeService == null) {
67  			throw new IllegalArgumentException("Parameter primaryKeyTypeService cannot be null");
68  		}
69  		
70  		registry = StrategyRegistry.newInstance(ActivationContextEncoder.class, registrations, true);
71  
72  		this.encoderSource = encoderSource;
73  		this.primaryKeyEncoderSource = primaryKeyEncoderSource;
74  		this.primaryKeyTypeService = primaryKeyTypeService;
75  		
76  
77  	}
78  
79  	@SuppressWarnings("unchecked")
80  	public <T> ActivationContextEncoder<T> get(Class<T> clasz) {
81  
82  		ActivationContextEncoder<T> encoder = registry.get(clasz);
83  
84  		if (encoder == null) {
85  			encoder = encoderSource.get(clasz);
86  		}
87  
88  		if (encoder == null) {
89  
90  			encoder = additionalEncoders.get(clasz);
91  			
92  			if (encoder == null) {
93  				
94  				PrimaryKeyEncoder<?, T> primaryKeyEncoder = primaryKeyEncoderSource.get(clasz);
95  				
96  				if (primaryKeyEncoder != null) {
97  					
98  					Class primaryKeyType = primaryKeyTypeService.getPrimaryKeyType(clasz);
99  					encoder = 
100 						new PrimaryKeyEncoderActivationContextEncoder(primaryKeyEncoder, primaryKeyType);
101 					
102 					additionalEncoders.put(clasz, encoder);
103 					
104 				}
105 				
106 			}
107 			
108 		}
109 
110 		if (encoder == null) {
111 			throw new IllegalArgumentException(
112 					"There is no ActivationContextEncoder configured for class " + clasz.getName());
113 		}
114 
115 		return encoder;
116 
117 	}
118 
119 }