1 package com.ca.directory.jxplorer; 2 3 import java.awt.*; 4 import java.util.*; 5 import java.util.logging.Logger ; 6 import java.util.logging.Level ; 7 import java.io.*; 8 import javax.swing.*; 9 10 import javax.naming.*; 11 import javax.naming.directory.*; 12 13 import com.ca.directory.jxplorer.tree.*; 14 import com.ca.directory.jxplorer.broker.*; 15 import com.ca.commons.naming.*; 16 import com.ca.commons.cbutil.*; 17 18 public class LdifExport extends CBDialog 19 { 20 JTextArea rootDN, newRootDN; 21 DataSource dataSource; 22 SmartTree searchTree; 23 SmartTree schemaTree; 24 25 FileWriter saveFile; 26 LdifUtility ldifutil = new LdifUtility(); 27 28 boolean usingSearch; 29 30 CBpbar pbar; 32 static String lastDirectory = null; 33 34 private static Logger log = Logger.getLogger(LdifExport.class.getName()); 35 36 56 public LdifExport(DN D, DataSource broker, SmartTree searchTree, boolean usingSearch, Frame owner) 57 { 58 this(D, broker, searchTree, usingSearch, owner, HelpIDs.LDIF_EXPORT_TREE); 59 } 60 61 82 public LdifExport(DN D, DataSource broker, SmartTree searchTree, boolean usingSearch, Frame owner, String helpID) 83 { 84 super(owner, CBIntText.get("LDIF Export"), helpID); 85 86 OK.setToolTipText(CBIntText.get("Perform the LDIF export")); 87 Cancel.setToolTipText(CBIntText.get("Cancel without performing an LDIF export")); 88 Help.setToolTipText(CBIntText.get("Display help about LDIF exporting")); 89 90 if (D==null) D = new DN(); 91 92 this.dataSource = broker; 93 this.searchTree = searchTree; 94 this.usingSearch = usingSearch; 95 96 display.add(new JLabel(CBIntText.get("Root DN")),0,0); 97 98 display.makeHeavy(); 99 100 rootDN = new JTextArea(D.toString()); 101 rootDN.setLineWrap(true); display.add(new JScrollPane(rootDN, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),1,0); 104 display.makeLight(); 105 106 display.add(new JLabel(CBIntText.get("New root DN")),0,1); 107 108 display.makeHeavy(); 109 newRootDN = new JTextArea(D.toString()); 110 newRootDN.setLineWrap(true); display.add(new JScrollPane(newRootDN, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),1,1); 113 display.makeLight(); 114 } 115 116 120 121 protected File adjustFileName(File readFile) 122 { 123 if (readFile == null) return null; 125 if (readFile.exists()) return readFile; 127 String name = readFile.getName(); 128 if (name.indexOf('.') != -1) return readFile; 130 name = name + ".ldif"; 131 132 return new File(readFile.getParentFile(), name); 133 } 134 135 140 141 public void doOK() 142 { 143 if(!checkRootDN()) return; 145 146 setVisible(false); 147 148 JFileChooser chooser = new JFileChooser(JXplorer.getProperty("ldif.homeDir")); 149 150 chooser.addChoosableFileFilter(new CBFileFilter(new String [] {"ldif", "ldi"},"Ldif Files (*.ldif, *.ldi)")); 151 152 int option = chooser.showSaveDialog(this); 153 154 if (option == JFileChooser.APPROVE_OPTION) { 156 File readFile = chooser.getSelectedFile(); 157 if (readFile == null) 158 { 159 CBUtility.error(CBIntText.get("Please select a file")); 160 } 161 else 162 { 163 readFile = adjustFileName(readFile); 165 int response = -1; 166 if (readFile.exists()) { 168 response = JOptionPane.showConfirmDialog(this, CBIntText.get("File ''{0}'' already exsists. Do you want to replace it?", new String [] {readFile.toString()}), 169 CBIntText.get("Overwrite Confirmation"), JOptionPane.YES_NO_OPTION ); 170 171 if (response != JOptionPane.YES_OPTION) 172 { 173 setVisible(true); 174 return; 175 } 176 } 177 178 JXplorer.setProperty("ldif.homeDir", readFile.getParent()); 179 doFileWrite(readFile); 180 } 181 } 182 } 183 184 192 public boolean checkRootDN() 193 { 194 String oldRoot = (rootDN.getText()).trim(); String newRoot = (newRootDN.getText()).trim(); 197 if((oldRoot == null || oldRoot.length() <= 0) && (newRoot == null || newRoot.length() <= 0)) 198 { 199 int response = JOptionPane.showConfirmDialog(this, CBIntText.get("Without a 'Root DN' and a 'New Root DN', the full tree will be exported. Do you want to continue?"), 200 CBIntText.get("Export Full Tree"), JOptionPane.YES_NO_OPTION ); 201 if (response != JOptionPane.YES_OPTION) 202 return false; 203 204 return true; 205 } 206 else if(oldRoot == null || oldRoot.length() <= 0) 207 { 208 JOptionPane.showMessageDialog(this, CBIntText.get("Please enter a 'Root DN'."), 209 CBIntText.get("Root DN"), JOptionPane.INFORMATION_MESSAGE ); 210 return false; 211 212 } 213 else if(newRoot == null || newRoot.length() <= 0) 214 { 215 JOptionPane.showMessageDialog(this, CBIntText.get("Please enter a 'New Root DN'."), 216 CBIntText.get("New Root DN"), JOptionPane.INFORMATION_MESSAGE ); 217 return false; 218 } 219 220 return true; 221 } 222 223 224 228 protected void doFileWrite(File saveFile) 229 { 230 if (saveFile == null) 231 CBUtility.error(CBIntText.get("Unable to write to empty file"), null); 232 233 final File myFile = saveFile; 234 235 dataSource.extendedRequest(new DataQuery(DataQuery.EXTENDED) 236 { 237 public void doExtendedRequest(Broker b) 238 { 239 try 240 { 241 FileWriter myFileWriter = new FileWriter(myFile); 242 243 pbar = new CBpbar(LdifExport.this, CBIntText.get("Saving LDIF file"), CBIntText.get("Saving Data")); 244 245 248 myFileWriter.write("version: 1\n"); 249 250 DN oldRoot = new DN(rootDN.getText()); DN newRoot = new DN(newRootDN.getText()); 253 if (usingSearch) 254 { 255 Vector bloop = searchTree.getAllNodes(new DN(rootDN.getText())); 256 saveLdifList(bloop, myFileWriter, oldRoot.toString(), newRoot.toString(), b); 257 } 258 else 259 { 260 saveLdifTree(oldRoot, myFileWriter, oldRoot.toString(), newRoot.toString(), b); 261 } 262 263 myFileWriter.close(); 267 } 268 catch (Exception e) 269 { 270 setException(e); 271 } 272 273 276 if(pbar.isCanceled()) myFile.delete(); 278 279 closeDown(); 280 return; 281 } 282 }); 283 } 284 285 286 287 299 300 public boolean saveLdifTree(DN treeApex, FileWriter saveFile, String origPrefix, String newPrefix, Broker broker) 301 { 302 if (treeApex==null) return false; 304 if (pbar == null) return false; 305 306 if (newPrefix==null) origPrefix = null; if ((origPrefix!=null)&&(origPrefix.equals(newPrefix))) {origPrefix = null; newPrefix = null; } 309 310 if (pbar.isCanceled()) return false; 312 Attributes atts = null; 313 314 try 315 { 316 if (treeApex.isEmpty() == false) 317 { 318 atts = broker.unthreadedReadEntry(treeApex, null); 319 } 320 321 if (atts != null) 322 { 323 DN escapeMe = new DN(treeApex); 324 ldifutil.writeLdifEntry(escapeMe.toString(), saveFile, origPrefix, newPrefix, atts); } 326 DXNamingEnumeration children = broker.unthreadedList(treeApex); 328 329 pbar.push(children.size()); 331 while (children != null && children.hasMore()) 332 { 333 String subDNString = ((NameClassPair)children.next()).getName(); 334 DN child = new DN(treeApex); DN subDN = new DN(subDNString); 336 child.addChildRDN(subDN.getLowestRDN()); 338 if (saveLdifTree(child, saveFile, origPrefix, newPrefix, broker)==false) return false; 340 } 341 } 342 catch (NamingException e) 343 { 344 CBUtility.error(this, CBIntText.get("Unable to read dn: {0} from directory", new String [] {treeApex.toString()}), e); 345 } 346 catch (Exception e) 347 { 348 CBUtility.error(this, CBIntText.get("General error reading dn: {0} from directory", new String [] {treeApex.toString()}), e); 349 e.printStackTrace(); 350 } 351 352 pbar.pop(); 353 pbar.inc(); 354 355 return true; 356 } 357 358 368 369 public void saveLdifList(Vector dns, FileWriter saveFile, String originalPrefix, String replacementPrefix, Broker broker) 370 { 371 if (replacementPrefix==null) originalPrefix = null; if ((originalPrefix!=null)&&(originalPrefix.equals(replacementPrefix))) { 374 originalPrefix = null; 375 replacementPrefix = null; 376 } 377 int size = dns.size(); 378 pbar.push(size); 379 380 for (int i=0; i<size; i++) 381 { 382 DN dn = (DN)dns.elementAt(i); 383 384 try 385 { 386 Attributes atts = broker.unthreadedReadEntry(dn, null); 387 ldifutil.writeLdifEntry(dn.toString(), saveFile, originalPrefix, replacementPrefix, atts); 388 } 389 catch (NamingException e) 390 { 391 log.log(Level.WARNING, "Unable to read dn: '" + dn.toString() + "' from directory ", e); 392 } 393 catch (Exception e) 394 { 395 log.log(Level.WARNING, "General error reading: dn: '" + dn.toString() + "' from directory ", e); 396 } 397 398 if (pbar.isCanceled()) return; 399 pbar.inc(); 400 } 401 pbar.close(); } 403 404 private void closeDown() 405 { 406 try 407 { 408 if (saveFile != null) saveFile.close(); log.warning("Closed LDIF file"); 410 } 411 catch (IOException e) {;} 412 413 if (pbar != null) pbar.close(); 414 setVisible(false); 415 dispose(); 416 } 417 } | Popular Tags |