Yell


--------------------------------------------------------------------------------
yell/doc/DevelopDairy.txt

02/16

@ The basic structure has been done before.  Today, I will focus on implement the

02/17
@ Implement the Struts + servlet structure

--------------------------------------------------------------------------------


yell/doc/spec/database/database.sql

drop table guest.YPosts
create table guest.YPosts (
id int not null,
keyWords varchar(500),
postTitle varchar(500),
postContent text,
poster bit,
previewCount int,
replyCount int,
marked bit,
ipAddress varchar(16),
submitTime datetime ,
primary key (
id
))

--------------------------------------------------------------------------------


yell/doc/spec/database/db.sql

if exists (select * from dbo.sysobjects where id = object_id(N'[guest].[YPostMapping]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [guest].[YPostMapping]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[guest].[YPosts]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [guest].[YPosts]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[guest].[YRegistedUser]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [guest].[YRegistedUser]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[guest].[YTmpUser]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [guest].[YTmpUser]
GO

CREATE TABLE [guest].[YPostMapping] (
 [PostId] [int] NULL ,
 [UserId] [int] NULL ,
 [RegistedUser] [bit] NULL
) ON [PRIMARY]
GO

CREATE TABLE [guest].[YPosts] (
 [id] [int] NOT NULL ,
 [keyWords] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
 [postTitle] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
 [postContent] [text] COLLATE Chinese_PRC_CI_AS NULL ,
 [poster] [bit] NULL ,
 [previewCount] [int] NULL ,
 [replyCount] [int] NULL ,
 [marked] [bit] NULL ,
 [ipAddress] [varchar] (16) COLLATE Chinese_PRC_CI_AS NULL ,
 [submitTime] [datetime] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

CREATE TABLE [guest].[YRegistedUser] (
 [UserId] [int] NOT NULL ,
 [UserName] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
 [NickName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [MobilNumber] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [timestamp] [timestamp] NULL
) ON [PRIMARY]
GO

CREATE TABLE [guest].[YTmpUser] (
 [TmpUserId] [int] NOT NULL ,
 [IPAddress] [varchar] (16) COLLATE Chinese_PRC_CI_AS NULL ,
 [timestamp] [timestamp] NULL
) ON [PRIMARY]
GO

--------------------------------------------------------------------------------


yell/doc/spec/database/dbdesign.xml

<?xml version="1.0" encoding="UTF-8"?>
<db-structure>
 <account>
  <aid/>
  <loginname/>
  <password/>
  <username/>
  <userscore/>
  <regdate/>
 </account>
 <note>
  <nid/>
  <keyword/>
  <title/>
  <content/>
  <postid/>
  <score/>
  <ip/>
  <posttime/>
 </note>
 <titlemapping>
  <score/>
  <title/>
 </titlemapping>
</db-structure>

--------------------------------------------------------------------------------


yell/etc/ear/application.xml

<!DOCTYPE application PUBLIC
   "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
   "http://java.sun.com/dtd/application_1_3.dtd">

<application>
   <display-name>Yell application</display-name>
    <module>
        <ejb>yellbiz.jar</ejb>
    </module>
    <module >
            <web >
                 <web-uri >yell.war</web-uri >
                 <context-root >yell</context-root >
            </web >
    </module >

</application>

--------------------------------------------------------------------------------


yell/build.properties

# Build properties of the project

YELL_HOME=D:/bigbug/Work/Project/yell
JBOSS_HOME=D:/bigbug/Work/Application/jboss-3.2.5

--------------------------------------------------------------------------------


yell/build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="yell" default="deploy" basedir=".">
 <property file="build.properties"/>
 <property file="version.properties"/>
 
 <property name="productname" value="${product.name}"/>
 <property name="version_major" value="${version.major}"/>
 <property name="version_minor" value="${version.minor}"/>
 <property name="yellhome" value="${YELL_HOME}"/>
 <property name="src.dir" value="${YELL_HOME}/src"/>
 <property name="bin.dir" value="${yellhome}/bin"/>
 <property name="build.dir" value="${bin.dir}/build"/>
 <property name="dist.dir" value="${bin.dir}/dist"/>
 <property name="debuggingflag" value="on"/>
 <property name="lib.dir" value="${YELL_HOME}/etc/lib"/>
 <property name="deploy.dir" value="${JBOSS_HOME}/server/default/deploy"/>
 
 <target name="deploy">
  <ant antfile="build-yell.xml"/>
 </target>
 
 <target name="clean">
  <delete dir="${yellhome}/bin"/>
  <delete file="${deploy.dir}/${productname}.ear"/>
 </target>

</project>


--------------------------------------------------------------------------------


yell/build-deploy.xml

--------------------------------------------------------------------------------


yell/build-initData.xml

--------------------------------------------------------------------------------


yell/build-xdoclet-hibernate.xml

--------------------------------------------------------------------------------


yell/build-yell.xml

<project name="yell" default="deploy" basedir=".">

 <property name="bizjarname" value="${productname}biz"/>
 
 <path id="project.classpath">
  <fileset dir="${lib.dir}">
   <include name="**/*.jar"/>
  </fileset>
  <pathelement location="${build.dir}/${bizjarname}/classes"/>
  <pathelement location="${build.dir}/web/WEB_INF/classes"/>
 </path>
 <target name="copy">
  <tstamp/>
  <mkdir dir="${bin.dir}"/>
  <mkdir dir="${build.dir}"/> 
  <mkdir dir="${build.dir}/${bizjarname}/classes"/>
  <javac srcdir="${src.dir}" destdir="${build.dir}/${bizjarname}/classes" debug="on"
    deprecation="on" memoryinitialsize="64M" memorymaximumsize="256M" fork="true">
   <include name="com/**"/>
   <classpath refid="project.classpath"/>
  </javac>
 
  <copy todir="${build.dir}/${bizjarname}/classes">
   <fileset dir="${src.dir}">
    <include name="**/*.hbm.xml"/>
   </fileset>
  </copy>
 
  <copy todir="${build.dir}/web">
   <fileset dir="${src.dir}/web">
    <include name="**/*"/>
   </fileset>
  </copy>
 
  <copy todir="${build.dir}/etc">
   <fileset dir="${YELL_HOME}/etc">
    <include name="**"/>
   </fileset>
  </copy>
 
  <copy todir="${build.dir}/web/WEB-INF/lib">
   <fileset dir="${YELL_HOME}/etc/lib">
    <include name="**"/>
   </fileset>
  </copy>
 
  <mkdir dir="${dist.dir}"/>
 </target>
 
 <!--
 <target name="xdoclet-hibernate" description="Generates Hibernate class descriptor files">
  <echo message="Target xdoclet-hibernate executed"/>
  <ant antfile="xdoclet-hibernate-build.xml" target="xdoclet-hibernate"/>
 </target>
 -->
 
 <target name="package" depends="copy">
  <jar jarfile="${dist.dir}/${bizjarname}.jar">
   <fileset dir="${build.dir}/${bizjarname}/classes">
    <include name="**/*"></include>
   </fileset>
  </jar>
  <war warfile="${dist.dir}/${productname}.war" webxml="${build.dir}/web/WEB-INF/web.xml">
   <fileset dir="${build.dir}/web">
    <include name="**/*"/>
   </fileset>
  </war>
  <ear earfile="${dist.dir}/${productname}.ear" appxml="${build.dir}/etc/ear/application.xml">
   <fileset dir="${dist.dir}" includes="*.sar,*.war,*jar">
   </fileset>
  </ear>
 </target>
 
 <target name="deploy" depends="package">
  <copy todir="${deploy.dir}" overwrite="true">
   <fileset dir="${dist.dir}">
    <include name="*.ear"></include>
   </fileset>
  </copy>
 </target>

</project>

--------------------------------------------------------------------------------


yell/version.properties

# The version of the server

product.name=yell
version.major=1
version.minor=0

--------------------------------------------------------------------------------


yell/biz/service/search/dao/YellsDAO.java

/*
 * Created on 2005-4-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.biz.service.search.dao;

import net.sf.hibernate.HibernateException;

import com.yell.common.dao.HibernateCommonDAO;
import com.yell.db.post.Posts;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class YellsDAO extends HibernateCommonDAO{

    public void updateYells(Posts post){
        try{
            update(post);
        }catch(HibernateException e){
            e.printStackTrace();
        }
       
    }
}

--------------------------------------------------------------------------------


yell/src/com/yell/biz/service/search/dao/YellsDAO.java

/*
 * Created on 2005-4-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.biz.service.search.dao;

import net.sf.hibernate.HibernateException;

import com.yell.common.dao.HibernateCommonDAO;
import com.yell.db.post.Posts;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class YellsDAO extends HibernateCommonDAO{

    public void updateYells(Posts post){
        try{
            update(post);
        }catch(HibernateException e){
            e.printStackTrace();
        }
       
    }
}

--------------------------------------------------------------------------------


yell/biz/web/search/action/SearchYellsAction.java

/*
 * Created on 2005-2-16
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.biz.web.search.action;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.yell.biz.web.search.form.KeyWordsForm;

public class SearchYellsAction extends Action {

    /**
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping actionMapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) {

        KeyWordsForm keyWordsForm = (KeyWordsForm) actionForm;
        String IP = request.getRemoteAddr();
        keyWordsForm.setIpAddress(IP);

        request.setAttribute("keyWordsForm", keyWordsForm);

        String forwardStr = "success";

        return actionMapping.findForward(forwardStr);
    }
}

--------------------------------------------------------------------------------


yell/biz/web/search/action/SubmitYellAction.java

/*
 * Created on 2005-3-1
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.biz.web.search.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.yell.biz.web.search.form.YellForm;
import com.yell.biz.web.search.form.YellsListForm;
import com.yell.db.post.Posts;
import com.yell.biz.service.search.dao.YellsDAO;
import com.yell.common.dao.HibernateSession;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SubmitYellAction extends Action {
   

    public ActionForward execute(ActionMapping actionMapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) {

        YellForm yellForm = (YellForm) actionForm;
       
        updateYell(yellForm);
        return actionMapping.findForward("success");
    }

   
    public void updateYell(YellForm yellForm){
        Posts post = new Posts();
        post.setId(yellForm.getId());
        post.setIpAddress(yellForm.getIpAddress());
        post.setKeyWords(yellForm.getKeyWords());
        post.setPostContent(yellForm.getPostContent());
        post.setPostTitle(yellForm.getPostTitle());
       
        try {
            Session session = HibernateSession.openSession();
//            Transaction tx= session.beginTransaction();
//            session.save(post);
//            tx.commit();
//            session.close();
           
            session.saveOrUpdate(post);
            session.flush();
//            HibernateSession.closeSession();
        } catch (HibernateException e) {
            e.printStackTrace();
        }finally{
            try {
                HibernateSession.closeSession();
            } catch (HibernateException e1) {
                e1.printStackTrace();
            }
        }
    }
}

--------------------------------------------------------------------------------


yell/biz/web/search/action/ViewYellsAction.java

/*
 * Created on 2005-2-28
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.biz.web.search.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.yell.biz.web.search.form.KeyWordsForm;
import com.yell.biz.web.search.form.YellsListForm;
import com.yell.common.dao.HibernateSession;


/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class ViewYellsAction extends Action {

    /**
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping actionMapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) {

        KeyWordsForm keywordForm =(KeyWordsForm)actionForm;
       
        try {
            String keyword = keywordForm.getSearchWords();
            request.setAttribute("yells", getYellsListByKeyword(keyword));

        } catch (HibernateException e) {
            e.printStackTrace();
        }

        return actionMapping.findForward("success");
    }

    /**
     * @return Collection
     */
    public Collection getAllYells() throws HibernateException {

        String sql = "from Posts as post";

        Session session;
        List rsList;

        try {
            session = HibernateSession.openSession();
            Query query = session.createQuery(sql);
            rsList = (List) query.list();

        } finally {
            HibernateSession.closeSession();
        }

        return rsList;
    }

    /**
     * @return Collection
     */
    public Collection getYellsListByKeyword(String keyword) throws HibernateException{
       
        String hql = "select post.id, post.postTitle from Posts as post";
       
        if(keyword.length() > 0){
            hql += " where post.keyWords ='";
            hql += keyword;
            hql += "'";
        }
        Session session;
        ArrayList result = new ArrayList();
       
        try{
            session = HibernateSession.openSession();
            Query query = session.createQuery(hql);
            List rsList = query.list();
           
            for(int i =0; i < rsList.size(); i ++){
                Object[] posts = (Object[]) rsList.get(i);
                YellsListForm yellList = new YellsListForm();
//                yellList.setId(post.getId());
                yellList.setYellTitle((String)posts[1]);
                result.add(yellList);
            }
           
        }finally{
            HibernateSession.closeSession();
        }
        return result;
    }

}

--------------------------------------------------------------------------------


yell/src/com/yell/biz/web/search/form/KeyWordsForm.java

/*
 * Created on 2005-2-16
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.biz.web.search.form;

import java.util.Date;

import org.apache.struts.action.ActionForm;

/**
 * @author big.bugs
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class KeyWordsForm extends ActionForm {
   
    private String searchWords;
    private String ipAddress;
    private String author;
    private Date submitTime;

    /**
     * @return Returns the ipAddress.
     */
    public String getIpAddress() {
        return ipAddress;
    }
    /**
     * @param ipAddress The ipAddress to set.
     */
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }
    /**
     * @return Returns the searchWords.
     */
    public String getSearchWords() {
        return searchWords;
    }
    /**
     * @param searchWords The searchWords to set.
     */
    public void setSearchWords(String searchWords) {
        this.searchWords = searchWords;
    }
    /**
     * @return Returns the submitTime.
     */
    public Date getSubmitTime() {
        return submitTime;
    }
    /**
     * @param submitTime The submitTime to set.
     */
    public void setSubmitTime(Date submitTime) {
        this.submitTime = submitTime;
    }
    /**
     * @return Returns the author.
     */
    public String getAuthor() {
        return author;
    }
    /**
     * @param author The author to set.
     */
    public void setAuthor(String author) {
        this.author = author;
    }
}


--------------------------------------------------------------------------------


yell/src/com/yell/biz/web/search/form/YellForm.java

/*
 * Created on 2005-3-1
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.biz.web.search.form;

import java.util.Date;

import org.apache.struts.action.ActionForm;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class YellForm extends ActionForm{

    private long id;
    private int userId;
    private String keyWords;
    private String postTitle;
    private String postContent;
    private String ipAddress;
    private boolean poster;
    private boolean marked;
    private Date submitTime;
   
   
    /**
     * @return Returns the id.
     */
    public long getId() {
        return id;
    }
    /**
     * @param id The id to set.
     */
    public void setId(long id) {
        this.id = id;
    }
    /**
     * @return Returns the ipAddress.
     */
    public String getIpAddress() {
        return ipAddress;
    }
    /**
     * @param ipAddress The ipAddress to set.
     */
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }
    /**
     * @return Returns the keyWords.
     */
    public String getKeyWords() {
        return keyWords;
    }
    /**
     * @param keyWords The keyWords to set.
     */
    public void setKeyWords(String keyWords) {
        this.keyWords = keyWords;
    }
    /**
     * @return Returns the marked.
     */
    public boolean isMarked() {
        return marked;
    }
    /**
     * @param marked The marked to set.
     */
    public void setMarked(boolean marked) {
        this.marked = marked;
    }
    /**
     * @return Returns the postContent.
     */
    public String getPostContent() {
        return postContent;
    }
    /**
     * @param postContent The postContent to set.
     */
    public void setPostContent(String postContent) {
        this.postContent = postContent;
    }
    /**
     * @return Returns the poster.
     */
    public boolean isPoster() {
        return poster;
    }
    /**
     * @param poster The poster to set.
     */
    public void setPoster(boolean poster) {
        this.poster = poster;
    }
    /**
     * @return Returns the postTitle.
     */
    public String getPostTitle() {
        return postTitle;
    }
    /**
     * @param postTitle The postTitle to set.
     */
    public void setPostTitle(String postTitle) {
        this.postTitle = postTitle;
    }
    /**
     * @return Returns the submitTime.
     */
    public Date getSubmitTime() {
        return submitTime;
    }
    /**
     * @param submitTime The submitTime to set.
     */
    public void setSubmitTime(Date submitTime) {
        this.submitTime = submitTime;
    }
    /**
     * @return Returns the userId.
     */
    public int getUserId() {
        return userId;
    }
    /**
     * @param userId The userId to set.
     */
    public void setUserId(int userId) {
        this.userId = userId;
    }
}


