KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.engines.shared;
2
3 import org.jahia.params.ParamBean;
4 import org.jahia.exceptions.JahiaException;
5 import org.jahia.data.fields.JahiaField;
6 import org.jahia.data.files.JahiaFileField;
7 import org.jahia.data.containers.JahiaContainer;
8 import org.jahia.data.constants.JahiaConstants;
9 import org.jahia.engines.JahiaEngine;
10 import org.jahia.engines.JahiaEngineTools;
11 import org.jahia.engines.filemanager.DAVFilemanager_Engine;
12 import org.jahia.engines.filemanager.TableEntry;
13 import org.jahia.services.webdav.DAVFileAccess;
14 import org.jahia.services.webdav.JahiaWebdavBaseService;
15 import org.jahia.registries.ServicesRegistry;
16 import org.jahia.services.acl.JahiaBaseACL;
17
18
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.HashSet JavaDoc;
24
25 /**
26  * Created by IntelliJ IDEA.
27  * User: toto
28  * Date: Jun 27, 2003
29  * Time: 2:45:58 PM
30  * To change this template use Options | File Templates.
31  */

32 public class DAVFile_Field {
33
34
35
36     private static DAVFile_Field theObject = null;
37 // private static final String JSP_FILE = "/jsp/jahia/engines/shared/davfile_field.jsp";
38
private static final String JavaDoc TREE_MANAGER_JSP = "/jsp/jahia/engines/shared/embedded_filemanager.jsp";
39     private static final String JavaDoc FLAT_MANAGER_JSP = "/jsp/jahia/engines/shared/embedded_flat_filemanager.jsp";
40     public static final String JavaDoc READONLY_JSP = "/jsp/jahia/engines/shared/readonly_file_field.jsp";
41
42
43    /***
44     * getInstance
45     * NK 21.01.2001
46     *
47     */

48     public static synchronized DAVFile_Field getInstance()
49     {
50         if (theObject == null) {
51             theObject = new DAVFile_Field();
52         }
53         return theObject;
54     } // end getInstance
55

56
57     /***
58         * handles the field actions
59         *
60         * @param jParams a ParamBean object
61         * @return true if everything went okay, false if not
62         * @see org.jahia.engines.JahiaEngine
63         *
64         */

65     public boolean handleField( ParamBean jParams, Integer JavaDoc modeInt, HashMap JavaDoc engineMap )
66     throws JahiaException
67     {
68         int mode = modeInt.intValue();
69         JahiaField theField = (JahiaField) engineMap.get( "theField" );
70         engineMap.put("filemanagerUrl", jParams.composeEngineUrl("filemanager", ""));
71         //logger.debug (theField.getID() );
72
//logger.debug ("mode is " + mode );
73

74         if ( mode != JahiaEngine.SAVE_MODE ){
75             String JavaDoc theScreen = jParams.getRequest().getParameter ("screen");
76             String JavaDoc lastScreen = jParams.getRequest().getParameter ("lastscreen");
77             if ( theScreen == null || lastScreen == null ){
78                 mode = JahiaEngine.LOAD_MODE;
79             }
80         }
81
82         switch (mode)
83         {
84             case (JahiaEngine.LOAD_MODE) : return composeEngineMap( jParams, engineMap, theField );
85             case (JahiaEngine.UPDATE_MODE) : return getFormData( jParams, engineMap, theField );
86             case (JahiaEngine.SAVE_MODE) : return saveData( jParams, engineMap, theField );
87         }
88         return false;
89     } // end handleField
90

91
92
93     /***
94         * gets POST data from the form and saves it in session
95         *
96         * @param jParams a ParamBean object
97         * @param engineMap the engine hashmap
98         * @param theField the field we are working on
99         * @return true if everything went okay, false if not
100         *
101         */

102     private boolean getFormData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
103     throws JahiaException
104     {
105         boolean out = true;
106
107         String JavaDoc fileID = jParams.getParameter("file_id");
108         fileID = TableEntry.javascriptDecode(fileID);
109         JahiaFileField fField;
110
111             DAVFileAccess objectDav = JahiaWebdavBaseService.getInstance().getDAVFileAccess(jParams, fileID);
112
113         fField = objectDav.getJahiaFileField();
114         theField.setValue(fileID);
115             theField.setObject(fField);
116
117             //logger.debug ("activefolderID=" + activeFolderID );
118
engineMap.put( "fileID", fileID );
119
120             String JavaDoc fixRights = jParams.getRequest().getParameter("fixrights");
121             if (fixRights != null && fixRights.equals("on")) {
122                 Set JavaDoc uris = new HashSet JavaDoc();
123                 String JavaDoc[] users = jParams.getRequest().getParameterValues("user");
124                 if (users != null) {
125                     for (int i = 0; i < users.length; i++) {
126                         uris.add(users[i]);
127                     }
128                 }
129                 objectDav.beginTransaction();
130                 objectDav.alignPermsWithField(theField, uris);
131                 objectDav.commitTransaction();
132             }
133
134             //logger.debug ("getFormData for " + jParams.getEngine() + " fileTitle is " + fileTitle);
135

136         return out;
137     } // end getFormData
138

139
140
141     /***
142      * saves data in datasource
143      *
144      * @param jParams a ParamBean object
145      * @param engineMap the engine hashmap
146      * @param theField the field we are working on
147      * @return true if everything went okay, false if not
148      *
149      */

150     private boolean saveData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
151     throws JahiaException
152     {
153         //logger.debug ("started ");
154
JahiaFileField fField = (JahiaFileField)theField.getObject();
155         if ( fField != null ){
156             //fField.setFileFieldTitle(fileTitle);
157
//fField.setFileID(fileID);
158
//theField.setObject(fField);
159
// 0 for parentAclID in saveField, because field already exists
160
// -> field already has an aclID
161
// -> no need to create a new one
162

163             //logger.debug ("fieldID= " + theField.getID());
164
boolean out = theField.save(jParams);
165
166
167             return out;
168             //ServicesRegistry.getInstance().getJahiaFieldService().saveField( theField, 0, jParams );
169
}
170
171         //engineMap.put( "engineOutputFile", JahiaEngine.CLOSE_JSP );
172

173         return true;
174
175
176     } // end saveData
177

178     /***
179         * composes engine hash map
180         *
181         * @param jParams a ParamBean object
182         * @param engineMap the engine hashmap
183         * @param theField the field we are working on
184         * @return true if everything went okay, false if not
185         *
186         */

187     private boolean composeEngineMap( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
188     throws JahiaException
189     {
190
191 // List list = checkRights(jParams, theField);
192
if (jParams.getRequest().getSession().getAttribute(jParams.getSiteID()+"-justUploaded") != null) {
193             jParams.setParameter("file_id",TableEntry.javascriptEncode((String JavaDoc) jParams.getRequest().getSession().getAttribute(jParams.getSiteID()+"-justUploaded")));
194             jParams.getRequest().getSession().removeAttribute(jParams.getSiteID()+"-justUploaded");
195             getFormData(jParams,engineMap, theField);
196         }
197         JahiaFileField fField = composeFileField(jParams,engineMap,theField);
198
199         boolean editable = false;
200         JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer");
201         if ( theContainer == null ){
202             // in case of a field , not a field in a container
203
editable = true;
204         } else {
205             HashMap JavaDoc ctnListFieldAcls = (HashMap JavaDoc) engineMap
206                     .get("ctnListFieldAcls");
207
208             if (theContainer.getListID() != 0 && ctnListFieldAcls != null
209                     && ctnListFieldAcls.size() > 0) {
210                 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL(
211                         ctnListFieldAcls, theField.getID());
212                 if (acl != null) {
213                     editable = acl.getPermission(jParams.getUser(),
214                             JahiaBaseACL.WRITE_RIGHTS, true);
215                 }
216             } else {
217                 editable = true;
218             }
219         }
220
221         String JavaDoc output = "";
222
223         //logger.debug ("composeEngineMap for " + jParams.getEngine() );
224
// set the object
225
theField.setObject(fField);
226
227         String JavaDoc value = theField.getValue();
228
229                 DAVFileAccess rm = JahiaWebdavBaseService.getInstance().getDAVFileAccess(jParams, fField.getRealName());
230         if (rm.isValid()) {
231                 engineMap.put( "deniedUsers" , rm.comparePermsWithField(theField));
232             engineMap.put( "canAdmin",new Boolean JavaDoc(rm.hasPermission(DAVFileAccess.MANAGE)));
233             jParams.setParameter("select-file", TableEntry.javascriptEncode(rm.getPath()));
234         } else {
235             value = JahiaConstants.NULL_STRING_MARKER;
236             jParams.setParameter("select-file", value);
237         }
238
239         List JavaDoc hiddenEntries = new ArrayList JavaDoc();
240
241         DAVFilemanager_Engine engine = DAVFilemanager_Engine.getInstance();
242
243         boolean flat = true;
244
245         String JavaDoc viewType = jParams.getParameter("viewtype");
246         if (viewType != null) {
247             jParams.getRequest().getSession().setAttribute("viewtype",viewType);
248         }
249         viewType = (String JavaDoc) jParams.getRequest().getSession().getAttribute("viewtype");
250         if (viewType != null) {
251             flat = viewType.equals("flat");
252         }
253         engine.prepareFileTree(jParams, engineMap, hiddenEntries, flat, true);
254         engineMap.put("fileManagerEngineUrl", jParams.composeEngineUrl("filemanager"));
255
256         if ( editable ){
257             if (flat) {
258                 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, FLAT_MANAGER_JSP );
259             } else {
260                 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, TREE_MANAGER_JSP );
261             }
262         } else {
263             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, READONLY_JSP );
264         }
265
266         engineMap.put( "fieldForm", output );
267
268         return true;
269     } // end composeEngineMap
270

