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

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

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