?? himessagefuncs.java
字號:
/**
*
*/
package com.tiandinet.HiMessage;
import com.tiandinet.hibernate.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import java.util.List;
import java.util.Iterator;
import java.util.Hashtable;
import java.util.Set;
/**
* @author Meng Yang
*
*/
public class HiMessageFuncs {
private Hashtable replyMsgs = new Hashtable();
/**
*
*/
public HiMessageFuncs() {
}
/**
* get Configuration
* @return Conf
*/
public Hashtable getReplyMsgs() {
return this.replyMsgs;
}
public Conf getConfiguration() {
Conf conf = null;
try {
Session session = HibernateUtil.getSession();
String sql = "select conf from Conf as conf";
Query query = session.createQuery(sql);
query.setMaxResults(1);
List result = query.list();
if (result.size() == 1) {
conf = (Conf)result.get(0);
}
else {
// add conf
Transaction tx = session.beginTransaction();
conf = new Conf();
conf.setPageshow(new Integer(20));
conf.setShowIp(new Integer(1));
conf.setTitle("HiMessage - TiandiNet.com");
session.save(conf);
tx.commit();
}
HibernateUtil.closeSession();
}
catch (Exception e) {
}
return conf;
}
/**
* get message total
* @return int
*/
public int getMessageTotal() {
int total = 0;
try {
Session session = HibernateUtil.getSession();
total = ((Integer)session.createQuery("select count(*) from Msg").iterate().next()).intValue();
HibernateUtil.closeSession();
}
catch (Exception he) {
total = 0;
}
return total;
}
/**
* get page total
*/
public int getPageTotal(int messageTotal, int pageshow) {
int pageTotal = 0;
if (messageTotal > 0) {
float totalTmp = (float)messageTotal/(float)pageshow;
float totalTmp2 = (float)(messageTotal/pageshow);
if (totalTmp > totalTmp2) {
pageTotal = (int)totalTmp2 + 1;
}
else {
pageTotal = (int)totalTmp2;
}
}
return pageTotal;
}
public Msg getMessage(String msgId, Session session) {
Msg msg = null;
try {
//Session session = HibernateUtil.getSession();
String sql = "select msg from Msg as msg where msg.MsgId=:msgid";
Query query = session.createQuery(sql);
query.setString("msgid", msgId);
query.setMaxResults(1);
Iterator result = query.iterate();
while (result.hasNext()) {
msg = (Msg)result.next();
}
//HibernateUtil.closeSession();
}
catch (Exception he) {
msg = null;
}
return msg;
}
public Msg getMessageForServletInvoke(String msgId) {
Msg msg = null;
try {
Session session = HibernateUtil.getSession();
String sql = "select msg from Msg as msg where msg.MsgId=:msgid";
Query query = session.createQuery(sql);
query.setString("msgid", msgId);
query.setMaxResults(1);
Iterator result = query.iterate();
while (result.hasNext()) {
msg = (Msg)result.next();
}
HibernateUtil.closeSession();
}
catch (Exception he) {
}
return msg;
}
/**
* get messages
* @param page
* @return Hashtable
*/
public Hashtable getMessages(int page) {
Conf conf = this.getConfiguration();
Hashtable ht = new Hashtable();
/*
int total = this.getMessageTotal();
if (total == 0) {
return ht;
}
*/
try {
Session session = HibernateUtil.getSession();
int pageShow = conf.getPageshow().intValue();
//String sql = "select msg from Msg as msg order by msg.MsgId desc";
String sql = "select msg from Msg as msg order by msg.PostTime desc";
Query query = session.createQuery(sql);
int firstResult = (page - 1) * pageShow;
query.setFirstResult(firstResult);
//query.setFetchSize(pageShow);
query.setMaxResults(pageShow);
//System.out.println(">>>>>>> firstResult: " + String.valueOf(firstResult));
//System.out.println(">>>>>>> pageShow: " + String.valueOf(pageShow));
Iterator result = query.iterate();
int i = 0;
while (result.hasNext()) {
Msg msg = (Msg)result.next();
Set s = msg.getPics();
ht.put(new Integer(i), msg);
if (msg.getReId() != null
&& !msg.getReId().equals("")
&& !msg.getReId().equals("NONE")) {
Msg replyMsg = this.getMessage(msg.getReId(), session);
if (replyMsg == null) {
replyMsg = new Msg();
replyMsg.setMsgId("Deleted");
}
this.replyMsgs.put(new String(msg.getMsgId()), replyMsg);
}
i++;
}
HibernateUtil.closeSession();
}
catch (Exception he) {
}
return ht;
}
/**
* get messages
* @param page
* @return List
*/
public List getMessagesList(int page) {
Conf conf = this.getConfiguration();
List result = null;
/*
int total = this.getMessageTotal();
if (total == 0) {
return result;
}
*/
try {
Session session = HibernateUtil.getSession();
int pageShow = conf.getPageshow().intValue();
String sql = "select msg from Msg as msg order by msg.MsgId desc";
Query query = session.createQuery(sql);
int firstResult = (page - 1) * pageShow;
query.setFirstResult(firstResult);
query.setFetchSize(pageShow);
result = query.list();
HibernateUtil.closeSession();
}
catch (Exception he) {
}
return result;
}
/**
* add new message
* @param reId ip name qq email msn content filename
* @return boolean
*/
public boolean addMessage(String reId,
String ip,
String name,
String headima,
String qq,
String email,
String msn,
String content,
String filename) {
try {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Msg msg = new Msg();
msg.setReId(reId);
msg.setIp(ip);
msg.setName(name);
msg.setHeadima(headima);
msg.setQq(qq);
msg.setEmail(email);
msg.setMsn(msn);
msg.setIcq("");
msg.setSite("");
msg.setIsEnable(new Integer(1));
msg.setPostTime(new java.util.Date());
msg.setContent(this.HtmlSpecialChars(content));
// set the uploaded file
if (filename != null
&& !filename.equals("NONE")) {
Pic pic = new Pic();
pic.setMsg(msg);
pic.setFilename(filename);
if (msg.getPics() == null) {
msg.setPics(new java.util.HashSet());
}
msg.getPics().add(pic);
}
session.save(msg);
tx.commit();
HibernateUtil.closeSession();
return true;
}
catch (Exception e) {
//
return false;
}
}
/**
*
*/
public String HtmlSpecialChars(String str)
{
if(str == null||str.equals(""))
{
return str;
}
StringBuffer temp = new StringBuffer();
int i = 0;
while(i < str.length())
{
if(str.charAt(i) == '\n')
{
temp = temp.append("<br>");
}
else if(str.charAt(i) == ' ')
{
temp = temp.append(" ");
}
else if(str.charAt(i) == '<')
{
temp = temp.append("<");
}
else if(str.charAt(i) == '>')
{
temp = temp.append(">");
}
else
{
temp = temp.append(str.substring(i,i+1));
}
i++;
}
String okstring = temp.toString();
return okstring;
}
/**
* Generate the page link
* @param url queryString pageTotal page
* @return String
*
* @author Meng Yang
*/
public String pageLink(String url, String queryString, int pageTotal, int page) {
int num = 2;
String link = "";
if (page < 1) {
page = 1;
}
else if (page > pageTotal) {
page = pageTotal;
}
if (!queryString.equals("")) {
String[] pattern = new String[4];
String[] replace = new String[4];
pattern[0] = "(^page=[0-9]*&)";
pattern[1] = "(&page=[0-9]*&)";
pattern[2] = "(&page=[0-9]*$)";
pattern[3] = "(^page=[0-9]*$)";
replace[0] = "";
replace[1] = "&";
replace[2] = "";
replace[3] = "";
for (int i = 0, k = pattern.length; i < k; i++) {
queryString = queryString.replaceAll(pattern[i], replace[i]);
}
}
if (!queryString.equals("")) {
link = url + "?" + queryString;
}
else {
link = url;
}
if (link.indexOf("?") == -1) {
link += "?page=";
}
else {
link += "&page=";
}
String linkString = "";
if (page != 1) {
linkString += "<a href=\"" + link + "1\">|<<</a> ";
linkString += "<a href=\"" + link + String.valueOf(page - 1) + "\"><</a> ";
}
int beginRange = page - 1;
if (beginRange > num) {
beginRange = num;
}
String last = "";
if (beginRange > 0) {
for (int i = page - beginRange; i < page; i++) {
last += "<a href=\"" + link + String.valueOf(i) + "\">" + String.valueOf(i) + "</a> ";
}
}
int endRange = pageTotal - page;
if (endRange > num) {
endRange = num;
}
String next = "";
if (endRange > 0) {
for (int i = page + 1; i < (page + endRange + 1); i++) {
next += "<a href=\"" + link + String.valueOf(i) + "\">" + String.valueOf(i) + "</a> ";
}
}
linkString += last + "<strong>" + String.valueOf(page) + "</strong> " + next;
if (page != pageTotal && pageTotal > 0) {
linkString += "<a href=\"" + link + String.valueOf(page + 1) + "\">></a> ";
linkString += "<a href=\"" + link + String.valueOf(pageTotal) + "\">>>|</a>";
}
return linkString;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -