DataObject.convert(..), ServiceResponse.toDataObject() 추가

master
mjkhan21 3 months ago
parent 4b183dd4e5
commit 0bd0b8c86b

@ -1,6 +1,7 @@
package cokr.xit.foundation.component; package cokr.xit.foundation.component;
import cokr.xit.foundation.AbstractComponent; import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.data.DataObject; import cokr.xit.foundation.data.DataObject;
/** .<br /> /** .<br />
@ -9,13 +10,73 @@ import cokr.xit.foundation.data.DataObject;
* @author mjkhan * @author mjkhan
*/ */
public class ServiceResponse extends AbstractComponent { 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 int affected;
private String private String
code, code,
message; message;
private Object target;
private DataObject info; private DataObject info;
private Boolean success; 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 * @return
@ -29,9 +90,9 @@ public class ServiceResponse extends AbstractComponent {
* @param affected * @param affected
* @return * @return
*/ */
public <T extends ServiceResponse> T setAffected(int affected) { public ServiceResponse setAffected(int affected) {
this.affected = affected; this.affected = affected;
return self(); return this;
} }
/** . /** .
@ -46,9 +107,9 @@ public class ServiceResponse extends AbstractComponent {
* @param code * @param code
* @return * @return
*/ */
public <T extends ServiceResponse> T setCode(String code) { public ServiceResponse setCode(String code) {
this.code = code; this.code = code;
return self(); return this;
} }
/** . /** .
@ -63,9 +124,19 @@ public class ServiceResponse extends AbstractComponent {
* @param message * @param message
* @return * @return
*/ */
public <T extends ServiceResponse> T setMessage(String message) { public ServiceResponse setMessage(String message) {
this.message = message; this.message = message;
return self(); return this;
}
@SuppressWarnings("unchecked")
public <T> 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 * @param val
* @return * @return
*/ */
public <T extends ServiceResponse> T info(String key, Object val) { public ServiceResponse info(String key, Object val) {
info().put(key, val); info().put(key, val);
return self(); return this;
} }
/** . /** .
@ -110,15 +181,15 @@ public class ServiceResponse extends AbstractComponent {
* @param success * @param success
* @return * @return
*/ */
public <T extends ServiceResponse> T setSuccess(boolean success) { public ServiceResponse setSuccess(boolean success) {
this.success = success; this.success = success;
return self(); return this;
} }
/** . /** .
* @return * @return
*/ */
public RuntimeException getThrowable() { public Throwable getThrowable() {
return throwable; return throwable;
} }
@ -127,9 +198,9 @@ public class ServiceResponse extends AbstractComponent {
* @param throwable * @param throwable
* @return * @return
*/ */
public <T extends ServiceResponse> T setThrowable(Throwable throwable) { public ServiceResponse setThrowable(Throwable throwable) {
this.throwable = runtimeException(throwable); this.throwable = throwable;
return self(); return this;
} }
/** throw. /** throw.
@ -137,7 +208,22 @@ public class ServiceResponse extends AbstractComponent {
*/ */
public boolean throwThrowable() { public boolean throwThrowable() {
if (throwable != null) if (throwable != null)
throw throwable; throw runtimeException(throwable);
return false; 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());
}
} }

@ -1,6 +1,7 @@
package cokr.xit.foundation.data; package cokr.xit.foundation.data;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import cokr.xit.foundation.Assert; import cokr.xit.foundation.Assert;
@ -119,6 +120,11 @@ public class DataObject extends StringMap<Object> {
return bool.booleanValue(); return bool.booleanValue();
} }
public DataObject convert(String name, Function<Object, ?> converter) {
Object obj = get(name);
return set(name, converter.apply(obj));
}
@Override @Override
public DataObject clone() { public DataObject clone() {
Object copy = super.clone(); Object copy = super.clone();

Loading…
Cancel
Save