com.bld.commons.controller.BaseSearchController Maven / Gradle / Ivy
The newest version!
package com.bld.commons.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import com.bld.commons.controller.mapper.ModelMapper;
import com.bld.commons.reflection.model.BaseParameter;
import com.bld.commons.reflection.model.QueryParameter;
import com.bld.commons.service.JpaService;
import com.bld.commons.utils.data.BaseModel;
import com.bld.commons.utils.data.CollectionResponse;
import com.bld.commons.utils.data.ObjectResponse;
public abstract class BaseSearchController, P extends BaseParameter, MM extends ModelMapper> {
/** The jpa service. */
@Autowired
protected JpaService jpaService;
protected CollectionResponse findByFilter(P baseParameter) throws Exception {
QueryParameter queryFilter = new QueryParameter<>(baseParameter);
CollectionResponse response = new CollectionResponse<>();
List list = this.jpaService.findByFilter(queryFilter);
Long totalCount = this.jpaService.countByFilter(queryFilter);
List listModel = new ArrayList<>();
for(E entity:list) {
M model=this.modelMapper().convertToModel(entity);
listModel.add(model);
}
response.setData(listModel);
response.setTotalCount(totalCount != null ? totalCount : Long.valueOf(0));
if(queryFilter.getPageable()!=null) {
response.setPageNumber(queryFilter.getPageable().getPageNumber());
response.setPageSize(queryFilter.getPageable().getPageSize());
}
return response;
}
protected ObjectResponse countByFilter(@RequestBody P baseParameter) throws Exception {
QueryParameter queryFilter = new QueryParameter<>(baseParameter);
Long count = this.jpaService.countByFilter(queryFilter);
return new ObjectResponse<>(count);
}
protected ObjectResponse singleResultFindByFilter(@RequestBody P baseParameter) throws Exception{
QueryParameterquery=new QueryParameter<>(baseParameter);
ObjectResponse response = new ObjectResponse<>();
E entity = this.jpaService.singleResultByFilter(query);
response.setData(this.modelMapper().convertToModel(entity));
return response;
}
protected abstract MM modelMapper();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy