ExecInfo 추가
parent
a0419681d6
commit
7700d098f8
@ -0,0 +1,269 @@
|
||||
package cokr.xit.foundation.component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import cokr.xit.foundation.AbstractObject;
|
||||
import cokr.xit.foundation.Assert;
|
||||
import cokr.xit.foundation.data.DataObject;
|
||||
|
||||
/**실행결과 정보
|
||||
* @author mjkhan
|
||||
*/
|
||||
public class ExecInfo extends AbstractObject {
|
||||
/**데이터처리 유형
|
||||
* @author mjkhan
|
||||
*/
|
||||
public static enum Type {
|
||||
/** 데이터 생성 */
|
||||
CREATE("C"),
|
||||
/** 데이터 생성 */
|
||||
READ("R"),
|
||||
/** 데이터 수정 */
|
||||
UPDATE("U"),
|
||||
/** 데이터 삭제 */
|
||||
DELETE("D");
|
||||
|
||||
private final String code;
|
||||
|
||||
private Type(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**데이터처리 유형의 코드를 반환한다.
|
||||
* @return 데이터처리 유형 코드
|
||||
*/
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**코드에 해당하는 Type을 반환한다.
|
||||
* @param code 데이터처리 유형 코드
|
||||
* @return DataProc
|
||||
*/
|
||||
public static Type codeOf(String code) {
|
||||
if (Assert.isEmpty(code))
|
||||
return null;
|
||||
|
||||
for (Type type: values()) {
|
||||
if (type.code.equals(code))
|
||||
return type;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("code: " + code);
|
||||
}
|
||||
}
|
||||
|
||||
/** 저장된 데이터 수가 1인 실행성공 정보 */
|
||||
public static final ExecInfo SUCCESS = new ExecInfo()
|
||||
.setSuccess(true)
|
||||
.setAffected(1)
|
||||
.seal();
|
||||
|
||||
private Type type;
|
||||
private Object target;
|
||||
private int affected;
|
||||
private String code;
|
||||
private String message;
|
||||
private DataObject info;
|
||||
private Throwable throwable;
|
||||
private Boolean success;
|
||||
private boolean sealed;
|
||||
|
||||
private ExecInfo seal() {
|
||||
sealed = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
private void ensureNotSealed() {
|
||||
if (sealed)
|
||||
throw new RuntimeException("The ExecInfo is sealed and unmodifiable.");
|
||||
}
|
||||
|
||||
/**데이터처리 유형을 반환한다.
|
||||
* @return 데이터처리 유형
|
||||
*/
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**데이터처리 유형을 설정한다.
|
||||
* @param type 데이터처리 유형
|
||||
*/
|
||||
public ExecInfo setType(Type type) {
|
||||
ensureNotSealed();
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**실행 대상 객체를 반환한다.
|
||||
* @return 실행 대상 객체
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getTarget() {
|
||||
return (T)target;
|
||||
}
|
||||
|
||||
/**실행 대상 객체를 설정한다.
|
||||
* @param object 실행 대상 객체
|
||||
* @return 현재 ExecResult
|
||||
*/
|
||||
public ExecInfo setTarget(Object object) {
|
||||
ensureNotSealed();
|
||||
this.target = object;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**저장된 데이터 수를 반환한다.
|
||||
* @return 저장된 데이터 수
|
||||
*/
|
||||
public int getAffected() {
|
||||
return affected;
|
||||
}
|
||||
|
||||
/**저장된 데이터 수를 설정한다.
|
||||
* @param affected 저장된 데이터 수
|
||||
* @return 현재 ExecResult
|
||||
*/
|
||||
public ExecInfo setAffected(int affected) {
|
||||
ensureNotSealed();
|
||||
this.affected = affected;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**결과코드를 반환한다.
|
||||
* @return 결과코드
|
||||
*/
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**결과코드를 설정한다.
|
||||
* @param code 결과코드
|
||||
* @return 현재 ExecResult
|
||||
*/
|
||||
public ExecInfo setCode(String code) {
|
||||
ensureNotSealed();
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**결과메시지를 반환한다.
|
||||
* @return 결과메시지
|
||||
*/
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**결과메시지를 설정한다.
|
||||
* @param message 결과메시지
|
||||
* @return 현재 ExecResult
|
||||
*/
|
||||
public ExecInfo setMessage(String message) {
|
||||
ensureNotSealed();
|
||||
this.message = message;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataObject getInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getInfo(String key) {
|
||||
if (info == null) return null;
|
||||
return (T)info.get(key);
|
||||
}
|
||||
|
||||
public ExecInfo setInfo(String key, Object obj) {
|
||||
ensureNotSealed();
|
||||
if (info == null)
|
||||
info = new DataObject();
|
||||
info.put(key, obj);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**오류를 반환한다.
|
||||
* @return 오류
|
||||
*/
|
||||
public Throwable getThrowable() {
|
||||
return throwable;
|
||||
}
|
||||
|
||||
/**오류를 설정한다.
|
||||
* @param throwable 오류
|
||||
* @return 현재 ExecResult
|
||||
*/
|
||||
public ExecInfo setThrowable(Throwable throwable) {
|
||||
ensureNotSealed();
|
||||
this.throwable = throwable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**오류가 설정되어 있으면 throw한다.
|
||||
* @return 오류가 설정되어 있지 않으면 false
|
||||
*/
|
||||
public boolean throwThrowable() {
|
||||
if (throwable != null)
|
||||
throw runtimeException(throwable);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**성공여부를 반환한다.
|
||||
* @return 성공여부
|
||||
*/
|
||||
public boolean isSuccess() {
|
||||
return throwable == null
|
||||
&& ifEmpty(success, Boolean.TRUE);
|
||||
}
|
||||
|
||||
/**성공여부를 설정한다.
|
||||
* @param success 성공여부
|
||||
* @return 현재 ExecResult
|
||||
*/
|
||||
public ExecInfo setSuccess(boolean success) {
|
||||
ensureNotSealed();
|
||||
this.success = success;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**성공한 결과로 영향받은 데이터수를 반환한다.
|
||||
* @param results 실행결과 목록
|
||||
* @return 성공한 결과로 영향받은 데이터수
|
||||
*/
|
||||
public static final int getAffected(List<ExecInfo> results) {
|
||||
return results.stream()
|
||||
.mapToInt(ExecInfo::getAffected)
|
||||
.sum();
|
||||
}
|
||||
|
||||
/**모든 결과가 성공인지 반환한다.
|
||||
* @param results 실행결과 목록
|
||||
* @return 모든 결과의 성공 여부
|
||||
*/
|
||||
public static final boolean isSuccess(List<ExecInfo> results) {
|
||||
for (ExecInfo result: results) {
|
||||
if (!result.isSuccess())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static final <T> List<T> getTargets(List<ExecInfo> results, Boolean success) {
|
||||
return results.stream()
|
||||
.filter(result ->
|
||||
(success != null ? success.equals(result.isSuccess()) : true)
|
||||
&& null != result.getTarget()
|
||||
)
|
||||
.map(result -> {
|
||||
T t = result.getTarget();
|
||||
return t;
|
||||
})
|
||||
.toList();
|
||||
}
|
||||
|
||||
public static final boolean throwThrowable(List<ExecInfo> results) {
|
||||
for (ExecInfo result: results)
|
||||
result.throwThrowable();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue