io.bitexpress.topia.commons.data.rpc.ResponseHelper2 Maven / Gradle / Ivy
package io.bitexpress.topia.commons.data.rpc;
import com.godmonth.util.dozer.DozerMapperFunction;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import io.bitexpress.topia.commons.basic.rpc.utils2.BodyResponseUtils;
import io.bitexpress.topia.commons.basic.rpc.utils2.ListBodyResponseUtils;
import io.bitexpress.topia.commons.pagination.Pagination;
import io.bitexpress.topia.commons.rpc.SystemCode;
import io.bitexpress.topia.commons.rpc.response.BodyResponse;
import io.bitexpress.topia.commons.rpc.response.ListBodyResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.dozer.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.domain.Page;
import java.io.Serializable;
import java.util.List;
public class ResponseHelper2 implements InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(ResponseHelper2.class);
protected Class modelClass;
protected Mapper mapper;
protected Function function;
@Override
public void afterPropertiesSet() throws Exception {
function = new DozerMapperFunction(mapper, modelClass);
}
public BodyResponse returnSuccess(DOMAIN domain) {
if (domain != null) {
return BodyResponseUtils.successBodyResponse(getFunction().apply(domain));
} else {
return BodyResponseUtils.successBodyResponse(null);
}
}
public ListBodyResponse returnListSuccess(List domainList) {
if (CollectionUtils.isNotEmpty(domainList)) {
List modelList = Lists.transform(domainList, getFunction());
return ListBodyResponseUtils.successListBodyResponse(modelList);
} else {
return ListBodyResponseUtils.successListBodyResponse(null);
}
}
public BodyResponse> returnPageSuccess(Page domainPage) {
Pagination transform = PageTransformer.transform(domainPage, getFunction());
return BodyResponseUtils.successBodyResponse(transform);
}
public BodyResponse> returnPageSuccess(Pagination domainPage) {
Pagination transform = PageTransformer.transform(domainPage, getFunction());
return BodyResponseUtils.successBodyResponse(transform);
}
public BodyResponse returnError(K key, Exception e) {
logger.error("", e);
BodyResponse sor = BodyResponseUtils.codeBodyResponse(null, SystemCode.FAILURE, null,
e.getMessage());
sor.getHeader().setTrace(ExceptionUtils.getStackTrace(e));
if (key != null) {
DOMAIN domain = findModel(key);
if (domain != null) {
sor.setBody(getFunction().apply(domain));
}
}
return sor;
}
protected DOMAIN findModel(Object key) {
return null;
}
public void setModelClass(Class modelClass) {
this.modelClass = modelClass;
}
public void setMapper(Mapper mapper) {
this.mapper = mapper;
}
public Function getFunction() {
return function;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy