package com.yem.ensign.weboffice.form;

import java.util.EventObject;
import java.util.regex.Pattern;


import com.yem.ensign.common.ConstantUtils;
import com.yem.ensign.common.StringUtils;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.events.ClickListener;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.control.events.ItemClickListener;
import kd.bos.algo.DataSet;
import kd.bos.db.DB;

/**
 *动态查询数据源 表单插件
 * @author lz(梁壮)
 * @date 2020-08-24 下午8:20:39
 */
public class DyquerydatasouEdit extends AbstractFormPlugin implements ClickListener, ItemClickListener {

	private static final String REGEX = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";

	String flt = "'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,";

	private static String reg1 = "\"(?:')|(?:--)|(/\\\\*(?:.|[\\\\n\\\\r])*?\\\\*/)|\"+(\\b(update|union|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";//"(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"+ //select|

	private static String reg = "(update|delete|insert|into|substr|into|drop|execute)";
	private static Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addItemClickListeners("tbmain");
		this.addClickListeners("tbmain");
	}

	@Override
	public void itemClick(ItemClickEvent evt) {
		super.itemClick(evt);
		String itemKey = evt.getItemKey();
		if (!StringUtils.isEmpty(itemKey)) {
			IDataModel model = this.getModel();
			IFormView view = this.getView();
			if ("yem_baritemap".equals(itemKey)) {
				String sqleditor = (String) model.getValue("yem_sqleditor");
				if (!StringUtils.isEmpty(sqleditor)) {
					sqleditor =sqleditor.replace("?","1");
					if (isValid(sqleditor)==false) {
						this.getView().showMessage("未能通过过滤器：str=" + sqleditor + "，请检查！ ");
						// this.getView().showMessage("当前脚本中存在【*】及其他特殊字符，请检查！ ");
						return;
					} else {
						DataSet data = DB.queryDataSet("", ConstantUtils.KZKF, sqleditor);
						String[] FieldNames = data.getRowMeta().getFieldNames();
						if (FieldNames.length > 0) {
							DynamicObjectCollection entryentitycol = this.getModel().getEntryEntity("yem_entryentity");
							this.getView().getModel().deleteEntryData("yem_entryentity");
							if (entryentitycol.getRowCount() == 0) {
								boolean isfid = false;
								for (int i = 0; i < FieldNames.length; i++) {
									String filedName = FieldNames[i];
									filedName = filedName.toUpperCase();
									if (filedName.equals("FID")) {
										isfid = true;
									}
									this.getView().getModel().createNewEntryRow("yem_entryentity");
									this.getView().getModel().setValue("yem_textfield", filedName, i);
								}
								if (isfid == false) {
									this.getView().showMessage("当前查询未包含FID，请检查！ ");
									this.getView().getModel().deleteEntryData("yem_entryentity");
									return;
								}
							}else 
							{
								this.getView().getModel().deleteEntryData("yem_entryentity");
								boolean isfid = false;
								for (int i = 0; i < FieldNames.length; i++) {
									String filedName = FieldNames[i];
									filedName = filedName.toUpperCase();
									if (filedName.equals("FID")) {
										isfid = true;
									}
									this.getView().getModel().createNewEntryRow("yem_entryentity");
									this.getView().getModel().setValue("yem_textfield", filedName, i);
								}
								if (isfid == false) {
									this.getView().showMessage("当前查询未包含FID，请检查！ ");
									this.getView().getModel().deleteEntryData("yem_entryentity");
									return;
								}
							}
						}
					}
				}
			}
		}
	}

	public static boolean isMatches(String str, String regex) {
		return str.matches(regex);
	}

	private boolean isValid(String str) {
		if (sqlPattern.matcher(str).find()) {
			return false;
		}
		return true;
	}
}
