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