Search in sources :

Example 36 with ObjectBundle

use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.

the class DefaultObjectBundleService method handleCreates.

//-----------------------------------------------------------------------------------
// Utility Methods
//-----------------------------------------------------------------------------------
private TypeReport handleCreates(Session session, Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle) {
    TypeReport typeReport = new TypeReport(klass);
    if (objects.isEmpty()) {
        return typeReport;
    }
    String message = "(" + bundle.getUsername() + ") Creating " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
    log.info(message);
    if (bundle.hasTaskId()) {
        notifier.notify(bundle.getTaskId(), message);
    }
    objects.forEach(object -> objectBundleHooks.forEach(hook -> hook.preCreate(object, bundle)));
    for (int idx = 0; idx < objects.size(); idx++) {
        IdentifiableObject object = objects.get(idx);
        if (Preheat.isDefault(object))
            continue;
        ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
        objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
        typeReport.addObjectReport(objectReport);
        preheatService.connectReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier());
        session.save(object);
        if (MetadataObject.class.isInstance(object)) {
            deletedObjectService.deleteDeletedObjects(new DeletedObjectQuery(object));
        }
        bundle.getPreheat().replace(bundle.getPreheatIdentifier(), object);
        objectBundleHooks.forEach(hook -> hook.postCreate(object, bundle));
        if (log.isDebugEnabled()) {
            String msg = "(" + bundle.getUsername() + ") Created object '" + bundle.getPreheatIdentifier().getIdentifiersWithName(object) + "'";
            log.debug(msg);
        }
        if (FlushMode.OBJECT == bundle.getFlushMode())
            session.flush();
    }
    return typeReport;
}
Also used : HibernateCacheManager(org.hisp.dhis.cache.HibernateCacheManager) PreheatService(org.hisp.dhis.preheat.PreheatService) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) MergeService(org.hisp.dhis.schema.MergeService) Session(org.hibernate.Session) Preheat(org.hisp.dhis.preheat.Preheat) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) MergeParams(org.hisp.dhis.schema.MergeParams) FlushMode(org.hisp.dhis.dxf2.metadata.FlushMode) TypeReport(org.hisp.dhis.feedback.TypeReport) ArrayList(java.util.ArrayList) Notifier(org.hisp.dhis.system.notification.Notifier) ObjectBundleCommitReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) Service(org.springframework.stereotype.Service) Map(java.util.Map) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DeletedObjectService(org.hisp.dhis.deletedobject.DeletedObjectService) SessionFactory(org.hibernate.SessionFactory) SchemaService(org.hisp.dhis.schema.SchemaService) MergeMode(org.hisp.dhis.common.MergeMode) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) PreheatParams(org.hisp.dhis.preheat.PreheatParams) List(java.util.List) CurrentUserService(org.hisp.dhis.user.CurrentUserService) MetadataObject(org.hisp.dhis.common.MetadataObject) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Transactional(org.springframework.transaction.annotation.Transactional) TypeReport(org.hisp.dhis.feedback.TypeReport) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 37 with ObjectBundle

use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testCreatePreheatValidationsInvalidObjects.

@Test
public void testCreatePreheatValidationsInvalidObjects() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_validate2.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertFalse(validate.getTypeReportMap().isEmpty());
    assertEquals(5, validate.getErrorReportsByCode(DataElement.class, ErrorCode.E5002).size());
    assertEquals(3, validate.getErrorReportsByCode(DataElement.class, ErrorCode.E4000).size());
}
Also used : ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 38 with ObjectBundle

use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testUpdateDataSetWithSectionsAndGreyedFields.