--------------------------------------------------------------------------------


yell/src/com/yell/biz/web/search/form/YellsListForm.java

/*
 * Created on 2005-2-28
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.biz.web.search.form;

import org.apache.struts.action.ActionForm;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class YellsListForm extends ActionForm {

    private Long id;

    private int previewCount;

    private int replyCount;

    private String yellTitle;

    private String yellAuthor;

    private String lastReply;

    private String lastReplyAuthor;

    /**
     * @return Returns the id.
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id
     *            The id to set.
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return Returns the lastReply.
     */
    public String getLastReply() {
        return lastReply;
    }

    /**
     * @param lastReply
     *            The lastReply to set.
     */
    public void setLastReply(String lastReply) {
        this.lastReply = lastReply;
    }

    /**
     * @return Returns the lastReplyAuthor.
     */
    public String getLastReplyAuthor() {
        return lastReplyAuthor;
    }

    /**
     * @param lastReplyAuthor
     *            The lastReplyAuthor to set.
     */
    public void setLastReplyAuthor(String lastReplyAuthor) {
        this.lastReplyAuthor = lastReplyAuthor;
    }

    /**
     * @return Returns the previewCount.
     */
    public int getPreviewCount() {
        return previewCount;
    }

    /**
     * @param previewCount
     *            The previewCount to set.
     */
    public void setPreviewCount(int previewCount) {
        this.previewCount = previewCount;
    }

    /**
     * @return Returns the replyCount.
     */
    public int getReplyCount() {
        return replyCount;
    }

    /**
     * @param replyCount
     *            The replyCount to set.
     */
    public void setReplyCount(int replyCount) {
        this.replyCount = replyCount;
    }

    /**
     * @return Returns the yellAuthor.
     */
    public String getYellAuthor() {
        return yellAuthor;
    }

    /**
     * @param yellAuthor
     *            The yellAuthor to set.
     */
    public void setYellAuthor(String yellAuthor) {
        this.yellAuthor = yellAuthor;
    }

    /**
     * @return Returns the yellTitle.
     */
    public String getYellTitle() {
        return yellTitle;
    }

    /**
     * @param yellTitle
     *            The yellTitle to set.
     */
    public void setYellTitle(String yellTitle) {
        this.yellTitle = yellTitle;
    }
}

