ref #8539 Add association when adding specimens to the matrix
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 11 Sep 2019 09:51:18 +0000 (11:51 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 11 Sep 2019 12:03:04 +0000 (14:03 +0200)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/CharacterMatrixBottomToolbar.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/SpecimenSelectionDialog.java

index 01c460e8224e0c2d57d0e995de28ccfeeca192e7..c8f8660e1171e2e8948df9ebe905c513f6d6549e 100644 (file)
@@ -12,6 +12,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.UUID;
 
 import org.eclipse.core.runtime.ICoreRunnable;
@@ -33,7 +34,9 @@ import org.eclipse.swt.widgets.Composite;
 
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
+import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.api.service.UpdateResult;
 import eu.etaxonomy.cdm.api.service.config.DescriptionAggregationConfiguration;
 import eu.etaxonomy.cdm.api.service.config.IdentifiableServiceConfiguratorImpl;
@@ -42,11 +45,16 @@ import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
 import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
 import eu.etaxonomy.cdm.model.description.CategoricalData;
+import eu.etaxonomy.cdm.model.description.DescriptionType;
 import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
 import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
 import eu.etaxonomy.cdm.model.description.PolytomousKey;
 import eu.etaxonomy.cdm.model.description.QuantitativeData;
 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
+import eu.etaxonomy.cdm.model.description.TaxonDescription;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
 import eu.etaxonomy.cdm.strategy.generate.PolytomousKeyGenerator;
 import eu.etaxonomy.cdm.strategy.generate.PolytomousKeyGeneratorConfigurator;
@@ -95,7 +103,32 @@ public class CharacterMatrixBottomToolbar extends Composite{
                 SpecimenSelectionDialog dialog = new SpecimenSelectionDialog(matrix.getShell(), matrix);
                 if(dialog.open()==Window.OK){
                     Collection<SpecimenNodeWrapper> wrappers = dialog.getSpecimen();
+                    if(wrappers.stream().anyMatch(wrapper->!wrapper.isIndividualsAssociation())
+                            && !MessagingUtils.confirmDialog(
+                                    "Create Taxon Association",
+                                    "Some specimens are not linked with taxon via IndividualsAssociation yet.\n"
+                                    + "Do you want to create this association?")){
+                        return;
+                    }
                     for (SpecimenNodeWrapper wrapper : wrappers) {
+                        if(!wrapper.isIndividualsAssociation()){
+                            Optional<TaxonDescription> associationDescriptionOptional = wrapper.getTaxonNode().getTaxon().getDescriptions().stream()
+                            .filter(desc->desc.getTypes().contains(DescriptionType.INDIVIDUALS_ASSOCIATION))
+                            .findFirst();
+                            Taxon taxon = wrapper.getTaxonNode().getTaxon();
+                            TaxonDescription associationDescription;
+                            if(!associationDescriptionOptional.isPresent()){
+                                associationDescription = TaxonDescription.NewInstance(taxon);
+                            }
+                            else{
+                                associationDescription = associationDescriptionOptional.get();
+                            }
+
+                            SpecimenOrObservationBase specimen = CdmStore.getService(IOccurrenceService.class).load(wrapper.getUuidAndTitleCache().getUuid());
+                            IndividualsAssociation association = IndividualsAssociation.NewInstance(specimen);
+                            associationDescription.addElement(association);
+                            CdmStore.getService(ITaxonService.class).saveOrUpdate(wrapper.getTaxonNode().getTaxon());
+                        }
                         SpecimenDescription specimenDescription = CdmStore.getService(IDescriptiveDataSetService.class)
                                 .findSpecimenDescription(matrix.getDescriptiveDataSet().getUuid(),
                                         wrapper.getUuidAndTitleCache().getUuid(), true);
index 764e3ac5dd840958a23d061fdd5c3391c68e1f83..382ed39faed849dd2adcc32d02b93e30457a0ffd 100644 (file)
@@ -1,11 +1,11 @@
 /**
-* Copyright (C) 2018 EDIT
-* European Distributed Institute of Taxonomy
-* http://www.e-taxonomy.eu
-*
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
+ * Copyright (C) 2018 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
 package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
 
 import java.util.ArrayList;
@@ -18,7 +18,6 @@ import java.util.stream.Collectors;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.ColumnLabelProvider;
-import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.ITableLabelProvider;
 import org.eclipse.jface.viewers.TableViewer;
@@ -35,6 +34,7 @@ import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.Text;
@@ -75,10 +75,11 @@ public class SpecimenSelectionDialog extends Dialog {
     protected Control createDialogArea(Composite parent) {
         Composite composite = (Composite) super.createDialogArea(parent);
         GridLayout gl_composite = new GridLayout();
+        gl_composite.numColumns = 2;
         composite.setLayout(gl_composite);
 
         Composite composite_1 = new Composite(composite, SWT.NONE);
-        composite_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
+        composite_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
         composite_1.setLayout(new GridLayout(4, false));
 
         txtTextFilter = new Text(composite_1, SWT.BORDER);
@@ -112,12 +113,18 @@ public class SpecimenSelectionDialog extends Dialog {
             @Override
             public void widgetSelected(SelectionEvent e) {
                 loadSpecimens();
-                list.setInput(matrix.getSpecimenCache());
+                refreshInput(matrix.getSpecimenCache());
             }
         });
 
+        Label lblNewLabel = new Label(composite, SWT.NONE);
+        lblNewLabel.setImage(ImageResources.getImage(ImageResources.WARNING_ICON));
+
+        Label lblNewLabel_1 = new Label(composite, SWT.NONE);
+        lblNewLabel_1.setText(": Specimen not associated with taxon");
+
         ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
-        scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
+        scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
         scrolledComposite.setExpandHorizontal(true);
         scrolledComposite.setExpandVertical(true);
         //table
@@ -153,17 +160,17 @@ public class SpecimenSelectionDialog extends Dialog {
             loadSpecimens();
         }
 
-        list.setInput(matrix.getSpecimenCache());
+        refreshInput(matrix.getSpecimenCache());
 
         columnSpecimen.getColumn().pack();
 
-        // enable tooltips
-        columnSpecimen.setLabelProvider(new SpecimenListLabelProvider());
-        ColumnViewerToolTipSupport.enableFor(columnSpecimen.getViewer());
-
         return composite;
     }
 
+    private void refreshInput(Object input){
+        list.setInput(input);
+    }
+
     private void applyFilter(){
         List<SpecimenNodeWrapper> result = new ArrayList<>();
         Collection<SpecimenNodeWrapper> specimenCache = matrix.getSpecimenCache();
@@ -182,7 +189,7 @@ public class SpecimenSelectionDialog extends Dialog {
                         .collect(Collectors.toList()));
             }
         }
-        list.setInput(result);
+        refreshInput(result);
     }
 
     private void loadSpecimens(){