~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/panel/LDAPAddPanel.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
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.
 
9
 * 
 
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.
 
14
 * 
 
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 **/
 
19
 
 
20
package com.netscape.admin.dirserv.panel;
 
21
 
 
22
import java.awt.*;
 
23
import java.awt.event.*;
 
24
import java.util.*;
 
25
import java.io.*;
 
26
import java.net.URL;
 
27
import javax.swing.*;
 
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;
 
40
 
 
41
/**
 
42
 * Panel for Directory Server resource page
 
43
 *
 
44
 * @author  rweltman
 
45
 * @version %I%, %G%
 
46
 * @date                9/15/97
 
47
 * @see     com.netscape.admin.dirserv
 
48
 */
 
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";
 
53
        }
 
54
 
 
55
        public void init() {
 
56
        JPanel grid = _myPanel;
 
57
        grid.setLayout( new GridBagLayout() );
 
58
                createFileArea( grid );
 
59
                createOptionsArea( grid );
 
60
                addBottomGlue();
 
61
                setLocalState( true );
 
62
                getAbstractDialog().setFocusComponent(_tfExport);
 
63
                getAbstractDialog().getAccessibleContext().setAccessibleDescription(_resource.getString(_section,
 
64
                                                                                                                                                                                                "description"));
 
65
        }
 
66
 
 
67
    private void createOptionsArea( JPanel grid ) {        
 
68
                GridBagConstraints gbc = getGBC();
 
69
 
 
70
                /* Options group */
 
71
        JPanel optionsPanel = new JPanel(new GridBagLayout());
 
72
                gbc.gridwidth = gbc.REMAINDER;
 
73
                gbc.fill = gbc.HORIZONTAL;
 
74
                gbc.anchor = gbc.NORTHWEST;
 
75
                gbc.weightx = 1.0;
 
76
                gbc.insets = getComponentInsets();
 
77
                grid.add(optionsPanel, gbc);                       
 
78
                                
 
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 );
 
83
 
 
84
                gbc.anchor = gbc.NORTHWEST;
 
85
                gbc.fill = gbc.NONE;
 
86
                gbc.weightx = 0;
 
87
                gbc.weighty = 0;
 
88
        gbc.gridwidth = gbc.REMAINDER;
 
89
                optionsPanel.add( _cbAddOnly, gbc );            
 
90
 
 
91
            _cbContinuous = makeJCheckBox( _section, "continuous" );
 
92
                _cbContinuous.setSelected( true );
 
93
                gbc.gridwidth = gbc.REMAINDER;
 
94
        optionsPanel.add( _cbContinuous, gbc ); 
 
95
 
 
96
                JLabel lblRejects = makeJLabel( _section, "rejects" );
 
97
        gbc.gridwidth = 4;
 
98
        optionsPanel.add( lblRejects, gbc );
 
99
        _tfRejects = makeJTextField( _section, "rejects", "", 30 );
 
100
                lblRejects.setLabelFor(_tfRejects);
 
101
                gbc.gridwidth = 3;
 
102
                optionsPanel.add( Box.createGlue(), gbc );
 
103
                gbc.fill = gbc.HORIZONTAL;
 
104
                
 
105
                gbc.weightx = 1.0;
 
106
        optionsPanel.add( _tfRejects, gbc );
 
107
                gbc.weightx = 0.0;
 
108
                _bRejects = makeJButton( "import", "browse-rejects-file" );
 
109
                gbc.fill = gbc.NONE;
 
110
        gbc.gridwidth = gbc.REMAINDER;
 
111
        optionsPanel.add(_bRejects,gbc);
 
112
        }
 
113
 
 
114
    protected void setLocalState( boolean state ) {
 
115
                if ( isLocal() ) {
 
116
                        return;
 
117
                }
 
118
                super.setLocalState( state );
 
119
    }
 
120
    
 
121
    
 
122
    protected void checkOkay() {
 
123
            String path = getFilename();
 
124
                String rejects = _tfRejects.getText().trim();
 
125
                AbstractDialog dlg = getAbstractDialog();
 
126
                if ( dlg != null ) {
 
127
                        boolean state = false;
 
128
                        if (path != null) {
 
129
                                if (path.length() > 0) {
 
130
                                        state = true;
 
131
                                        if (rejects != null) {
 
132
                                                File rejectsFile = new File(rejects);
 
133
                                                File pathFile = new File(path);
 
134
                                                if (rejectsFile.equals(pathFile)) {
 
135
                                                        state = false;
 
136
                                                }
 
137
                                        }
 
138
                                }
 
139
                        } 
 
140
                        dlg.setOKButtonEnabled( state );
 
141
                } else {
 
142
                }
 
143
        }
 