--------------------------------------------------------------------------------


yell/src/com/yell/common/dao/CommonDAO.java

/*
 * Created on 2005-2-18
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.common.dao;

import java.io.Serializable;

import net.sf.hibernate.HibernateException;

import com.yell.db.common.core.PersistObject;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public abstract class CommonDAO {

    public abstract void persist(PersistObject o) throws HibernateException;

    public abstract void update(PersistObject o) throws HibernateException;

    public abstract void delete(Class clazz, Serializable id) throws HibernateException;

    public abstract PersistObject findById(Class clazz, Serializable id) throws HibernateException;

}


--------------------------------------------------------------------------------


yell/src/com/yell/common/dao/HibernateCommonDAO.java

/*
 * Created on 2005-2-18
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.common.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import com.yell.db.common.core.PersistObject;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class HibernateCommonDAO extends CommonDAO {

    /*
     * (non-Javadoc)
     *
     * @see com.yell.common.dao.CommonDAO#persist(com.yell.db.common.core.PersistObject)
     */
    public void persist(PersistObject o) throws HibernateException {
        try {
            Session session = HibernateSession.openSession();
            session.save(o);
            session.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            HibernateSession.closeSession();
        }

    }

    /*
     * (non-Javadoc)
     *
     * @see com.yell.common.dao.CommonDAO#update(com.yell.db.common.core.PersistObject)
     */
    public void update(PersistObject o) throws HibernateException {
        try {
            Session session = HibernateSession.openSession();
            session.saveOrUpdate(o);
            session.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            HibernateSession.closeSession();
        }

    }

    /*
     * (non-Javadoc)
     *
     * @see com.yell.common.dao.CommonDAO#delete(java.lang.Class,
     *      java.io.Serializable)
     */
    public void delete(Class clazz, Serializable id) throws HibernateException {
        try {
            Session session = HibernateSession.openSession();
            Object o = session.load(clazz, id);
            session.delete(o);
            session.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            HibernateSession.closeSession();
        }

    }

    public void delete(PersistObject o) throws HibernateException {
        delete(o.getClass(), new Long(o.getId()));
    }

    /**
     * @return PersistObject
     */
    public PersistObject findById(PersistObject o) throws HibernateException {
        return (PersistObject) findById(o.getClass(), new Long(o.getId()));
    }

    /*
     * (non-Javadoc)
     *
     * @see com.yell.common.dao.CommonDAO#findById(java.lang.Class,
     *      java.io.Serializable)
     */
    public PersistObject findById(Class clazz, Serializable id)
            throws HibernateException {
        // TODO To be implemented
        return null;
    }

   
    /**
     * @return Collection
     */
    public Collection searchBySql(String sql) throws HibernateException {
        Session session = HibernateSession.openSession();
        Collection searchResult = new ArrayList();

        try{
            searchResult = session.find(sql);
            session.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            HibernateSession.closeSession();
        }
       
        return searchResult;
    }

}

