📄 cmsimportmoduledata.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/legacy/CmsImportModuledata.java,v $
* Date : $Date: 2006/03/27 14:53:03 $
* Version: $Revision: 1.18 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2002 Alkacon Software (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.legacy;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsMessageContainer;
import org.opencms.importexport.CmsImport;
import org.opencms.importexport.CmsImportExportException;
import org.opencms.importexport.I_CmsImport;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.report.I_CmsReport;
import org.opencms.util.CmsUUID;
import org.opencms.xml.CmsXmlException;
import com.opencms.defaults.master.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.zip.ZipEntry;
import org.apache.commons.logging.Log;
import org.dom4j.Document;
import org.dom4j.Element;
/**
* Holds the functionaility to import resources from the filesystem
* or a zip file into the OpenCms COS.
*
* @author Alexander Kandzior (a.kandzior@alkacon.com)
* @author Michael Emmerich (m.emmerich@alkacon.com)
* @author Thomas Weckert (t.weckert@alkacon.com)
*
* @version $Revision: 1.18 $ $Date: 2006/03/27 14:53:03 $
*
* @deprecated Will not be supported past the OpenCms 6 release.
*/
public class CmsImportModuledata extends CmsImport implements Serializable {
/** Serial version UID required for safe serialization. */
private static final long serialVersionUID = 2474902557481830457L;
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsImportModuledata.class);
/**
* Constructs a new import object which imports the module data from an OpenCms
* export zip file or a folder in the "real" file system.<p>
*
* @param cms the current cms object
* @param importFile the file or folder to import from
* @param importPath the path in the cms VFS to import into
* @param report a report object to output the progress information to
*/
public CmsImportModuledata(CmsObject cms, String importFile, String importPath, I_CmsReport report) {
// set member variables
m_cms = cms;
m_importFile = importFile;
m_importPath = importPath;
m_report = report;
m_importingChannelData = true;
m_importImplementations = OpenCms.getImportExportManager().getImportVersionClasses();
}
/**
* Gets the available modules in the current system
* and imports the data for existing modules.<p>
* @throws CmsException in case something goes wrong
*/
public void importModuleMasters() throws CmsException {
// get list of legacy modules that have a publish class
Iterator it = CmsLegacyModuleAction.getLegacyModulePublishClasses().iterator();
// now get the subIds of each module
Hashtable availableModules = new Hashtable();
while (it.hasNext()) {
String classname = (String)it.next();
// get the subId of the module
try {
int subId = getContentDefinition(classname, new Class[] {CmsObject.class}, new Object[] {m_cms}).getSubId();
// put the subid and the classname into the hashtable of available modules
availableModules.put("" + subId, classname);
} catch (Exception e) {
// do nothing
}
}
// now get the moduledata for import
List masterNodes;
Element currentMasterElement;
String subid;
try {
// get all master-nodes
masterNodes = m_docXml.selectNodes("//" + CmsExportModuledata.C_EXPORT_TAG_MASTER);
int length = masterNodes.size();
// walk through all files in manifest
for (int i = 0; i < length; i++) {
currentMasterElement = (Element)masterNodes.get(i);
// get the subid of the modulemaster
subid = CmsImport.getChildElementTextValue(
currentMasterElement,
CmsExportModuledata.C_EXPORT_TAG_MASTER_SUBID);
// check if there exists a module with this subid
String classname = (String)availableModules.get(subid);
if ((classname != null) && !("".equals(classname.trim()))) {
// import the dataset, the channelrelation and the media
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_SUCCESSION_2,
String.valueOf(i + 1),
String.valueOf(length)), I_CmsReport.FORMAT_NOTE);
importMaster(subid, classname, currentMasterElement);
}
}
} catch (Exception exc) {
throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, exc);
}
}
/**
* Imports the moduledata and writes them to the cms even if there already exist
* conflicting files.<p>
*
* @throws CmsImportExportException if something goes wrong
* @throws CmsXmlException if the manifest of the import could not be unmarshalled
*/
public synchronized void importResources() throws CmsImportExportException, CmsXmlException {
// initialize the import
openImportFile();
m_report.println(
Messages.get().container(Messages.RPT_IMPORT_VERSION_1, new Integer(m_importVersion)),
I_CmsReport.FORMAT_NOTE);
try {
// first import the channels
m_report.println(
Messages.get().container(Messages.RPT_IMPORT_CHANNELS_BEGIN_0),
I_CmsReport.FORMAT_HEADLINE);
//importAllResources(null, null, null, null, null);
// now find the correct import implementation
m_cms.getRequestContext().saveSiteRoot();
m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
Iterator i = m_importImplementations.iterator();
while (i.hasNext()) {
I_CmsImport imp = (I_CmsImport)i.next();
if (imp.getVersion() == m_importVersion) {
// this is the correct import version, so call it for the import process
imp.importResources(
m_cms,
m_importPath,
m_report,
m_importResource,
m_importZip,
m_docXml);
break;
}
}
m_cms.getRequestContext().restoreSiteRoot();
m_report.println(
Messages.get().container(Messages.RPT_IMPORT_CHANNELS_END_0),
I_CmsReport.FORMAT_HEADLINE);
// now import the moduledata
m_report.println(
Messages.get().container(Messages.RPT_IMPORT_MODULE_BEGIN_0),
I_CmsReport.FORMAT_HEADLINE);
importModuleMasters();
m_report.println(Messages.get().container(Messages.RPT_IMPORT_MODULE_END_0), I_CmsReport.FORMAT_HEADLINE);
} catch (CmsXmlException e) {
throw e;
} catch (CmsImportExportException e) {
throw e;
} catch (CmsException e) {
m_report.println(e);
CmsMessageContainer message = Messages.get().container(
Messages.ERR_COS_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
} finally {
// close the import file
closeImportFile();
}
}
/**
* Gets the content definition class method constructor.<p>
*
* @param classname the name of the cd class
* @param classes types needed for cd constructor
* @param objects objects needed for cd constructor
* @return content definition object
*/
protected CmsMasterContent getContentDefinition(String classname, Class[] classes, Object[] objects) {
CmsMasterContent cd = null;
try {
Class cdClass = Class.forName(classname);
Constructor co = cdClass.getConstructor(classes);
cd = (CmsMasterContent)co.newInstance(objects);
} catch (InvocationTargetException ite) {
if (CmsLog.getLog(this).isWarnEnabled()) {
CmsLog.getLog(this).warn("Invocation target exception", ite);
}
} catch (NoSuchMethodException nsm) {
if (CmsLog.getLog(this).isWarnEnabled()) {
CmsLog.getLog(this).warn("Requested method was not found", nsm);
}
} catch (InstantiationException ie) {
if (CmsLog.getLog(this).isWarnEnabled()) {
CmsLog.getLog(this).warn("The reflected class is abstract", ie);
}
} catch (Exception e) {
if (CmsLog.getLog(this).isWarnEnabled()) {
CmsLog.getLog(this).warn("Other exception", e);
}
}
return cd;
}
/**
* Gets the channel relations for the master from the xml file.<p>
*
* @param masterElement the current element of the xml file
* @return vector containing the ids of all channels of the master
*/
protected Vector getMasterChannelRelation(Element masterElement) {
Vector channelRelations = new Vector();
// get the channelnames of the master
List channelNodes = masterElement.selectNodes("*/" + CmsExportModuledata.C_EXPORT_TAG_MASTER_CHANNELNAME);
// walk through all channelrelations
for (int j = 0; j < channelNodes.size(); j++) {
// get the name of the channel
String channelName = ((Element)channelNodes.get(j)).getTextTrim();
// try to read the channel and get its channelid
if ((channelName != null) && !("".equals(channelName.trim()))) {
channelRelations.addElement(channelName);
}
}
return channelRelations;
}
/**
* Gets the dataset for the master from the xml file.<p>
*
* @param subId the subid of the module
* @param masterElement the current element of the xml file
* @return the dataset with the imported information
* @throws CmsException in case something goes wrong
*/
protected CmsMasterDataSet getMasterDataSet(int subId, Element masterElement) throws CmsException {
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;
CmsMasterDataSet newDataset = new CmsMasterDataSet();
// get the file with the dataset of the master
datasetfile = ((Element)masterElement.selectNodes("./" + CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASET).get(0)).getTextTrim();
Document datasetXml = CmsImportVersion1.getXmlDocument(getFileInputStream(datasetfile));
Element dataset = (Element)datasetXml.getRootElement().selectNodes(
"./" + CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASET).get(0);
// get the information from the dataset and add it to the dataset
// first add the subid
newDataset.m_subId = subId;
master_id = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_ID);
if (master_id != null) {
newDataset.m_masterId = new CmsUUID(master_id);
} else {
newDataset.m_masterId = CmsUUID.getNullUUID();
}
// get the id of the user or set the owner to the current user
username = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_USER);
CmsUUID userId = null;
try {
if ((username != null) && !("".equals(username.trim()))) {
userId = m_cms.readUser(username).getId();
}
} catch (Exception e) {
// userId will be current user
userId = m_cms.getRequestContext().currentUser().getId();
}
newDataset.m_userId = userId;
// get the id of the group or set the group to the current user
groupname = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_GROUP);
CmsUUID groupId = CmsUUID.getNullUUID();
try {
if ((groupname != null) && !("".equals(groupname.trim()))) {
groupId = m_cms.readGroup(groupname).getId();
}
} catch (Exception e) {
try {
groupId = m_cms.readGroup(OpenCms.getDefaultUsers().getGroupUsers()).getId();
} catch (Exception e2) {
// ignore
}
}
newDataset.m_groupId = groupId;
// set the accessflags or the default flags
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -