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.FileInputStream;
019: import java.io.FileOutputStream;
020: import java.io.IOException;
021: import java.sql.SQLException;
022:
023: import javax.servlet.ServletContext;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import net.sf.hibernate.HibernateException;
028: import net.sf.hibernate.Session;
029:
030: import org.apache.struts.action.ActionError;
031: import org.apache.struts.action.ActionErrors;
032: import org.apache.struts.action.ActionForm;
033: import org.apache.struts.action.ActionForward;
034: import org.apache.struts.action.ActionMapping;
035:
036: import dlog4j.formbean.SiteForm;
037: import dlog4j.formbean.TemplateForm;
038:
039: /**
040: * 用于处理个人DLOG的Action类
041: * @author Liudong
042: */
043: public class DlogSiteAction extends AdminActionBase {
044:
045: public final static String ERROR_KEY = "site";
046:
047: /**
048: * 修改BLOG的模板信息
049: * @param mapping
050: * @param form
051: * @param request
052: * @param response
053: * @return
054: * @throws Exception
055: */
056: public ActionForward doUpdateTemplate(ActionMapping mapping,
057: ActionForm form, HttpServletRequest request,
058: HttpServletResponse response) throws Exception {
059: ActionErrors errors = new ActionErrors();
060: TemplateForm temp = (TemplateForm) form;
061: String path = "/WEB-INF/template/" + temp.getFile();
062: String realPath = getServlet().getServletContext().getRealPath(
063: path);
064: update(realPath, temp.getContent());
065: return mapping.getInputForward();
066: }
067:
068: /**
069: * 修改个人DLOG的基本信息
070: * @param ActionMapping mapping
071: * @param ActionForm form
072: * @param HttpServletRequest request
073: * @param HttpServletResponse response
074: * @return ActionForward
075: * @throws Exception
076: */
077: public ActionForward doEditSite(ActionMapping mapping,
078: ActionForm form, HttpServletRequest request,
079: HttpServletResponse response) throws Exception {
080:
081: ActionErrors errors = new ActionErrors();
082: SiteForm site = (SiteForm) form;
083: Session ssn = null;
084: try {
085: ssn = getSession();
086: SiteForm old = (SiteForm) ssn.load(SiteForm.class,
087: new Integer(site.getId()));
088: old.setDisplayName(site.getDisplayName());
089: old.setDetail(site.getDetail());
090: old.setCss(site.getCss());
091: old.setIcon(site.getIcon());
092: old.setLogo(site.getLogo());
093: old.setUrl(site.getUrl());
094: ssn.update(old);
095: commitSession(ssn, false);
096: //更新布局模板
097: ServletContext context = getServlet().getServletContext();
098: String path = context
099: .getRealPath("/WEB-INF/jsp/layout/html_layout.htm");
100: update(path, request.getParameter("layout"));
101: //更新会话中的信息
102: request.getSession().setAttribute(SiteForm.class.getName(),
103: old);
104: } catch (SQLException e) {
105: errors
106: .add(ERROR_KEY, new ActionError(
107: "database_exception"));
108: } catch (HibernateException e) {
109: errors.add(ERROR_KEY,
110: new ActionError("hibernate_exception"));
111: } finally {
112: close(ssn);
113: }
114: ActionForward forward = mapping.getInputForward();
115: forward.setRedirect(true);
116: return forward;
117: }
118:
119: /**
120: * 更新网站布局模板
121: * @param path 模板文件路径
122: * @param newLayout 新模板
123: * @return 如果模板改变则返回true,否则返回false
124: * @throws IOException
125: */
126: protected boolean update(String path, String newLayout)
127: throws IOException {
128: boolean updated = false;
129: FileInputStream in = null;
130: FileOutputStream fos = null;
131: try {
132: byte[] bs = new byte[512];
133: in = new FileInputStream(path);
134: StringBuffer layout = new StringBuffer();
135: do {
136: int rc = in.read(bs);
137: if (rc == -1)
138: break;
139: layout.append(new String(bs, 0, rc));
140: if (rc < bs.length)
141: break;
142: } while (true);
143: in.close();
144: in = null;
145: if (!layout.toString().equals(newLayout)) {
146: fos = new FileOutputStream(path);
147: fos.write(newLayout.getBytes());
148: }
149: } finally {
150: if (fos != null)
151: fos.close();
152: if (in != null)
153: in.close();
154: }
155: return updated;
156: }
157: }
|