Project

General

Profile

Download (10.6 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

    
12

    
13

    
14
 import java.util.UUID;
15

    
16
import org.eclipse.swt.widgets.Control;
17
import org.eclipse.swt.widgets.Shell;
18
import org.eclipse.swt.widgets.Text;
19

    
20
import eu.etaxonomy.cdm.api.application.ICdmRepository;
21
import eu.etaxonomy.cdm.api.service.IReferenceService;
22
import eu.etaxonomy.cdm.model.agent.Person;
23
import eu.etaxonomy.cdm.model.agent.Team;
24
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
25
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
28
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
29
import eu.etaxonomy.taxeditor.newWizard.NewReferenceWizard;
30
import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
31
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
32
import eu.etaxonomy.taxeditor.store.CdmStore;
33

    
34

    
35
/**
36
 * @author k.luther
37
 * @date 02.05.2017
38
 *
39

    
40
 */
41
public class ExtReferenceSelectionDialog extends
42
    AbstractFilteredCdmResourceSelectionDialog<Reference> {
43

    
44
        protected static boolean isInReference = false;
45
        private Reference currentReference;
46
        ICdmRepository controller;
47
        ICdmRemoteSource remoteSource ;
48
        Reference remoteSourceRef;
49
        IReferenceService service = null;
50

    
51

    
52
        /**
53
         * <p>select</p>
54
         *
55
         * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
56
         * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
57
         * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
58
         * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
59
         */
60
        public static Reference select(Shell shell, //ConversationHolder conversation,
61
                Reference reference, boolean isInReference) {
62
            ExtReferenceSelectionDialog dialog = new ExtReferenceSelectionDialog(shell, //conversation,
63
                    "Choose a reference", false, reference, isInReference);
64
            return getSelectionFromDialog(dialog);
65
        }
66

    
67
        /**
68
         * <p>select</p>
69
         *
70
         * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
71
         * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
72
         * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
73
         * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
74
         */
75
        public static Reference select(Shell shell, //ConversationHolder conversation,
76
                Reference reference) {
77
            ExtReferenceSelectionDialog dialog = new ExtReferenceSelectionDialog(shell, //conversation,
78
                    "Choose a reference", false, reference);
79
            return getSelectionFromDialog(dialog);
80
        }
81

    
82

    
83

    
84

    
85

    
86

    
87
        /**
88
         * <p>Constructor for FilteredReferenceSelectionDialog.</p>
89
         *
90
         * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
91
         * @param title a {@link java.lang.String} object.
92
         * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
93
         * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
94
         * @param multi a boolean.
95
         */
96
        protected ExtReferenceSelectionDialog(Shell shell, //ConversationHolder conversation,
97
                String title, boolean multi, Reference reference) {
98
            super(shell, //conversation,
99
                    title, multi, ExtReferenceSelectionDialog.class.getCanonicalName(), null);
100
            this.currentReference = reference;
101

    
102
//            controller = CdmStore.getCurrentApplicationConfiguration();
103
//            String currentValue = System.getProperty("cdm.server.dev.port");
104
            System.setProperty("cdm.server.dev.port", "8080");
105
            remoteSource = CdmServerInfo.getDevServerRemoteSource();
106
            remoteSourceRef = ReferenceFactory.newDatabase();
107
            remoteSourceRef.setTitle(remoteSource.getName());
108
         System.setProperty("cdm.server.dev.port", "80");
109
        }
110

    
111

    
112

    
113

    
114
        /**
115
         * <p>Constructor for FilteredReferenceSelectionDialog.</p>
116
         *
117
         * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
118
         * @param title a {@link java.lang.String} object.
119
         * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
120
         * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
121
         * @param multi a boolean.
122
         */
123
        protected ExtReferenceSelectionDialog(Shell shell, //ConversationHolder conversation,
124
                String title, boolean multi, Reference reference, boolean isInReference) {
125
            super(shell, //conversation,
126
                    title, multi, ExtReferenceSelectionDialog.class.getCanonicalName(), null);
127
            this.isInReference = isInReference;
128
            this.currentReference = reference;
129

    
130
//            controller = CdmStore.getCurrentApplicationConfiguration();
131
//            CdmServerInfoConfig parameterObject = new CdmServerInfoConfig("localhost", "localhost", 8080, "cdmserver/", true);
132
//            CdmServerInfo selectedCsii = new CdmServerInfo(parameterObject);
133
//            CdmInstanceInfo selectedCdmInstance = selectedCsii.getInstanceFromName("local-cuba") ;
134
//            Integer port;
135
//            remoteSource = selectedCsii.getCdmRemoteSource(selectedCdmInstance, 80);
136

    
137

    
138
        }
139

    
140

    
141
        /* (non-Javadoc)
142
         * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#getPersistentObject(java.util.UUID)
143
         */
144
        /** {@inheritDoc} */
145
        @Override
146
        protected Reference getPersistentObject(UUID cdmUuid) {
147
            Reference extRef = service.load(cdmUuid);
148

    
149
            Reference newRef = ReferenceFactory.newReference( extRef.getType());
150
            if (extRef.getTitle() != null){
151
                newRef.setTitle(extRef.getTitle());
152
            }else{
153
                newRef.setTitleCache(extRef.getTitleCache(), true);
154
            }
155
            if (extRef.getAbbrevTitle() != null){
156
                newRef.setAbbrevTitle(extRef.getAbbrevTitle());
157
            }else{
158
                newRef.setAbbrevTitleCache(extRef.getAbbrevTitleCache(), true);
159
            }
160

    
161
            if (extRef.getInReference() != null){
162
                Reference inRef = extRef.getInReference();
163
                Reference newRefInRef = ReferenceFactory.newReference( extRef.getInBook().getType());
164
                if (inRef.getTitle() != null){
165
                    newRefInRef.setTitle(inRef.getTitle());
166
                }else{
167
                    newRefInRef.setTitleCache(inRef.getTitleCache(), true);
168
                }
169
                if (inRef.getAbbrevTitle() != null){
170
                    newRefInRef.setAbbrevTitle(inRef.getAbbrevTitle());
171
                }else{
172
                    newRefInRef.setAbbrevTitleCache(inRef.getAbbrevTitleCache(), true);
173
                }
174
                newRefInRef.addSource(OriginalSourceType.Other, String.valueOf(newRefInRef.getId()), "Reference", remoteSourceRef, null);
175
                newRef.setInReference(newRefInRef);
176
                newRef.setProtectedTitleCache(false);
177
                newRef.setProtectedAbbrevTitleCache(false);
178
                newRef.addSource(OriginalSourceType.Other, String.valueOf(newRef.getId()), "Reference", remoteSourceRef, null);
179
            }
180
            TeamOrPersonBase author;
181
            if (extRef.getAuthorship() != null){
182
                if (extRef.getAuthorship() instanceof Person){
183
                     author = Person.NewTitledInstance(extRef.getAuthorship().getTitleCache());
184
                     author.setProtectedTitleCache(true);
185
                }else{
186
                   author = Team.NewInstance();
187
                   Person newMember;
188

    
189
                   for (Person member:((Team)extRef.getAuthorship()).getTeamMembers()){
190
                       newMember = Person.NewTitledInstance(member.getTitleCache());
191
                       ((Team)author).addTeamMember(newMember);
192
                   }
193

    
194
                }
195
                newRef.setAuthorship(author);
196
            }
197

    
198
            newRef = CdmStore.getService(IReferenceService.class).save(newRef);
199

    
200
            return newRef;
201
        }
202

    
203
        /* (non-Javadoc)
204
         * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
205
         */
206
        /** {@inheritDoc} */
207
        @Override
208
        protected void search() {
209
            Control control =getSearchField();
210
            String pattern = null;
211
            if (control != null){
212
                pattern = ((Text)control).getText();
213
            }
214

    
215
            if (remoteSource != null){
216
//               if (service == null){
217
//                   service = CdmApplicationRemoteConfiguration.getExtReferenceService(remoteSource);
218
//               }
219
               if (pattern == null || pattern.equals("?")){ //$NON-NLS-1$
220
                    if (isInReference && currentReference != null){
221
                        model = service.getUuidAndTitleCache(null, null, currentReference.getType());
222
                    }else{
223
                        model = service.getUuidAndTitleCache(null, null);
224
                    }
225
                }else{
226
                    if (isInReference && currentReference != null){
227
                        model = service.getUuidAndTitleCache(limitOfInitialElements, pattern, currentReference.getType());
228
                    }else{
229
                        model = service.getUuidAndTitleCache(limitOfInitialElements,pattern);
230

    
231
                    }
232
                }
233
            }
234
        }
235

    
236

    
237
        /** {@inheritDoc} */
238
        @Override
239
        protected String getTitle(Reference cdmObject) {
240
            if(cdmObject == null){
241
                return "";
242
            }else{
243
                return super.getTitle(cdmObject);
244
            }
245
//          }else{
246
//              return DefaultReferenceCacheStrategy.putAuthorToEndOfString(cdmObject.getTitleCache(), cdmObject.getAuthorship().getTitleCache());
247
//          }
248
        }
249

    
250
        /** {@inheritDoc} */
251
        @Override
252
        protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
253
            return new NewReferenceWizard();
254
        }
255

    
256
        /** {@inheritDoc} */
257
        @Override
258
        protected String[] getNewWizardText() {
259
            return new String[]{"Reference "};
260
        }
261

    
262
    }
263

    
264

    
(10-10/39)