Commit 6389d253 authored by Matija Obreza's avatar Matija Obreza
Browse files

Push Article#summary to Transifex for translation

parent b434dfe4
......@@ -27,6 +27,8 @@ import org.genesys2.spring.RequestAttributeLocaleResolver;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.PageRequest;
......@@ -56,7 +58,8 @@ public class ArticleController extends BaseController {
@Autowired
private RequestAttributeLocaleResolver localeResolver;
private ObjectMapper mapper = new ObjectMapper();
@Autowired
private ObjectMapper mapper;
@RequestMapping(value = "/transifex", params = { "post" }, method = RequestMethod.POST)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
......@@ -66,11 +69,13 @@ public class ArticleController extends BaseController {
}
Article article = contentService.getGlobalArticle(slug, getLocale());
String resourceName = "article-" + slug;
String body = String.format("<div class=\"summary\">%s</div><div class=\"body\">%s</div>", article.getSummary(), article.getBody());
try {
if (transifexService.resourceExists(resourceName)) {
transifexService.updateXhtmlResource(resourceName, article.getTitle(), article.getBody());
transifexService.updateXhtmlResource(resourceName, article.getTitle(), body);
} else {
transifexService.createXhtmlResource(resourceName, article.getTitle(), article.getBody());
transifexService.createXhtmlResource(resourceName, article.getTitle(), body);
}
} catch (IOException e) {
model.addAttribute("responseFromTransifex", "article.transifex-failed");
......@@ -127,12 +132,23 @@ public class ArticleController extends BaseController {
String title = null;
String body = null;
String summary = null;
try {
JsonNode jsonObject = mapper.readTree(translatedResource);
String content = jsonObject.get("content").asText();
title = content.split("<title>")[1].split("</title>")[0];
body = content.split("<body>")[1].split("</body>")[0];
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();
}
} catch (IOException e) {
_logger.error(e.getMessage(), e);
throw e;
......@@ -145,6 +161,7 @@ public class ArticleController extends BaseController {
article.setTitle(title);
article.setBody(body);
article.setSummary(summary);
article.setLang(language);
model.addAttribute("article", article);
......@@ -249,7 +266,8 @@ public class ArticleController extends BaseController {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@RequestMapping(value = "/save-article/{language}", params = { "id" }, method = { RequestMethod.POST })
public String saveExistingGlobalArticle(ModelMap model, @PathVariable("language") String language, @RequestParam("id") long id,
@RequestParam("slug") String slug, @RequestParam("title") String title, @RequestParam("body") String body, @RequestParam(value = "summary", required = false) String summary) {
@RequestParam("slug") String slug, @RequestParam("title") String title, @RequestParam("body") String body,
@RequestParam(value = "summary", required = false) String summary) {
Article article = contentService.updateArticle(id, slug, title, body, summary);
......@@ -267,7 +285,8 @@ public class ArticleController extends BaseController {
@RequestMapping(value = "/blurp/create-blurp", method = { RequestMethod.POST })
public String createBlurp(ModelMap model, @RequestParam("clazz") String clazz, @RequestParam("entityId") long entityId,
@RequestParam(required = false, value = "title") String title, @RequestParam("body") String body, @RequestParam(value = "summary", required = false) String summary) throws ClassNotFoundException {
@RequestParam(required = false, value = "title") String title, @RequestParam("body") String body,
@RequestParam(value = "summary", required = false) String summary) throws ClassNotFoundException {
contentService.updateArticle(Class.forName(clazz), entityId, "blurp", title, body, summary, getLocale());
return "redirect:/";
......
......@@ -16,6 +16,7 @@
package org.genesys2.server.servlet.controller.transifex;
import java.io.IOException;
import java.util.Locale;
import org.genesys2.server.aspect.AsAdmin;
......@@ -24,6 +25,8 @@ import org.genesys2.server.service.ContentService;
import org.genesys2.server.servlet.controller.BaseController;
import org.genesys2.transifex.client.TransifexException;
import org.genesys2.transifex.client.TransifexService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
......@@ -34,6 +37,9 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Transifex web hook listener.
*
......@@ -50,15 +56,15 @@ public class TransifexAPIController extends BaseController {
@Autowired(required = false)
private TransifexService transifexService;
@Value("${transifex.content.template}")
private String contentTemplate;
@Value("${transifex.min.translated}")
private int transifexMinTranslated;
@Value("${transifex.hook.key}")
private Object transifexHookKey;
@Autowired
private ObjectMapper mapper;
/**
* Note: the hook key value should be set in preferences
*/
......@@ -107,12 +113,35 @@ public class TransifexAPIController extends BaseController {
_logger.warn("Locale: " + locale);
String resourceBody = transifexService.getTranslatedResource("article-".concat(slug), locale);
String title = resourceBody.split("<title>")[1].split("</title>")[0];
_logger.warn("Title: " + title);
String body = resourceBody.split("<body>")[1].split("</body>")[0];
_logger.warn("Body: " + body);
String title = null;
String body = null;
String summary = null;
try {
JsonNode jsonObject = mapper.readTree(resourceBody);
String content = jsonObject.get("content").asText();
Document doc = Jsoup.parse(content);
title = doc.title();
_logger.info("Title: " + title);
if (content.contains("class=\"summary")) {
// 1st <div class="summary">...
summary = doc.body().child(0).html();
_logger.info("Summary: " + summary);
// 2nd <div class="body">...
body = doc.body().child(1).html();
_logger.info("Body: " + body);
} else {
// Old fashioned body-only approach
body = doc.body().html();
}
} catch (IOException e) {
_logger.error(e.getMessage(), e);
throw new TransifexException("Oops", e);
}
// Extract article from database we need (correct locale + do not use
// default (EN) language)
......
......@@ -20,7 +20,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.transifex.client.TransifexService;
import org.genesys2.transifex.client.TransifexServiceImpl;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
......@@ -34,32 +33,8 @@ public class ApplicationConfig {
public static final Log LOG = LogFactory.getLog(ApplicationConfig.class);
@Bean
public FactoryBean<TransifexService> transifexService() {
TransifexService i = null;
try {
i = new TransifexServiceImpl();
} catch (Throwable e) {
LOG.warn("Transifex integration not enabled: " + e.getMessage(), e);
}
final TransifexService instance = i;
return new FactoryBean<TransifexService>() {
@Override
public TransifexService getObject() throws Exception {
return instance;
}
@Override
public Class<?> getObjectType() {
return TransifexService.class;
}
@Override
public boolean isSingleton() {
return true;
}
};
public TransifexService transifexService() {
return new TransifexServiceImpl();
}
}
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