Project

General

Profile

Download (20.5 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.ui.section.userecords;
2

    
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6
import java.util.ListIterator;
7
import java.util.Map;
8
import java.util.Set;
9
import org.apache.log4j.Logger;
10
import org.eclipse.swt.events.SelectionEvent;
11
import org.eclipse.swt.events.SelectionListener;
12
import eu.etaxonomy.cdm.model.common.Language;
13
import eu.etaxonomy.cdm.model.common.LanguageString;
14
import eu.etaxonomy.cdm.model.common.Marker;
15
import eu.etaxonomy.cdm.model.description.CategoricalData;
16
import eu.etaxonomy.cdm.model.description.Modifier;
17
import eu.etaxonomy.cdm.model.description.State;
18
import eu.etaxonomy.cdm.model.description.StateData;
19
import eu.etaxonomy.taxeditor.store.CdmStore;
20
import eu.etaxonomy.taxeditor.store.TermStore;
21
import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
22
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
23
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
24
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
25

    
26

    
27
/**
28
 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
29
 *
30
 * @author a.theys	
31
 * @created mar 13, 2012
32
 * @version 1.0
33
 */
34
public class UseRecordDetailElement extends
35
		AbstractCdmDetailElement<CategoricalData> implements SelectionListener {
36

    
37
	private static final Logger logger = Logger
38
			.getLogger(UseRecordDetailElement.class);
39

    
40
	private TermComboElement<State> combo_UseCategory;
41
	private TermComboElement<State> combo_UseSubCategory;
42
	private TermComboElement<Modifier> combo_PlantPart;
43
	private TermComboElement<Modifier> combo_Country;
44
	private TermComboElement<Modifier> combo_HumanGroup;
45
	private TermComboElement<Modifier> combo_EthnicGroup;
46

    
47
	public UseRecordDetailElement(CdmFormFactory formFactory,
48
			ICdmFormElement formElement) {
49
		super(formFactory, formElement);
50
	}
51

    
52
	/** {@inheritDoc} */
53
	@Override
54
	protected void createControls(ICdmFormElement formElement,
55
			CategoricalData entity, int style) {
56

    
57
		combo_UseCategory = formFactory.createTermComboElement(State.class,	this, "Use Category", null, style);
58
		combo_UseCategory.addSelectionListener(this);
59
		combo_UseSubCategory = formFactory.createTermComboElement(State.class, this, "Use SubCategory", null, style);
60
		combo_PlantPart = formFactory.createTermComboElement(Modifier.class, this, "Plant Part", null, style);
61
		combo_HumanGroup = formFactory.createTermComboElement(Modifier.class, this, "Human Group", null, style);
62
		combo_HumanGroup.addSelectionListener(this);
63
		combo_EthnicGroup = formFactory.createTermComboElement(Modifier.class, this, "Ethnic Group", null, style);
64
		combo_Country	= formFactory.createTermComboElement(Modifier.class, this, "Country", null, style);
65
		
66

    
67
		List<State> termsUseCategory = setUseCategoryComboTerms(TermStore.getTerms(State.class, null, false));
68
		combo_UseCategory.setTerms(termsUseCategory);
69
		combo_UseCategory.setSelection(stateSelection("Use Category"));
70
		
71
		List<State> termsSubCategory = setUseCategoryComboTerms(TermStore.getTerms(State.class, null, false), combo_UseCategory.getSelection());
72
		
73
		if (termsSubCategory != null) {
74
			combo_UseSubCategory.setTerms(termsSubCategory);
75
			combo_UseSubCategory.setSelection(stateSelection("Use SubCategory"));
76
			combo_UseSubCategory.setEnabled(true);
77
		}
78
		else {
79
			combo_UseSubCategory.setEnabled(false);
80
		}
81
		
82
		List<Modifier> plantPartsToAdd = setModifierComboTerms(TermStore.getTerms(Modifier.class, null, false), "Plant Part");
83
		combo_PlantPart.setTerms(plantPartsToAdd);
84
		combo_PlantPart.setSelection(modifierSelection("Plant Part"));
85
		
86
		List<Modifier> countriesToAdd = setModifierComboTerms(TermStore.getTerms(Modifier.class, null, false), "Country");
87
		combo_Country.setTerms(countriesToAdd);
88
		combo_Country.setSelection(modifierSelection("Country"));
89
		
90
		List<Modifier> humanGroupToAdd = setModifierComboTerms(TermStore.getTerms(Modifier.class, null, false), "Human Group");
91
		combo_HumanGroup.setTerms(humanGroupToAdd);
92
		combo_HumanGroup.setSelection(modifierSelection("Human Group"));
93
		
94
		List<Modifier> ethnicGroupToAdd = setEthnicGroupComboTerms(TermStore.getTerms(Modifier.class, null, false), combo_HumanGroup.getSelection());
95
		if (ethnicGroupToAdd != null) {
96
			combo_EthnicGroup.setTerms(ethnicGroupToAdd);
97
			combo_EthnicGroup.setSelection(modifierSelection("Ethnic Group"));
98
			combo_EthnicGroup.setEnabled(true);
99
		} else {
100
			combo_EthnicGroup.setEnabled(false);
101
		}
102

    
103
	}
104

    
105
	private Modifier modifierSelection(String comboCategory) {
106
		if(!getEntity().getModifiers().isEmpty()) {
107
			for (Modifier modifier : getEntity().getModifiers()) {
108
				if(GetVocabularyType(modifier, comboCategory)) {
109
					return modifier;
110
				}
111
			}
112
		} else {
113
			return null;
114
		}
115
		return null;
116
		
117
	}
118
	
119
	private State stateSelection(String comboCategory) {
120
		if (!getEntity().getStates().isEmpty()) {
121
			for (StateData statedata : getEntity().getStates()) {
122
				if (statedata.getModifyingText().get(CdmStore.getDefaultLanguage()) != null) {
123
					String testString = statedata.getModifyingText().get(CdmStore.getDefaultLanguage()).getText();
124
					if(testString.equals(comboCategory)) {
125
						if(statedata.getState() !=null) {
126
							return statedata.getState();
127
						}
128
					}
129
				}				
130
			}
131
		}
132
		else {
133
			return null;
134
		}
135
		return null;
136
		
137
	}
138
	
139
	private List<Modifier> setModifierComboTerms(List<Modifier> listOfTerms, String modType) {
140
		List<Modifier> termsToAdd = new ArrayList<Modifier>();
141
		for (Modifier term : listOfTerms) {
142

    
143
			if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(modType))) {
144
				termsToAdd.add(term);
145
			}
146
			else if ((term.getVocabulary() != null) && (term.getPartOf() == null) && (term.getVocabulary().getTitleCache().equals(modType))) {
147
				termsToAdd.add(term);
148
			}
149
		}
150
		
151
		return termsToAdd;
152
	}
