⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shql.java

📁 对jsp页面中有多个查询条件时
💻 JAVA
字号:
package org.storm.xwguan.search;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.storm.xwguan.exception.ConditionsEmptyException;
import org.storm.xwguan.exception.ConditionsNotFoundException;
import org.storm.xwguan.exception.ParamNullEmptyException;
import org.storm.xwguan.interfaces.IForm;
import org.storm.xwguan.interfaces.ISHql;
import org.storm.xwguan.parse.ConditionsMapping;
import org.storm.xwguan.parse.FieldsMapping;
import org.storm.xwguan.parse.FieldsReflect;
import org.storm.xwguan.parse.Parse;
import org.storm.xwguan.util.Constants;

public abstract class SHql implements ISHql {

	private FieldsReflect fieldsReflect;
	private Parse parse;

	// 查询条件不为空的字段保存信息
	private Map<String, String> values = new HashMap<String, String>();
	private Map<Integer, String> names = new HashMap<Integer, String>();

	private String pojoName;// 表对应的pojo名
	private String tableName;// 表名
	private List<FieldsMapping> fieldsList;// 字段描述信息保存列表
	private List<ConditionsMapping> conditionsList;// 额外查询条件信息保存列表

	private String shql;
	private String haveNoConditions;// 没有额外查询条件时,不为空值的语句
	private String path;
	private IForm form;
	private int num;// 查询条件不为空的个数

