Commit f0cb70c3 authored by Matija Obreza's avatar Matija Obreza

transifex-client 0.2-SNAPSHOT

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