| 1 16 package dlog4j.action; 17 18 import java.io.FileInputStream ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.sql.SQLException ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 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 44 public class DlogSiteAction extends AdminActionBase { 45 46 public final static String ERROR_KEY = "site"; 47 48 49 58 public ActionForward doUpdateTemplate( 59 ActionMapping mapping, 60 ActionForm form, 61 HttpServletRequest request, 62 HttpServletResponse response) 63 throws Exception { 64 ActionErrors errors = new ActionErrors(); 65 TemplateForm temp = (TemplateForm)form; 66 String path = "/WEB-INF/template/" + temp.getFile(); 67 String realPath = getServlet().getServletContext().getRealPath(path); 68 update(realPath, temp.getContent()); 69 return mapping.getInputForward(); 70 } 71 72 81 public ActionForward doEditSite( 82 ActionMapping mapping, 83 ActionForm form, 84 HttpServletRequest request, 85 HttpServletResponse response) 86 throws Exception { 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 (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 ServletContext context = getServlet().getServletContext(); 104 String path = context.getRealPath("/WEB-INF/jsp/layout/html_layout.htm"); 105 update(path, request.getParameter("layout")); 106 request.getSession().setAttribute(SiteForm.class.getName(),old); 108 } catch(SQLException 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 127 protected boolean update(String path, String newLayout) 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 } 158 | Popular Tags |