1 13 21 package org.jahia.engines.shared; 22 23 import java.util.HashMap ; 24 import java.util.Properties ; 25 import java.util.Vector ; 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 JSP_FILE = "/jsp/jahia/engines/shared/file_field.jsp"; 47 public static final String READONLY_JSP = "/jsp/jahia/engines/shared/readonly_file_field.jsp"; 48 49 50 55 public static synchronized File_Field getInstance() 56 { 57 if (theObject == null) { 58 theObject = new File_Field(); 59 } 60 return theObject; 61 } 63 64 73 public boolean handleField( ParamBean jParams, Integer modeInt, HashMap engineMap ) 74 throws JahiaException 75 { 76 int mode = modeInt.intValue(); 77 JahiaField theField = (JahiaField) engineMap.get( "theField" ); 78 81 if ( mode != JahiaEngine.SAVE_MODE ){ 82 String theScreen = jParams.getRequest().getParameter ("screen"); 83 String 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 } 98 99 100 109 private boolean getFormData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 110 throws JahiaException 111 { 112 113 boolean out = true; 114 115 117 JahiaFileField fField = composeFileField(jParams,engineMap,theField); 118 String fileTitle = jParams.getRequest().getParameter("file_title"); 119 if ( fileTitle == null ){ 120 fileTitle = ""; 121 } 122 123 Integer fileID; 124 try { 125 fileID = new Integer ( 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 ()); 132 } else { 133 fField.setFile(jFile); 134 } 135 } else { 136 fileID = new Integer (-1); 137 } 138 } catch (NumberFormatException nfe) { 139 fileID = new Integer (-1); 140 } 141 142 if ( fField != null ){ 143 fField.setFileID(fileID.intValue()); 144 fField.setFileFieldTitle(fileTitle); 145 } 146 theField.setObject(fField); 147 148 Integer activeFolderID; 149 try { 150 activeFolderID = 151 new Integer ( jParams.getRequest().getParameter("active_folder_id") ); 152 } catch (NumberFormatException nfe) { 153 activeFolderID = new Integer (-1); 154 } 155 156 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 } 168 activeFolder = fmng.getRootFolder(); 169 } 170 activeFolderID = new Integer (activeFolder.getFolderID()); 171 } 173 174 engineMap.put( "fileID", fileID ); 175 engineMap.put( "activeFolderID", activeFolderID ); 176 engineMap.put("fileTitle",fileTitle); 177 178 return out; 180 181 } 183 184 185 194 private boolean saveData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 195 throws JahiaException 196 { 197 199 JahiaFileField fField = (JahiaFileField)theField.getObject(); 200 if ( fField != null ){ 201 208 return theField.save(jParams); 210 } 212 213 215 return true; 216 217 218 } 220 221 230 private boolean composeEngineMap( ParamBean jParams, HashMap engineMap, JahiaField theField ) 231 throws JahiaException 232 { 233 234 boolean editable = false; 235 JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer"); 236 if ( theContainer == null ){ 237 editable = true; 239 } else { 240 HashMap ctnListFieldAcls = (HashMap ) 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 output = ""; 257 258 260 JahiaFileField fField = composeFileField(jParams,engineMap,theField); 261 262 theField.setObject(fField); 264 265 if ( editable ){ 266 267 268 Vector v = ServicesRegistry.getInstance() 270 .getJahiaFilemanagerService() 271 .getFilesByPage(jParams.getSiteID(),jParams.getPageID(),true); 272 273 Vector filesList = new Vector (); 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 Vector datas = new Vector (); 291 datas.add(""); 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 } 306 307 316 private JahiaFileField composeFileField( ParamBean jParams, 317 HashMap engineMap, JahiaField theField ) 318 throws JahiaException 319 { 320 JahiaFileField fField = null; 321 322 if (theField.getObject() == null) { 323 324 Folder activeFolder = null; 326 Integer activeFolderID = null; 327 Filemanager fmng = null; 328 329 fmng = ServicesRegistry.getInstance() 331 .getJahiaFilemanagerService().getFilemanagerByOwner(jParams.getJahiaID()); 332 if ( fmng == null ){ 333 } else { 335 activeFolder = fmng.getRootFolder(); 336 } 337 338 activeFolderID = new Integer (activeFolder.getFolderID()); 339 340 String fileTitle = ""; 341 342 345 engineMap.put( "fileID", new Integer (-1) ); 346 engineMap.put( "activeFolderID", activeFolderID ); 347 engineMap.put( "fileTitle", fileTitle ); 348 349 JahiaFile jahiaFile = new JahiaFile( 350 fmng.getFilemanagerID(), 351 activeFolder.getFolderID(), 352 "", "", "", 0, 0, "", "", "", String.valueOf(ServicesRegistry.getInstance() 361 .getJahiaVersionService().getCurrentVersionID()), JahiaFile.STATE_ACTIVE ); 363 364 fField = new JahiaFileField(jahiaFile, new Properties ()); 365 } else { 366 fField = (JahiaFileField)theField.getObject(); 367 369 int fileID = fField.getFileID(); 370 371 JahiaFile fileItem = ServicesRegistry.getInstance() 372 .getJahiaFilemanagerService().getFileDB(fileID, fField.getVersion()); 373 374 Folder activeFolder = null; 376 Integer activeFolderID = null; 377 378 if ( fileItem != null ) { 379 activeFolder = ServicesRegistry.getInstance() 381 .getJahiaFilemanagerService().getFolderByFile(fileID); 382 } else { 383 Filemanager fmng = ServicesRegistry.getInstance() 385 .getJahiaFilemanagerService() 386 .getFilemanagerByOwner(jParams.getJahiaID()); 387 if ( fmng == null ){ 388 } 390 activeFolder = fmng.getRootFolder(); 391 } 392 393 activeFolderID = new Integer (activeFolder.getFolderID()); 394 395 engineMap.put( "fileID", new Integer (fileID) ); 396 engineMap.put( "activeFolderID", activeFolderID ); 397 engineMap.put( "fileTitle", fField.getFileFieldTitle() ); 398 } 399 400 return fField; 401 402 } 404 405 } | Popular Tags |