--------------------------------------------------------------------------------


yell/src/com/yell/common/dao/HibernateSession.java

/*
 * Created on 2005-2-18
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.common.dao;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class HibernateSession {

    private static final ThreadLocal session = new ThreadLocal();
   
   
    /**
     * Gets current session from SessionFactory
     *
     * @return Session
     */
    public static Session openSession() throws HibernateException {
        Configuration conf = new Configuration().configure();
        SessionFactory sf = conf.buildSessionFactory();
       
        Session hSession = (Session)session.get();
        hSession = sf.openSession();
        session.set(hSession);
        return hSession;
    }


    public static void closeSession() throws HibernateException {
        Session hSession = (Session)session.get();
        session.set(null);
       
        if(hSession != null){
            hSession.flush();
            hSession.close();
        }
        return;
    }

    /**
     * @return Session
     */
    public static synchronized Session getCurrentSession() {
        Session hSession = (Session)session.get();
       
        return hSession;
    }

}

--------------------------------------------------------------------------------


yell/src/com/yell/common/util/HQLTest.java

/*
 * Created on 2005-3-1
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.common.util;

import java.io.File;
import java.util.List;

import com.yell.db.post.Posts;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class HQLTest {

    private static String fileName = "D://bigbug//Work//Project//yell//src//web//WEB-INF//classes//hibernate.cfg.xml";
    public static void main(String[] args) {
        File file = new File(fileName);
        String hql = "select post.id,post.keyWords from Posts as post where post.keyWords ='bigbug'";
       
        try {
            Configuration conf = new Configuration().configure(file);
            SessionFactory sf = conf.buildSessionFactory();
            Session session = sf.openSession();
           
            Query query = session.createQuery(hql);
            List rsList = query.list();
           
            for(int i =0; i < rsList.size(); i++){
                Object[] posts = (Object[]) rsList.get(i);
                Long testId = (Long) posts[0];
                String title = (String) posts[1];
                System.out.println(testId + "  " + title);
            }
            session.close();
        } catch (HibernateException e) {

            e.printStackTrace();
        }
    }
}

--------------------------------------------------------------------------------


yell/src/com/yell/common/web/BaseAction.java

/*
 * Created on 2005-2-16
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.common.web;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class BaseAction extends Action{

    public void BaseAction() {
       
    }
    /*
    public ActionForward execute(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) {
       
       
       
    }*/
}

