ref #7854: session handling in distribution editor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / wizard / AbstractTermSelectionWizardPage.java
1 /**
2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.preference.wizard;
10
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.UUID;
16
17 import org.apache.commons.lang.StringUtils;
18 import org.eclipse.jface.layout.GridLayoutFactory;
19 import org.eclipse.jface.viewers.CheckboxTreeViewer;
20 import org.eclipse.jface.wizard.WizardPage;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Composite;
24
25 import eu.etaxonomy.cdm.api.service.ITermService;
26 import eu.etaxonomy.cdm.api.service.IVocabularyService;
27 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
28 import eu.etaxonomy.cdm.model.common.TermType;
29 import eu.etaxonomy.cdm.model.common.TermVocabulary;
30 import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
31 import eu.etaxonomy.cdm.persistence.dto.TermDto;
32 import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
33 import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermSorter;
34 import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoContentProvider;
35 import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoLabelProvider;
36 import eu.etaxonomy.taxeditor.store.CdmStore;
37
38 /**
39 * @author k.luther
40 * @since 04.06.2018
41 *
42 */
43 public abstract class AbstractTermSelectionWizardPage extends WizardPage {
44
45 protected CheckBoxTreeComposite treeComposite;
46 private List<TermVocabularyDto> vocabularies = new ArrayList<>();
47 boolean localPref;
48
49 protected TermType type;
50
51 protected AbstractTermSelectionWizardPage(String pageName, TermType type) {
52 super(pageName);
53 // TODO check if configuration exists
54 // CdmStore.getCurrentSessionManager().bindNullSession();
55 this.type = type;
56 }
57
58 @Override
59 public void createControl(Composite parent){
60 parent.setLayout(new GridLayout());
61 treeComposite = new CheckBoxTreeComposite(parent, new TermDtoContentProvider(), new TermDtoLabelProvider(), SWT.NONE);
62 treeComposite.getViewer().setComparator(new DefinedTermSorter());
63 rememberCheckedValues(getCheckedValuesFromPreferences());
64 GridLayoutFactory.fillDefaults().applyTo(treeComposite);
65 setControl(treeComposite);
66 }
67
68 protected abstract String getCheckedValuesFromPreferences();
69
70 protected CheckboxTreeViewer getViewer(){
71 return treeComposite.getViewer();
72 }
73
74 public List<TermVocabularyDto> getVocabularies() {
75 return vocabularies;
76 }
77
78 public void addVocabularies(TermVocabularyDto vocabulary) {
79 this.vocabularies.add(vocabulary);
80 }
81
82 protected void setVocabularies(List<TermVocabularyDto> vocs) {
83 vocabularies = vocs;
84 }
85
86 protected void rememberCheckedValues(String checkedValues) {
87 initialiseVocabularies();
88 treeComposite.getViewer().setInput(getVocabularies());
89
90 if (checkedValues != null && checkedValues != "") { //$NON-NLS-1$
91 String[] listChecked = checkedValues.split(";"); //$NON-NLS-1$
92 String[] listCheckedComma = checkedValues.split(","); //$NON-NLS-1$
93 List<String> checked = new ArrayList<>();
94 if (listChecked != null ){
95 checked = Arrays.asList(listChecked);
96 }
97 if (listCheckedComma != null && checkedValues.contains(",")){ //$NON-NLS-1$
98 checked = Arrays.asList(listCheckedComma);
99 }
100 List<AbstractTermDto> termsFromStringValues = getTermsFromStringValues(checked);
101 treeComposite.setCheckedElements(termsFromStringValues.toArray());
102
103 }
104 }
105
106 private List<AbstractTermDto> getTermsFromStringValues(List<String> listValue) {
107 List<AbstractTermDto> termlist = new ArrayList<>();
108 for (String s : listValue) {
109 if (!StringUtils.isBlank(s)){
110 UUID uuid = UUID.fromString(s);
111 ITermService termService = CdmStore.getService(ITermService.class);
112 DefinedTermBase definedTermBase = termService.load(uuid);
113 if(definedTermBase != null){
114 termlist.add(TermDto.fromTerm(definedTermBase, true));
115 }else{
116 IVocabularyService vocabularyService = CdmStore.getService(IVocabularyService.class);
117 TermVocabulary termVocabulary = vocabularyService.load(uuid);
118 if (termVocabulary != null){
119 termlist.add(new TermVocabularyDto(uuid, termVocabulary.getRepresentations()));
120 }
121 }
122 }
123 }
124 return termlist;
125 }
126
127 protected void initialiseVocabularies() {
128 if (getVocabularies() != null) {
129 getVocabularies().clear();
130 }
131 List<TermVocabularyDto> vocs = new ArrayList<>();
132 vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
133
134 setVocabularies(vocs);
135 }
136
137 protected abstract List<TermVocabularyDto> getVocabulariesFromPreference();
138
139
140 }