001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.xml.applicationdata;
011:
012: import java.io.File;
013: import java.util.*;
014:
015: import org.mmbase.storage.search.*;
016: import org.mmbase.storage.search.implementation.*;
017: import org.mmbase.module.core.*;
018: import org.mmbase.module.corebuilders.InsRel;
019: import org.mmbase.util.logging.*;
020: import org.mmbase.util.xml.ApplicationReader;
021:
022: /**
023: * This is used to export a full backup, by writing all nodes to XML.
024: *
025: * @since MMBase-1.8
026: * @author Pierre van Rooden
027: * @version $Id: FullBackupDataWriter.java,v 1.8 2007/02/25 17:56:58 nklasens Exp $
028: */
029: public class FullBackupDataWriter {
030:
031: /**
032: * Logging instance
033: */
034: private static Logger log = Logging
035: .getLoggerInstance(FullBackupDataWriter.class);
036:
037: /**
038: * Writes all nodes to XML.
039: * @param reader A <code>ApplicationReader</code> initialised to read the application's description (xml) file
040: * @param targetPath The path where to save the application
041: * @param mmbase Reference to the MMbase processormodule. Used to retrieve the nodes to write.
042: * @param logger Storage for messages which can be displayed to the user.
043: * @throws IOException if a file could not be written
044: * @throws SearchQueryException if data could not be obtained from the database
045: */
046: public static void writeContext(ApplicationReader reader,
047: String targetPath, MMBase mmbase, Logger logger)
048: throws SearchQueryException {
049: // Create directory for data files.
050: String subTargetPath = targetPath + "/" + reader.getName()
051: + "/";
052: File file = new File(subTargetPath);
053: file.mkdirs();
054: // Write the nodes
055: writeNodes(subTargetPath, mmbase, logger);
056: logger.info("Full backup finished.");
057: }
058:
059: /**
060: * Searches the MMBase cloud, collecting all nodes and storing them in data files.
061: *
062: * @param targetPath The path where to save the application data
063: * @param mmb MMBase object used to retrieve builder information
064: * @param logger Storage for messages which can be displayed to the user.
065: * @throws IOException if a file could not be written
066: * @throws SearchQueryException if data could not be obtained from the database
067: */
068: static void writeNodes(String subTargetPath, MMBase mmbase,
069: Logger logger) throws SearchQueryException {
070: for (Object element : mmbase.getBuilders()) {
071: MMObjectBuilder builder = (MMObjectBuilder) element;
072:
073: // Skip virtual builders and a set of system builders
074: String builderName = builder.getTableName();
075: if (builder.isVirtual() || builderName.equals("reldef")
076: || builderName.equals("typerel")
077: || builderName.equals("versions")
078: || builderName.equals("syncnodes")
079: || builderName.equals("daymarks")
080: || builderName.equals("oalias")
081: || builderName.equals("icaches")) {
082: continue;
083: }
084: boolean isRelation = builder instanceof InsRel;
085:
086: NodeSearchQuery query = new NodeSearchQuery(builder);
087: StepField otypeField = query.getField(builder
088: .getField(MMObjectBuilder.FIELD_OBJECT_TYPE));
089: BasicFieldValueConstraint constraint = new BasicFieldValueConstraint(
090: otypeField, builder.getObjectType());
091: query.setConstraint(constraint);
092:
093: // Add this builder's nodes to set (by nodenumber).
094: List<MMObjectNode> nodes = builder.getStorageConnector()
095: .getNodes(query, false);
096: writeNodes(subTargetPath, mmbase, logger, builder, nodes,
097: isRelation);
098: }
099: }
100:
101: /**
102: * Writes the nodes of a particular type to the corresponding XML file.
103: * @param builder The builder.
104: * @param nodes The nodes, must type corresponding to the builder.
105: * @param subTargetPath Path where the XML file is written.
106: * @param mmb MMBase object used to retrieve builder information
107: * @param logger Used to store messages that can be shown to the user
108: * @param isRelation Indicates whether the nodes to write are data (false) or relation (true) nodes
109: */
110: static void writeNodes(String subTargetPath, MMBase mmbase,
111: Logger logger, MMObjectBuilder builder,
112: List<MMObjectNode> nodes, boolean isRelation) {
113:
114: // Create nodewriter for this builder
115: NodeWriter nodeWriter = new NodeWriter(mmbase, logger,
116: subTargetPath, builder.getTableName(), isRelation);
117:
118: Iterator<MMObjectNode> iNodes = nodes.iterator();
119: int nrWritten = 0;
120: while (iNodes.hasNext()) {
121: MMObjectNode node = iNodes.next();
122: // Write node if it's of the correct type.
123: if (node.getBuilder() == builder) {
124: nodeWriter.write(node);
125: nrWritten++;
126: }
127: }
128: nodeWriter.done();
129:
130: log.debug("Builder " + builder.getTableName() + ": "
131: + nrWritten + (isRelation ? " relations" : " nodes")
132: + " written.");
133: }
134:
135: }
|