AbstractEntity createdBy, createdAt, modifiedBy, lastModified 자동 설정

master
mjkhan21 1 year ago
parent 6069927fef
commit f522105cb5

@ -1,12 +1,15 @@
package cokr.xit.foundation.data.paging; package cokr.xit.foundation.data.paging;
import java.sql.Statement; import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.resultset.ResultSetHandler; import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Invocation;
@ -15,6 +18,7 @@ import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.session.RowBounds;
import cokr.xit.foundation.AbstractComponent; import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.AbstractEntity;
import cokr.xit.foundation.component.QueryRequest; import cokr.xit.foundation.component.QueryRequest;
/** MyBatis Interceptor /** MyBatis Interceptor
@ -22,6 +26,7 @@ import cokr.xit.foundation.component.QueryRequest;
*/ */
@Intercepts({ @Intercepts({
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @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}) @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})
}) })
public class PagingSupport extends AbstractComponent implements Interceptor { public class PagingSupport extends AbstractComponent implements Interceptor {
@ -31,10 +36,75 @@ public class PagingSupport extends AbstractComponent implements Interceptor {
@Override @Override
public Object intercept(Invocation invocation) throws Throwable { public Object intercept(Invocation invocation) throws Throwable {
switch (getStep(invocation)) { Object[] args = invocation.getArgs();
case "Executor.query": return setPaging(invocation); Object arg0 = args[0];
case "ResultSetHandler.handleResultSets": return setPagingInfo(invocation); if (arg0 instanceof MappedStatement) {
default: return invocation.proceed(); 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;
} }
} }

Loading…
Cancel
Save