--------------------------------------------------------------------------------


yell/src/com/yell/common/web/BaseAction.java

/*
 * Created on 2005-2-16
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.common.web;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class BaseAction extends Action{

    public void BaseAction() {
       
    }
    /*
    public ActionForward execute(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) {
       
       
       
    }*/
}

--------------------------------------------------------------------------------


yell/src/com/yell/db/common/core/DefaultDBObject.java

/*
 * Created on 2005-2-17
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.db.common.core;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DefaultDBObject extends PersistObject {

}


--------------------------------------------------------------------------------


yell/src/com/yell/db/common/core/PersistObject.java

/*
 * Created on 2005-2-17
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.db.common.core;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PersistObject extends PlainObject{
   
    static final long serialVersionUID = -4936771248317097620L;
    private long                id;
   
    /**
     * @return Returns the id.
     */
    public long getId() {
        return id;
    }
    /**
     * @param id The id to set.
     */
    public void setId(long id) {
        this.id = id;
    }
   
    public Object clone() throws CloneNotSupportedException {
        PersistObject rootObject = (PersistObject) super.clone();
        rootObject.setId(0);
        return rootObject;
    }
   
    public boolean equals(Object other) {
        if (!(other.getClass().isInstance(this))) return false;
        final PersistObject bObject = (PersistObject) other;

        if (this.getId()==0 || bObject.getId()==0) return super.equals(bObject);
        if (this.getId() == bObject.getId()) return true;
       
        return false;
    }   
}