@Test
public void testUpdateDataSetWithSectionsAndGreyedFields() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_sections_gf.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertTrue(validate.getErrorReports().isEmpty());
    objectBundleService.commit(bundle);
    Section section1 = manager.get(Section.class, "JwcV2ZifEQf");
    assertNotNull(section1.getDataSet());
    assertEquals(1, section1.getCategoryCombos().size());
    assertTrue(section1.getGreyedFields().isEmpty());
    assertEquals(1, section1.getDataElements().size());
    assertNotNull(section1.getDataSet());
    Section section2 = manager.get(Section.class, "C50M0WxaI7y");
    assertNotNull(section2.getDataSet());
    assertEquals(1, section2.getCategoryCombos().size());
    assertEquals(1, section2.getGreyedFields().size());
    assertEquals(1, section2.getDataElements().size());
    assertNotNull(section2.getDataSet());
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_sections_gf_update.json").getInputStream(), RenderFormat.JSON);
    params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.setObjects(metadata);
    bundle = objectBundleService.create(params);
    validate = objectBundleValidationService.validate(bundle);
    assertTrue(validate.getErrorReports().isEmpty());
    objectBundleService.commit(bundle);
    List<DataSet> dataSets = manager.getAll(DataSet.class);
    List<Section> sections = manager.getAll(Section.class);
    List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
    List<DataElement> dataElements = manager.getAll(DataElement.class);
    List<UserAuthorityGroup> userRoles = manager.getAll(UserAuthorityGroup.class);
    List<User> users = manager.getAll(User.class);
    List<DataElementOperand> dataElementOperands = manager.getAll(DataElementOperand.class);
    assertFalse(organisationUnits.isEmpty());
    assertFalse(dataElements.isEmpty());
    assertFalse(users.isEmpty());
    assertFalse(userRoles.isEmpty());
    assertEquals(1, dataSets.size());
    assertEquals(2, sections.size());
    assertEquals(1, dataElementOperands.size());
    DataSet dataSet = dataSets.get(0);
    assertEquals("Updated Data Set", dataSet.getName());
    assertEquals(2, dataSet.getSections().size());
    assertNotNull(dataSet.getUser());
    section1 = manager.get(Section.class, "JwcV2ZifEQf");
    assertNotNull(section1.getDataSet());
    assertEquals(1, section1.getCategoryCombos().size());
    assertEquals(1, section1.getGreyedFields().size());
    assertEquals(1, section1.getDataElements().size());
    assertNotNull(section1.getDataSet());
    section2 = manager.get(Section.class, "C50M0WxaI7y");
    assertNotNull(section2.getDataSet());
    assertEquals(1, section2.getCategoryCombos().size());
    assertTrue(section2.getGreyedFields().isEmpty());
    assertEquals(1, section2.getDataElements().size());
    assertNotNull(section2.getDataSet());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) User(org.hisp.dhis.user.User) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) DataSet(org.hisp.dhis.dataset.DataSet) Section(org.hisp.dhis.dataset.Section) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElement(org.hisp.dhis.dataelement.DataElement) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) List(java.util.List) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 39 with ObjectBundle

use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.

the class ObjectBundleServiceUserTest method testUpdateUsers.

