KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > shared > File_Field


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 //
14
// File_Field
15
// 21.01.2001 NK
16
// 09.05.2001 NK Synchronize with rigth, logs screen
17
//
18
// handleField( mode, jParams )
19
//
20

21 package org.jahia.engines.shared;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import org.jahia.data.FormDataManager;
28 import org.jahia.data.containers.JahiaContainer;
29 import org.jahia.data.fields.JahiaField;
30 import org.jahia.data.files.JahiaFile;
31 import org.jahia.data.files.JahiaFileField;
32 import org.jahia.engines.JahiaEngine;
33 import org.jahia.engines.JahiaEngineTools;
34 import org.jahia.engines.filemanager.Filemanager_Engine;
35 import org.jahia.exceptions.JahiaException;
36 import org.jahia.params.ParamBean;
37 import org.jahia.registries.ServicesRegistry;
38 import org.jahia.services.acl.JahiaBaseACL;
39 import org.jahia.services.filemanager.Filemanager;
40 import org.jahia.services.filemanager.Folder;
41
42 public class File_Field
43 {
44
45     private static File_Field theObject = null;
46     private static final String JavaDoc JSP_FILE = "/jsp/jahia/engines/shared/file_field.jsp";
47     public static final String JavaDoc READONLY_JSP = "/jsp/jahia/engines/shared/readonly_file_field.jsp";
48
49
50    /***
51     * getInstance
52     * NK 21.01.2001
53     *
54     */

55     public static synchronized File_Field getInstance()
56     {
57         if (theObject == null) {
58             theObject = new File_Field();
59         }
60         return theObject;
61     } // end getInstance
62

63
64     /***
65         * handles the field actions
66         *
67         * @param jParams a ParamBean object
68         * @param mode the mode, according to JahiaEngine
69         * @return true if everything went okay, false if not
70         * @see org.jahia.engines.JahiaEngine
71         *
72         */

73     public boolean handleField( ParamBean jParams, Integer JavaDoc modeInt, HashMap JavaDoc engineMap )
74     throws JahiaException
75     {
76         int mode = modeInt.intValue();
77         JahiaField theField = (JahiaField) engineMap.get( "theField" );
78         //logger.debug (theField.getID() );
79
//logger.debug ("mode is " + mode );
80

81         if ( mode != JahiaEngine.SAVE_MODE ){
82             String JavaDoc theScreen = jParams.getRequest().getParameter ("screen");
83             String JavaDoc lastScreen = jParams.getRequest().getParameter ("lastscreen");
84             if ( theScreen == null || lastScreen == null ){
85                 mode = JahiaEngine.LOAD_MODE;
86             }
87         }
88
89         switch (mode)
90         {
91             case (JahiaEngine.LOAD_MODE) : return composeEngineMap( jParams, engineMap, theField );
92             case (JahiaEngine.UPDATE_MODE) : return getFormData( jParams, engineMap, theField );
93             case (JahiaEngine.SAVE_MODE) : return saveData( jParams, engineMap, theField );
94         }
95         return false;
96     } // end handleField
97

98
99
100     /***
101         * gets POST data from the form and saves it in session
102         *
103         * @param jParams a ParamBean object
104         * @param engineMap the engine hashmap
105         * @param theField the field we are working on
106         * @return true if everything went okay, false if not
107         *
108         */

109     private boolean getFormData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
110     throws JahiaException
111     {
112
113         boolean out = true;
114
115         //logger.debug ("started");
116

117         JahiaFileField fField = composeFileField(jParams,engineMap,theField);
118         String JavaDoc fileTitle = jParams.getRequest().getParameter("file_title");
119         if ( fileTitle == null ){
120             fileTitle = "";
121         }
122
123         Integer JavaDoc fileID;
124         try {
125             fileID = new Integer JavaDoc( jParams.getRequest().getParameter("file_id") );
126             JahiaFile jFile = ServicesRegistry.getInstance()
127                             .getJahiaFilemanagerService()
128                             .getFileDB(fileID.intValue());
129             if ( jFile != null ){
130                 if ( fField == null ){
131                     fField = new JahiaFileField(jFile,new Properties JavaDoc());
132                 } else {
133                     fField.setFile(jFile);
134                 }
135             } else {
136                 fileID = new Integer JavaDoc(-1);
137             }
138         } catch (NumberFormatException JavaDoc nfe) {
139             fileID = new Integer JavaDoc(-1);
140         }
141
142         if ( fField != null ){
143            fField.setFileID(fileID.intValue());
144            fField.setFileFieldTitle(fileTitle);
145         }
146         theField.setObject(fField);
147
148         Integer JavaDoc activeFolderID;
149         try {
150             activeFolderID =
151                     new Integer JavaDoc( jParams.getRequest().getParameter("active_folder_id") );
152         } catch (NumberFormatException JavaDoc nfe) {
153             activeFolderID = new Integer JavaDoc(-1);
154         }
155
156         //logger.debug ("activefolderID=" + activeFolderID );
157
if ( activeFolderID == null || activeFolderID.intValue()==-1 ){
158             Folder activeFolder = ServicesRegistry.getInstance()
159                                 .getJahiaFilemanagerService()
160                                 .getFolderByFile( fileID.intValue() );
161             if ( activeFolder == null ){
162                Filemanager fmng = ServicesRegistry.getInstance()
163                                 .getJahiaFilemanagerService()
164                                 .getFilemanagerByOwner(jParams.getJahiaID());
165                if ( fmng == null ){
166                     // throws exception
167
}
168                activeFolder = fmng.getRootFolder();
169             }
170             activeFolderID = new Integer JavaDoc(activeFolder.getFolderID());
171             //logger.debug (" activeFolder name is " + activeFolder.getFolderName() );
172
}
173
174         engineMap.put( "fileID", fileID );
175         engineMap.put( "activeFolderID", activeFolderID );
176         engineMap.put("fileTitle",fileTitle);
177
178         //logger.debug ("getFormData for " + jParams.getEngine() + " fileTitle is " + fileTitle);
179
return out;
180
181     } // end getFormData
182

183
184
185     /***
186      * saves data in datasource
187      *
188      * @param jParams a ParamBean object
189      * @param engineMap the engine hashmap
190      * @param theField the field we are working on
191      * @return true if everything went okay, false if not
192      *
193      */

194     private boolean saveData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
195     throws JahiaException
196     {
197         //logger.debug ("started ");
198

199         JahiaFileField fField = (JahiaFileField)theField.getObject();
200         if ( fField != null ){
201             //fField.setFileFieldTitle(fileTitle);
202
//fField.setFileID(fileID);
203
//theField.setObject(fField);
204
// 0 for parentAclID in saveField, because field already exists
205
// -> field already has an aclID
206
// -> no need to create a new one
207

208             //logger.debug ("fieldID= " + theField.getID());
209
return theField.save(jParams);
210             //ServicesRegistry.getInstance().getJahiaFieldService().saveField( theField, 0, jParams );
211
}
212
213         //engineMap.put( "engineOutputFile", JahiaEngine.CLOSE_JSP );
214

215         return true;
216
217
218     } // end saveData
219

220
221     /***
222         * composes engine hash map
223         *
224         * @param jParams a ParamBean object
225         * @param engineMap the engine hashmap
226         * @param theField the field we are working on
227         * @return true if everything went okay, false if not
228         *
229         */

230     private boolean composeEngineMap( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
231     throws JahiaException
232     {
233
234         boolean editable = false;
235         JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer");
236         if ( theContainer == null ){
237             // in case of a field , not a field in a container
238
editable = true;
239         } else {
240             HashMap JavaDoc ctnListFieldAcls = (HashMap JavaDoc) engineMap
241                     .get("ctnListFieldAcls");
242
243             if (theContainer.getListID() != 0 && ctnListFieldAcls != null
244                     && ctnListFieldAcls.size() > 0) {
245                 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL(
246                         ctnListFieldAcls, theField.getID());
247                 if (acl != null) {
248                     editable = acl.getPermission(jParams.getUser(),
249                             JahiaBaseACL.WRITE_RIGHTS, true);
250                 }
251             } else {
252                 editable = true;
253             }
254         }
255
256         String JavaDoc output = "";
257
258         //logger.debug ("composeEngineMap for " + jParams.getEngine() );
259

260         JahiaFileField fField = composeFileField(jParams,engineMap,theField);
261
262         // set the object
263
theField.setObject(fField);
264
265         if ( editable ){
266
267
268             // the files list for a gived page and all files made public
269
Vector JavaDoc v = ServicesRegistry.getInstance()
270                                                 .getJahiaFilemanagerService()
271                                                 .getFilesByPage(jParams.getSiteID(),jParams.getPageID(),true);
272
273             Vector JavaDoc filesList = new Vector JavaDoc();
274
275             if ( jParams.getUser().isAdminMember(jParams.getSiteID()) ){
276                 filesList = v;
277             } else {
278                 int size = v.size();
279                 JahiaFile jf = null;
280                 for (int i=0 ; i<size ; i++){
281                     jf = (JahiaFile)v.get(i);
282                     if ( (jf.getPublic()>0) || jf.getUploadUser().equals(jParams.getUser().getName()) ){
283                         filesList.add(jf);
284                     }
285                 }
286             }
287             engineMap.put( "filesList", filesList.elements() );
288
289             // the filemanager engine url
290
Vector JavaDoc datas = new Vector JavaDoc();
291             datas.add(""); // action
292
datas.add(theField);
293             engineMap.put( "filemanagerUrl", Filemanager_Engine.getInstance().renderLink( jParams, datas ) );
294
295             theField.setValue( FormDataManager.getInstance().formEncode(theField.getValue()) );
296             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, JSP_FILE );
297         } else {
298             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, READONLY_JSP );
299         }
300
301         engineMap.put( "fieldForm", output );
302
303         return true;
304     } // end composeEngineMap
305

