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

📄 formprocessbean.java

📁 OBPM是一个开源
💻 JAVA
字号:
package cn.myapps.core.dynaform.form.ejb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.beanutils.PropertyUtils;

import cn.myapps.base.action.ParamsTable;
import cn.myapps.base.dao.DAOFactory;
import cn.myapps.base.dao.DataPackage;
import cn.myapps.base.dao.IBaseDAO;
import cn.myapps.base.dao.PersistenceUtils;
import cn.myapps.base.dao.ValueObject;
import cn.myapps.base.ejb.BaseProcessBean;
import cn.myapps.core.dynaform.form.action.ImpropriateException;
import cn.myapps.core.dynaform.form.dao.FormDAO;
import cn.myapps.core.user.action.WebUser;
import cn.myapps.util.Debug;
import cn.myapps.util.StringUtil;
import cn.myapps.util.sequence.Sequence;

public class FormProcessBean extends BaseProcessBean implements FormProcess {

	public void doCreate(ValueObject vo) throws Exception {
		ParamsTable params = new ParamsTable();
		params.setParameter("s_name", ((Form) vo).getName());
		if (((Form) vo).getModule() != null
				&& ((Form) vo).getModule().getId() != null
				&& ((Form) vo).getModule().getId().equals("")) {
			params.setParameter("s_module", ((Form) vo).getModule().getId());
		}
		params.setParameter("s_application", ((Form) vo).getApplicationid());
		Collection colls = this.doSimpleQuery(params);
		if (colls != null && colls.size() > 0) {
			throw new ImpropriateException(
					"Exist same name,please choose another!");
		} else {
			super.doCreate(vo);
		}
	}

	public void doCreate(ValueObject vo, WebUser user) throws Exception {
		Form formVO = (Form) vo;
		formVO.setId(Sequence.getSequence());
		String template = formVO.getTemplatecontext();
		// template =
		// template.replaceAll("\\[计算插入模板\\]</MARQUEE>|</IMAGE>|</IMG>","");
		template = template.replaceAll("</IMAGE>|</IMG>", "");
		formVO.setTemplatecontext(template);

		((FormDAO) getDAO()).create(formVO, user);
	}

	public void doUpdate(ValueObject vo, WebUser user) throws Exception {
		Form formVO = (Form) vo;
		// 若模版的名字修改,相应修改该站点下面的所有模板内容
		if (formVO.getType() == 2 || formVO.getType() == 3) {

			Form oldform = (Form) doView(formVO.getId());
			if (oldform != null && formVO.getName() != null
					&& oldform.getName() != null
					&& !oldform.getName().equals(formVO.getName())) {
				Debug.println("Update FormName Starting------");
				changeFormName(oldform, formVO, vo.getApplicationid());
				Debug.println("Update FormName End------");
			}
		}
		String template = formVO.getTemplatecontext();
		// template =
		// template.replaceAll("\\[计算插入模板\\]</MARQUEE>|</IMAGE>|</IMG>","");
		template = template.replaceAll("</IMAGE>|</IMG>", "");
		formVO.setTemplatecontext(template);
		((FormDAO) getDAO()).update(formVO, user);
	}

	public Form doViewByFormName(String formName, String application)
			throws Exception {
		return (Form) ((FormDAO) getDAO())
				.findByFormName(formName, application);
	}

	public Collection doGetFields(ParamsTable params, String application)
			throws Exception {

		Collection rtn = new ArrayList();
		params.setParameter("application", application);
		DataPackage result = ((FormDAO) getDAO()).query(params);
		// get all fields
		Iterator iter = result.datas.iterator();
		while (iter.hasNext()) {
			Form form = (Form) iter.next();
			rtn.addAll(form.deepSearchFields(form.getFields()));
		}
		return rtn;
	}

	// 检查模版有效性
	public Collection doValidity(Form form) throws Exception {
		Collection errors = new ArrayList();
		boolean hasBrief = false;
		// 检查是否有名称有中文
		if (StringUtil.haveChinesewords(form.getName())) {
			ValidateMessage errormsg = new ValidateMessage();
			errormsg.setErrmessage("模板中不能含有中文名称!");
			errormsg.setFieldname("name");
			errors.add(errormsg);
		}
		// 名称是否已经在该频道中应用
		if (!checkDuplicateName(form, form.getApplicationid())) {
			ValidateMessage errormsg = new ValidateMessage();
			errormsg.setErrmessage("名称已经在该频道中应用!");
			errormsg.setFieldname("name");
			errors.add(errormsg);
		}
		// 检查字段名称合法性
		Collection fields = form.getFields();
		if (fields != null) {
			Iterator iterator = fields.iterator();
			while (iterator.hasNext()) {
				FormField field = (FormField) iterator.next();
				if (field != null) {
					// if (field.getName() == null
					// || field.getName().trim().length() <= 0)
					// throw new Exception("表单中存在字段未定义名称!");
					hasBrief = hasBrief
							|| (field.getIsbrief() != null && field
									.getIsbrief().trim().equals("1"))
							|| (field.getIsbrief() != null && field
									.getIsbrief().toLowerCase().equals("true"))
							|| (field.getIsbrief() != null && field
									.getIsbrief().toLowerCase().equals("yes"))
							|| (field.getIsbrief() != null && field
									.getIsbrief().toLowerCase().equals("y"));
					if (!checkFieldNames(field.getName())) {
						ValidateMessage errormsg = new ValidateMessage();
						errormsg.setErrmessage("不能用'" + field.getName()
								+ "'作为字段名称!");
						errormsg.setFieldname("templatecontext");
						errors.add(errormsg);
					}
				}
			}
		}
		// 表单模板时一定要有内容
		if (form.getTemplatecontext() != null
				&& form.getTemplatecontext().length() > 0) {
		} else {
			ValidateMessage errormsg = new ValidateMessage();
			errormsg.setErrmessage("模版内容不能为空!");
			errormsg.setFieldname("templatecontext");
			errors.add(errormsg);
		}

		// 判断是否存在重复字段名称
		// if(haveDuplicateFieldNames(form))
		// throw new Exception("表单中存在重复字段名称!");

		return errors;
	}

