Commit c1e22c09 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'ui-152-cms-with-transifex' into 'master'

Added api endpoint for transifex posting/fetching/deleting

See merge request genesys-pgr/genesys-server!310
parents f1d2b495 4d40a724
......@@ -20,9 +20,15 @@ import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import io.swagger.annotations.Api;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.ClassPK;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys.catalog.service.ShortFilterService.FilterInfo;
import org.genesys2.server.api.ApiBaseController;
......@@ -36,9 +42,15 @@ import org.genesys2.server.model.impl.MenuItem;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.filter.ArticleFilter;
import org.genesys2.spring.ResourceNotFoundException;
import org.genesys2.transifex.client.TransifexException;
import org.genesys2.transifex.client.TransifexService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
......@@ -46,10 +58,14 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* The Class CMSController.
*
......@@ -74,6 +90,17 @@ public class CMSController {
@Autowired
private ShortFilterService shortFilterService;
@Autowired(required = false)
private TransifexService transifexService;
@Resource
private Set<String> supportedLocales;
private static final String defaultLanguage = "en";
@Autowired
private ObjectMapper mapper;
/**
* List of last 6 ActivityPost.
*/
......@@ -325,4 +352,136 @@ public class CMSController {
return contentService.ensureMenuItem(menuKey, url, text);
}
@PostMapping(value = "/transifex")
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public Article postToTransifex(@RequestParam("slug") String slug,
@RequestParam(value = "targetId", required = false) Long targetId, @RequestParam("classPkShortName") String classPkShortName) throws IOException {
if (transifexService == null) {
throw new ResourceNotFoundException("translationService not enabled");
}
Article article;
String resourceName;
if (targetId != null) {
article = contentService.getArticleBySlugLangTargetIdClassPk(slug, LocaleContextHolder.getLocale().getLanguage(), targetId, classPkShortName);
resourceName = "article-" + slug + "-" + classPkShortName + "-" + targetId;
} else {
article = contentService.getGlobalArticle(slug, LocaleContextHolder.getLocale());
resourceName = "article-" + slug;
}
String body = String.format("<div class=\"summary\">%s</div><div class=\"body\">%s</div>", article.getSummary(), article.getBody());
if (transifexService.resourceExists(resourceName)) {
transifexService.updateXhtmlResource(resourceName, article.getTitle(), body);
} else {
transifexService.createXhtmlResource(resourceName, article.getTitle(), body);
}
return article;
}
@DeleteMapping(value = "/transifex")
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public Boolean deleteFromTransifex(@RequestParam("slug") String slug,
@RequestParam(value = "targetId", required = false) Long targetId, @RequestParam("classPkShortName") String classPkShortName) throws TransifexException {
if (transifexService == null) {
throw new ResourceNotFoundException("translationService not enabled");
}
String resourceName;
if (targetId != null) {
resourceName = "article-" + slug + "-" + classPkShortName + "-" + targetId;
} else {
resourceName = "article-" + slug;
}
return transifexService.deleteResource(resourceName);
}
/**
* Fetch all from Transifex and store
*
* @param slug - article slug
* @return map with results of fetching
* @throws Exception
*/
@PostMapping(value = "/transifex/fetchAll")
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public Map<String, String> fetchAllFromTransifex(@RequestParam("slug") String slug, @RequestParam(value = "targetId", required = false) Long targetId,
@RequestParam(value = "classPkShortName", required = false) String classPkShortName, @RequestParam(name = "template", defaultValue = "false") Boolean template) throws Exception {
if (transifexService == null) {
throw new ResourceNotFoundException("translationService not enabled");
}
String resourceName;
if (targetId != null) {
resourceName = "article-" + slug + "-" + classPkShortName + "-" + targetId;
} else {
resourceName = "article-" + slug;
}
Map<String, String> responses = new HashMap<>(20);
for (String lang : supportedLocales) {
if (defaultLanguage.equalsIgnoreCase(lang)) {
continue;
}
Locale locale = Locale.forLanguageTag(lang);
LOG.info("Fetching article {} translation for {}", resourceName, locale);
String translatedResource;
try {
translatedResource = transifexService.getTranslatedResource(resourceName, locale);
} catch (TransifexException e) {
LOG.warn(e.getMessage(), e);
if (e.getCause() != null) {
responses.put(lang, e.getLocalizedMessage() + ": " + e.getCause().getLocalizedMessage());
} else {
responses.put(lang, e.getLocalizedMessage());
}
// throw new Exception(e.getMessage(), e);
continue;
}
String title;
String body;
String summary = null;
try {
JsonNode jsonObject = mapper.readTree(translatedResource);
String content = jsonObject.get("content").asText();
Document doc = Jsoup.parse(content);
title = doc.title();
if (content.contains("class=\"summary")) {
// 1st <div class="summary">...
summary = doc.body().child(0).html();
// 2nd <div class="body">...
body = doc.body().child(1).html();
} else {
// Old fashioned body-only approach
body = doc.body().html();
}
if (targetId != null && StringUtils.isNotBlank(classPkShortName)) {
ClassPK classPk = contentService.getClassPk(classPkShortName);
contentService.updateArticle(Class.forName(classPk.getClassname()), targetId, slug, title, body, summary, new Locale(lang));
responses.put(lang, "article.translations-updated");
} else if (targetId == null && StringUtils.isBlank(classPkShortName)) {
contentService.updateGlobalArticle(slug, locale, title, body, summary);
responses.put(lang, "article.translations-updated");
} else {
responses.put(lang, "Error updating local content");
}
} catch (IOException e) {
LOG.warn(e.getMessage(), e);
responses.put(lang, e.getMessage());
}
}
return responses;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment