KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > content > eventhandler > CEExtModifyHandler


1 package de.webman.content.eventhandler;
2
3 import com.teamkonzept.web.*;
4 import com.teamkonzept.webman.*;
5 import com.teamkonzept.webman.db.TKWebmanDBManager;
6 import com.teamkonzept.webman.mainint.*;
7 import com.teamkonzept.webman.mainint.db.*;
8 import com.teamkonzept.webman.mainint.db.queries.*;
9 import com.teamkonzept.webman.mainint.events.*;
10 import com.teamkonzept.lib.*;
11 import com.teamkonzept.field.*;
12 import com.teamkonzept.field.db.*;
13 import com.teamkonzept.db.*;
14 import com.teamkonzept.publishing.markups.*;
15 import org.apache.log4j.Category;
16 import de.webman.acl.Policy;
17 import java.sql.*;
18 import java.io.*;
19
20 /**
21  * Modifes either a new or an existing content.
22  *
23  * @author $Author: alex $
24  * @version $Revision: 1.14.2.1 $
25  */

26 public class CEExtModifyHandler
27     extends DefaultEventHandler
28     implements ParameterTypes,
29                FrameConstants,
30                DatabaseDefaults,
31                QueryConstants
32 {
33
34     /** for Logging */
35     // private static Category cat = Category.getInstance(CEExtModifyHandler.class);
36

37     /** Action command */
38     static final String JavaDoc DELETE = "DELETE";
39
40     static final String JavaDoc EXT_MODIFY = "EXT_MODIFY";
41     static final String JavaDoc TARGET = "TARGET";
42     /** Action command */
43     static final String JavaDoc UPLOAD = "UPLOAD";
44
45     static final String JavaDoc CE_EXT_MODIFY = "CE_EXT_MODIFY";
46
47     /**
48      * The singleton instance.
49      */

50     private static final CEExtModifyHandler INSTANCE = new CEExtModifyHandler();
51
52     /**
53      * Prevents outside instantiation.
54      */

55     private CEExtModifyHandler ()
56     {
57         // NOP
58
}
59
60     /**
61      * Returns the singleton instance of this handler.
62      */

63     public static CEExtModifyHandler getInstance ()
64     {
65         return INSTANCE;
66     }
67
68     /**
69      * Handles the specified event.
70      *
71      * @param evt the event to be handled.
72      * @throws TKException if any error occurred during event handling.
73      */

74     public void handleEvent (TKEvent evt)
75         throws TKException
76     {
77         try
78         {
79             CEUtils.checkEvent(evt);
80             TKHashtable params = evt.getParams().getClass( PARAMETER );
81
82             TKHTMLTemplate t;// = evt.getPrepHTMLTemplate( "ce_upload.tmpl" );
83

84             ContentContext ceContext = new ContentContext (evt.getParams(), evt.getName());
85             TKFormDBData data = new TKFormDBData( ceContext.formId.intValue() );
86             TKFormDBInterface.Get( data );
87             TKBaseField field = TKFieldRegistry.getFieldFromDB( data );
88
89             String JavaDoc action = evt.getParameter( ACTION, EXT_MODIFY );
90             String JavaDoc target = evt.getParameter( TARGET, EXT_MODIFY );
91             //---- ACTION: SHOWFILE ----//
92
if( target == null || target.equals("") )
93                 target = evt.getParameter( PARAMETER, TARGET ) ;
94
95
96             TKUploadField extField = (TKUploadField) field.getTarget( target, "");
97
98
99             if(action != null && action.equals(UPLOAD)){
100
101                 t = evt.getPrepHTMLTemplate( "ce_fields.tmpl" );
102                 // Content auf gešndert setzten
103
t.set("CHANGED", new Integer JavaDoc(1));
104                 TKUploadFileInputStream fileInputStr = ((TKUploadFileInputStream)params.get( TKUploadField.GETFILENAMESTREAM ));
105                 File tmpFile = fileInputStr.file;
106                 /* TKUploadField eventually modifies NEW_FILENAME */
107                 String JavaDoc filename = evt.getParameter( PARAMETER,TKUploadField.NEW_FILENAME);
108                 if(filename == null || filename.equals("")){
109                     filename = extField.splitBrowserFileName(fileInputStr.getUploadFileName());
110                 }
111                 filename = TKUploadField.checkFileName(filename);
112                 TKHashtable header = fileInputStr.getUploadHeader();
113                 String JavaDoc contentType = TKUploadField.fixContentType(header.get(TKUploadField.CONTENT_TYPE).toString());
114
115                 // Mimetype checken !
116
if (! extField.compareMimes(contentType))
117                 {
118                     Object JavaDoc[] args = {contentType};
119                     throw new TKUserException("Mime Typen matchen nicht", NOT_MATCHING_MIMETYPES, USER_SEVERITY, false, args, null);
120                 }
121
122                 if (extField.isSaveInDB())
123                 {
124                     // save in DB
125
int newMediaID = MediaManager.storeMedia(tmpFile, filename);
126                     params.put( target+"."+TKUploadField.NEW_MEDIA_ID, String.valueOf(newMediaID));
127                     tmpFile.delete();
128                 }
129                 else
130                 {
131                     // Upload kommt ins Filesystem
132
extField.saveToFilesystem(params, tmpFile, filename);
133                     t.set( TKUploadField.DOCUMENTS_FROM_FILESYSTEM , Boolean.TRUE);
134                     params.remove(target+".MEDIA_ID"); // falls sie vorher (Umschalten im Formular) da war
135

136                 }
137
138                 // delete the tempfile
139
fileInputStr.close();
140
141                 Object JavaDoc realData = field.compileData( "", evt.getParams().getClass( PARAMETER ), new TKHashtable() );
142
143                 field.fillIntoTemplate( t, realData, "" );
144
145                 extField.extModify(action, t,params, target );
146
147             }
148             else if(action != null && action.equals(DELETE)){
149                 t = evt.getPrepHTMLTemplate( "ce_fields.tmpl" );
150
151                 params.put( target+"."+TKUploadField.MEDIA_ID_PARNAME,"");
152                 params.put( target,""); // Filneame
153

154                 Object JavaDoc realData = field.compileData( "", evt.getParams().getClass( PARAMETER ), new TKHashtable() );
155
156                 field.fillIntoTemplate( t, realData, "" );
157
158             }
159             else
160             {
161                 // for actions: CANCEL, MODIFY,
162
t = evt.getPrepHTMLTemplate( "ce_upload.tmpl" );
163                 extField.extModify(action, t,params, target );
164             }
165             CEUtils.keepCEContext(evt, t );
166
167             WebManEvent.fillEventsIntoTemplate(evt.getRemoteUser(), t, ContextConstants.CONTENT_EDIT, Policy.CONTENT_TREE_ID, ceContext.conNodeId);
168
169             evt.finishTemplate(t);
170
171         }
172         catch (Throwable JavaDoc e)
173         {
174             // TO DO : Analyze Exception !
175
throw WebmanExceptionHandler.getException(e);
176         }
177     }
178
179     /**
180      * Checks wether this handler is capable to handle the specified event.
181      *
182      * @param evt the event to be handled.
183      * @return <CODE>true</CODE> if this handler is capable to handle the
184      * specified event, otherwise <CODE>false</CODE>.
185      */

186     public boolean isHandler (TKEvent evt)
187     {
188         return evt.getName().equalsIgnoreCase(CE_EXT_MODIFY);
189     }
190
191 }
192
Popular Tags