271
272     /***
273         * composes JahiaFileField object
274         *
275         * @param jParams a ParamBean object
276         * @param engineMap the engine hashmap
277         * @param theField the field we are working on
278         * @return the page object
279         *
280         */

281     private JahiaFileField composeFileField( ParamBean jParams,
282             HashMap JavaDoc engineMap, JahiaField theField )
283     throws JahiaException
284     {
285
286         JahiaFileField fField = (JahiaFileField)theField.getObject();
287             String JavaDoc movedFrom = (String JavaDoc) jParams.getRequest().getSession().getAttribute(jParams.getSiteID()+"-movedfrom");
288             if (movedFrom != null) {
289                 jParams.getRequest().getSession().removeAttribute(jParams.getSiteID()+"-movedfrom");
290                 String JavaDoc movedTo = (String JavaDoc) jParams.getRequest().getSession().getAttribute(jParams.getSiteID()+"-movedto");
291                 if (fField.getRealName().equals(movedFrom)) {
292                     jParams.setParameter("file_id", TableEntry.javascriptEncode(movedTo));
293                     this.getFormData(jParams, engineMap, theField);
294                     fField = (JahiaFileField) theField.getObject();
295                 }
296             }
297
298         engineMap.put( "filename", TableEntry.javascriptEncode(fField.getRealName()));
299
300         if (fField.getRealName().equals("")) {
301             engineMap.put( "filenotloaded", fField.getType());
302         } else {
303             engineMap.remove("filenotloaded");
304         }
305
306         return fField;
307
308     } // end composePage
309

310
311 }
312
Popular Tags