📄 cmsimportmoduledata.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/file/Attic/CmsImportModuledata.java,v $
* Date : $Date: 2003/03/25 16:35:07 $
* Version: $Revision: 1.12 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* 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
* version 2.1 of the License, or (at your option) any later version.
*
* 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 OpenCms, please see the
* OpenCms 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.file;
import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.defaults.master.CmsMasterContent;
import com.opencms.defaults.master.CmsMasterDataSet;
import com.opencms.defaults.master.CmsMasterMedia;
import com.opencms.report.I_CmsReport;
import com.opencms.template.A_CmsXmlContent;
import com.opencms.util.Encoder;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
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.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.zip.ZipEntry;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* Holds the functionaility to import resources from the filesystem
* or a zip file into the OpenCms COS.
*
* @author Edna Falkenhan
* @author Alexander Kandzior (a.kandzior@alkacon.com)
*
* @version $Revision: 1.12 $ $Date: 2003/03/25 16:35:07 $
*/
public class CmsImportModuledata extends CmsImport implements I_CmsConstants, Serializable {
/**
* Constructs a new import object which imports the module date 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
* @throws CmsException if something goes wrong
*/
public CmsImportModuledata(
CmsObject cms,
String importFile,
String importPath,
I_CmsReport report
) throws CmsException {
// set member variables
m_cms = cms;
m_importFile = importFile;
m_importPath = importPath;
m_report = report;
m_importingChannelData = true;
}
/**
* Imports the moduledata and writes them to the cms even if there already exist
* conflicting files.<p>
* @throws CmsException in case something goes wrong
*/
public void importResources() throws CmsException {
// initialize the import
openImportFile();
try{
// first import the channels
m_report.println(m_report.key("report.import_channels_begin"), I_CmsReport.C_FORMAT_HEADLINE);
importAllResources(null, null, null, null, null);
m_report.println(m_report.key("report.import_channels_end"), I_CmsReport.C_FORMAT_HEADLINE);
// now import the moduledata
m_report.println(m_report.key("report.import_moduledata_begin"), I_CmsReport.C_FORMAT_HEADLINE);
importModuleMasters();
m_report.println(m_report.key("report.import_moduledata_end"), I_CmsReport.C_FORMAT_HEADLINE);
} catch (CmsException e){
throw e;
} finally {
// close the import file
closeImportFile();
}
}
/**
* 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 all available modules in this system
Hashtable moduleExportables = new Hashtable();
m_cms.getRegistry().getModuleExportables(moduleExportables);
// now get the subIds of each module
Hashtable availableModules = new Hashtable();
Enumeration modElements = moduleExportables.elements();
while(modElements.hasMoreElements()){
String classname = (String)modElements.nextElement();
// 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
NodeList masterNodes;
Element currentElement;
String subid;
try {
// get all master-nodes
masterNodes = m_docXml.getElementsByTagName(CmsExportModuledata.C_EXPORT_TAG_MASTER);
int length = masterNodes.getLength();
// walk through all files in manifest
for (int i = 0; i < length; i++) {
currentElement = (Element) masterNodes.item(i);
// get the subid of the modulemaster
subid = getTextNodeValue(currentElement, 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(" ( " + (i+1) + " / " + length + " ) ");
importMaster(subid, classname, currentElement);
}
}
} catch (Exception exc) {
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
/**
* Imports a single master.<p>
*
* @param subId the subid of the module
* @param classname the name of the module class
* @param currentElement the current element of the xml file
* @throws CmsException in case something goes wrong
*/
private void importMaster(String subId, String classname, Element currentElement) throws CmsException{
// print out some information to the report
m_report.print(m_report.key("report.importing"), I_CmsReport.C_FORMAT_NOTE);
CmsMasterDataSet newDataset = new CmsMasterDataSet();
Vector channelRelations = new Vector();
Vector masterMedia = new Vector();
// try to get the dataset
try{
int subIdInt = Integer.parseInt(subId);
newDataset = getMasterDataSet(subIdInt, currentElement);
} catch (Exception e){
m_report.println(e);
throw new CmsException("Cannot get dataset ", e);
}
m_report.print("'" + Encoder.escapeHtml(newDataset.m_title) + "' (" + classname + ")");
m_report.print(m_report.key("report.dots"), I_CmsReport.C_FORMAT_NOTE);
// try to get the channelrelations
try{
channelRelations = getMasterChannelRelation(currentElement);
} catch (Exception e){
m_report.println(e);
throw new CmsException("Cannot get channelrelations ", e);
}
// try to get the media
try{
masterMedia = getMasterMedia(currentElement);
} catch (Exception e){
m_report.println(e);
throw new CmsException("Cannot get media ", e);
}
// add the channels and media to the dataset
newDataset.m_channelToAdd = channelRelations;
newDataset.m_mediaToAdd = masterMedia;
// create the new content definition
CmsMasterContent newMaster = getContentDefinition(classname,
new Class[] {CmsObject.class, CmsMasterDataSet.class},
new Object[] {m_cms, newDataset});
try{
int userId = newMaster.getOwner();
int groupId = newMaster.getGroupId();
// first insert the new master
newMaster.importMaster();
// now update the master because user and group might be changed
newMaster.chown(m_cms, userId);
newMaster.chgrp(m_cms,groupId);
} catch (Exception e){
m_report.println(e);
throw new CmsException("Cannot write master ", e);
}
m_report.println(m_report.key("report.ok"), I_CmsReport.C_FORMAT_OK);
}
/**
* Gets the dataset for the master from the xml file.<p>
*
* @param subId the subid of the module
* @param currentElement the current element of the xml file
* @return the dataset with the imported information
* @throws CmsException in case something goes wrong
*/
private CmsMasterDataSet getMasterDataSet(int subId, Element currentElement) throws CmsException{
String datasetfile, username, groupname, accessFlags, publicationDate, purgeDate, flags,
feedId, feedReference, feedFilename, title;
// get the new dataset object
CmsMasterDataSet newDataset = new CmsMasterDataSet();
// get the file with the dataset of the master
datasetfile = getTextNodeValue(currentElement, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASET);
Document datasetXml = this.getXmlFile(datasetfile);
Element dataset = datasetXml.getDocumentElement();
// get the information from the dataset and add it to the dataset
// first add the subid
newDataset.m_subId = subId;
newDataset.m_masterId = C_UNKNOWN_ID;
// get the id of the user or set the owner to the current user
username = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_USER);
int userId = m_cms.getRequestContext().currentUser().getId();
try{
if((username != null) && !("".equals(username.trim()))){
userId = m_cms.readUser(username).getId();
}
} catch (Exception e){
}
newDataset.m_userId = userId;
// get the id of the group or set the group to the current user
groupname = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_GROUP);
int groupId = m_cms.getRequestContext().currentGroup().getId();
try{
if((groupname != null) && !("".equals(groupname.trim()))){
groupId = m_cms.readGroup(groupname).getId();
}
} catch (Exception e){
}
newDataset.m_groupId = groupId;
// set the accessflags or the default flags
accessFlags = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_ACCESSFLAGS);
try{
newDataset.m_accessFlags = Integer.parseInt(accessFlags);
} catch (Exception e){
newDataset.m_accessFlags = C_ACCESS_DEFAULT_FLAGS;
}
// set the publication date
publicationDate = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_PUBLICATIONDATE);
try{
newDataset.m_publicationDate = convertDate(publicationDate);
} catch (Exception e){
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -