1 30 31 package com.genimen.djeneric.tools.export; 32 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.FileNotFoundException ; 36 import java.io.IOException ; 37 import java.sql.SQLException ; 38 import java.util.HashMap ; 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 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 infile) throws FileNotFoundException , IOException , SQLException , DjenericException 69 { 70 checkModel(new FileInputStream (infile)); 71 HashMap newIds = determineNewIds(new FileInputStream (infile)); 72 long importCount = importDump(new FileInputStream (infile), newIds); 73 importRelations(new FileInputStream (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 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 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 [] 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 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 user = null; 136 String 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 context = args[2]; 157 158 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 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 infile = new File (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 e) 190 { 191 DjLogger.log(e); 192 } 193 194 System.exit(0); 196 } 197 198 } | Popular Tags |