DocSourceDAO代碼:
- package com.jbcom.support.cms.dao;
- import com.jbcom.platform.spring.IBaseDao;
- public interface DocSourceDAO extends IBaseDao {
- public String getHQL_All();
- public String getHQL_RowCount();
- public boolean isNameExist(String docSourceId, String name);
- public boolean isNameExist(String name);
- }
DocSourceDAOImpl代碼:
- package com.jbcom.support.cms.dao.impl;
- import java.util.ArrayList;
- import java.util.List;
- import com.jbcom.platform.spring.BaseDaoImpl;
- import com.jbcom.support.cms.dao.DocSourceDAO;
- import com.jbcom.support.cms.hbm.DocSourceHBM;
- public class DocSourceDAOImpl extends BaseDaoImpl implements DocSourceDAO {
- public String getHQL_All() {
- return "from DocSourceHBM docSourceHBM";
- }
- public String getHQL_RowCount() {
- return "select count(*) from DocSourceHBM docSourceHBM";
- }
- protected Class getPersistentClass() {
- return DocSourceHBM.class;
- }
- public boolean isNameExist(String docSourceId,String name){
- List docSource ;
- docSource = new ArrayList();
- boolean reasult = false;
- if(!docSource.equals("0"))
- docSource = findWithHQL("FROM DocSourceHBM docSourceHBM where docSourceHBM.docSourceId<>'"
- + docSourceId
- + "' and docSourceHBM.name='"
- + name + "'");
- else
- docSource = findWithHQL("FROM DocSourceHBM docSourceHBM where docSourceHBM.name='"+name+"'");
- if (docSource.size() != 0)
- reasult = true;
- return reasult;
- }
- public boolean isNameExist(String name){
- return isNameExist("0", name);
- }
- }
DocSourceEdit代碼:
- package com.jbcom.support.cms.pages;
- import org.apache.tapestry.IRequestCycle;
- import org.apache.tapestry.valid.IValidationDelegate;
- import org.apache.tapestry.valid.ValidationConstraint;
- import com.jbcom.support.cms.dao.DocSourceDAO;
- import com.jbcom.support.cms.hbm.DocSourceHBM;
- import com.jbcom.support.cms.html.DocmentPage;
- import com.jbcom.support.tapestry.Visit;
- public abstract class DocSourceEdit extends DocmentPage {
- // 設定DAO類
- public abstract DocSourceDAO getDocSourceDAO();
- // 具體操作對象
- public abstract DocSourceHBM getDocSourceHBM();
- public abstract void setDocSourceHBM(DocSourceHBM docSourceHBM);
- public void save(IRequestCycle cycle) {
- IValidationDelegate validationDelegate = (IValidationDelegate) getBeans()
- .getBean("delegate");
- doValit(validationDelegate);
- if (validationDelegate.getHasErrors()){
- return;
- }
- DocSourceDAO dao = getDocSourceDAO();
- // 寫入建立人
- getDocSourceHBM().setUser(((Visit)getVisit()).getLoginUser());
- dao.save(getDocSourceHBM());
- // 傳回清單頁
- DocSourceList nextPage = (DocSourceList) cycle.getPage("DocSourceList");
- cycle.activate(nextPage);
- }
- private void doValit(IValidationDelegate validationDelegate) {
- if (validationDelegate.getHasErrors())
- return;
- DocSourceDAO docSourceDAO = getDocSourceDAO();
- String errorMessage = "";
- // 來源名稱為空
- if (getDocSourceHBM().getName() == null
- || getDocSourceHBM().getName().trim().equals("")) {
- errorMessage = "請填寫名稱,名稱不能為空!";
- validationDelegate.record(errorMessage,
- ValidationConstraint.CONSISTENCY);
- return;
- }
- // 來源名稱重複
- boolean reasult;
- if(getDocSourceHBM().getDocSourceId()==null)
- reasult = docSourceDAO.isNameExist(getDocSourceHBM().getName());
- else
- reasult = docSourceDAO.isNameExist(getDocSourceHBM().getDocSourceId(),getDocSourceHBM().getName());
- if(reasult){
- errorMessage = "該名稱已經被使用,請使用一個有效的名稱!";
- validationDelegate.record(errorMessage,
- ValidationConstraint.CONSISTENCY);
- return;
- }
- // 标題過長
- if(getDocSourceHBM().getName().length() >= 50)
- {
- errorMessage = "名稱過長!";
- validationDelegate.record(errorMessage,
- ValidationConstraint.CONSISTENCY);
- return;
- }
- // 描述過長
- if(getDocSourceHBM().getDescription()!=null){
- if(getDocSourceHBM().getDescription().length() >= 50)
- {
- errorMessage = "描述過長!";
- validationDelegate.record(errorMessage,
- ValidationConstraint.CONSISTENCY);
- return;
- }
- }
- // 來源連結位址為空
- if(getDocSourceHBM().getLink()==null ||getDocSourceHBM().getLink()=="" ){
- errorMessage = "請輸入來源的連結位址!";
- validationDelegate.record(errorMessage,
- ValidationConstraint.CONSISTENCY);
- return;
- }
- // 連結過長
- if(getDocSourceHBM().getLink().length() >= 50)
- {
- errorMessage = "連結過長!";
- validationDelegate.record(errorMessage,
- ValidationConstraint.CONSISTENCY);
- return;
- }
- }
- }