306
307     /***
308         * composes JahiaFileField object
309         *
310         * @param jParams a ParamBean object
311         * @param engineMap the engine hashmap
312         * @param theField the field we are working on
313         * @return the page object
314         *
315         */

316     private JahiaFileField composeFileField( ParamBean jParams,
317             HashMap JavaDoc engineMap, JahiaField theField )
318     throws JahiaException
319     {
320         JahiaFileField fField = null;
321
322         if (theField.getObject() == null) {
323
324             // get the active folder
325
Folder activeFolder = null;
326             Integer JavaDoc activeFolderID = null;
327             Filemanager fmng = null;
328
329             // set to root folder
330
fmng = ServicesRegistry.getInstance()
331                  .getJahiaFilemanagerService().getFilemanagerByOwner(jParams.getJahiaID());
332             if ( fmng == null ){
333                 // throws exception
334
} else {
335                 activeFolder = fmng.getRootFolder();
336             }
337
338             activeFolderID = new Integer JavaDoc(activeFolder.getFolderID());
339
340             String JavaDoc fileTitle = "";
341
342             //logger.debug ("activefolderID=" + activeFolderID );
343
//logger.debug ("fileTitle=" + fileTitle );
344

345             engineMap.put( "fileID", new Integer JavaDoc(-1) );
346             engineMap.put( "activeFolderID", activeFolderID );
347             engineMap.put( "fileTitle", fileTitle );
348
349             JahiaFile jahiaFile = new JahiaFile(
350                     fmng.getFilemanagerID(),
351                     activeFolder.getFolderID(),
352                     "", // upload user
353
"", // realname
354
"", // storage name
355
0, // modif date
356
0, // size
357
"", // type
358
"", // title
359
"", // descr
360
String.valueOf(ServicesRegistry.getInstance()
361                           .getJahiaVersionService().getCurrentVersionID()), // version
362
JahiaFile.STATE_ACTIVE );
363
364             fField = new JahiaFileField(jahiaFile, new Properties JavaDoc());
365         } else {
366             fField = (JahiaFileField)theField.getObject();
367             //System.out.println(" composeFileField(), getObject not null and fField.getFieldID()=" + fField.getFieldID());
368

369             int fileID = fField.getFileID();
370
371             JahiaFile fileItem = ServicesRegistry.getInstance()
372                                 .getJahiaFilemanagerService().getFileDB(fileID, fField.getVersion());
373
374             // get the active folder
375
Folder activeFolder = null;
376             Integer JavaDoc activeFolderID = null;
377
378             if ( fileItem != null ) {
379                 // the active folder
380
activeFolder = ServicesRegistry.getInstance()
381                              .getJahiaFilemanagerService().getFolderByFile(fileID);
382             } else {
383                 // set to root folder
384
Filemanager fmng = ServicesRegistry.getInstance()
385                                  .getJahiaFilemanagerService()
386                                  .getFilemanagerByOwner(jParams.getJahiaID());
387                 if ( fmng == null ){
388                     // throws exception
389
}
390                 activeFolder = fmng.getRootFolder();
391             }
392
393             activeFolderID = new Integer JavaDoc(activeFolder.getFolderID());
394
395             engineMap.put( "fileID", new Integer JavaDoc(fileID) );
396             engineMap.put( "activeFolderID", activeFolderID );
397             engineMap.put( "fileTitle", fField.getFileFieldTitle() );
398         }
399
400         return fField;
401
402     } // end composePage
403

404
405 } // end File_Field
406
Popular Tags