KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > wysiwyg > action > EditTextAction


1 package com.dotmarketing.portlets.wysiwyg.action;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.portlet.ActionRequest;
6 import javax.portlet.ActionResponse;
7 import javax.portlet.PortletConfig;
8 import javax.servlet.jsp.PageContext JavaDoc;
9
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionMapping;
12
13 import com.dotmarketing.factories.InodeFactory;
14 import com.dotmarketing.portlets.files.factories.FileFactory;
15 import com.dotmarketing.portlets.files.model.File;
16 import com.dotmarketing.util.Logger;
17 import com.dotmarketing.util.UtilMethods;
18 import com.dotmarketing.util.WebKeys;
19 import com.liferay.portal.struts.PortletAction;
20 import com.liferay.portal.util.Constants;
21 import com.liferay.util.servlet.SessionMessages;
22 /**
23  * @author Maria
24  */

25
26 public class EditTextAction extends PortletAction {
27
28     public void processAction(
29             ActionMapping mapping, ActionForm form, PortletConfig config,
30             ActionRequest req, ActionResponse res)
31         throws Exception JavaDoc {
32     
33         String JavaDoc cmd = req.getParameter(Constants.CMD);
34         Logger.debug(this, "Inside EditTextAction cmd=" + cmd);
35
36         try {
37             Logger.debug(this, "Going to run SaveTextAction");
38             _retrieveTextAction(req, res, config, form);
39         }
40         catch (Exception JavaDoc ae) {
41             Logger.error(this, ae.toString(), ae);
42             req.setAttribute(PageContext.EXCEPTION, ae);
43             setForward(req, Constants.COMMON_ERROR);
44         }
45
46         if ((cmd != null) && cmd.equals(Constants.SAVE)) {
47             try {
48                 Logger.debug(this, "Going to run SaveTextAction");
49                 _saveTextAction(req, res, config, form);
50                 setForward(req, "portlet.ext.wysiwyg.window_close");
51             }
52             catch (Exception JavaDoc ae) {
53                 Logger.error(this, ae.toString(), ae);
54                 req.setAttribute(PageContext.EXCEPTION, ae);
55                 setForward(req, Constants.COMMON_ERROR);
56             }
57         }
58         else {
59             setForward(req, "portlet.ext.wysiwyg.edit_text");
60         }
61     }
62     private void _saveTextAction(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form)
63     throws Exception JavaDoc {
64         
65         File file = (File) InodeFactory.getInode(req.getParameter("inode"), File.class);
66         
67         Logger.debug(this, "I'm inside SaveTextAction");
68         //gets file extension
69
String JavaDoc fileExtension = com.dotmarketing.util.UtilMethods.getFileExtension(file.getFileName());
70
71         //gets the real path to the assets directory
72
String JavaDoc filePath = FileFactory.getRealAssetsRootPath();
73
74         //creates the path where to save this file based on the inode
75
String JavaDoc fileInodePath = String.valueOf(file.getInode());
76         if (fileInodePath.length()==1) {
77             fileInodePath = fileInodePath + "0";
78         }
79         try{
80
81             //creates the path with inode{1} + inode{2}
82
fileInodePath = fileInodePath.substring(0,1) + java.io.File.separator + fileInodePath.substring(1,2);
83     
84             //creates the current file as inode{1}/inode{2}/inode.file_extension
85
java.io.File JavaDoc currF = new java.io.File JavaDoc(filePath + java.io.File.separator + fileInodePath + java.io.File.separator + file.getInode() + "." + fileExtension);
86     
87             //gets outputstream from the file
88
java.io.FileOutputStream JavaDoc fout = new java.io.FileOutputStream JavaDoc(currF);
89             //gets buffered outputstrem to write the data in the file
90
java.io.BufferedOutputStream JavaDoc bout = new java.io.BufferedOutputStream JavaDoc(fout);
91
92             String JavaDoc text = req.getParameter("text");
93             bout.write(text.getBytes());
94             bout.flush();
95             bout.close();
96
97             SessionMessages.add(req, "message", "message.file.text.save");
98
99         }
100         catch (IOException JavaDoc e) {
101             Logger.error(this, "Error opening file in SaveTextAction", e);
102         }
103
104     }
105     private void _retrieveTextAction(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form)
106     throws Exception JavaDoc {
107
108         File file = (File) InodeFactory.getInode(req.getParameter("inode"), File.class);
109         
110         //gets file extension
111
String JavaDoc fileExtension = com.dotmarketing.util.UtilMethods.getFileExtension(file.getFileName());
112
113         //gets the real path to the assets directory
114
String JavaDoc filePath = FileFactory.getRealAssetsRootPath();
115
116         //creates the path where to save this file based on the inode
117
String JavaDoc fileInodePath = String.valueOf(file.getInode());
118         if (fileInodePath.length()==1) {
119             fileInodePath = fileInodePath + "0";
120         }
121         try{
122             //creates the path with inode{1} + inode{2}
123
fileInodePath = fileInodePath.substring(0,1) + java.io.File.separator + fileInodePath.substring(1,2);
124     
125             //creates the current file as inode{1}/inode{2}/inode.file_extension
126
java.io.File JavaDoc currF = new java.io.File JavaDoc(filePath + java.io.File.separator + fileInodePath + java.io.File.separator + file.getInode() + "." + fileExtension);
127     
128             Logger.debug(this, "Opening file: " + filePath + java.io.File.separator + fileInodePath + java.io.File.separator + file.getInode() + "." + fileExtension);
129
130             //gets input stream from the data from the uploaded file
131
java.io.FileInputStream JavaDoc is = new java.io.FileInputStream JavaDoc(currF);
132             //gets bufferedinput stream to read data
133
java.io.BufferedInputStream JavaDoc bin = new java.io.BufferedInputStream JavaDoc(is);
134     
135             StringBuffer JavaDoc strBuffer = new StringBuffer JavaDoc();
136             
137             byte[] strText = new byte[bin.available()];
138             
139             int result = bin.read(strText);
140
141             String JavaDoc strFinal = new String JavaDoc(strText);
142
143             req.setAttribute(WebKeys.WYSIWYG_EDIT_TEXT, UtilMethods.escapeHTMLSpecialChars(strFinal));
144         }
145         catch (IOException JavaDoc e) {
146             Logger.error(this, "error opening file in EditTextAction", e);
147         }
148     }
149
150 }
151
Popular Tags