KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > export > Importer


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.genimen.djeneric.tools.export;
32
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.util.HashMap JavaDoc;
39
40 import com.genimen.djeneric.language.Messages;
41 import com.genimen.djeneric.repository.DjContext;
42 import com.genimen.djeneric.repository.DjContextManager;
43 import com.genimen.djeneric.repository.DjPersistenceManager;
44 import com.genimen.djeneric.repository.DjPersistenceManagerFactory;
45 import com.genimen.djeneric.repository.DjSession;
46 import com.genimen.djeneric.repository.DjUser;
47 import com.genimen.djeneric.repository.DjUserContextAssociation;
48 import com.genimen.djeneric.repository.exceptions.DjenericException;
49 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
50 import com.genimen.djeneric.repository.rdbms.RdbmsPersistenceManager;
51 import com.genimen.djeneric.util.DjDefaultProgressDisplayer;
52 import com.genimen.djeneric.util.DjEnvironment;
53 import com.genimen.djeneric.util.DjLogPrintStream;
54 import com.genimen.djeneric.util.DjLogger;
55 import com.genimen.djeneric.util.DjProgressDisplayer;
56 import com.genimen.djeneric.util.DjVersion;
57
58 public class Importer extends Dumper
59 {
60   private final static String JavaDoc LOG_FILE_NAME = DjEnvironment.getAbsoluteFileName("importer.log");
61
62   public Importer(DjProgressDisplayer monitor, RdbmsPersistenceManager manager) throws DjenericException
63   {
64     super(monitor, manager);
65     _manager.bootstrapRepositoryIfNeeded(new DjDefaultProgressDisplayer());
66   }
67
68   public long importDump(File JavaDoc infile) throws FileNotFoundException JavaDoc, IOException JavaDoc, SQLException JavaDoc, DjenericException
69   {
70     checkModel(new FileInputStream JavaDoc(infile));
71     HashMap JavaDoc newIds = determineNewIds(new FileInputStream JavaDoc(infile));
72     long importCount = importDump(new FileInputStream JavaDoc(infile), newIds);
73     importRelations(new FileInputStream JavaDoc(infile), newIds);
74     getSession().commit();
75     _progressMonitor.displayProgress(Messages.getString("Dumper.ImportDone", String.valueOf(importCount)));
76     close();
77     return importCount;
78   }
79
80   public void createAndSetContext(DjDefaultProgressDisplayer pd, String JavaDoc context) throws DjenericException,
81       ObjectNotDefinedException
82   {
83     DjContextManager cmgr = getManager().getContextManager();
84     DjSession session = getManager().createSession();
85     DjContext ctxt = cmgr.createNewContext();
86     ctxt.setCode(context);
87     ctxt.setName(context);
88     ctxt.persist(session);
89     pd.displayProgress(Messages.getString("Importer.created", context));
90
91     String JavaDoc curUser = getManager().getDjenericUser();
92     if (curUser == null) curUser = getManager().getRepositoryUser();
93     if (curUser != null)
94     {
95       try
96       {
97         DjUser self = cmgr.getUser(curUser);
98
99         DjUserContextAssociation dca = cmgr.createNewUserContextAssociation();
100         dca.setContext(ctxt);
101         dca.setUser(self);
102         dca.setCreate(true);
103         dca.setDelete(true);
104         dca.setModify(true);
105         dca.setQuery(true);
106         dca.persist(session);
107         pd.displayProgress(Messages.getString("Importer.AssociatedWithUser", context, self.getCode()));
108       }
109       catch (DjenericException de)
110       {
111         pd.displayProgress(Messages.getString("Importer.CouldNotAssociateContext"));
112       }
113     }
114
115     session.commit();
116
117     setContext(context);
118   }
119
120   public static void main(String JavaDoc[] args)
121   {
122     try
123     {
124       if (args.length < 4)
125       {
126         System.out.println(Messages.getString("Importer.Usage", Importer.class.getName()));
127         return;
128       }
129
130       String JavaDoc banner = Messages.getString("global.Version", Importer.class.getName(), DjVersion.getVersion());
131       DjLogPrintStream.logAll(banner, LOG_FILE_NAME);
132
133       System.out.println(Messages.getString("global.Reading", args[0]));
134
135       String JavaDoc user = null;
136       String JavaDoc password = null;
137
138       if (args.length >= 5)
139       {
140         user = args[4];
141       }
142
143       if (args.length >= 6)
144       {
145         password = args[5];
146       }
147
148       DjPersistenceManagerFactory fact = new DjPersistenceManagerFactory(args[0]);
149       DjPersistenceManager mgr = fact.createInstance(args[1], user, password);
150
151       if (!(mgr instanceof RdbmsPersistenceManager)) throw new DjenericException(Messages
152           .getString("global.UnsupportedRdbms", mgr.getClass().getName()));
153
154       DjDefaultProgressDisplayer pd = new DjDefaultProgressDisplayer();
155       Importer importer = new Importer(pd, (RdbmsPersistenceManager) mgr);
156       String JavaDoc context = args[2];
157
158       // Should we try a merge?
159
if (context.endsWith("+"))
160       {
161         context = context.substring(0, context.length() - 1);
162         importer.setContext(context);
163       }
164       else
165       {
166         try
167         {
168           importer.setContext(context);
169           // If this succeeds then the context already exists...
170
System.err.println(Messages.getString("Importer.ContextExists", context));
171           System.exit(0);
172         }
173         catch (ObjectNotDefinedException ond)
174         {
175           importer.createAndSetContext(pd, context);
176         }
177       }
178
179       File JavaDoc infile = new File JavaDoc(args[3]);
180
181       long importCount = importer.importDump(infile);
182       pd.displayProgress(Messages.getString("Importer.ImportDone", String.valueOf(importCount)));
183     }
184     catch (DjenericException e)
185     {
186       DjLogger.log(e);
187
188     }
189     catch (Exception JavaDoc e)
190     {
191       DjLogger.log(e);
192     }
193
194     // Kill a possible AWT event loop
195
System.exit(0);
196   }
197
198 }
Popular Tags