Commit c8a881d5 authored by Matija Obreza's avatar Matija Obreza
Browse files

TransifexService provided by a factory bean

parent 6674581d
......@@ -50,7 +50,7 @@ public class ArticleController extends BaseController {
@Autowired
private ContentService contentService;
@Autowired
@Autowired(required = false)
private TransifexService transifexService;
@Autowired
......@@ -61,6 +61,9 @@ public class ArticleController extends BaseController {
@RequestMapping(value = "/transifex", params = { "post" }, method = RequestMethod.POST)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public String postTotransifex(@RequestParam("slug") String slug, Model model) {
if (transifexService == null) {
throw new ResourceNotFoundException("translationService not enabled");
}
Article article = contentService.getGlobalArticle(slug, getLocale());
String resourceName = "article-" + slug;
try {
......@@ -82,6 +85,10 @@ public class ArticleController extends BaseController {
@RequestMapping(value = "/transifex", params = { "remove" }, method = RequestMethod.POST)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public String deleteFromtransifex(@RequestParam("slug") String slug, Model model) {
if (transifexService == null) {
throw new ResourceNotFoundException("translationService not enabled");
}
Article article = contentService.getGlobalArticle(slug, getLocale());
model.addAttribute("article", article);
......@@ -102,6 +109,9 @@ public class ArticleController extends BaseController {
@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) throws Exception {
if (transifexService == null) {
throw new ResourceNotFoundException("translationService not enabled");
}
Locale locale = new Locale(language);
......@@ -188,7 +198,7 @@ public class ArticleController extends BaseController {
* @return
*/
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@RequestMapping("{url}/edit")
@RequestMapping("{url:.+}/edit")
public String edit(ModelMap model, @PathVariable(value = "url") String slug) {
_logger.debug("Editing article " + slug);
return "redirect:/content/" + slug + "/edit/" + LocaleContextHolder.getLocale().getLanguage();
......@@ -203,7 +213,7 @@ public class ArticleController extends BaseController {
* @return
*/
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@RequestMapping("{url}/edit/{language}")
@RequestMapping("{url:.+}/edit/{language}")
public String edit(ModelMap model, @PathVariable(value = "url") String slug, @PathVariable("language") String language) {
_logger.debug("Editing article " + slug);
......
......@@ -47,7 +47,7 @@ public class TransifexAPIController extends BaseController {
@Autowired
private ContentService contentService;
@Autowired
@Autowired(required = false)
private TransifexService transifexService;
@Value("${transifex.content.template}")
......@@ -64,8 +64,7 @@ public class TransifexAPIController extends BaseController {
*/
@AsAdmin
@RequestMapping(value = "/hook/{hookKey:.+}", method = RequestMethod.POST)
public @ResponseBody
String webHookHandle(@PathVariable("hookKey") String hookKey, @RequestParam(value = "project") String projectSlug,
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) {
......@@ -97,6 +96,11 @@ public class TransifexAPIController extends BaseController {
}
private Article updateArticle(String slug, String lang) throws TransifexException {
if (transifexService == null) {
_logger.error("translationService not available.");
throw new TransifexException("transifex.com service not available", null);
}
_logger.info("Fetching updated translation for article " + slug + " lang=" + lang);
Locale locale = new Locale(lang);
......
......@@ -16,22 +16,50 @@
package org.genesys2.spring.config;
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;
import org.springframework.context.annotation.ImportResource;
@Configuration
@Import({ SpringProperties.class, SpringCommonConfig.class, SpringAclConfig.class, SpringSchedulerConfig.class, SpringDataBaseConfig.class, SpringMailConfig.class,
SpringSecurityOauthConfig.class, SpringCacheConfig.class, ElasticsearchConfig.class })
@Import({ SpringProperties.class, SpringCommonConfig.class, SpringAclConfig.class, SpringSchedulerConfig.class, SpringDataBaseConfig.class,
SpringMailConfig.class, SpringSecurityOauthConfig.class, SpringCacheConfig.class, ElasticsearchConfig.class })
@ImportResource({ "classpath:/spring/spring-security.xml" })
public class ApplicationConfig {
public static final Log LOG = LogFactory.getLog(ApplicationConfig.class);
@Bean
public TransifexService transifexService() {
return new TransifexServiceImpl();
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;
}
};
}
}
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