Project

General

Profile

« Previous | Next » 

Revision 7f777e9c

Added by Andreas Müller almost 9 years ago

Add users and roles to E+M import #3979

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/app/berlinModelImport/EuroMedActivator.java
21 21
import java.util.UUID;
22 22

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

  
27
import eu.etaxonomy.cdm.api.application.FirstDataInserter;
26 28
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
29
import eu.etaxonomy.cdm.api.service.IGroupService;
27 30
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution;
28 31
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution.AggregationMode;
29 32
import eu.etaxonomy.cdm.api.service.pager.Pager;
......
40 43
import eu.etaxonomy.cdm.io.common.IImportConfigurator.DO_REFERENCES;
41 44
import eu.etaxonomy.cdm.io.common.IImportConfigurator.EDITOR;
42 45
import eu.etaxonomy.cdm.io.common.Source;
46
import eu.etaxonomy.cdm.model.agent.Person;
43 47
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
48
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
49
import eu.etaxonomy.cdm.model.common.Group;
44 50
import eu.etaxonomy.cdm.model.common.Language;
45 51
import eu.etaxonomy.cdm.model.common.Marker;
46 52
import eu.etaxonomy.cdm.model.common.MarkerType;
47 53
import eu.etaxonomy.cdm.model.common.Representation;
48 54
import eu.etaxonomy.cdm.model.common.TermVocabulary;
55
import eu.etaxonomy.cdm.model.common.User;
49 56
import eu.etaxonomy.cdm.model.description.Feature;
50 57
import eu.etaxonomy.cdm.model.description.FeatureNode;
51 58
import eu.etaxonomy.cdm.model.description.FeatureTree;
......
54 61
import eu.etaxonomy.cdm.model.location.NamedAreaType;
55 62
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
56 63
import eu.etaxonomy.cdm.model.name.Rank;
64
import eu.etaxonomy.cdm.model.taxon.Taxon;
65
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
66
import eu.etaxonomy.cdm.persistence.hibernate.permission.Role;
67
import eu.etaxonomy.cdm.persistence.query.MatchMode;
57 68
import eu.etaxonomy.cdm.persistence.query.OrderHint;
58 69

  
59 70

  
......
67 78
 *
68 79
 */
69 80
public class EuroMedActivator {
70
	@SuppressWarnings("unused")
71 81
	private static final Logger logger = Logger.getLogger(EuroMedActivator.class);
72 82

  
73 83
	//database validation status (create, update, validate ...)
......
142 152

  
143 153
// **************** ALL *********************
144 154

  
145
	boolean invers =   !(hbm2dll == DbSchemaValidation.CREATE);
155
	boolean invers = !(hbm2dll == DbSchemaValidation.CREATE);
146 156

  
147 157
	static final boolean doUser = true;
148 158
//	//authors
......
158 168

  
159 169
	//taxa
160 170
	static final boolean doTaxa = true;
161
	static final boolean doRelTaxa = false;
171
	static final boolean doRelTaxa = true;  //FIXME revert
162 172
	static final boolean doFacts = true;
163 173
	static final boolean doOccurences = true;
164 174
	static final boolean doCommonNames = true;
......
238 248
		CdmDefaultImport<BerlinModelImportConfigurator> bmImport = new CdmDefaultImport<BerlinModelImportConfigurator>();
239 249
		bmImport.invoke(config);
240 250

  
241
		//Rename Ranks (still needed?)
251
		renameRanks(config, bmImport);
252

  
253
		createFeatureTree(config, bmImport);
254

  
255
		changeCommonNameLabel(config, bmImport);
256

  
257
        runTransmissionEngine(config, bmImport);
258

  
259
        importShapefile(config, bmImport);
260

  
261
        createUsersAndRoles(config, bmImport);
262

  
263
	    markAreasAsHidden(config, bmImport);
264

  
265
		System.out.println("End import from BerlinModel ("+ source.getDatabase() + ")...");
266
	}
267

  
268
    private void renameRanks(BerlinModelImportConfigurator config,
269
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
270
        //Rename Ranks (still needed?)
242 271
		if (config.isDoTaxonNames() && (config.getCheck().isImport() )  ){
243 272
			ICdmApplicationConfiguration app = bmImport.getCdmAppController();
244 273
			TransactionStatus tx = app.startTransaction();
......
261 290
				e.printStackTrace();
262 291
			}
263 292
		}
