Project

General

Profile

Download (28.5 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.cdm.app.berlinModelImport;
11

    
12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.io.InputStreamReader;
15
import java.io.Reader;
16
import java.util.Arrays;
17
import java.util.HashMap;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Set;
21
import java.util.UUID;
22

    
23
import org.apache.log4j.Logger;
24
import org.springframework.security.core.GrantedAuthority;
25
import org.springframework.transaction.TransactionStatus;
26

    
27
import eu.etaxonomy.cdm.api.application.FirstDataInserter;
28
import eu.etaxonomy.cdm.api.application.ICdmRepository;
29
import eu.etaxonomy.cdm.api.service.IGroupService;
30
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution;
31
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution.AggregationMode;
32
import eu.etaxonomy.cdm.api.service.pager.Pager;
33
import eu.etaxonomy.cdm.app.common.CdmDestinations;
34
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
35
import eu.etaxonomy.cdm.database.DbSchemaValidation;
36
import eu.etaxonomy.cdm.database.ICdmDataSource;
37
import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
38
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
39
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
40
import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
41
import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
42
import eu.etaxonomy.cdm.io.common.IImportConfigurator.DO_REFERENCES;
43
import eu.etaxonomy.cdm.io.common.IImportConfigurator.EDITOR;
44
import eu.etaxonomy.cdm.io.common.Source;
45
import eu.etaxonomy.cdm.model.agent.Person;
46
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
47
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
48
import eu.etaxonomy.cdm.model.common.Group;
49
import eu.etaxonomy.cdm.model.common.Language;
50
import eu.etaxonomy.cdm.model.common.Representation;
51
import eu.etaxonomy.cdm.model.common.User;
52
import eu.etaxonomy.cdm.model.description.Feature;
53
import eu.etaxonomy.cdm.model.description.FeatureNode;
54
import eu.etaxonomy.cdm.model.description.FeatureTree;
55
import eu.etaxonomy.cdm.model.location.NamedArea;
56
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
57
import eu.etaxonomy.cdm.model.location.NamedAreaType;
58
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
59
import eu.etaxonomy.cdm.model.name.Rank;
60
import eu.etaxonomy.cdm.model.taxon.Taxon;
61
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
62
import eu.etaxonomy.cdm.persistence.hibernate.permission.Role;
63
import eu.etaxonomy.cdm.persistence.query.MatchMode;
64
import eu.etaxonomy.cdm.persistence.query.OrderHint;
65

    
66

    
67
/**
68
 * TODO add the following to a wiki page:
69
 * HINT: If you are about to import into a mysql data base running under windows and if you wish to
70
 * dump and restore the resulting data bas under another operation systen
71
 * you must set the mysql system variable lower_case_table_names = 0 in order to create data base with table compatible names.
72
 *
73
 * @author a.mueller
74
 */
75
public class EuroMedActivator {
76
	private static final Logger logger = Logger.getLogger(EuroMedActivator.class);
77

    
78
	//database validation status (create, update, validate ...)
79
	static DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
80
//    static final Source berlinModelSource = BerlinModelSources.euroMed_Pub2();
81
	static final Source berlinModelSource = BerlinModelSources.euroMed_BGBM42();
82
//	static final Source berlinModelSource = BerlinModelSources.euroMed_PESI3();
83
//
84
  static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
85
//    static final ICdmDataSource cdmDestination = CdmDestinations.cdm_local_euromed();
86
//    static final ICdmDataSource cdmDestination = CdmDestinations.cdm_local_euromed2();
87
//	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_euroMed();
88

    
89
    //check - import
90
    static final CHECK check = CHECK.CHECK_ONLY;
91

    
92
    static final boolean doUser = true;
93
//  //authors
94
    static final boolean doAuthors = true;
95
    //references
96
    static final DO_REFERENCES doReferences =  DO_REFERENCES.ALL;
97
    //names
98
    static final boolean doTaxonNames = true;
99
    static final boolean doRelNames = true;
100
    static final boolean doNameStatus = true;
101
    static final boolean doNameFacts = true;
102

    
103
    //taxa
104
    static final boolean doTaxa = true;
105
    static final boolean doFacts = true;
106
    static final boolean doRelTaxa = true;
107
    static final boolean doOccurrences = false;
108
    static final boolean doOccurrenceSources = true;
109
    static final boolean doCommonNames = true;  //currently takes very long
110

    
111
  //serious types do not exist in E+M except for name types which are handled in name relations
112
    static final boolean doTypes = false;  //serious types do not exist in E+M except for name types which are handled in name relations
113

    
114

    
115
    static final boolean doRunTransmissionEngine = false; // (hbm2dll == DbSchemaValidation.VALIDATE);
116

    
117
    //etc.
118
    static final boolean doMarker = false;  //no relevant markers exist
119

    
120
    boolean invers = !(hbm2dll == DbSchemaValidation.CREATE);
121

    
122
    boolean doPreliminaryRefDetailsWithNames = true;
123

    
124
    boolean logNotMatchingOldNames = false;
125
    boolean logMatchingNotExportedOldNames = false;  //true
126
    boolean checkOldNameIsSynonym = true;
127

    
128
	static final boolean includePesiExport = false;
129

    
130
	static final int sourceSecId = 7000000; //500000
131
	static final UUID classificationUuid = UUID.fromString("314a68f9-8449-495a-91c2-92fde8bcf344");
132
	static final boolean useSingleClassification = true;
133
	static final String classificationName = "Euro+Med 2018";
134
	static final UUID featureTreeUuid = UUID.fromString("6a5e1c2b-ec0d-46c8-9c7d-a2059267ffb7");
135
	static final Object[] featureKeyList = new Integer[]{1, 31, 4, 98, 41};
136

    
137
	// set to zero for unlimited nameFacts
138
	static final int maximumNumberOfNameFacts = 0;
139

    
140
	static final int partitionSize = 2500;
141

    
142

    
143
	//editor - import
144
	static final EDITOR editor = EDITOR.EDITOR_AS_EDITOR;
145

    
146
	//NomenclaturalCode
147
	static final NomenclaturalCode nomenclaturalCode = NomenclaturalCode.ICNAFP;
148

    
149
	//ignore null
150
	static final boolean ignoreNull = true;
151

    
152
	static final boolean switchSpeciesGroup = true;
153

    
154
	static boolean useClassification = true;
155

    
156
	static boolean isSplitTdwgCodes = false;
157
	static boolean useEmAreaVocabulary = true;
158

    
159
	private final boolean removeHttpMapsAnchor = true;
160

    
161

    
162
	static final String infrGenericRankAbbrev = "[unranked]";
163
	static final String infrSpecificRankAbbrev = "[unranked]";
164

    
165
	static boolean useLastScrutinyAsSec = true;
166
	static boolean warnForDifferingSynonymReference = false;
167

    
168

    
169
	static String taxonTable = "v_cdm_exp_taxaAll";
170
	static String classificationQuery = " SELECT DISTINCT t.PTRefFk, r.RefCache FROM PTaxon t INNER JOIN Reference r ON t.PTRefFk = r.RefId WHERE t.PTRefFk = " + sourceSecId;
171
	static String relPTaxonIdQuery = " SELECT r.RelPTaxonId "
172
					+ " FROM RelPTaxon AS r "
173
					+ "   INNER JOIN v_cdm_exp_taxaDirect AS a ON r.PTNameFk2 = a.PTNameFk AND r.PTRefFk2 = a.PTRefFk "
174
					+ "   INNER JOIN PTaxon As pt1 ON pt1.PTNameFk = r.PTNameFk1 AND pt1.PTRefFk = r.PTRefFk1 "
175
					+ " WHERE r.RelPTaxonID NOT IN (1874890,1874959,1874932,1874793,1874956,1874971,1874902,1874696) " //Relations to unpublished Kew genus taxa of Bethulaceae which are not imported anymore, but Bethalaceae is still imported
176
					+ "     AND NOT (pt1.PTRefFk = 8000000 AND pt1.publishFlag = 0) ";
177
	static String nameIdTable = " v_cdm_exp_namesAll ";
178
	static String referenceIdTable = " v_cdm_exp_refAll ";
179
	static String refDetailFilter =  " RefDetailID IN (SELECT RefDetailID FROM v_cdm_exp_RefDetail) ";
180
	static String factFilter = " factId IN ( SELECT factId FROM v_cdm_exp_factsAll WHERE FactCategoryFk NOT IN (12, 14, 249, 251))";
181
	static String occurrenceFilter = " occurrenceId IN ( SELECT occurrenceId FROM v_cdm_exp_occurrenceAll )";
182
	static String occurrenceSourceFilter = " occurrenceFk IN ( SELECT occurrenceId FROM v_cdm_exp_occurrenceAll )";
183
	static String commonNameFilter = " commonNameId IN ( SELECT commonNameId FROM v_cdm_exp_commonNamesAll )";
184
	static String webMarkerFilter = " TableNameFk <> 500 OR ( RIdentifierFk IN (SELECT RIdentifier FROM v_cdm_exp_taxaAll)) ";
185
	static String authorTeamFilter = null;  //*/ " authorTeamId IN (SELECT authorTeamId FROM v_cdm_exp_authorTeamsAll) ";
186
	static String authorFilter =  null;  //*/ " authorId IN (SELECT authorId FROM v_cdm_exp_authorsAll) ";
187

    
188

    
189

    
190
// **************** ALL *********************
191

    
192

    
193

    
194

    
195
	public void importEm2CDM (Source source, ICdmDataSource destination, DbSchemaValidation hbm2dll){
196
		System.out.println("Start import from BerlinModel("+ berlinModelSource.getDatabase() + ") to " + cdmDestination.getDatabase() + " ...");
197
		//make BerlinModel Source
198

    
199
		BerlinModelImportConfigurator config = BerlinModelImportConfigurator.NewInstance(source,  destination);
200

    
201
		config.setClassificationName(classificationName);
202

    
203
		config.setClassificationUuid(classificationUuid);
204
		config.setSourceSecId(sourceSecId);
205
		config.setNomenclaturalCode(nomenclaturalCode);
206
		config.setIgnoreNull(ignoreNull);
207

    
208
		config.setDoAuthors(doAuthors ^ invers);
209
		config.setDoReferences(invers ? doReferences.invers() : doReferences);
210
		config.setDoTaxonNames(doTaxonNames ^ invers);
211
		config.setDoRelNames(doRelNames ^ invers);
212
		config.setDoNameStatus(doNameStatus ^ invers);
213
		config.setDoTypes(doTypes);  //always false
214
		config.setDoNameFacts(doNameFacts ^ invers);
215
		config.setDoTaxa(doTaxa ^ invers);
216
		config.setDoRelTaxa(doRelTaxa ^ invers);
217
		config.setDoFacts(doFacts ^ invers);
218
		config.setDoOccurrence(doOccurrences ^ invers);
219
		config.setDoOccurrenceSources(doOccurrenceSources ^ invers);
220
        config.setDoCommonNames(doCommonNames ^ invers);
221

    
222
		config.setDoMarker(doMarker);
223
		config.setDoUser(doUser ^ invers);
224

    
225
		config.setEuroMed(true);
226

    
227
		config.setLogNotMatchingOldNames(logNotMatchingOldNames);
228
		config.setLogMatchingNotExportedOldNames(logMatchingNotExportedOldNames);
229
		config.setCheckOldNameIsSynonym(checkOldNameIsSynonym);
230

    
231
		config.setUseClassification(useClassification);
232
		config.setSourceRefUuid(BerlinModelTransformer.uuidSourceRefEuroMed);
233
		config.setEditor(editor);
234
		config.setDbSchemaValidation(hbm2dll);
235
		config.setUseLastScrutinyAsSec(useLastScrutinyAsSec);
236
		config.setWarnForDifferingSynonymReference(warnForDifferingSynonymReference);
237

    
238
		// maximum number of name facts to import
239
		config.setMaximumNumberOfNameFacts(maximumNumberOfNameFacts);
240

    
241
		config.setInfrGenericRankAbbrev(infrGenericRankAbbrev);
242
		config.setInfrSpecificRankAbbrev(infrSpecificRankAbbrev);
243
		config.setRemoveHttpMapsAnchor(removeHttpMapsAnchor);
244

    
245
		config.setDoPreliminaryRefDetailsWithNames(doPreliminaryRefDetailsWithNames);
246

    
247
//		filter
248
		config.setTaxonTable(taxonTable);
249
		config.setClassificationQuery(classificationQuery);
250
		config.setRelTaxaIdQuery(relPTaxonIdQuery);
251
		config.setNameIdTable(nameIdTable);
252
		config.setReferenceIdTable(referenceIdTable);
253
		config.setAuthorTeamFilter(authorTeamFilter);
254
		config.setAuthorFilter(authorFilter);
255
		config.setFactFilter(factFilter);
256
		config.setRefDetailFilter(refDetailFilter);
257
		config.setCommonNameFilter(commonNameFilter);
258
		config.setOccurrenceFilter(occurrenceFilter);
259
		config.setOccurrenceSourceFilter(occurrenceSourceFilter);
260
		config.setWebMarkerFilter(webMarkerFilter);
261
		config.setUseSingleClassification(useSingleClassification);
262

    
263
		//TDWG codes
264
		config.setSplitTdwgCodes(isSplitTdwgCodes);
265
		config.setUseEmAreaVocabulary(useEmAreaVocabulary);
266

    
267
		config.setCheck(check);
268
		config.setEditor(editor);
269
		config.setRecordsPerTransaction(partitionSize);
270

    
271
		config.setSwitchSpeciesGroup(switchSpeciesGroup);
272

    
273
		// invoke import
274
		CdmDefaultImport<BerlinModelImportConfigurator> bmImport = new CdmDefaultImport<>();
275
		bmImport.invoke(config);
276

    
277
		renameRanks(config, bmImport);
278

    
279
		createFeatureTree(config, bmImport);
280

    
281
		changeCommonNameLabel(config, bmImport);
282

    
283
		createUsersAndRoles(config, bmImport);
284

    
285
        runTransmissionEngine(config, bmImport);
286

    
287
        importShapefile(config, bmImport);
288

    
289
//        markAreasAsHidden(config, bmImport);  //has been moved to BM occurrence import
290

    
291
		System.out.println("End import from BerlinModel ("+ source.getDatabase() + ")...");
292
	}
293

    
294
	//Rename Ranks (still needed?)
295
    private void renameRanks(BerlinModelImportConfigurator config,
296
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
297

    
298
        if (config.isDoTaxonNames() && (config.getCheck().isImport() )  ){
299
			ICdmRepository app = bmImport.getCdmAppController();
300
			TransactionStatus tx = app.startTransaction();
301
			try {
302
				Rank sectBot = (Rank)app.getTermService().find(Rank.SECTION_BOTANY().getUuid());
303
				Representation repr = sectBot.getRepresentation(Language.ENGLISH());
304
				repr.setAbbreviatedLabel(repr.getAbbreviatedLabel().replace("(bot.)", "").trim());
305
				repr.setLabel(repr.getLabel().replace("(Botany)", "").trim());
306
				sectBot.setTitleCache(null, false);  //to definitely update the titleCache also
307
				app.getTermService().saveOrUpdate(sectBot);
308

    
309
				Rank subSectBot = (Rank)app.getTermService().find(Rank.SECTION_BOTANY().getUuid());
310
				repr = subSectBot.getRepresentation(Language.ENGLISH());
311
				repr.setAbbreviatedLabel(repr.getAbbreviatedLabel().replace("(bot.)", "").trim());
312
				repr.setLabel(repr.getLabel().replace("(Botany)", "").trim());
313
				subSectBot.setTitleCache(null, false);  //to definitely update the titleCache also
314
				app.getTermService().saveOrUpdate(subSectBot);
315
				app.commitTransaction(tx);
316
			} catch (Exception e) {
317
			    e.printStackTrace();
318
                logger.error("Exception in renameRanks: " + e.getMessage());
319
			}
320
		}
321
    }
322

    
323
    //create feature tree
324
    private void createFeatureTree(BerlinModelImportConfigurator config,
325
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport){
326
	    if (config.isDoFacts() && (config.getCheck().isImport()  )  ){
327
			try {
328
                ICdmRepository app = bmImport.getCdmAppController();
329
                TransactionStatus tx = app.startTransaction();
330

    
331
                //make feature tree
332
                FeatureTree tree = TreeCreator.flatTree(featureTreeUuid, config.getFeatureMap(), featureKeyList);
333
                tree.setTitleCache("Euro+Med Feature Tree", true);
334
                FeatureNode imageNode = FeatureNode.NewInstance(Feature.IMAGE());
335
                tree.getRoot().addChild(imageNode);
336
                FeatureNode distributionNode = FeatureNode.NewInstance(Feature.DISTRIBUTION());
337
                tree.getRoot().addChild(distributionNode, 1);
338
                FeatureNode commonNameNode = FeatureNode.NewInstance(Feature.COMMON_NAME());
339
                tree.getRoot().addChild(commonNameNode, 2);
340
                app.getFeatureTreeService().saveOrUpdate(tree);
341

    
342
                app.commitTransaction(tx);
343
            } catch (Exception e) {
344
                e.printStackTrace();
345
                logger.error("Exception in createFeatureTree: " + e.getMessage());
346
            }
347
		}
348
    }
349

    
350
    //Change common name label
351
    private void changeCommonNameLabel(BerlinModelImportConfigurator config,
352
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
353
	    if (config.isDoFacts() && (config.getCheck().isImport()  )  ){
354
	        try {
355
                ICdmRepository app = bmImport.getCdmAppController();
356
                TransactionStatus tx = app.startTransaction();
357

    
358
                DefinedTermBase<?> commonNameFeature = app.getTermService().find(Feature.COMMON_NAME().getUuid());
359
                commonNameFeature.setLabel("Common Names", Language.ENGLISH());
360
                commonNameFeature.setTitleCache(null, false);  //to definitely update the titleCache also
361
                app.getTermService().saveOrUpdate(commonNameFeature);
362

    
363
                app.commitTransaction(tx);
364
            } catch (Exception e) {
365
                e.printStackTrace();
366
                logger.error("Exception in changeCommonNameLabel: " + e.getMessage());
367
            }
368
	    }
369
    }
370

    
371
    //1. run transmission engine #3979
372
    private void runTransmissionEngine(BerlinModelImportConfigurator config,
373
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
374

    
375
        if (doRunTransmissionEngine && (config.getCheck().isImport()  )  ){
376
            try {
377
                ICdmRepository app = bmImport.getCdmAppController();
378

    
379
                final List<String> term_init_strategy = Arrays.asList(new String []{
380
                        "representations"
381
                });
382

    
383
                UUID uuidSuperAreaLevel = BerlinModelTransformer.uuidEuroMedAreaLevelFirst;
384
                NamedAreaLevel euroMedLevel1 = (NamedAreaLevel)app.getTermService().find(uuidSuperAreaLevel);
385

    
386
                Pager<NamedArea> areaPager = app.getTermService().list(
387
                        euroMedLevel1,
388
                        (NamedAreaType) null,
389
                        null,
390
                        null,
391
                        (List<OrderHint>) null,
392
                        term_init_strategy);
393
                TransmissionEngineDistribution transmissionEngineDistribution = (TransmissionEngineDistribution)app.getBean("transmissionEngineDistribution");
394
                transmissionEngineDistribution.accumulate(
395
                        AggregationMode.byAreasAndRanks,
396
                        areaPager.getRecords(),
397
                        Rank.UNRANKED_INFRASPECIFIC(),   //or do we even want to start from lower (UNKNOWN?)
398
                        Rank.GENUS(),
399
                        null,
400
                        DefaultProgressMonitor.NewInstance());
401
            } catch (Exception e) {
402
                e.printStackTrace();
403
                logger.error("Exception in markAreasAsHidden: " + e.getMessage());
404
            }
405
        }
406
    }
407

    
408
//    //5.Mark areas to be hidden #3979 .5
409
//    private void markAreasAsHidden(BerlinModelImportConfigurator config,
410
//            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
411
//
412
//        if (config.isDoOccurrence() && (config.getCheck().isImport())){
413
//	        try {
414
//                ICdmRepository app = bmImport.getCdmAppController();
415
//                TransactionStatus tx = app.startTransaction();
416
//
417
//                MarkerType hiddenAreaMarkerType = MarkerType.NewInstance("Used to hide distributions for the named areas in publications", "Hidden Area", null);
418
//                hiddenAreaMarkerType.setUuid(BerlinModelTransformer.uuidHiddenArea);
419
//                @SuppressWarnings("unchecked")
420
//                TermVocabulary<MarkerType> vocUserDefinedMarkerTypes = app.getVocabularyService().find(CdmImportBase.uuidUserDefinedMarkerTypeVocabulary);
421
//                if (vocUserDefinedMarkerTypes == null){
422
//                    String message = "Marker type vocabulary could not be found. Hidden areas not added.";
423
//                    logger.error(message);
424
//                    System.out.println(message);
425
//                }else{
426
//                    vocUserDefinedMarkerTypes.addTerm(hiddenAreaMarkerType);
427
//                    app.getVocabularyService().saveOrUpdate(vocUserDefinedMarkerTypes);
428
//
429
//                    //Add hidden area marker to Rs(C) and Rs(N)
430
//                    hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs);
431
//                    hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_B);
432
//                    hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_C);
433
//                    hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_E);
434
//                    hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_N);
435
//                    hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_K);
436
//                    hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_W);
437
//                 }
438
//                app.commitTransaction(tx);
439
//            } catch (Exception e) {
440
//                e.printStackTrace();
441
//                logger.error("Exception in markAreasAsHidden: " + e.getMessage());
442
//            }
443
//	    }
444
//    }
445
//
446
//    private void hideArea(ICdmRepository app, MarkerType hiddenAreaMarkerType, UUID areaUuid) {
447
//        NamedArea area = (NamedArea)app.getTermService().find(areaUuid);
448
//        area.addMarker(Marker.NewInstance(hiddenAreaMarkerType, true));
449
//        app.getTermService().saveOrUpdate(area);
450
//    }
451

    
452
    //2. import shapefile attributes #3979 .2
453
    private void importShapefile(BerlinModelImportConfigurator config,
454
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
455

    
456
        if (config.isDoOccurrence() && (config.getCheck().isImport())){
457

    
458
	       try {
459
	           UUID areaVocabularyUuid = BerlinModelTransformer.uuidVocEuroMedAreas;
460
               List<String> idSearchFields = Arrays.asList(new String[]{"EMAREA","PARENT"});
461
               String wmsLayerName = "euromed_2013";
462
               Set<UUID> areaUuidSet = null;
463

    
464
               ICdmRepository app = bmImport.getCdmAppController();
465
               IEditGeoService geoService = (IEditGeoService)app.getBean("editGeoService");
466

    
467
               Map<NamedArea, String> resultMap;
468
               try {
469
                   InputStream in = EuroMedActivator.class.getResourceAsStream("/euromed/euromed_2013.csv");
470
                   Reader reader = new InputStreamReader(in, "UTF-8");
471

    
472
                   resultMap = geoService.mapShapeFileToNamedAreas(
473
                               reader, idSearchFields , wmsLayerName , areaVocabularyUuid, areaUuidSet);
474
                   Map<String, String> flatResultMap = new HashMap<>(resultMap.size());
475
                   for(NamedArea area : resultMap.keySet()){
476
                       flatResultMap.put(area.getTitleCache() + " [" + area.getUuid() + "]", resultMap.get(area));
477
                   }
478
               } catch (IOException e) {
479
                    String message = "IOException when reading from mapping file or creating result map.";
480
                    logger.error(message);
481
                    System.out.println(message);
482
               }
483
            } catch (Exception e) {
484
                e.printStackTrace();
485
                logger.error("Exception in importShapefile: " + e.getMessage());
486
            }
487
	    }
488
    }
489

    
490
    //4. Create users and assign roles  #3979
491
    private void createUsersAndRoles(BerlinModelImportConfigurator config,
492
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
493

    
494
        try {
495
            if (config.isDoRelTaxa() && (config.getCheck().isImport())){
496
                ICdmRepository app = bmImport.getCdmAppController();
497
                TransactionStatus tx = app.startTransaction();
498

    
499
                //eraabstraube
500
                String eraabstraube = "eraabstraube";
501
                List<User> users = app.getUserService().listByUsername(eraabstraube, MatchMode.EXACT, null, null, null, null, null);
502
                User userEraabStraube;
503
                if (users.isEmpty()){
504
                    userEraabStraube = User.NewInstance(eraabstraube, eraabstraube);
505
                }else{
506
                    userEraabStraube = users.get(0);
507
                }
508
                if (userEraabStraube.getPerson() == null){
509
                    Person eckhard = Person.NewInstance();
510
                    eckhard.setFamilyName("von Raab-Straube");
511
                    eckhard.setGivenName("Eckhard");
512
                    eckhard.setPrefix("Dr.");
513
                    userEraabStraube.setPerson(eckhard);
514
                }
515
                app.getUserService().saveOrUpdate(userEraabStraube);
516

    
517
                //groups
518
                Group groupEditor = app.getGroupService().load(Group.GROUP_EDITOR_UUID);
519
                groupEditor.addMember(userEraabStraube);
520
                app.getGroupService().saveOrUpdate(groupEditor);
521

    
522
                Group groupProjectManager = app.getGroupService().load(Group.GROUP_PROJECT_MANAGER_UUID);
523
                groupProjectManager.addMember(userEraabStraube);
524
                app.getGroupService().saveOrUpdate(groupProjectManager);
525

    
526
                String[] publishRoles = new String[]{Role.ROLE_PUBLISH.toString()};
527
                Group groupPublisher = checkGroup(app.getGroupService(), Group.GROUP_PUBLISHER_UUID, "Publisher", publishRoles);
528
                groupPublisher.addMember(userEraabStraube);
529
                app.getGroupService().saveOrUpdate(groupPublisher);
530

    
531
                UUID uuidEuroMedPlantBaseGroup = UUID.fromString("91be42ea-ad04-4458-9836-389277e773db");
532
                String[] emPlantBaseRoles = new String[]{"TAXONNODE.[CREATE,READ,UPDATE,DELETE]"};
533
                Group euroMedPlantbase = checkGroup(app.getGroupService(), uuidEuroMedPlantBaseGroup, "Euro+Med Plantbase", emPlantBaseRoles);
534
                euroMedPlantbase.addMember(userEraabStraube);
535
                app.getGroupService().saveOrUpdate(euroMedPlantbase);
536

    
537
                //cichorieae-editor
538
                String cichorieaeEditor = "cichorieae-editor";
539
                app.getUserService().listByUsername(cichorieaeEditor, MatchMode.EXACT, null, null, null, null, null);
540
                User userCichEditor;
541
                if (users.isEmpty()){
542
                    userCichEditor = User.NewInstance(cichorieaeEditor, cichorieaeEditor);
543
                }else{
544
                    userCichEditor = users.get(0);
545
                }
546
                app.getUserService().saveOrUpdate(userCichEditor);
547

    
548
                //groups
549
                groupEditor.addMember(userCichEditor);
550
                app.getGroupService().saveOrUpdate(groupEditor);
551

    
552
                UUID uuidCichorieaeSubtree = null;
553
                UUID uuidCichorieae = UUID.fromString("63c7dbeb-b9a2-48b8-a75f-e3fe5e161f7c");
554
                Taxon cich = (Taxon)app.getTaxonService().find(uuidCichorieae);
555
                if (cich != null){
556
                    if (!cich.getTaxonNodes().isEmpty()){
557
                        TaxonNode cichNode = cich.getTaxonNodes().iterator().next();
558
                        uuidCichorieaeSubtree = cichNode.getUuid();
559
                    }
560
                }
561

    
562
                String[] cichorieaeRoles = new String[]{};
563
                if (uuidCichorieaeSubtree != null){
564
                    cichorieaeRoles = new String[]{"TAXONNODE.[CREATE,READ,UPDATE,DELETE]{"+uuidCichorieaeSubtree.toString()+"}"};
565
                }else{
566
                    String message = "Cichorieae node could not be found for cichorieae-editor role";
567
                    logger.warn(message);
568
                    System.out.println(message);
569
                }
570
                UUID uuidCichorieaeGroup = UUID.fromString("a630938d-dd4f-48c2-9406-91def487b11e");
571
                String cichorieaeGroupName = "Cichorieae";
572
                Group cichorieaeGroup = checkGroup(app.getGroupService(), uuidCichorieaeGroup, cichorieaeGroupName, cichorieaeRoles);
573
                cichorieaeGroup.addMember(userCichEditor);
574
                app.getGroupService().saveOrUpdate(cichorieaeGroup);
575

    
576
                app.commitTransaction(tx);
577
            }
578
        } catch (Exception e) {
579
            e.printStackTrace();
580
            logger.error("Exception in createUsersAndRoles: " + e.getMessage());
581
        }
582
    }
583

    
584
	/**
585
	  * copied from {@link FirstDataInserter#checkGroup}
586
     */
587
    private Group checkGroup(IGroupService groupService, UUID groupUuid, String groupName, String[] requiredAuthorities) {
588
        Group group = groupService.load(groupUuid);
589
        if(group == null){
590
            group = Group.NewInstance();
591
            group.setUuid(groupUuid);
592
            logger.info("New Group '" + groupName + "' created");
593
        }
594
        group.setName(groupName); // force name
595

    
596
        Set<GrantedAuthority> grantedAuthorities = group.getGrantedAuthorities();
597

    
598
        for(String a : requiredAuthorities){
599
            boolean isMissing = true;
600
            for(GrantedAuthority ga : grantedAuthorities){
601
                if(a.equals(ga.getAuthority())){
602
                    isMissing = false;
603
                    break;
604
                }
605
            }
606
            if(isMissing){
607
                GrantedAuthorityImpl newGa = GrantedAuthorityImpl.NewInstance(a);
608
                group.addGrantedAuthority(newGa);
609
                logger.info("New GrantedAuthority '" + a + "' added  to '" + groupName + "'");
610
            }
611
        }
612
        groupService.saveOrUpdate(group);
613
        logger.info("Check of group  '" + groupName + "' done");
614
        return group;
615
    }
616

    
617

    
618
	/**
619
	 * @param args
620
	 */
621
	public static void main(String[] args) {
622
		EuroMedActivator importActivator = new EuroMedActivator();
623
		Source source = berlinModelSource;
624
		ICdmDataSource cdmRepository = CdmDestinations.chooseDestination(args) != null ? CdmDestinations.chooseDestination(args) : cdmDestination;
625

    
626
		importActivator.importEm2CDM(source, cdmRepository, hbm2dll);
627
		if (includePesiExport){
628
			//not available from here since E+M was moved to app-import
629
//			PesiExportActivatorEM exportActivator = new PesiExportActivatorEM();
630
//			exportActivator.doExport(cdmRepository);
631
		}
632
		System.exit(0);
633

    
634
	}
635

    
636
}
(5-5/10)