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

com.labbol.cocoon.controller.BaseCocoonController Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
/**
 * 
 */
package com.labbol.cocoon.controller;

import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.util.NumberUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;
import org.yelong.core.model.ModelNullProperty;
import org.yelong.core.model.service.ModelService;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.labbol.core.controller.BaseCoreController;
import com.labbol.core.model.BaseModel;
import com.labbol.core.queryinfo.QueryInfo;
import com.labbol.core.queryinfo.filter.QueryFilterInfo;
import com.labbol.core.service.LabbolModelService;

/**
 * @author pengfei
 * @date 2019年11月13日上午8:52:56
 * @version 1.2
 */
@CrossOrigin
//@LogRecord//Cocoon下的默认全部记录日志
public abstract class BaseCocoonController extends BaseCoreController{

	@Resource(name = "modelService")
	protected LabbolModelService modelService;

	/**
	 * 设置model的排序信息。根据sort请求参数
	 * @date 2019年11月13日上午10:20:48
	 * @param model
	 * @see #getSortFieldMap()
	 */
	@SuppressWarnings("unchecked")
	@Deprecated
	protected  void setSortMap(M model) {
		String sortInfoJson = getRequest().getParameter("sort");
		if( StringUtils.isEmpty(sortInfoJson) ) {
			return;
		}
		Gson gson = new Gson();
		ArrayList> sortInfoMapList = gson.fromJson(sortInfoJson, ArrayList.class);
		for (Map querySortInfoMap : sortInfoMapList) {
			model.addSortField(querySortInfoMap.get("property"), querySortInfoMap.getOrDefault("direction", "DESC"));
		}
	}
	
	/**
	 * 获取排序的字段集合
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Map getSortFieldMap(){
		String sortInfoJson = getRequest().getParameter("sort");
		if(StringUtils.isBlank(sortInfoJson)) {
			return Collections.emptyMap();
		} else {
			Gson gson = getGson();
			ArrayList> sortInfoMapList = gson.fromJson(sortInfoJson, ArrayList.class);
			Map sortFieldMap = new HashMap<>(sortInfoMapList.size());
			for (Map map : sortInfoMapList) {
				sortFieldMap.put(map.get("property"), map.getOrDefault("direction","DESC"));
			}
			return sortFieldMap;
		}
	}
	

	protected String pageInfoToJson(PageInfo pageInfo) {
		Gson gson = getGson();
		Map result = new HashMap(2);
		result.put("total", pageInfo.getTotal());
		result.put("root", pageInfo.getList());
		return gson.toJson(result);
	}

	/**
	 * 修改model注入的前缀
	 * @date 2019年11月12日下午5:46:21
	 * @version 1.2
	 * @param binder
	 */
	@InitBinder
	public void initBinderModel(WebDataBinder binder) {    
		binder.setFieldDefaultPrefix("model.");    
		//加入时间解析
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, new NumberFormat() {
			private static final long serialVersionUID = -8790771485055558186L;

			@Override
			public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
				return null;
			}

			@Override
			public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
				return null;
			}

			@Override
			public Number parse(String source, ParsePosition parsePosition) {
				if(StringUtils.isBlank(source)) {
					parsePosition.setIndex(1);
					return ModelNullProperty.INTEGER_NULL;
				} else {
					parsePosition.setIndex(source.length());
					return NumberUtils.parseNumber(source, Integer.class);
				}
			}
		},false));
	} 

	/**
	 * @return 获取请求中传入的查询过滤信息
	 */
	protected List getQueryFilterInfos(){
		//增加queryFilterInfos查询条件
		String filters = getRequest().getParameter("filters");
		if(StringUtils.isBlank(filters)) {
			return Collections.emptyList();
		} else {
			QueryInfo queryInfo = getGson().fromJson(filters, QueryInfo.class);
			List queryFilterInfos = queryInfo.getFilters();
			if(CollectionUtils.isEmpty(queryFilterInfos)) {
				return Collections.emptyList();
			} else {
				return queryFilterInfos;
			}
		}
	}


	//=====================Get/Set==================

	public ModelService getModelService() {
		return modelService;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy