天天看點

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. }