initial commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>de.bstly.we</groupId>
|
||||
<artifactId>webstly-main</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>i18n</name>
|
||||
<artifactId>webstly-i18n</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>de.bstly.we</groupId>
|
||||
<artifactId>webstly-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.we.i18n.businesslogic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import de.bstly.we.i18n.model.I18n;
|
||||
import de.bstly.we.i18n.repository.I18nRepository;
|
||||
|
||||
/**
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class I18nManager {
|
||||
|
||||
@Autowired
|
||||
private I18nRepository i18nRepository;
|
||||
private Gson gson = new Gson();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param locale
|
||||
* @return
|
||||
*/
|
||||
public I18n get(String locale) {
|
||||
return i18nRepository.findById(locale).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param locale
|
||||
* @return
|
||||
*/
|
||||
public JsonObject getLabel(String locale) {
|
||||
I18n i18n = get(locale);
|
||||
if (i18n != null && StringUtils.hasText(i18n.getLabel())) {
|
||||
JsonElement element = JsonParser.parseString(i18n.getLabel());
|
||||
if (element != null && element.isJsonObject()) {
|
||||
return element.getAsJsonObject();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<String> getLocales() {
|
||||
return i18nRepository.findAll().stream().map(I18n::getLocale).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param dest
|
||||
* @param src
|
||||
*/
|
||||
protected void extendJsonObject(JsonObject dest, JsonObject src) {
|
||||
for (Entry<String, JsonElement> srcEntry : src.entrySet()) {
|
||||
String srcKey = srcEntry.getKey();
|
||||
JsonElement srcValue = srcEntry.getValue();
|
||||
if (dest.has(srcKey)) {
|
||||
JsonElement destValue = dest.get(srcKey);
|
||||
if (destValue.isJsonObject() && srcValue.isJsonObject()) {
|
||||
extendJsonObject(destValue.getAsJsonObject(), srcValue.getAsJsonObject());
|
||||
} else {
|
||||
dest.add(srcKey, srcValue);
|
||||
}
|
||||
} else {
|
||||
dest.add(srcKey, srcValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param locale
|
||||
* @param newLabel
|
||||
* @return
|
||||
*/
|
||||
public I18n addLabel(String locale, JsonObject newLabel) {
|
||||
JsonObject label = getLabel(locale);
|
||||
|
||||
if (label == null || label.size() == 0 || label.entrySet().isEmpty()) {
|
||||
label = newLabel;
|
||||
} else {
|
||||
extendJsonObject(label, newLabel);
|
||||
}
|
||||
|
||||
I18n i18n = new I18n();
|
||||
i18n.setLocale(locale);
|
||||
i18n.setLabel(gson.toJson(label));
|
||||
|
||||
return i18nRepository.save(i18n);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param locale
|
||||
* @param label
|
||||
* @return
|
||||
*/
|
||||
public I18n setLabel(String locale, JsonObject label) {
|
||||
I18n i18n = new I18n();
|
||||
i18n.setLocale(locale);
|
||||
i18n.setLabel(gson.toJson(label));
|
||||
|
||||
return i18nRepository.save(i18n);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param locale
|
||||
*/
|
||||
public void deleteLabel(String locale) {
|
||||
if (i18nRepository.existsById(locale)) {
|
||||
i18nRepository.deleteById(locale);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.we.i18n.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonIOException;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import de.bstly.we.controller.BaseController;
|
||||
import de.bstly.we.i18n.businesslogic.I18nManager;
|
||||
|
||||
/**
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/i18n")
|
||||
public class I18nController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private I18nManager i18nManager;
|
||||
private Gson gson = new Gson();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping
|
||||
public List<String> getLocales() {
|
||||
return i18nManager.getLocales();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param locale
|
||||
* @param response
|
||||
* @throws JsonIOException
|
||||
* @throws IOException
|
||||
*/
|
||||
@GetMapping("/{locale}")
|
||||
public void getLabel(@PathVariable("locale") String locale, HttpServletResponse response)
|
||||
throws JsonIOException, IOException {
|
||||
JsonObject label = i18nManager.getLabel(locale);
|
||||
if (label != null) {
|
||||
response.setCharacterEncoding("utf-8");
|
||||
gson.toJson(label, response.getWriter());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param locale
|
||||
* @param label
|
||||
*/
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
@PostMapping("/{locale}")
|
||||
public void setLabel(@PathVariable("locale") String locale, @RequestBody Object label) {
|
||||
JsonElement element = gson.toJsonTree(label);
|
||||
|
||||
if (element != null && element.isJsonObject()) {
|
||||
i18nManager.setLabel(locale, element.getAsJsonObject());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param locale
|
||||
* @param label
|
||||
*/
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
@PutMapping("/{locale}")
|
||||
public void addLabel(@PathVariable("locale") String locale, @RequestBody Object label) {
|
||||
JsonElement element = gson.toJsonTree(label);
|
||||
|
||||
if (element != null && element.isJsonObject()) {
|
||||
i18nManager.addLabel(locale, element.getAsJsonObject());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param locale
|
||||
*/
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
@DeleteMapping("/{locale}")
|
||||
public void deleteLocale(@PathVariable("locale") String locale) {
|
||||
i18nManager.deleteLabel(locale);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.we.i18n.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "i18n", uniqueConstraints = @UniqueConstraint(columnNames = { "locale" }))
|
||||
public class I18n {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Id
|
||||
@Column(name = "locale", unique = true, nullable = false)
|
||||
private String locale;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Lob
|
||||
@Column(name = "label")
|
||||
private String label;
|
||||
|
||||
/**
|
||||
* @return the locale
|
||||
*/
|
||||
public String getLocale() {
|
||||
return locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param locale the locale to set
|
||||
*/
|
||||
public void setLocale(String locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the label
|
||||
*/
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param label the label to set
|
||||
*/
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.we.i18n.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import de.bstly.we.i18n.model.I18n;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
@Repository
|
||||
public interface I18nRepository
|
||||
extends JpaRepository<I18n, String>, QuerydslPredicateExecutor<I18n> {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user