@Test
public void testUpdateUsers() throws IOException {
    createUserAndInjectSecurityContext(true);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/users.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setAtomicMode(AtomicMode.NONE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertEquals(1, validate.getErrorReportsByCode(UserAuthorityGroup.class, ErrorCode.E5003).size());
    objectBundleService.commit(bundle);
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/users_update.json").getInputStream(), RenderFormat.JSON);
    params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.setAtomicMode(AtomicMode.NONE);
    params.setObjects(metadata);
    bundle = objectBundleService.create(params);
    validate = objectBundleValidationService.validate(bundle);
    assertEquals(1, validate.getErrorReportsByCode(UserAuthorityGroup.class, ErrorCode.E5001).size());
    objectBundleService.commit(bundle);
    List<User> users = manager.getAll(User.class);
    assertEquals(4, users.size());
    User userA = manager.get(User.class, "sPWjoHSY03y");
    User userB = manager.get(User.class, "MwhEJUnTHkn");
    assertNotNull(userA);
    assertNotNull(userB);
    assertNotNull(userA.getUserCredentials().getUserInfo());
    assertNotNull(userB.getUserCredentials().getUserInfo());
    assertNotNull(userA.getUserCredentials().getUserInfo().getUserCredentials());
    assertNotNull(userB.getUserCredentials().getUserInfo().getUserCredentials());
    assertEquals("UserAA", userA.getUserCredentials().getUserInfo().getUserCredentials().getUsername());
    assertEquals("UserBB", userB.getUserCredentials().getUserInfo().getUserCredentials().getUsername());
    assertNotNull(userA.getUserCredentials().getUser());
    assertNotNull(userB.getUserCredentials().getUser());
    assertNotNull(userA.getUserCredentials().getUser().getUserCredentials());
    assertNotNull(userB.getUserCredentials().getUser().getUserCredentials());
    assertEquals("admin", userA.getUserCredentials().getUser().getUserCredentials().getUsername());
    assertEquals("admin", userB.getUserCredentials().getUser().getUserCredentials().getUsername());
}
Also used : User(org.hisp.dhis.user.User) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 40 with ObjectBundle

use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.

the class ObjectBundleServiceUserTest method testCreateUsers.

@Test
public void testCreateUsers() throws IOException {
    createUserAndInjectSecurityContext(true);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/users.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setAtomicMode(AtomicMode.NONE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertEquals(1, validate.getErrorReportsByCode(UserAuthorityGroup.class, ErrorCode.E5003).size());
    objectBundleService.commit(bundle);
    List<User> users = manager.getAll(User.class);
    assertEquals(4, users.size());
    User userA = manager.get(User.class, "sPWjoHSY03y");
    User userB = manager.get(User.class, "MwhEJUnTHkn");
    assertNotNull(userA);
    assertNotNull(userB);
    assertNotNull(userA.getUserCredentials().getUserInfo());
    assertNotNull(userB.getUserCredentials().getUserInfo());
    assertNotNull(userA.getUserCredentials().getUserInfo().getUserCredentials());
    assertNotNull(userB.getUserCredentials().getUserInfo().getUserCredentials());
    assertEquals("UserA", userA.getUserCredentials().getUserInfo().getUserCredentials().getUsername());
    assertEquals("UserB", userB.getUserCredentials().getUserInfo().getUserCredentials().getUsername());
    assertNotNull(userA.getUserCredentials().getUser());
    assertNotNull(userB.getUserCredentials().getUser());
    assertNotNull(userA.getUserCredentials().getUser().getUserCredentials());
    assertNotNull(userB.getUserCredentials().getUser().getUserCredentials());
    assertEquals("admin", userA.getUserCredentials().getUser().getUserCredentials().getUsername());
    assertEquals("admin", userB.getUserCredentials().getUser().getUserCredentials().getUsername());
    assertEquals(1, userA.getOrganisationUnits().size());
    assertEquals(1, userB.getOrganisationUnits().size());
}
Also used : User(org.hisp.dhis.user.User) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)40 List (java.util.List)38 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)38 DhisSpringTest (org.hisp.dhis.DhisSpringTest)36 Test (org.junit.Test)36 ClassPathResource (org.springframework.core.io.ClassPathResource)36 DataElement (org.hisp.dhis.dataelement.DataElement)20 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)14 User (org.hisp.dhis.user.User)14 DataSet (org.hisp.dhis.dataset.DataSet)12 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)10 ObjectReport (org.hisp.dhis.feedback.ObjectReport)7 Section (org.hisp.dhis.dataset.Section)5 TypeReport (org.hisp.dhis.feedback.TypeReport)4 ObjectBundleCommitReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport)3 Program (org.hisp.dhis.program.Program)3 ProgramStage (org.hisp.dhis.program.ProgramStage)3 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)3 ValidationRule (org.hisp.dhis.validation.ValidationRule)3 HashMap (java.util.HashMap)2