Search in sources :

Example 46 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testCreateAndUpdateMetadata1.

@Test
public void testCreateAndUpdateMetadata1() throws IOException {
    defaultSetup();
    Map<String, DataElement> dataElementMap = manager.getIdMap(DataElement.class, IdScheme.UID);
    UserGroup userGroup = manager.get(UserGroup.class, "ugabcdefghA");
    assertEquals(4, dataElementMap.size());
    assertNotNull(userGroup);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_create_and_update1.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE_AND_UPDATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    assertTrue(objectBundleValidationService.validate(bundle).getErrorReports().isEmpty());
    objectBundleService.commit(bundle);
    DataElement dataElementA = dataElementMap.get("deabcdefghA");
    DataElement dataElementB = dataElementMap.get("deabcdefghB");
    DataElement dataElementC = dataElementMap.get("deabcdefghC");
    DataElement dataElementD = dataElementMap.get("deabcdefghD");
    assertNotNull(dataElementA);
    assertNotNull(dataElementB);
    assertNotNull(dataElementC);
    assertNotNull(dataElementD);
    assertEquals("DEA", dataElementA.getName());
    assertEquals("DEB", dataElementB.getName());
    assertEquals("DEC", dataElementC.getName());
    assertEquals("DED", dataElementD.getName());
    assertEquals("DECA", dataElementA.getCode());
    assertEquals("DECB", dataElementB.getCode());
    assertEquals("DECC", dataElementC.getCode());
    assertEquals("DECD", dataElementD.getCode());
    assertEquals("DESA", dataElementA.getShortName());
    assertEquals("DESB", dataElementB.getShortName());
    assertEquals("DESC", dataElementC.getShortName());
    assertEquals("DESD", dataElementD.getShortName());
    assertEquals("DEDA", dataElementA.getDescription());
    assertEquals("DEDB", dataElementB.getDescription());
    assertEquals("DEDC", dataElementC.getDescription());
    assertEquals("DEDD", dataElementD.getDescription());
    assertEquals(1, dataElementA.getUserGroupAccesses().size());
    assertEquals(0, dataElementB.getUserGroupAccesses().size());
    assertEquals(1, dataElementC.getUserGroupAccesses().size());
    assertEquals(0, dataElementD.getUserGroupAccesses().size());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) UserGroup(org.hisp.dhis.user.UserGroup) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 47 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class HibernateGenericStore method save.

@Override
public void save(T object, User user, boolean clearSharing) {
    String username = user != null ? user.getUsername() : "system-process";
    if (IdentifiableObject.class.isAssignableFrom(object.getClass())) {
        BaseIdentifiableObject identifiableObject = (BaseIdentifiableObject) object;
        identifiableObject.setAutoFields();
        identifiableObject.setLastUpdatedBy(user);
        if (clearSharing) {
            identifiableObject.setPublicAccess(AccessStringHelper.DEFAULT);
            if (identifiableObject.getUserGroupAccesses() != null) {
                identifiableObject.getUserGroupAccesses().clear();
            }
            if (identifiableObject.getUserAccesses() != null) {
                identifiableObject.getUserAccesses().clear();
            }
        }
        if (identifiableObject.getUser() == null) {
            identifiableObject.setUser(user);
        }
    }
    if (user != null && aclService.isShareable(clazz)) {
        BaseIdentifiableObject identifiableObject = (BaseIdentifiableObject) object;
        if (clearSharing) {
            if (aclService.canMakePublic(user, identifiableObject.getClass())) {
                if (aclService.defaultPublic(identifiableObject.getClass())) {
                    identifiableObject.setPublicAccess(AccessStringHelper.READ_WRITE);
                }
            } else if (aclService.canMakePrivate(user, identifiableObject.getClass())) {
                identifiableObject.setPublicAccess(AccessStringHelper.newInstance().build());
            }
        }
        if (!checkPublicAccess(user, identifiableObject)) {
            AuditLogUtil.infoWrapper(log, username, object, AuditLogUtil.ACTION_CREATE_DENIED);
            throw new CreateAccessDeniedException(object.toString());
        }
    }
    AuditLogUtil.infoWrapper(log, username, object, AuditLogUtil.ACTION_CREATE);
    getSession().save(object);
    if (MetadataObject.class.isInstance(object)) {
        deletedObjectService.deleteDeletedObjects(new DeletedObjectQuery((IdentifiableObject) object));
    }
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) CreateAccessDeniedException(org.hisp.dhis.hibernate.exception.CreateAccessDeniedException) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject)