153
	
154

    
155

    
156
	private List<State> setUseCategoryComboTerms(List<State> listOfTerms) {
157
		List<State> termsToAdd = new ArrayList<State>();
158
		for (State term : listOfTerms) {
159

    
160
			if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals("Use Category"))) {
161
				termsToAdd.add(term);
162
			}
163
			else if ((term.getVocabulary() !=null) && (term.getPartOf() == null) && (term.getVocabulary().getTitleCache().equals("Use Category"))) {
164
				termsToAdd.add(term);
165
			}
166
		}
167
		return termsToAdd;
168

    
169
	}
170

    
171
	private List<State> setUseCategoryComboTerms(List<State> listOfTerms,
172
			State selectedUseCategory) {
173
		List<State> termsToAdd = new ArrayList<State>();
174
		if (combo_UseCategory.getSelection() != null) {
175
			
176
			for (State term : listOfTerms) {
177
				if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(selectedUseCategory.getTitleCache()))) {
178
					termsToAdd.add(term);
179
				}
180
				else if ((term.getVocabulary() !=null) && (term.getVocabulary().getTitleCache().equals(selectedUseCategory.getTitleCache()))) {
181
					termsToAdd.add(term);
182
				}
183
	
184
			}
185
		}
186
		else if (!getEntity().getStates().isEmpty()) {
187
			for (StateData stateData : getEntity().getStates()) {
188
				if (stateData.getModifyingText().get(CdmStore.getDefaultLanguage()) != null) {
189
					if ((stateData.getState() != null) && (stateData.getModifyingText().get(CdmStore.getDefaultLanguage()).getText() == "Use Category")) {
190
						for (State term : listOfTerms) {
191
							if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(stateData.getState().getTitleCache()))) {
192
								termsToAdd.add(term);
193
							} else if ((term.getVocabulary() != null) && (term.getVocabulary().getTitleCache().equals(stateData.getState().getTitleCache()))) {
194
								termsToAdd.add(term);
195
							}
196
						}
197
						
198
					}
199
				}
