| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal.util; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.PrimaryKeyEncoder; |
| 18 | |
import org.apache.tapestry5.ValueEncoder; |
| 19 | |
import org.apache.tapestry5.ioc.services.Coercion; |
| 20 | |
import org.apache.tapestry5.ioc.services.TypeCoercer; |
| 21 | |
import org.apache.tapestry5.util.DefaultPrimaryKeyEncoder; |
| 22 | |
|
| 23 | |
import java.io.Serializable; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
@SuppressWarnings({ "unchecked" }) |
| 31 | 144 | public class PrimaryKeyEncoder2ValueEncoder implements Coercion<PrimaryKeyEncoder, ValueEncoder> |
| 32 | |
{ |
| 33 | |
|
| 34 | |
private final TypeCoercer coercer; |
| 35 | |
|
| 36 | |
public PrimaryKeyEncoder2ValueEncoder(TypeCoercer coercer) |
| 37 | 62 | { |
| 38 | 62 | this.coercer = coercer; |
| 39 | 62 | } |
| 40 | |
|
| 41 | |
public ValueEncoder coerce(final PrimaryKeyEncoder input) |
| 42 | |
{ |
| 43 | 46 | final Class keyType = input.getKeyType(); |
| 44 | |
|
| 45 | 46 | if (keyType == null) |
| 46 | |
{ |
| 47 | 4 | String message = String.format("Unable to extract primary key type from %s. " + |
| 48 | |
"This represents a change from Tapestry 5.0 to Tapestry 5.1.", input); |
| 49 | |
|
| 50 | 4 | if (input instanceof DefaultPrimaryKeyEncoder) |
| 51 | 2 | message += |
| 52 | |
" Class DefaultPrimaryKeyEncoder now includes a constructor for specifying the key type. " + |
| 53 | |
"You should change the code that instantiates the encoder."; |
| 54 | |
else |
| 55 | 2 | message += " You should ensure that the getKeyType() method returns the correct Class."; |
| 56 | |
|
| 57 | 4 | throw new RuntimeException(message); |
| 58 | |
} |
| 59 | |
|
| 60 | 42 | return new ValueEncoder() |
| 61 | |
{ |
| 62 | |
public String toClient(Object value) |
| 63 | |
{ |
| 64 | 78 | Object key = input.toKey(value); |
| 65 | |
|
| 66 | 78 | return coercer.coerce(key, String.class); |
| 67 | |
} |
| 68 | |
|
| 69 | |
public Object toValue(String clientValue) |
| 70 | |
{ |
| 71 | 26 | Serializable key = (Serializable) coercer.coerce(clientValue, keyType); |
| 72 | |
|
| 73 | 26 | return input.toValue(key); |
| 74 | |
} |
| 75 | |
|
| 76 | |
@Override |
| 77 | 42 | public String toString() |
| 78 | |
{ |
| 79 | 0 | return String.format("<ValueEncoder coercion wrapper around PrimaryKeyEncoder[%s]>", |
| 80 | |
keyType.getName()); |
| 81 | |
} |
| 82 | |
}; |
| 83 | |
} |
| 84 | |
} |