1 33 34 package com.opencms.legacy; 35 36 import org.opencms.file.CmsObject; 37 import org.opencms.file.CmsResource; 38 import org.opencms.i18n.CmsEncoder; 39 import org.opencms.i18n.CmsMessageContainer; 40 import org.opencms.importexport.CmsImport; 41 import org.opencms.importexport.CmsImportExportException; 42 import org.opencms.importexport.I_CmsImport; 43 import org.opencms.main.CmsException; 44 import org.opencms.main.CmsLog; 45 import org.opencms.main.OpenCms; 46 import org.opencms.report.I_CmsReport; 47 import org.opencms.util.CmsUUID; 48 import org.opencms.xml.CmsXmlException; 49 50 import com.opencms.defaults.master.*; 51 52 import java.io.File ; 53 import java.io.FileInputStream ; 54 import java.io.InputStream ; 55 import java.io.Serializable ; 56 import java.lang.reflect.Constructor ; 57 import java.lang.reflect.InvocationTargetException ; 58 import java.text.ParseException ; 59 import java.text.SimpleDateFormat ; 60 import java.util.Hashtable ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.Vector ; 64 import java.util.zip.ZipEntry ; 65 66 import org.apache.commons.logging.Log; 67 68 import org.dom4j.Document; 69 import org.dom4j.Element; 70 71 83 public class CmsImportModuledata extends CmsImport implements Serializable { 84 85 86 private static final long serialVersionUID = 2474902557481830457L; 87 88 89 private static final Log LOG = CmsLog.getLog(CmsImportModuledata.class); 90 91 100 public CmsImportModuledata(CmsObject cms, String importFile, String importPath, I_CmsReport report) { 101 102 m_cms = cms; 104 m_importFile = importFile; 105 m_importPath = importPath; 106 m_report = report; 107 m_importingChannelData = true; 108 m_importImplementations = OpenCms.getImportExportManager().getImportVersionClasses(); 109 } 110 111 116 public void importModuleMasters() throws CmsException { 117 118 Iterator it = CmsLegacyModuleAction.getLegacyModulePublishClasses().iterator(); 120 121 Hashtable availableModules = new Hashtable (); 123 while (it.hasNext()) { 124 String classname = (String )it.next(); 125 try { 127 int subId = getContentDefinition(classname, new Class [] {CmsObject.class}, new Object [] {m_cms}).getSubId(); 128 availableModules.put("" + subId, classname); 130 } catch (Exception e) { 131 } 133 134 } 135 List masterNodes; 137 Element currentMasterElement; 138 String subid; 139 140 try { 141 masterNodes = m_docXml.selectNodes("//" + CmsExportModuledata.C_EXPORT_TAG_MASTER); 143 int length = masterNodes.size(); 144 145 for (int i = 0; i < length; i++) { 147 currentMasterElement = (Element)masterNodes.get(i); 148 subid = CmsImport.getChildElementTextValue( 150 currentMasterElement, 151 CmsExportModuledata.C_EXPORT_TAG_MASTER_SUBID); 152 String classname = (String )availableModules.get(subid); 154 if ((classname != null) && !("".equals(classname.trim()))) { 155 m_report.print(org.opencms.report.Messages.get().container( 157 org.opencms.report.Messages.RPT_SUCCESSION_2, 158 String.valueOf(i + 1), 159 String.valueOf(length)), I_CmsReport.FORMAT_NOTE); 160 importMaster(subid, classname, currentMasterElement); 161 } 162 } 163 } catch (Exception exc) { 164 throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, exc); 165 } 166 } 167 168 175 public synchronized void importResources() throws CmsImportExportException, CmsXmlException { 176 177 openImportFile(); 179 m_report.println( 180 Messages.get().container(Messages.RPT_IMPORT_VERSION_1, new Integer (m_importVersion)), 181 I_CmsReport.FORMAT_NOTE); 182 try { 183 m_report.println( 185 Messages.get().container(Messages.RPT_IMPORT_CHANNELS_BEGIN_0), 186 I_CmsReport.FORMAT_HEADLINE); 187 m_cms.getRequestContext().saveSiteRoot(); 190 m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 191 Iterator i = m_importImplementations.iterator(); 192 while (i.hasNext()) { 193 I_CmsImport imp = (I_CmsImport)i.next(); 194 if (imp.getVersion() == m_importVersion) { 195 imp.importResources( 197 m_cms, 198 m_importPath, 199 m_report, 200 m_importResource, 201 m_importZip, 202 m_docXml); 203 break; 204 } 205 } 206 m_cms.getRequestContext().restoreSiteRoot(); 207 m_report.println( 208 Messages.get().container(Messages.RPT_IMPORT_CHANNELS_END_0), 209 I_CmsReport.FORMAT_HEADLINE); 210 211 m_report.println( 213 Messages.get().container(Messages.RPT_IMPORT_MODULE_BEGIN_0), 214 I_CmsReport.FORMAT_HEADLINE); 215 importModuleMasters(); 216 m_report.println(Messages.get().container(Messages.RPT_IMPORT_MODULE_END_0), I_CmsReport.FORMAT_HEADLINE); 217 } catch (CmsXmlException e) { 218 219 throw e; 220 } catch (CmsImportExportException e) { 221 222 throw e; 223 } catch (CmsException e) { 224 m_report.println(e); 225 226 CmsMessageContainer message = Messages.get().container( 227 Messages.ERR_COS_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0); 228 if (LOG.isDebugEnabled()) { 229 LOG.debug(message.key(), e); 230 } 231 232 throw new CmsImportExportException(message, e); 233 } finally { 234 closeImportFile(); 236 } 237 } 238 239 247 protected CmsMasterContent getContentDefinition(String classname, Class [] classes, Object [] objects) { 248 249 CmsMasterContent cd = null; 250 try { 251 Class cdClass = Class.forName(classname); 252 Constructor co = cdClass.getConstructor(classes); 253 cd = (CmsMasterContent)co.newInstance(objects); 254 } catch (InvocationTargetException ite) { 255 if (CmsLog.getLog(this).isWarnEnabled()) { 256 CmsLog.getLog(this).warn("Invocation target exception", ite); 257 } 258 } catch (NoSuchMethodException nsm) { 259 if (CmsLog.getLog(this).isWarnEnabled()) { 260 CmsLog.getLog(this).warn("Requested method was not found", nsm); 261 } 262 } catch (InstantiationException ie) { 263 if (CmsLog.getLog(this).isWarnEnabled()) { 264 CmsLog.getLog(this).warn("The reflected class is abstract", ie); 265 } 266 } catch (Exception e) { 267 if (CmsLog.getLog(this).isWarnEnabled()) { 268 CmsLog.getLog(this).warn("Other exception", e); 269 } 270 } 271 return cd; 272 } 273 274 280 protected Vector getMasterChannelRelation(Element masterElement) { 281 282 Vector channelRelations = new Vector (); 283 List channelNodes = masterElement.selectNodes("*/" + CmsExportModuledata.C_EXPORT_TAG_MASTER_CHANNELNAME); 285 286 for (int j = 0; j < channelNodes.size(); j++) { 288 String channelName = ((Element)channelNodes.get(j)).getTextTrim(); 290 if ((channelName != null) && !("".equals(channelName.trim()))) { 292 channelRelations.addElement(channelName); 293 } 294 } 295 return channelRelations; 296 } 297 298 306 protected CmsMasterDataSet getMasterDataSet(int subId, Element masterElement) throws CmsException { 307 308 String datasetfile = null, username = null, groupname = null, accessFlags = null, publicationDate = null, purgeDate = null, creationDate = null, flags = null, feedId = null, feedReference = null, feedFilename = null, title = null, master_id = null; 309 310 CmsMasterDataSet newDataset = new CmsMasterDataSet(); 311 312 datasetfile = ((Element)masterElement.selectNodes("./" + CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASET).get(0)).getTextTrim(); 314 315 Document datasetXml = CmsImportVersion1.getXmlDocument(getFileInputStream(datasetfile)); 316 Element dataset = (Element)datasetXml.getRootElement().selectNodes( 317 "./" + CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASET).get(0); 318 319 newDataset.m_subId = subId; 322 323 master_id = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_ID); 324 if (master_id != null) { 325 newDataset.m_masterId = new CmsUUID(master_id); 326 } else { 327 newDataset.m_masterId = CmsUUID.getNullUUID(); 328 } 329 330 username = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_USER); 332 CmsUUID userId = null; 333 try { 334 if ((username != null) && !("".equals(username.trim()))) { 335 userId = m_cms.readUser(username).getId(); 336 } 337 } catch (Exception e) { 338 userId = m_cms.getRequestContext().currentUser().getId(); 340 } 341 342 newDataset.m_userId = userId; 343 groupname = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_GROUP); 345 346 CmsUUID groupId = CmsUUID.getNullUUID(); 347 try { 348 if ((groupname != null) && !("".equals(groupname.trim()))) { 349 groupId = m_cms.readGroup(groupname).getId(); 350 } 351 } catch (Exception e) { 352 try { 353 groupId = m_cms.readGroup(OpenCms.getDefaultUsers().getGroupUsers()).getId(); 354 } catch (Exception e2) { 355 } 357 } 358 359 newDataset.m_groupId = groupId; 360 accessFlags = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_ACCESSFLAGS); 362 try { 363 newDataset.m_accessFlags = Integer.parseInt(accessFlags); 364 } catch (Exception e) { 365 newDataset.m_accessFlags = com.opencms.core.I_CmsConstants.C_ACCESS_DEFAULT_FLAGS; 366 } 367 publicationDate = CmsImport.getChildElementTextValue( 369 dataset, 370 CmsExportModuledata.C_EXPORT_TAG_MASTER_PUBLICATIONDATE); 371 try { 372 newDataset.m_publicationDate = convertDate(publicationDate); 373 } catch (Exception e) { 374 } 376 purgeDate = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_PURGEDATE); 378 try { 379 newDataset.m_purgeDate = convertDate(purgeDate); 380 } catch (Exception e) { 381 } 383 try { 385 creationDate = CmsImport.getChildElementTextValue( 386 dataset, 387 CmsExportModuledata.C_EXPORT_TAG_MASTER_CREATEDATE); 388 newDataset.m_dateCreated = convertDate(creationDate); 389 } catch (Exception e) { 390 } 392 flags = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FLAGS); 394 try { 395 newDataset.m_flags = Integer.parseInt(flags); 396 } catch (Exception e) { 397 } 399 feedId = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FEEDID); 401 try { 402 newDataset.m_feedId = Integer.parseInt(feedId); 403 } catch (Exception e) { 404 } 406 feedReference = CmsImport.getChildElementTextValue( 408 dataset, 409 CmsExportModuledata.C_EXPORT_TAG_MASTER_FEEDREFERENCE); 410 try { 411 newDataset.m_feedReference = Integer.parseInt(feedReference); 412 } catch (Exception e) { 413 } 415 feedFilename = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FEEDFILENAME); 417 newDataset.m_feedFilename = feedFilename; 418 title = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_TITLE); 420 newDataset.m_title = title; 421 for (int i = 0; i < newDataset.m_dataBig.length; i++) { 423 String filename = CmsImport.getChildElementTextValue( 424 dataset, 425 CmsExportModuledata.C_EXPORT_TAG_MASTER_DATABIG + i); 426 String value = new String (); 427 if (filename != null && !"".equals(filename.trim())) { 428 value = new String (getFileBytes(filename)); 430 } 431 newDataset.m_dataBig[i] = value; 432 } 433 for (int i = 0; i < newDataset.m_dataMedium.length; i++) { 435 String filename = CmsImport.getChildElementTextValue( 436 dataset, 437 CmsExportModuledata.C_EXPORT_TAG_MASTER_DATAMEDIUM + i); 438 String value = new String (); 439 if (filename != null && !"".equals(filename.trim())) { 440 value = new String (getFileBytes(filename)); 442 } 443 newDataset.m_dataMedium[i] = value; 444 } 445 for (int i = 0; i < newDataset.m_dataSmall.length; i++) { 447 String filename = CmsImport.getChildElementTextValue( 448 dataset, 449 CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASMALL + i); 450 String value = new String (); 451 if (filename != null && !"".equals(filename.trim())) { 452 value = new String (getFileBytes(filename)); 454 } 455 newDataset.m_dataSmall[i] = value; 456 } 457 for (int i = 0; i < newDataset.m_dataInt.length; i++) { 459 String value = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATAINT 460 + i); 461 try { 462 newDataset.m_dataInt[i] = new Integer (value).intValue(); 463 } catch (Exception e) { 464 newDataset.m_dataInt[i] = 0; 465 } 466 } 467 for (int i = 0; i < newDataset.m_dataReference.length; i++) { 469 String value = CmsImport.getChildElementTextValue( 470 dataset, 471 CmsExportModuledata.C_EXPORT_TAG_MASTER_DATAREFERENCE + i); 472 try { 473 newDataset.m_dataReference[i] = new Integer (value).intValue(); 474 } catch (Exception e) { 475 newDataset.m_dataReference[i] = 0; 476 } 477 } 478 for (int i = 0; i < newDataset.m_dataDate.length; i++) { 480 String value = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATADATE 481 + i); 482 try { 483 newDataset.m_dataDate[i] = convertDate(value); 484 } catch (Exception e) { 485 newDataset.m_dataDate[i] = 0; 486 } 487 } 488 return newDataset; 489 } 490 491 498 protected Vector getMasterMedia(Element masterElement) throws CmsException { 499 500 Vector masterMedia = new Vector (); 501 List mediaNodes = masterElement.selectNodes("*/" + CmsExportModuledata.C_EXPORT_TAG_MASTER_MEDIA); 503 for (int j = 0; j < mediaNodes.size(); j++) { 505 String mediaFilename = ((Element)mediaNodes.get(j)).getTextTrim(); 507 if ((mediaFilename != null) && !("".equals(mediaFilename.trim()))) { 509 CmsMasterMedia newMedia = getMediaData(mediaFilename); 510 masterMedia.add(newMedia); 511 } 512 } 513 return masterMedia; 514 } 515 516 524 protected void importMaster(String subId, String classname, Element masterElement) throws CmsException { 525 526 CmsMasterDataSet newDataset = new CmsMasterDataSet(); 527 Vector channelRelations = new Vector (); 528 Vector masterMedia = new Vector (); 529 530 m_report.print(org.opencms.importexport.Messages.get().container( 531 org.opencms.importexport.Messages.RPT_IMPORTING_0), I_CmsReport.FORMAT_NOTE); 532 533 try { 535 int subIdInt = Integer.parseInt(subId); 536 newDataset = getMasterDataSet(subIdInt, masterElement); 537 } catch (Exception e) { 538 m_report.println(e); 539 throw new CmsLegacyException("Cannot get dataset ", e); 540 } 541 542 m_report.print(Messages.get().container( 543 Messages.RPT_ARGUMENT_2, 544 CmsEncoder.escapeHtml(newDataset.m_title), 545 classname)); 546 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 547 548 try { 550 channelRelations = getMasterChannelRelation(masterElement); 551 } catch (Exception e) { 552 m_report.println(e); 553 throw new CmsLegacyException("Cannot get channelrelations ", e); 554 } 555 556 try { 558 masterMedia = getMasterMedia(masterElement); 559 } catch (Exception e) { 560 m_report.println(e); 561 throw new CmsLegacyException("Cannot get media ", e); 562 } 563 564 newDataset.m_channelToAdd = channelRelations; 566 newDataset.m_mediaToAdd = masterMedia; 567 CmsMasterContent newMaster = getContentDefinition(classname, new Class [] { 569 CmsObject.class, 570 CmsMasterDataSet.class}, new Object [] {m_cms, newDataset}); 571 572 try { 573 CmsUUID userId = newMaster.getOwner(); 574 CmsUUID groupId = newMaster.getGroupId(); 575 newMaster.importMaster(); 577 newMaster.chown(m_cms, userId); 579 newMaster.chgrp(m_cms, groupId); 580 } catch (Exception e) { 581 m_report.println(e); 582 throw new CmsLegacyException("Cannot write master ", e); 583 } 584 585 m_report.println( 586 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 587 I_CmsReport.FORMAT_OK); 588 } 589 590 596 private long convertDate(String date) { 597 598 java.text.SimpleDateFormat formatterFullTime = new SimpleDateFormat ("M/d/yy h:mm a"); 599 long adate = 0; 600 try { 601 adate = formatterFullTime.parse(date).getTime(); 602 } catch (ParseException e) { 603 java.text.SimpleDateFormat formatterFullTimeDe = new SimpleDateFormat ("dd.MM.yyyy HH:mm"); 604 try { 605 adate = formatterFullTimeDe.parse(date).getTime(); 606 } catch (ParseException e2) { 607 } 609 } 610 611 return adate; 612 } 613 614 621 private InputStream getFileInputStream(String filename) throws CmsException { 622 623 try { 624 if (m_importZip != null) { 626 ZipEntry entry = m_importZip.getEntry(filename); 628 return m_importZip.getInputStream(entry); 629 } else { 630 File xmlFile = new File (m_importResource, filename); 632 return new FileInputStream (xmlFile); 633 } 634 } catch (Exception e) { 635 throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e); 636 } 637 } 638 639 646 private CmsMasterMedia getMediaData(String mediaFilename) throws CmsException { 647 648 String position = null, width = null, height = null, size = null, mimetype = null, type = null, title = null, name = null, description = null, contentfile = null; 649 CmsMasterMedia newMedia = null; 650 Document mediaXml = null; 651 Element rootElement = null; 652 byte[] mediacontent = null; 653 654 newMedia = new CmsMasterMedia(); 655 mediaXml = CmsImportVersion1.getXmlDocument(getFileInputStream(mediaFilename)); 656 rootElement = mediaXml.getRootElement(); 657 658 position = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_POSITION).get( 659 0)).getTextTrim(); 660 try { 661 newMedia.setPosition(Integer.parseInt(position)); 662 } catch (Exception e) { 663 } 665 666 width = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_WIDTH).get(0)).getTextTrim(); 667 try { 668 newMedia.setWidth(Integer.parseInt(width)); 669 } catch (Exception e) { 670 } 672 673 height = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_HEIGHT).get(0)).getTextTrim(); 674 try { 675 newMedia.setHeight(Integer.parseInt(height)); 676 } catch (Exception e) { 677 } 679 680 size = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_SIZE).get(0)).getTextTrim(); 681 try { 682 newMedia.setSize(Integer.parseInt(size)); 683 } catch (Exception e) { 684 } 686 687 mimetype = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_MIMETYPE).get( 688 0)).getTextTrim(); 689 newMedia.setMimetype(mimetype); 690 691 type = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_TYPE).get(0)).getTextTrim(); 692 try { 693 newMedia.setType(Integer.parseInt(type)); 694 } catch (Exception e) { 695 } 697 698 title = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_TITLE).get(0)).getTextTrim(); 699 newMedia.setTitle(title); 700 701 name = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_NAME).get(0)).getTextTrim(); 702 newMedia.setName(name); 703 704 description = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_DESCRIPTION).get( 705 0)).getTextTrim(); 706 newMedia.setDescription(description); 707 708 contentfile = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_CONTENT).get( 709 0)).getTextTrim(); 710 try { 711 mediacontent = getFileBytes(contentfile); 712 } catch (Exception e) { 713 m_report.println(e); 714 } 715 newMedia.setMedia(mediacontent); 716 717 return newMedia; 718 } 719 } 720 | Popular Tags |