--------------------------------------------------------------------------------


yell/src/com/yell/db/common/core/PlainObject.java

/*
 * Created on 2005-2-17
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.db.common.core;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Date;

/**
 * @author big.bug
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PlainObject implements Serializable, Cloneable{

    private static final String NULL                = "<null>";
    private static final String GET                 = "get";
    private static final String IS                  = "is";
    private static final String END_BRACKET         = "]";
    private static final String FIRST_BRAKET        = "[";
    private static final char   AT                  = '@';
    private static final String EQUALS              = " = ";
    private static final String BLANK_STRING        = "";
   
    static final long           serialVersionUID    = -3920827908241085518L;
   
    public boolean checkField (Field field) {
        if(Modifier.isTransient(field.getModifiers()) ||
            Modifier.isStatic(field.getModifiers())){
            return false;   
            }
        if(field.getType().isPrimitive()) {
            return true;
        }
        if(field.getType().isInstance(new String())) return true;
        if(field.getType().isInstance(new Date())) return true;
        return false;
    }
   
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
   
    public Object cloneWithId() throws CloneNotSupportedException {
        return super.clone();
    }
}

--------------------------------------------------------------------------------


yell/src/com/yell/db/post/Posts.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.yell.db.post.Posts" table="guest.YPosts">
<id name="id" column="id">
<generator class="increment"/>
</id>
<property name="keyWords" column="keyWords"/>
<property name="postTitle" column="postTitle"/>
<property name="postContent" column="postContent"/>
<property name="poster" column="poster"/>
<property name="previewCount" column="previewCount"/>
<property name="replyCount" column="replyCount"/>
<property name="marked" column="marked"/>
<property name="ipAddress" column="ipAddress"/>
<property name="submitTime" column="submitTime"/>
</class>
</hibernate-mapping>

--------------------------------------------------------------------------------


yell/src/com/yell/db/post/Posts.java

/*
 * Created on 2005-2-17
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.yell.db.post;

import java.util.Date;

import com.yell.db.common.core.PersistObject;

/**
 *
 * @hiberate.class table="YPosts"
 */
