1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.r2.common.helper;
17
18 import java.util.ArrayList;
19 import java.util.LinkedHashMap;
20 import java.util.List;
21 import java.util.Map;
22
23
24
25
26
27
28
29
30
31
32
33
34 public final class EntityMergeHelper<E, INFO> {
35
36
37
38
39
40
41
42 public static interface EntityMergeOptions<E, INFO> {
43
44
45
46
47
48
49
50 public String getEntityId(E entity);
51
52
53
54
55
56
57
58 public String getInfoId(INFO info);
59
60
61
62
63
64
65
66
67 public List<Object> merge(E entity, INFO info);
68
69
70
71
72
73
74
75 public E create(INFO info);
76
77 }
78
79
80
81
82
83
84 public static interface StringMergeOptions<E> {
85
86
87
88
89
90
91
92 public String getKey(E entity);
93
94
95
96
97
98
99
100 public E create(String value);
101
102 }
103
104
105
106
107
108
109 public static final class EntityMergeResult<E> {
110
111 private final List<E> mergedList;
112
113 private final List<Object> orphanList;
114
115 public EntityMergeResult(List<E> mergedList, List<Object> orphanList) {
116 super();
117 this.mergedList = mergedList;
118 this.orphanList = orphanList;
119 }
120
121 public List<E> getMergedList() {
122 return mergedList;
123 }
124
125 public List<Object> getOrphanList() {
126 return orphanList;
127 }
128
129 }
130
131
132 public EntityMergeHelper() {
133 }
134
135
136
137
138
139
140
141
142
143 public EntityMergeResult<E> mergeStringList(List<E> entityList,
144 List<String> stringList, StringMergeOptions<E> options) {
145
146
147
148
149
150
151
152 List<E> mergedList = new ArrayList<E>();
153
154 List<Object> orphanDataList = new ArrayList<Object>();
155
156 Map<String, E> existingEntityMap = new LinkedHashMap<String, E>();
157
158 if (entityList != null) {
159 for (E e : entityList) {
160
161 String key = options.getKey(e);
162
163 existingEntityMap.put(key, e);
164
165 }
166 }
167 for (String string : stringList) {
168
169 E entity = existingEntityMap.get(string);
170
171 if (entity == null) {
172
173
174 entity = options.create(string);
175
176 } else {
177
178 existingEntityMap.remove(string);
179 }
180
181 mergedList.add(entity);
182
183 }
184
185 orphanDataList.addAll(existingEntityMap.values());
186
187 return new EntityMergeResult<E>(mergedList, orphanDataList);
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201 public EntityMergeResult<E> merge(List<E> entityList, List<INFO> infoList,
202 EntityMergeOptions<E, INFO> options) {
203
204 List<Object> orphanDataList = new ArrayList<Object>();
205
206 Map<String, E> existingEntityMap = new LinkedHashMap<String, E>();
207
208 if (entityList != null) {
209 for (E e : entityList) {
210
211 String id = options.getEntityId(e);
212
213 existingEntityMap.put(id, e);
214
215 }
216 }
217
218 List<E> mergedList = new ArrayList<E>();
219
220 for (INFO info : infoList) {
221
222 String id = options.getInfoId(info);
223
224 E entity = null;
225 if (existingEntityMap.containsKey(id)) {
226
227 entity = existingEntityMap.remove(id);
228
229 orphanDataList.addAll(options.merge(entity, info));
230
231 } else {
232
233
234 entity = options.create(info);
235 }
236
237 mergedList.add(entity);
238 }
239
240 orphanDataList.addAll(existingEntityMap.values());
241
242 return new EntityMergeResult<E>(mergedList, orphanDataList);
243
244 }
245
246 }