split occurrence and occurrence source import
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / app / berlinModelImport / EuroMedActivator.java
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.setUseClassification(useClassification);
228 config.setSourceRefUuid(BerlinModelTransformer.uuidSourceRefEuroMed);
229 config.setEditor(editor);
230 config.setDbSchemaValidation(hbm2dll);
231 config.setUseLastScrutinyAsSec(useLastScrutinyAsSec);
232 config.setWarnForDifferingSynonymReference(warnForDifferingSynonymReference);
233
234 // maximum number of name facts to import
235 config.setMaximumNumberOfNameFacts(maximumNumberOfNameFacts);
236
237 config.setInfrGenericRankAbbrev(infrGenericRankAbbrev);
238 config.setInfrSpecificRankAbbrev(infrSpecificRankAbbrev);
239 config.setRemoveHttpMapsAnchor(removeHttpMapsAnchor);
240
241 config.setDoPreliminaryRefDetailsWithNames(doPreliminaryRefDetailsWithNames);
242
243 // filter
244 config.setTaxonTable(taxonTable);
245 config.setClassificationQuery(classificationQuery);
246 config.setRelTaxaIdQuery(relPTaxonIdQuery);
247 config.setNameIdTable(nameIdTable);
248 config.setReferenceIdTable(referenceIdTable);
249 config.setAuthorTeamFilter(authorTeamFilter);
250 config.setAuthorFilter(authorFilter);
251 config.setFactFilter(factFilter);
252 config.setRefDetailFilter(refDetailFilter);
253 config.setCommonNameFilter(commonNameFilter);
254 config.setOccurrenceFilter(occurrenceFilter);
255 config.setOccurrenceSourceFilter(occurrenceSourceFilter);
256 config.setWebMarkerFilter(webMarkerFilter);
257 config.setUseSingleClassification(useSingleClassification);
258
259 //TDWG codes
260 config.setSplitTdwgCodes(isSplitTdwgCodes);
261 config.setUseEmAreaVocabulary(useEmAreaVocabulary);
262
263 config.setCheck(check);
264 config.setEditor(editor);
265 config.setRecordsPerTransaction(partitionSize);
266
267 config.setSwitchSpeciesGroup(switchSpeciesGroup);
268
269 // invoke import
270 CdmDefaultImport<BerlinModelImportConfigurator> bmImport = new CdmDefaultImport<>();
271 bmImport.invoke(config);
272
273 renameRanks(config, bmImport);
274
275 createFeatureTree(config, bmImport);
276
277 changeCommonNameLabel(config, bmImport);
278
279 createUsersAndRoles(config, bmImport);
280
281 runTransmissionEngine(config, bmImport);
282
283 importShapefile(config, bmImport);
284
285 // markAreasAsHidden(config, bmImport); //has been moved to BM occurrence import
286
287 System.out.println("End import from BerlinModel ("+ source.getDatabase() + ")...");
288 }
289
290 //Rename Ranks (still needed?)
291 private void renameRanks(BerlinModelImportConfigurator config,
292 CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
293
294 if (config.isDoTaxonNames() && (config.getCheck().isImport() ) ){
295 ICdmRepository app = bmImport.getCdmAppController();
296 TransactionStatus tx = app.startTransaction();
297 try {
298 Rank sectBot = (Rank)app.getTermService().find(Rank.SECTION_BOTANY().getUuid());
299 Representation repr = sectBot.getRepresentation(Language.ENGLISH());
300 repr.setAbbreviatedLabel(repr.getAbbreviatedLabel().replace("(bot.)", "").trim());
301 repr.setLabel(repr.getLabel().replace("(Botany)", "").trim());
302 sectBot.setTitleCache(null, false); //to definitely update the titleCache also
303 app.getTermService().saveOrUpdate(sectBot);
304
305 Rank subSectBot = (Rank)app.getTermService().find(Rank.SECTION_BOTANY().getUuid());
306 repr = subSectBot.getRepresentation(Language.ENGLISH());
307 repr.setAbbreviatedLabel(repr.getAbbreviatedLabel().replace("(bot.)", "").trim());
308 repr.setLabel(repr.getLabel().replace("(Botany)", "").trim());
309 subSectBot.setTitleCache(null, false); //to definitely update the titleCache also
310 app.getTermService().saveOrUpdate(subSectBot);
311 app.commitTransaction(tx);
312 } catch (Exception e) {
313 e.printStackTrace();
314 logger.error("Exception in renameRanks: " + e.getMessage());
315 }
316 }
317 }
318
319 //create feature tree
320 private void createFeatureTree(BerlinModelImportConfigurator config,
321 CdmDefaultImport<BerlinModelImportConfigurator> bmImport){
322 if (config.isDoFacts() && (config.getCheck().isImport() ) ){
323 try {
324 ICdmRepository app = bmImport.getCdmAppController();
325 TransactionStatus tx = app.startTransaction();
326
327 //make feature tree
328 FeatureTree tree = TreeCreator.flatTree(featureTreeUuid, config.getFeatureMap(), featureKeyList);
329 tree.setTitleCache("Euro+Med Feature Tree", true);
330 FeatureNode imageNode = FeatureNode.NewInstance(Feature.IMAGE());
331 tree.getRoot().addChild(imageNode);
332 FeatureNode distributionNode = FeatureNode.NewInstance(Feature.DISTRIBUTION());
333 tree.getRoot().addChild(distributionNode, 1);
334 FeatureNode commonNameNode = FeatureNode.NewInstance(Feature.COMMON_NAME());
335 tree.getRoot().addChild(commonNameNode, 2);
336 app.getFeatureTreeService().saveOrUpdate(tree);
337
338 app.commitTransaction(tx);
339 } catch (Exception e) {
340 e.printStackTrace();
341 logger.error("Exception in createFeatureTree: " + e.getMessage());
342 }
343 }
344 }
345
346 //Change common name label
347 private void changeCommonNameLabel(BerlinModelImportConfigurator config,
348 CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
349 if (config.isDoFacts() && (config.getCheck().isImport() ) ){
350 try {
351 ICdmRepository app = bmImport.getCdmAppController();
352 TransactionStatus tx = app.startTransaction();
353
354 DefinedTermBase<?> commonNameFeature = app.getTermService().find(Feature.COMMON_NAME().getUuid());
355 commonNameFeature.setLabel("Common Names", Language.ENGLISH());
356 commonNameFeature.setTitleCache(null, false); //to definitely update the titleCache also
357 app.getTermService().saveOrUpdate(commonNameFeature);
358
359 app.commitTransaction(tx);
360 } catch (Exception e) {
361 e.printStackTrace();
362 logger.error("Exception in changeCommonNameLabel: " + e.getMessage());
363 }
364 }
365 }
366
367 //1. run transmission engine #3979
368 private void runTransmissionEngine(BerlinModelImportConfigurator config,
369 CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
370
371 if (doRunTransmissionEngine && (config.getCheck().isImport() ) ){
372 try {
373 ICdmRepository app = bmImport.getCdmAppController();
374
375 final List<String> term_init_strategy = Arrays.asList(new String []{
376 "representations"
377 });
378
379 UUID uuidSuperAreaLevel = BerlinModelTransformer.uuidEuroMedAreaLevelFirst;
380 NamedAreaLevel euroMedLevel1 = (NamedAreaLevel)app.getTermService().find(uuidSuperAreaLevel);
381
382 Pager<NamedArea> areaPager = app.getTermService().list(
383 euroMedLevel1,
384 (NamedAreaType) null,
385 null,
386 null,
387 (List<OrderHint>) null,
388 term_init_strategy);
389 TransmissionEngineDistribution transmissionEngineDistribution = (TransmissionEngineDistribution)app.getBean("transmissionEngineDistribution");
390 transmissionEngineDistribution.accumulate(
391 AggregationMode.byAreasAndRanks,
392 areaPager.getRecords(),
393 Rank.UNRANKED_INFRASPECIFIC(), //or do we even want to start from lower (UNKNOWN?)
394 Rank.GENUS(),
395 null,
396 DefaultProgressMonitor.NewInstance());
397 } catch (Exception e) {
398 e.printStackTrace();
399 logger.error("Exception in markAreasAsHidden: " + e.getMessage());
400 }
401 }
402 }
403
404 // //5.Mark areas to be hidden #3979 .5
405 // private void markAreasAsHidden(BerlinModelImportConfigurator config,
406 // CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
407 //
408 // if (config.isDoOccurrence() && (config.getCheck().isImport())){
409 // try {
410 // ICdmRepository app = bmImport.getCdmAppController();
411 // TransactionStatus tx = app.startTransaction();
412 //
413 // MarkerType hiddenAreaMarkerType = MarkerType.NewInstance("Used to hide distributions for the named areas in publications", "Hidden Area", null);
414 // hiddenAreaMarkerType.setUuid(BerlinModelTransformer.uuidHiddenArea);
415 // @SuppressWarnings("unchecked")
416 // TermVocabulary<MarkerType> vocUserDefinedMarkerTypes = app.getVocabularyService().find(CdmImportBase.uuidUserDefinedMarkerTypeVocabulary);
417 // if (vocUserDefinedMarkerTypes == null){
418 // String message = "Marker type vocabulary could not be found. Hidden areas not added.";
419 // logger.error(message);
420 // System.out.println(message);
421 // }else{
422 // vocUserDefinedMarkerTypes.addTerm(hiddenAreaMarkerType);
423 // app.getVocabularyService().saveOrUpdate(vocUserDefinedMarkerTypes);
424 //
425 // //Add hidden area marker to Rs(C) and Rs(N)
426 // hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs);
427 // hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_B);
428 // hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_C);
429 // hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_E);
430 // hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_N);
431 // hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_K);
432 // hideArea(app, hiddenAreaMarkerType, BerlinModelTransformer.uuidRs_W);
433 // }
434 // app.commitTransaction(tx);
435 // } catch (Exception e) {
436 // e.printStackTrace();
437 // logger.error("Exception in markAreasAsHidden: " + e.getMessage());
438 // }
439 // }
440 // }
441 //
442 // private void hideArea(ICdmRepository app, MarkerType hiddenAreaMarkerType, UUID areaUuid) {
443 // NamedArea area = (NamedArea)app.getTermService().find(areaUuid);
444 // area.addMarker(Marker.NewInstance(hiddenAreaMarkerType, true));
445 // app.getTermService().saveOrUpdate(area);
446 // }
447
448 //2. import shapefile attributes #3979 .2
449 private void importShapefile(BerlinModelImportConfigurator config,
450 CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
451
452 if (config.isDoOccurrence() && (config.getCheck().isImport())){
453
454 try {
455 UUID areaVocabularyUuid = BerlinModelTransformer.uuidVocEuroMedAreas;
456 List<String> idSearchFields = Arrays.asList(new String[]{"EMAREA","PARENT"});
457 String wmsLayerName = "euromed_2013";
458 Set<UUID> areaUuidSet = null;
459
460 ICdmRepository app = bmImport.getCdmAppController();
461 IEditGeoService geoService = (IEditGeoService)app.getBean("editGeoService");
462
463 Map<NamedArea, String> resultMap;
464 try {
465 InputStream in = EuroMedActivator.class.getResourceAsStream("/euromed/euromed_2013.csv");
466 Reader reader = new InputStreamReader(in, "UTF-8");
467
468 resultMap = geoService.mapShapeFileToNamedAreas(
469 reader, idSearchFields , wmsLayerName , areaVocabularyUuid, areaUuidSet);
470 Map<String, String> flatResultMap = new HashMap<>(resultMap.size());
471 for(NamedArea area : resultMap.keySet()){
472 flatResultMap.put(area.getTitleCache() + " [" + area.getUuid() + "]", resultMap.get(area));
473 }
474 } catch (IOException e) {
475 String message = "IOException when reading from mapping file or creating result map.";
476 logger.error(message);
477 System.out.println(message);
478 }
479 } catch (Exception e) {
480 e.printStackTrace();
481 logger.error("Exception in importShapefile: " + e.getMessage());
482 }
483 }
484 }
485
486 //4. Create users and assign roles #3979
487 private void createUsersAndRoles(BerlinModelImportConfigurator config,
488 CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
489
490 try {
491 if (config.isDoRelTaxa() && (config.getCheck().isImport())){
492 ICdmRepository app = bmImport.getCdmAppController();
493 TransactionStatus tx = app.startTransaction();
494
495 //eraabstraube
496 String eraabstraube = "eraabstraube";
497 List<User> users = app.getUserService().listByUsername(eraabstraube, MatchMode.EXACT, null, null, null, null, null);
498 User userEraabStraube;
499 if (users.isEmpty()){
500 userEraabStraube = User.NewInstance(eraabstraube, eraabstraube);
501 }else{
502 userEraabStraube = users.get(0);
503 }
504 if (userEraabStraube.getPerson() == null){
505 Person eckhard = Person.NewInstance();
506 eckhard.setFamilyName("von Raab-Straube");
507 eckhard.setGivenName("Eckhard");
508 eckhard.setPrefix("Dr.");
509 userEraabStraube.setPerson(eckhard);
510 }
511 app.getUserService().saveOrUpdate(userEraabStraube);
512
513 //groups
514 Group groupEditor = app.getGroupService().load(Group.GROUP_EDITOR_UUID);
515 groupEditor.addMember(userEraabStraube);
516 app.getGroupService().saveOrUpdate(groupEditor);
517
518 Group groupProjectManager = app.getGroupService().load(Group.GROUP_PROJECT_MANAGER_UUID);
519 groupProjectManager.addMember(userEraabStraube);
520 app.getGroupService().saveOrUpdate(groupProjectManager);
521
522 String[] publishRoles = new String[]{Role.ROLE_PUBLISH.toString()};
523 Group groupPublisher = checkGroup(app.getGroupService(), Group.GROUP_PUBLISHER_UUID, "Publisher", publishRoles);
524 groupPublisher.addMember(userEraabStraube);
525 app.getGroupService().saveOrUpdate(groupPublisher);
526
527 UUID uuidEuroMedPlantBaseGroup = UUID.fromString("91be42ea-ad04-4458-9836-389277e773db");
528 String[] emPlantBaseRoles = new String[]{"TAXONNODE.[CREATE,READ,UPDATE,DELETE]"};
529 Group euroMedPlantbase = checkGroup(app.getGroupService(), uuidEuroMedPlantBaseGroup, "Euro+Med Plantbase", emPlantBaseRoles);
530 euroMedPlantbase.addMember(userEraabStraube);
531 app.getGroupService().saveOrUpdate(euroMedPlantbase);
532
533 //cichorieae-editor
534 String cichorieaeEditor = "cichorieae-editor";
535 app.getUserService().listByUsername(cichorieaeEditor, MatchMode.EXACT, null, null, null, null, null);
536 User userCichEditor;
537 if (users.isEmpty()){
538 userCichEditor = User.NewInstance(cichorieaeEditor, cichorieaeEditor);
539 }else{
540 userCichEditor = users.get(0);
541 }
542 app.getUserService().saveOrUpdate(userCichEditor);
543
544 //groups
545 groupEditor.addMember(userCichEditor);
546 app.getGroupService().saveOrUpdate(groupEditor);
547
548 UUID uuidCichorieaeSubtree = null;
549 UUID uuidCichorieae = UUID.fromString("63c7dbeb-b9a2-48b8-a75f-e3fe5e161f7c");
550 Taxon cich = (Taxon)app.getTaxonService().find(uuidCichorieae);
551 if (cich != null){
552 if (!cich.getTaxonNodes().isEmpty()){
553 TaxonNode cichNode = cich.getTaxonNodes().iterator().next();
554 uuidCichorieaeSubtree = cichNode.getUuid();
555 }
556 }
557
558 String[] cichorieaeRoles = new String[]{};
559 if (uuidCichorieaeSubtree != null){
560 cichorieaeRoles = new String[]{"TAXONNODE.[CREATE,READ,UPDATE,DELETE]{"+uuidCichorieaeSubtree.toString()+"}"};
561 }else{
562 String message = "Cichorieae node could not be found for cichorieae-editor role";
563 logger.warn(message);
564 System.out.println(message);
565 }
566 UUID uuidCichorieaeGroup = UUID.fromString("a630938d-dd4f-48c2-9406-91def487b11e");
567 String cichorieaeGroupName = "Cichorieae";
568 Group cichorieaeGroup = checkGroup(app.getGroupService(), uuidCichorieaeGroup, cichorieaeGroupName, cichorieaeRoles);
569 cichorieaeGroup.addMember(userCichEditor);
570 app.getGroupService().saveOrUpdate(cichorieaeGroup);
571
572 app.commitTransaction(tx);
573 }
574 } catch (Exception e) {
575 e.printStackTrace();
576 logger.error("Exception in createUsersAndRoles: " + e.getMessage());
577 }
578 }
579
580 /**
581 * copied from {@link FirstDataInserter#checkGroup}
582 */
583 private Group checkGroup(IGroupService groupService, UUID groupUuid, String groupName, String[] requiredAuthorities) {
584 Group group = groupService.load(groupUuid);
585 if(group == null){
586 group = Group.NewInstance();
587 group.setUuid(groupUuid);
588 logger.info("New Group '" + groupName + "' created");
589 }
590 group.setName(groupName); // force name
591
592 Set<GrantedAuthority> grantedAuthorities = group.getGrantedAuthorities();
593
594 for(String a : requiredAuthorities){
595 boolean isMissing = true;
596 for(GrantedAuthority ga : grantedAuthorities){
597 if(a.equals(ga.getAuthority())){
598 isMissing = false;
599 break;
600 }
601 }
602 if(isMissing){
603 GrantedAuthorityImpl newGa = GrantedAuthorityImpl.NewInstance(a);
604 group.addGrantedAuthority(newGa);
605 logger.info("New GrantedAuthority '" + a + "' added to '" + groupName + "'");
606 }
607 }
608 groupService.saveOrUpdate(group);
609 logger.info("Check of group '" + groupName + "' done");
610 return group;
611 }
612
613
614 /**
615 * @param args
616 */
617 public static void main(String[] args) {
618 EuroMedActivator importActivator = new EuroMedActivator();
619 Source source = berlinModelSource;
620 ICdmDataSource cdmRepository = CdmDestinations.chooseDestination(args) != null ? CdmDestinations.chooseDestination(args) : cdmDestination;
621
622 importActivator.importEm2CDM(source, cdmRepository, hbm2dll);
623 if (includePesiExport){
624 //not available from here since E+M was moved to app-import
625 // PesiExportActivatorEM exportActivator = new PesiExportActivatorEM();
626 // exportActivator.doExport(cdmRepository);
627 }
628 System.exit(0);
629
630 }
631
632 }