Project

General

Profile

« Previous | Next » 

Revision 02746e7f

Added by Patrick Plitzner over 4 years ago

ref #7095 Fix session problems

  • load WS with property path to avoid multiple representation exception

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/CharacterMatrix.java
13 13
import java.io.FileOutputStream;
14 14
import java.io.IOException;
15 15
import java.util.ArrayList;
16
import java.util.Arrays;
16 17
import java.util.Collection;
17 18
import java.util.Collections;
18 19
import java.util.HashMap;
20
import java.util.HashSet;
19 21
import java.util.List;
20 22
import java.util.Map;
21 23
import java.util.Properties;
......
104 106
import eu.etaxonomy.cdm.api.service.IWorkingSetService;
105 107
import eu.etaxonomy.cdm.model.common.TermVocabulary;
106 108
import eu.etaxonomy.cdm.model.description.DescriptionBase;
109
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
107 110
import eu.etaxonomy.cdm.model.description.Feature;
108 111
import eu.etaxonomy.cdm.model.description.FeatureTree;
109 112
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
110 113
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
111 114
import eu.etaxonomy.cdm.model.description.State;
112 115
import eu.etaxonomy.cdm.model.description.WorkingSet;
116
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
113 117
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
114 118
import eu.etaxonomy.taxeditor.editor.workingSet.matrix.categorical.CategoricalDataCellEditor;
115 119
import eu.etaxonomy.taxeditor.editor.workingSet.matrix.categorical.CategoricalDataDisplayConverter;
......
120 124
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
121 125
import eu.etaxonomy.taxeditor.model.ImageResources;
122 126
import eu.etaxonomy.taxeditor.model.MessagingUtils;
127
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
128
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
123 129
import eu.etaxonomy.taxeditor.store.CdmStore;
124 130
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
125 131
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
......
129 135
 * @since Nov 26, 2017
130 136
 *
131 137
 */