144
 
 
145
    public void changedUpdate(DocumentEvent e) {
 
146
                super.changedUpdate( e );
 
147
                checkOkay();
 
148
        }
 
149
 
 
150
    public void removeUpdate(DocumentEvent e) {
 
151
                super.removeUpdate( e );
 
152
                checkOkay();
 
153
        }
 
154
 
 
155
    public void insertUpdate(DocumentEvent e) {
 
156
                super.insertUpdate( e );
 
157
                checkOkay();
 
158
        }
 
159
 
 
160
    /**
 
161
     *  handle incoming event
 
162
     *
 
163
     * @param e event
 
164
     */
 
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);
 
174
                        } else {
 
175
                                File theFile = new File(file);
 
176
                                if (theFile.isAbsolute()) {
 
177
                                        file = DSFileDialog.getFileName(file, false, extensions,
 
178
                                                                                                        descriptions, (Component)this);
 
179
                                } else {
 
180
                                        file = DSFileDialog.getFileName(false, extensions,
 
181
                                                                                                descriptions, (Component)this, file, defaultPath);
 
182
                                }
 
183
                        }
 
184
                        if (file != null)
 
185
                                setFilename(file);
 
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);
 
192
                        } else {
 
193
                                File theFile = new File(file);
 
194
                                if (theFile.isAbsolute()) {
 
195
                                        file = DSFileDialog.getFileName(file, false, (Component)this);
 
196
                                } else {
 
197
                                        file = DSFileDialog.getFileName(false, null,
 
198
                                                                                                        null, (Component)this, file, defaultPath);
 
199
                                }
 
200
                        }
 
201
            if (file != null) {
 
202
                _tfRejects.setText(file);
 
203
                        }
 
204
        } else {
 
205
                        super.actionPerformed(e);
 
206
                }
 
207
    }
 
208
 
 
209
    public void okCallback() {
 
210
        boolean status = false;
 
211
 
 
212
            String importPath = getFilename();
 
213
 
 
214
                /*We only test if the file name is not empty*/
 
215
                if (importPath.trim().equals("")) {
 
216
                        if (!validateFilename())
 
217
                                return;
 
218
                }
 
219
 
 
220
                String base = null;
 
221
                ConsoleInfo info = getModel().getServerInfo();
 
222
        LDAPConnection ldc = info.getLDAPConnection();
 
223
            try {
 
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;
 
229
                                } else {
 
230
                                        importPath = getDefaultPath(getModel()) + importPath;
 
231
                                }
 
232
                        }
 
233
                        /* Check if the file can be read */
 
234
                        importPathFile = new File( importPath );                        
 
235
                        try {
 
236
                                FileInputStream test = new FileInputStream(importPathFile);
 
237
                        } catch (Exception e) {
 
238
                                DSUtil.showErrorDialog( getModel().getFrame(),
 
239
                                                                                "cantopen", importPath, _section );
 
240
                                return;
 
241
                        }
 
242
 
 
243
                        importPath = importPathFile.getCanonicalPath();
 
244
                        
 
245
                        String rejects = _tfRejects.getText();
 
246
                        if ( rejects.length() < 1 ) {
 
247
                                rejects = null;
 
248
                        } else {
 
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;
 
254
                                        } else {                                        
 
255
                                                rejects = getDefaultPath(getModel()) + rejects;
 
256
                                        }
 
257
                                }
 
258
                                rejectsFile = new File( rejects );
 
259
                                
 
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", 
 
264
                                                                                        "",
 
265
                                                                                        _section );
 
266
                                        return;
 
267
                                }
 
268
 
 
269
                                if (DSUtil.fileExists(rejectsFile)) {
 
270
                                        int response = DSUtil.showConfirmationDialog(getModel().getFrame(),
 
271
                                                                                                                                 "confirm-delete-reject-file",
 
272
                                                                                                                                 DSUtil.inverseAbreviateString(rejects, 30),
 
273
                                                                                                                                 _section );
 
274
                                        if ( response != JOptionPane.YES_OPTION ) {
 
275
                                                return;
 
276
                                        }
 
277
                                }
 