200
			}
201
			
202
		}
203
			
204
		else {
205
			return null;
206
		}
207

    
208
		return termsToAdd;
209
	}
210
	
211
	private List<Modifier> setEthnicGroupComboTerms(List<Modifier> listOfTerms,
212
			Modifier selectedHumangroup) {
213
		List<Modifier> termsToAdd = new ArrayList<Modifier>();
214
		if (combo_HumanGroup.getSelection() != null) {
215
			for (Modifier term : listOfTerms) {
216
	
217
				if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(selectedHumangroup.getTitleCache()))) {
218
					termsToAdd.add(term);
219
				}
220
				else if ((term.getVocabulary() != null) && (term.getVocabulary().getTitleCache().equals(selectedHumangroup.getTitleCache()))) {
221
					termsToAdd.add(term);
222
				}
223
			}
224
		}
225
		else if (!getEntity().getModifiers().isEmpty()) {
226
			for (Modifier modifier : getEntity().getModifiers()) {
227
				if (modifier.getMarkers() != null) {
228
					for (Marker marker : modifier.getMarkers()) {
229
						if(marker.getMarkerType().getTitleCache().equals("Human Group")) {
230
							for (Modifier term : listOfTerms) {
231
								if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(modifier.getTitleCache()))) {
232
									termsToAdd.add(modifier);
233
								} else if ((term.getVocabulary() != null) && (term.getVocabulary().getTitleCache().equals(modifier.getTitleCache()))) {
234
									termsToAdd.add(modifier);
235
								}
236
							}
237
							
238
						}
239
					}
240
				}
241
			}
242
		} else {
243
			return null;
244
		}
245
		
246
		return termsToAdd;
247
	}
248

    
249
	/** {@inheritDoc} */
250
	@Override
