KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > action > DlogSiteAction


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.action;
17
18 import java.io.FileInputStream JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27
28 import net.sf.hibernate.HibernateException;
29 import net.sf.hibernate.Session;
30
31 import org.apache.struts.action.ActionError;
32 import org.apache.struts.action.ActionErrors;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36
37 import dlog4j.formbean.SiteForm;
38 import dlog4j.formbean.TemplateForm;
39
40 /**
41  * 用于处理个人DLOG的Action类
42  * @author Liudong
43  */

44 public class DlogSiteAction extends AdminActionBase {
45
46     public final static String JavaDoc ERROR_KEY = "site";
47     
48
49     /**
50      * 修改BLOG的模板信息
51      * @param mapping
52      * @param form
53      * @param request
54      * @param response
55      * @return
56      * @throws Exception
57      */

58     public ActionForward doUpdateTemplate(
59         ActionMapping mapping,
60         ActionForm form,
61         HttpServletRequest JavaDoc request,
62         HttpServletResponse JavaDoc response)
63         throws Exception JavaDoc {
64         ActionErrors errors = new ActionErrors();
65         TemplateForm temp = (TemplateForm)form;
66         String JavaDoc path = "/WEB-INF/template/" + temp.getFile();
67         String JavaDoc realPath = getServlet().getServletContext().getRealPath(path);
68         update(realPath, temp.getContent());
69         return mapping.getInputForward();
70     }
71     
72     /**
73      * 修改个人DLOG的基本信息
74      * @param ActionMapping mapping
75      * @param ActionForm form
76      * @param HttpServletRequest request
77      * @param HttpServletResponse response
78      * @return ActionForward
79      * @throws Exception
80      */

81     public ActionForward doEditSite(
82         ActionMapping mapping,
83         ActionForm form,
84         HttpServletRequest JavaDoc request,
85         HttpServletResponse JavaDoc response)
86         throws Exception JavaDoc {
87         
88         ActionErrors errors = new ActionErrors();
89         SiteForm site = (SiteForm)form;
90         Session ssn = null;
91         try {
92             ssn = getSession();
93             SiteForm old = (SiteForm)ssn.load(SiteForm.class,new Integer JavaDoc(site.getId()));
94             old.setDisplayName(site.getDisplayName());
95             old.setDetail(site.getDetail());
96             old.setCss(site.getCss());
97             old.setIcon(site.getIcon());
98             old.setLogo(site.getLogo());
99             old.setUrl(site.getUrl());
100             ssn.update(old);
101             commitSession(ssn,false);
102             //更新布局模板
103
ServletContext JavaDoc context = getServlet().getServletContext();
104             String JavaDoc path = context.getRealPath("/WEB-INF/jsp/layout/html_layout.htm");
105             update(path, request.getParameter("layout"));
106             //更新会话中的信息
107
request.getSession().setAttribute(SiteForm.class.getName(),old);
108         } catch(SQLException JavaDoc e) {
109             errors.add(ERROR_KEY,new ActionError("database_exception"));
110         } catch(HibernateException e) {
111             errors.add(ERROR_KEY,new ActionError("hibernate_exception"));
112         }finally {
113             close(ssn);
114         }
115         ActionForward forward = mapping.getInputForward();
116         forward.setRedirect(true);
117         return forward;
118     }
119
120     /**
121      * 更新网站布局模板
122      * @param path 模板文件路径
123      * @param newLayout 新模板
124      * @return 如果模板改变则返回true,否则返回false
125      * @throws IOException
126      */

127     protected boolean update(String JavaDoc path, String JavaDoc newLayout) throws IOException JavaDoc{
128         boolean updated = false;
129         FileInputStream JavaDoc in = null;
130         FileOutputStream JavaDoc fos = null;
131         try{
132             byte[] bs = new byte[512];
133             in = new FileInputStream JavaDoc(path);
134             StringBuffer JavaDoc layout = new StringBuffer JavaDoc();
135             do{
136                 int rc = in.read(bs);
137                 if(rc==-1)
138                     break;
139                 layout.append(new String JavaDoc(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 JavaDoc(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 }
158
Popular Tags