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);
}
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());
}
}
}
}
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());
}
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;
}
Aggregations