001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.action;
017:
018: import java.io.BufferedReader;
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023: import java.text.MessageFormat;
024: import java.text.SimpleDateFormat;
025: import java.util.Date;
026: import java.util.List;
027:
028: import javax.servlet.ServletContext;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import net.sf.hibernate.HibernateException;
033: import net.sf.hibernate.Query;
034: import net.sf.hibernate.Session;
035:
036: import org.apache.commons.lang.StringUtils;
037: import org.apache.struts.action.Action;
038: import org.apache.struts.action.ActionError;
039: import org.apache.struts.action.ActionErrors;
040: import org.apache.struts.action.ActionForm;
041: import org.apache.struts.action.ActionForward;
042: import org.apache.struts.action.ActionMapping;
043: import org.htmlparser.Node;
044: import org.htmlparser.Parser;
045: import org.htmlparser.tags.TitleTag;
046: import org.htmlparser.util.NodeIterator;
047:
048: import dlog4j.Globals;
049: import dlog4j.SiteManager;
050: import dlog4j.UserManager;
051: import dlog4j.blog.BlogTrackBack;
052: import dlog4j.blog.TrackBackResp;
053: import dlog4j.formbean.BookMarkBean;
054: import dlog4j.formbean.CategoryForm;
055: import dlog4j.formbean.DraftForm;
056: import dlog4j.formbean.LogForm;
057: import dlog4j.formbean.LoginTrackBean;
058: import dlog4j.formbean.ReplyForm;
059: import dlog4j.formbean.SiteForm;
060: import dlog4j.formbean.UserForm;
061: import dlog4j.search.SearchProxy;
062: import dlog4j.security.DlogRole;
063: import dlog4j.security.SecurityConfig;
064: import dlog4j.util.RequestUtils;
065: import dlog4j.util.mail.Mailer;
066:
067: /**
068: * DlogAction.java created by EasyStruts - XsltGen.
069: * http://easystruts.sf.net
070: * created on 02-06-2004
071: *
072: * XDoclet definition:
073: * @struts:action validate="true"
074: * @struts:action-forward name="/calendar.jsp" path="/calendar.jsp"
075: * @struts:action-forward name="/log_comment.jsp" path="/log_comment.jsp"
076: * @struts:action-forward name="/comment.jsp" path="/comment.jsp"
077: * @struts:action-forward name="/log_category.jsp" path="/log_category.jsp" redirect="true"
078: */
079: public class DlogLogAction extends DlogActionBase {
080:
081: public final static String PARAM_DRAFT = "draft_id";
082: // --------------------------------------------------------- Instance Variables
083: static String uploadDir = null;
084: static String MODIFY_APPEND_FORMAT = "MODIFY_APPEND_FORMAT";
085:
086: // --------------------------------------------------------- Methods
087:
088: /**
089: * 删除日记的书签
090: */
091: public ActionForward doDeleteBookMark(ActionMapping mapping,
092: ActionForm form, HttpServletRequest request,
093: HttpServletResponse response, String mark_id)
094: throws Exception {
095: Session ssn = null;
096: //判断用户是否已登录
097: ActionErrors msgs = new ActionErrors();
098: UserForm user = UserForm.getLoginUser(request);
099: if (user != null && user.isLogin())
100: try {
101: ssn = getSession();
102: SiteForm site = SiteManager.getCurrentSite(request);
103: int markid = Integer.parseInt(mark_id);
104: BookMarkBean bmb = (BookMarkBean) ssn.load(
105: BookMarkBean.class, new Integer(markid));
106: if (bmb.getUser().getId() != user.getId())
107: msgs.add("bookmark", new ActionError(
108: "operation_not_allow"));
109: else
110: ssn.delete(bmb);
111: } catch (HibernateException e) {
112: } catch (NumberFormatException e) {//如果解析参数错误不理会
113: } finally {
114: commitSession(ssn, true);
115: }
116: ActionForward forward = new ActionForward(mapping.findForward(
117: "bookmark").getPath(), false);
118: if (!msgs.isEmpty())
119: this .saveErrors(request, msgs);
120: else
121: forward.setRedirect(true);
122:
123: return forward;
124: }
125:
126: /**
127: * 添加日记的书签
128: */
129: public ActionForward doAddBookMark(ActionMapping mapping,
130: ActionForm form, HttpServletRequest request,
131: HttpServletResponse response, String log_id)
132: throws Exception {
133: Session ssn = null;
134: //判断用户是否已登录
135: ActionErrors msgs = new ActionErrors();
136: UserForm user = UserForm.getLoginUser(request);
137: if (user != null && user.isLogin())
138: try {
139: ssn = getSession();
140: SiteForm site = SiteManager.getCurrentSite(request);
141: int logid = Integer.parseInt(log_id);
142: LogForm log = new LogForm();
143: log.setId(logid);
144: //判断书签是否已经存在
145: String hql = "SELECT COUNT(b.id) FROM "
146: + BookMarkBean.class.getName()
147: + " AS b WHERE b.site.id=? AND b.log.id=? AND b.user.id=?";
148: Query q = ssn.createQuery(hql);
149: q.setInteger(0, site.getId());
150: q.setInteger(1, logid);
151: q.setInteger(2, user.getId());
152: int uc = 0;
153: try {
154: uc = ((Integer) q.list().get(0)).intValue();
155: } catch (Exception e) {
156: }
157: if (uc == 0) {
158: BookMarkBean bmb = new BookMarkBean(site, user, log);
159: bmb.setCreateTime(new Date());
160: bmb.setType(BookMarkBean.BM_LOG);
161: ssn.save(bmb);
162: msgs.add("bookmark", new ActionError(
163: "bookmark.addok"));
164: } else
165: msgs.add("bookmark", new ActionError(
166: "bookmark.exits"));
167: } catch (NumberFormatException e) {//如果解析参数错误不理会
168: } finally {
169: commitSession(ssn, true);
170: }
171:
172: ActionForward forward = new ActionForward(mapping.findForward(
173: "showlog").getPath(), false);
174: if (!msgs.isEmpty())
175: this .saveErrors(request, msgs);
176: else
177: forward.setRedirect(true);
178:
179: return forward;
180: }
181:
182: /**
183: * 添加评论
184: * @param ActionMapping mapping
185: * @param ActionForm form
186: * @param HttpServletRequest request
187: * @param HttpServletResponse response
188: * @return ActionForward
189: * @throws Exception
190: */
191: public ActionForward doAddReply(ActionMapping mapping,
192: ActionForm form, HttpServletRequest request,
193: HttpServletResponse response) throws Exception {
194: ReplyForm reply = (ReplyForm) form;
195: Session ssn = null;
196: //判断用户是否已登录
197: String loginName = request.getParameter("loginName");
198: String password = request.getParameter("password");
199: if (loginName != null && password != null) {
200: ActionErrors es = new ActionErrors();
201: if (StringUtils.isEmpty(loginName))
202: es
203: .add("updateReply", new ActionError(
204: "user_not_login"));
205: else {
206: //检查用户名口令是否正确
207: ssn = getSession();
208: UserForm user = UserManager.getUser(ssn, SiteManager
209: .getCurrentSite(request), loginName);
210: //检查用户名是否存在
211: if (user != null) {
212: //检查用户是否被暂停
213: if (user.getUserRole() == DlogRole.ROLE_GUEST)
214: es.add("updateReply", new ActionError(
215: "user_pause"));
216: //检查密码
217: else if (StringUtils.equals(user.getPassword(),
218: password)) {
219: //保存用户信息至会话
220: user.setLastTime(new Date());
221: user.setLoginCount(user.getLoginCount() + 1);
222: ssn.update(user);
223: UserManager.fillUserWithLogAndReplyCount(ssn,
224: user, false);
225: //集成web-security的权限控制
226: DlogRole role = (DlogRole) SecurityConfig
227: .getConfig().getRoleById(
228: user.getUserRole() & 31);
229: if (role == null) {
230: role = SecurityConfig.getConfig()
231: .getRoleById(DlogRole.ROLE_COMMON);
232: user.setUserRole(DlogRole.ROLE_COMMON);
233: ssn.update(user);
234: }
235: user.setRole(role);
236: //保存用户资料到会话
237: user.saveLoginUser(request);
238: //用户登录跟踪
239: LoginTrackBean ltb = new LoginTrackBean(request);
240: ssn.save(ltb);
241: commitSession(ssn, true);
242: } else
243: es.add("updateReply", new ActionError(
244: "password_error"));
245: } else
246: es.add("updateReply", new ActionError(
247: "loginName_noexits"));
248: }
249:
250: if (!es.isEmpty()) {
251: saveErrors(request, es);
252: return mapping.getInputForward();
253: }
254: }
255: UserForm loginUser = UserForm.getLoginUser(request);
256: if (loginUser != null && loginUser.isLogin())
257: try {
258: ssn = getSession();
259: SiteForm site = SiteManager.getCurrentSite(request);
260: //如果是同一个人同一个内容的就不给添加
261: String hql = "FROM "
262: + ReplyForm.class.getName()
263: + " AS r WHERE r.site.id=? AND r.author.id=? AND r.log.id=?";
264: Query q = ssn.createQuery(hql);
265: q.setInteger(0, site.getId());
266: q.setInteger(1, loginUser.getId());
267: q.setInteger(2, reply.getLogId());
268: List rs = q.list();
269: boolean bNew = true;
270: for (int i = 0; i < rs.size(); i++) {
271: ReplyForm r = (ReplyForm) rs.get(i);
272: if (r.getContent().equals(reply.getContent())) {
273: bNew = false;
274: break;
275: }
276: }
277: rs.clear();
278: if (bNew) {
279: reply.setAuthor(loginUser);
280: reply.setWriteTime(new Date());
281: reply.setSite(site);
282: ssn.save(reply);
283: int logid = reply.getLogId();
284: LogForm log = (LogForm) ssn.load(LogForm.class,
285: new Integer(logid));
286: log.setReplyCount(log.getReplyCount() + 1);
287: ssn.update(log);
288: //根据设置判断是否发送邮件提醒
289: if (1 == log.getReplyNotify()) {
290: if (StringUtils.isEmpty(log.getOwner()
291: .getEmail())) {
292: getServlet()
293: .log(
294: "***** The email of Author is empty or illegal. *****");
295: } else if (!StringUtils.equals(log.getOwner()
296: .getLoginName(), reply.getAuthor()
297: .getLoginName())) {
298: try {
299: Mailer sender = Mailer.getMailer();
300: String url = site.getUrl()
301: + "/showlog.jspe?log_id="
302: + log.getId() + "#"
303: + reply.getId();
304: String curTime = new SimpleDateFormat(
305: "yyyy-MM-dd HH:mm")
306: .format(new Date());
307: String replyUser = loginUser
308: .getDisplayName();
309: if (StringUtils.isNotEmpty(loginUser
310: .getEmail())) {
311: StringBuffer link = new StringBuffer();
312: link.append("<a href=\"mailto:");
313: link.append(loginUser.getEmail());
314: link.append("\">");
315: link.append(replyUser);
316: link.append("</a>");
317: replyUser = link.toString();
318: }
319: String[] args = {
320: log.getOwner().getDisplayName(),
321: log.getTitle(), replyUser, url,
322: curTime, reply.getContent() };
323: String template = getContentTemplate();
324: String content = MessageFormat.format(
325: template, args);
326: Parser html = Parser
327: .createParser(new String(
328: content.getBytes(),
329: "8859_1"));
330: Node[] nodes = html
331: .extractAllNodesThatAre(TitleTag.class);
332: String title = nodes[0]
333: .toPlainTextString();
334: sender.send(site.getDisplayName(),
335: new String[] { log.getOwner()
336: .getEmail() }, title,
337: content);
338: } catch (Exception e) {
339: getServlet()
340: .log(
341: "Send reply notification failed.",
342: e);
343: }
344: }
345: }
346: }
347: } finally {
348: commitSession(ssn, true);
349: reply.reset(mapping, request);
350: }
351: else {
352: return mapping.findForward("fail_to_reply");
353: }
354: StringBuffer forward = new StringBuffer();
355: forward.append(mapping.getInput());
356: forward.append("?log_id=");
357: forward.append(reply.getLogId());
358: if (reply.getLog().getCategoryId() != -1) {
359: forward.append("&cat_id=");
360: forward.append(reply.getLog().getCategoryId());
361: }
362: forward.append('#');
363: forward.append(reply.getId());
364: return new ActionForward(forward.toString(), true);
365: }
366:
367: /**
368: * 修改评论
369: * @param ActionMapping mapping
370: * @param ActionForm form
371: * @param HttpServletRequest request
372: * @param HttpServletResponse response
373: * @return ActionForward
374: * @throws Exception
375: */
376: public ActionForward doDeleteReply(ActionMapping mapping,
377: ActionForm form, HttpServletRequest request,
378: HttpServletResponse response, String reply_id)
379: throws Exception {
380: int rid = -1;
381: try {
382: rid = Integer.parseInt(reply_id);
383: } catch (Exception e) {
384: }
385: int logid = -1;
386: if (rid != -1) {
387: Session ssn = null;
388: //判断用户是否已登录
389: UserForm user = UserForm.getLoginUser(request);
390: if (user != null && user.isLogin())
391: try {
392: ssn = getSession();
393: ReplyForm old = (ReplyForm) ssn.load(
394: ReplyForm.class, new Integer(rid));
395: logid = old.getLogId();
396: if (!user.isAdmin()
397: && old.getAuthorId() != user.getId()) {
398: ActionErrors errors = new ActionErrors();
399: errors.add("updateReply", new ActionError(
400: "operation_not_allow"));
401: if (!errors.isEmpty())
402: saveErrors(request, errors);
403: } else {
404: ssn.delete(old);
405: LogForm log = (LogForm) ssn.load(LogForm.class,
406: new Integer(logid));
407: log.setReplyCount(log.getReplyCount() - 1);
408: ssn.update(log);
409: SearchProxy proxy = SearchProxy.getReplyQuery();
410: proxy.deleteIndex(new int[] { old.getId() });
411: }
412: } catch (HibernateException e) {
413: } finally {
414: commitSession(ssn, true);
415: }
416: }
417: StringBuffer url = new StringBuffer(100);
418: url.append(mapping.getInput());
419: url.append('?');
420: if (logid > 0) {
421: url.append("&log_id=");
422: url.append(logid);
423: }
424: String user_id = request.getParameter(Globals.PARAM_USER);
425: if (user_id != null) {
426: url.append("&userid=");
427: url.append(user_id);
428: }
429: String page = request.getParameter("page");
430: if (page != null) {
431: url.append("&page=");
432: url.append(page);
433: }
434: return new ActionForward(url.toString(), true);
435: }
436:
437: /**
438: * 修改评论
439: * @param ActionMapping mapping
440: * @param ActionForm form
441: * @param HttpServletRequest request
442: * @param HttpServletResponse response
443: * @return ActionForward
444: * @throws Exception
445: */
446: public ActionForward doUpdateReply(ActionMapping mapping,
447: ActionForm form, HttpServletRequest request,
448: HttpServletResponse response) throws Exception {
449: ReplyForm reply = (ReplyForm) form;
450: Session ssn = null;
451: //判断用户是否已登录
452: UserForm user = UserForm.getLoginUser(request);
453: if (user != null && user.isLogin())
454: try {
455: ssn = getSession();
456: ReplyForm old = (ReplyForm) ssn.load(ReplyForm.class,
457: new Integer(reply.getId()));
458: if (!user.isAdmin()
459: && old.getAuthorId() != user.getId()) {
460: ActionErrors errors = new ActionErrors();
461: errors.add("updateReply", new ActionError(
462: "operation_not_allow"));
463: if (!errors.isEmpty())
464: saveErrors(request, errors);
465: } else {
466: boolean needModifier = true;
467: if (old.getAuthor().getId() == user.getId()) {
468: long ct = System.currentTimeMillis();
469: long interval = (ct - old.getWriteTime()
470: .getTime()) / 3600000;
471: if (interval < 2)
472: needModifier = false;
473: }
474: if (needModifier) {
475: String append_text = getModifyAppendMessage(
476: request, reply.getShowFormerly() != 1,
477: user.getLoginName(), user
478: .getDisplayName());
479: old
480: .setContent(reply.getContent()
481: + append_text);
482: } else
483: old.setContent(reply.getContent());
484: old.setShowFormerly(reply.getShowFormerly());
485: ssn.update(old);
486: SearchProxy proxy = SearchProxy.getReplyQuery();
487: proxy.updateIndex(old);
488: }
489: } catch (HibernateException e) {
490: } finally {
491: commitSession(ssn, true);
492: }
493: else
494: return mapping.findForward("fail_to_reply");
495: StringBuffer forward = new StringBuffer();
496: forward.append(mapping.getInput());
497: forward.append("?log_id=");
498: forward.append(reply.getLogId());
499: forward.append("&cat_id=");
500: forward.append(reply.getLog().getCategoryId());
501: forward.append('#');
502: forward.append(reply.getId());
503: return new ActionForward(forward.toString(), true);
504: }
505:
506: /**
507: * 根据编辑模式决定如何处理修改戳
508: * @param req
509: * @param html
510: * @param userName
511: * @return
512: */
513: protected String getModifyAppendMessage(HttpServletRequest req,
514: boolean html, String loginName, String userName) {
515: Object[] args = new String[] {
516: Globals.FORMAT_DT.format(new Date()), loginName,
517: userName };
518: String append_text = getResources(req).getMessage(
519: req.getLocale(), MODIFY_APPEND_FORMAT, args);
520: if (!html) {
521: try {
522: Parser p = Parser.createParser(new String(append_text
523: .getBytes(), "8859_1"));
524: NodeIterator nodes = p.elements();
525: while (nodes.hasMoreNodes()) {
526: Node node = nodes.nextNode();
527: append_text = node.toPlainTextString().trim();
528: append_text = "\r\n\r\n"
529: + new String(StringUtils.replace(
530: append_text, " ", " ")
531: .getBytes("8859_1"));
532: break;
533: }
534: } catch (Exception e) {
535: }
536: }
537: return append_text;
538: }
539:
540: /**
541: * 日志删除
542: * @param ActionMapping mapping
543: * @param ActionForm form
544: * @param HttpServletRequest request
545: * @param HttpServletResponse response
546: * @return ActionForward
547: * @throws Exception
548: */
549: public ActionForward doDeleteLog(ActionMapping mapping,
550: ActionForm form, HttpServletRequest request,
551: HttpServletResponse response) throws Exception {
552:
553: String forward = "home";
554: LogForm log = (LogForm) form;
555: //判断用户是否已登录
556: UserForm user = UserForm.getLoginUser(request);
557: if (user != null && user.isLogin()) {
558: Session ssn = getSession();
559: LogForm old = (LogForm) ssn.load(LogForm.class,
560: new Integer(log.getId()));
561: if (old != null)
562: try {
563: old.setStatus(LogForm.STATUS_DELETED);
564: old.setDeleteTime(new Date());
565: ssn.update(old);
566: SearchProxy proxy = SearchProxy.getLogQuery();
567: proxy.deleteIndex(new int[] { old.getId() });
568: } finally {
569: commitSession(ssn, true);
570: }
571: }
572: return new ActionForward(mapping.findForward(forward).getPath()
573: + "?cat_id=" + log.getCategoryId(), true);
574: }
575:
576: /**
577: * 删除草稿
578: * @param ActionMapping mapping
579: * @param ActionForm form
580: * @param HttpServletRequest request
581: * @param HttpServletResponse response
582: * @return ActionForward
583: * @throws Exception
584: */
585: public ActionForward doDeleteDraft(ActionMapping mapping,
586: ActionForm form, HttpServletRequest request,
587: HttpServletResponse response) throws Exception {
588:
589: String forward = "draft";
590: LogForm log = (LogForm) form;
591: //判断用户是否已登录
592: UserForm user = UserForm.getLoginUser(request);
593:
594: if (user != null && user.isLogin()
595: && (user.isAdmin() || user.isFriend())) {
596: Session ssn = null;
597: try {
598: ssn = getSession();
599: int draft_id = Integer.parseInt(request
600: .getParameter(PARAM_DRAFT));
601: DraftForm draft = (DraftForm) ssn.load(DraftForm.class,
602: new Integer(draft_id));
603: ssn.delete(draft);
604: } finally {
605: commitSession(ssn, true);
606: }
607: } else
608: forward = "home";
609: return mapping.findForward(forward);
610: }
611:
612: /**
613: * 更新草稿
614: * @param ActionMapping mapping
615: * @param ActionForm form
616: * @param HttpServletRequest request
617: * @param HttpServletResponse response
618: * @return ActionForward
619: * @throws Exception
620: */
621: public ActionForward doUpdateDraft(ActionMapping mapping,
622: ActionForm form, HttpServletRequest request,
623: HttpServletResponse response) throws Exception {
624:
625: String forward = "draft";
626: LogForm log = (LogForm) form;
627: //判断用户是否已登录
628: UserForm user = UserForm.getLoginUser(request);
629: if (user == null || !user.isLogin())
630: forward = "home";
631: else if (user.isAdmin() || user.isFriend()) {
632: Session ssn = null;
633: try {
634: ssn = getSession();
635: int draft_id = Integer.parseInt(request
636: .getParameter(PARAM_DRAFT));
637: DraftForm draft = (DraftForm) ssn.load(DraftForm.class,
638: new Integer(draft_id));
639: if (!user.isAdmin()
640: && draft.getOwner().getId() != user.getId()) {
641: ActionErrors errors = new ActionErrors();
642: errors.add("updateDraft", new ActionError(
643: "operation_not_allow"));
644: if (!errors.isEmpty())
645: saveErrors(request, errors);
646: } else {
647: draft.setAuthor(log.getAuthor());
648: draft.setAuthorUrl(log.getAuthorUrl());
649: draft.setContent(log.getContent());
650: draft.setLogTime(new Date());
651: draft.setMoodLevel(log.getMoodLevel());
652: draft.setOwner(user);
653: draft.setRefUrl(log.getRefUrl());
654: draft.setShowFormerly(log.getShowFormerly());
655: draft.setSite(SiteManager.getCurrentSite(request));
656: draft.setTitle(log.getTitle());
657: draft.setUseFace(log.getUseFace());
658: draft.setUseUbb(log.getUseUbb());
659: draft.setWeather(log.getWeather());
660: ssn.update(draft);
661: }
662: } finally {
663: commitSession(ssn, true);
664: }
665: } else
666: forward = "home";
667: return mapping.findForward(forward);
668: }
669:
670: /**
671: * 日志保存为草稿
672: * @param ActionMapping mapping
673: * @param ActionForm form
674: * @param HttpServletRequest request
675: * @param HttpServletResponse response
676: * @return ActionForward
677: * @throws Exception
678: */
679: public ActionForward doSaveDraft(ActionMapping mapping,
680: ActionForm form, HttpServletRequest request,
681: HttpServletResponse response) throws Exception {
682:
683: String forward = "draft";
684: LogForm log = (LogForm) form;
685: //判断用户是否已登录
686: UserForm user = UserForm.getLoginUser(request);
687:
688: if (user != null && user.isLogin()
689: && (user.isAdmin() || user.isFriend())) {
690: Session ssn = null;
691: DraftForm draft = new DraftForm();
692: draft.setAuthor(log.getAuthor());
693: draft.setAuthorUrl(log.getAuthorUrl());
694: draft.setContent(log.getContent());
695: draft.setLogTime(new Date());
696: draft.setMoodLevel(log.getMoodLevel());
697: draft.setOwner(user);
698: draft.setRefUrl(log.getRefUrl());
699: draft.setShowFormerly(log.getShowFormerly());
700: draft.setSite(SiteManager.getCurrentSite(request));
701: draft.setTitle(log.getTitle());
702: draft.setUseFace(log.getUseFace());
703: draft.setUseUbb(log.getUseUbb());
704: draft.setWeather(log.getWeather());
705: try {
706: ssn = getSession();
707: ssn.save(draft);
708: } finally {
709: commitSession(ssn, true);
710: }
711: } else {
712: forward = "fail_to_save";
713: }
714: return mapping.findForward(forward);
715: }
716:
717: /**
718: * 日志修改
719: * @param ActionMapping mapping
720: * @param ActionForm form
721: * @param HttpServletRequest request
722: * @param HttpServletResponse response
723: * @return ActionForward
724: * @throws Exception
725: */
726: public ActionForward doEditLog(ActionMapping mapping,
727: ActionForm form, HttpServletRequest request,
728: HttpServletResponse response) throws Exception {
729:
730: String forward = "showlog";
731: LogForm log = (LogForm) form;
732: //判断用户是否已登录
733: UserForm user = UserForm.getLoginUser(request);
734:
735: if (user != null && user.isLogin()) {
736: Session ssn = getSession();
737: LogForm old = (LogForm) ssn.load(LogForm.class,
738: new Integer(log.getId()));
739: old.setTitle(log.getTitle());
740: boolean needModifier = true;
741: if (old.getOwner().getId() == user.getId()) {
742: long ct = System.currentTimeMillis();
743: long interval = (ct - old.getLogTime().getTime()) / 3600000;
744: if (interval < 6)
745: needModifier = false;
746: }
747: if (needModifier) {
748: String append_text = getModifyAppendMessage(request,
749: log.getShowFormerly() != 1,
750: user.getLoginName(), user.getDisplayName());
751: old.setContent(log.getContent() + append_text);
752: } else
753: old.setContent(log.getContent());
754: old.setAuthor(log.getAuthor());
755: old.setAuthorUrl(log.getAuthorUrl());
756: old.setMoodLevel(log.getMoodLevel());
757: old.setRefUrl(log.getRefUrl());
758: old.setWeather(log.getWeather());
759: old.setUseUbb(log.getUseUbb());
760: old.setUseFace(log.getUseFace());
761: old.setShowFormerly(log.getShowFormerly());
762: old.setSearchKey(log.getSearchKey());
763: old.setReplyNotify(log.getReplyNotify());
764: if (log.getCategoryId() != old.getCategoryId()) {
765: CategoryForm cat = (CategoryForm) ssn.load(
766: CategoryForm.class, new Integer(log
767: .getCategoryId()));
768: old.setCategory(cat);
769: }
770:
771: try {
772: ssn.update(old);
773: SearchProxy proxy = SearchProxy.getLogQuery();
774: proxy.updateIndex(old);
775: } finally {
776: commitSession(ssn, true);
777: }
778: } else {
779: return mapping.findForward("fail_to_save");
780: }
781: return new ActionForward(mapping.findForward(forward).getPath()
782: + "?log_id=" + log.getId(), true);
783: }
784:
785: /**
786: * 日志添加
787: * @param ActionMapping mapping
788: * @param ActionForm form
789: * @param HttpServletRequest request
790: * @param HttpServletResponse response
791: * @return ActionForward
792: * @throws Exception
793: */
794: public ActionForward doAddLog(ActionMapping mapping,
795: ActionForm form, HttpServletRequest request,
796: HttpServletResponse response) throws Exception {
797:
798: String forward = "home";
799: LogForm log = (LogForm) form;
800: //判断用户是否已登录
801: UserForm user = UserForm.getLoginUser(request);
802:
803: if (user != null && user.isLogin()) {
804: log.setOwner(user);
805: if (log.getLogTime() == null)
806: log.setLogTime(new Date());
807: Session ssn = getSession();
808: try {
809: SiteForm site = SiteManager.getCurrentSite(request);
810: log.setSite(site);
811: ssn.save(log);
812: int draftId = -1;
813: try {
814: draftId = Integer.parseInt(request
815: .getParameter(PARAM_DRAFT));
816: } catch (Exception e) {
817: }
818: if (draftId != -1) {
819: DraftForm draft = (DraftForm) ssn.load(
820: DraftForm.class, new Integer(draftId));
821: if (draft != null)
822: ssn.delete(draft);
823: }
824: //判断是否添加书签
825: if ("1".equals(request.getParameter("bookmark"))) {
826: BookMarkBean bmb = new BookMarkBean(log.getSite(),
827: user, log);
828: bmb.setCreateTime(new Date());
829: bmb.setType(BookMarkBean.BM_LOG);
830: ssn.save(bmb);
831: }
832: if (StringUtils.isNotEmpty(log.getRefUrl())) {
833: //启动TrackBack线程
834: final LogForm logForm = log;
835: final SiteForm siteForm = site;
836: final String log_url = RequestUtils
837: .getBaseURL(request);
838: final Action action = this ;
839: new Thread() {
840: public void run() {
841: try {
842: TrackBackResp resp = BlogTrackBack
843: .track(
844: logForm.getRefUrl(),
845: log_url
846: + "/showlog.jspe?log_id="
847: + logForm
848: .getId(),
849: siteForm
850: .getDisplayName(),
851: logForm.getTitle(), "");
852: } catch (Exception e) {
853: action.getServlet().log(
854: "TrackBack Failed. url="
855: + logForm.getRefUrl(),
856: e);
857: }
858: }
859: }.start();
860: }
861: } finally {
862: commitSession(ssn, true);
863: }
864: } else {
865: //用户在操作时会话已经超时,显示刚才添加的信息给用户避免丢失。
866: return mapping.findForward("fail_to_save");
867: }
868: return new ActionForward(mapping.findForward(forward).getPath()
869: + "?cat_id=" + log.getCategoryId(), true);
870: }
871:
872: /**
873: * 得到一个唯一的文件名
874: * @param extName
875: * @return
876: */
877: protected String getUniqueFileName(String extName) {
878: final SimpleDateFormat sdf = new SimpleDateFormat(
879: "yyyyMMddHHmmssSSSS.");
880: String fn = null;
881: do {
882: fn = sdf.format(new Date()) + extName;
883: if (new File(uploadDir + fn).exists())
884: continue;
885: break;
886: } while (true);
887: return fn;
888: }
889:
890: /**
891: * 获取文件的扩展名
892: * @param file
893: * @return
894: */
895: protected static String getFileExtendName(String file) {
896: int idx = file.lastIndexOf('.');
897: return (idx == -1 || idx == (file.length() - 1)) ? "" : file
898: .substring(idx + 1).toLowerCase();
899: }
900:
901: /**
902: * 获取上传文件保存的目录全路径
903: * @return
904: */
905: protected String getUploadDir() {
906: String path = servlet.getServletContext().getInitParameter(
907: "uploadDir");
908: if (path == null)
909: path = "uploads";
910: String webpath = servlet.getServletContext().getRealPath(path);
911: if (webpath.endsWith(File.separator))
912: webpath += File.separator;
913: return webpath;
914: }
915:
916: public static void main(String[] args) {
917: //System.out.println("EXTEND:"+getUniqueFileName("jpg"));
918: }
919:
920: /**
921: * 获得忘记密码提示内容的模板
922: * @return
923: * @throws IOException
924: */
925: protected String getContentTemplate() throws IOException {
926: ServletContext sc = getServlet().getServletContext();
927: InputStream in = sc
928: .getResourceAsStream("/WEB-INF/template/reply_tip.html");
929: StringBuffer template = new StringBuffer(512);
930: BufferedReader reader = null;
931: try {
932: reader = new BufferedReader(new InputStreamReader(in));
933: do {
934: String line = reader.readLine();
935: if (line == null)
936: break;
937: template.append(line);
938: template.append("\r\n");
939: } while (true);
940: } finally {
941: in.close();
942: }
943: return template.toString();
944: }
945: }
|