All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.bitexpress.topia.commons.data.rpc.ResponseHelper Maven / Gradle / Ivy

There is a newer version: 7.9.1
Show newest version
package io.bitexpress.topia.commons.data.rpc;

import java.util.List;

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 com.godmonth.util.dozer.DozerMapperFunction;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import io.bitexpress.topia.commons.basic.rpc.utils.ListResultResponseUtils;
import io.bitexpress.topia.commons.basic.rpc.utils.ResultResponseUtils;
import io.bitexpress.topia.commons.pagination.Pagination;
import io.bitexpress.topia.commons.rpc.ListResultResponse;
import io.bitexpress.topia.commons.rpc.ResultResponse;
import io.bitexpress.topia.commons.rpc.SystemCode;

@Deprecated
public class ResponseHelper implements InitializingBean {
	private static final Logger logger = LoggerFactory.getLogger(ResponseHelper.class);

	protected Class modelClass;

	protected Mapper mapper;

	protected Function function;

	@Override
	public void afterPropertiesSet() throws Exception {
		function = new DozerMapperFunction(mapper, modelClass);
	}

	public ResultResponse returnSuccess(DOMAIN domain) {
		if (domain != null) {
			return ResultResponseUtils.successResultResponse(getFunction().apply(domain));
		} else {
			return ResultResponseUtils.successResultResponse(null);
		}
	}

	public ListResultResponse returnListSuccess(List domainList) {
		if (CollectionUtils.isNotEmpty(domainList)) {
			List modelList = Lists.transform(domainList, getFunction());
			return ListResultResponseUtils.successListResultResponse(modelList);
		} else {
			return ListResultResponseUtils.successListResultResponse(null);
		}
	}

	public ResultResponse> returnPageSuccess(Page domainPage) {
		Pagination transform = PageTransformer.transform(domainPage, getFunction());
		return ResultResponseUtils.successResultResponse(transform);
	}

	public ResultResponse> returnPageSuccess(Pagination domainPage) {
		Pagination transform = PageTransformer.transform(domainPage, getFunction());
		return ResultResponseUtils.successResultResponse(transform);
	}

	public  ResultResponse returnError(K key, Exception e) {
		logger.error("", e);
		ResultResponse sor = ResultResponseUtils.codeResultResponse(null, SystemCode.FAILURE, null,
				e.getMessage());
		sor.setTrace(ExceptionUtils.getStackTrace(e));
		if (key != null) {
			DOMAIN domain = findModel(key);
			if (domain != null) {
				sor.setResult(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