Updated version in pom / project files to taxeditor version : 5.31.0 and cdmlib versi...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / wizard / AvailableDistributionPage.java
1 /**
2 * Copyright (C) 2014 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 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.apache.commons.lang3.StringUtils;
21 import org.eclipse.swt.widgets.Composite;
22
23 import eu.etaxonomy.cdm.api.service.ITermService;
24 import eu.etaxonomy.cdm.api.service.IVocabularyService;
25 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
26 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
27 import eu.etaxonomy.cdm.model.term.TermType;
28 import eu.etaxonomy.cdm.model.term.TermVocabulary;
29 import eu.etaxonomy.cdm.persistence.dto.TermDto;
30 import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
31 import eu.etaxonomy.taxeditor.l10n.Messages;
32 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34
35 /**
36 * @author a.oppermann
37 * @date 21.07.2014
38 *
39 */
40 public class AvailableDistributionPage extends AbstractTermSelectionWizardPage<TermDto> {
41
42
43
44 public AvailableDistributionPage(String pageName) {
45 super(pageName, TermType.NamedArea);
46 this.localPref = true;
47
48
49 }
50
51
52 @Override
53 public void createControl(Composite parent) {
54 setTitle(Messages.AvailableDistributionPage_PAGE_TITLE);
55 setDescription(Messages.AvailableDistributionPage_PAGE_DESCRIPTION);
56
57 super.createControl(parent);
58 }
59
60 @Override
61 protected String getCheckedValuesFromPreferences() {
62 String checkedValues = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaTerms.getKey(), true);
63
64 return checkedValues;
65 }
66
67 @Override
68 protected void rememberCheckedValues(String checkedValues) {
69 initialiseVocabularies();
70
71 treeComposite.getViewer().setInput(getVocabularies());
72
73 if (checkedValues != null && checkedValues != "") { //$NON-NLS-1$
74 String[] listChecked = checkedValues.split(";"); //$NON-NLS-1$
75 String[] listCheckedComma = checkedValues.split(","); //$NON-NLS-1$
76 List<String> checked = new ArrayList<>();
77 if (listChecked != null ){
78 checked = Arrays.asList(listChecked);
79 }
80 if (listCheckedComma != null && checkedValues.contains(",")){ //$NON-NLS-1$
81 checked = Arrays.asList(listCheckedComma);
82 }
83 List<TermDto> termsFromStringValues = getTermsFromStringValues(checked);
84 treeComposite.setCheckedElements(termsFromStringValues.toArray());
85 }
86
87 }
88
89 @Override
90 public void dispose() {
91 // CdmStore.getCurrentSessionManager().dispose(this);
92 super.dispose();
93 }
94
95 // @Override
96 // public ICdmEntitySession getCdmEntitySession() {
97 // return CdmStore.getCurrentSessionManager().getNullSession();
98 // }
99 //
100 // @Override
101 // public <T extends CdmBase> Collection<T> getRootEntities() {
102 // return null;
103 // }
104 //
105 // @Override
106 public Map<Object, List<String>> getPropertyPathsMap() {
107 Map<Object, List<String>> propertyPathsMap = new HashMap<Object, List<String>>();
108 List<String> termsPropertyPaths = Arrays.asList(new String[] {
109 "includes" //$NON-NLS-1$
110 });
111 propertyPathsMap.put("includes", termsPropertyPaths); //$NON-NLS-1$
112 propertyPathsMap.put("terms", termsPropertyPaths); //$NON-NLS-1$
113 return propertyPathsMap;
114 }
115
116 @Override
117 protected List<TermVocabularyDto> getVocabulariesFromPreference(){
118 List<TermVocabularyDto> vocs = new ArrayList<>();
119
120 if (PreferencesUtil.getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies) == null && PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey()) == null){
121 vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
122 }else{
123 String vocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
124 if (vocString == null || vocString.equals("")){
125 vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
126 return vocs;
127 }
128 String[] arrayVocs = vocString.split(";"); //$NON-NLS-1$
129
130 Set<UUID> uuidVocs = new HashSet<>();
131 for (String voc: arrayVocs){
132 if (!StringUtils.isBlank(voc)){
133 uuidVocs.add(UUID.fromString(voc));
134 }
135 }
136 List<TermVocabulary> tempVocs = CdmStore.getService(IVocabularyService.class).find(uuidVocs);
137 for (TermVocabulary<?> voc: tempVocs){
138 vocs.add(new TermVocabularyDto(voc.getUuid(), voc.getRepresentations(), voc.getTermType(), voc.getTitleCache(), voc.isAllowDuplicates(), voc.isOrderRelevant(), voc.isFlat()));
139 }
140 }
141 return vocs;
142 }
143 @Override
144 protected void initialiseVocabularies() {
145 if (getVocabularies() != null) {
146 getVocabularies().clear();
147 }
148 List<TermVocabularyDto> vocs = new ArrayList<>();
149 vocs = getVocabulariesFromPreference();
150
151 setVocabularies(vocs);
152 }
153
154 @Override
155 protected List<TermDto> getTermsFromStringValues(List<String> listValue) {
156
157 List<TermDto> termlist = new ArrayList<>();
158 ITermService termService = CdmStore.getService(ITermService.class);
159 List<UUID> uuidList = new ArrayList<>();
160 for (String s : listValue) {
161 if (!StringUtils.isBlank(s)){
162 UUID uuid = UUID.fromString(s);
163 uuidList.add(uuid);
164 }
165 }
166 termlist = new ArrayList<>(termService.findByUUIDsAsDto(uuidList));
167
168
169 if (listValue.isEmpty()){
170 List<DefinedTermBase> terms = CdmStore.getTermManager().getAllTerms(type, null);
171 for (DefinedTermBase<?> term: terms){
172 termlist.add(TermDto.fromTerm(term, true));
173 }
174 }
175
176 return termlist;
177
178
179 }
180
181 }