package com.eactive.eai.rms.data.entity.onl.b2badaptermap;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.b2badaptermapping.B2BAdapterMap;
import com.eactive.eai.data.entity.onl.b2badaptermapping.B2BAdapterMapId;
import com.eactive.eai.data.entity.onl.b2badaptermapping.QB2BAdapterMap;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.querydsl.core.types.dsl.BooleanExpression;

@Service
@Transactional
public class B2bAdapterMapService extends AbstractDataService<B2BAdapterMap, B2BAdapterMapId, B2BAdapterMapRepository> {

	public Page<B2BAdapterMap> findPage(Pageable pageable, B2bAdapterMapSearch uiSearch) {
		QB2BAdapterMap qObject = QB2BAdapterMap.b2BAdapterMap;
		BooleanExpression predicate = null;

		if (StringUtils.isNotBlank(uiSearch.getSearchAdptBzwkGrp())) {
			predicate = andExpression(predicate,
					qObject.psvsysadptrbzwkgroupname.containsIgnoreCase(uiSearch.getSearchAdptBzwkGrp()));
		}

		if (StringUtils.isNotBlank(uiSearch.getSearchAdptBzwkName())) {
			predicate = andExpression(predicate,
					qObject.adptrbzwkname.containsIgnoreCase(uiSearch.getSearchAdptBzwkName()));
		}

		if (StringUtils.isNotBlank(uiSearch.getSearchEaiSevrInstncName())) {
			predicate = andExpression(predicate,
					qObject.id.eaisevrinstncname.containsIgnoreCase(uiSearch.getSearchEaiSevrInstncName()));
		}

		if (StringUtils.isNotBlank(uiSearch.getSearchEaiSvcName())) {
			predicate = andExpression(predicate,
					qObject.id.eaisvcname.containsIgnoreCase(uiSearch.getSearchEaiSvcName()));
		}

		if (predicate == null)
			return repository.findAll(pageable);

		return repository.findAll(predicate, pageable);
	}
	
	public Iterable<B2BAdapterMap> findAll(B2bAdapterMapSearch uiSearch) {
		QB2BAdapterMap qObject = QB2BAdapterMap.b2BAdapterMap;
		BooleanExpression predicate = null;

		if (StringUtils.isNotBlank(uiSearch.getSearchAdptBzwkGrp())) {
			predicate = andExpression(predicate,
					qObject.psvsysadptrbzwkgroupname.containsIgnoreCase(uiSearch.getSearchAdptBzwkGrp()));
		}

		if (StringUtils.isNotBlank(uiSearch.getSearchAdptBzwkName())) {
			predicate = andExpression(predicate,
					qObject.adptrbzwkname.containsIgnoreCase(uiSearch.getSearchAdptBzwkName()));
		}

		if (StringUtils.isNotBlank(uiSearch.getSearchEaiSevrInstncName())) {
			predicate = andExpression(predicate,
					qObject.id.eaisevrinstncname.containsIgnoreCase(uiSearch.getSearchEaiSevrInstncName()));
		}

		if (StringUtils.isNotBlank(uiSearch.getSearchEaiSvcName())) {
			predicate = andExpression(predicate,
					qObject.id.eaisvcname.containsIgnoreCase(uiSearch.getSearchEaiSvcName()));
		}

		if (predicate == null)
			return repository.findAll();

		return repository.findAll(predicate);
	}

	private BooleanExpression andExpression(BooleanExpression predicate, BooleanExpression right) {
		return predicate == null ? right : predicate.and(right);
	}

}
