Commit f0cb70c3 authored by Matija Obreza's avatar Matija Obreza

transifex-client 0.2-SNAPSHOT

parent 144b7f02
......@@ -113,12 +113,6 @@
<dependencies>
<dependency>
<groupId>org.genesys2</groupId>
<artifactId>transifex-client</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
<!--Test dependencies -->
<dependency>
<groupId>junit</groupId>
......@@ -522,24 +516,12 @@
<artifactId>urlrewritefilter</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<!--dom4j-->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
<dependency>
<groupId>org.genesys2</groupId>
<artifactId>transifex-client</artifactId>
<version>0.2-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
......
......@@ -25,10 +25,8 @@ import org.genesys2.server.model.impl.Article;
import org.genesys2.server.service.ContentService;
import org.genesys2.spring.RequestAttributeLocaleResolver;
import org.genesys2.spring.ResourceNotFoundException;
import org.genesys2.transifex.client.TransifexException;
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.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.PageRequest;
......@@ -42,6 +40,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Controller
@RequestMapping("/content")
public class ArticleController extends BaseController {
......@@ -51,9 +52,12 @@ public class ArticleController extends BaseController {
@Autowired
private TransifexService transifexService;
@Autowired
private RequestAttributeLocaleResolver localeResolver;
private ObjectMapper mapper = new ObjectMapper();
@RequestMapping(value = "/transifex", params = { "post" }, method = RequestMethod.POST)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public String postTotransifex(@RequestParam("slug") String slug, Model model) {
......@@ -79,38 +83,49 @@ public class ArticleController extends BaseController {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public String deleteFromtransifex(@RequestParam("slug") String slug, Model model) {
Article article = contentService.getGlobalArticle(slug, getLocale());
if (transifexService.deleteResource("article-" + slug)) {
model.addAttribute("responseFromTransifex", "article.transifex-resource-removed");
model.addAttribute("article", article);
} else {
model.addAttribute("article", article);
try {
if (transifexService.deleteResource("article-" + slug)) {
model.addAttribute("responseFromTransifex", "article.transifex-resource-removed");
} else {
model.addAttribute("responseFromTransifex", "article.transifex-failed");
}
} catch (TransifexException e) {
_logger.error(e.getMessage(), e);
model.addAttribute("responseFromTransifex", "article.transifex-failed");
model.addAttribute("article", article);
}
return "/content/article-edit";
}
@RequestMapping(value = "/translate/{slug}/{language}", method = RequestMethod.GET)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public String fetchFromtransifex(@PathVariable("slug") String slug, @PathVariable("language") String language, Model model) {
public String fetchFromtransifex(@PathVariable("slug") String slug, @PathVariable("language") String language, Model model) throws Exception {
Locale locale = new Locale(language);
Article article = contentService.getGlobalArticle(slug, locale, false);
JSONObject jsonObject;
JSONParser jsonParser = new JSONParser();
String translatedResource = transifexService.getTranslatedResource("article-" + slug, locale);
String content;
String translatedResource;
try {
translatedResource = transifexService.getTranslatedResource("article-" + slug, locale);
} catch (TransifexException e) {
_logger.error(e.getMessage(), e);
throw new Exception(e.getMessage(), e);
}
String title = null;
String body = null;
try {
jsonObject = (JSONObject) jsonParser.parse(translatedResource);
content = (String) jsonObject.get("content");
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];
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
_logger.error(e.getMessage(), e);
throw e;
}
if (article == null) {
......
......@@ -20,6 +20,7 @@ import org.genesys2.server.aspect.AsAdmin;
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.TransifexException;
import org.genesys2.transifex.client.TransifexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -79,14 +80,19 @@ public class TransifexAPIController extends BaseController {
if (translatedPercentage != null && translatedPercentage >= transifexMinTranslated) {
// fetch updated resource from Transifex
updateArticle(slug, language);
try {
updateArticle(slug, language);
} catch (TransifexException e) {
_logger.error(e.getMessage(), e);
throw new RuntimeException("Server error");
}
return "Thanks!";
} else {
return "Not sufficiently translated";
}
}
private Article updateArticle(String slug, String lang) {
private Article updateArticle(String slug, String lang) throws TransifexException {
_logger.info("Fetching updated translation for article " + slug + " lang=" + lang);
Locale locale = new Locale(lang);
......
......@@ -16,8 +16,14 @@
package org.genesys2.transifex.client;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.apache.commons.lang3.RandomStringUtils;
import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -27,12 +33,7 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
//@Ignore
@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TransifexClientTest.Config.class, initializers = PropertyPlacholderInitializer.class)
public class TransifexClientTest {
......@@ -53,7 +54,7 @@ public class TransifexClientTest {
private TransifexService transifexService;
@Test
public void testPostResource() throws IOException {
public void testPostResource() throws IOException, TransifexException {
// Post new resource
String resourceSlug = "test-".concat(RandomStringUtils.randomAlphanumeric(30));
String resourceTitle = "Test ".concat(RandomStringUtils.randomAlphanumeric(60));
......@@ -74,7 +75,7 @@ public class TransifexClientTest {
}
@Test
public void testUpdateResource() throws IOException {
public void testUpdateResource() throws IOException, TransifexException {
// Post new resource
String resourceSlug = "test-".concat(RandomStringUtils.randomAlphanumeric(30));
String resourceTitle = "Test ".concat(RandomStringUtils.randomAlphanumeric(60));
......
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