Project

General

Profile

Download (7.13 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.ui.dialog.selection;
10

    
11
import java.util.UUID;
12

    
13
import org.eclipse.swt.widgets.Shell;
14

    
15
import eu.etaxonomy.cdm.api.service.IReferenceService;
16
import eu.etaxonomy.cdm.config.ICdmSource;
17
import eu.etaxonomy.cdm.model.agent.Person;
18
import eu.etaxonomy.cdm.model.agent.Team;
19
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
20
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
21
import eu.etaxonomy.cdm.model.reference.Reference;
22
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
23
import eu.etaxonomy.taxeditor.newWizard.NewReferenceWizard;
24
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
25
import eu.etaxonomy.taxeditor.store.CdmStore;
26

    
27
/**
28
 * @author k.luther
29
 * @date 02.05.2017
30
 */
31
public class ExtReferenceSelectionDialog extends
32
        AbstractFilteredCdmResourceSelectionDialog<Reference> {
33

    
34
    protected static boolean isInReference = false;
35
    private Reference currentReference;
36
    private ICdmSource remoteSource ;
37
    private Reference remoteSourceRef;
38
    private IReferenceService service = null;
39

    
40
    public static Reference select(Shell shell,
41
            Reference reference, boolean isInReference) {
42
        ExtReferenceSelectionDialog dialog = new ExtReferenceSelectionDialog(shell,
43
                "Choose a reference", false, reference, isInReference);
44
        return getSelectionFromDialog(dialog);
45
    }
46

    
47
    public static Reference select(Shell shell,
48
            Reference reference) {
49
        ExtReferenceSelectionDialog dialog = new ExtReferenceSelectionDialog(shell,
50
                "Choose a reference", false, reference);
51
        return getSelectionFromDialog(dialog);
52
    }
53

    
54
    protected ExtReferenceSelectionDialog(Shell shell,
55
            String title, boolean multi, Reference reference) {
56
        super(shell, title, multi, ExtReferenceSelectionDialog.class.getCanonicalName());
57
        this.currentReference = reference;
58
//            controller = CdmStore.getCurrentApplicationConfiguration();
59
        remoteSource = CdmServerInfo.getDevServerRemoteSource();
60
        remoteSourceRef = ReferenceFactory.newDatabase();
61
        remoteSourceRef.setTitle(remoteSource.getName());
62
    }
63

    
64
    protected ExtReferenceSelectionDialog(Shell shell,
65
            String title, boolean multi, Reference reference, boolean isInReference) {
66
        super(shell, title, multi, ExtReferenceSelectionDialog.class.getCanonicalName());
67
        ExtReferenceSelectionDialog.isInReference = isInReference;
68
        this.currentReference = reference;
69

    
70
//            controller = CdmStore.getCurrentApplicationConfiguration();
71
//            CdmServerInfoConfig parameterObject = new CdmServerInfoConfig("localhost", "localhost", 8080, "cdmserver/", true);
72
//            CdmServerInfo selectedCsii = new CdmServerInfo(parameterObject);
73
//            CdmInstanceInfo selectedCdmInstance = selectedCsii.getInstanceFromName("local-cuba") ;
74
//            Integer port;
75
//            remoteSource = selectedCsii.getCdmRemoteSource(selectedCdmInstance, 80);
76
    }
77

    
78
    @Override
79
    protected Reference getPersistentObject(UUID cdmUuid) {
80
        Reference extRef = service.load(cdmUuid);
81

    
82
        Reference newRef = ReferenceFactory.newReference( extRef.getType());
83
        if (extRef.getTitle() != null){
84
            newRef.setTitle(extRef.getTitle());
85
        }else{
86
            newRef.setTitleCache(extRef.getTitleCache(), true);
87
        }
88
        if (extRef.getAbbrevTitle() != null){
89
            newRef.setAbbrevTitle(extRef.getAbbrevTitle());
90
        }else{
91
            newRef.setAbbrevTitleCache(extRef.getAbbrevTitleCache(), true);
92
        }
93

    
94
        if (extRef.getInReference() != null){
95
            Reference inRef = extRef.getInReference();
96
            Reference newRefInRef = ReferenceFactory.newReference( extRef.getInBook().getType());
97
            if (inRef.getTitle() != null){
98
                newRefInRef.setTitle(inRef.getTitle());
99
            }else{
100
                newRefInRef.setTitleCache(inRef.getTitleCache(), true);
101
            }
102
            if (inRef.getAbbrevTitle() != null){
103
                newRefInRef.setAbbrevTitle(inRef.getAbbrevTitle());
104
            }else{
105
                newRefInRef.setAbbrevTitleCache(inRef.getAbbrevTitleCache(), true);
106
            }
107
            newRefInRef.addSource(OriginalSourceType.Other, String.valueOf(newRefInRef.getId()), "Reference", remoteSourceRef, null);
108
            newRef.setInReference(newRefInRef);
109
            newRef.setProtectedTitleCache(false);
110
            newRef.setProtectedAbbrevTitleCache(false);
111
            newRef.addSource(OriginalSourceType.Other, String.valueOf(newRef.getId()), "Reference", remoteSourceRef, null);
112
        }
113
        TeamOrPersonBase<?> author;
114
        if (extRef.getAuthorship() != null){
115
            if (extRef.getAuthorship() instanceof Person){
116
                 author = Person.NewTitledInstance(extRef.getAuthorship().getTitleCache());
117
                 author.setProtectedTitleCache(true);
118
            }else{
119
               author = Team.NewInstance();
120
               Person newMember;
121

    
122
               for (Person member:((Team)extRef.getAuthorship()).getTeamMembers()){
123
                   newMember = Person.NewTitledInstance(member.getTitleCache());
124
                   ((Team)author).addTeamMember(newMember);
125
               }
126
            }
127
            newRef.setAuthorship(author);
128
        }
129
        newRef = CdmStore.getService(IReferenceService.class).save(newRef);
130
        return newRef;
131
    }
132

    
133
    @Override
134
    protected void callService(String pattern) {
135

    
136
        if (remoteSource != null){
137
//               if (service == null){
138
//                   service = CdmApplicationRemoteConfiguration.getExtReferenceService(remoteSource);
139
//               }
140
           if (pattern.equals("?")){ //$NON-NLS-1$
141
                if (isInReference && currentReference != null){
142
                    model = service.getUuidAndTitleCache(null, null, currentReference.getType());
143
                }else{
144
                    model = service.getUuidAndTitleCache(null, null);
145
                }
146
            }else{
147
                if (isInReference && currentReference != null){
148
                    model = service.getUuidAndTitleCache(limitOfInitialElements, pattern, currentReference.getType());
149
                }else{
150
                    model = service.getUuidAndTitleCache(limitOfInitialElements,pattern);
151
                }
152
            }
153
         }
154
    }
155

    
156
    @Override
157
    protected String getTitle(Reference cdmObject) {
158
        if(cdmObject == null){
159
            return "";
160
        }else{
161
            return super.getTitle(cdmObject);
162
        }
163
//          }else{
164
//              return DefaultReferenceCacheStrategy.putAuthorToEndOfString(cdmObject.getTitleCache(), cdmObject.getAuthorship().getTitleCache());
165
//          }
166
    }
167

    
168
    @Override
169
    protected NewReferenceWizard getNewEntityWizard(String parameter) {
170
        return new NewReferenceWizard(null);
171
    }
172

    
173
    @Override
174
    protected String[] getNewWizardText() {
175
        return new String[]{"Reference "};
176
    }
177
}
(14-14/46)