Example 48 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class DataElementCategoryControllerDocumentation method testAddDeleteCollectionItem.

@Test
public void testAddDeleteCollectionItem() throws Exception {
    MockHttpSession session = getSession("ALL");
    DataElementCategory category = createDataElementCategory('A');
    manager.save(category);
    Schema schema = schemaService.getSchema(DataElementCategory.class);
    List<Property> properties = schema.getProperties();
    for (Property property : properties) {
        if (property.isCollection()) {
            String collectionName = property.getCollectionName();
            IdentifiableObject item = createTestObject(property.getItemKlass(), 'A', category);
            if (item == null) {
                continue;
            } else {
                manager.save(item);
            }
            mvc.perform(post("/" + ENDPOINT + "/" + category.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint(ENDPOINT + "/add" + collectionName)).andExpect(status().isNoContent());
            mvc.perform(delete("/" + ENDPOINT + "/" + category.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint(ENDPOINT + "/delete" + collectionName)).andExpect(status().isNoContent());
        }
    }
}
Also used : Schema(org.hisp.dhis.schema.Schema) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) MockHttpSession(org.springframework.mock.web.MockHttpSession) Property(org.hisp.dhis.schema.Property) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

Example 49 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class DataElementControllerDocumentation method testAddDeleteCollectionItem.

@Test
public void testAddDeleteCollectionItem() throws Exception {
    MockHttpSession session = getSession("ALL");
    DataElement de = createDataElement('A');
    manager.save(de);
    Schema schema = schemaService.getSchema(DataElement.class);
    List<Property> properties = schema.getProperties();
    for (Property property : properties) {
        if (property.isCollection()) {
            String collectionName = property.getCollectionName();
            IdentifiableObject item = createTestObject(property.getItemKlass(), 'A');
            if (item == null) {
                continue;
            } else {
                manager.save(item);
            }
            mvc.perform(post("/dataElements/" + de.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint("data-elements/add" + collectionName)).andExpect(status().isNoContent());
            mvc.perform(delete("/dataElements/" + de.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint("data-elements/delete" + collectionName)).andExpect(status().isNoContent());
        }
    }
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Schema(org.hisp.dhis.schema.Schema) MockHttpSession(org.springframework.mock.web.MockHttpSession) Property(org.hisp.dhis.schema.Property) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

Example 50 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class GetTranslationsAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    className = className != null && CLASS_ALIAS.containsKey(className) ? CLASS_ALIAS.get(className) : className;
    Locale locale = LocaleUtils.getLocale(loc);
    IdentifiableObject object = identifiableObjectManager.getObject(objectUid, className);
    translations = TranslationUtils.convertTranslations(object.getTranslations(), locale);
    return SUCCESS;
}
Also used : Locale(java.util.Locale) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)124 List (java.util.List)76 Test (org.junit.Test)67 DhisSpringTest (org.hisp.dhis.DhisSpringTest)64 ClassPathResource (org.springframework.core.io.ClassPathResource)54 DataElement (org.hisp.dhis.dataelement.DataElement)44 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)39 User (org.hisp.dhis.user.User)37 DataSet (org.hisp.dhis.dataset.DataSet)24 ArrayList (java.util.ArrayList)22 ObjectReport (org.hisp.dhis.feedback.ObjectReport)22 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)22 Schema (org.hisp.dhis.schema.Schema)19 HashMap (java.util.HashMap)18 TypeReport (org.hisp.dhis.feedback.TypeReport)18 Set (java.util.Set)15 ErrorReport (org.hisp.dhis.feedback.ErrorReport)15 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)15 Map (java.util.Map)14 Property (org.hisp.dhis.schema.Property)13