Project

General

Profile

Download (10.3 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.Shell;
17

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

    
32

    
33
/**
34
 * @author k.luther
35
 * @date 02.05.2017
36
 *
37

    
38
 */
39
public class ExtReferenceSelectionDialog extends
40
    AbstractFilteredCdmResourceSelectionDialog<Reference> {
41

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

    
49

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

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

    
80

    
81

    
82

    
83

    
84

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

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

    
109

    
110

    
111

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

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

    
135

    
136
        }
137

    
138

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

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

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

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

    
192
                }
193
                newRef.setAuthorship(author);
194
            }
195

    
196
            newRef = CdmStore.getService(IReferenceService.class).save(newRef);
197

    
198
            return newRef;
199
        }
200

    
201
        /* (non-Javadoc)
202
         * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
203
         */
204
        /** {@inheritDoc} */
205
        @Override
206
        protected void callService(String pattern) {
207

    
208

    
209
            if (remoteSource != null){
210
//               if (service == null){
211
//                   service = CdmApplicationRemoteConfiguration.getExtReferenceService(remoteSource);
212
//               }
213
               if (pattern.equals("?")){ //$NON-NLS-1$
214
                    if (isInReference && currentReference != null){
215
                        model = service.getUuidAndTitleCache(null, null, currentReference.getType());
216
                    }else{
217
                        model = service.getUuidAndTitleCache(null, null);
218
                    }
219
                }else{
220
                    if (isInReference && currentReference != null){
221
                        model = service.getUuidAndTitleCache(limitOfInitialElements, pattern, currentReference.getType());
222
                    }else{
223
                        model = service.getUuidAndTitleCache(limitOfInitialElements,pattern);
224

    
225
                    }
226
                }
227
             }
228
        }
229

    
230

    
231
        /** {@inheritDoc} */
232
        @Override
233
        protected String getTitle(Reference cdmObject) {
234
            if(cdmObject == null){
235
                return "";
236
            }else{
237
                return super.getTitle(cdmObject);
238
            }
239
//          }else{
240
//              return DefaultReferenceCacheStrategy.putAuthorToEndOfString(cdmObject.getTitleCache(), cdmObject.getAuthorship().getTitleCache());
241
//          }
242
        }
243

    
244
        /** {@inheritDoc} */
245
        @Override
246
        protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
247
            return new NewReferenceWizard();
248
        }
249

    
250
        /** {@inheritDoc} */
251
        @Override
252
        protected String[] getNewWizardText() {
253
            return new String[]{"Reference "};
254
        }
255

    
256
    }
257

    
258

    
(10-10/41)