diff --git a/src/main/java/cokr/xit/foundation/component/ServiceResponse.java b/src/main/java/cokr/xit/foundation/component/ServiceResponse.java index 2b61eb7..ce49483 100644 --- a/src/main/java/cokr/xit/foundation/component/ServiceResponse.java +++ b/src/main/java/cokr/xit/foundation/component/ServiceResponse.java @@ -1,6 +1,7 @@ package cokr.xit.foundation.component; import cokr.xit.foundation.AbstractComponent; +import cokr.xit.foundation.Assert; import cokr.xit.foundation.data.DataObject; /**서비스 응답으로 반환하는 정보를 갖는 객체의 베이스 클래스.
@@ -9,13 +10,73 @@ import cokr.xit.foundation.data.DataObject; * @author mjkhan */ public class ServiceResponse extends AbstractComponent { + /**데이터처리 유형 + * @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); + } + } + + private Type type; private int affected; private String code, message; + private Object target; private DataObject info; private Boolean success; - private RuntimeException throwable; + private Throwable throwable; + + /**데이터처리 유형을 반환한다. + * @return 데이터처리 유형 + */ + public Type getType() { + return type; + } + + /**데이터처리 유형을 설정한다. + * @param type 데이터처리 유형 + */ + public ServiceResponse setType(Type type) { + this.type = type; + return this; + } /**저장된 데이터수를 반환한다. * @return 저장된 데이터수 @@ -29,9 +90,9 @@ public class ServiceResponse extends AbstractComponent { * @param affected 저장된 데이터수 * @return 현재 응답 */ - public T setAffected(int affected) { + public ServiceResponse setAffected(int affected) { this.affected = affected; - return self(); + return this; } /**결과코드를 반환한다. @@ -46,9 +107,9 @@ public class ServiceResponse extends AbstractComponent { * @param code 결과코드 * @return 현재 응답 */ - public T setCode(String code) { + public ServiceResponse setCode(String code) { this.code = code; - return self(); + return this; } /**결과메시지를 반환한다. @@ -63,9 +124,19 @@ public class ServiceResponse extends AbstractComponent { * @param message 결과메시지 * @return 현재 응답 */ - public T setMessage(String message) { + public ServiceResponse setMessage(String message) { this.message = message; - return self(); + return this; + } + + @SuppressWarnings("unchecked") + public T getTarget() { + return (T)target; + } + + public ServiceResponse setTarget(Object target) { + this.target = target; + return this; } /**응답 추가정보를 반환한다. @@ -88,9 +159,9 @@ public class ServiceResponse extends AbstractComponent { * @param val 추가정보 * @return 현재 응답 */ - public T info(String key, Object val) { + public ServiceResponse info(String key, Object val) { info().put(key, val); - return self(); + return this; } /**성공여부를 반환한다. @@ -110,15 +181,15 @@ public class ServiceResponse extends AbstractComponent { * @param success 성공여부 * @return 현재 응답 */ - public T setSuccess(boolean success) { + public ServiceResponse setSuccess(boolean success) { this.success = success; - return self(); + return this; } /**오류를 반환한다. * @return 오류 */ - public RuntimeException getThrowable() { + public Throwable getThrowable() { return throwable; } @@ -127,9 +198,9 @@ public class ServiceResponse extends AbstractComponent { * @param throwable 오류 * @return 현재 응답 */ - public T setThrowable(Throwable throwable) { - this.throwable = runtimeException(throwable); - return self(); + public ServiceResponse setThrowable(Throwable throwable) { + this.throwable = throwable; + return this; } /**오류가 설정되어 있으면 throw한다. @@ -137,7 +208,22 @@ public class ServiceResponse extends AbstractComponent { */ public boolean throwThrowable() { if (throwable != null) - throw throwable; + throw runtimeException(throwable); return false; } + + public DataObject toDataObject() { + DataObject map = new DataObject() + .set("affected", getAffected()) + .set("code", getCode()) + .set("message", getMessage()) + .set("target", getTarget()); + + if (!isEmpty(info)) + info.forEach(map::put); + + return map + .set("success", isSuccess()) + .set("throwable", getThrowable()); + } } \ No newline at end of file diff --git a/src/main/java/cokr/xit/foundation/data/DataObject.java b/src/main/java/cokr/xit/foundation/data/DataObject.java index 5ae1612..cfab17a 100644 --- a/src/main/java/cokr/xit/foundation/data/DataObject.java +++ b/src/main/java/cokr/xit/foundation/data/DataObject.java @@ -1,6 +1,7 @@ package cokr.xit.foundation.data; import java.util.Map; +import java.util.function.Function; import java.util.function.Supplier; import cokr.xit.foundation.Assert; @@ -119,6 +120,11 @@ public class DataObject extends StringMap { return bool.booleanValue(); } + public DataObject convert(String name, Function converter) { + Object obj = get(name); + return set(name, converter.apply(obj)); + } + @Override public DataObject clone() { Object copy = super.clone();