Search in sources :

Example 91 with IdentifiableObject

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

the class TranslateAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    className = className != null && CLASS_ALIAS.containsKey(className) ? CLASS_ALIAS.get(className) : className;
    log.info("Classname: " + className + ", uid: " + uid + ", loc: " + loc);
    IdentifiableObject object = identifiableObjectManager.getObject(uid, className);
    HttpServletRequest request = ServletActionContext.getRequest();
    Set<ObjectTranslation> listObjectTranslation = new HashSet<>(object.getTranslations());
    for (TranslationProperty p : TranslationProperty.values()) {
        Enumeration<String> paramNames = request.getParameterNames();
        Collections.list(paramNames).forEach(paramName -> {
            if (paramName.equalsIgnoreCase(p.getName())) {
                String[] paramValues = request.getParameterValues(paramName);
                if (!ArrayUtils.isEmpty(paramValues) && StringUtils.isNotEmpty(paramValues[0])) {
                    listObjectTranslation.removeIf(o -> o.getProperty().equals(p) && o.getLocale().equalsIgnoreCase(loc));
                    listObjectTranslation.add(new ObjectTranslation(loc, p, paramValues[0]));
                }
            }
        });
    }
    identifiableObjectManager.updateTranslations(object, listObjectTranslation);
    return SUCCESS;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TranslationProperty(org.hisp.dhis.translation.TranslationProperty) ObjectTranslation(org.hisp.dhis.translation.ObjectTranslation) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HashSet(java.util.HashSet)

Example 92 with IdentifiableObject

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

the class AbstractWebApiTest method testGetAll.

@Test
public void testGetAll() throws Exception {
    Map<Class<? extends IdentifiableObject>, IdentifiableObject> defaultObjectMap = manager.getDefaults();
    IdentifiableObject defaultTestObject = defaultObjectMap.get(testClass);
    int valueToTest = defaultTestObject != null ? 5 : 4;
    manager.save(createTestObject(testClass, 'A'));
    manager.save(createTestObject(testClass, 'B'));
    manager.save(createTestObject(testClass, 'C'));
    manager.save(createTestObject(testClass, 'D'));
    MockHttpSession session = getSession("ALL");
    List<FieldDescriptor> fieldDescriptors = new ArrayList<>();
    fieldDescriptors.addAll(ResponseDocumentation.pager());
    fieldDescriptors.add(fieldWithPath(schema.getPlural()).description(schema.getPlural()));
    mvc.perform(get(schema.getRelativeApiEndpoint()).session(session).accept(TestUtils.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(TestUtils.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$." + schema.getPlural()).isArray()).andExpect(jsonPath("$." + schema.getPlural() + ".length()").value(valueToTest)).andDo(documentPrettyPrint(schema.getPlural() + "/all", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : ArrayList(java.util.ArrayList) MockHttpSession(org.springframework.mock.web.MockHttpSession) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) Test(org.junit.Test)

Example 93 with IdentifiableObject

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

the class MetadataSyncImportHandler method importMetadata.

public MetadataSyncSummary importMetadata(MetadataSyncParams syncParams, String versionSnapShot) {
    MetadataVersion version = getMetadataVersion(syncParams);
    MetadataImportParams importParams = syncParams.getImportParams();
    MetadataSyncSummary metadataSyncSummary = new MetadataSyncSummary();
    if (importParams == null) {
        throw new MetadataSyncServiceException("MetadataImportParams for the Sync cant be null.");
    }
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> classListMap = parseClassListMap(versionSnapShot);
    if (classListMap == null) {
        throw new MetadataSyncServiceException("ClassListMap can't be null");
    }
    importParams.setObjects(classListMap);
    ImportReport importReport = null;
    try {
        importReport = metadataImportService.importMetadata(importParams);
    } catch (Exception e) {
        String message = "Exception occurred while trying to import the metadata. " + e.getMessage();
        log.error(message, e);
        throw new MetadataSyncImportException(message, e);
    }
    boolean addNewVersion = handleImportReport(importReport, version);
    if (addNewVersion) {
        try {
            metadataVersionDelegate.addNewMetadataVersion(version);
        } catch (MetadataVersionServiceException e) {
            throw new MetadataSyncServiceException(e.getMessage(), e);
        }
    }
    metadataSyncSummary.setImportReport(importReport);
    metadataSyncSummary.setMetadataVersion(version);
    return metadataSyncSummary;
}
Also used : MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) IOException(java.io.IOException) MetadataSyncImportException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncImportException) MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) MetadataSyncImportException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncImportException) List(java.util.List)

