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.io.Serializable;
18
19 import org.apache.tapestry5.EventContext;
20 import org.apache.tapestry5.PrimaryKeyEncoder;
21
22 import br.com.arsmachina.tapestrycrud.encoder.ActivationContextEncoder;
23
24 /**
25 * An {@link ActivationContextEncoder} implementation based in a {@link PrimaryKeyEncoder}.
26 *
27 * @author Thiago H. de Paula Figueiredo
28 * @param <T> the entity class related to this DAO.
29 * @param <K> the type of the field that represents the entity class' primary key.
30 */
31 class PrimaryKeyEncoderActivationContextEncoder<T, K extends Serializable> implements
32 ActivationContextEncoder<T> {
33
34 final private Class<K> primaryKeyType;
35
36 final private PrimaryKeyEncoder<K, T> primaryKeyEncoder;
37
38 /**
39 * @param primaryKeyType
40 */
41 @SuppressWarnings("unchecked")
42 public PrimaryKeyEncoderActivationContextEncoder(PrimaryKeyEncoder<K, T> primaryKeyEncoder,
43 Class<K> primaryKeyType) {
44
45 if (primaryKeyType == null) {
46 throw new IllegalArgumentException("Parameter primaryKeyType cannot be null");
47 }
48
49 if (primaryKeyEncoder == null) {
50 throw new IllegalArgumentException("Parameter primaryKeyEncoder cannot be null");
51 }
52
53 this.primaryKeyEncoder = primaryKeyEncoder;
54 this.primaryKeyType = primaryKeyType;
55
56 }
57
58 /**
59 * @see br.com.arsmachina.tapestrycrud.encoder.ActivationContextEncoder#toActivationContext(java.lang.Object)
60 */
61 public Object toActivationContext(T object) {
62 return primaryKeyEncoder.toKey(object);
63 }
64
65 /**
66 * @see br.com.arsmachina.tapestrycrud.encoder.ActivationContextEncoder#toObject(org.apache.tapestry5.EventContext)
67 */
68 public T toObject(EventContext value) {
69 return primaryKeyEncoder.toValue(value.get(primaryKeyType, 0));
70 }
71
72 }