Mercurial > hg > ismi-richfaces
annotate src/main/java/de/mpiwg/itgroup/ismi/merge/ImportMerge.java @ 138:15c26735fff7
fix some NPEs with ImportMerge.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Fri, 16 Jun 2017 15:44:30 +0200 |
parents | 3c585df9f874 |
children |
rev | line source |
---|---|
96 | 1 package de.mpiwg.itgroup.ismi.merge; |
2 | |
3 import java.io.File; | |
4 import java.io.FileInputStream; | |
5 import java.io.InputStreamReader; | |
6 import java.io.Reader; | |
7 import java.io.Serializable; | |
8 import java.util.ArrayList; | |
9 import java.util.HashMap; | |
10 import java.util.List; | |
11 import java.util.Map; | |
12 | |
13 import javax.faces.event.ActionEvent; | |
14 import javax.faces.model.SelectItem; | |
15 | |
16 import org.apache.commons.lang.StringUtils; | |
17 import org.apache.log4j.Logger; | |
18 import org.mpi.openmind.repository.bo.Attribute; | |
19 import org.mpi.openmind.repository.bo.Entity; | |
20 import org.mpi.openmind.repository.bo.Node; | |
21 import org.mpi.openmind.repository.bo.Relation; | |
22 import org.mpi.openmind.repository.utils.CsvNodeListReader; | |
98 | 23 import org.richfaces.event.FileUploadEvent; |
24 import org.richfaces.model.UploadedFile; | |
96 | 25 |
26 import de.mpiwg.itgroup.ismi.entry.beans.AbstractISMIBean; | |
27 | |
28 public class ImportMerge extends AbstractISMIBean implements Serializable { | |
29 | |
30 private static Logger logger = Logger.getLogger(ImportMerge.class); | |
31 | |
32 private static final long serialVersionUID = 1L; | |
33 | |
34 private String importFilename = "/var/tmp/ismi-import.csv"; | |
35 private String importFileMsg = null; | |
36 | |
37 private List<Node> importNodeList; | |
38 private int importNodeIdx; | |
39 private int importNodeListLength = 0; | |
40 private Node importNode; | |
41 private String importNodeMsg; | |
42 | |
43 public static String FIRST_VALUE = "old value"; | |
44 public static String SECOND_VALUE = "new value"; | |
45 public static String IGNORE = "ignore"; | |
46 public static String TAKE = "take"; | |
47 | |
48 private boolean showAttributeMapping = false; | |
49 private boolean showSrcRelationMapping = false; | |
50 private boolean showTarRelationMapping = false; | |
51 | |
52 private boolean entitiesLoaded = false; | |
53 | |
54 private Map<String, String> firstAttMap = new HashMap<String, String>(); | |
55 private Map<String, String> secondAttMap = new HashMap<String, String>(); | |
56 | |
57 private Entity firstEntity; | |
58 private Entity secondEntity; | |
59 | |
60 private String firstId; | |
61 private String secondId; | |
62 | |
63 private Entity entResult; | |
64 private List<Attribute> resultAtts; | |
65 private List<Relation> resultSrcRels; | |
66 private List<Relation> resultTarRels; | |
67 | |
68 private List<String> attLabels; | |
69 private Map<String, String> selectedAtts; | |
70 | |
71 Map<Long, String> selectedFirstSrcRelations; | |
72 Map<Long, String> selectedSecondSrcRelations; | |
73 Map<Long, String> selectedFirstTarRelations; | |
74 Map<Long, String> selectedSecondTarRelations; | |
75 | |
76 | |
77 public ImportMerge() { | |
78 } | |
79 | |
98 | 80 public void uploadListener(FileUploadEvent event) throws Exception { |
81 UploadedFile item = event.getUploadedFile(); | |
82 importFilename = item.getName(); | |
83 reset(); | |
84 try { | |
85 Reader importReader = new InputStreamReader(item.getInputStream(), "UTF-8"); | |
86 importNodeList = CsvNodeListReader.readCsv(importReader); | |
87 | |
88 this.importNodeListLength = importNodeList.size(); | |
89 this.importFileMsg = "File "+ importFilename + " contains " + importNodeListLength + " Nodes"; | |
90 this.importNodeIdx = 0; | |
91 loadImportNode(); | |
92 | |
93 } catch (Exception e) { | |
94 addErrorMsg("The import file could no be loaded."); | |
95 addErrorMsg("Error: "+e); | |
96 } | |
97 } | |
98 | |
99 | |
97 | 100 /** |
101 * Action that loads the import file and parses it into the Nodes list. | |
102 * | |
103 * @param event | |
104 */ | |
96 | 105 public void loadImportFile(ActionEvent event) { |
106 reset(); | |
107 try { | |
108 File importFile = new File(this.importFilename); | |
109 if (!importFile.canRead()) { | |
110 this.importFileMsg = "File missing!"; | |
111 return; | |
112 } | |
113 Reader importReader = new InputStreamReader(new FileInputStream(importFile), "UTF-8"); | |
114 importNodeList = CsvNodeListReader.readCsv(importReader); | |
115 | |
116 this.importNodeListLength = importNodeList.size(); | |
117 this.importFileMsg = importNodeListLength + " Nodes"; | |
118 this.importNodeIdx = 0; | |
97 | 119 loadImportNode(); |
96 | 120 |
121 } catch (Exception e) { | |
122 addErrorMsg("The import file could no be loaded."); | |
123 addErrorMsg("Error: "+e); | |
124 } | |
125 } | |
126 | |
97 | 127 /** |
128 * Load the current Node from the import list. | |
129 * | |
130 * @param event | |
131 */ | |
132 public void loadImportNode() { | |
96 | 133 reset(); |
134 try { | |
135 importNode = importNodeList.get(importNodeIdx); | |
136 importNodeMsg = null; | |
137 firstEntity = null; | |
138 secondEntity = null; | |
139 | |
140 if (importNode.getNodeType().equals("ATTRIBUTE")) { | |
97 | 141 /* |
142 * Attribute | |
143 */ | |
96 | 144 Attribute att = (Attribute) importNode; |
145 String attName = att.getName(); | |
146 if (attName == null) { | |
147 importNodeMsg = "Attribute has no name!"; | |
148 return; | |
149 } | |
150 Long source_id = att.getSourceId(); | |
151 if (source_id == null) { | |
152 importNodeMsg = "Attribute has no source_id to attach to!"; | |
153 return; | |
154 } else { | |
155 Entity systemEnt = getWrapper().getEntityById(source_id); | |
156 if (systemEnt != null) { | |
157 if (systemEnt.isLightweight()) { | |
158 systemEnt = getWrapper().getEntityContent(systemEnt); | |
159 } | |
160 Attribute systemAtt = systemEnt.getAttributeByName(attName); | |
161 if ((systemAtt != null) && (systemAtt.getOwnValue().equals(att.getOwnValue()))) { | |
162 importNodeMsg = "Same attribute exists already!"; | |
163 return; | |
164 } | |
165 // create new Entity with this Attribute | |
166 Entity newEnt = (Entity) systemEnt.clone(); | |
167 newEnt.addAttribute(att); | |
168 this.secondEntity = newEnt; | |
169 // compare with old version | |
170 this.firstEntity = systemEnt; | |
171 } else { | |
172 importNodeMsg = "Entity for Attribute does not exist!"; | |
173 return; | |
174 } | |
175 } | |
176 } else if (importNode.getNodeType().equals("ENTITY")) { | |
97 | 177 /* |
178 * Entity | |
179 */ | |
96 | 180 Entity ent = (Entity) importNode; |
181 Long id = ent.getId(); | |
182 if (id == null) { | |
183 // new Entity | |
184 this.secondEntity = ent; | |
185 // what to use to compare? | |
186 this.firstEntity = new Entity(); | |
97 | 187 this.firstEntity.setLightweight(false); |
96 | 188 |
189 } else { | |
190 // entity exists | |
191 Entity systemEnt = getWrapper().getEntityById(id); | |
192 if (systemEnt != null) { | |
193 if (systemEnt.isLightweight()) { | |
194 systemEnt = getWrapper().getEntityContent(systemEnt); | |
195 } | |
196 importNodeMsg = "Entity exists (but may be different)."; | |
197 // TODO: does it make sense to check for equality? | |
198 this.secondEntity = ent; | |
199 // compare with old version | |
200 this.firstEntity = systemEnt; | |
201 } else { | |
97 | 202 importNodeMsg = "Entity does not exist!"; |
96 | 203 // TODO: try to undelete? |
204 this.firstEntity = new Entity(); | |
97 | 205 this.firstEntity.setLightweight(false); |
96 | 206 this.secondEntity = ent; |
207 } | |
208 } | |
209 } else if (importNode.getNodeType().equals("RELATION")) { | |
97 | 210 /* |
211 * Relation | |
212 */ | |
96 | 213 Relation rel = (Relation) importNode; |
214 String relName = rel.getObjectClass(); | |
215 if (relName == null) { | |
216 importNodeMsg = "Relation has no name!"; | |
217 return; | |
218 } | |
219 Long source_id = rel.getSourceId(); | |
220 Long target_id = rel.getTargetId(); | |
221 if (source_id == null) { | |
134
25bfcc9d757c
effort to re-use more relations when saving entities.
casties
parents:
124
diff
changeset
|
222 // no source id |
96 | 223 importNodeMsg = "Relation has no source_id!"; |
224 return; | |
225 } else if (target_id == null) { | |
134
25bfcc9d757c
effort to re-use more relations when saving entities.
casties
parents:
124
diff
changeset
|
226 // no target id |
96 | 227 importNodeMsg = "Relation has no target_id!"; |
228 return; | |
229 } else { | |
134
25bfcc9d757c
effort to re-use more relations when saving entities.
casties
parents:
124
diff
changeset
|
230 // get source and target Entities |
96 | 231 Entity sourceEnt = getWrapper().getEntityById(source_id); |
232 Entity targetEnt = getWrapper().getEntityById(target_id); | |
233 if (sourceEnt == null) { | |
234 importNodeMsg = "Relation source does not exist!"; | |
235 } else if (targetEnt == null) { | |
236 importNodeMsg = "Relation target does not exist!"; | |
237 } else { | |
238 if (sourceEnt.isLightweight()) { | |
239 sourceEnt = getWrapper().getEntityContent(sourceEnt); | |
240 } | |
134
25bfcc9d757c
effort to re-use more relations when saving entities.
casties
parents:
124
diff
changeset
|
241 // get existing Relation |
96 | 242 Relation systemRel = sourceEnt.getSourceRelation(relName, target_id); |
243 if ((systemRel != null) && (systemRel.getObjectClass().equals(rel.getObjectClass()))) { | |
244 // TODO: also check Relation attributes | |
245 importNodeMsg = "Same Relation exists already!"; | |
246 return; | |
247 } | |
248 // create new Entity with this Relation | |
97 | 249 Entity newEnt = new Entity(); |
250 newEnt.setLightweight(false); | |
96 | 251 newEnt.addSourceRelation(rel); |
252 this.secondEntity = newEnt; | |
253 // compare with old version | |
254 this.firstEntity = sourceEnt; | |
255 } | |
256 } | |
257 } | |
258 } catch (Exception e) { | |
259 addErrorMsg("The import Node could no be loaded."); | |
97 | 260 addErrorMsg("Error: "+e); |
96 | 261 } |
262 } | |
263 | |
264 public void skipCurrentNode(ActionEvent event) { | |
265 importNodeIdx += 1; | |
266 if (importNodeIdx >= importNodeList.size()) { | |
267 importNodeIdx = importNodeList.size() - 1; | |
268 } | |
269 if (importNodeIdx < 0) { | |
270 importNodeIdx = 0; | |
271 } | |
97 | 272 loadImportNode(); |
96 | 273 } |
274 | |
137
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
275 public void skipToPrevNode(ActionEvent event) { |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
276 importNodeIdx -= 1; |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
277 if (importNodeIdx >= importNodeList.size()) { |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
278 importNodeIdx = importNodeList.size() - 1; |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
279 } |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
280 if (importNodeIdx < 0) { |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
281 importNodeIdx = 0; |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
282 } |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
283 loadImportNode(); |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
284 } |
3c585df9f874
improved merge import dialog. now with prev+next buttons.
casties
parents:
134
diff
changeset
|
285 |
96 | 286 public void loadFirstEntity(ActionEvent event) { |
287 reset(); | |
288 try { | |
289 if (firstEntity != null && secondEntity != null) { | |
97 | 290 // create list of differences |
96 | 291 deployDifferences(); |
292 } else { | |
293 if (importNodeMsg != null) { | |
294 addErrorMsg(importNodeMsg); | |
295 } | |
296 } | |
297 } catch (Exception e) { | |
298 addErrorMsg("The first entity could no be loaded."); | |
299 } | |
300 } | |
301 | |
302 @Override | |
303 public void reset() { | |
304 this.attLabels = new ArrayList<String>(); | |
305 this.selectedAtts = new HashMap<String, String>(); | |
306 this.selectedFirstSrcRelations = new HashMap<Long, String>(); | |
307 this.selectedSecondSrcRelations = new HashMap<Long, String>(); | |
308 this.selectedFirstTarRelations = new HashMap<Long, String>(); | |
309 this.selectedSecondTarRelations = new HashMap<Long, String>(); | |
310 this.entResult = null; | |
311 | |
312 this.entitiesLoaded = false; | |
313 this.showAttributeMapping = false; | |
314 this.showSrcRelationMapping = false; | |
315 this.showTarRelationMapping = false; | |
316 } | |
317 | |
97 | 318 /** |
319 * Create list of differences in attributes and relations for display. | |
320 */ | |
96 | 321 private void deployDifferences() { |
322 this.showAttributeMapping = true; | |
323 this.showSrcRelationMapping = true; | |
324 this.showTarRelationMapping = true; | |
325 this.entitiesLoaded = true; | |
326 if (this.firstEntity != null && this.secondEntity != null) { | |
327 /* | |
328 if (firstEntity.isLightweight()) { | |
329 this.firstEntity = getWrapper().getEntityContent(this.firstEntity); | |
330 } | |
331 if (secondEntity.isLightweight()) { | |
332 this.secondEntity = getWrapper().getEntityContent(this.secondEntity); | |
333 } | |
334 | |
335 this.firstEntity = (Entity) firstEntity.clone(); | |
336 this.secondEntity = (Entity) secondEntity.clone(); | |
337 */ | |
338 | |
97 | 339 /* |
340 * attributes | |
341 */ | |
96 | 342 this.attLabels = new ArrayList<String>(); |
343 this.selectedAtts = new HashMap<String, String>(); | |
344 this.firstAttMap = new HashMap<String, String>(); | |
345 this.secondAttMap = new HashMap<String, String>(); | |
346 | |
347 for (Attribute att : this.firstEntity.getAttributes()) { | |
348 firstAttMap.put(att.getName(), att.getValue()); | |
349 if (!attLabels.contains(att.getName())) { | |
350 attLabels.add(att.getName()); | |
138
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
351 selectedAtts.put(att.getName(), SECOND_VALUE); |
96 | 352 } |
353 } | |
354 | |
355 for (Attribute att : this.secondEntity.getAttributes()) { | |
356 secondAttMap.put(att.getName(), att.getValue()); | |
357 if (!attLabels.contains(att.getName())) { | |
358 attLabels.add(att.getName()); | |
138
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
359 selectedAtts.put(att.getName(), SECOND_VALUE); |
96 | 360 } |
361 } | |
362 | |
97 | 363 /* |
364 * source relations | |
365 */ | |
96 | 366 this.selectedFirstSrcRelations = new HashMap<Long, String>(); |
367 this.selectedSecondSrcRelations = new HashMap<Long, String>(); | |
368 | |
369 for (Relation rel : this.firstEntity.getSourceRelations()) { | |
370 rel.setTarget(getWrapper().getEntityById(rel.getTargetId())); | |
371 selectedFirstSrcRelations.put(rel.getId(), TAKE); | |
372 } | |
373 | |
374 for (Relation rel : this.secondEntity.getSourceRelations()) { | |
138
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
375 rel.setTarget(getWrapper().getEntityById(rel.getTargetId())); |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
376 if (rel.getTarget() != null) { |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
377 selectedSecondSrcRelations.put(rel.getId(), TAKE); |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
378 } else { |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
379 logger.error("source relation to merge has nonexistent target: "+rel); |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
380 selectedSecondSrcRelations.put(rel.getId(), IGNORE); |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
381 } |
96 | 382 } |
383 | |
97 | 384 /* |
385 * target relations | |
386 */ | |
96 | 387 this.selectedFirstTarRelations = new HashMap<Long, String>(); |
388 this.selectedSecondTarRelations = new HashMap<Long, String>(); | |
389 | |
390 for (Relation rel : this.firstEntity.getTargetRelations()) { | |
391 rel.setSource(getWrapper().getEntityById(rel.getSourceId())); | |
392 selectedFirstTarRelations.put(rel.getId(), TAKE); | |
393 } | |
394 | |
395 for (Relation rel : this.secondEntity.getTargetRelations()) { | |
138
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
396 rel.setSource(getWrapper().getEntityById(rel.getSourceId())); |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
397 if (rel.getSource() != null) { |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
398 selectedSecondTarRelations.put(rel.getId(), TAKE); |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
399 } else { |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
400 logger.error("target relation to merge has nonexistent source: " + rel); |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
401 selectedSecondTarRelations.put(rel.getId(), IGNORE); |
15c26735fff7
fix some NPEs with ImportMerge.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
137
diff
changeset
|
402 } |
96 | 403 } |
404 | |
405 } | |
406 } | |
407 | |
97 | 408 /** |
409 * Preview merged Entity as text. | |
410 * | |
411 * @param event | |
412 */ | |
96 | 413 public void preview(ActionEvent event) { |
414 this.generateResultEntity(); | |
415 } | |
416 | |
97 | 417 /** |
418 * Execute Merge action from UI. | |
419 */ | |
420 public void listenerExecuteMerge() { | |
421 this.executeMerge(); | |
422 getAppBean().getSimpleSearchCache().setMapDirty(true); | |
423 } | |
424 | |
425 /** | |
426 * Merge Entities. | |
427 * | |
428 * firstEntity is current one. secondEntity is imported. | |
429 */ | |
96 | 430 private void executeMerge() { |
431 | |
97 | 432 logger.info("Starting merge import " + firstEntity.getObjectClass() + " [" + getUserName() + "]" |
96 | 433 + "[firstEntity=" + firstEntity.getId() + ", secondEntity=" + secondEntity.getId() + "]"); |
434 | |
435 try { | |
436 this.generateResultEntity(); | |
437 if (this.entResult != null) { | |
97 | 438 // print to log |
96 | 439 this.printMergeInfo(entResult); |
97 | 440 // save the new Entity |
124
28a0c2726466
changed saveEntity() to use new EditIntent. Set up EditIntent for TEXT, PERSON, REFERENCE.
casties
parents:
108
diff
changeset
|
441 this.getWrapper().saveEntity(this.entResult, getSessionUser().getEmail() + "_merge", null); |
97 | 442 // update generated ownvalues |
96 | 443 this.updateRelatedOW(this.entResult, getSessionUser().getEmail() + "_merge"); |
444 | |
97 | 445 logger.info("Merge import successful " + firstEntity.getObjectClass() + " [" + getUserName() + "]" |
96 | 446 + "[firstEntity=" + firstEntity.getId() + ", secondEntity=" + secondEntity.getId() |
447 + ", generatedEntity=" + entResult.getId() + "]"); | |
448 | |
449 this.firstEntity = null; | |
450 this.secondEntity = null; | |
451 | |
97 | 452 addGeneralMsg("The imported entity was merged successfully"); |
96 | 453 addGeneralMsg("The new entity has the id " + this.entResult.getId()); |
454 this.reset(); | |
455 } | |
456 } catch (Exception e) { | |
457 printInternalError(e); | |
458 logger.error("[" + getUserName() + "] " + e.getMessage(), e); | |
459 } | |
460 } | |
461 | |
462 private void printMergeInfo(Entity ent) { | |
463 StringBuilder sb = new StringBuilder("\n\n"); | |
464 | |
465 sb.append("-----------------------------------------\n"); | |
466 sb.append("Merging result [" + getUserName() + "]\n"); | |
467 sb.append(ent.toString() + "\n"); | |
468 sb.append("Attributes:\n"); | |
469 for (Attribute att : ent.getAttributes()) { | |
470 sb.append("\t" + att.toString() + "\n"); | |
471 } | |
472 | |
473 sb.append("Src Relations:\n"); | |
474 for (Relation src : ent.getSourceRelations()) { | |
475 sb.append("\t" + src.toString() + "\n"); | |
476 } | |
477 | |
478 sb.append("Tar Relations:\n"); | |
479 for (Relation tar : ent.getTargetRelations()) { | |
480 sb.append("\t" + tar.toString() + "\n"); | |
481 } | |
482 | |
483 sb.append("-----------------------------------------\n"); | |
484 logger.info(sb.toString()); | |
485 } | |
486 | |
97 | 487 /** |
488 * Generate new Entity taking selected Attributes and Relations from both Entities. | |
489 * | |
490 * Uses id from firstEntity, creating a new version of this entity. | |
491 */ | |
492 private void generateResultEntity() { | |
493 // create new Entity | |
494 this.entResult = new Entity(); | |
495 this.entResult.setLightweight(false); | |
496 // use id from firstEntity | |
497 Long newId = this.firstEntity.getId(); | |
498 if (newId == null) { | |
499 // Entity is new -- try imported id | |
500 newId = secondEntity.getId(); | |
501 } | |
502 this.entResult.setId(newId); | |
503 // use object_class from firstEntity | |
504 String oc = this.firstEntity.getObjectClass(); | |
505 if (oc == null) { | |
506 // Entity is new -- try imported object_class | |
507 oc = secondEntity.getObjectClass(); | |
508 } | |
509 this.entResult.setObjectClass(oc); | |
510 | |
511 /* | |
512 * generating attributes | |
513 */ | |
514 try { | |
515 for (String attName : this.selectedAtts.keySet()) { | |
516 String selected = this.selectedAtts.get(attName); | |
517 String value = ""; | |
518 if (selected.equals(FIRST_VALUE)) { | |
519 value = (firstEntity.getAttributeByName(attName) == null) ? "" | |
520 : firstEntity.getAttributeByName(attName).getOwnValue(); | |
521 } else if (selected.equals(SECOND_VALUE)) { | |
522 value = (secondEntity.getAttributeByName(attName) == null) ? "" | |
523 : secondEntity.getAttributeByName(attName).getOwnValue(); | |
524 } | |
525 this.entResult.addAttribute(new Attribute(attName, "text", value)); | |
526 } | |
527 } catch (Exception e) { | |
528 logger.error(e); | |
529 addErrorMsg("Please inform support of this exception: " + e.getMessage()); | |
530 } | |
531 | |
532 /* | |
533 * generating source relations | |
534 */ | |
535 for (Relation rel : firstEntity.getSourceRelations()) { | |
536 String selectedValue = this.selectedFirstSrcRelations.get(rel.getId()); | |
537 if (StringUtils.isNotEmpty(selectedValue) && selectedValue.equals(TAKE)) { | |
538 if (!this.entResult.containsSourceRelation(rel.getOwnValue(), rel.getTargetId())) { | |
539 this.entResult.addSourceRelation(generateSrcRelation(rel)); | |
540 } | |
541 } | |
542 } | |
543 | |
544 for (Relation rel : secondEntity.getSourceRelations()) { | |
545 String selectedValue = this.selectedSecondSrcRelations.get(rel.getId()); | |
546 if (StringUtils.isNotEmpty(selectedValue) && selectedValue.equals(TAKE)) { | |
547 if (!this.entResult.containsSourceRelation(rel.getOwnValue(), rel.getTargetId())) { | |
548 this.entResult.addSourceRelation(generateSrcRelation(rel)); | |
549 } | |
550 } | |
551 } | |
552 | |
553 /* | |
554 * generating target relations | |
555 */ | |
556 for (Relation rel : firstEntity.getTargetRelations()) { | |
557 String selectedValue = this.selectedFirstTarRelations.get(rel.getId()); | |
558 if (StringUtils.isNotEmpty(selectedValue) && selectedValue.equals(TAKE)) { | |
559 // ensuring that there is no two equals relations. | |
560 if (!this.entResult.containsTargetRelation(rel.getOwnValue(), rel.getSourceId())) { | |
561 this.entResult.addTargetRelation(generateTarRelation(rel)); | |
562 } | |
563 } | |
564 } | |
565 | |
566 for (Relation rel : secondEntity.getTargetRelations()) { | |
567 String selectedValue = this.selectedSecondTarRelations.get(rel.getId()); | |
568 if (StringUtils.isNotEmpty(selectedValue) && selectedValue.equals(TAKE)) { | |
569 if (!this.entResult.containsTargetRelation(rel.getOwnValue(), rel.getSourceId())) { | |
570 this.entResult.addTargetRelation(generateTarRelation(rel)); | |
571 } | |
572 } | |
573 } | |
574 } | |
575 | |
576 private Relation generateSrcRelation(Relation rel) { | |
577 Relation newRel = new Relation(); | |
578 newRel.setOwnValue(rel.getOwnValue()); | |
579 newRel.setTarget(getWrapper().getEntityById(rel.getTargetId())); | |
580 return newRel; | |
581 } | |
582 | |
583 private Relation generateTarRelation(Relation rel) { | |
584 Relation newRel = new Relation(); | |
585 newRel.setOwnValue(rel.getOwnValue()); | |
586 newRel.setSource(getWrapper().getEntityById(rel.getSourceId())); | |
587 return newRel; | |
588 } | |
589 | |
590 public List<SelectItem> getAttSelectItems() { | |
591 List<SelectItem> items = new ArrayList<SelectItem>(); | |
592 items.add(new SelectItem(FIRST_VALUE)); | |
593 items.add(new SelectItem(SECOND_VALUE)); | |
594 items.add(new SelectItem(IGNORE)); | |
595 return items; | |
596 } | |
597 | |
598 public List<SelectItem> getRelSelectItems() { | |
599 List<SelectItem> items = new ArrayList<SelectItem>(); | |
600 items.add(new SelectItem(TAKE)); | |
601 items.add(new SelectItem(IGNORE)); | |
602 return items; | |
603 } | |
604 | |
96 | 605 public void actionShowTarRelationMapping(ActionEvent event) { |
606 this.showTarRelationMapping = true; | |
607 } | |
608 | |
609 public void actionHideTarRelationMapping(ActionEvent event) { | |
610 this.showTarRelationMapping = false; | |
611 } | |
612 | |
613 public void actionShowSrcRelationMapping(ActionEvent event) { | |
614 this.showSrcRelationMapping = true; | |
615 } | |
616 | |
617 public void actionHideSrcRelationMapping(ActionEvent event) { | |
618 this.showSrcRelationMapping = false; | |
619 } | |
620 | |
621 public void actionShowAttributeMapping(ActionEvent event) { | |
622 this.showAttributeMapping = true; | |
623 } | |
624 | |
625 public void actionHideAttributeMapping(ActionEvent event) { | |
626 this.showAttributeMapping = false; | |
627 } | |
628 | |
629 public Entity getEntResult() { | |
630 return entResult; | |
631 } | |
632 | |
633 public void setEntResult(Entity entResult) { | |
634 this.entResult = entResult; | |
635 } | |
636 | |
637 public List<Attribute> getResultAtts() { | |
638 return resultAtts; | |
639 } | |
640 | |
641 public void setResultAtts(List<Attribute> resultAtts) { | |
642 this.resultAtts = resultAtts; | |
643 } | |
644 | |
645 public List<Relation> getResultSrcRels() { | |
646 return resultSrcRels; | |
647 } | |
648 | |
649 public void setResultSrcRels(List<Relation> resultSrcRels) { | |
650 this.resultSrcRels = resultSrcRels; | |
651 } | |
652 | |
653 public List<Relation> getResultTarRels() { | |
654 return resultTarRels; | |
655 } | |
656 | |
657 public void setResultTarRels(List<Relation> resultTarRels) { | |
658 this.resultTarRels = resultTarRels; | |
659 } | |
660 | |
661 public Entity getFirstEntity() { | |
662 return firstEntity; | |
663 } | |
664 | |
665 public void setFirstEntity(Entity firstEntity) { | |
666 this.firstEntity = firstEntity; | |
667 } | |
668 | |
669 public Entity getSecondEntity() { | |
670 return secondEntity; | |
671 } | |
672 | |
673 public void setSecondEntity(Entity secondEntity) { | |
674 this.secondEntity = secondEntity; | |
675 } | |
676 | |
677 public String getFirstId() { | |
678 return firstId; | |
679 } | |
680 | |
681 public void setFirstId(String firstId) { | |
682 this.firstId = firstId; | |
683 } | |
684 | |
685 public String getSecondId() { | |
686 return secondId; | |
687 } | |
688 | |
689 public void setSecondId(String secondId) { | |
690 this.secondId = secondId; | |
691 } | |
692 | |
693 public List<String> getAttLabels() { | |
694 return attLabels; | |
695 } | |
696 | |
697 public void setAttLabels(List<String> attLabels) { | |
698 this.attLabels = attLabels; | |
699 } | |
700 | |
701 public Map<String, String> getFirstAttMap() { | |
702 return firstAttMap; | |
703 } | |
704 | |
705 public void setFirstAttMap(Map<String, String> firstAttMap) { | |
706 this.firstAttMap = firstAttMap; | |
707 } | |
708 | |
709 public Map<String, String> getSecondAttMap() { | |
710 return secondAttMap; | |
711 } | |
712 | |
713 public void setSecondAttMap(Map<String, String> secondAttMap) { | |
714 this.secondAttMap = secondAttMap; | |
715 } | |
716 | |
717 public Map<String, String> getSelectedAtts() { | |
718 return selectedAtts; | |
719 } | |
720 | |
721 public void setSelectedAtts(Map<String, String> selectedAtts) { | |
722 this.selectedAtts = selectedAtts; | |
723 } | |
724 | |
725 public boolean isShowAttributeMapping() { | |
726 return showAttributeMapping; | |
727 } | |
728 | |
729 public void setShowAttributeMapping(boolean showAttributeMapping) { | |
730 this.showAttributeMapping = showAttributeMapping; | |
731 } | |
732 | |
733 public boolean isEntitiesLoaded() { | |
734 return entitiesLoaded; | |
735 } | |
736 | |
737 public void setEntitiesLoaded(boolean entitiesLoaded) { | |
738 this.entitiesLoaded = entitiesLoaded; | |
739 } | |
740 | |
741 public Map<Long, String> getSelectedFirstSrcRelations() { | |
742 return selectedFirstSrcRelations; | |
743 } | |
744 | |
745 public void setSelectedFirstSrcRelations(Map<Long, String> selectedFirstSrcRelations) { | |
746 this.selectedFirstSrcRelations = selectedFirstSrcRelations; | |
747 } | |
748 | |
749 public Map<Long, String> getSelectedSecondSrcRelations() { | |
750 return selectedSecondSrcRelations; | |
751 } | |
752 | |
753 public void setSelectedSecondSrcRelations(Map<Long, String> selectedSecondSrcRelations) { | |
754 this.selectedSecondSrcRelations = selectedSecondSrcRelations; | |
755 } | |
756 | |
757 public boolean isShowSrcRelationMapping() { | |
758 return showSrcRelationMapping; | |
759 } | |
760 | |
761 public void setShowSrcRelationMapping(boolean showSrcRelationMapping) { | |
762 this.showSrcRelationMapping = showSrcRelationMapping; | |
763 } | |
764 | |
765 public boolean isShowTarRelationMapping() { | |
766 return showTarRelationMapping; | |
767 } | |
768 | |
769 public void setShowTarRelationMapping(boolean showTarRelationMapping) { | |
770 this.showTarRelationMapping = showTarRelationMapping; | |
771 } | |
772 | |
773 public Map<Long, String> getSelectedFirstTarRelations() { | |
774 return selectedFirstTarRelations; | |
775 } | |
776 | |
777 public void setSelectedFirstTarRelations(Map<Long, String> selectedFirstTarRelations) { | |
778 this.selectedFirstTarRelations = selectedFirstTarRelations; | |
779 } | |
780 | |
781 public Map<Long, String> getSelectedSecondTarRelations() { | |
782 return selectedSecondTarRelations; | |
783 } | |
784 | |
785 public void setSelectedSecondTarRelations(Map<Long, String> selectedSecondTarRelations) { | |
786 this.selectedSecondTarRelations = selectedSecondTarRelations; | |
787 } | |
788 | |
789 /** | |
790 * @return the importFilename | |
791 */ | |
792 public String getImportFilename() { | |
793 return importFilename; | |
794 } | |
795 | |
796 /** | |
797 * @param importFilename | |
798 * the importFilename to set | |
799 */ | |
800 public void setImportFilename(String importFilename) { | |
801 this.importFilename = importFilename; | |
802 } | |
803 | |
804 /** | |
805 * @return the importFileMsg | |
806 */ | |
807 public String getImportFileMsg() { | |
808 return importFileMsg; | |
809 } | |
810 | |
811 /** | |
812 * @param importFileMsg | |
813 * the importFileMsg to set | |
814 */ | |
815 public void setImportFileMsg(String importFileMsg) { | |
816 this.importFileMsg = importFileMsg; | |
817 } | |
818 | |
819 /** | |
820 * @return the importNode | |
821 */ | |
822 public Node getImportNode() { | |
823 return importNode; | |
824 } | |
825 | |
826 public String getImportNodeAsText() { | |
827 String s = ""; | |
828 try { | |
829 String nt = importNode.getNodeType(); | |
830 if (nt.equals("ATTRIBUTE")) { | |
831 Attribute att = (Attribute) importNode; | |
97 | 832 s = "ATTRIBUTE " + att.getName() + " [" + att.getSourceObjectClass() + " " + att.getSourceId() + "] = " + att.getOwnValue(); |
96 | 833 } else if (nt.equals("ENTITY")) { |
834 Entity ent = (Entity) importNode; | |
97 | 835 s = "ENTITY " + ent.getObjectClass() + " [" + ent.getId() + "] : " + ent.getOwnValue(); |
96 | 836 } else if (nt.equals("RELATION")) { |
837 Relation rel = (Relation) importNode; | |
97 | 838 s = "RELATION " + rel.getObjectClass() + " [" + rel.getId() + "] : " |
96 | 839 + "source=[" + rel.getSourceObjectClass() + " " + rel.getSourceId() + "] " |
840 + "target=[" + rel.getTargetObjectClass() + " " + rel.getTargetId() + "]"; | |
841 } else { | |
842 s = importNode.toString(); | |
843 } | |
844 } catch (Exception e) { | |
845 logger.error(e); | |
846 } | |
847 return s; | |
848 } | |
849 | |
850 /** | |
851 * @param importNode the importNode to set | |
852 */ | |
853 public void setImportNode(Node importNode) { | |
854 this.importNode = importNode; | |
855 } | |
856 | |
857 /** | |
108 | 858 * @return the importNodeList |
859 */ | |
860 public List<Node> getImportNodeList() { | |
861 return importNodeList; | |
862 } | |
863 | |
864 /** | |
865 * @param importNodeList the importNodeList to set | |
866 */ | |
867 public void setImportNodeList(List<Node> importNodeList) { | |
868 this.importNodeList = importNodeList; | |
869 } | |
870 | |
871 /** | |
96 | 872 * @return the importNodeListLength |
873 */ | |
874 public int getImportNodeListLength() { | |
875 return importNodeListLength; | |
876 } | |
877 | |
878 /** | |
879 * @return the importNodeIdx | |
880 */ | |
881 public int getImportNodeIdx() { | |
882 return importNodeIdx; | |
883 } | |
884 | |
885 /** | |
886 * @param importNodeIdx the importNodeIdx to set | |
887 */ | |
888 public void setImportNodeIdx(int importNodeIdx) { | |
889 this.importNodeIdx = importNodeIdx; | |
890 } | |
891 | |
892 /** | |
893 * @return the importNodeMsg | |
894 */ | |
895 public String getImportNodeMsg() { | |
896 return importNodeMsg; | |
897 } | |
898 | |
899 /** | |
900 * @param importNodeMsg the importNodeMsg to set | |
901 */ | |
902 public void setImportNodeMsg(String importNodeMsg) { | |
903 this.importNodeMsg = importNodeMsg; | |
904 } | |
905 | |
906 } |