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.Collections;
18  import java.util.Set;
19  
20  import org.apache.tapestry5.PrimaryKeyEncoder;
21  
22  import br.com.arsmachina.module.service.ModuleService;
23  import br.com.arsmachina.tapestrycrud.encoder.ActivationContextEncoder;
24  import br.com.arsmachina.tapestrycrud.encoder.Encoder;
25  import br.com.arsmachina.tapestrycrud.encoder.LabelEncoder;
26  import br.com.arsmachina.tapestrycrud.module.TapestryCrudModule;
27  import br.com.arsmachina.tapestrycrud.services.TapestryCrudModuleService;
28  
29  /**
30   * Default {@link ModuleService} implementation.
31   * 
32   * @author Thiago H. de Paula Figueiredo
33   */
34  public class TapestryCrudModuleServiceImpl implements TapestryCrudModuleService {
35  
36  	final private Set<TapestryCrudModule> modules;
37  
38  	/**
39  	 * Single constructor of this class.
40  	 * 
41  	 * @param entityClasses a {@link Set} of {@link TapestryCrudModule}s. It cannot be null.
42  	 */
43  	public TapestryCrudModuleServiceImpl(Set<TapestryCrudModule> modules) {
44  
45  		if (modules == null) {
46  			throw new IllegalArgumentException("Parameter modules cannot be null");
47  		}
48  
49  		this.modules = Collections.unmodifiableSet(modules);
50  
51  	}
52  
53  	public Set<TapestryCrudModule> getModules() {
54  		return modules;
55  	}
56  
57  	public <T> Class<? extends ActivationContextEncoder<T>> getActivationContextEncoderClass(
58  			Class<T> entityClass) {
59  		
60  		Class<? extends ActivationContextEncoder<T>> encoder = null;
61  		
62  		for (TapestryCrudModule module : modules) {
63  			
64  			encoder = module.getActivationContextEncoderClass(entityClass);
65  			
66  			if (encoder != null) {
67  				break;
68  			}
69  			
70  		}
71  		
72  		return encoder;
73  		
74  	}
75  
76  	public <T> Class<? extends Encoder<T, ?>> getEncoderClass(
77  			Class<T> entityClass) {
78  		
79  		Class<? extends Encoder<T, ?>> encoder = null;
80  		
81  		for (TapestryCrudModule module : modules) {
82  			
83  			encoder = module.getEncoderClass(entityClass);
84  			
85  			if (encoder != null) {
86  				break;
87  			}
88  			
89  		}
90  		
91  		return encoder;
92  		
93  	}
94  
95  	public <T> Class<? extends LabelEncoder<T>> getLabelEncoderClass(Class<T> entityClass) {
96  		
97  		Class<? extends LabelEncoder<T>> encoder = null;
98  		
99  		for (TapestryCrudModule module : modules) {
100 			
101 			encoder = module.getLabelEncoderClass(entityClass);
102 			
103 			if (encoder != null) {
104 				break;
105 			}
106 			
107 		}
108 		
109 		return encoder;
110 
111 		
112 	}
113 
114 	public <T> Class<? extends PrimaryKeyEncoder<?, T>> getPrimaryKeyEncoderClass(
115 			Class<T> entityClass) {
116 
117 		Class<? extends PrimaryKeyEncoder<?, T>> encoder = null;
118 		
119 		for (TapestryCrudModule module : modules) {
120 			
121 			encoder = module.getPrimaryKeyEncoderClass(entityClass);
122 			
123 			if (encoder != null) {
124 				break;
125 			}
126 			
127 		}
128 		
129 		return encoder;
130 
131 	}
132 
133 }