Example 94 with IdentifiableObject

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

the class UserRoleObjectBundleHook method postCommit.

@Override
@SuppressWarnings("unchecked")
public void postCommit(ObjectBundle bundle) {
    if (!bundle.getObjectMap().containsKey(UserAuthorityGroup.class))
        return;
    List<IdentifiableObject> objects = bundle.getObjectMap().get(UserAuthorityGroup.class);
    Map<String, Map<String, Object>> userRoleReferences = bundle.getObjectReferences(UserAuthorityGroup.class);
    if (userRoleReferences == null || userRoleReferences.isEmpty()) {
        return;
    }
    for (IdentifiableObject object : objects) {
        object = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
        Map<String, Object> userRoleReferenceMap = userRoleReferences.get(object.getUid());
        if (userRoleReferenceMap == null || userRoleReferenceMap.isEmpty()) {
            continue;
        }
        UserAuthorityGroup userRole = (UserAuthorityGroup) object;
        userRole.setDataSets((Set<DataSet>) userRoleReferenceMap.get("dataSets"));
        userRole.setPrograms((Set<Program>) userRoleReferenceMap.get("programs"));
        preheatService.connectReferences(userRole, bundle.getPreheat(), bundle.getPreheatIdentifier());
        sessionFactory.getCurrentSession().update(userRole);
    }
}
Also used : Program(org.hisp.dhis.program.Program) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) DataSet(org.hisp.dhis.dataset.DataSet) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Map(java.util.Map) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 95 with IdentifiableObject

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

the class AbstractGridView method renderMergedOutputModel.

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Object object = model.get("model");
    List<Grid> grids = new ArrayList<>();
    if (WebMetadata.class.isAssignableFrom(object.getClass())) {
        WebMetadata metadata = (WebMetadata) object;
        Collection<Field> fields = ReflectionUtils.collectFields(WebMetadata.class, PredicateUtils.idObjectCollections);
        for (Field field : fields) {
            List<IdentifiableObject> identifiableObjects = ReflectionUtils.invokeGetterMethod(field.getName(), metadata);
            if (identifiableObjects == null || identifiableObjects.isEmpty()) {
                continue;
            }
            Grid grid = new ListGrid();
            grid.setTitle(identifiableObjects.get(0).getClass().getSimpleName() + "s");
            boolean nameable = false;
            grid.addHeader(new GridHeader("UID", false, false));
            grid.addHeader(new GridHeader("Name", false, false));
            if (NameableObject.class.isAssignableFrom(identifiableObjects.get(0).getClass())) {
                grid.addHeader(new GridHeader("ShortName", false, false));
                nameable = true;
            }
            grid.addHeader(new GridHeader("Code", false, false));
            for (IdentifiableObject identifiableObject : identifiableObjects) {
                grid.addRow();
                grid.addValue(identifiableObject.getUid());
                grid.addValue(identifiableObject.getName());
                if (nameable) {
                    grid.addValue(((NameableObject) identifiableObject).getShortName());
                }
                grid.addValue(identifiableObject.getCode());
            }
            grids.add(grid);
        }
    } else {
        IdentifiableObject identifiableObject = (IdentifiableObject) object;
        Grid grid = new ListGrid();
        grid.setTitle(identifiableObject.getClass().getSimpleName());
        grid.addEmptyHeaders(2);
        grid.addRow().addValue("UID").addValue(identifiableObject.getUid());
        grid.addRow().addValue("Name").addValue(identifiableObject.getName());
        if (NameableObject.class.isAssignableFrom(identifiableObject.getClass())) {
            grid.addRow().addValue("ShortName").addValue(((NameableObject) identifiableObject).getShortName());
            grid.addRow().addValue("Description").addValue(((NameableObject) identifiableObject).getDescription());
        }
        grid.addRow().addValue("Code").addValue(identifiableObject.getCode());
        grids.add(grid);
    }
    renderGrids(grids, response);
}
Also used : Field(java.lang.reflect.Field) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) NameableObject(org.hisp.dhis.common.NameableObject) ListGrid(org.hisp.dhis.system.grid.ListGrid) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) GridHeader(org.hisp.dhis.common.GridHeader)

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