132
public class CharacterMatrix implements IE4SavablePart, IPartContentHasDetails, IConversationEnabled, IDirtyMarkable{
138
public class CharacterMatrix implements IE4SavablePart, IPartContentHasDetails, IConversationEnabled, IDirtyMarkable,
139
        ICdmEntitySessionEnabled{
140

  
141
    private static final List<String> WS_PROPERTY_PATH = Arrays.asList(new String[] {
142
            "descriptions", //$NON-NLS-1$
143
            "descriptions.descriptionElements", //$NON-NLS-1$
144
            "descriptions.descriptionElements.inDescription", //$NON-NLS-1$
145
            "descriptions.descriptionElements.inDescription.descriptionElements", //$NON-NLS-1$
146
            "descriptions.descriptionElements.feature", //$NON-NLS-1$
147
    });
133 148

  
134 149
    private static final String CHARACTER_MATRIX_STATE_PROPERTIES = "characterMatrixState.properties";
135 150

  
......
145 160

  
146 161
    private ConversationHolder conversation;
147 162

  
163
    private ICdmEntitySession cdmEntitySession;
164

  
148 165
    @Inject
149 166
    private ESelectionService selService;
150 167

  
......
169 186
        if(CdmStore.isActive() && conversation==null){
170 187
            conversation = CdmStore.createConversation();
171 188
        }
189
        if(cdmEntitySession == null){
190
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
191
        }
172 192
        else{
173 193
            return;
174 194
        }
......
178 198

  
179 199

  
180 200
    public void init(UUID workingSetUuid) {
181
        this.workingSet = CdmStore.getService(IWorkingSetService.class).load(workingSetUuid);
201
        this.workingSet = CdmStore.getService(IWorkingSetService.class).load(workingSetUuid, WS_PROPERTY_PATH);
182 202
        thisPart.setLabel(workingSet.getLabel());
183 203

  
184 204
        Composite toolbarComposite = new Composite(parent, SWT.NONE);
......
518 538
        btnAddDescription.addSelectionListener(new SelectionAdapter() {
519 539
            @Override
520 540
            public void widgetSelected(SelectionEvent e) {
521
                DescriptionSelectionDialog dialog = new DescriptionSelectionDialog(natTable.getShell(), workingSet);
541
                SpecimenSelectionDialog dialog = new SpecimenSelectionDialog(natTable.getShell(), workingSet);
522 542
                if(dialog.open()==Window.OK){
523
                    Collection<DescriptionBase> selectedDescriptions = dialog.getDescriptions();
543
                    Collection<SpecimenOrObservationBase> specimens = dialog.getSpecimen();
524 544
                    boolean hasAdded = false;
525
                    for (DescriptionBase description : selectedDescriptions) {
545
                    for (SpecimenOrObservationBase specimen : specimens) {
546
                        SpecimenDescription description = getDescriptionForWorkingSet(specimen);
526 547
                        if(!workingSet.getDescriptions().contains(description)){
527
                            CharacterMatrix.this.descriptions.add(new RowWrapper((SpecimenDescription) description));
548
                            CharacterMatrix.this.descriptions.add(new RowWrapper(description));
528 549
                            workingSet.addDescription(description);
529 550
                            hasAdded = true;
530 551
                        }
......
535 556
                }
536 557
            }
537 558
        });
538

  
539 559
        parent.layout();
540 560
    }
541 561

  
562
    private SpecimenDescription getDescriptionForWorkingSet(SpecimenOrObservationBase specimen){
563
        Set<SpecimenDescription> descriptions = specimen.getDescriptions();
564
        if(descriptions!=null){
565
            Set<Feature> features = workingSet.getDescriptiveSystem().getDistinctFeatures();
566
            for (SpecimenDescription specimenDescription : descriptions) {
567
                Set<Feature> specimenDescriptionFeatures = new HashSet<>();
568
                for (DescriptionElementBase descriptionElementBase : specimenDescription.getElements()) {
569
                    specimenDescriptionFeatures.add(descriptionElementBase.getFeature());
570
                }
571
                if(specimenDescriptionFeatures.containsAll(features)){
572
                    return specimenDescription;
573
                }
574
            }
575
        }
576
        //Create new specimen description if no match was found
577
        setDirty();
578
        SpecimenDescription newDesription = SpecimenDescription.NewInstance(specimen);
579
        newDesription.setTitleCache("WorkingSet "+workingSet.getLabel()+" "+newDesription.generateTitle(), true);
580
        return newDesription;
581

  
582
    }
583

  
542 584
    private void initLabels(final ColumnOverrideLabelAccumulator columnLabelAccumulator,
543 585
            int index, Feature feature) {
544 586

  
......
657 699
        if(conversation!=null){
658 700
            conversation.bind();
659 701
        }
702
        if(cdmEntitySession != null) {
703
            cdmEntitySession.bind();
704
        }
660 705
    }
661 706

  
662 707
    @PreDestroy
663 708
    public void dispose(){
664
        if(conversation!=null){
709
        if (conversation != null) {
665 710
            conversation.close();
666 711
            conversation = null;
667 712
        }
668
        try (FileOutputStream tableStateStream =
669
                new FileOutputStream(getStatePropertiesFile())) {
670
            natTableState.store(tableStateStream, null);
671
        } catch (IOException ioe) {
672
            ioe.printStackTrace();
713
        if(cdmEntitySession != null) {
714
            cdmEntitySession.dispose();
715
            cdmEntitySession = null;
716
        }
717
        dirty.setDirty(false);
718
        if(natTableState!=null){
719
            try (FileOutputStream tableStateStream =
720
                    new FileOutputStream(getStatePropertiesFile())) {
721
                natTableState.store(tableStateStream, null);
722
            } catch (IOException ioe) {
723
                ioe.printStackTrace();
724
            }
673 725
        }
674 726
    }
675 727

  
676

  
677 728
    private File getStatePropertiesFile() {
678 729
        return new File(WorkbenchUtility.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES);
679 730
    }
680 731

  
681

  
682 732
    /**
683 733
     * {@inheritDoc}
684 734
     */
......
686 736
    public void update(CdmDataChangeMap arg0) {
687 737
    }
688 738

  
689

  
690 739
    /**
691 740
     * {@inheritDoc}
692 741
     */
......
695 744
        return conversation;
696 745
    }
697 746

  
698

  
699 747
    /**
700 748
     * {@inheritDoc}
701 749
     */
702 750
    @Override
703 751
    public void changed(Object element) {
704
        dirty.setDirty(true);
752
        setDirty();
705 753
        natTable.refresh();
706 754
    }
707 755

  
708

  
709 756
    /**
710 757
     * {@inheritDoc}
711 758
     */
712 759
    @Override
713 760
    public void forceDirty() {
714
        dirty.setDirty(true);
761
        setDirty();
762
    }
763

  
764

  
765
    /**
766
     * {@inheritDoc}
767
     */
768
    @Override
769
    public ICdmEntitySession getCdmEntitySession() {
770
        return cdmEntitySession;
771
    }
772

  
773

  
774
    /**
775
     * {@inheritDoc}
776
     */
777
    @Override
778
    public Collection<WorkingSet> getRootEntities() {
779
        return Collections.singleton(this.workingSet);
780
    }
781

  
782

  
783
    /**
784
     * {@inheritDoc}
785
     */
786
    @Override
787
    public Map<Object, List<String>> getPropertyPathsMap() {
788
        Map<Object, List<String>> propertyMap = new HashMap<>();
789
        propertyMap.put(SpecimenOrObservationBase.class,WS_PROPERTY_PATH);
790
        return propertyMap;
715 791
    }
716 792

  
717 793
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/DescriptionSelectionDialog.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.editor.workingSet.matrix;
10

  
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

  
17
import org.eclipse.jface.dialogs.Dialog;
18
import org.eclipse.jface.viewers.ArrayContentProvider;
19
import org.eclipse.jface.viewers.IStructuredSelection;
20
import org.eclipse.jface.viewers.LabelProvider;
21
import org.eclipse.jface.viewers.TableViewer;
22
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Control;
25
import org.eclipse.swt.widgets.Shell;
26

  
27
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
28
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29
import eu.etaxonomy.cdm.model.description.DescriptionBase;
30
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
31
import eu.etaxonomy.cdm.model.description.WorkingSet;
32
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35
import eu.etaxonomy.taxeditor.model.ImageResources;
36
import eu.etaxonomy.taxeditor.store.CdmStore;
37

  
38
/**
39
 * @author pplitzner
40
 * @since Jan 3, 2018
41
 *
42
 */
43
public class DescriptionSelectionDialog extends Dialog {
44

  
45
    private TableViewer list;
46

  
47
    private WorkingSet workingSet;
48

  
49
    Collection<DescriptionBase> selectedDescriptions = new ArrayList<>();
50

  
51
    protected DescriptionSelectionDialog(Shell parentShell, WorkingSet workingSet) {
52
        super(parentShell);
53
        this.workingSet = workingSet;
54
    }
55

  
56
    @Override
57
    protected Control createDialogArea(Composite parent) {
58
        Composite composite = (Composite) super.createDialogArea(parent);
59
        list = new TableViewer(composite);
60
        list.setContentProvider(new ArrayContentProvider());
61
        list.setLabelProvider(new DescriptionLabelProvider());
62

  
63
        //gather descriptions
64
        Set<TaxonNode> taxonNodes = workingSet.getTaxonSubtreeFilter();
65
        Set<SpecimenDescription> specimenDescriptions = new HashSet<>();
66
        for (TaxonNode node : taxonNodes) {
67
            specimenDescriptions.addAll(addDescriptionsRecursively(node));
68
        }
69
        list.setInput(specimenDescriptions);
70
        return composite;
71
    }
72

  
73
    private Set<SpecimenDescription> addDescriptionsRecursively(TaxonNode node){
74
        Set<SpecimenDescription> descriptions = new HashSet<>();
75
        if(node.getTaxon()!=null){
76
            Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
77
            Collection<SpecimenOrObservationBase> specimens = CdmStore.getService(IOccurrenceService.class).listByAssociatedTaxon(SpecimenOrObservationBase.class, null, taxon, null, null, null, null, null);
78
            for (SpecimenOrObservationBase specimen : specimens) {
79
                Set<SpecimenDescription> specimenDescriptions = specimen.getSpecimenDescriptions();
80
                for (SpecimenDescription specimenDescription : specimenDescriptions) {
81
                    descriptions.add(specimenDescription);
82
                }
83
            }
84
        }
85
        List<TaxonNode> childNodes = node.getChildNodes();
86
        for (TaxonNode childNode : childNodes) {
87
            descriptions.addAll(addDescriptionsRecursively(childNode));
88
        }
89
        return descriptions;
90
    }
91

  
92
    /**
93
     * {@inheritDoc}
94
     */
95
    @Override
96
    protected void okPressed() {
97
        selectedDescriptions = ((IStructuredSelection)list.getSelection()).toList();
98
        super.okPressed();
99
    }
100
    public Collection<DescriptionBase> getDescriptions(){
101
        return selectedDescriptions;
102
    }
103

  
104
    private class DescriptionLabelProvider extends LabelProvider{
105
        /**
106
         * {@inheritDoc}
107
         */
108
        @Override
109
        public Image getImage(Object element) {
110
            if(workingSet.getDescriptions().contains(element)){
111
                return ImageResources.getImage(ImageResources.SYNCED);
112
            }
113
            return null;
114
        }
115
    }
116
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/SpecimenSelectionDialog.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.editor.workingSet.matrix;
10

  
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

  
17
import org.eclipse.jface.dialogs.Dialog;
18
import org.eclipse.jface.viewers.ArrayContentProvider;
19
import org.eclipse.jface.viewers.IStructuredSelection;
20
import org.eclipse.jface.viewers.LabelProvider;
21
import org.eclipse.jface.viewers.TableViewer;
22
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Control;
25
import org.eclipse.swt.widgets.Shell;
26

  
27
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
28
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29
import eu.etaxonomy.cdm.model.description.WorkingSet;
30
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33
import eu.etaxonomy.taxeditor.model.ImageResources;
34
import eu.etaxonomy.taxeditor.store.CdmStore;
35

  
36
/**
37
 * @author pplitzner
38
 * @since Jan 3, 2018
39
 *
40
 */
41
public class SpecimenSelectionDialog extends Dialog {
42

  
43
    private TableViewer list;
44

  
45
    private WorkingSet workingSet;
46

  
47
    Collection<SpecimenOrObservationBase> selectedSpecimens = new ArrayList<>();
48

  
49
    protected SpecimenSelectionDialog(Shell parentShell, WorkingSet workingSet) {
50
        super(parentShell);
51
        this.workingSet = workingSet;
52
    }
53

  
54
    @Override
55
    protected Control createDialogArea(Composite parent) {
56
        Composite composite = (Composite) super.createDialogArea(parent);
57
        list = new TableViewer(composite);
58
        list.setContentProvider(new ArrayContentProvider());
59
        list.setLabelProvider(new DescriptionLabelProvider());
60

  
61
        //gather descriptions
62
        Set<TaxonNode> taxonNodes = workingSet.getTaxonSubtreeFilter();
63
        Set<SpecimenOrObservationBase> specimens = new HashSet<>();
64
        for (TaxonNode node : taxonNodes) {
65
            specimens.addAll(addDescriptionsRecursively(node));
66
        }
67
        list.setInput(specimens);
68
        return composite;
69
    }
70

  
71
    private Collection<SpecimenOrObservationBase> addDescriptionsRecursively(TaxonNode node){
72
        Collection<SpecimenOrObservationBase> specimens = new HashSet<>();
73
        if(node.getTaxon()!=null){
74
            Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
75
            specimens = CdmStore.getService(IOccurrenceService.class).listByAssociatedTaxon(SpecimenOrObservationBase.class, null, taxon, null, null, null, null, null);
76
        }
77
        List<TaxonNode> childNodes = node.getChildNodes();
78
        for (TaxonNode childNode : childNodes) {
79
            specimens.addAll(addDescriptionsRecursively(childNode));
80
        }
81
        return specimens;
82
    }
83

  
84
    /**
85
     * {@inheritDoc}
86
     */
87
    @Override
88
    protected void okPressed() {
89
        selectedSpecimens = ((IStructuredSelection)list.getSelection()).toList();
90
        super.okPressed();
91
    }
92

  
93
    public Collection<SpecimenOrObservationBase> getSpecimen(){
94
        return selectedSpecimens;
95
    }
96

  
97
    private class DescriptionLabelProvider extends LabelProvider{
98
        /**
99
         * {@inheritDoc}
100
         */
101
        @Override
102
        public Image getImage(Object element) {
103
            if(workingSet.getDescriptions().contains(element)){
104
                return ImageResources.getImage(ImageResources.SYNCED);
105
            }
106
            return null;
107
        }
108
    }
109
}

Also available in: Unified diff