56a66d8644ea7acd3195705bd8ec640c1e019877
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / userecords / UseRecordDetailElement.java
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 import eu.etaxonomy.taxeditor.ui.section.description.DescriptionElementDetailElement;
26
27 public class UseRecordDetailElement extends
28 AbstractCdmDetailElement<CategoricalData> implements SelectionListener {
29
30 private static final Logger logger = Logger
31 .getLogger(UseRecordDetailElement.class);
32
33 private TermComboElement<State> combo_UseCategory;
34 private TermComboElement<State> combo_UseSubCategory;
35 private TermComboElement<Modifier> combo_PlantPart;
36 private TermComboElement<Modifier> combo_Country;
37 private TermComboElement<Modifier> combo_HumanGroup;
38 private TermComboElement<Modifier> combo_EthnicGroup;
39
40 public UseRecordDetailElement(CdmFormFactory formFactory,
41 ICdmFormElement formElement) {
42 super(formFactory, formElement);
43 }
44
45 /** {@inheritDoc} */
46 @Override
47 protected void createControls(ICdmFormElement formElement,
48 CategoricalData entity, int style) {
49
50 combo_UseCategory = formFactory.createTermComboElement(State.class, this, "Use Category", null, style);
51 combo_UseCategory.addSelectionListener(this);
52 combo_UseSubCategory = formFactory.createTermComboElement(State.class, this, "Use SubCategory", null, style);
53 combo_Country = formFactory.createTermComboElement(Modifier.class, this, "Country", null, style);
54 combo_PlantPart = formFactory.createTermComboElement(Modifier.class, this, "Plant Part", null, style);
55 combo_HumanGroup = formFactory.createTermComboElement(Modifier.class, this, "Human Group", null, style);
56 combo_HumanGroup.addSelectionListener(this);
57 combo_EthnicGroup = formFactory.createTermComboElement(Modifier.class, this, "Ethnic Group", null, style);
58
59
60 List<State> termsUseCategory = setUseCategoryComboTerms(TermStore.getTerms(State.class, null, false));
61 combo_UseCategory.setTerms(termsUseCategory);
62 combo_UseCategory.setSelection(stateSelection("Use Category"));
63
64 List<State> termsSubCategory = setUseCategoryComboTerms(TermStore.getTerms(State.class, null, false), combo_UseCategory.getSelection());
65
66 if (termsSubCategory != null) {
67 combo_UseSubCategory.setTerms(termsSubCategory);
68 combo_UseSubCategory.setSelection(stateSelection("Use SubCategory"));
69 combo_UseSubCategory.setEnabled(true);
70 }
71 else {
72 combo_UseSubCategory.setEnabled(false);
73 }
74
75 List<Modifier> plantPartsToAdd = setModifierComboTerms(TermStore.getTerms(Modifier.class, null, false), "Plant Part");
76 combo_PlantPart.setTerms(plantPartsToAdd);
77 combo_PlantPart.setSelection(modifierSelection("Plant Part"));
78
79 List<Modifier> countriesToAdd = setModifierComboTerms(TermStore.getTerms(Modifier.class, null, false), "Country");
80 combo_Country.setTerms(countriesToAdd);
81 combo_Country.setSelection(modifierSelection("Country"));
82
83 List<Modifier> humanGroupToAdd = setModifierComboTerms(TermStore.getTerms(Modifier.class, null, false), "Human Group");
84 combo_HumanGroup.setTerms(humanGroupToAdd);
85 combo_HumanGroup.setSelection(modifierSelection("Human Group"));
86
87 List<Modifier> ethnicGroupToAdd = setEthnicGroupComboTerms(TermStore.getTerms(Modifier.class, null, false), combo_HumanGroup.getSelection());
88 if (ethnicGroupToAdd != null) {
89 combo_EthnicGroup.setTerms(ethnicGroupToAdd);
90 combo_EthnicGroup.setSelection(modifierSelection("Ethnic Group"));
91 combo_EthnicGroup.setEnabled(true);
92 } else {
93 combo_EthnicGroup.setEnabled(false);
94 }
95
96 }
97
98 private Modifier modifierSelection(String comboCategory) {
99 if(!getEntity().getModifiers().isEmpty()) {
100 for (Modifier modifier : getEntity().getModifiers()) {
101 if(GetVocabularyType(modifier, comboCategory)) {
102 return modifier;
103 }
104 }
105 } else {
106 return null;
107 }
108 return null;
109
110 }
111
112 private State stateSelection(String comboCategory) {
113 if (!getEntity().getStates().isEmpty()) {
114 for (StateData statedata : getEntity().getStates()) {
115 if (statedata.getModifyingText().get(CdmStore.getDefaultLanguage()) != null) {
116 String testString = statedata.getModifyingText().get(CdmStore.getDefaultLanguage()).getText();
117 if(testString.equals(comboCategory)) {
118 if(statedata.getState() !=null) {
119 return statedata.getState();
120 }
121 }
122 }
123 }
124 }
125 else {
126 return null;
127 }
128 return null;
129
130 }
131
132 private List<Modifier> setModifierComboTerms(List<Modifier> listOfTerms, String modType) {
133 List<Modifier> termsToAdd = new ArrayList<Modifier>();
134 for (Modifier term : listOfTerms) {
135
136 if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(modType))) {
137 termsToAdd.add(term);
138 }
139 else if ((term.getVocabulary() != null) && (term.getPartOf() == null) && (term.getVocabulary().getTitleCache().equals(modType))) {
140 termsToAdd.add(term);
141 }
142 }
143
144 return termsToAdd;
145 }
146
147
148
149 private List<State> setUseCategoryComboTerms(List<State> listOfTerms) {
150 List<State> termsToAdd = new ArrayList<State>();
151 for (State term : listOfTerms) {
152
153 if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals("Use Category"))) {
154 termsToAdd.add(term);
155 }
156 else if ((term.getVocabulary() !=null) && (term.getPartOf() == null) && (term.getVocabulary().getTitleCache().equals("Use Category"))) {
157 termsToAdd.add(term);
158 }
159 }
160 return termsToAdd;
161
162 }
163
164 private List<State> setUseCategoryComboTerms(List<State> listOfTerms,
165 State selectedUseCategory) {
166 List<State> termsToAdd = new ArrayList<State>();
167 if (combo_UseCategory.getSelection() != null) {
168
169 for (State term : listOfTerms) {
170 if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(selectedUseCategory.getTitleCache()))) {
171 termsToAdd.add(term);
172 }
173 else if ((term.getVocabulary() !=null) && (term.getVocabulary().getTitleCache().equals(selectedUseCategory.getTitleCache()))) {
174 termsToAdd.add(term);
175 }
176
177 }
178 }
179 else if (!getEntity().getStates().isEmpty()) {
180 for (StateData stateData : getEntity().getStates()) {
181 if (stateData.getModifyingText().get(CdmStore.getDefaultLanguage()) != null) {
182 if ((stateData.getState() != null) && (stateData.getModifyingText().get(CdmStore.getDefaultLanguage()).getText() == "Use Category")) {
183 for (State term : listOfTerms) {
184 if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(stateData.getState().getTitleCache()))) {
185 termsToAdd.add(term);
186 } else if ((term.getVocabulary() != null) && (term.getVocabulary().getTitleCache().equals(stateData.getState().getTitleCache()))) {
187 termsToAdd.add(term);
188 }
189 }
190
191 }
192 }
193 }
194
195 }
196
197 else {
198 return null;
199 }
200
201 return termsToAdd;
202 }
203
204 private List<Modifier> setEthnicGroupComboTerms(List<Modifier> listOfTerms,
205 Modifier selectedHumangroup) {
206 List<Modifier> termsToAdd = new ArrayList<Modifier>();
207 if (combo_HumanGroup.getSelection() != null) {
208 for (Modifier term : listOfTerms) {
209
210 if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(selectedHumangroup.getTitleCache()))) {
211 termsToAdd.add(term);
212 }
213 else if ((term.getVocabulary() != null) && (term.getVocabulary().getTitleCache().equals(selectedHumangroup.getTitleCache()))) {
214 termsToAdd.add(term);
215 }
216 }
217 }
218 else if (!getEntity().getModifiers().isEmpty()) {
219 for (Modifier modifier : getEntity().getModifiers()) {
220 if (modifier.getMarkers() != null) {
221 for (Marker marker : modifier.getMarkers()) {
222 if(marker.getMarkerType().getTitleCache().equals("Human Group")) {
223 for (Modifier term : listOfTerms) {
224 if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(modifier.getTitleCache()))) {
225 termsToAdd.add(modifier);
226 } else if ((term.getVocabulary() != null) && (term.getVocabulary().getTitleCache().equals(modifier.getTitleCache()))) {
227 termsToAdd.add(modifier);
228 }
229 }
230
231 }
232 }
233 }
234 }
235 } else {
236 return null;
237 }
238
239 return termsToAdd;
240 }
241
242 /** {@inheritDoc} */
243 @Override
244 public void handleEvent(Object eventSource) {
245 /*
246 * if(eventSource == checkbox_orderRelevant){
247 * getEntity().setOrderRelevant(checkbox_orderRelevant.getSelection());
248 * }
249 */
250 if (eventSource == combo_UseCategory) {
251 if (combo_UseCategory.getSelection() != null) {
252 boolean isChanged = false;
253 boolean isUseCategoryChanged = false;
254 if (!getEntity().getStates().isEmpty()) {
255 for (StateData useCategoryStateDataCheck : getEntity().getStates()) {
256 Map<Language, LanguageString> modText = useCategoryStateDataCheck.getModifyingText();
257 if(modText.get(CdmStore.getDefaultLanguage()) != null && modText.get(CdmStore.getDefaultLanguage()).getText().equals("Use Category")) {
258 if(!useCategoryStateDataCheck.getState().getTitleCache().equals(combo_UseCategory.getSelection().getTitleCache())) {
259 isUseCategoryChanged = true;
260 }
261
262 }
263 }
264 ListIterator<StateData> itrExistingStates = getEntity().getStates().listIterator();
265 while(itrExistingStates.hasNext()) {
266 StateData existingStateData = itrExistingStates.next();
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 itrExistingStates.remove();
274 StateData stateData = StateData.NewInstance(combo_UseCategory.getSelection());
275 stateData.setState(combo_UseCategory.getSelection());
276 stateData.putModifyingText(CdmStore.getDefaultLanguage(),"Use Category");
277 itrExistingStates.add(stateData);
278
279 }
280 } else if (modifyingText.get(CdmStore.getDefaultLanguage()).getText().equals("Use SubCategory") && isUseCategoryChanged == true) {
281 itrExistingStates.remove();
282 }
283 }
284 }
285 }
286
287
288 if (isChanged == false) {
289 StateData stateData = StateData
290 .NewInstance(combo_UseCategory.getSelection());
291 stateData.setState(combo_UseCategory.getSelection());
292 stateData.putModifyingText(CdmStore.getDefaultLanguage(), "Use Category");
293 getEntity().addState(stateData);
294 }
295
296 combo_UseSubCategory.setEnabled(true);
297
298 }
299 else {
300 if (!getEntity().getStates().isEmpty()) {
301 Iterator<StateData> itr = getEntity().getStates().iterator();
302 while(itr.hasNext()) {
303 StateData stateToRemove = itr.next();
304 itr.remove();
305 }
306 List<State> termsSubCategory = new ArrayList<State>();
307 State emptyState = State.NewInstance();
308 termsSubCategory.add(emptyState);
309 combo_UseSubCategory.setTerms(termsSubCategory);
310 combo_UseSubCategory.setEnabled(false);
311 }
312 }
313 }
314
315 if (eventSource == combo_UseSubCategory) {
316 if (combo_UseSubCategory.getSelection() != null) {
317 boolean isChanged = false;
318 if (!getEntity().getStates().isEmpty()) {
319 ListIterator<StateData> itrExistingStates = getEntity().getStates().listIterator();
320 while(itrExistingStates.hasNext()) {
321 StateData existingStateData = itrExistingStates.next();
322 Map<Language, LanguageString> modifyingText = existingStateData
323 .getModifyingText();
324 if (modifyingText.get(CdmStore.getDefaultLanguage()) != null) {
325 if (modifyingText.get(CdmStore.getDefaultLanguage()).getText().equals("Use SubCategory")) {
326 isChanged = true;
327 if(!existingStateData.getState().getTitleCache().equals(combo_UseSubCategory.getSelection().getTitleCache())) {
328 itrExistingStates.remove();
329 StateData stateData = StateData.NewInstance(combo_UseSubCategory.getSelection());
330 stateData.setState(combo_UseSubCategory.getSelection());
331 stateData.putModifyingText(CdmStore.getDefaultLanguage(),"Use SubCategory");
332 itrExistingStates.add(stateData);
333 }
334 }
335 }
336 }
337 }
338
339 if (isChanged == false){
340 StateData stateData = StateData
341 .NewInstance(combo_UseSubCategory.getSelection());
342 stateData.setState(combo_UseSubCategory.getSelection());
343 stateData.putModifyingText(CdmStore.getDefaultLanguage(), "Use SubCategory");
344 getEntity().addState(stateData);
345 }
346
347 } else {
348 if (!getEntity().getStates().isEmpty()) {
349 Iterator<StateData> itrStateData = getEntity().getStates().iterator();
350 while(itrStateData.hasNext()) {
351 StateData existingStateData = itrStateData.next();
352 Map<Language, LanguageString> modifyingText = existingStateData
353 .getModifyingText();
354
355 if (modifyingText.get(CdmStore.getDefaultLanguage()) != null) {
356 if (modifyingText.get(CdmStore.getDefaultLanguage()).getText() == "Use SubCategory") {
357 itrStateData.remove();
358 }
359 }
360 }
361 }
362 }
363 }
364 if (eventSource == combo_PlantPart) {
365 if (combo_PlantPart.getSelection() != null) {
366 Iterator<Modifier> itrExistingModifiers = getEntity().getModifiers().iterator();
367 while (itrExistingModifiers.hasNext()) {
368 Modifier modifier = itrExistingModifiers.next();
369 if(GetVocabularyType(modifier, "Plant Part")) {
370 itrExistingModifiers.remove();
371 }
372
373 }
374 Modifier plantPart = combo_PlantPart.getSelection();
375 getEntity().addModifier(plantPart);
376 } else {
377 Set<Modifier> modifiers = getEntity().getModifiers();
378 if(!modifiers.isEmpty()) {
379 Iterator<Modifier> itr = modifiers.iterator();
380 while (itr.hasNext()) {
381 Modifier currentMod = itr.next();
382 if(GetVocabularyType(currentMod, "Plant Part")) {
383 itr.remove();
384 }
385 }
386 }
387
388 }
389 }
390 if (eventSource == combo_Country) {
391 if (combo_Country.getSelection() != null) {
392 Iterator<Modifier> itrExistingModifiers = getEntity().getModifiers().iterator();
393 while (itrExistingModifiers.hasNext()) {
394 Modifier modifier = itrExistingModifiers.next();
395 if(GetVocabularyType(modifier, "Country")) {
396 itrExistingModifiers.remove();
397 }
398
399 }
400 Modifier country = combo_Country.getSelection();
401 getEntity().addModifier(country);
402 } else {
403 Set<Modifier> modifiers = getEntity().getModifiers();
404 if(!modifiers.isEmpty()) {
405 Iterator<Modifier> itr = modifiers.iterator();
406 while (itr.hasNext()) {
407 Modifier currentMod = itr.next();
408 if(GetVocabularyType(currentMod, "Country")) {
409 itr.remove();
410 }
411 }
412 }
413 }
414 }
415 if (eventSource == combo_HumanGroup) {
416 if (combo_HumanGroup.getSelection() != null) {
417 boolean isHumanGroupChanged = true;
418 for (Modifier modToCheck: getEntity().getModifiers()) {
419 if(modToCheck.equals(combo_HumanGroup.getSelection()) && (GetVocabularyType(modToCheck, "Human Group"))) {
420 isHumanGroupChanged = false;
421 }
422
423
424 }
425 Iterator<Modifier> itrExistingModifiers = getEntity().getModifiers().iterator();
426 while (itrExistingModifiers.hasNext()) {
427 Modifier modifier = itrExistingModifiers.next();
428 if((GetVocabularyType(modifier, "Human Group") || GetVocabularyType(modifier, "Ethnic Group")) && isHumanGroupChanged) {
429 itrExistingModifiers.remove();
430 }
431
432 }
433 if(isHumanGroupChanged){
434 Modifier humanGroup = combo_HumanGroup.getSelection();
435 getEntity().addModifier(humanGroup);
436 }
437 combo_EthnicGroup.setEnabled(true);
438 } else {
439 Set<Modifier> modifiers = getEntity().getModifiers();
440 if(!modifiers.isEmpty()) {
441 Iterator<Modifier> itr = modifiers.iterator();
442 while (itr.hasNext()) {
443 Modifier currentMod = itr.next();
444 if(GetVocabularyType(currentMod, "Human Group") || GetVocabularyType(currentMod, "Ethnic Group")) {
445 itr.remove();
446 }
447 }
448 }
449 List<Modifier> emptyListToResetComboBox = new ArrayList<Modifier>();
450 Modifier emptyMod = Modifier.NewInstance();
451 emptyListToResetComboBox.add(emptyMod);
452 combo_EthnicGroup.setTerms(emptyListToResetComboBox);
453 combo_EthnicGroup.setEnabled(false);
454
455 }
456 }
457 if (eventSource == combo_EthnicGroup) {
458 if (combo_EthnicGroup.getSelection() != null) {
459 Iterator<Modifier> itrExistingModifiers = getEntity().getModifiers().iterator();
460 while (itrExistingModifiers.hasNext()) {
461 Modifier modifier = itrExistingModifiers.next();
462 if(GetVocabularyType(modifier, combo_HumanGroup.getSelection().getTitleCache())) {
463 itrExistingModifiers.remove();
464 }
465
466 }
467 Modifier ethnicGroup = combo_EthnicGroup.getSelection();
468 getEntity().addModifier(ethnicGroup);
469 } else {
470 Set<Modifier> modifiers = getEntity().getModifiers();
471 if(!modifiers.isEmpty()) {
472
473 Iterator<Modifier> itr = modifiers.iterator();
474 while (itr.hasNext()) {
475 Modifier currentMod = itr.next();
476 if(GetVocabularyType(currentMod, "Ethnic Group")) {
477 itr.remove();
478 }
479 }
480
481 }
482 }
483 }
484 }
485
486
487
488 private boolean GetVocabularyType(Modifier term, String vocabularyExpected) {
489 if ((term.getPartOf() != null) && (term.getPartOf().getTitleCache().equals(vocabularyExpected))) {
490 return true;
491 }
492 else if ((term.getVocabulary() != null) && (term.getPartOf()==null) && (term.getVocabulary().getTitleCache().equals(vocabularyExpected))) {
493 return true;
494 }
495 else if ((vocabularyExpected.equals("Ethnic Group")) && (term.getVocabulary() != null) && ((term.getPartOf() != null))) {
496 return true;
497 }
498 return false;
499 }
500
501
502
503 /** {@inheritDoc} */
504 @Override
505 public void setEntity(CategoricalData entity) {
506 super.setEntity(entity);
507 }
508
509 @Override
510 public void widgetSelected(SelectionEvent e) {
511 Object eventSource = e.getSource();
512 for (Object object: combo_UseCategory.getControls()) {
513 if (object == eventSource) {
514 State selectedUseCategory = combo_UseCategory.getSelection();
515 //List<State> newSubCategories = setUseCategoryComboTerms(TermStore.getTerms(State.class, null, false),selectedUseCategory);
516 if (selectedUseCategory != null) {
517 combo_UseSubCategory.setTerms(setUseCategoryComboTerms(TermStore.getTerms(State.class, null, false),selectedUseCategory));
518
519 if (!getEntity().getStates().isEmpty()) {
520 Iterator<StateData> itrExistingState = getEntity().getStates().iterator();
521 while(itrExistingState.hasNext()) {
522 StateData existingStateData = (StateData) itrExistingState.next();
523 Map<Language, LanguageString> modifyingText = existingStateData.getModifyingText();
524 if (modifyingText.get(CdmStore.getDefaultLanguage()) != null) {
525 if (modifyingText.get(CdmStore.getDefaultLanguage())
526 .getText().equals("Use SubCategory")) {
527 combo_UseSubCategory.setSelection(stateSelection("Use SubCategory"));
528 }
529 }
530 }
531 }
532 }
533 }
534 }
535 for (Object object: combo_HumanGroup.getControls()) {
536 if (object == eventSource) {
537 Modifier selectedHumanGroup = combo_HumanGroup.getSelection();
538 if (selectedHumanGroup != null) {
539 combo_EthnicGroup.setTerms(setEthnicGroupComboTerms(TermStore.getTerms(Modifier.class, null, false), selectedHumanGroup));
540 if (!getEntity().getModifiers().isEmpty()) {
541 Iterator<Modifier> iterModifiers = getEntity().getModifiers().iterator();
542 while (iterModifiers.hasNext()) {
543 Modifier existingModifier = iterModifiers.next();
544 if(GetVocabularyType(existingModifier, "Ethnic Group")) {
545 combo_EthnicGroup.setSelection(modifierSelection("Ethnic Group"));
546 }
547 }
548 }
549
550 }
551 }
552 }
553 }
554
555 @Override
556 public void widgetDefaultSelected(SelectionEvent e) {
557 // TODO Auto-generated method stub
558
559 }
560
561 }