1
/** BEGIN COPYRIGHT BLOCK
2
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
3
* Copyright (C) 2005 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* END COPYRIGHT BLOCK **/
20
package com.netscape.admin.dirserv.panel;
23
import java.awt.event.*;
28
import javax.swing.event.*;
29
import javax.swing.text.*;
30
import javax.swing.border.EmptyBorder;
31
import com.netscape.management.client.util.AbstractDialog;
32
import com.netscape.management.client.util.Debug;
33
import com.netscape.management.client.util.ResourceSet;
34
import com.netscape.management.client.util.LDAPUtil;
35
import com.netscape.management.client.console.ConsoleInfo;
36
import com.netscape.admin.dirserv.*;
37
import netscape.ldap.*;
38
import netscape.ldap.util.*;
39
import java.text.SimpleDateFormat;
42
* Panel for Directory Server resource page
47
* @see com.netscape.admin.dirserv
49
public class LDAPAddPanel extends FilePanel {
50
public LDAPAddPanel(IDSModel model) {
51
super(model, "import", true, false);
52
_helpToken = "configuration-database-import-ldap-dbox-help";
56
JPanel grid = _myPanel;
57
grid.setLayout( new GridBagLayout() );
58
createFileArea( grid );
59
createOptionsArea( grid );
61
setLocalState( true );
62
getAbstractDialog().setFocusComponent(_tfExport);
63
getAbstractDialog().getAccessibleContext().setAccessibleDescription(_resource.getString(_section,
67
private void createOptionsArea( JPanel grid ) {
68
GridBagConstraints gbc = getGBC();
71
JPanel optionsPanel = new JPanel(new GridBagLayout());
72
gbc.gridwidth = gbc.REMAINDER;
73
gbc.fill = gbc.HORIZONTAL;
74
gbc.anchor = gbc.NORTHWEST;
76
gbc.insets = getComponentInsets();
77
grid.add(optionsPanel, gbc);
79
_cbAddOnly = makeJCheckBox( _section, "addonly" );
80
/* The factory method doesn't seem to pick up the default
81
state from the properties file */
82
_cbAddOnly.setSelected( false );
84
gbc.anchor = gbc.NORTHWEST;
88
gbc.gridwidth = gbc.REMAINDER;
89
optionsPanel.add( _cbAddOnly, gbc );
91
_cbContinuous = makeJCheckBox( _section, "continuous" );
92
_cbContinuous.setSelected( true );
93
gbc.gridwidth = gbc.REMAINDER;
94
optionsPanel.add( _cbContinuous, gbc );
96
JLabel lblRejects = makeJLabel( _section, "rejects" );
98
optionsPanel.add( lblRejects, gbc );
99
_tfRejects = makeJTextField( _section, "rejects", "", 30 );
100
lblRejects.setLabelFor(_tfRejects);
102
optionsPanel.add( Box.createGlue(), gbc );
103
gbc.fill = gbc.HORIZONTAL;
106
optionsPanel.add( _tfRejects, gbc );
108
_bRejects = makeJButton( "import", "browse-rejects-file" );
110
gbc.gridwidth = gbc.REMAINDER;
111
optionsPanel.add(_bRejects,gbc);
114
protected void setLocalState( boolean state ) {
118
super.setLocalState( state );
122
protected void checkOkay() {
123
String path = getFilename();
124
String rejects = _tfRejects.getText().trim();
125
AbstractDialog dlg = getAbstractDialog();
127
boolean state = false;
129
if (path.length() > 0) {
131
if (rejects != null) {
132
File rejectsFile = new File(rejects);
133
File pathFile = new File(path);
134
if (rejectsFile.equals(pathFile)) {
140
dlg.setOKButtonEnabled( state );
145
public void changedUpdate(DocumentEvent e) {
146
super.changedUpdate( e );
150
public void removeUpdate(DocumentEvent e) {
151
super.removeUpdate( e );
155
public void insertUpdate(DocumentEvent e) {
156
super.insertUpdate( e );
161
* handle incoming event
165
public void actionPerformed(ActionEvent e) {
166
if ( e.getSource().equals(_bExport) ) {
167
String file = getFilename();
168
String[] extensions = { "ldif" };
169
String[] descriptions = { _resource.getString( "filefilter", "ldif-label" ) };
170
String defaultPath = getDefaultPath(getModel());
171
if ( (file == null) || (file.trim().length() < 1) ) {
172
file = DSFileDialog.getFileName(false, extensions,
173
descriptions, (Component)this, "*.ldif", defaultPath);
175
File theFile = new File(file);
176
if (theFile.isAbsolute()) {
177
file = DSFileDialog.getFileName(file, false, extensions,
178
descriptions, (Component)this);
180
file = DSFileDialog.getFileName(false, extensions,
181
descriptions, (Component)this, file, defaultPath);
186
} else if ( e.getSource().equals(_bRejects) ) {
187
String file = _tfRejects.getText();
188
String defaultPath = getDefaultPath(getModel());
189
if ( (file == null) || (file.trim().length() < 1) ) {
190
file = DSFileDialog.getFileName(false, null,
191
null, (Component)this, "rejects", defaultPath);
193
File theFile = new File(file);
194
if (theFile.isAbsolute()) {
195
file = DSFileDialog.getFileName(file, false, (Component)this);
197
file = DSFileDialog.getFileName(false, null,
198
null, (Component)this, file, defaultPath);
202
_tfRejects.setText(file);
205
super.actionPerformed(e);
209
public void okCallback() {
210
boolean status = false;
212
String importPath = getFilename();
214
/*We only test if the file name is not empty*/
215
if (importPath.trim().equals("")) {
216
if (!validateFilename())
221
ConsoleInfo info = getModel().getServerInfo();
222
LDAPConnection ldc = info.getLDAPConnection();
224
File importPathFile = new File( importPath );
225
/* If no path was provided, we try first with the path of the DSFileDialog and then with the default path of the console */
226
if ( !importPathFile.isAbsolute() ) {
227
if (DSFileDialog.getPath() != null) {
228
importPath = DSFileDialog.getPath() + importPath;
230
importPath = getDefaultPath(getModel()) + importPath;
233
/* Check if the file can be read */
234
importPathFile = new File( importPath );
236
FileInputStream test = new FileInputStream(importPathFile);
237
} catch (Exception e) {
238
DSUtil.showErrorDialog( getModel().getFrame(),
239
"cantopen", importPath, _section );
243
importPath = importPathFile.getCanonicalPath();
245
String rejects = _tfRejects.getText();
246
if ( rejects.length() < 1 ) {
249
File rejectsFile = new File( rejects );
250
/* If no path was provided, we try first with the path of the DSFileDialog and then with the default path of the console */
251
if ( !rejectsFile.isAbsolute() ) {
252
if (DSFileDialog.getPath() != null) {
253
rejects = DSFileDialog.getPath() + rejects;
255
rejects = getDefaultPath(getModel()) + rejects;
258
rejectsFile = new File( rejects );
260
/* Check if the rejects file AND the import file are the same */
261
if (rejectsFile.equals(importPathFile)) {
262
DSUtil.showErrorDialog( getModel().getFrame(),
263
"rejectsfileisimportfile",
269
if (DSUtil.fileExists(rejectsFile)) {
270
int response = DSUtil.showConfirmationDialog(getModel().getFrame(),
271
"confirm-delete-reject-file",
272
DSUtil.inverseAbreviateString(rejects, 30),
274
if ( response != JOptionPane.YES_OPTION ) {
280
/* Check if we can write in the rejects file */
282
FileOutputStream test =new FileOutputStream( rejectsFile );
283
} catch (Exception e) {
284
String[] args = {DSUtil.inverseAbreviateString(rejects, 40)};
285
DSUtil.showErrorDialog( getModel().getFrame(),
286
"unwritable-check", args,
291
boolean addOnly = _cbAddOnly.isSelected();
292
boolean continuous = _cbContinuous.isSelected();
293
DSExportImport ds = new DSExportImport( ldc,
298
String title = _resource.getString("import", "title");
299
LDAPAddProgressDialog dlg = new LDAPAddProgressDialog(getModel().getFrame(),
304
ds.addEntryChangeListener( dlg );
307
Thread th = new Thread(ds);
309
} catch ( Exception e ) {
310
Debug.println( "LDAPAddPanel.okCallback: " +
316
status = ds.getStatus();
318
/* We display a result message if the operation has been cancelled */
319
if (dlg.hasBeenCancelled()) {
321
String[] args = { Integer.toString(ds.getEntryCount()),
322
Integer.toString(ds.getRejectCount()) };
323
DSUtil.showErrorDialog( getModel().getFrame(),
327
if ( ds.getError() == ds.STATUS_UNWRITABLE ) {
328
String[] args = {DSUtil.inverseAbreviateString(rejects, 40),
329
Integer.toString(ds.STATUS_UNWRITABLE)};
330
DSUtil.showErrorDialog( getModel().getFrame(),
334
} else if (ds.getError() == ds.LDIF_SYNTAX_ERROR) {
335
String[] args = {DSUtil.abreviateString(ds.getLastDN(), 30),
336
Integer.toString(ds.LDIF_SYNTAX_ERROR)};
337
DSUtil.showErrorDialog( getModel().getFrame(), "BulkAddError-ldifSyntaxError", args, "general" );
339
} else if (ds.getError() == ds.MALFORMED_EXPRESSION_ERROR) {
340
int position = ds.getEntryCount() + ds.getRejectCount() +1;
341
String lastDN = ds.getLastDN();
343
if (lastDN == null) {
344
String[] args = {Integer.toString(position),
345
Integer.toString(ds.MALFORMED_EXPRESSION_ERROR)};
346
DSUtil.showErrorDialog( getModel().getFrame(),
347
"malformed-expression-error-number",
351
String[] args = {DSUtil.abreviateString(lastDN, 30),
352
Integer.toString(ds.MALFORMED_EXPRESSION_ERROR)};
353
DSUtil.showErrorDialog( getModel().getFrame(),
354
"malformed-expression-error-dn",
359
DSUtil.showErrorDialog( getModel().getFrame(),
360
"failed", Integer.toString(ds.getError()) , _section );
364
} catch ( Exception e ) {
365
Debug.println( "LDAPAddPanel.okCallback: import " +
370
getModel().contentChanged();
373
/* No state to preserve */
377
public void resetCallback() {
378
/* No state to preserve */
383
class LDAPAddProgressDialog extends GenericProgressDialog implements IEntryChangeListener, ActionListener {
384
public LDAPAddProgressDialog(JFrame parent,
389
super(parent, true, TEXT_FIELD_AND_CANCEL_BUTTON_OPTION, title, comp);
390
addActionListener(this);
392
setTextInTextAreaLabel(_resource.getString(_section,"rejects-progressdialog-label"));
398
* Called when an entry changes on import. Return
399
* true to continue, false to stop.
401
public boolean entryChanged( String dn, String msg ) {
405
/* A message to display */
406
if ( _continue && (dn != null) ) {
407
if ((_ds.getCurrentEntry() % 5) == 0) {
408
String[] args = {String.valueOf(_ds.getCurrentEntry()),
409
DSUtil.abreviateString(dn, 60)};
410
setTextInLabel( _resource.getString(_section,
411
"addingentrynumber-label",
415
if (!dn.trim().equals("")) {
416
appendTextToTextArea(dn+": "+msg+"\n");
418
appendTextToTextArea(msg+"\n");
423
} else if (_continue ) {
424
boolean status = _ds.getStatus();
426
String[] args = { Integer.toString(_ds.getEntryCount()),
427
Integer.toString(_ds.getRejectCount()) };
428
setTextInLabel( _resource.getString(_section,
432
if ( _ds.getError() == _ds.STATUS_UNWRITABLE ) {
433
String[] args = {_rejects,
434
Integer.toString(_ds.STATUS_UNWRITABLE)};
435
setTextInLabel( _resource.getString(_section,
438
} else if (_ds.getError() == _ds.LDIF_SYNTAX_ERROR) {
439
String[] args = {DSUtil.abreviateString(_ds.getLastDN(), 45),
440
Integer.toString(_ds.LDIF_SYNTAX_ERROR)};
441
setTextInLabel( _resource.getString( "general" ,
442
"BulkAddError-ldifSyntaxError-msg",
444
} else if (_ds.getError() == _ds.MALFORMED_EXPRESSION_ERROR) {
445
int position = _ds.getEntryCount() + _ds.getRejectCount() +1;
446
String lastDN = _ds.getLastDN();
448
if (lastDN == null) {
449
String[] args = {Integer.toString(position),
450
Integer.toString(_ds.MALFORMED_EXPRESSION_ERROR)};
451
setTextInLabel( _resource.getString(_section,
452
"malformed-expression-error-number-msg",
455
String[] args = {DSUtil.abreviateString(lastDN, 45),
456
Integer.toString(_ds.MALFORMED_EXPRESSION_ERROR)};
457
setTextInLabel( _resource.getString(_section,
458
"malformed-expression-error-dn-msg",
462
String[] args = {Integer.toString(_ds.getError())};
463
setTextInLabel( _resource.getString(_section,
470
/* Operation cancelled */
477
public boolean entryChanged( String dn ) {
478
return entryChanged( dn, null );
482
* Handle incoming event from button.
486
public void actionPerformed(ActionEvent e) {
487
if ( e.getActionCommand().equals( GenericProgressDialog.CANCEL )) {
492
} else if (e.getActionCommand().equals( GenericProgressDialog.CLOSE )) {
497
public boolean hasBeenCancelled() {
501
private String _rejects;
502
private DSExportImport _ds;
503
private boolean _continue = true;
504
private boolean _done = false;
507
private JCheckBox _cbContinuous;
508
private JCheckBox _cbAddOnly;
509
private JLabel _lblRejects;
510
private JButton _bRejects;
511
private JTextField _tfRejects;
512
private static final String _section = "import";