Project

General

Profile

Download (20.3 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.Map;
7
import java.util.Set;
8
import org.apache.log4j.Logger;
9
import org.eclipse.swt.events.SelectionEvent;
10
import org.eclipse.swt.events.SelectionListener;
11
import eu.etaxonomy.cdm.model.common.Language;
12
import eu.etaxonomy.cdm.model.common.LanguageString;
13
import eu.etaxonomy.cdm.model.common.Marker;
14
import eu.etaxonomy.cdm.model.description.CategoricalData;
15
import eu.etaxonomy.cdm.model.description.Modifier;
16
import eu.etaxonomy.cdm.model.description.State;
17
import eu.etaxonomy.cdm.model.description.StateData;
18
import eu.etaxonomy.taxeditor.store.CdmStore;
19
import eu.etaxonomy.taxeditor.store.TermStore;
20
import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
21
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
22
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
23
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
24
import eu.etaxonomy.taxeditor.ui.section.description.DescriptionElementDetailElement;
25

    
26
//public class UseRecordDetailElement extends AbstractDetailedDescriptionDetailElement<CategoricalData> {
27
public class UseRecordDetailElement extends
28
		AbstractCdmDetailElement<CategoricalData> implements SelectionListener {
29

    
30
	private static final Logger logger = Logger
31
			.getLogger(DescriptionElementDetailElement.class);
32

    
33
	// private CheckboxElement checkbox_orderRelevant;
34
	private TermComboElement<State> combo_UseCategory;
35
	private TermComboElement<State> combo_UseSubCategory;
36
	private TermComboElement<Modifier> combo_PlantPart;
37
	private TermComboElement<Modifier> combo_Country;
38
	private TermComboElement<Modifier> combo_HumanGroup;
39
	private TermComboElement<Modifier> combo_EthnicGroup;
40
	// private AbstractDetailedDescriptionDetailElement detailElement;
41

    
42
	/*
43
	 * public UseRecordDetailElement(CdmFormFactory formFactory, ICdmFormElement
44
	 * formElement, CategoricalData entity, int style) { super(formFactory,
45
	 * formElement, entity, style); }
46
	 */
47
	/*
48
	 * public UseRecordDetailElement(CdmFormFactory formFactory, ICdmFormElement
49
	 * formElement, int style) {
50
	 */
51
	public UseRecordDetailElement(CdmFormFactory formFactory,
52
			ICdmFormElement formElement) {
53
		super(formFactory, formElement);
54
	}
55

    
56
	/** {@inheritDoc} */
57
	@Override
58
	protected void createControls(ICdmFormElement formElement,
59
			CategoricalData entity, int style) {
60

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

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

    
107
	}
108

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

    
147
			if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(modType))) {
148
				termsToAdd.add(term);
149
			}
150
			else if ((term.getVocabulary() != null) && (term.getPartOf() == null) && (term.getVocabulary().getTitleCache().equals(modType))) {
151
				termsToAdd.add(term);
152
			}
153
		}
154
		
155
		return termsToAdd;
156
	}
157
	
158

    
159

    
160
	private List<State> setUseCategoryComboTerms(List<State> listOfTerms) {
161
		List<State> termsToAdd = new ArrayList<State>();
162
		for (State term : listOfTerms) {
163

    
164
			if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals("Use Category"))) {
165
				termsToAdd.add(term);
166
			}
167
			else if ((term.getVocabulary() !=null) && (term.getPartOf() == null) && (term.getVocabulary().getTitleCache().equals("Use Category"))) {
168
				termsToAdd.add(term);
169
			}
170
		}
171
		return termsToAdd;
172

    
173
	}
174

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

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

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

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

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

    
508
	/** {@inheritDoc} */
509
	@Override
510
	public void setEntity(CategoricalData entity) {
511
		super.setEntity(entity);
512
	}
513

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

    
562
	@Override
563
	public void widgetDefaultSelected(SelectionEvent e) {
564
		// TODO Auto-generated method stub
565

    
566
	}
567

    
568
}
(1-1/2)