Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / AbcdImportPreference.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;
10
11 import java.net.URI;
12
13 import org.eclipse.jface.layout.GridLayoutFactory;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.CLabel;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.events.SelectionListener;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Combo;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Label;
26
27 import eu.etaxonomy.cdm.api.application.ICdmRepository;
28 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
29 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
30 import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
31 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
32 import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
33 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
34 import eu.etaxonomy.taxeditor.l10n.Messages;
35 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
36 import eu.etaxonomy.taxeditor.store.CdmStore;
37
38 /**
39 * @author k.luther
40 * @since 23.03.2018
41 *
42 */
43 public class AbcdImportPreference extends CdmPreferencePage implements IE4PreferencePage, SelectionListener {
44
45 private static final String CATALOGUE_NUMBER = "Catalogue number";
46
47 private static final String BARCODE = "Barcode";
48
49 private static final String ACCESSION_NUMBER = "Accession number";
50
51 protected Abcd206ImportConfigurator configurator;
52
53 protected Combo nomenclaturalCodeSelectionCombo;
54
55 protected boolean allowOverride = true;
56 protected boolean override = true;
57 protected CdmPreference preference = null;
58 Combo textDNAProviderString;
59 private Composite composite;
60
61
62 Button checkBoxMediaSpecimen;
63 Button checkBoxIgnoreExisting;
64 Button checkBoxIgnoreAuthorship;
65 Combo checkBoxMapUnitId;
66 Button checkBoxRemoveCountry;
67 Button checkBoxMoveToDefaultClassification;
68 Button checkBoxImportSiblings;
69 Button checkBoxAddIndividualsAssociations;
70 Button checkBoxReuseDescriptiveGroups;
71 Button checkBoxReuseExistingTaxa;
72
73
74 Combo useLocalOrAdmin;
75
76 @Override
77 protected Control createContents(Composite parent) {
78 getValues();
79
80 if (!isAdminPreference && !allowOverride){
81 Label label = new Label(parent, SWT.NONE);
82 label.setText("The CDM settings don't allow to set the abcd import preference locally. If you need to make local settings, please ask an administrator.");
83 this.noDefaultAndApplyButton();
84 return parent;
85 }
86 composite = new Composite(parent, SWT.NULL);
87 GridLayout gridLayout = new GridLayout();
88 composite.setLayout(gridLayout);
89
90 final CLabel description = new CLabel(composite, SWT.NULL);
91 description.setText(Messages.AbcdImportPreference_description);
92 checkBoxMediaSpecimen = new Button(composite, SWT.CHECK);
93 checkBoxMediaSpecimen.setText(Messages.AbcdImportPreference_media_as_mediaSpecimen);
94 checkBoxMediaSpecimen
95 .setToolTipText(Messages.AbcdImportPreference_media_as_subUnit);
96 checkBoxMediaSpecimen.addSelectionListener(new SelectionAdapter() {
97 @Override
98 public void widgetSelected(SelectionEvent e) {
99 configurator.setAddMediaAsMediaSpecimen(!configurator.isAddMediaAsMediaSpecimen());
100 setApply(true);
101 }
102 });
103
104 checkBoxIgnoreExisting = new Button(composite, SWT.CHECK);
105
106 checkBoxIgnoreExisting.setText(Messages.AbcdImportPreference_not_import_existing_specimen);
107 checkBoxIgnoreExisting
108 .setToolTipText(Messages.AbcdImportPreference_not_import_existing_specimen_tooltip);
109 checkBoxIgnoreExisting.addSelectionListener(new SelectionAdapter() {
110 @Override
111 public void widgetSelected(SelectionEvent e) {
112 configurator.setIgnoreImportOfExistingSpecimen(!configurator.isIgnoreImportOfExistingSpecimen());
113 setApply(true);
114 }
115 });
116
117 checkBoxIgnoreAuthorship = new Button(composite, SWT.CHECK);
118
119 checkBoxIgnoreAuthorship.setText(Messages.AbcdImportPreference_ignore_author);
120 checkBoxIgnoreAuthorship
121 .setToolTipText(Messages.AbcdImportPreference_ignore_author_tooltip);
122 checkBoxIgnoreAuthorship.addSelectionListener(new SelectionAdapter() {
123 @Override
124 public void widgetSelected(SelectionEvent e) {
125 configurator.setIgnoreAuthorship(!configurator.isIgnoreAuthorship());
126 setApply(true);
127 }
128 });
129
130
131 GridData gridData = new GridData();
132 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
133 gridData.horizontalIndent = 9;
134 gridData.minimumWidth=100;
135 // classificationSelection.setLayoutData(gridData);
136 Label unitIdLabel = new Label(composite, SWT.NONE);
137 unitIdLabel.setText(Messages.AbcdImportPreference_map_unit_nr_catalog_number);
138 checkBoxMapUnitId = new Combo(composite, SWT.BORDER| SWT.READ_ONLY);
139 checkBoxMapUnitId.setLayoutData(gridData);
140 checkBoxMapUnitId.add(ACCESSION_NUMBER);
141 checkBoxMapUnitId.add(BARCODE);
142 checkBoxMapUnitId.add(CATALOGUE_NUMBER);
143 checkBoxMapUnitId
144 .setToolTipText(Messages.AbcdImportPreference_map_unit_number_catalog_number_tooltip);
145 checkBoxMapUnitId.addSelectionListener(this);
146
147 checkBoxRemoveCountry = new Button(composite, SWT.CHECK);
148
149 checkBoxRemoveCountry.setText(Messages.AbcdImportPreference_remove_country_from_locality);
150 checkBoxRemoveCountry
151 .setToolTipText(Messages.AbcdImportPreference_remove_country_from_locality_tooltip);
152 checkBoxRemoveCountry.addSelectionListener(new SelectionAdapter() {
153 @Override
154 public void widgetSelected(SelectionEvent e) {
155 configurator.setRemoveCountryFromLocalityText(!configurator.isRemoveCountryFromLocalityText());
156 setApply(true);
157 }
158 });
159
160 checkBoxMoveToDefaultClassification = new Button(composite, SWT.CHECK);
161 checkBoxMoveToDefaultClassification.setSelection(configurator.isMoveNewTaxaToDefaultClassification());
162 checkBoxMoveToDefaultClassification.setText(Messages.AbcdImportPreference_create_new_classification_new_taxa);
163 checkBoxMoveToDefaultClassification
164 .setToolTipText(Messages.AbcdImportPreference_create_new_classification_new_taxa_tooltip);
165 checkBoxMoveToDefaultClassification.addSelectionListener(new SelectionAdapter() {
166 @Override
167 public void widgetSelected(SelectionEvent e) {
168 configurator.setMoveNewTaxaToDefaultClassification(!configurator.isMoveNewTaxaToDefaultClassification());
169 setApply(true);
170 }
171 });
172
173 checkBoxImportSiblings = new Button(composite, SWT.CHECK);
174
175 checkBoxImportSiblings.setText(Messages.AbcdImportPreference_import_all_children_for_cultures_or_tissues);
176 checkBoxImportSiblings
177 .setToolTipText(Messages.AbcdImportPreference_import_all_children_for_cultures_or_tissues_tooltip);
178 checkBoxImportSiblings.addSelectionListener(new SelectionAdapter() {
179 @Override
180 public void widgetSelected(SelectionEvent e) {
181 configurator.setGetSiblings(!configurator.isGetSiblings());
182 setApply(true);
183 }
184 });
185
186 checkBoxAddIndividualsAssociations = new Button(composite, SWT.CHECK);
187
188 checkBoxAddIndividualsAssociations.setText(Messages.AbcdImportPreference_create_Individual_Association);
189 checkBoxAddIndividualsAssociations
190 .setToolTipText(Messages.AbcdImportPreference_create_Individual_Association_tooltip);
191 checkBoxAddIndividualsAssociations.addSelectionListener(new SelectionAdapter() {
192 @Override
193 public void widgetSelected(SelectionEvent e) {
194 configurator.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(!configurator.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
195 setApply(true);
196 }
197 });
198
199 checkBoxReuseDescriptiveGroups = new Button(composite, SWT.CHECK);
200
201 checkBoxReuseDescriptiveGroups.setText(Messages.AbcdImportPreference_reuse_descriptive_group);
202 checkBoxReuseDescriptiveGroups
203 .setToolTipText(Messages.AbcdImportPreference_reuse_descriptive_group_tooltip);
204 checkBoxReuseDescriptiveGroups.addSelectionListener(new SelectionAdapter() {
205 @Override
206 public void widgetSelected(SelectionEvent e) {
207 configurator.setReuseExistingDescriptiveGroups(!configurator.isReuseExistingDescriptiveGroups());
208 setApply(true);
209 }
210 });
211
212 checkBoxReuseExistingTaxa = new Button(composite, SWT.CHECK);
213
214 checkBoxReuseExistingTaxa.setText(Messages.AbcdImportPreference_reuse_existing_taxa);
215 checkBoxReuseExistingTaxa
216 .setToolTipText(Messages.AbcdImportPreference_reuse_existing_taxa_tooltip);
217 checkBoxReuseExistingTaxa.addSelectionListener(new SelectionAdapter() {
218 @Override
219 public void widgetSelected(SelectionEvent e) {
220 configurator.setReuseExistingTaxaWhenPossible(!configurator.isReuseExistingTaxaWhenPossible());
221 setApply(true);
222 }
223 });
224 GridData sepGrid = createTextGridData();
225 Label separator = new Label(composite, SWT.HORIZONTAL|SWT.SEPARATOR);
226 separator.setLayoutData(sepGrid);
227 separator.setVisible(false);
228 Label labelRef = new Label(composite, SWT.NONE);
229 labelRef.setText("Biocase provider for associated DNA");
230 textDNAProviderString = new Combo(composite, SWT.BORDER);
231
232 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
233 gridData.horizontalIndent = 9;
234 gridData.minimumWidth=200;
235
236 textDNAProviderString.setLayoutData(gridData);
237 GridLayoutFactory.fillDefaults();
238 String biocaseProvider = null;
239 CdmPreference pref = null;
240
241 pref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.BioCaseProvider);
242 biocaseProvider = pref!= null? pref.getValue():null;
243
244 if (!isAdminPreference && ((pref != null && pref.isAllowOverride()) || pref == null) ){
245 biocaseProvider = PreferencesUtil.getStringValue(PreferencePredicate.BioCaseProvider.getKey(), true);
246 }
247 if (biocaseProvider != null){
248 String items[] = biocaseProvider.split(";");
249 textDNAProviderString.setItems(items);
250 }
251
252 textDNAProviderString.setEnabled(true);
253 // textDNAProviderString.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1));
254 textDNAProviderString.addSelectionListener(this);
255
256 Label nomenclaturalCodeLabel = new Label(composite, SWT.NONE);
257 nomenclaturalCodeLabel.setText("Nomenclatural Code");
258 nomenclaturalCodeSelectionCombo = new Combo(composite, SWT.BORDER| SWT.READ_ONLY);
259 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
260 gridData.horizontalIndent = 9;
261 nomenclaturalCodeSelectionCombo.setLayoutData(gridData);
262 GridLayoutFactory.fillDefaults();
263 for(NomenclaturalCode code: NomenclaturalCode.values()){
264 nomenclaturalCodeSelectionCombo.add(code.getKey());
265 }
266
267 nomenclaturalCodeSelectionCombo.addSelectionListener(this);
268 if (preference != null){
269 allowOverride = preference.isAllowOverride();
270 }
271
272 Label sep = new Label(composite, SWT.HORIZONTAL|SWT.SEPARATOR);
273 sep.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT, true, false));
274 GridLayoutFactory.fillDefaults();
275 useLocalOrAdmin = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
276 if (!isAdminPreference){
277 useLocalOrAdmin.add(LocalOrDefaultEnum.Default.getLabel(), 0);
278 useLocalOrAdmin.add(LocalOrDefaultEnum.Local.getLabel(), 1);
279
280 }else{
281 useLocalOrAdmin.add(LocalOrDefaultEnum.AllowOverride.getLabel(), 0);
282 useLocalOrAdmin.add(LocalOrDefaultEnum.Database.getLabel(), 1);
283 }
284
285 if (!isAdminPreference){
286 if (override && allowOverride){
287 useLocalOrAdmin.select(1);
288 }else{
289 useLocalOrAdmin.select(0);
290 composite.setEnabled(false);
291 }
292 }else{
293 if (allowOverride){
294 useLocalOrAdmin.select(0);
295 }else{
296 useLocalOrAdmin.select(1);
297 }
298 }
299 useLocalOrAdmin.addSelectionListener(this);
300 if (!allowOverride && !isAdminPreference){
301 useLocalOrAdmin.setEnabled(false);
302 }
303 GridData gridDataLocal = new GridData();
304 gridDataLocal = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
305 // gridDataLocal.horizontalIndent = 9;
306 gridDataLocal.minimumWidth=100;
307 gridDataLocal.widthHint= nomenclaturalCodeSelectionCombo.getBorderWidth();
308 useLocalOrAdmin.setLayoutData(gridDataLocal);
309 GridLayoutFactory.fillDefaults();
310 setSelections();
311
312 return composite;
313 }
314
315 @Override
316 public void widgetSelected(SelectionEvent e) {
317 if (e.getSource().equals(nomenclaturalCodeSelectionCombo)){
318 this.configurator.setNomenclaturalCode(NomenclaturalCode.getByKey(nomenclaturalCodeSelectionCombo.getText()));
319 }
320 if (e.getSource().equals(checkBoxMapUnitId)){
321 String text = checkBoxMapUnitId.getText();
322 if (text.equals(ACCESSION_NUMBER)){
323 this.configurator.setMapUnitIdToAccessionNumber(true);
324 this.configurator.setMapUnitIdToBarcode(false);
325 this.configurator.setMapUnitIdToCatalogNumber(false);
326 }else if (text.equals(BARCODE)){
327 this.configurator.setMapUnitIdToAccessionNumber(false);
328 this.configurator.setMapUnitIdToBarcode(true);
329 this.configurator.setMapUnitIdToCatalogNumber(false);
330 }else{
331 this.configurator.setMapUnitIdToAccessionNumber(false);
332 this.configurator.setMapUnitIdToBarcode(false);
333 this.configurator.setMapUnitIdToCatalogNumber(true);
334 }
335 }
336 if (e.getSource().equals(useLocalOrAdmin)){
337 if (useLocalOrAdmin.getText().equals(LocalOrDefaultEnum.Default.getLabel()) || useLocalOrAdmin.getText().equals(LocalOrDefaultEnum.Database.getLabel())){
338 composite.setEnabled(false);
339 PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(configurator, preference.getValue());
340 setSelections();
341
342 if (isAdminPreference){
343 allowOverride = false;
344 }else{
345 override = false;
346 }
347 }else{
348 composite.setEnabled(true);
349 if (isAdminPreference){
350 allowOverride = true;
351 }else{
352 override = true;
353 }
354 }
355 }
356
357 if (e.getSource().equals(textDNAProviderString)){
358 String textDnaProvider = textDNAProviderString.getText();
359 try{
360 this.configurator.setDnaSoure(URI.create(textDnaProvider));
361 }catch(IllegalArgumentException iae){
362 //do not set as dna source
363 }
364 }
365 this.setApply(true);
366
367
368 // PreferencesUtil.recursiveSetEnabled(composite, override);
369 setApply(true);
370
371 }
372
373 @Override
374 public boolean performOk() {
375 if (!isApply()){
376 return true;
377 }
378 if (configurator != null){
379 try{
380 configurator.setDnaSoure(URI.create(textDNAProviderString.getText()));
381 }catch(IllegalArgumentException e){
382 //dna provider is not an uri
383 }
384 String configString = configurator.toString();
385
386
387 if (override){
388 PreferencesUtil.setStringValue(PreferencePredicate.AbcdImportConfig.getKey(), configString);
389 PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), override);
390
391 }else{
392 PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), override);
393 }
394 PreferencesUtil.setStringValue(PreferencePredicate.AbcdImportConfig.getKey(), "");
395
396
397 }
398 return true;
399 }
400
401 /**
402 * {@inheritDoc}
403 */
404 @Override
405 public void widgetDefaultSelected(SelectionEvent e) {
406 // TODO Auto-generated method stub
407
408 }
409
410 @Override
411 protected void performDefaults() {
412 configurator = Abcd206ImportConfigurator.NewInstance(null,null);
413 configurator.setNomenclaturalCode(PreferencesUtil.getPreferredNomenclaturalCode());
414 override = false;
415 setSelections();
416 setApply(true);
417 }
418
419 @Override
420 public void getValues() {
421 isAdminPreference = false;
422 configurator = Abcd206ImportConfigurator.NewInstance(null,null);
423 ICdmRepository controller;
424 controller = CdmStore.getCurrentApplicationConfiguration();
425 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AbcdImportConfig);
426 preference = controller.getPreferenceService().find(key);
427
428 if (preference != null ){
429 allowOverride = preference.isAllowOverride();
430 }else{
431 preference = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AbcdImportConfig, configurator.toString());
432 }
433
434 override = PreferencesUtil.getBooleanValue(
435 PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), true) != null? PreferencesUtil.getBooleanValue(
436 PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), true):false;
437 if (allowOverride && override){
438 String configString = PreferencesUtil.getStringValue(PreferencePredicate.AbcdImportConfig.getKey(), true);
439 configurator = Abcd206ImportConfigurator.NewInstance(null, null);
440 PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(configurator, configString);
441 }else{
442 PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(configurator, preference.getValue());
443 }
444 }
445
446 protected void setSelections(){
447 checkBoxMediaSpecimen.setSelection(configurator.isAddMediaAsMediaSpecimen());
448 checkBoxIgnoreExisting.setSelection(configurator.isIgnoreImportOfExistingSpecimen());
449 checkBoxIgnoreAuthorship.setSelection(configurator.isIgnoreAuthorship());
450 if (configurator.isMapUnitIdToAccessionNumber()){
451 checkBoxMapUnitId.select(0);
452 }else if (configurator.isMapUnitIdToBarcode()){
453 checkBoxMapUnitId.select(1);
454 }else{
455 checkBoxMapUnitId.select(2);
456 }
457
458
459 checkBoxRemoveCountry.setSelection(configurator.isRemoveCountryFromLocalityText());
460 checkBoxImportSiblings.setSelection(configurator.isGetSiblings());
461 checkBoxAddIndividualsAssociations.setSelection(configurator.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
462 checkBoxReuseDescriptiveGroups.setSelection(configurator.isReuseExistingDescriptiveGroups());
463 checkBoxReuseExistingTaxa.setSelection(configurator.isReuseExistingTaxaWhenPossible());
464
465 textDNAProviderString.setText(configurator.getDnaSoure()!= null? configurator.getDnaSoure().toString():"");
466 int index = 0;
467 if (configurator.getNomenclaturalCode() != null){
468 for (String label : nomenclaturalCodeSelectionCombo.getItems()){
469 if (label.equals(configurator.getNomenclaturalCode().getKey())){
470 nomenclaturalCodeSelectionCombo.select(index);
471 }
472 index++;
473 }
474 }else{
475 for (String label : nomenclaturalCodeSelectionCombo.getItems()){
476 NomenclaturalCode code = PreferencesUtil.getPreferredNomenclaturalCode();
477 if (label.equals(code.getKey())){
478 nomenclaturalCodeSelectionCombo.select(index);
479 configurator.setNomenclaturalCode(code);
480 }
481 index++;
482 }
483
484 }
485
486 }
487
488 }