Приложение Б

aquahellishSoftware and s/w Development

Dec 13, 2013 (3 years and 7 months ago)

79 views

80


ПРИЛОЖЕНИЕ Б

(
обязательное)

Про
граммный код класса

работающего с базой данных


package

com.bsuir
.testapp.database;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Iterator;

import org.apache.log4j.Level;

import

org.apache.log4j.Logger;

import org.hibernate.HibernateException;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.Transaction;

import com.
bsuir
.testapp.model.News;

import com.
bsuir
.testapp.util.Constants;

import com.
bsuir
.testapp.util.HibernateSession;


public class NewsHibernateDAO implements INewsDAO {


private static HibernateSession hibSession;


private Transaction tx = null;


private Session session = null;


private Logger logger;


private Query query;


/**



* @param

logger



* org.apache.log4j.logger. Spring bean.



* @see org.apache.log4j.Logger



*/


public void setLogger(Logger value) {



logger = value;


}



public NewsHibernateDAO(HibernateSession hibSession) {



NewsHibernateDAO.hibSession = hibSessi
on;


}



@SuppressWarnings({ "rawtypes", "unchecked" })

81



@Override


public ArrayList<News> getList(int page, int range) throws Exception {



ArrayList<News> allNews = new ArrayList<News>();



ArrayList<News> news = new ArrayList<News>();



int recCount = 0
;




try {




session = hibSession.getFactory().getCurrentSession();




tx = session.beginTransaction();




query =
session.getNamedQuery(Constants.HIBERNATE_NQ_SELECT_COUNT);




for (Iterator it = query.iterate(); it.hasNext();) {





it.next();





recCount++;




}




// counting the first and the last record on the page




int firstRec = (page
-

1) * range;




int lastRec = page * range
-

1;





query =
session.getNamedQuery(Constants.HIBERNATE_NQ_GET_LIST);





allNews = (ArrayList<News>) query.lis
t();









for (int i = firstRec; i <= lastRec; i++) {





try {






news.add(allNews.get(i));





} catch (IndexOutOfBoundsException e) {






break;





}




}





tx.commit();




for (int i = 0; i < news.size(); i++) {





News singleNews = news.get(
i);





String dbDate = singleNews.getDate();

82






String appDate =
DataTypeTransfer.getFormatedDate(dbDate);





singleNews.setDate(appDate);




}




return news;



} catch (HibernateException e) {




if (logger.isEnabledFor(Level.ERROR)) {





logger.error(NewsDAO.class.toString() + ".getList ," +
e);




}




if (tx != null) {





tx.rollback();




}




throw new SQLException(e);




} catch (Exception e) {




if (logger.isEnabledFor(Level.ERROR)) {





logger.error(NewsDAO.class.toString() + ".g
etList ," +
e);




}




throw e;



}



}



@Override


public News fetchByID(int id) throws Exception {



try {




session = hibSession.getFactory().getCurrentSession();





tx = session.beginTransaction();





query =
session.getNamedQuery(Constants.HIBERN
ATE_NQ_FETCH)






.setParameter("id", id);





News news = (News) query.uniqueResult();




if (news == null) {

83






query = session







.getNamedQuery(Constants.HIBERNATE_NQ_FETCH_LAST);





news = (News) query.uniqueResult();




}




tx.commit();




String formatedDate =
DataTypeTransfer.getFormatedDate(news.getDate());




news.setDate(formatedDate);




return news;



} catch (HibernateException e) {




if (logger.isEnabledFor(Level.ERROR)) {





logger.error(NewsDAO.class.toString() + ".getList

," +
e);




}




if (tx != null) {





tx.rollback();




}




throw new SQLException(e);




} catch (NullPointerException e) {




throw new NullPointerException();



} catch (Exception e) {




if (logger.isEnabledFor(Level.ERROR)) {





logger.error(NewsD
AO.class.toString() + ".getList ," +
e);




}




throw e;



}



}



@Override


public void setNews(News news, String action) throws SQLException,




ClassNotFoundException {



int id = news.getId();




session = hibSession.getFactory().getCurrentSession();

84




tx = session.beginTransaction();



query = session.createSQLQuery(Constants.DB_ALTER_SESSION);



query.executeUpdate();




if (Constants.ACTION_EDIT.equals(action)





|| Constants.ACTION_INSERT.equals(action)) {




News newsToUpdate = new News();




ne
wsToUpdate = news;





session.saveOrUpdate(newsToUpdate);




} else {





String hibQuery = QueryGenerator.getHibernateQuery(id,
action);




query = session.createQuery(hibQuery);




query.executeUpdate();



}



tx.commit();


}

}