From f522105cb520a82bfd423a59d8cfafad9f6df28a Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Tue, 18 Jul 2023 09:38:14 +0900 Subject: [PATCH] =?UTF-8?q?AbstractEntity=20createdBy,=20createdAt,=20modi?= =?UTF-8?q?fiedBy,=20lastModified=20=EC=9E=90=EB=8F=99=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../foundation/data/paging/PagingSupport.java | 78 ++++++++++++++++++- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/src/main/java/cokr/xit/foundation/data/paging/PagingSupport.java b/src/main/java/cokr/xit/foundation/data/paging/PagingSupport.java index 77005c2..97c499f 100644 --- a/src/main/java/cokr/xit/foundation/data/paging/PagingSupport.java +++ b/src/main/java/cokr/xit/foundation/data/paging/PagingSupport.java @@ -1,12 +1,15 @@ package cokr.xit.foundation.data.paging; import java.sql.Statement; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; import java.util.Map; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.resultset.ResultSetHandler; import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; @@ -15,6 +18,7 @@ import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import cokr.xit.foundation.AbstractComponent; +import cokr.xit.foundation.AbstractEntity; import cokr.xit.foundation.component.QueryRequest; /**조회 결과의 페이징 처리를 지원하는 MyBatis Interceptor @@ -22,6 +26,7 @@ import cokr.xit.foundation.component.QueryRequest; */ @Intercepts({ @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), + @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}), @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}) }) public class PagingSupport extends AbstractComponent implements Interceptor { @@ -31,10 +36,75 @@ public class PagingSupport extends AbstractComponent implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { - switch (getStep(invocation)) { - case "Executor.query": return setPaging(invocation); - case "ResultSetHandler.handleResultSets": return setPagingInfo(invocation); - default: return invocation.proceed(); + Object[] args = invocation.getArgs(); + Object arg0 = args[0]; + if (arg0 instanceof MappedStatement) { + MappedStatement stmt = (MappedStatement)arg0; + SqlCommandType sqlType = stmt.getSqlCommandType(); + switch (sqlType) { + case SELECT: return setPaging(invocation); + case INSERT: + case UPDATE: + case DELETE: return setEntityProperties(invocation, sqlType); + default: return invocation.proceed(); + } + } else + switch (getStep(invocation)) { + case "ResultSetHandler.handleResultSets": return setPagingInfo(invocation); + default: return invocation.proceed(); + } + } + + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); + + private Object setEntityProperties(Invocation invocation, SqlCommandType sqlType) throws Exception { + Object[] args = invocation.getArgs(); + Object arg = args[1]; + setEntityProperties(sqlType, arg, null); + + return invocation.proceed(); + } + + private void setEntityProperties(SqlCommandType sqlType, Object obj, String now) { + if (isEmpty(obj)) return; + + now = ifEmpty(now, () -> dateFormat.format(new Date())); + if (obj instanceof AbstractEntity) { + setProperties(sqlType, (AbstractEntity)obj, now); + } else if (obj instanceof Map) { + Map map = (Map)obj; + for (Object o: map.values()) + setEntityProperties(sqlType, o, now); + } else if (obj instanceof Iterable) { + Iterable iterable = (Iterable)obj; + for (Object o: iterable) + setEntityProperties(sqlType, o, now); + } else if (obj.getClass().isArray()) { + Object[] objs = (Object[])obj; + for (Object o: objs) + setEntityProperties(sqlType, o, now); + } + } + + private void setProperties(SqlCommandType sqlType, AbstractEntity entity, String now) { + String userID = currentUser().getId(); + switch (sqlType) { + case INSERT: + entity.setCreatedBy(userID); + entity.setCreatedAt(now); + entity.setModifiedBy(userID); + entity.setLastModified(now); + break; + case UPDATE: + entity.setModifiedBy(userID); + entity.setLastModified(now); + break; + case DELETE: + entity.setRemovedBy(userID); + entity.setRemovedAt(now); + entity.setUseYN("N"); + break; + default: break; } }