	// 检查字段名称合法性
	private boolean checkFieldNames(String fieldname) throws Exception {
		String[] keyword = { "id", "taskid", "siteid", "channelid", "caption",
				"author", "source", "ispicture", "isfirst", "weight",
				"relationalwords", "keywords", "summary", "content",
				"formname", "created", "lastmodified", "readers", "writers",
				"owners", "owner", "flow", "state" };
		String[] htmltag = { " ", "<", ">", "[", "]", "{", "}", "'", "\"" };
		if (fieldname == null)
			return true;

		for (int i = 0; i < keyword.length; ++i)
			if (fieldname.toLowerCase().trim().equals(keyword[i]))
				return false;
		for (int i = 0; i < htmltag.length; ++i)
			if (fieldname.indexOf(htmltag[i]) >= 0)
				return false;
		return true;
	}

	// 判断是否存在重复字段名称
	public boolean haveDuplicateFieldNames(Form form) throws Exception {
		Collection fields = form.getFields();
		if (fields != null) {
			Iterator iterator = fields.iterator();
			while (iterator.hasNext()) {
				FormField field = (FormField) iterator.next();
				if (field != null) {
					if (checkFieldCount(form, field.getName()) > 1)
						return true;
				}
			}
		}
		return false;
	}

	private int checkFieldCount(Form form, String fieldName) throws Exception {
		int count = 0;
		Collection fields = form.getFields();
		if (fields != null) {
			Iterator iterator = fields.iterator();
			while (iterator.hasNext()) {
				FormField field = (FormField) iterator.next();
				if (field != null) {
					if (field.getName().trim().equals(fieldName))
						count++;
				}
			}
		}
		return count;
	}

	// 检查是否有重名
	private boolean checkDuplicateName(Form form, String application)
			throws Exception {
		ParamsTable params = new ParamsTable();
		params.setParameter("s_name", form.getName());
		// params.setParameter("l_siteid", String.valueOf(form.getSiteid()));
		params.setParameter("xl_id", String.valueOf(form.getId()));

		DataPackage forms = ((FormDAO) getDAO()).query(params);
		if (forms != null)
			return forms.datas.size() <= 0;
		else
			return true;
	}

	public void changeFormName(Form oldform, Form newform, String application)
			throws Exception {

		ParamsTable params = new ParamsTable();
		// params.setParameter("n_siteid", String.valueOf(newform.getSiteid()));
		DataPackage datas = ((FormDAO) getDAO()).query(params);

		if (datas != null) {
			Iterator forms = datas.getDatas().iterator();
			while (forms.hasNext()) {
				Form form = (Form) forms.next();
				String content = form.getTemplatecontext();
				if (content != null) {
					String regex = "/" + oldform.getName() + ".html2";
					String replacement = "/" + newform.getName() + ".html2";
					Debug.println("---->" + regex + " replace to "
							+ replacement);
					content = content.replaceAll(regex, replacement);
					form.setTemplatecontext(content);
				}
				((FormDAO) getDAO()).update(form);
			}
		}
	}

	public Collection get_formList(String application) throws Exception {
		return this.doSimpleQuery(null, application);
		// return ((FormDAO) getDAO()).simpleQuery(null, application);
	}

	public Collection getFormsByModule(String moduleid, String application)
			throws Exception {
		return ((FormDAO) getDAO()).getFormsByModule(moduleid, application);
	}

	public Collection getSearchFormsByApplication(String appid,
			String application) throws Exception {
		return ((FormDAO) getDAO()).getSearchFormsByApplication(appid,
				application);
	}

	protected IBaseDAO getDAO() throws Exception {
		return DAOFactory.getDefaultDAO(Form.class.getName());
	}

	public Collection getSearchFormsByModule(String moduleid, String application)
			throws Exception {
		return ((FormDAO) getDAO()).getSearchFormsByModule(moduleid,
				application);
	}

	public void doUpdate(ValueObject vo) throws Exception {
		try {
			PersistenceUtils.beginTransaction();
			ValueObject po = getDAO().find(vo.getId());
			if (((Form) vo).getVersion() != ((Form) po).getVersion())
				throw new ImpropriateException(
						"Already having been impropriate by others , can not Save");
			((Form) vo).setVersion(((Form) vo).getVersion() + 1);
			if (po != null) {
				PropertyUtils.copyProperties(po, vo);
				getDAO().update(po);
			} else {
				getDAO().update(vo);
			}

			PersistenceUtils.commitTransaction();
		} catch (ImpropriateException e) {
			PersistenceUtils.rollbackTransaction();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			PersistenceUtils.rollbackTransaction();
		}
	}

	public DataPackage doFormList(ParamsTable params, String application)
			throws Exception {
		return ((FormDAO) getDAO()).queryForm(params, application);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.myapps.core.dynaform.form.ejb.FormProcess#doSaveShortHand(cn.myapps.base.dao.ValueObject,
	 *      cn.myapps.base.action.ParamsTable, java.lang.String[],
	 *      java.lang.String[])
	 */
}

⌨️ 快捷键说明

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