Commit 2260f545 authored by 楊慶堂's avatar 楊慶堂

處理查詢結果,如果是 Collection 的話,第一筆沒處理就不處理了

parent 32d321b4
...@@ -23,10 +23,7 @@ import org.springframework.context.i18n.LocaleContextHolder; ...@@ -23,10 +23,7 @@ import org.springframework.context.i18n.LocaleContextHolder;
import com.google.common.cache.Cache; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
/** /** Spring data 取出後進行多國語系的替換 快取多語資料 30 分鐘 */
* Spring data 取出後進行多國語系的替換
* 快取多語資料 30 分鐘
*/
public class I18NPostProcessor implements MethodInterceptor { public class I18NPostProcessor implements MethodInterceptor {
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
private EntityManager em; private EntityManager em;
...@@ -45,19 +42,32 @@ public class I18NPostProcessor implements MethodInterceptor { ...@@ -45,19 +42,32 @@ public class I18NPostProcessor implements MethodInterceptor {
process(result); process(result);
return result; return result;
} }
/**
* 處理查詢結果,如果是 Collection 的話,第一筆沒處理就不處理了
* @param result
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private void process(Object result) throws IllegalAccessException, InvocationTargetException { private void process(Object result) throws IllegalAccessException, InvocationTargetException {
if(result instanceof Collection) { if (result instanceof Collection) {
for (Object entity : (Collection)result) { for (Object entity : (Collection) result) {
processEntity(entity); if (!processEntity(entity)) break;
} }
} else { } else {
processEntity(result); processEntity(result);
} }
} }
private void processEntity(Object entity) /**
* @param entity
* @return 是否有處理
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private boolean processEntity(Object entity)
throws IllegalAccessException, InvocationTargetException { throws IllegalAccessException, InvocationTargetException {
boolean result = false;
I18NTranslates i18n = entity.getClass().getAnnotation(I18NTranslates.class); I18NTranslates i18n = entity.getClass().getAnnotation(I18NTranslates.class);
if (i18n != null) { if (i18n != null) {
logger.trace("I18n process: {}", entity); logger.trace("I18n process: {}", entity);
...@@ -75,11 +85,12 @@ public class I18NPostProcessor implements MethodInterceptor { ...@@ -75,11 +85,12 @@ public class I18NPostProcessor implements MethodInterceptor {
} }
} }
} }
result = true;
} }
return result;
} }
/** /**
*
* @param i18n * @param i18n
* @param entity * @param entity
* @param clazz * @param clazz
...@@ -93,19 +104,20 @@ public class I18NPostProcessor implements MethodInterceptor { ...@@ -93,19 +104,20 @@ public class I18NPostProcessor implements MethodInterceptor {
String language = LocaleContextHolder.getLocale().toString(); String language = LocaleContextHolder.getLocale().toString();
String table = i18n.table(); String table = i18n.table();
String idColumn = i18n.id(); String idColumn = i18n.id();
Map<String, Map<String, String>> translateTable = translateTable(table, language, i18n.mapping(), idColumn); Map<String, Map<String, String>> translateTable =
translateTable(table, language, i18n.mapping(), idColumn);
return translateTable.get(id); return translateTable.get(id);
} }
private Map<String, Map<String, String>> translateTable(String table, String language, I18NTranslate[] mapping, String idColumn) { private Map<String, Map<String, String>> translateTable(
String table, String language, I18NTranslate[] mapping, String idColumn) {
String key = "Table:" + table + "_" + language; String key = "Table:" + table + "_" + language;
Map<String, Map<String, String>> result = cache.getIfPresent(key); Map<String, Map<String, String>> result = cache.getIfPresent(key);
if (result == null) { if (result == null) {
logger.trace("translate query, table: {}", table); logger.trace("translate query, table: {}", table);
String sql = String sql = String.format("select * from %s where languageCode = ?", table);
String.format("select * from %s where languageCode = ?", table);
Query query = em.createNativeQuery(sql); Query query = em.createNativeQuery(sql);
SQLQuery query1 = query.unwrap(org.hibernate.SQLQuery.class); SQLQuery query1 = query.unwrap(org.hibernate.SQLQuery.class);
query1.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); query1.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
......
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