	public void init() {
		try {
			fieldsReflect = new FieldsReflect(form);
		} catch (Exception e) {
			e.printStackTrace();
		}
		values = fieldsReflect.getValues();
		names = fieldsReflect.getNames();
		try {
			parse = new Parse(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		pojoName = parse.getPojoName();
		tableName = parse.getTableName();
		fieldsList = parse.getFieldsList();
		conditionsList = parse.getConditionsList();
		shql = getShql();

		num = values.size() - checkHaveNO();
		haveNoConditions = produceSHql();
	}

	private String produceBy(int type, boolean isBound, boolean isFirst,
			boolean isBlurry, String formName, String tableFieldName,
			String value) {
		switch (type) {
		case Constants.INTEGER:
			if (isBound == true && isFirst == true) {
				// 如果是范围查询字段并且是查询条件的第一个 >=
				return tableFieldName + Constants.K4 + Integer.parseInt(value);
			} else if (isBound == true && isFirst == false) {
				// 如果是范围查询字段并且是查询条件的第二个 <=
				return tableFieldName + Constants.K2 + Integer.parseInt(value);
			} else {

				// 一般的查询条件
				return tableFieldName + Constants.K3 + Integer.parseInt(value);
			}
		case Constants.FLOAT:
			if (isBound == true && isFirst == true) {
				return tableFieldName + Constants.K4 + Float.parseFloat(value);
			} else if (isBound == true && isFirst == false) {
				return tableFieldName + Constants.K2 + Float.parseFloat(value);
			} else {

				return tableFieldName + Constants.K3 + Float.parseFloat(value);
			}
		case Constants.DOUBLE:
			if (isBound == true && isFirst == true) {
				return tableFieldName + Constants.K4
						+ Double.parseDouble(value);
			} else if (isBound == true && isFirst == false) {
				return tableFieldName + Constants.K2
						+ Double.parseDouble(value);
			} else {
				return tableFieldName + Constants.K3
						+ Double.parseDouble(value);
			}
		case Constants.STRING:
			if (isBound == true && isFirst == true) {
				return tableFieldName + Constants.K7 + value + Constants.K8;
			} else if (isBound == true && isFirst == false) {
				return tableFieldName + Constants.K5 + value + Constants.K8;
			} else {
				if (isBlurry == true) {
					return tableFieldName + Constants.K13 + value
							+ Constants.K14;
				} else {
					return tableFieldName + Constants.K6 + value + Constants.K8;
				}
			}
		default:
			return null;
		}
	}

	// 得到在配置文件中没有对form中字段进行描述的个数
	private int checkHaveNO() {
		int j = 0;
		for (int i = 0; i < names.size(); i++) {
			boolean flag = false;
			for (FieldsMapping fldmap : fieldsList) {
				String name = fldmap.getFormName();
				if (names.get(i).equals(name)) {
					flag = true;
					continue;
				}
			}
			if (flag == false && values.get(names.get(i)) != null) {
				++j;
			}
		}
		return j;
	}

	// 得到没有额外查询条件的hql语句
	private String produceSHql() {

		String shql = new String();
		int size = 0;
		for (FieldsMapping fldmap : fieldsList) {
			String name = fldmap.getFormName();
			String tableFieldName = fldmap.getTableFieldName();
			String value = values.get(name);
			if (value != null) {
				String temp = produceBy(fldmap.getType(), fldmap.isBound(),
						fldmap.isFirst(), fldmap.isBlurry(), name,
						tableFieldName, value);
				shql = shql + temp;

				++size;
				if (size < num) {
					// 如果没有到最后一个不为空值的查询条件时,中间要加 and
					shql = shql + Constants.K9;
				}
			}
		}
		return shql;
	}

	// 添加额外查询条件
	private String addAdditionConditions() throws ConditionsEmptyException {
		String shql = new String();
		int size = 0;
		if (conditionsList.size() < 1) {
			// 如果配置文件中额外查询条件信息为空,抛出异常
			throw new ConditionsEmptyException();
		}
		for (ConditionsMapping cdsmap : conditionsList) {
			String temp = produceBy(cdsmap.getType(), cdsmap.isBound(), cdsmap
					.isFirst(), cdsmap.isBlurry(), cdsmap.getName(), cdsmap
					.getTableFieldName(), cdsmap.getValue());
			shql = shql + temp;
			++size;
			if (size < conditionsList.size()) {
				// 如果没有到最后一个不为空值的额外查询条件时,中间要加 and
				shql = shql + Constants.K9;
			}
		}
		if (num < 1) {
			// 如果查询条件全为空,则直接加上额外查询条件
			return shql;
		} else {
			// 如果查询条件不全为空,则查询条件和额外查询条件中间加 and
			return haveNoConditions + Constants.K9 + shql;

		}
	}

	// 添加一个指定的额外查询条件
	private String addAdditionConditions(final String key)
			throws ParamNullEmptyException, ConditionsNotFoundException,
			ConditionsEmptyException {

		String shql = new String();
		if (conditionsList.size() < 1) {
			// 如果配置文件中额外查询条件信息为空,抛出异常
			throw new ConditionsEmptyException();
		}
		if (key == null || key.trim().equals(Constants.K15)) {
			// 指定查询条件为null或为空,抛出异常
			throw new ParamNullEmptyException();
		}
		int size = 0;
		for (ConditionsMapping cdsmap : conditionsList) {
			if (key.trim().equals(cdsmap.getName())) {
				String temp = produceBy(cdsmap.getType(), cdsmap.isBound(),
						cdsmap.isFirst(), cdsmap.isBlurry(), cdsmap.getName(),
						cdsmap.getTableFieldName(), cdsmap.getValue());
				shql = shql + temp;
				++size;
			}
		}
		if (size == 0) {
			// 在配置文件中没有找到指定该条件异常
			throw new ConditionsNotFoundException();
		}
		if (num < 1) {
			// 如果查询条件全为空,则直接加上额外查询条件
			return shql;
		} else {
			// 如果查询条件不全为空,则查询条件和额外查询条件中间加 and
			return haveNoConditions + Constants.K9 + shql;
		}
	}

	// 添加两个指定的额外查询条件

	private String addAdditionConditions(final String key1, final String key2)
			throws ConditionsEmptyException, ParamNullEmptyException,
			ConditionsNotFoundException {
		String shql = new String();
		if (conditionsList.size() < 1) {
			// 如果配置文件中额外查询条件信息为空,抛出异常
			throw new ConditionsEmptyException();
		}
		if (key1 == null || key2 == null || key1.trim().equals(Constants.K15)
				|| key2.trim().equals(Constants.K15)) {
			// 指定查询条件为null或为空,抛出异常
			throw new ParamNullEmptyException();
		}
		int size = 0;
		for (ConditionsMapping cdsmap : conditionsList) {
			if (key1.trim().equals(cdsmap.getName())
					|| key2.trim().equals(cdsmap.getName())) {
				String temp = produceBy(cdsmap.getType(), cdsmap.isBound(),
						cdsmap.isFirst(), cdsmap.isBlurry(), cdsmap.getName(),
						cdsmap.getTableFieldName(), cdsmap.getValue());
				shql = shql + temp;
				++size;
				if (size < 2) {
					// 如果没有到最后一个不为空值的额外查询条件时,中间要加 and
					shql = shql + Constants.K9;
				}
			}
		}
		if (size != 2) {
			// 在配置文件中没有找到指定该条件一个或两个,抛出异常
			throw new ConditionsNotFoundException();
		}
		if (num < 1) {
			// 如果查询条件全为空,则直接加上额外查询条件
			return shql;
		} else {
			// 如果查询条件不全为空,则查询条件和额外查询条件中间加 and
			return haveNoConditions + Constants.K9 + shql;
		}
	}

	// 得到没有额外查询条件hql语句
	public String getSentenceHaveNoConditions() {
		if (num < 1) {
			// 在所有查询字段都为空时,默认查出表中全部信息
			// 这时 shql=havaNoConditions
			return shql;
		}
		else {
			return shql + Constants.K1 + haveNoConditions;
		}
	}

	// 得到有额外查询条件的hql语句
	public String getSentenceHaveConditions() {

		try {
			return shql + Constants.K1 + addAdditionConditions();
		} catch (ConditionsEmptyException e) {
			e.printStackTrace();
		}
		return null;

	}

	// 得到一个指定的额外查询条件的hql语句
	public String getSentenceHaveConditions(final String key) {

		try {
			return shql + Constants.K1 + addAdditionConditions(key);
		} catch (ParamNullEmptyException e) {
			e.printStackTrace();
		} catch (ConditionsNotFoundException e) {
			e.printStackTrace();
		} catch (ConditionsEmptyException e) {
			e.printStackTrace();
		}
		return null;

	}

	// 得到两个指定的额外查询条件的hql语句
	public String getSentenceHaveConditions(final String key1, final String key2) {
		try {
			return shql + Constants.K1 + addAdditionConditions(key1, key2);
		} catch (ParamNullEmptyException e) {
			e.printStackTrace();
		} catch (ConditionsNotFoundException e) {
			e.printStackTrace();
		} catch (ConditionsEmptyException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void setConfigFileLocation(final String path) {
		this.path = path;
	}

	public void setReflectForm(final IForm form) {
		this.form = form;
	}

	public String getPojoName() {
		return pojoName;
	}

	public String getTableName() {
		return tableName;
	}

	public boolean isEmpty() {
		return values.isEmpty();
	}

	protected abstract String getShql();

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -