View Javadoc

1   package net.sf.cantina.datasource;
2   
3   import net.sf.cantina.DataSource;
4   import net.sf.cantina.Realm;
5   import net.sf.cantina.Document;
6   import net.sf.cantina.exceptions.DuplicateKeyException;
7   import org.apache.log4j.Logger;
8   import java.util.*;
9   
10  /***
11   * @author Stephane JAIS
12   */
13  public abstract class BackupUtils
14  {
15    private static final Logger logger = Logger.getLogger(BackupUtils.class);
16  
17    protected static void importDataSource (DataSource source,DataSource dest)
18    throws Exception
19    {
20     //copy realms
21      Collection realms = source.findAllRealms();
22      for (Iterator realmIterator = realms.iterator(); realmIterator.hasNext();)
23        dumpRealm((Realm)realmIterator.next(),dest);
24  
25      //copy documents
26      Collection documentIds = source.selectAllDocumentIds();
27      for (Iterator docIterator = documentIds.iterator(); docIterator.hasNext();)
28        dumpDocument(source.loadDocument(docIterator.next().toString()),dest);
29    }
30  
31    /***
32     * Dumps a document to a datasource, overriding any previously saved document.
33     * @param original
34     * @param destination
35     * @throws Exception
36     */
37  
38    protected static void dumpDocument(Document original, DataSource destination)
39    throws Exception
40    {
41      try
42      {
43        destination.createDocument(original.getDocumentId());
44      } catch (DuplicateKeyException e) {}
45      Document copy = destination.loadDocument(original.getDocumentId());
46      copy.setRealm(destination.loadRealm(original.getRealm().getName()));
47      copy.setContentType(original.getContentType());
48      for (Iterator locales = original.getAvailableLocales().iterator();locales.hasNext();)
49      {
50        Locale l = (Locale)locales.next();
51        copy.setContent(l,original.getContent(l));
52      }
53      destination.saveDocument(copy);
54    }
55  
56    /***
57     * Dumps a realm to a datasource, overriding any previously saved realm.
58     * @param original
59     * @param destination
60     * @throws Exception
61     */
62  
63    protected static void dumpRealm(Realm original, DataSource destination)
64    throws Exception
65    {
66      try
67      {
68        destination.createRealm(original.getName());
69      } catch (DuplicateKeyException e) {}
70      Realm copy = destination.loadRealm(original.getName());
71      copy.setDefaultLocale(original.getDefaultLocale());
72      destination.saveRealm(copy);
73    }
74  }