Search in sources :

Example 1 with ObjectTranslation

use of org.hisp.dhis.translation.ObjectTranslation in project dhis2-core by dhis2.

the class AbstractCrudController method replaceTranslations.

@RequestMapping(value = "/{uid}/translations", method = RequestMethod.PUT)
public void replaceTranslations(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
    WebOptions options = new WebOptions(rpParameters);
    List<T> entities = getEntity(pvUid, options);
    if (entities.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
    }
    T persistedObject = entities.get(0);
    User user = currentUserService.getCurrentUser();
    if (!aclService.canUpdate(user, persistedObject)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    T object = renderService.fromJson(request.getInputStream(), getEntityClass());
    TypeReport typeReport = new TypeReport(ObjectTranslation.class);
    List<ObjectTranslation> objectTranslations = Lists.newArrayList(object.getTranslations());
    for (int idx = 0; idx < object.getTranslations().size(); idx++) {
        ObjectReport objectReport = new ObjectReport(ObjectTranslation.class, idx);
        ObjectTranslation translation = objectTranslations.get(idx);
        if (translation.getLocale() == null) {
            objectReport.addErrorReport(new ErrorReport(ObjectTranslation.class, ErrorCode.E4000, "locale").setErrorKlass(getEntityClass()));
        }
        if (translation.getProperty() == null) {
            objectReport.addErrorReport(new ErrorReport(ObjectTranslation.class, ErrorCode.E4000, "property").setErrorKlass(getEntityClass()));
        }
        if (translation.getValue() == null) {
            objectReport.addErrorReport(new ErrorReport(ObjectTranslation.class, ErrorCode.E4000, "value").setErrorKlass(getEntityClass()));
        }
        typeReport.addObjectReport(objectReport);
        if (!objectReport.isEmpty()) {
            typeReport.getStats().incIgnored();
        }
    }
    if (!typeReport.getErrorReports().isEmpty()) {
        WebMessage webMessage = WebMessageUtils.typeReport(typeReport);
        webMessageService.send(webMessage, response, request);
        return;
    }
    manager.updateTranslations(persistedObject, object.getTranslations());
    manager.update(persistedObject);
    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
Also used : User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ObjectTranslation(org.hisp.dhis.translation.ObjectTranslation) ObjectReport(org.hisp.dhis.feedback.ObjectReport) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) ErrorReport(org.hisp.dhis.feedback.ErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ObjectTranslation

use of org.hisp.dhis.translation.ObjectTranslation in project dhis2-core by dhis2.

the class BaseIdentifiableObject method loadTranslationsCacheIfEmpty.

/**
     * Populates the translationsCache map unless it is already populated.
     */
private void loadTranslationsCacheIfEmpty() {
    if (translationCache.isEmpty()) {
        for (ObjectTranslation translation : translations) {
            if (translation.getLocale() != null && translation.getProperty() != null && !StringUtils.isEmpty(translation.getValue())) {
                String key = ObjectTranslation.getCacheKey(translation.getLocale(), translation.getProperty());
                translationCache.put(key, translation.getValue());
            }
        }
    }
}
Also used : ObjectTranslation(org.hisp.dhis.translation.ObjectTranslation)

Example 3 with ObjectTranslation

use of org.hisp.dhis.translation.ObjectTranslation in project dhis2-core by dhis2.

the class TranslationWebApiTest method testOK.

@Test
public void testOK() throws Exception {
    Locale locale = Locale.FRENCH;
    MockHttpSession session = getSession("ALL");
    DataElement dataElementA = createDataElement('A');
    identifiableObjectManager.save(dataElementA);
    String valueToCheck = "frenchTranslated";
    dataElementA.getTranslations().add(new ObjectTranslation(locale.getLanguage(), TranslationProperty.NAME, valueToCheck));
    mvc.perform(put("/dataElements/" + dataElementA.getUid() + "/translations").session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(TestUtils.convertObjectToJsonBytes(dataElementA))).andExpect(status().is(HttpStatus.SC_NO_CONTENT));
    MvcResult result = mvc.perform(get("/dataElements/" + dataElementA.getUid() + "?locale=" + locale.getLanguage()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andReturn();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(result.getResponse().getContentAsString());
    assertEquals(valueToCheck, node.get("displayName").asText());
}
Also used : Locale(java.util.Locale) DataElement(org.hisp.dhis.dataelement.DataElement) MockHttpSession(org.springframework.mock.web.MockHttpSession) ObjectTranslation(org.hisp.dhis.translation.ObjectTranslation) JsonNode(org.codehaus.jackson.JsonNode) MvcResult(org.springframework.test.web.servlet.MvcResult) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest)

Example 4 with ObjectTranslation

use of org.hisp.dhis.translation.ObjectTranslation 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)

Aggregations

ObjectTranslation (org.hisp.dhis.translation.ObjectTranslation)4 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 JsonNode (org.codehaus.jackson.JsonNode)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)1 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)1 ErrorReport (org.hisp.dhis.feedback.ErrorReport)1 ObjectReport (org.hisp.dhis.feedback.ObjectReport)1 TypeReport (org.hisp.dhis.feedback.TypeReport)1 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)1 TranslationProperty (org.hisp.dhis.translation.TranslationProperty)1 User (org.hisp.dhis.user.User)1 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)1 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)1 Test (org.junit.Test)1 MockHttpSession (org.springframework.mock.web.MockHttpSession)1