1 13 package org.jahia.services.filemanager; 14 15 16 import org.jahia.data.files.JahiaFile; 17 import org.jahia.registries.ServicesRegistry; 18 19 import java.util.Hashtable ; 20 import java.util.Vector ; 21 22 29 public class Folder { 30 31 private static org.apache.log4j.Logger logger = 32 org.apache.log4j.Logger.getLogger (Folder.class); 33 34 35 private int m_FolderID = -1; 36 37 private int m_FilemanagerID = -1; 38 39 private int m_ParentID = -1; 40 41 private int m_Depth = -1; 42 43 private String m_FolderName = ""; 44 45 46 private FolderDB m_FolderDB; 47 48 49 52 protected Folder () { 53 } 54 55 62 public Folder ( 63 int filemanagerID, 64 int parentID, 65 String folderName) { 66 67 m_FilemanagerID = filemanagerID; 68 m_ParentID = parentID; 69 m_FolderName = folderName; 70 } 71 72 public int getFolderID () { 73 return m_FolderID; 74 } 75 76 protected void setFolderID (int id) { 77 m_FolderID = id; 78 } 79 80 public int getFilemanagerID () { 81 return m_FilemanagerID; 82 } 83 84 protected void setFilemanager (int id) { 85 m_FilemanagerID = id; 86 } 87 88 89 public int getParentID () { 90 return m_ParentID; 91 } 92 93 public void setParentID (int id) { 94 m_ParentID = id; 95 this.setDepth (this.getParent ().getDepth () + 1); 96 } 97 98 public int getDepth () { 99 return m_Depth; 100 } 101 102 protected void setDepth (int depth) { 103 m_Depth = depth; 104 } 105 106 public String getFolderName () { 107 return m_FolderName; 108 } 109 110 public void setFolderName (String folderName) { 111 m_FolderName = folderName; 112 } 113 114 115 120 public Folder getParent () { 121 return FolderDB.getInstance ().getFolder (this.getParentID ()); 122 } 123 124 125 129 public void getParents (Hashtable parents) { 130 131 parents.put (new Integer (this.getFolderID ()), new Integer (this.getFolderID ())); 132 Folder parent = FolderDB.getInstance ().getFolder (this.getParentID ()); 133 if (parent != null) { 134 logger.debug ("getParents():" + parent.getFolderID ()); 135 parent.getParents (parents); 137 } 138 } 139 140 144 public Vector getFiles () { 145 146 Vector files = null; 147 148 String orderSql = " order by title_jahia_file"; 150 files = 151 ServicesRegistry.getInstance ().getJahiaFilemanagerService ().getFilesByFolderDB ( 152 this.getFolderID (), orderSql); 153 return files; 154 } 155 156 157 165 public boolean deleteFiles (String [] fileIDs) { 166 167 boolean success = true; 168 169 Vector files = this.getFiles (); 171 if (files != null) { 172 JahiaFile aFile = null; 173 int nbFile = files.size (); 174 175 for (int i = 0; i < nbFile; i++) { 176 177 aFile = (JahiaFile) files.get (i); 178 if (inValues (String.valueOf (aFile.getFileID ()), fileIDs)) { 179 if (!ServicesRegistry.getInstance ().getJahiaFilemanagerService () 180 .deleteFileDB (aFile.getFileID ())) { 181 success = false; 182 break; 183 } 184 } 185 } 186 } 187 return success; 188 } 189 190 191 198 public boolean addFolder (String folderName) { 199 200 if (folderName.length () > 0) { 201 202 Folder folder = new Folder (this.getFilemanagerID (), 204 this.getFolderID (), folderName 206 ); 207 208 if (!FolderDB.getInstance ().insert (folder)) { 209 logger.debug ("addFolder, error create new Folder in db"); 210 return false; 211 } 212 } 213 return true; 214 } 215 216 225 public boolean moveFiles (Folder destFolder, String [] fileIDs) { 226 227 logger.debug (" moveFiles started "); 228 229 boolean success = true; 230 231 if (fileIDs != null && destFolder != null) { 232 233 Vector files = this.getFiles (); 235 JahiaFile aFile = null; 236 237 if (files != null) { 238 239 int nbFile = files.size (); 240 241 for (int i = 0; i < nbFile; i++) { 242 243 aFile = (JahiaFile) files.get (i); 244 if (inValues (String.valueOf (aFile.getFileID ()), fileIDs)) { 245 aFile.setFolderID (destFolder.getFolderID ()); 246 if (!(ServicesRegistry.getInstance ().getJahiaFilemanagerService () 247 .updateFileDB (aFile, aFile.getVersion ()))) { 248 logger.debug ("moveFiles, error updating files info"); 250 success = false; 251 break; 252 } 253 } 254 } 255 } 256 } else { 257 success = false; 258 } 259 260 return success; 261 262 } 263 264 265 273 public boolean renameFolder (String folderName) { 274 275 logger.debug (" renameFolder started "); 276 277 if (folderName.length () > 0) { 278 279 this.setFolderName (folderName); 280 if (!FolderDB.getInstance ().update (this)) { 281 return false; 282 } 283 } 284 return true; 285 } 286 287 288 297 protected boolean inValues (String aValue, String [] values) { 298 if (values != null) { 299 for (int i = 0; i < values.length; i++) { 300 if (aValue.equals (values[i])) { 301 return true; 302 } 303 } 304 } 305 return false; 306 } 307 308 } 309 | Popular Tags |