278
                                
 
279
                                
 
280
                                /* Check if we can write in the rejects file */                         
 
281
                                try {
 
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,
 
287
                                                                                        _section );
 
288
                                        return;
 
289
                                }                               
 
290
                        }
 
291
                        boolean addOnly = _cbAddOnly.isSelected();
 
292
                        boolean continuous = _cbContinuous.isSelected();                        
 
293
                        DSExportImport ds = new DSExportImport( ldc,
 
294
                                                                                                        importPath,
 
295
                                                                                                        addOnly,
 
296
                                                                                                        continuous,
 
297
                                                                                                        rejects );
 
298
                        String title = _resource.getString("import", "title");
 
299
                        LDAPAddProgressDialog dlg = new LDAPAddProgressDialog(getModel().getFrame(),                                                                                                                              
 
300
                                                                                                                                  title,
 
301
                                                                                                                                  this,
 
302
                                                                                                                                  ds,
 
303
                                                                                                                                  rejects);
 
304
                        ds.addEntryChangeListener( dlg );
 
305
                        hideDialog();
 
306
                        try {
 
307
                                Thread th = new Thread(ds);
 
308
                                th.start();                     
 
309
                        } catch ( Exception e ) {
 
310
                                Debug.println( "LDAPAddPanel.okCallback: " +
 
311
                                                           e );
 
312
                                e.printStackTrace();
 
313
                                return;
 
314
                        }                       
 
315
                        dlg.packAndShow();
 
316
                        status = ds.getStatus();
 
317
                        
 
318
                        /* We display a result message if the operation has been cancelled */
 
319
                        if (dlg.hasBeenCancelled()) {
 
320
                                if ( status ) {
 
321
                                        String[] args = { Integer.toString(ds.getEntryCount()),
 
322
                                                                          Integer.toString(ds.getRejectCount()) };
 
323
                                        DSUtil.showErrorDialog( getModel().getFrame(),
 
324
                                                                                        "succeeded", args,
 
325
                                                                                        _section );
 
326
                                } else {                                        
 
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(),
 
331
                                                                                                "unwritable", args,
 
332
                                                                                                _section );
 
333
 
 
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" );
 
338
 
 
339
                                        } else if (ds.getError() == ds.MALFORMED_EXPRESSION_ERROR) {
 
340
                                                int position = ds.getEntryCount() + ds.getRejectCount() +1;
 
341
                                                String lastDN = ds.getLastDN();
 
342
                                                
 
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", 
 
348
                                                                                                        args,
 
349
                                                                                                        _section  );
 
350
                                                } else {
 
351
                                                        String[] args = {DSUtil.abreviateString(lastDN, 30),
 
352
                                                                                         Integer.toString(ds.MALFORMED_EXPRESSION_ERROR)};
 
353
                                                        DSUtil.showErrorDialog( getModel().getFrame(),
 
354
                                                                                                        "malformed-expression-error-dn", 
 
355
                                                                                                        args,
 
356
                                                                                                        _section  );
 
357
                                                }                               
 
358
                                        } else {
 
359
                                                DSUtil.showErrorDialog( getModel().getFrame(),
 
360
                                                                                                "failed", Integer.toString(ds.getError()) , _section );
 
361
                                        }
 
362
                                }                                                          
 
363
                        }
 
364
                } catch ( Exception e ) {
 
365
                        Debug.println( "LDAPAddPanel.okCallback: import " +
 
366
                                                   "failed - " + e );
 
367
                        e.printStackTrace();
 
368
                }               
 
369
                if ( status ) {
 
370
                        getModel().contentChanged();
 
371
                }
 
372
 
 
373
                /* No state to preserve */
 
374
                clearDirtyFlag();                               
 
375
    }
 
376
 
 
377
    public void resetCallback() {
 
378
                /* No state to preserve */
 
379
                clearDirtyFlag();
 
380
                hideDialog();
 
381
    }
 
