1 package org.jahia.services.webdav; 2 3 import org.apache.log4j.Logger; 4 import org.apache.slide.structure.ObjectNode; 5 import org.jahia.data.fields.JahiaField; 6 import org.jahia.data.files.JahiaFile; 7 import org.jahia.data.files.JahiaFileField; 8 import org.jahia.exceptions.JahiaException; 9 import org.jahia.params.ParamBean; 10 import org.jahia.registries.ServicesRegistry; 11 import org.jahia.services.database.ConnectionDispenser; 12 import org.jahia.services.fields.ContentField; 13 import org.jahia.services.fields.ContentFieldTypes; 14 import org.jahia.services.filemanager.Filemanager; 15 import org.jahia.services.filemanager.JahiaFilemanagerService; 16 import org.jahia.services.sites.JahiaSite; 17 import org.jahia.services.usermanager.JahiaGroupManagerService; 18 import org.jahia.services.usermanager.JahiaUser; 19 import org.jahia.services.version.EntryLoadRequest; 20 import org.jahia.services.cache.HtmlCache; 21 import org.jahia.services.cache.CacheFactory; 22 import org.jahia.services.containers.ContentContainer; 23 import org.jahia.services.containers.ContentContainerList; 24 import org.jahia.utils.JahiaTools; 25 import org.jahia.content.ContentContainerListsXRefManager; 26 import org.jahia.content.ContentFieldXRefManager; 27 import org.jahia.bin.Jahia; 28 29 import java.io.File ; 30 import java.sql.*; 31 import java.util.*; 32 33 40 public class JahiaWebdavBaseService 41 implements JahiaWebdavService { 42 private static Logger logger = Logger.getLogger (JahiaWebdavBaseService.class); 43 private static JahiaWebdavBaseService instance; 44 45 public static JahiaWebdavBaseService getInstance () { 46 if (instance == null) { 47 instance = new JahiaWebdavBaseService (); 48 } 49 return instance; 50 } 51 52 public JahiaWebdavBaseService() { 53 } 54 55 public DAVFileAccess getDAVFileAccess (ParamBean jParams, String path) { 56 return new DAVFileAccess (jParams, jParams.getSite(), jParams.getUser(), path); 57 } 58 59 public DAVFileAccess getDAVFileAccess (ParamBean jParams, JahiaSite site, JahiaUser user, String path) { 60 return new DAVFileAccess (jParams, site, user, path); 61 } 62 63 public DAVFileAccess getDAVFileAccess (JahiaSite site, String path) { 64 return new DAVFileAccess (site, path); 65 } 66 67 public JahiaFileField getJahiaFileField (ParamBean jParams, String path) { 68 DAVFileAccess objectDav = getDAVFileAccess (jParams, path); 69 return objectDav.getJahiaFileField (); 70 } 71 72 public JahiaFileField getJahiaFileField (ParamBean jParams, JahiaSite site, JahiaUser user, String path) { 73 DAVFileAccess objectDav = getDAVFileAccess (jParams, site, user, path); 74 return objectDav.getJahiaFileField (); 75 } 76 77 public Map migrate (ParamBean jParams) 78 throws JahiaException { 79 HashMap mapping = new HashMap (); 80 HashMap filesToFileFields = new HashMap (); 81 HashMap usageMap = new HashMap (); 82 HashMap namesInDir = new HashMap (); 83 84 JahiaFilemanagerService fm = ServicesRegistry.getInstance (). 85 getJahiaFilemanagerService (); 86 87 logger.info ("Starting file migration...."); 88 89 DAVFileAccess objectDav = getDAVFileAccess (jParams, jParams.getSite(), jParams.getUser(), "/shared"); 90 91 try { 92 if (!ServicesRegistry.getInstance ().getJahiaGroupManagerService (). 93 lookupGroup (jParams.getSiteID (), 94 JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME). 95 isMember (jParams.getUser ())) { 96 throw new JahiaException ("Migration not authorized", 97 "User '" + 98 jParams.getUser ().getUsername () + 99 "' has no permission to migrate the filemanager", 100 JahiaException.ENGINE_ERROR, 101 JahiaException.WARNING_SEVERITY); 102 } 103 objectDav.beginTransaction (); 104 boolean create = objectDav.createCollection ("import") != null; 105 objectDav.commitTransaction (); 106 if (!create) { 107 return mapping; 108 } 109 objectDav = getDAVFileAccess (jParams, jParams.getSite(), jParams.getUser(), "/shared/import"); 110 111 Connection connection = ConnectionDispenser.getConnection (); 112 Statement statement; 113 ResultSet rs; 114 115 Filemanager f = fm.getFilemanagerByOwner (jParams.getSiteID ()); 116 logger.debug ("Filemanager : " + f); 117 if (f == null) { 118 return mapping; 119 } 120 statement = connection.createStatement (); 121 rs = 122 statement.executeQuery ( 123 "select id_jahia_filefield, prop_value_filefield from jahia_file_fields where prop_name_filefield='field_file_fileid'"); 124 int resultCount = 0; 125 while (rs.next ()) { 126 resultCount++; 127 logger.debug ("Processing result #" + resultCount); 128 Integer fileField = new Integer (rs.getInt (1)); 129 Integer fileId = new Integer (rs.getInt (2)); 130 List thisUsages = findUsages (fileField.toString (), jParams, false); 131 List fileUsage = (List) usageMap.get (fileId); 132 if (fileUsage == null) { 133 fileUsage = new ArrayList (); 134 usageMap.put (fileId, fileUsage); 135 } 136 fileUsage.addAll (thisUsages); 137 HashSet fileFields = (HashSet) filesToFileFields.get (fileId); 138 if (fileFields == null) { 139 fileFields = new HashSet (); 140 filesToFileFields.put (fileId, fileFields); 141 } 142 fileFields.add (fileField); 143 } 144 statement.close (); 145 146 Map titles = new HashMap (); 147 statement = connection.createStatement (); 148 rs = 149 statement.executeQuery ( 150 "select id_jahia_filefield, prop_value_filefield from jahia_file_fields where prop_name_filefield='field_file_title'"); 151 while (rs.next ()) { 152 Integer fileField = new Integer (rs.getInt (1)); 153 String title = rs.getString (2); 154 titles.put (fileField, title); 155 } 156 statement.close (); 157 158 statement = connection.createStatement (); 159 rs = statement.executeQuery ("select ID_JAHIA_FILE ,FILEMGRID_JAHIA_FILE ,FLDID_JAHIA_FILE ,UPLOADUSER_JAHIA_FILE ,PAGEID_JAHIA_FILE ,IS_PUBLIC_JAHIA_FILE ,REALNAME_JAHIA_FILE ,STORAGENAME_JAHIA_FILE ,LASTMODIFDATE_JAHIA_FILE ,SIZE_JAHIA_FILE ,TYPE_JAHIA_FILE ,TITLE_JAHIA_FILE ,DESCR_JAHIA_FILE ,VERSION_JAHIA_FILE ,STATE_JAHIA_FILE from jahia_filemgr_files where FILEMGRID_JAHIA_FILE=" + 160 f.getFilemanagerID () + 161 " ORDER BY ID_JAHIA_FILE"); 162 163 while (rs.next ()) { 164 int fileID = rs.getInt ("id_jahia_file"); 165 int filemanagerID = rs.getInt ("filemgrid_jahia_file"); 166 int folderID = rs.getInt ("fldid_jahia_file"); 167 String uploadUser = rs.getString ("uploaduser_jahia_file"); 168 int pageID = rs.getInt ("pageid_jahia_file"); 169 int isPublic = rs.getInt ("is_public_jahia_file"); 170 String realName = rs.getString ("realname_jahia_file"); 171 String storageName = rs.getString ("storagename_jahia_file"); 172 long lastModifDate = rs.getLong ("lastmodifdate_jahia_file"); 173 long size = rs.getLong ("size_jahia_file"); 174 String type = rs.getString ("type_jahia_file"); 175 String title = rs.getString ("title_jahia_file"); 176 String descr = rs.getString ("descr_jahia_file"); 177 String version = rs.getString ("version_jahia_file"); 178 int state = rs.getInt ("state_jahia_file"); 179 JahiaFile aFile = new JahiaFile (filemanagerID, 180 folderID, 181 uploadUser, 182 realName, 183 storageName, 184 lastModifDate, 185 size, 186 type, 187 title, 188 descr, 189 version, 190 state); 191 aFile.setFileID (fileID); 192 aFile.setPageID (pageID); 193 aFile.setPublic (isPublic); 194 logger.debug ("File " + fileID + "," + realName + "," + 195 storageName); 196 File file = fm.getRealFile (aFile); 197 if ((file != null) && (file.exists ())) { 198 objectDav.beginTransaction (); 199 String dirName; 200 String realTitle = null; 201 List usages = (List) usageMap.get (new Integer (fileID)); 202 if (usages == null || usages.isEmpty ()) { 203 dirName = "unused"; 204 } else { 205 UsageEntry usageEntry = (UsageEntry) usages.iterator (). 206 next (); 207 String pageTitle = usageEntry.getPageTitle (); 208 dirName = cleanPageTitle (pageTitle); 209 realTitle = 210 (String ) titles.get ( 211 ((Set) filesToFileFields.get (new Integer (fileID))).iterator () 212 .next ()); 213 } 214 215 DAVFileAccess directoryDav = getDAVFileAccess (jParams, 216 jParams.getSite(), jParams.getUser(), objectDav.getPath () + "/" + dirName); 217 if (!directoryDav.isValid ()) { 218 objectDav.createCollection (dirName); 219 directoryDav = getDAVFileAccess (jParams, 220 jParams.getSite(), jParams.getUser(), objectDav.getPath () + "/" + dirName); 221 namesInDir.put (dirName, new HashSet ()); 222 } 223 224 HashSet names = (HashSet) namesInDir.get (dirName); 225 226 String name = realName; 227 if (names != null) { 228 int i = 1; 229 int dot = realName.lastIndexOf ('.'); 230 while (names.contains (name.toLowerCase ())) { 231 if (dot > -1) { 232 name = realName.substring (0, dot) + "_" + (i++) + 233 realName.substring (dot); 234 } else { 235 name = realName + "_" + (i++); 236 } 237 } 238 names.add (name.toLowerCase ()); 239 } else { 240 logger.warn ("Duplicate set not found for directory " + 241 dirName); 242 } 243 name = cleanName (name); 244 245 boolean uploadSucceeded = directoryDav.uploadFile (name, 246 file, type, false) != null; 247 if (uploadSucceeded) { 248 DAVFileAccess uploadedFile = getDAVFileAccess ( 249 jParams, jParams.getSite(), jParams.getUser(), directoryDav.getPath () + "/" + name); 250 logger.debug ("Upload to " + uploadedFile.getPath () + 251 " result : " + uploadedFile.isValid ()); 252 if (uploadedFile.isValid ()) { 253 uploadedFile.setProperty ("DAV:", "owner", 254 aFile.getUploadUsername (), true); 255 if (realTitle != null && realTitle.length () > 0) { 256 uploadedFile.setName (realTitle); 257 } 258 uploadedFile.setProperty ("http://www.jahia.org/", 259 "description", descr, false); 260 uploadedFile.changePermissions ("nobody", "---"); 261 uploadedFile.changePermissions ("user", "---"); 262 if (usages != null) { 263 Set fileFields = (Set) filesToFileFields.get (new 264 Integer (fileID)); 265 for (Iterator iterator = fileFields.iterator (); 266 iterator.hasNext ();) { 267 Integer fileField = (Integer ) iterator.next (); 268 move (fileField.toString (), 269 uploadedFile.getPath (), 270 jParams.getSiteKey ()); 271 } 272 for (Iterator iterator = usages.iterator (); 273 iterator.hasNext ();) { 274 UsageEntry usageEntry = (UsageEntry) 275 iterator. 276 next (); 277 if (usageEntry.getField () != null && (usageEntry.getWorkflow () == 278 EntryLoadRequest.WAITING_WORKFLOW_STATE || 279 usageEntry.getWorkflow () == 280 EntryLoadRequest.STAGING_WORKFLOW_STATE || 281 usageEntry.getWorkflow () == 282 EntryLoadRequest.ACTIVE_WORKFLOW_STATE)) { 283 JahiaField theField = ServicesRegistry. 284 getInstance ().getJahiaFieldService (). 285 loadField (usageEntry.getField (). 286 getID (), 287 jParams); 288 Set deniedUsers = uploadedFile. 289 comparePermsWithField (theField); 290 Set uris = new HashSet (); 291 for (Iterator it2 = deniedUsers. 292 iterator (); 293 it2.hasNext ();) { 294 ObjectNode objectNode = (ObjectNode) 295 it2.next (); 296 uris.add (objectNode.getUri ()); 297 } 298 uploadedFile.alignPermsWithField ( 299 theField, 300 uris); 301 } 302 } 303 } 304 mapping.put (new Integer (aFile.getFileID ()), 305 uploadedFile.getPath ()); 306 objectDav.commitTransaction (); 307 } else { 308 objectDav.rollbackTransaction (); 309 } 310 } else { 311 logger.error ("Upload did not succeed !"); 312 objectDav.rollbackTransaction (); 313 } 314 } else { 315 logger.warn ("Warning, file " + file + 316 " doesn't exist on disk"); 317 } 318 } 319 statement.close (); 320 logger.info ("File mapping :" + mapping); 321 322 } catch (Exception e) { 334 objectDav.rollbackTransaction (); 335 throw new JahiaException ("Migration did not succeed", 336 "Exception occured during migration", 337 JahiaException.ENGINE_ERROR, 338 JahiaException.WARNING_SEVERITY, e); 339 } 340 341 fm.deleteFilemanager (jParams.getUser (), jParams.getSiteID ()); 342 343 return mapping; 344 } 345 346 public void move (String sourceUri, String destinationUri, String siteKey) { 347 try { 348 JahiaSite site = ServicesRegistry.getInstance (). 349 getJahiaSitesService ().getSiteByKey (siteKey); 350 Connection connection = ConnectionDispenser.getConnection (); 351 List l = findUsages (sourceUri, site, false); 352 HashSet lockChanged = new HashSet (); 353 PreparedStatement statement = connection.prepareStatement ( 354 "update jahia_fields_data set value_jahia_fields_data=? where ID_JAHIA_FIELDS_DATA=? and VERSION_ID=? and WORKFLOW_STATE=? and LANGUAGE_CODE=?"); 355 for (Iterator iterator = l.iterator (); iterator.hasNext ();) { 356 UsageEntry v = (UsageEntry) iterator.next (); 357 358 String newVal = destinationUri + 359 v.getVal ().substring (sourceUri.length ()); 360 statement.setString (1, newVal); 361 statement.setInt (2, v.getId ()); 362 statement.setInt (3, v.getVersion ()); 363 statement.setInt (4, v.getWorkflow ()); 364 statement.setString (5, v.getLang ()); 365 statement.executeUpdate (); 366 367 if (!lockChanged.contains (v.getVal ()) && 368 (v.getWorkflow () == EntryLoadRequest.ACTIVE_WORKFLOW_STATE || 369 v.getWorkflow () == EntryLoadRequest.WAITING_WORKFLOW_STATE)) { 370 lockChanged.add (v.getVal ()); 371 DAVFileAccess oldfile = JahiaWebdavBaseService.getInstance (). 372 getDAVFileAccess (site, v.getVal ()); 373 if (oldfile.isValid () && oldfile.hasRevisions ()) { 374 oldfile.unlockFile (true); 375 } 376 if (Jahia.getSettings().isFileLockOnPublication()) { 377 DAVFileAccess newfile = JahiaWebdavBaseService.getInstance (). 378 getDAVFileAccess (site, newVal); 379 if (newfile.isValid () && newfile.hasRevisions ()) { 380 newfile.lockFile (true); 381 } 382 } 383 } 384 ContentField.removeFromCache (v.getId ()); 385 HtmlCache htmlCache = CacheFactory.getHtmlCache (); 386 if (htmlCache == null) 387 logger.warn ("Could not get the HTML cache instance!!"); 388 if (htmlCache != null) { 389 ContentField field = ContentField.getField(v.getId()); 390 if (field.getContainerID () > 0) { 391 ContentContainerList theList = ContentContainerList.getContainerList(ContentContainer.getContainer (field.getContainerID ()).getParentContainerListID()); 392 Set containerPageRefs = ContentContainerListsXRefManager. 396 getInstance (). 397 getAbsoluteContainerListPageIDs (theList. 398 getID ()); 399 400 if (containerPageRefs != null) { 402 Iterator pageRefIDs = containerPageRefs.iterator (); 403 while (pageRefIDs.hasNext ()) { 404 Integer curPageID = (Integer ) pageRefIDs.next (); 405 if (htmlCache != null) 406 htmlCache.invalidatePageEntries (curPageID.toString ()); 407 } 408 } else { 409 logger.debug ("Why is cross ref list empty ?"); 410 } 411 } else { 412 Set fieldPageRefs = ContentFieldXRefManager.getInstance (). 413 getAbsoluteFieldPageIDs (field.getID ()); 414 415 if (fieldPageRefs != null) { 417 Iterator pageRefIDs = fieldPageRefs.iterator (); 418 while (pageRefIDs.hasNext ()) { 419 Integer curPageID = (Integer ) pageRefIDs.next (); 420 421 if (htmlCache != null) 422 htmlCache.invalidatePageEntries (curPageID.toString ()); 423 } 424 } else { 425 logger.debug ("Why is cross ref list empty ?"); 426 } 427 } 428 htmlCache.invalidatePageEntries (Integer.toString(field.getPageID())); 429 } 430 } 431 statement.close (); 432 } catch (SQLException e) { 433 e.printStackTrace (); 434 } catch (JahiaException e) { 435 e.printStackTrace (); 436 } 437 } 438 439 public List findUsages (String sourceUri, JahiaSite site, 440 boolean onlyLockedUsages) { 441 return findUsages (sourceUri, null, site.getID (), onlyLockedUsages); 442 } 443 444 public List findUsages (String sourceUri, ParamBean jParams, 445 boolean onlyLockedUsages) { 446 return findUsages (sourceUri, jParams, jParams.getSiteID (), 447 onlyLockedUsages); 448 } 449 450 private List findUsages (String sourceUri, ParamBean jParams, int siteId, 451 boolean onlyLockedUsages) { 452 List l = new ArrayList (); 453 try { 454 Connection connection = ConnectionDispenser.getConnection (); 455 String sql = "select ID_JAHIA_FIELDS_DATA,VERSION_ID,WORKFLOW_STATE,LANGUAGE_CODE,value_jahia_fields_data from jahia_fields_data, jahia_fields_def WHERE fielddefid_jahia_fields_data=id_jahia_fields_def and jahiaid_jahia_fields_def=? and (value_jahia_fields_data like ? or value_jahia_fields_data like ?) and type_jahia_fields_data=" + 456 ContentFieldTypes.FILE + " and workflow_state<>"+EntryLoadRequest.DELETED_WORKFLOW_STATE; 457 if (onlyLockedUsages) { 459 sql += " and (workflow_state=" + 460 EntryLoadRequest.WAITING_WORKFLOW_STATE + 461 " or workflow_state=" + 462 EntryLoadRequest.ACTIVE_WORKFLOW_STATE + ")"; 463 } 464 465 PreparedStatement statement = connection.prepareStatement (sql); 466 statement.setInt (1, siteId); 467 statement.setString (2, sourceUri); 468 if (sourceUri.endsWith ("/")) { 469 statement.setString (3, sourceUri + "%"); 470 } else { 471 statement.setString (3, sourceUri + "/%"); 472 } 473 ResultSet res = statement.executeQuery (); 474 while (res.next ()) { 475 UsageEntry curUsage = null; 476 try { 477 curUsage = new UsageEntry (res.getInt (1), res.getInt (2), 478 res.getInt (3), res.getString (4), 479 res.getString (5), jParams); 480 } catch (JahiaException je) { 481 curUsage = null; 482 logger.error ( 483 "Error while trying to find field usage of sourceUri=" + 484 sourceUri + " on site " + siteId, je); 485 } 486 if (curUsage != null) { 487 l.add (curUsage); 488 } 489 } 490 statement.close (); 491 } catch (SQLException e) { 492 logger.error ("Error while trying to find field usage of sourceUri=" + 493 sourceUri + " on site " + siteId, e); 494 } 495 return l; 496 } 497 498 private String cleanPageTitle (String pageTitle) { 499 String cleanTitle = pageTitle; 500 if ((pageTitle == null) || ("".equals (pageTitle))) { 501 cleanTitle = "unused"; 502 } 503 504 cleanTitle = cleanTitle(cleanTitle); 505 506 return cleanTitle; 507 } 508 509 public String cleanTitle(String cleanTitle) { 510 cleanTitle = JahiaTools.html2text (cleanTitle); 512 513 cleanTitle = JahiaTools.replacePattern (cleanTitle, "/", "_"); 515 cleanTitle = JahiaTools.replacePattern (cleanTitle, "\\", "_"); 516 cleanTitle = JahiaTools.replacePattern (cleanTitle, ":", "_"); 517 cleanTitle = JahiaTools.replacePattern (cleanTitle, ";", "_"); 518 cleanTitle = JahiaTools.replacePattern (cleanTitle, "*", "_"); 519 cleanTitle = JahiaTools.replacePattern (cleanTitle, "?", "_"); 520 cleanTitle = JahiaTools.replacePattern (cleanTitle, "<", "_"); 521 cleanTitle = JahiaTools.replacePattern (cleanTitle, ">", "_"); 522 cleanTitle = JahiaTools.replacePattern (cleanTitle, "|", "_"); 523 cleanTitle = JahiaTools.replacePattern (cleanTitle, "¦", "_"); 524 cleanTitle = JahiaTools.replacePattern (cleanTitle, "#", "_"); 525 cleanTitle = JahiaTools.replacePattern (cleanTitle, "$", "_"); 526 cleanTitle = JahiaTools.replacePattern (cleanTitle, "\"", "_"); 527 cleanTitle = JahiaTools.replacePattern (cleanTitle, "+", " "); 528 cleanTitle = JahiaTools.replacePattern (cleanTitle, "%", "_"); 529 530 cleanTitle = cleanTitle.trim (); 531 return cleanTitle; 532 } 533 534 private String cleanName (String name) { 535 536 String cleanName = name; 537 cleanName = JahiaTools.replacePattern (cleanName, "+", " "); 538 cleanName = cleanName.trim (); 539 540 return cleanName; 541 } 542 543 } | Popular Tags |