251
	public void handleEvent(Object eventSource) {
252
		if (eventSource == combo_UseCategory) {
253
			if (combo_UseCategory.getSelection() != null) {
254
				boolean isChanged = false;
255
				boolean isUseCategoryChanged = false;
256
				if (!getEntity().getStates().isEmpty()) {
257
					for (StateData useCategoryStateDataCheck : getEntity().getStates()) {
258
						Map<Language, LanguageString> modText = useCategoryStateDataCheck.getModifyingText();
259
						if(modText.get(CdmStore.getDefaultLanguage()) != null && modText.get(CdmStore.getDefaultLanguage()).getText().equals("Use Category")) {
260
							if(!useCategoryStateDataCheck.getState().getTitleCache().equals(combo_UseCategory.getSelection().getTitleCache())) {
261
								isUseCategoryChanged = true;
262
							}
263
							
264
						}
265
					}
266
					ListIterator<StateData> itrExistingStates = getEntity().getStates().listIterator();
267
					while(itrExistingStates.hasNext()) {
268
						StateData existingStateData = itrExistingStates.next();
269
						Map<Language, LanguageString> modifyingText = existingStateData
270
								.getModifyingText();
271
						if (modifyingText.get(CdmStore.getDefaultLanguage()) != null) {
272
							if (modifyingText.get(CdmStore.getDefaultLanguage()).getText().equals("Use Category")) {
273
								isChanged = true;
274
								if(!existingStateData.getState().getTitleCache().equals(combo_UseCategory.getSelection().getTitleCache())) {
275
									itrExistingStates.remove();
276
									StateData stateData = StateData.NewInstance(combo_UseCategory.getSelection());
277
									stateData.setState(combo_UseCategory.getSelection());
278
									stateData.putModifyingText(CdmStore.getDefaultLanguage(),"Use Category");
279
									itrExistingStates.add(stateData);
280
									
281
								}
282
							} else if (modifyingText.get(CdmStore.getDefaultLanguage()).getText().equals("Use SubCategory") && isUseCategoryChanged == true) {
283
								itrExistingStates.remove();
284
							}
285
						}
286
					}
287
				}
288
				
289
				
290
				if (isChanged == false) {
291
					StateData stateData = StateData
292
							.NewInstance(combo_UseCategory.getSelection());
293
					stateData.setState(combo_UseCategory.getSelection());
294
					stateData.putModifyingText(CdmStore.getDefaultLanguage(), "Use Category");
295
					getEntity().addState(stateData);
296
				}
297
				
298
				combo_UseSubCategory.setEnabled(true);
299
	
300
			}
301
			else {
302
				if (!getEntity().getStates().isEmpty()) {
303
					Iterator<StateData> itr = getEntity().getStates().iterator();
304
					while(itr.hasNext()) {
305
						StateData stateToRemove = itr.next();
306
						itr.remove();
307
					}
308
					List<State> termsSubCategory = new ArrayList<State>();
309
					State emptyState = State.NewInstance();
310
					termsSubCategory.add(emptyState);
311
					combo_UseSubCategory.setTerms(termsSubCategory);
312
					combo_UseSubCategory.setEnabled(false);
313
				}
314
			}
315
		}
316
			
317
		if (eventSource == combo_UseSubCategory) {
318
			if (combo_UseSubCategory.getSelection() != null) {
319
				boolean isChanged = false;
320
				if (!getEntity().getStates().isEmpty()) {
321
					ListIterator<StateData> itrExistingStates = getEntity().getStates().listIterator();
322
					while(itrExistingStates.hasNext()) {
323
						StateData existingStateData = itrExistingStates.next();
324
						Map<Language, LanguageString> modifyingText = existingStateData
325
								.getModifyingText();
326
						if (modifyingText.get(CdmStore.getDefaultLanguage()) != null) {
327
							if (modifyingText.get(CdmStore.getDefaultLanguage()).getText().equals("Use SubCategory")) {
328
								isChanged = true;
329
								if(!existingStateData.getState().getTitleCache().equals(combo_UseSubCategory.getSelection().getTitleCache())) {
330
									itrExistingStates.remove();
331
									StateData stateData = StateData.NewInstance(combo_UseSubCategory.getSelection());
332
									stateData.setState(combo_UseSubCategory.getSelection());
333
									stateData.putModifyingText(CdmStore.getDefaultLanguage(),"Use SubCategory");
334
									itrExistingStates.add(stateData);
335
								}
336
							}
337
						}
338
					}
339
				}
340
				
341
				if (isChanged == false){
342
					StateData stateData = StateData
343
							.NewInstance(combo_UseSubCategory.getSelection());
344
					stateData.setState(combo_UseSubCategory.getSelection());
345
					stateData.putModifyingText(CdmStore.getDefaultLanguage(), "Use SubCategory");
346
					getEntity().addState(stateData);
347
				}
348
	
349
			} else {
350
				if (!getEntity().getStates().isEmpty()) {
351
					Iterator<StateData> itrStateData = getEntity().getStates().iterator();
352
					while(itrStateData.hasNext()) {
353
						StateData existingStateData = itrStateData.next();
354
						Map<Language, LanguageString> modifyingText = existingStateData
355
								.getModifyingText();
356

    
357
						if (modifyingText.get(CdmStore.getDefaultLanguage()) != null) {
358
							if (modifyingText.get(CdmStore.getDefaultLanguage()).getText() == "Use SubCategory") {
359
								itrStateData.remove();
360
							}
361
						}
362
					}
363
				}
364
			}
365
		}
366
		if (eventSource == combo_PlantPart) {
367
			if (combo_PlantPart.getSelection() != null) {
368
				Iterator<Modifier> itrExistingModifiers = getEntity().getModifiers().iterator();
369
				while (itrExistingModifiers.hasNext()) {
370
					Modifier modifier = itrExistingModifiers.next();
371
					if(GetVocabularyType(modifier, "Plant Part")) {
372
						itrExistingModifiers.remove();
373
					}
374
						
375
				}	
376
				Modifier plantPart = combo_PlantPart.getSelection();
377
				getEntity().addModifier(plantPart);
378
			} else {
379
				Set<Modifier> modifiers = getEntity().getModifiers();
380
				if(!modifiers.isEmpty()) {
381
					Iterator<Modifier> itr = modifiers.iterator();
382
					while (itr.hasNext()) {
383
						Modifier currentMod = itr.next();
384
						if(GetVocabularyType(currentMod, "Plant Part")) {
385
							itr.remove();
386
						}
387
					}
388
				}
389
				
390
			}
391
		}
392
		if (eventSource == combo_Country) {
393
			if (combo_Country.getSelection() != null) {
394
				Iterator<Modifier> itrExistingModifiers = getEntity().getModifiers().iterator();
395
				while (itrExistingModifiers.hasNext()) {
396
					Modifier modifier = itrExistingModifiers.next();
397
					if(GetVocabularyType(modifier, "Country")) {
398
						itrExistingModifiers.remove();
399
					}
400
						
401
				}
402
				Modifier country = combo_Country.getSelection();
403
				getEntity().addModifier(country);
404
			} else {
405
				Set<Modifier> modifiers = getEntity().getModifiers();
406
				if(!modifiers.isEmpty()) {
407
					Iterator<Modifier> itr = modifiers.iterator();
408
					while (itr.hasNext()) {
409
						Modifier currentMod = itr.next();
410
						if(GetVocabularyType(currentMod, "Country")) {
411
							itr.remove();
412
						}
413
					}
414
				}
415
			}
416
		}
417
		if (eventSource == combo_HumanGroup) {
418
			if (combo_HumanGroup.getSelection() != null) {
419
				boolean isHumanGroupChanged = true;
420
				for (Modifier modToCheck: getEntity().getModifiers()) {
421
					if(modToCheck.equals(combo_HumanGroup.getSelection()) && (GetVocabularyType(modToCheck, "Human Group"))) {
422
						isHumanGroupChanged = false;
423
				}
424
				
425
					
426
				}
427
				Iterator<Modifier> itrExistingModifiers = getEntity().getModifiers().iterator();
428
				while (itrExistingModifiers.hasNext()) {
429
					Modifier modifier = itrExistingModifiers.next();
430
					if((GetVocabularyType(modifier, "Human Group") || GetVocabularyType(modifier, "Ethnic Group")) && isHumanGroupChanged) {
431
						itrExistingModifiers.remove();
432
					}
433
						
434
				}	
435
				if(isHumanGroupChanged){
436
					Modifier humanGroup = combo_HumanGroup.getSelection();
437
					getEntity().addModifier(humanGroup);
438
				}
439
				combo_EthnicGroup.setEnabled(true);
440
			} else {
441
				Set<Modifier> modifiers = getEntity().getModifiers();
442
				if(!modifiers.isEmpty()) {
443
					Iterator<Modifier> itr = modifiers.iterator();
444
					while (itr.hasNext()) {
445
						Modifier currentMod = itr.next();
446
						if(GetVocabularyType(currentMod, "Human Group") || GetVocabularyType(currentMod, "Ethnic Group")) {
447
							itr.remove();
448
						}
449
					}
450
				}
451
				List<Modifier> emptyListToResetComboBox = new ArrayList<Modifier>();
452
				Modifier emptyMod = Modifier.NewInstance();
453
				emptyListToResetComboBox.add(emptyMod);
454
				combo_EthnicGroup.setTerms(emptyListToResetComboBox);
455
				combo_EthnicGroup.setEnabled(false);
456
				
457
			}
458
		}
459
		if (eventSource == combo_EthnicGroup) {
460
			if (combo_EthnicGroup.getSelection() != null) {
461
				Iterator<Modifier> itrExistingModifiers = getEntity().getModifiers().iterator();
462
					while (itrExistingModifiers.hasNext()) {
463
						Modifier modifier = itrExistingModifiers.next();
464
						if(GetVocabularyType(modifier, combo_HumanGroup.getSelection().getTitleCache())) {
465
							itrExistingModifiers.remove();
466
						}
467
						
468
					}	
469
					Modifier ethnicGroup = combo_EthnicGroup.getSelection();
470
					getEntity().addModifier(ethnicGroup);
471
			} else {
472
				Set<Modifier> modifiers = getEntity().getModifiers();
473
				if(!modifiers.isEmpty()) {
474
					
475
					Iterator<Modifier> itr = modifiers.iterator();
476
					while (itr.hasNext()) {
477
						Modifier currentMod = itr.next();
478
						if(GetVocabularyType(currentMod, "Ethnic Group")) {
479
							itr.remove();
480
						}
481
					}
482
					
483
				}
484
			}
485
		}
486
	}