public class Posts extends PersistObject{

    private long id;

    private String keyWords;

    private String postTitle;

    private String postContent;

    private boolean poster;

    private boolean marked;

    private int previewCount;

    private int replyCount;

    private String ipAddress;

    private Date submitTime;

    /**
     * @return Returns the id.
     */
    public long getId() {
        return id;
    }

    /**
     * @param id The id to set.
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return Returns the ipAddress.
     */
    public String getIpAddress() {
        return ipAddress;
    }

    /**
     * @param ipAddress The ipAddress to set.
     */
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    /**
     * @return Returns the keyWords.
     */
    public String getKeyWords() {
        return keyWords;
    }

    /**
     * @param keyWords The keyWords to set.
     */
    public void setKeyWords(String keyWords) {
        this.keyWords = keyWords;
    }

    /**
     * @return Returns the marked.
     */
    public boolean isMarked() {
        return marked;
    }

    /**
     * @param marked The marked to set.
     */
    public void setMarked(boolean marked) {
        this.marked = marked;
    }

    /**
     * @return Returns the postContent.
     */
    public String getPostContent() {
        return postContent;
    }

    /**
     * @param postContent The postContent to set.
     */
    public void setPostContent(String postContent) {
        this.postContent = postContent;
    }

    /**
     * @return Returns the poster.
     */
    public boolean isPoster() {
        return poster;
    }

    /**
     * @param poster The poster to set.
     */
    public void setPoster(boolean poster) {
        this.poster = poster;
    }

    /**
     * @return Returns the postTitle.
     */
    public String getPostTitle() {
        return postTitle;
    }

    /**
     * @param postTitle The postTitle to set.
     */
    public void setPostTitle(String postTitle) {
        this.postTitle = postTitle;
    }

    /**
     * @return Returns the previewCount.
     */
    public int getPreviewCount() {
        return previewCount;
    }

    /**
     * @param previewCount The previewCount to set.
     */
    public void setPreviewCount(int previewCount) {
        this.previewCount = previewCount;
    }

    /**
     * @return Returns the replyCount.
     */
    public int getReplyCount() {
        return replyCount;
    }

    /**
     * @param replyCount The replyCount to set.
     */
    public void setReplyCount(int replyCount) {
        this.replyCount = replyCount;
    }

    /**
     * @return Returns the submitTime.
     */
    public Date getSubmitTime() {
        return submitTime;
    }

    /**
     * @param submitTime The submitTime to set.
     */
    public void setSubmitTime(Date submitTime) {
        this.submitTime = submitTime;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值