Commit 1b1861fd authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza
Browse files

Fix wrong merge of c8ec8d78e3e6e193cd740dab92f69afa3d1ec1d8 commit

parent 4b168563
......@@ -20,21 +20,20 @@ import org.genesys2.server.model.impl.Article;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.servlet.controller.BaseController;
import org.genesys2.transifex.client.TransifexService;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Transifex web hook listener.
*
* @author matijaobreza
*
*/
@Controller
@RequestMapping(value = "/transifex")
public class TransifexAPIController extends BaseController {
......@@ -45,66 +44,72 @@ public class TransifexAPIController extends BaseController {
@Autowired
private TransifexService transifexService;
@Value("${transifex.project}")
private String projectSlug;
@Value("${transifex.username}")
private String trasifexUserName;
@Value("${transifex.password}")
private String transifexPassord;
@Value("${transifex.base.api.url}")
private String baseApiURL;
@Value("${transifex.content.template}")
private String contentTemplate;
@Value("${transifex.min.translated}")
private int transifexMinTranslated;
@Value("${transifex.hook.key}")
private Object transifexHookKey;
/**
* Use "magic" SHA1 hash value just for some security purposes
* <p/>
*
* Note: the very same value should be in Transifex preferences
*
* @see classpath:spring/spring-security.xml
*/
@RequestMapping(value = "/hooks/40874cca86ca396169a5f4e6ebf6e4bf7199c4e7", method = RequestMethod.POST)
public void webHookHandle(@RequestParam(value = "project") String projectSlug,
@RequestParam(value = "resource") String resource,
@RequestParam(value = "language") String language,
@RequestParam(value = "translated") int translatedPercentage) throws UnsupportedEncodingException {
//TODO may be revisit this
//currently we do this due to nature of Transifex hook
String articleSlug = resource.split("-")[1];
Article article = new Article();
JSONObject jsonObject;
JSONParser jsonParser = new JSONParser();
String translatedResource = transifexService.getTranslatedResource(resource, new Locale(language));
String content;
String title = null;
String body = null;
try {
jsonObject = (JSONObject) jsonParser.parse(translatedResource);
content = (String) jsonObject.get("content");
title = content.split("<title>")[1].split("</title>")[0];
body = content.split("<body>")[1].split("</body>")[0];
} catch (ParseException e) {
e.printStackTrace();
@RequestMapping(value = "/hook/{hookKey:.+}", method = RequestMethod.POST)
public @ResponseBody
String webHookHandle(@PathVariable("hookKey") String hookKey, @RequestParam(value = "project") String projectSlug,
@RequestParam(value = "resource") String resource, @RequestParam(value = "language") String language,
@RequestParam(value = "translated") Integer translatedPercentage, Model model) {
if (!transifexHookKey.equals(hookKey)) {
_logger.error("Invalid key provided for Transifex callback hook: " + hookKey);
throw new RuntimeException("I don't know you!");
}
article.setTitle(title);
article.setBody(body);
article.setSlug(articleSlug);
article.setLang(language);
article.setClassPk(contentService.ensureClassPK(article.getClass()));
if (!resource.startsWith("article-")) {
_logger.warn("Ignoring Transifex'd hook for " + resource);
return "Ignored";
}
List<Article> articles = new ArrayList<>();
articles.add(article);
String slug = resource.split("-")[1];
_logger.warn("Transifex'd article slug=" + slug);
if (translatedPercentage != null && translatedPercentage >= transifexMinTranslated) {
// fetch updated resource from Transifex
updateArticle(slug, language);
return "Thanks!";
} else {
return "Not sufficiently translated";
}
}
private Article updateArticle(String slug, String lang) {
_logger.info("Fetching updated translation for article " + slug + " lang=" + lang);
Locale locale = new Locale(lang);
String resourceBody = transifexService.getTranslatedResource("article-".concat(slug), locale);
String title = resourceBody.split("<title>")[1].split("</title>")[0];
String body = resourceBody.split("<body>")[1].split("</body>")[0];
// Extract article from database we need (correct locale + do not use
// default (EN) language)
Article article = contentService.getGlobalArticle(slug, locale, false);
if (article == null) {
// No article for selected locale
article = contentService.createGlobalArticle(slug, locale, title, body);
} else {
// Update article for locale
article = contentService.updateArticle(article.getId(), article.getSlug(), title, body);
}
contentService.save(articles);
_logger.info("Updated translation for article " + slug + " lang=" + lang);
return article;
}
}
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