ExecInfo 추가

master
mjkhan21 2 weeks ago
parent a0419681d6
commit 7700d098f8

@ -1,5 +1,9 @@
package cokr.xit.foundation.component;
import java.util.ArrayList;
import java.util.List;
import java.util.function.ToIntFunction;
import cokr.xit.foundation.ApplicationException;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.UserInfo;
@ -105,4 +109,41 @@ public interface AbstractMapper extends Assert.Support, Convert.Support {
default ApplicationException applicationException(Throwable t) {
return ApplicationException.get(null);
}
/** .
* @param <T>
* @param proc
* @param objs
* @return
*/
default <T> List<ExecInfo> save(ToIntFunction<T> proc, List<T> objs) {
ArrayList<ExecInfo> results = new ArrayList<>();
for (T obj: objs)
try {
int affected = proc.applyAsInt(obj);
if (affected > 0)
results.add(
affected == 1 ?
ExecInfo.SUCCESS :
new ExecInfo().setAffected(affected)
);
else if (affected < 1)
results.add(
new ExecInfo()
.setTarget(obj)
.setAffected(affected)
.setSuccess(false)
.setMessage("저장하지 못했습니다.")
);
} catch (Exception e) {
results.add(
new ExecInfo()
.setTarget(obj)
.setThrowable(e)
);
}
return results;
}
}

@ -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;
}
}

@ -8,5 +8,4 @@ package cokr.xit.foundation.component;
* @author mjkhan
*/
public class QueryRequest extends ServiceRequest {
private static final long serialVersionUID = 1L;
}

@ -1,17 +1,13 @@
package cokr.xit.foundation.component;
import java.io.Serializable;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.AbstractObject;
/** .<br />
* (4 ) .<br />
* .
* @author mjkhan
*/
public abstract class ServiceRequest extends AbstractComponent implements Serializable {
private static final long serialVersionUID = 1L;
public abstract class ServiceRequest extends AbstractObject {
private String
type,
by,

@ -1,7 +1,10 @@
package cokr.xit.foundation.component;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.Assert;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import cokr.xit.foundation.AbstractObject;
import cokr.xit.foundation.data.DataObject;
/** .<br />
@ -9,133 +12,29 @@ 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;
public class ServiceResponse extends AbstractObject {
private List<ExecInfo> results;
private DataObject info;
private Boolean success;
private Throwable throwable;
/** .
* @return
*/
public Type getType() {
return type;
public List<ExecInfo> getResults() {
return ifEmpty(results, Collections::emptyList);
}
/** .
* @param type
*/
public ServiceResponse setType(Type type) {
this.type = type;
return this;
}
/** .
* @return
*/
public int getAffected() {
return affected;
}
/** .
* @param <T>
* @param affected
* @return
*/
public ServiceResponse setAffected(int affected) {
this.affected = affected;
return this;
}
/** .
* @return
*/
public String getCode() {
return code;
}
/** .
* @param <T>
* @param code
* @return
*/
public ServiceResponse setCode(String code) {
this.code = code;
public ServiceResponse setResults(List<ExecInfo> results) {
this.results = results;
return this;
}
/** .
* @return
*/
public String getMessage() {
return message;
}
/** .
* @param <T>
* @param message
* @return
*/
public ServiceResponse setMessage(String message) {
this.message = message;
return this;
}
public ServiceResponse add(ExecInfo result) {
if (results == null)
results = new ArrayList<>();
else if (!(results instanceof ArrayList)) {
List<ExecInfo> tmp = results;
results = new ArrayList<>(tmp);
}
@SuppressWarnings("unchecked")
public <T> T getTarget() {
return (T)target;
}
results.add(result);
public ServiceResponse setTarget(Object target) {
this.target = target;
return this;
}
@ -149,7 +48,7 @@ public class ServiceResponse extends AbstractComponent {
/** .
* @return
*/
public DataObject info() {
private DataObject info() {
return ifEmpty(info, () -> info = new DataObject());
}
@ -164,66 +63,17 @@ public class ServiceResponse extends AbstractComponent {
return this;
}
/** .
* @return
* <ul><li> true</li>
* <li> false</li>
* <li> </li>
* </ul>
*/
public boolean isSuccess() {
return throwable == null
&& ifEmpty(success, Boolean.TRUE);
}
/**( true) .
* @param <T>
* @param success
* @return
/** .
* @return
*/
public ServiceResponse setSuccess(boolean success) {
this.success = success;
return this;
}
/** .
* @return
*/
public Throwable getThrowable() {
return throwable;
}
/** RuntimeException .
* @param <T>
* @param throwable
* @return
*/
public ServiceResponse setThrowable(Throwable throwable) {
this.throwable = throwable;
return this;
public int getAffected() {
return ExecInfo.getAffected(results);
}
/** throw.
* @return false
/** .
* @return
*/
public boolean throwThrowable() {
if (throwable != null)
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());
public boolean isSuccess() {
return ExecInfo.isSuccess(results);
}
}

@ -111,4 +111,4 @@ public class DataProc {
this.error = Assert.rootCause(error);
return this;
}
}
}
Loading…
Cancel
Save