487
	
488
	
489

    
490
	private boolean GetVocabularyType(Modifier term, String vocabularyExpected) {
491
		if ((term.getPartOf() != null)  && (term.getPartOf().getTitleCache().equals(vocabularyExpected))) {
492
			return true;
493
		}
494
		else if ((term.getVocabulary() != null) && (term.getPartOf()==null) && (term.getVocabulary().getTitleCache().equals(vocabularyExpected))) {
495
			return true;
496
		}
497
		else if ((vocabularyExpected.equals("Ethnic Group"))  && (term.getVocabulary() != null) && ((term.getPartOf() != null))) {
498
			return true;
499
		}
500
	return false;
501
	}
502
	
503
	
504

    
505
	/** {@inheritDoc} */
506
	@Override
507
	public void setEntity(CategoricalData entity) {
508
		super.setEntity(entity);
509
	}
510

    
511
	@Override
512
	public void widgetSelected(SelectionEvent e) {
513
		Object eventSource = e.getSource();
514
		for (Object object: combo_UseCategory.getControls()) {
515
			if (object == eventSource) {
516
				State selectedUseCategory = combo_UseCategory.getSelection();
517
				if (selectedUseCategory != null) {
518
					combo_UseSubCategory.setTerms(setUseCategoryComboTerms(TermStore.getTerms(State.class, null, false),selectedUseCategory));
519
				
520
					if (!getEntity().getStates().isEmpty()) {
521
						Iterator<StateData> itrExistingState = getEntity().getStates().iterator();
522
						while(itrExistingState.hasNext()) {
523
							StateData existingStateData = (StateData) itrExistingState.next();
524
							Map<Language, LanguageString> modifyingText = existingStateData.getModifyingText();
525
							if (modifyingText.get(CdmStore.getDefaultLanguage()) != null) {
526
								if (modifyingText.get(CdmStore.getDefaultLanguage())
527
										.getText().equals("Use SubCategory")) {
528
									combo_UseSubCategory.setSelection(stateSelection("Use SubCategory"));
529
								}
530
							}
531
						}
532
					}
533
				}
534
			}
535
		}
536
		for (Object object: combo_HumanGroup.getControls()) {
537
			if (object == eventSource) {
538
				Modifier selectedHumanGroup = combo_HumanGroup.getSelection();
539
				if (selectedHumanGroup != null) {
540
					combo_EthnicGroup.setTerms(setEthnicGroupComboTerms(TermStore.getTerms(Modifier.class, null, false), selectedHumanGroup));
541
					if (!getEntity().getModifiers().isEmpty()) {
542
						Iterator<Modifier> iterModifiers = getEntity().getModifiers().iterator();
543
						while (iterModifiers.hasNext()) {
544
							Modifier existingModifier = iterModifiers.next();
545
							if(GetVocabularyType(existingModifier, "Ethnic Group")) {
546
								combo_EthnicGroup.setSelection(modifierSelection("Ethnic Group"));
547
							}
548
						}
549
					}
550
					
551
				}
552
			}
553
		}
554
	}
555

    
556
	@Override
557
	public void widgetDefaultSelected(SelectionEvent e) {
558
		// TODO Auto-generated method stub
559

    
560
	}
561

    
562
}
(1-1/2)