01: package com.technoetic.xplanner.domain;
02:
03: import java.util.*;
04:
05: public class RelationshipMappingRegistry {
06: private static RelationshipMappingRegistry instance;
07:
08: //DEBT(DATADRIVEN) Move the relationship converters to the Domain meta data repository (relationships should become part of the DomainClass)
09: Map dataToDomainRelationshipMappingMap = new HashMap();
10: Map domainToActionMappingMap = new HashMap();
11:
12: public static void initDefaultRegistry() {
13: instance = new RelationshipMappingRegistry();
14: //Relations mapping
15: instance.addMapping(UserStory.class, new RelationshipConvertor(
16: "customerId", "customer"));
17: instance.addMapping(Task.class, new RelationshipConvertor(
18: "storyId", "story"));
19: instance.addMapping(Note.class, new RelationshipConvertor(
20: "attachmentId", "file"));
21: }
22:
23: private void addMapping(Class domainClass, ActionMapping mapping) {
24: Map classMappings = (Map) domainToActionMappingMap
25: .get(domainClass);
26: if (classMappings == null) {
27: classMappings = new LinkedHashMap();
28: domainToActionMappingMap.put(domainClass, classMappings);
29: }
30: classMappings.put(mapping.getName(), mapping);
31: }
32:
33: public void addMapping(Class domainClass,
34: RelationshipConvertor convertor) {
35: HashMap classMappings = (HashMap) dataToDomainRelationshipMappingMap
36: .get(domainClass);
37: if (classMappings == null) {
38: classMappings = new HashMap();
39: dataToDomainRelationshipMappingMap.put(domainClass,
40: classMappings);
41: }
42: classMappings.put(convertor.getAdapterProperty(), convertor);
43: }
44:
45: public RelationshipConvertor getRelationshipMapping(
46: DomainObject domainObject, String propertyName) {
47: Map classMappings = (Map) dataToDomainRelationshipMappingMap
48: .get(domainObject.getClass());
49: if (classMappings == null)
50: return null;
51: return (RelationshipConvertor) classMappings.get(propertyName);
52: }
53:
54: public Collection getRelationshipMappings(DomainObject domainObject) {
55: Map classMappings = (Map) dataToDomainRelationshipMappingMap
56: .get(domainObject.getClass());
57: if (classMappings == null)
58: return Collections.EMPTY_LIST;
59: return classMappings.values();
60: }
61:
62: public ActionMapping getActionMapping(DomainObject domainObject,
63: String action) {
64: Map classMappings = (Map) domainToActionMappingMap
65: .get(domainObject.getClass());
66: if (classMappings == null)
67: return null;
68: return (ActionMapping) classMappings.get(action);
69: }
70:
71: //FIXME Should sort the actions => add an order to the definition
72: public Collection getActionsMappings(DomainObject domainObject) {
73: Map classMappings = (Map) domainToActionMappingMap
74: .get(domainObject.getClass());
75: if (classMappings == null)
76: return Collections.EMPTY_LIST;
77: return classMappings.values();
78: }
79:
80: /**
81: * @deprecated DEBT(SPRING) Should be injected
82: */
83: public static RelationshipMappingRegistry getInstance() {
84: if (instance == null)
85: initDefaultRegistry();
86: return instance;
87: }
88:
89: public static void setInstanceForTest(
90: RelationshipMappingRegistry instance) {
91: RelationshipMappingRegistry.instance = instance;
92: }
93: }
|