293
    }
264 294

  
265
		//create feature tree
295
    private void createFeatureTree(BerlinModelImportConfigurator config,
296
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
297
        //create feature tree
266 298
		if (config.isDoFacts() && (config.getCheck().isImport()  )  ){
267 299
			ICdmApplicationConfiguration app = bmImport.getCdmAppController();
268 300
			TransactionStatus tx = app.startTransaction();
......
278 310
			app.getFeatureTreeService().saveOrUpdate(tree);
279 311

  
280 312
            app.commitTransaction(tx);
281

  
282

  
283 313
		}
314
    }
284 315

  
285
		//Change common name label
316
    private void changeCommonNameLabel(BerlinModelImportConfigurator config,
317
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
318
        //Change common name label
286 319
	    if (config.isDoFacts() && (config.getCheck().isImport()  )  ){
287 320
	        ICdmApplicationConfiguration app = bmImport.getCdmAppController();
288 321
	        TransactionStatus tx = app.startTransaction();
......
294 327

  
295 328
            app.commitTransaction(tx);
296 329
	    }
330
    }
297 331

  
298
		if (config.isDoRelTaxa()){
299
		    //??
300
		}
301

  
332
    private void runTransmissionEngine(BerlinModelImportConfigurator config,
333
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
302 334
        //Transmission engine #3979 .1
303 335
        if (config.isDoOccurrence() && (config.getCheck().isImport()  )  ){
304 336
            ICdmApplicationConfiguration app = bmImport.getCdmAppController();
......
322 354
                    null,
323 355
                    DefaultProgressMonitor.NewInstance());
324 356
        }
357
    }
358

  
359
    private void markAreasAsHidden(BerlinModelImportConfigurator config,
360
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
361
        //5.Mark areas to be hidden #3979 .5
362
	    if (config.isDoOccurrence() && (config.getCheck().isImport())){
363
	        ICdmApplicationConfiguration app = bmImport.getCdmAppController();
325 364

  
365
	        MarkerType hiddenAreaMarkerType = MarkerType.NewInstance("", "Hidden area", null);
366
	        hiddenAreaMarkerType.setUuid(BerlinModelTransformer.uuidHiddenArea);
367
	        @SuppressWarnings("unchecked")
368
            TermVocabulary<MarkerType> vocUserDefinedMarkerTypes = app.getVocabularyService().find(CdmImportBase.uuidUserDefinedMarkerTypeVocabulary);
369
	        if (vocUserDefinedMarkerTypes == null){
370
	            String message = "Marker type vocabulary could not be found. Hidden areas not added.";
371
	            logger.error(message);
372
	            System.out.println(message);
373
	        }else{
374
	            vocUserDefinedMarkerTypes.addTerm(hiddenAreaMarkerType);
375
	            app.getVocabularyService().saveOrUpdate(vocUserDefinedMarkerTypes);
326 376

  
327
//      //import shapefile attributes #3979 .2
377
	            //Add hidden area marker to Rs(C) and Rs(N)
378
	            NamedArea rs_c = (NamedArea)app.getTermService().find(BerlinModelTransformer.uuidRs_C);
379
	            rs_c.addMarker(Marker.NewInstance(hiddenAreaMarkerType, true));
380
	            app.getTermService().saveOrUpdate(rs_c);
381
	            NamedArea rs_n = (NamedArea)app.getTermService().find(BerlinModelTransformer.uuidRs_N);
382
                rs_n.addMarker(Marker.NewInstance(hiddenAreaMarkerType, true));
383
                app.getTermService().saveOrUpdate(rs_n);
384
	        }
385
	    }
386
    }
387

  
388
    private void importShapefile(BerlinModelImportConfigurator config,
389
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
390
        //      //import shapefile attributes #3979 .2
328 391
	    if (config.isDoOccurrence() && (config.getCheck().isImport())){
329 392

  
330 393
	       UUID areaVocabularyUuid = BerlinModelTransformer.uuidVocEuroMedAreas;
......
354 417
                System.out.println(message);
355 418
           }
356 419
	    }
