KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > communicator > applications > webtalk > controller > RenameFileAction


1 /*
2  * RenameFileAction.java
3  *
4  * Created on June 14, 2003, 9:47 PM
5  */

6
7 package com.quikj.application.communicator.applications.webtalk.controller;
8 import java.io.*;
9 import java.sql.*;
10 import java.util.*;
11 import javax.servlet.*;
12 import javax.servlet.http.*;
13 import org.apache.struts.action.*;
14 import com.quikj.application.communicator.admin.model.*;
15 import com.quikj.application.communicator.admin.controller.*;
16
17 /**
18  *
19  * @author Vinod Batra
20  */

21 public final class RenameFileAction extends Action{
22     
23     /** Creates a new instance of RenameFileAction */
24     public RenameFileAction() {
25     }
26     public ActionForward execute(ActionMapping mapping,
27     ActionForm form,
28     HttpServletRequest request,
29     HttpServletResponse response)
30     throws IOException, ServletException {
31         
32         Locale locale = getLocale(request);
33         ActionErrors errors = new ActionErrors();
34         
35         // first check if the user has a connection
36
Connection c = (Connection)request.getSession().getAttribute("connection");
37         if (c == null) {
38             errors.add(ActionErrors.GLOBAL_ERROR,
39             new ActionError("error.not.logged.in"));
40             saveErrors(request, errors);
41             return mapping.findForward("logon");
42         }
43         
44         AccountElement element = (AccountElement)request.getSession().getAttribute("userInfo");
45         if ((element.isAdminLevel() == false) && (element.isCustomerLevel() == false))
46         {
47             errors.add(ActionErrors.GLOBAL_ERROR,
48             new ActionError("error.insufficient.privilege"));
49             saveErrors(request, errors);
50             
51             return mapping.findForward("main_menu");
52         }
53         
54         RenameFileForm renameForm = (RenameFileForm) form;
55         
56         String JavaDoc name = renameForm.getFileName();
57         String JavaDoc rename= renameForm.getRenameTo();
58         
59         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(request.getSession().getServletContext().getRealPath(""));
60         
61         buffer.append(File.separator);
62         buffer.append(UploadFileAction.APP_PATH);
63         
64         buffer.append(File.separator);
65         buffer.append(UploadFileAction.DATA_PATH);
66         
67         buffer.append(File.separator);
68         buffer.append(UploadFileAction.HTML_PATH);
69         
70         buffer.append(File.separator);
71         buffer.append(element.getDomain());
72         
73         File directory = new File(buffer.toString());
74         
75         File file = new File(directory, name);
76         if (!file.exists()) {
77             errors.add("fileName", new ActionError("file.no.exists"));
78             saveErrors(request, errors);
79             return (new ActionForward(mapping.getInput()));
80             
81         }
82         
83         if (!file.canWrite()) {
84             errors.add("fileName", new ActionError("file.no.permission.write"));
85             saveErrors(request, errors);
86             return (new ActionForward(mapping.getInput()));
87         }
88         
89         boolean result = file.renameTo(new File(directory,rename));
90         if (result == false) {
91             errors.add("fileName", new ActionError("file.no.rename"));
92             saveErrors(request, errors);
93             return (new ActionForward(mapping.getInput()));
94             
95         }
96         
97         String JavaDoc fileSuccess = "renaming";
98         request.getSession().setAttribute("fileSuccess", fileSuccess);
99         
100         WebTalkRelatedTasks menu = new WebTalkRelatedTasks();
101         menu.addLink(new LinkAttribute("Upload File", "upload_file_input"));
102         menu.addLink(new LinkAttribute("Delete File", "delete_file_input"));
103         return (mapping.findForward("file_success"));
104         
105     }
106 }
Popular Tags