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 ; 20 import java.util.List ; 21 import java.util.ArrayList ; 22 import java.util.Set ; 23 import java.util.HashSet ; 24 25 32 public class DAVFile_Field { 33 34 35 36 private static DAVFile_Field theObject = null; 37 private static final String TREE_MANAGER_JSP = "/jsp/jahia/engines/shared/embedded_filemanager.jsp"; 39 private static final String FLAT_MANAGER_JSP = "/jsp/jahia/engines/shared/embedded_flat_filemanager.jsp"; 40 public static final String READONLY_JSP = "/jsp/jahia/engines/shared/readonly_file_field.jsp"; 41 42 43 48 public static synchronized DAVFile_Field getInstance() 49 { 50 if (theObject == null) { 51 theObject = new DAVFile_Field(); 52 } 53 return theObject; 54 } 56 57 65 public boolean handleField( ParamBean jParams, Integer modeInt, HashMap 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 74 if ( mode != JahiaEngine.SAVE_MODE ){ 75 String theScreen = jParams.getRequest().getParameter ("screen"); 76 String 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 } 91 92 93 102 private boolean getFormData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 103 throws JahiaException 104 { 105 boolean out = true; 106 107 String 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 engineMap.put( "fileID", fileID ); 119 120 String fixRights = jParams.getRequest().getParameter("fixrights"); 121 if (fixRights != null && fixRights.equals("on")) { 122 Set uris = new HashSet (); 123 String [] 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 136 return out; 137 } 139 140 141 150 private boolean saveData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 151 throws JahiaException 152 { 153 JahiaFileField fField = (JahiaFileField)theField.getObject(); 155 if ( fField != null ){ 156 163 boolean out = theField.save(jParams); 165 166 167 return out; 168 } 170 171 173 return true; 174 175 176 } 178 187 private boolean composeEngineMap( ParamBean jParams, HashMap engineMap, JahiaField theField ) 188 throws JahiaException 189 { 190 191 if (jParams.getRequest().getSession().getAttribute(jParams.getSiteID()+"-justUploaded") != null) { 193 jParams.setParameter("file_id",TableEntry.javascriptEncode((String ) 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 editable = true; 204 } else { 205 HashMap ctnListFieldAcls = (HashMap ) 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 output = ""; 222 223 theField.setObject(fField); 226 227 String 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 (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 hiddenEntries = new ArrayList (); 240 241 DAVFilemanager_Engine engine = DAVFilemanager_Engine.getInstance(); 242 243 boolean flat = true; 244 245 String viewType = jParams.getParameter("viewtype"); 246 if (viewType != null) { 247 jParams.getRequest().getSession().setAttribute("viewtype",viewType); 248 } 249 viewType = (String ) 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 } 271 272 281 private JahiaFileField composeFileField( ParamBean jParams, 282 HashMap engineMap, JahiaField theField ) 283 throws JahiaException 284 { 285 286 JahiaFileField fField = (JahiaFileField)theField.getObject(); 287 String movedFrom = (String ) jParams.getRequest().getSession().getAttribute(jParams.getSiteID()+"-movedfrom"); 288 if (movedFrom != null) { 289 jParams.getRequest().getSession().removeAttribute(jParams.getSiteID()+"-movedfrom"); 290 String movedTo = (String ) 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 } 310 311 } 312 | Popular Tags |