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.module;
16  
17  import org.apache.tapestry5.PrimaryKeyEncoder;
18  import org.apache.tapestry5.ioc.services.ClassNameLocator;
19  
20  import br.com.arsmachina.module.AbstractModule;
21  import br.com.arsmachina.tapestrycrud.encoder.ActivationContextEncoder;
22  import br.com.arsmachina.tapestrycrud.encoder.Encoder;
23  import br.com.arsmachina.tapestrycrud.encoder.LabelEncoder;
24  
25  /**
26   * Default {@link TapestryCrudModule} implementation.
27   * 
28   * @author Thiago H. de Paula Figueiredo
29   */
30  public class DefaultTapestryCrudModule extends AbstractModule implements TapestryCrudModule {
31  
32  	/**
33  	 * Single constructor of this class.
34  	 * 
35  	 * @param name a {@link String} containing the module name. It cannot be null.
36  	 * @param rootPackage a {@link String} containing the module parent package. It cannot be null.
37  	 * @param classNameLocator a {@link ClassNameLocator}. It cannot be null.
38  	 * @param daoImplementationSubpackage a {@link String}. It cannot be null.
39  	 */
40  	public DefaultTapestryCrudModule(String name, String rootPackage,
41  			ClassNameLocator classNameLocator, String daoImplementationSubpackage) {
42  		
43  		super(name, rootPackage, classNameLocator);
44  		
45  	}
46  
47  	@SuppressWarnings("unchecked")
48  	public <T> Class<? extends ActivationContextEncoder<T>> getActivationContextEncoderClass(
49  			Class<T> entityClass) {
50  
51  		return getClass(getActivationContextEncoderClassName(entityClass));
52  
53  	}
54  
55  	@SuppressWarnings("unchecked")
56  	public <T> Class<? extends Encoder<T, ?>> getEncoderClass(Class<T> entityClass) {
57  
58  		return getClass(getEncoderClassName(entityClass));
59  
60  	}
61  
62  	@SuppressWarnings("unchecked")
63  	public <T> Class<? extends LabelEncoder<T>> getLabelEncoderClass(Class<T> entityClass) {
64  		return getClass(getLabelEncoderClassName(entityClass));
65  	}
66  
67  	@SuppressWarnings("unchecked")
68  	public <T> Class<? extends PrimaryKeyEncoder<?, T>> getPrimaryKeyEncoderClass(
69  			Class<T> entityClass) {
70  		return getClass(getPrimaryKeyEncoderClassName(entityClass));
71  	}
72  
73  	/**
74  	 * Returns the fully-qualified name of the activation context encoder for a given entity class.
75  	 * 
76  	 * @param clasz a {@link Class}. It cannot be null.
77  	 * @return a {@link String} or null (if no corresponding one is found).
78  	 */
79  	protected String getActivationContextEncoderClassName(Class<?> entityClass) {
80  
81  		return String.format("%s.web.encoder.activationcontext.%sActivationContextEncoder",
82  				getRootPackage(), entityClass.getSimpleName());
83  
84  	}
85  
86  	/**
87  	 * Returns the fully-qualified name of the encoder for a given entity class.
88  	 * 
89  	 * @param clasz a {@link Class}. It cannot be null.
90  	 * @return a {@link String} or null (if no corresponding one is found).
91  	 */
92  	protected String getEncoderClassName(Class<?> entityClass) {
93  
94  		return String.format("%s.web.encoder.%sEncoder", getRootPackage(), entityClass.getSimpleName());
95  
96  	}
97  
98  	/**
99  	 * Returns the fully-qualified name of the label encoder for a given entity class.
100 	 * 
101 	 * @param clasz a {@link Class}. It cannot be null.
102 	 * @return a {@link String} or null (if no corresponding one is found).
103 	 */
104 	protected String getLabelEncoderClassName(Class<?> entityClass) {
105 
106 		return String.format("%s.web.encoder.label.%sLabelEncoder", getRootPackage(),
107 				entityClass.getSimpleName());
108 
109 	}
110 
111 	/**
112 	 * Returns the fully-qualified name of the primary key encoder for a given entity class.
113 	 * 
114 	 * @param clasz a {@link Class}. It cannot be null.
115 	 * @return a {@link String} or null (if no corresponding one is found).
116 	 */
117 	protected String getPrimaryKeyEncoderClassName(Class<?> entityClass) {
118 
119 		return String.format("%s.web.encoder.primarykey.%sPrimaryKeyEncoder", getRootPackage(),
120 				entityClass.getSimpleName());
121 
122 	}
123 
124 	@Override
125 	public String toString() {
126 		return String.format("TapestryCrudModule %s (%s)", getName(), getRootPackage());
127 	}
128 
129 }