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

master
mjkhan21 3 months ago
parent 4b183dd4e5
commit 0bd0b8c86b

@ -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;
/** .<br />
@ -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 extends ServiceResponse> 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 extends ServiceResponse> 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 extends ServiceResponse> T setMessage(String message) {
public ServiceResponse setMessage(String 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
* @return
*/
public <T extends ServiceResponse> 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 extends ServiceResponse> 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 extends ServiceResponse> 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());
}
}

@ -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<Object> {
return bool.booleanValue();
}
public DataObject convert(String name, Function<Object, ?> converter) {
Object obj = get(name);
return set(name, converter.apply(obj));
}
@Override
public DataObject clone() {
Object copy = super.clone();

Loading…
Cancel
Save