382
 
 
383
        class LDAPAddProgressDialog extends GenericProgressDialog implements IEntryChangeListener, ActionListener {
 
384
                public LDAPAddProgressDialog(JFrame parent,                                                                      
 
385
                                                                         String title, 
 
386
                                                                         Component comp,
 
387
                                                                         DSExportImport ds,
 
388
                                                                         String rejects) {
 
389
                        super(parent, true, TEXT_FIELD_AND_CANCEL_BUTTON_OPTION, title, comp);
 
390
                        addActionListener(this);
 
391
                        setLabelRows(2);
 
392
                        setTextInTextAreaLabel(_resource.getString(_section,"rejects-progressdialog-label"));
 
393
                        _ds = ds;
 
394
                        _rejects = rejects;
 
395
                }
 
396
                        
 
397
                /**
 
398
                 * Called when an entry changes on import. Return
 
399
                 * true to continue, false to stop.
 
400
                 */
 
401
                public boolean entryChanged( String dn, String msg ) {
 
402
                        if ( dn == null ) {
 
403
                                _done = true;
 
404
                        }
 
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",
 
412
                                                                                                                args));                         
 
413
                                }
 
414
                                if (msg != null) {
 
415
                                        if (!dn.trim().equals("")) {
 
416
                                                appendTextToTextArea(dn+": "+msg+"\n");
 
417
                                        } else {
 
418
                                                appendTextToTextArea(msg+"\n");
 
419
                                        }
 
420
                                }
 
421
                                return true;
 
422
                                /* Import is over*/
 
423
                        } else if (_continue ) {
 
424
                                boolean status = _ds.getStatus();
 
425
                                if ( status ) {
 
426
                                        String[] args = { Integer.toString(_ds.getEntryCount()),
 
427
                                                                          Integer.toString(_ds.getRejectCount()) };                                     
 
428
                                        setTextInLabel( _resource.getString(_section,
 
429
                                                                                                                "succeeded-msg", 
 
430
                                                                                                                args));                                 
 
431
                                } else {
 
432
                                        if ( _ds.getError() == _ds.STATUS_UNWRITABLE ) {
 
433
                                                String[] args = {_rejects,
 
434
                                                                                 Integer.toString(_ds.STATUS_UNWRITABLE)};
 
435
                                                setTextInLabel( _resource.getString(_section,
 
436
                                                                                                                        "unwritable-msg",
 
437
                                                                                                                        args));
 
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", 
 
443
                                                                                                                         args));        
 
444
                                        } else if (_ds.getError() == _ds.MALFORMED_EXPRESSION_ERROR) {
 
445
                                                int position = _ds.getEntryCount() + _ds.getRejectCount() +1;
 
446
                                                String lastDN = _ds.getLastDN();
 
447
                                                
 
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", 
 
453
                                                                                                                                args));
 
454
                                                } else {
 
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", 
 
459
                                                                                                                                args));
 
460
                                                }                               
 
461
                                        } else {
 
462
                                                String[] args = {Integer.toString(_ds.getError())};
 
463
                                                setTextInLabel( _resource.getString(_section,
 
464
                                                                                                                        "failed-msg",
 
465
                                                                                                                        args));
 
466
                                        }
 
467
                                }
 
468
                                waitForClose();                 
 
469
                                return false;
 
470
                                /* Operation cancelled */
 
471
                        } else  {
 
472
                                closeCallBack();           
 
473
                        }
 
474
                        return false;                   
 
475
                }
 
476
                
 
477
                public boolean entryChanged( String dn ) {
 
478
                        return entryChanged( dn, null );
 
479
                }
 
480
                
 
481
                /**
 
482
                 *      Handle incoming event from button.
 
483
                 *
 
484
                 * @param e event
 
485
                 */
 
486
                public void actionPerformed(ActionEvent e) {
 
487
                        if ( e.getActionCommand().equals( GenericProgressDialog.CANCEL ))  {
 
488
                                _continue = false;
 
489
                                if ( _done )  {
 
490
                                        closeCallBack();
 
491
                                }                                       
 
492
                        } else if (e.getActionCommand().equals( GenericProgressDialog.CLOSE )) {
 
493
                                closeCallBack();
 
494
                        }
 
495
                }
 
496
 
 
497
                public boolean hasBeenCancelled() {
 
498
                        return !_continue;
 
499
                }
 
500
                
 
501
                private String _rejects;
 
502
                private DSExportImport _ds;
 
503
                private boolean _continue = true;
 
504
                private boolean _done = false;          
 
505
        }
 
506
 
 
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";
 
513
}