mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 17:56:20 +00:00
Closes #30: Delete locale & theme concept
This commit is contained in:
@@ -10,21 +10,11 @@ import com.google.common.collect.Lists;
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class Constants {
|
||||
/**
|
||||
* Default locale.
|
||||
*/
|
||||
public static final String DEFAULT_LOCALE_ID = "en";
|
||||
|
||||
/**
|
||||
* Default timezone ID.
|
||||
*/
|
||||
public static final String DEFAULT_TIMEZONE_ID = "Europe/London";
|
||||
|
||||
/**
|
||||
* Default theme ID.
|
||||
*/
|
||||
public static final String DEFAULT_THEME_ID = "default.less";
|
||||
|
||||
/**
|
||||
* Administrator's default password ("admin").
|
||||
*/
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.sismics.docs.core.dao.jpa;
|
||||
|
||||
import com.sismics.docs.core.model.jpa.Locale;
|
||||
import com.sismics.util.context.ThreadLocalContext;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Locale DAO.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class LocaleDao {
|
||||
/**
|
||||
* Gets a locale by its ID.
|
||||
*
|
||||
* @param id Locale ID
|
||||
* @return Locale
|
||||
*/
|
||||
public Locale getById(String id) {
|
||||
EntityManager em = ThreadLocalContext.get().getEntityManager();
|
||||
try {
|
||||
return em.find(Locale.class, id);
|
||||
} catch (NoResultException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of all locales.
|
||||
*
|
||||
* @return List of locales
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Locale> findAll() {
|
||||
EntityManager em = ThreadLocalContext.get().getEntityManager();
|
||||
Query q = em.createQuery("select l from Locale l order by l.id");
|
||||
return q.getResultList();
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@ import org.mindrot.jbcrypt.BCrypt;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.sismics.docs.core.constant.AuditLogType;
|
||||
import com.sismics.docs.core.constant.Constants;
|
||||
import com.sismics.docs.core.dao.jpa.criteria.UserCriteria;
|
||||
import com.sismics.docs.core.dao.jpa.dto.UserDto;
|
||||
import com.sismics.docs.core.model.jpa.User;
|
||||
@@ -78,7 +77,6 @@ public class UserDao {
|
||||
// Create the user
|
||||
user.setCreateDate(new Date());
|
||||
user.setPassword(hashPassword(user.getPassword()));
|
||||
user.setTheme(Constants.DEFAULT_THEME_ID);
|
||||
em.persist(user);
|
||||
|
||||
// Create audit log
|
||||
@@ -102,10 +100,7 @@ public class UserDao {
|
||||
User userFromDb = (User) q.getSingleResult();
|
||||
|
||||
// Update the user
|
||||
userFromDb.setLocaleId(user.getLocaleId());
|
||||
userFromDb.setEmail(user.getEmail());
|
||||
userFromDb.setTheme(user.getTheme());
|
||||
userFromDb.setFirstConnection(user.isFirstConnection());
|
||||
|
||||
// Create audit log
|
||||
AuditLogUtil.create(userFromDb, AuditLogType.UPDATE);
|
||||
@@ -231,7 +226,7 @@ public class UserDao {
|
||||
Map<String, Object> parameterMap = new HashMap<String, Object>();
|
||||
List<String> criteriaList = new ArrayList<String>();
|
||||
|
||||
StringBuilder sb = new StringBuilder("select u.USE_ID_C as c0, u.USE_USERNAME_C as c1, u.USE_EMAIL_C as c2, u.USE_CREATEDATE_D as c3, u.USE_IDLOCALE_C as c4");
|
||||
StringBuilder sb = new StringBuilder("select u.USE_ID_C as c0, u.USE_USERNAME_C as c1, u.USE_EMAIL_C as c2, u.USE_CREATEDATE_D as c3");
|
||||
sb.append(" from T_USER u ");
|
||||
|
||||
// Add search criterias
|
||||
@@ -260,7 +255,6 @@ public class UserDao {
|
||||
userDto.setUsername((String) o[i++]);
|
||||
userDto.setEmail((String) o[i++]);
|
||||
userDto.setCreateTimestamp(((Timestamp) o[i++]).getTime());
|
||||
userDto.setLocaleId((String) o[i++]);
|
||||
userDtoList.add(userDto);
|
||||
}
|
||||
paginatedList.setResultList(userDtoList);
|
||||
|
||||
@@ -11,11 +11,6 @@ public class UserDto {
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* Locale ID.
|
||||
*/
|
||||
private String localeId;
|
||||
|
||||
/**
|
||||
* Username.
|
||||
*/
|
||||
@@ -49,24 +44,6 @@ public class UserDto {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of localeId.
|
||||
*
|
||||
* @return localeId
|
||||
*/
|
||||
public String getLocaleId() {
|
||||
return localeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter of localeId.
|
||||
*
|
||||
* @param localeId localeId
|
||||
*/
|
||||
public void setLocaleId(String localeId) {
|
||||
this.localeId = localeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of username.
|
||||
*
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.sismics.docs.core.model.jpa;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
|
||||
/**
|
||||
* Locale entity.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "T_LOCALE")
|
||||
public class Locale {
|
||||
/**
|
||||
* Locale ID (ex: fr_FR).
|
||||
*/
|
||||
@Id
|
||||
@Column(name = "LOC_ID_C", length = 10)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* Getter of id.
|
||||
*
|
||||
* @return id
|
||||
*/
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter of id.
|
||||
*
|
||||
* @param id id
|
||||
*/
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper(this)
|
||||
.add("id", id)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -27,12 +27,6 @@ public class User implements Loggable {
|
||||
@Column(name = "USE_ID_C", length = 36)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* Locale ID.
|
||||
*/
|
||||
@Column(name = "USE_IDLOCALE_C", nullable = false, length = 10)
|
||||
private String localeId;
|
||||
|
||||
/**
|
||||
* Role ID.
|
||||
*/
|
||||
@@ -63,18 +57,6 @@ public class User implements Loggable {
|
||||
@Column(name = "USE_EMAIL_C", nullable = false, length = 100)
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* Theme.
|
||||
*/
|
||||
@Column(name = "USE_THEME_C", nullable = false, length = 100)
|
||||
private String theme;
|
||||
|
||||
/**
|
||||
* True if the user hasn't dismissed the first connection screen.
|
||||
*/
|
||||
@Column(name = "USE_FIRSTCONNECTION_B", nullable = false)
|
||||
private boolean firstConnection;
|
||||
|
||||
/**
|
||||
* Creation date.
|
||||
*/
|
||||
@@ -105,24 +87,6 @@ public class User implements Loggable {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of localeId.
|
||||
*
|
||||
* @return localeId
|
||||
*/
|
||||
public String getLocaleId() {
|
||||
return localeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter of localeId.
|
||||
*
|
||||
* @param localeId localeId
|
||||
*/
|
||||
public void setLocaleId(String localeId) {
|
||||
this.localeId = localeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of roleId.
|
||||
*
|
||||
@@ -195,42 +159,6 @@ public class User implements Loggable {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of theme.
|
||||
*
|
||||
* @return theme
|
||||
*/
|
||||
public String getTheme() {
|
||||
return theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter of theme.
|
||||
*
|
||||
* @param theme theme
|
||||
*/
|
||||
public void setTheme(String theme) {
|
||||
this.theme = theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of firstConnection.
|
||||
*
|
||||
* @return firstConnection
|
||||
*/
|
||||
public boolean isFirstConnection() {
|
||||
return firstConnection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter of firstConnection.
|
||||
*
|
||||
* @param firstConnection firstConnection
|
||||
*/
|
||||
public void setFirstConnection(boolean firstConnection) {
|
||||
this.firstConnection = firstConnection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of createDate.
|
||||
*
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.sismics.util;
|
||||
|
||||
import com.sismics.docs.core.constant.Constants;
|
||||
import com.sismics.docs.core.dao.jpa.LocaleDao;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Locale utilities.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class LocaleUtil {
|
||||
/**
|
||||
* Returns the locale from its language / country code (ex: fr_FR).
|
||||
*
|
||||
* @param localeCode Locale code
|
||||
* @return Locate instance
|
||||
*/
|
||||
public static final Locale getLocale(String localeCode) {
|
||||
String[] localeCodeArray = localeCode.split("_");
|
||||
String language = localeCodeArray[0];
|
||||
String country = "";
|
||||
String variant = "";
|
||||
if (localeCodeArray.length >= 2) {
|
||||
country = localeCodeArray[1];
|
||||
}
|
||||
if (localeCodeArray.length >= 3) {
|
||||
variant = localeCodeArray[2];
|
||||
}
|
||||
return new Locale(language, country, variant);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the ID of the locale from the HTTP Accept-Language header.
|
||||
*
|
||||
* @param acceptLanguageHeader header
|
||||
* @return Locale ID
|
||||
*/
|
||||
public static String getLocaleIdFromAcceptLanguage(String acceptLanguageHeader) {
|
||||
String localeId = null;
|
||||
if (StringUtils.isNotBlank(acceptLanguageHeader)) {
|
||||
acceptLanguageHeader = acceptLanguageHeader.replaceAll("-", "_");
|
||||
localeId = acceptLanguageHeader.split(",")[0];
|
||||
}
|
||||
if (StringUtils.isNotBlank(localeId)) {
|
||||
LocaleDao localeDao = new LocaleDao();
|
||||
com.sismics.docs.core.model.jpa.Locale locale = localeDao.getById(localeId);
|
||||
if (locale != null) {
|
||||
localeId = locale.getId();
|
||||
} else {
|
||||
// The client provided an unknown locale
|
||||
localeId = Constants.DEFAULT_LOCALE_ID;
|
||||
}
|
||||
}
|
||||
if (StringUtils.isBlank(localeId)) {
|
||||
localeId = Constants.DEFAULT_LOCALE_ID;
|
||||
}
|
||||
return localeId;
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.sismics.util;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* Message utilities.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class MessageUtil {
|
||||
/**
|
||||
* Returns a message formated in the specified locale.
|
||||
* Returns **key** if no message is set for this key.
|
||||
*
|
||||
* @param locale Locale
|
||||
* @param key Message key
|
||||
* @param args Arguments of the message
|
||||
* @return Formated message
|
||||
*/
|
||||
public static String getMessage(Locale locale, String key, Object... args) {
|
||||
ResourceBundle resources = ResourceBundle.getBundle("messages", locale);
|
||||
String message = null;
|
||||
try {
|
||||
message = resources.getString(key);
|
||||
} catch (MissingResourceException e) {
|
||||
message = "**" + key + "**";
|
||||
}
|
||||
return MessageFormat.format(message, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the resource bundle containing messages for the locale.
|
||||
*
|
||||
* @param locale Locale to use
|
||||
* @return Resource bundle
|
||||
*/
|
||||
public static ResourceBundle getMessage(Locale locale) {
|
||||
return ResourceBundle.getBundle("messages", locale);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@
|
||||
<class>com.sismics.docs.core.model.jpa.AuthenticationToken</class>
|
||||
<class>com.sismics.docs.core.model.jpa.BaseFunction</class>
|
||||
<class>com.sismics.docs.core.model.jpa.Config</class>
|
||||
<class>com.sismics.docs.core.model.jpa.Locale</class>
|
||||
<class>com.sismics.docs.core.model.jpa.User</class>
|
||||
<class>com.sismics.docs.core.model.jpa.RoleBaseFunction</class>
|
||||
<class>com.sismics.docs.core.model.jpa.Document</class>
|
||||
|
||||
@@ -1 +1 @@
|
||||
db.version=0
|
||||
db.version=1
|
||||
@@ -0,0 +1,5 @@
|
||||
alter table T_USER drop column USE_IDLOCALE_C;
|
||||
alter table T_USER drop column USE_THEME_C;
|
||||
alter table T_USER drop column USE_FIRSTCONNECTION_B;
|
||||
drop table T_LOCALE;
|
||||
update T_CONFIG set CFG_VALUE_C = '1' where CFG_ID_C = 'DB_VERSION';
|
||||
@@ -19,7 +19,6 @@ public class TestJpa extends BaseTransactionalTest {
|
||||
User user = new User();
|
||||
user.setUsername("username");
|
||||
user.setEmail("toto@docs.com");
|
||||
user.setLocaleId("fr");
|
||||
user.setRoleId("admin");
|
||||
user.setPrivateKey("AwesomePrivateKey");
|
||||
String id = userDao.create(user);
|
||||
|
||||
Reference in New Issue
Block a user