KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > xml > applicationdata > FullBackupDataWriter


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.xml.applicationdata;
11
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.*;
15
16 import org.mmbase.bridge.util.HugeNodeListIterator;
17 import org.mmbase.storage.search.*;
18 import org.mmbase.storage.search.implementation.*;
19 import org.mmbase.module.core.*;
20 import org.mmbase.module.corebuilders.InsRel;
21 import org.mmbase.util.logging.*;
22 import org.mmbase.util.xml.ApplicationReader;
23
24 /**
25  * This is used to export a full backup, by writing all nodes to XML.
26  *
27  * @since MMBase-1.8
28  * @author Pierre van Rooden
29  * @version $Id: FullBackupDataWriter.java,v 1.3 2005/10/07 18:42:49 michiel Exp $
30  */

31 public class FullBackupDataWriter {
32
33     /**
34      * Logging instance
35      */

36     private static Logger log = Logging.getLoggerInstance(FullBackupDataWriter.class);
37
38     /**
39      * Writes all nodes to XML.
40      * @param reader A <code>ApplicationReader</code> initialised to read the application's description (xml) file
41      * @param targetPath The path where to save the application
42      * @param mmbase Reference to the MMbase processormodule. Used to retrieve the nodes to write.
43      * @param logger Storage for messages which can be displayed to the user.
44      * @throws IOException if a file could not be written
45      * @throws SearchQueryException if data could not be obtained from the database
46      */

47     public static void writeContext(ApplicationReader reader, String JavaDoc targetPath, MMBase mmbase, Logger logger) throws IOException JavaDoc, SearchQueryException {
48         // Create directory for data files.
49
String JavaDoc subTargetPath = targetPath + "/" + reader.getName() + "/";
50         File JavaDoc file = new File JavaDoc(subTargetPath);
51         file.mkdirs();
52         // Write the nodes
53
writeNodes(subTargetPath, mmbase, logger);
54         logger.info("Full backup finished.");
55     }
56
57     /**
58      * Searches the MMBase cloud, collecting all nodes and storing them in data files.
59      *
60      * @param targetPath The path where to save the application data
61      * @param mmb MMBase object used to retrieve builder information
62      * @param logger Storage for messages which can be displayed to the user.
63      * @throws IOException if a file could not be written
64      * @throws SearchQueryException if data could not be obtained from the database
65      */

66     static void writeNodes(String JavaDoc subTargetPath, MMBase mmbase, Logger logger) throws IOException JavaDoc, SearchQueryException {
67         // Get all loaded builders.
68
for (Iterator i = mmbase.getBuilders().iterator(); i.hasNext(); ) {
69             MMObjectBuilder builder = (MMObjectBuilder) i.next();
70
71             // Skip virtual builders and a set of system builders
72
String JavaDoc builderName = builder.getTableName();
73             if (builder.isVirtual() ||
74                 builderName.equals("reldef") ||
75                 builderName.equals("typerel") ||
76                 builderName.equals("versions") ||
77                 builderName.equals("syncnodes") ||
78                 builderName.equals("daymarks") ||
79                 builderName.equals("oalias") ||
80                 builderName.equals("icaches")) {
81                 continue;
82             }
83             boolean isRelation = builder instanceof InsRel;
84
85             NodeSearchQuery query = new NodeSearchQuery(builder);
86             StepField otypeField = query.getField(builder.getField(MMObjectBuilder.FIELD_OBJECT_TYPE));
87             BasicFieldValueConstraint constraint = new BasicFieldValueConstraint(otypeField, new Integer JavaDoc(builder.getObjectType()));
88             query.setConstraint(constraint);
89
90             // Add this builder's nodes to set (by nodenumber).
91
List nodes = builder.getNodes(query);
92             writeNodes(subTargetPath, mmbase, logger, builder, nodes, isRelation);
93         }
94     }
95
96     /**
97      * Writes the nodes of a particular type to the corresponding XML file.
98      * @param builder The builder.
99      * @param nodes The nodes, must type corresponding to the builder.
100      * @param subTargetPath Path where the XML file is written.
101      * @param mmb MMBase object used to retrieve builder information
102      * @param logger Used to store messages that can be shown to the user
103      * @param isRelation Indicates whether the nodes to write are data (false) or relation (true) nodes
104      */

105     static void writeNodes(String JavaDoc subTargetPath, MMBase mmbase, Logger logger, MMObjectBuilder builder, List nodes, boolean isRelation) {
106
107         // Create nodewriter for this builder
108
NodeWriter nodeWriter = new NodeWriter(mmbase, logger, subTargetPath, builder.getTableName(), isRelation);
109
110         Iterator iNodes = nodes.iterator();
111         int nrWritten = 0;
112         while (iNodes.hasNext()) {
113             MMObjectNode node = (MMObjectNode) iNodes.next();
114             // Write node if it's of the correct type.
115
if (node.getBuilder() == builder) {
116                 nodeWriter.write(node);
117                 nrWritten++;
118             }
119         }
120         nodeWriter.done();
121
122         log.debug("Builder " + builder.getTableName() + ": " + nrWritten + (isRelation ? " relations" : " nodes") + " written.");
123     }
124
125 }
126
Popular Tags