420
    }
357 421

  
358
	    //5.Mark areas to be hidden #3979 .5
359
	    if (config.isDoOccurrence() && (config.getCheck().isImport())){
422
    private void createUsersAndRoles(BerlinModelImportConfigurator config,
423
            CdmDefaultImport<BerlinModelImportConfigurator> bmImport) {
424
        //4. Create users and assign roles  #3979
425
	    if (config.isDoRelTaxa() && (config.getCheck().isImport())){
360 426
	        ICdmApplicationConfiguration app = bmImport.getCdmAppController();
427
	        TransactionStatus tx = app.startTransaction();
361 428

  
362
	        MarkerType hiddenAreaMarkerType = MarkerType.NewInstance("", "Hidden area", null);
363
	        hiddenAreaMarkerType.setUuid(BerlinModelTransformer.uuidHiddenArea);
364
	        @SuppressWarnings("unchecked")
365
            TermVocabulary<MarkerType> vocUserDefinedMarkerTypes = app.getVocabularyService().find(CdmImportBase.uuidUserDefinedMarkerTypeVocabulary);
366
	        if (vocUserDefinedMarkerTypes == null){
367
	            String message = "Marker type vocabulary could not be found. Hidden areas not added.";
368
	            logger.error(message);
369
	            System.out.println(message);
429
	        //eraabstraube
430
	        String eraabstraube = "eraabstraube";
431
	        List<User> users = app.getUserService().listByUsername(eraabstraube, MatchMode.EXACT, null, null, null, null, null);
432
	        User userEraabStraube;
433
	        if (users.isEmpty()){
434
	            userEraabStraube = User.NewInstance(eraabstraube, eraabstraube);
370 435
	        }else{
371
	            vocUserDefinedMarkerTypes.addTerm(hiddenAreaMarkerType);
372
	            app.getVocabularyService().saveOrUpdate(vocUserDefinedMarkerTypes);
373

  
374
	            //Add hidden area marker to Rs(C) and Rs(N)
375
	            NamedArea rs_c = (NamedArea)app.getTermService().find(BerlinModelTransformer.uuidRs_C);
376
	            rs_c.addMarker(Marker.NewInstance(hiddenAreaMarkerType, true));
377
	            app.getTermService().saveOrUpdate(rs_c);
378
	            NamedArea rs_n = (NamedArea)app.getTermService().find(BerlinModelTransformer.uuidRs_N);
379
                rs_n.addMarker(Marker.NewInstance(hiddenAreaMarkerType, true));
380
                app.getTermService().saveOrUpdate(rs_n);
436
	            userEraabStraube = users.get(0);
437
	        }
438
	        if (userEraabStraube.getPerson() == null){
439
    	        Person eckhard = Person.NewInstance();
440
                eckhard.setLastname("von Raab-Straube");
441
                eckhard.setFirstname("Eckhard");
442
                eckhard.setPrefix("Dr.");
443
                userEraabStraube.setPerson(eckhard);
381 444
	        }
445
            app.getUserService().saveOrUpdate(userEraabStraube);
446

  
447
            //groups
448
            Group groupEditor = app.getGroupService().load(Group.groupEditorUuid);
449
            groupEditor.addMember(userEraabStraube);
450
            app.getGroupService().saveOrUpdate(groupEditor);
451

  
452
            Group groupProjectManager = app.getGroupService().load(Group.groupProjectManagerUuid);
453
            groupProjectManager.addMember(userEraabStraube);
454
            app.getGroupService().saveOrUpdate(groupProjectManager);
455

  
456
            String[] publishRoles = new String[]{Role.ROLE_PUBLISH.toString()};
457
            Group groupPublisher = checkGroup(app.getGroupService(), Group.groupPublisherUuid, "Publisher", publishRoles);
458
            groupPublisher.addMember(userEraabStraube);
459
            app.getGroupService().saveOrUpdate(groupPublisher);
460

  
461
            UUID uuidEuroMedPlantBaseGroup = UUID.fromString("91be42ea-ad04-4458-9836-389277e773db");
462
            String[] emPlantBaseRoles = new String[]{"TAXONNODE.[CREATE,READ,UPDATE,DELETE]"};
463
            Group euroMedPlantbase = checkGroup(app.getGroupService(), uuidEuroMedPlantBaseGroup, "Euro+Med Plantbase", emPlantBaseRoles);
464
            euroMedPlantbase.addMember(userEraabStraube);
465
            app.getGroupService().saveOrUpdate(euroMedPlantbase);
466

  
467
            //cichorieae-editor
468
            String cichorieaeEditor = "cichorieae-editor";
469
            app.getUserService().listByUsername(cichorieaeEditor, MatchMode.EXACT, null, null, null, null, null);
470
            User userCichEditor;
471
            if (users.isEmpty()){
472
                userCichEditor = User.NewInstance(cichorieaeEditor, cichorieaeEditor);
473
            }else{
474
                userCichEditor = users.get(0);
475
            }
476
            app.getUserService().saveOrUpdate(userCichEditor);
477

  
478
            //groups
479
            groupEditor.addMember(userCichEditor);
480
            app.getGroupService().saveOrUpdate(groupEditor);
481

  
482
            UUID uuidCichorieaeSubtree = null;
483
            UUID uuidCichorieae = UUID.fromString("63c7dbeb-b9a2-48b8-a75f-e3fe5e161f7c");
484
            Taxon cich = (Taxon)app.getTaxonService().find(uuidCichorieae);
485
            if (cich != null){
486
                TaxonNode cichNode = cich.getTaxonNodes().iterator().next();
487
                if (cichNode != null){
488
                    uuidCichorieaeSubtree = cichNode.getUuid();
489
                }
490
            }
491

  
492
            String[] cichorieaeRoles = new String[]{};
493
            if (uuidCichorieaeSubtree != null){
494
                cichorieaeRoles = new String[]{"TAXONNODE.[CREATE,READ,UPDATE,DELETE]{"+uuidCichorieaeSubtree.toString()+"}"};
495
            }else{
496
                String message = "Cichorieae node could not be found";
497
                logger.warn(message);
498
                System.out.println(message);
499
            }
500
            UUID uuidCichorieaeGroup = UUID.fromString("a630938d-dd4f-48c2-9406-91def487b11e");
501
            String cichorieaeGroupName = "cichorieae";
502
            Group cichorieaeGroup = checkGroup(app.getGroupService(), uuidCichorieaeGroup, cichorieaeGroupName, cichorieaeRoles);
503
            cichorieaeGroup.addMember(userCichEditor);
504
            app.getGroupService().saveOrUpdate(cichorieaeGroup);
505

  
506
            app.commitTransaction(tx);
382 507
	    }
508
    }
383 509

  
384
		System.out.println("End import from BerlinModel ("+ source.getDatabase() + ")...");
510
	/**
511
	  * copied from {@link FirstDataInserter#checkGroup}
512
     */
513
    private Group checkGroup(IGroupService groupService, UUID groupUuid, String groupName, String[] requiredAuthorities) {
514
        Group group = groupService.load(groupUuid);
515
        if(group == null){
516
            group = Group.NewInstance();
517
            group.setUuid(groupUuid);
518
            logger.info("New Group '" + groupName + "' created");
519
        }
520
        group.setName(groupName); // force name
521

  
522
        Set<GrantedAuthority> grantedAuthorities = group.getGrantedAuthorities();
523

  
524
        for(String a : requiredAuthorities){
525
            boolean isMissing = true;
526
            for(GrantedAuthority ga : grantedAuthorities){
527
                if(a.equals(ga.getAuthority())){
528
                    isMissing = false;
529
                    break;
530
                }
531
            }
532
            if(isMissing){
533
                GrantedAuthorityImpl newGa = GrantedAuthorityImpl.NewInstance();
534
                newGa.setAuthority(a);
535
                group.addGrantedAuthority(newGa);
536
                logger.info("New GrantedAuthority '" + a + "' added  to '" + groupName + "'");
537
            }
538
        }
539
        groupService.saveOrUpdate(group);
540
        logger.info("Check of group  '" + groupName + "' done");
541
        return group;
542
    }
385 543

  
386
	}
387 544

  
388 545
	/**
389 546
	 * @param args

Also available in: Unified diff