3
3
import com .nhl .link .move .LmRuntimeException ;
4
4
import com .nhl .link .move .mapper .Mapper ;
5
5
import com .nhl .link .move .runtime .task .createorupdate .CreateOrUpdateTuple ;
6
- import org .apache .cayenne .CayenneDataObject ;
6
+ import com .nhl .link .move .writer .TargetPropertyWriter ;
7
+ import com .nhl .link .move .writer .TargetPropertyWriterFactory ;
7
8
import org .apache .cayenne .DataObject ;
8
9
import org .apache .cayenne .DataRow ;
9
10
import org .apache .cayenne .ObjectContext ;
10
- import org .apache .cayenne .ObjectId ;
11
- import org .apache .cayenne .PersistenceState ;
12
11
import org .apache .cayenne .access .DataContext ;
12
+ import org .apache .cayenne .map .ObjEntity ;
13
13
import org .slf4j .Logger ;
14
14
import org .slf4j .LoggerFactory ;
15
15
16
16
import java .util .ArrayList ;
17
17
import java .util .HashMap ;
18
18
import java .util .List ;
19
19
import java .util .Map ;
20
- import java .util .Objects ;
21
- import java .util .function .Function ;
22
- import java .util .stream .Collectors ;
23
20
24
21
/**
25
22
* @since 1.3
@@ -28,31 +25,35 @@ public class CreateOrUpdateMerger {
28
25
29
26
private static final Logger LOGGER = LoggerFactory .getLogger (CreateOrUpdateMerger .class );
30
27
28
+ private ObjEntity objEntity ;
31
29
private Mapper mapper ;
30
+ private TargetPropertyWriterFactory <?> writerFactory ;
32
31
33
- public CreateOrUpdateMerger (Mapper mapper ) {
32
+ public CreateOrUpdateMerger (ObjEntity objEntity , Mapper mapper , TargetPropertyWriterFactory <?> writerFactory ) {
33
+ this .objEntity = objEntity ;
34
34
this .mapper = mapper ;
35
+ this .writerFactory = writerFactory ;
35
36
}
36
37
37
- public void merge (List <CreateOrUpdateTuple <DataRow >> mapped ) {
38
- for (CreateOrUpdateTuple <DataRow > t : mapped ) {
38
+ public void merge (List <CreateOrUpdateTuple <DataObject >> mapped ) {
39
+ for (CreateOrUpdateTuple <DataObject > t : mapped ) {
39
40
if (!t .isCreated ()) {
40
41
merge (t .getSource (), t .getTarget ());
41
42
}
42
43
}
43
44
}
44
45
45
- public List <CreateOrUpdateTuple <DataRow >> map (ObjectContext context ,
46
+ public List <CreateOrUpdateTuple <DataObject >> map (ObjectContext context ,
46
47
Map <Object , Map <String , Object >> mappedSources ,
47
48
List <DataRow > matchedTargets ) {
48
49
49
50
// clone mappedSources as we are planning to truncate it in this method
50
51
Map <Object , Map <String , Object >> localMappedSources = new HashMap <>(mappedSources );
51
52
52
- List <CreateOrUpdateTuple <DataRow >> result = new ArrayList <>();
53
+ List <CreateOrUpdateTuple <DataObject >> result = new ArrayList <>();
53
54
54
55
for (DataRow t : matchedTargets ) {
55
- DataObject object = ((DataContext ) context ).objectFromDataRow ("etl11t_temp" , t );
56
+ DataObject object = ((DataContext ) context ).objectFromDataRow (objEntity . getName () , t );
56
57
Object key = mapper .keyForTarget (object );
57
58
58
59
Map <String , Object > src = localMappedSources .remove (key );
@@ -64,61 +65,89 @@ public List<CreateOrUpdateTuple<DataRow>> map(ObjectContext context,
64
65
}
65
66
66
67
// skip phantom updates...
67
- if (willUpdate (src , t )) {
68
- result .add (new CreateOrUpdateTuple <>(src , t , false ));
68
+ if (willUpdate (src , object )) {
69
+ result .add (new CreateOrUpdateTuple <>(src , create (( DataContext ) context , t , false ) , false ));
69
70
}
70
71
}
71
72
72
73
// everything that's left are new objects
73
74
for (Map .Entry <Object , Map <String , Object >> e : localMappedSources .entrySet ()) {
74
75
75
- DataRow t = create ((DataContext ) context , e .getValue ());
76
+ DataObject t = create ((DataContext ) context , e .getValue (), true );
76
77
77
78
result .add (new CreateOrUpdateTuple <>(e .getValue (), t , true ));
78
79
}
79
80
80
81
return result ;
81
82
}
82
83
83
- protected boolean willUpdate (Map <String , Object > source , DataRow target ) {
84
+ protected boolean willUpdate (Map <String , Object > source , DataObject target ) {
84
85
85
86
if (source .isEmpty ()) {
86
87
return false ;
87
88
}
88
89
89
90
for (Map .Entry <String , Object > e : source .entrySet ()) {
90
- // TODO: check for properties that are absent in the target
91
- String attribute = e .getKey ();
92
- if (!target .containsKey (attribute ) || !Objects .equals (e .getValue (), target .get (attribute ))) {
91
+ TargetPropertyWriter writer = writerFactory .getOrCreateWriter (e .getKey ());
92
+ if (writer == null ) {
93
+ LOGGER .info ("Source contains property not mapped in the target: " + e .getKey () + ". Skipping..." );
94
+ continue ;
95
+ }
96
+
97
+ if (writer .willWrite (target , e .getValue ())) {
93
98
return true ;
94
99
}
95
100
}
96
101
97
102
return false ;
98
103
}
99
104
100
- protected DataRow create (DataContext context , Map <String , Object > source ) {
101
- source = source .entrySet ().stream ().collect (Collectors .toMap (e -> e .getKey ().substring (3 ), Map .Entry ::getValue ));
102
- DataRow row = new DataRow (source );
103
- // DataObject object = context.objectFromDataRow("etl11t_temp", row);
105
+ protected DataObject create (DataContext context , Map <String , Object > source , boolean shouldRegister ) {
106
+ // Map<String, Object> target = new HashMap<>();
107
+ // for (Map.Entry<String, Object> e : source.entrySet()) {
108
+ // if (e.getValue() != null) {
109
+ // target.put(e.getKey()/*.substring(3)*/, e.getValue());
110
+ // }
111
+ // }
112
+ DataObject target = (DataObject ) context .newObject (objEntity .getName ());
113
+ // TODO: ID
104
114
// object.setObjectId(new ObjectId("etl11t_temp", "id", source.get("id")));
105
- // context.registerNewObject(object);
106
- DataObject object = (DataObject ) context .newObject ("etl11t_temp" );
107
- object .setObjectId (new ObjectId ("etl11t_temp" , "id" , source .get ("id" )));
108
- context .registerNewObject (object );
109
- // object.getObjectId().getIdSnapshot().clear();
110
- // object.getObjectId().getIdSnapshot().put("id", source.get("id"));
111
- source .forEach ((k , v ) -> {
112
- if (!k .equals ("id" )) {
113
- object .writePropertyDirectly (k , v );
115
+ if (shouldRegister ) {
116
+ context .registerNewObject (target );
117
+ }
118
+ // target.forEach((k, v) -> {
119
+ // if (!k.equals("id")) {
120
+ // object.writePropertyDirectly(k, v);
121
+ // }
122
+ // });
123
+
124
+ for (Map .Entry <String , Object > e : source .entrySet ()) {
125
+ TargetPropertyWriter writer = writerFactory .getOrCreateWriter (e .getKey ());
126
+ if (writer == null ) {
127
+ LOGGER .info ("Source contains property not mapped in the target: " + e .getKey () + ". Skipping..." );
128
+ continue ;
129
+ }
130
+ if (writer .willWrite (target , e .getValue ())) {
131
+ writer .write (target , e .getValue ());
114
132
}
115
- });
133
+ }
116
134
117
- return row ;
135
+ return target ;
118
136
}
119
137
120
- private void merge (Map <String , Object > source , DataRow target ) {
121
- // TODO: check for properties that are absent in the target
122
- target .putAll (source );
138
+ private void merge (Map <String , Object > source , DataObject target ) {
139
+
140
+ if (source .isEmpty ()) {
141
+ return ;
142
+ }
143
+
144
+ for (Map .Entry <String , Object > e : source .entrySet ()) {
145
+ TargetPropertyWriter writer = writerFactory .getOrCreateWriter (e .getKey ());
146
+ if (writer == null ) {
147
+ LOGGER .info ("Source contains property not mapped in the target: " + e .getKey () + ". Skipping..." );
148
+ continue ;
149
+ }
150
+ writer .write (target , e .getValue ());
151
+ }
123
152
}
124
153
}
0 commit comments