天天看点

Tapestry 验证用户名

DocSourceDAO代码:

  1. package com.jbcom.support.cms.dao;
  2. import com.jbcom.platform.spring.IBaseDao;
  3. public interface DocSourceDAO extends IBaseDao {
  4.         public String getHQL_All();
  5.         public String getHQL_RowCount();
  6.     public boolean isNameExist(String docSourceId, String name);
  7.     public boolean isNameExist(String name);
  8. }

DocSourceDAOImpl代码:

  1. package com.jbcom.support.cms.dao.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.jbcom.platform.spring.BaseDaoImpl;
  5. import com.jbcom.support.cms.dao.DocSourceDAO;
  6. import com.jbcom.support.cms.hbm.DocSourceHBM;
  7. public class DocSourceDAOImpl extends BaseDaoImpl implements DocSourceDAO {
  8.     public String getHQL_All() {
  9.         return "from DocSourceHBM docSourceHBM";
  10.     }
  11.     public String getHQL_RowCount() {
  12.         return "select count(*) from DocSourceHBM docSourceHBM";
  13.     }
  14.     protected Class getPersistentClass() {
  15.         return DocSourceHBM.class;
  16.     }
  17.     public boolean isNameExist(String docSourceId,String name){
  18.         List docSource ;
  19.         docSource = new ArrayList();
  20.         boolean reasult = false;
  21.         if(!docSource.equals("0"))
  22.             docSource = findWithHQL("FROM DocSourceHBM docSourceHBM where docSourceHBM.docSourceId<>'"
  23.                     + docSourceId
  24.                     + "' and docSourceHBM.name='"
  25.                     + name + "'");
  26.         else
  27.             docSource = findWithHQL("FROM DocSourceHBM docSourceHBM where docSourceHBM.name='"+name+"'");
  28.         if (docSource.size() != 0)
  29.             reasult = true;
  30.         return reasult;
  31.     }
  32.     public boolean isNameExist(String name){
  33.         return isNameExist("0", name);
  34.     }
  35. }

DocSourceEdit代码:

  1. package com.jbcom.support.cms.pages;
  2. import org.apache.tapestry.IRequestCycle;
  3. import org.apache.tapestry.valid.IValidationDelegate;
  4. import org.apache.tapestry.valid.ValidationConstraint;
  5. import com.jbcom.support.cms.dao.DocSourceDAO;
  6. import com.jbcom.support.cms.hbm.DocSourceHBM;
  7. import com.jbcom.support.cms.html.DocmentPage;
  8. import com.jbcom.support.tapestry.Visit;
  9. public abstract class DocSourceEdit extends DocmentPage {
  10.     // 设定DAO类
  11.     public abstract DocSourceDAO getDocSourceDAO();
  12.     // 具体操作对象
  13.     public abstract DocSourceHBM getDocSourceHBM();
  14.     public abstract void setDocSourceHBM(DocSourceHBM docSourceHBM);
  15.     public void save(IRequestCycle cycle) {
  16.         IValidationDelegate validationDelegate = (IValidationDelegate) getBeans()
  17.                 .getBean("delegate");
  18.         doValit(validationDelegate);        
  19.         if (validationDelegate.getHasErrors()){         
  20.             return;
  21.         }
  22.         DocSourceDAO dao = getDocSourceDAO();       
  23.         // 写入创建人
  24.         getDocSourceHBM().setUser(((Visit)getVisit()).getLoginUser());
  25.         dao.save(getDocSourceHBM());        
  26.         // 返回列表页
  27.         DocSourceList nextPage = (DocSourceList) cycle.getPage("DocSourceList");
  28.         cycle.activate(nextPage);
  29.     }
  30.     private void doValit(IValidationDelegate validationDelegate) {
  31.         if (validationDelegate.getHasErrors())
  32.             return;
  33.         DocSourceDAO docSourceDAO = getDocSourceDAO();
  34.         String errorMessage = "";
  35.         // 来源名称为空
  36.         if (getDocSourceHBM().getName() == null
  37.                 || getDocSourceHBM().getName().trim().equals("")) {
  38.             errorMessage = "请填写名称,名称不能为空!";
  39.             validationDelegate.record(errorMessage,
  40.                     ValidationConstraint.CONSISTENCY);
  41.             return;
  42.         }
  43.         // 来源名称重复
  44.         boolean reasult;
  45.         if(getDocSourceHBM().getDocSourceId()==null)
  46.             reasult = docSourceDAO.isNameExist(getDocSourceHBM().getName());
  47.         else
  48.             reasult = docSourceDAO.isNameExist(getDocSourceHBM().getDocSourceId(),getDocSourceHBM().getName());
  49.         if(reasult){
  50.             errorMessage = "该名称已经被使用,请使用一个有效的名称!";
  51.             validationDelegate.record(errorMessage,
  52.                     ValidationConstraint.CONSISTENCY);
  53.             return;
  54.         }
  55.         // 标题过长
  56.         if(getDocSourceHBM().getName().length() >= 50)
  57.         {
  58.             errorMessage = "名称过长!";
  59.             validationDelegate.record(errorMessage,
  60.                     ValidationConstraint.CONSISTENCY);
  61.             return;
  62.         }
  63.         // 描述过长
  64.         if(getDocSourceHBM().getDescription()!=null){
  65.         if(getDocSourceHBM().getDescription().length() >= 50)       
  66.         {
  67.             errorMessage = "描述过长!";
  68.             validationDelegate.record(errorMessage,
  69.                     ValidationConstraint.CONSISTENCY);
  70.             return;
  71.         }
  72.         }
  73.         // 来源链接地址为空
  74.         if(getDocSourceHBM().getLink()==null ||getDocSourceHBM().getLink()=="" ){
  75.             errorMessage = "请输入来源的链接地址!";
  76.             validationDelegate.record(errorMessage,
  77.                     ValidationConstraint.CONSISTENCY);
  78.             return;
  79.         }
  80.         // 链接过长
  81.         if(getDocSourceHBM().getLink().length() >= 50)
  82.         {
  83.             errorMessage = "链接过长!";
  84.             validationDelegate.record(errorMessage,
  85.                     ValidationConstraint.CONSISTENCY);
  86.             return;
  87.         }
  88.     }
  89. }