1 31 32 package org.opencms.db; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsProperty; 37 import org.opencms.file.CmsPropertyDefinition; 38 import org.opencms.file.CmsResource; 39 import org.opencms.file.CmsResourceFilter; 40 import org.opencms.file.CmsVfsException; 41 import org.opencms.file.types.CmsResourceTypeFolder; 42 import org.opencms.main.CmsEvent; 43 import org.opencms.main.CmsException; 44 import org.opencms.main.I_CmsEventListener; 45 import org.opencms.main.OpenCms; 46 import org.opencms.util.CmsFileUtil; 47 48 import java.io.ByteArrayInputStream ; 49 import java.io.File ; 50 import java.io.FileInputStream ; 51 import java.io.IOException ; 52 import java.util.ArrayList ; 53 import java.util.Collections ; 54 import java.util.List ; 55 import java.util.StringTokenizer ; 56 import java.util.zip.ZipEntry ; 57 import java.util.zip.ZipInputStream ; 58 59 68 public class CmsImportFolder { 69 70 71 private CmsObject m_cms; 72 73 74 private String m_importFolderName; 75 76 77 private String m_importPath; 78 79 80 private File m_importResource; 81 82 83 private boolean m_validZipFile; 84 85 86 private ZipInputStream m_zipStreamIn; 87 88 97 public CmsImportFolder(byte[] content, String importPath, CmsObject cms, boolean noSubFolder) 98 throws CmsException { 99 100 m_importPath = importPath; 101 m_cms = cms; 102 try { 103 m_zipStreamIn = new ZipInputStream (new ByteArrayInputStream (content)); 105 m_cms.readFolder(importPath, CmsResourceFilter.IGNORE_EXPIRATION); 106 importZipResource(m_zipStreamIn, m_importPath, noSubFolder); 108 } catch (Exception e) { 109 throw new CmsVfsException(Messages.get().container(Messages.ERR_IMPORT_FOLDER_1, importPath), e); 110 } 111 } 112 113 121 public CmsImportFolder(String importFolderName, String importPath, CmsObject cms) 122 throws CmsException { 123 124 try { 125 m_importFolderName = importFolderName; 126 m_importPath = importPath; 127 m_cms = cms; 128 getImportResource(); 130 m_cms.lockResource(m_importPath); 132 if (m_zipStreamIn == null) { 134 importResources(m_importResource, m_importPath); 135 } else { 136 importZipResource(m_zipStreamIn, m_importPath, false); 137 } 138 m_cms.unlockResource(m_importPath); 140 } catch (Exception e) { 141 throw new CmsVfsException(Messages.get().container( 142 Messages.ERR_IMPORT_FOLDER_2, 143 importFolderName, 144 importPath), e); 145 } 146 } 147 148 153 public boolean isValidZipFile() { 154 155 return m_validZipFile; 156 } 157 158 162 private void getImportResource() throws CmsVfsException { 163 164 m_importResource = new File(m_importFolderName); 166 if (m_importResource.isFile()) { 168 try { 169 m_zipStreamIn = new ZipInputStream (new FileInputStream (m_importResource)); 170 } catch (IOException e) { 171 throw new CmsVfsException(Messages.get().container( 173 Messages.ERR_NO_ZIPFILE_1, 174 m_importResource.getName()), e); 175 } 176 } 177 } 178 179 186 private void importResources(File folder, String importPath) throws Exception { 187 188 String [] diskFiles = folder.list(); 189 File currentFile; 190 191 for (int i = 0; i < diskFiles.length; i++) { 192 currentFile = new File(folder, diskFiles[i]); 193 194 if (currentFile.isDirectory()) { 195 m_cms.createResource(importPath + currentFile.getName(), CmsResourceTypeFolder.RESOURCE_TYPE_ID); 197 importResources(currentFile, importPath + currentFile.getName() + "/"); 198 } else { 199 int type = OpenCms.getResourceManager().getDefaultTypeForName(currentFile.getName()).getTypeId(); 201 byte[] content = CmsFileUtil.readFile(currentFile); 202 m_cms.createResource(importPath + currentFile.getName(), type, content, null); 204 content = null; 205 } 206 } 207 } 208 209 217 private void importZipResource(ZipInputStream zipStreamIn, String importPath, boolean noSubFolder) throws Exception { 218 219 int todo = 0; 220 222 boolean isFolder = false; 223 int j, r, stop, size; 224 int entries = 0; 225 byte[] buffer = null; 226 boolean resourceExists; 227 228 while (true) { 229 j = 0; 231 stop = 0; 232 ZipEntry entry = zipStreamIn.getNextEntry(); 234 if (entry == null) { 235 break; 236 } 237 entries++; String actImportPath = importPath; 239 String title = CmsResource.getName(entry.getName()); 240 String filename = m_cms.getRequestContext().getFileTranslator().translateResource(entry.getName()); 241 StringTokenizer st = new StringTokenizer (filename, "/\\"); 243 int count = st.countTokens(); 244 String [] path = new String [count]; 245 246 if (filename.endsWith("\\") || filename.endsWith("/")) { 247 isFolder = true; } else { 249 isFolder = false; } 251 while (st.hasMoreTokens()) { 252 path[j] = st.nextToken(); 254 j++; 255 } 256 stop = isFolder ? path.length : (path.length - 1); 257 258 if (noSubFolder) { 259 stop = 0; 260 } 261 for (r = 0; r < stop; r++) { 263 try { 264 m_cms.createResource(actImportPath + path[r], CmsResourceTypeFolder.RESOURCE_TYPE_ID); 265 } catch (CmsException e) { 266 } 268 actImportPath += path[r]; 269 actImportPath += "/"; 270 } 271 if (!isFolder) { 272 int type = OpenCms.getResourceManager().getDefaultTypeForName(path[path.length - 1]).getTypeId(); 274 size = new Long (entry.getSize()).intValue(); 275 if (size == -1) { 276 buffer = CmsFileUtil.readFully(zipStreamIn, false); 277 } else { 278 buffer = CmsFileUtil.readFully(zipStreamIn, size, false); 279 } 280 filename = actImportPath + path[path.length - 1]; 281 282 try { 283 m_cms.lockResource(filename); 284 285 m_cms.readResource(filename); 286 resourceExists = true; 287 } catch (CmsException e) { 288 resourceExists = false; 289 } 290 291 if (resourceExists) { 292 CmsResource res = m_cms.readResource(filename, CmsResourceFilter.ALL); 293 CmsFile file = CmsFile.upgrade(res, m_cms); 294 byte[] contents = file.getContents(); 295 try { 296 m_cms.replaceResource(filename, res.getTypeId(), buffer, Collections.EMPTY_LIST); 297 } catch (CmsDbSqlException sqlExc) { 298 file.setContents(contents); 300 m_cms.writeFile(file); 301 throw sqlExc; 302 } 303 304 OpenCms.fireCmsEvent(new CmsEvent( 305 I_CmsEventListener.EVENT_RESOURCE_AND_PROPERTIES_MODIFIED, 306 Collections.singletonMap("resource", res))); 307 } else { 308 String newResName = actImportPath + path[path.length - 1]; 309 if (title.lastIndexOf('.') != -1) { 310 title = title.substring(0, title.lastIndexOf('.')); 311 } 312 List properties = new ArrayList (1); 313 CmsProperty titleProp = new CmsProperty(); 314 titleProp.setName(CmsPropertyDefinition.PROPERTY_TITLE); 315 if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) { 316 titleProp.setStructureValue(title); 317 } else { 318 titleProp.setResourceValue(title); 319 } 320 properties.add(titleProp); 321 try { 322 m_cms.createResource(newResName, type, buffer, properties); 323 } catch (CmsDbSqlException sqlExc) { 324 m_cms.lockResource(newResName); 326 m_cms.deleteResource(newResName, CmsResource.DELETE_PRESERVE_SIBLINGS); 327 throw sqlExc; 328 } 329 } 330 } 331 332 zipStreamIn.closeEntry(); 334 } 335 zipStreamIn.close(); 336 if (entries > 0) { 337 m_validZipFile = true; 339 } 340 } 341 } 342
| Popular Tags
|