Package com.dbxml.db.admin.nodes

Source Code of com.dbxml.db.admin.nodes.DocumentsNode

package com.dbxml.db.admin.nodes;

/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: DocumentsNode.java,v 1.12 2006/02/02 18:53:46 bradford Exp $
*/

import com.dbxml.db.admin.Admin;
import com.dbxml.db.client.CollectionClient;
import com.dbxml.util.Stopwatch;
import com.dbxml.util.dbXMLException;
import java.awt.Cursor;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;

/**
* DocumentsNode
*/

public final class DocumentsNode extends AdminNode implements HasCollection, HasChildren, HasMenu {
   private static final AdminNode[] EmptyAdminNodes = new AdminNode[0];
   private static final Icon DocumentsIcon = new ImageIcon(DocumentsNode.class.getResource("docs_mini.gif"));

   public static final int ACTION_IMPORT = 0;

   private static final MenuItem[] Menu = new MenuItem[]{
      new MenuItem(ACTION_IMPORT, "Import Documents")
   };

   private CollectionClient col;

   public DocumentsNode(AdminNode parentNode, CollectionClient col) {
      super(parentNode);

      this.col = col;
   }

   public String getLabel() {
      return "Documents";
   }

   public Icon getIcon() {
      return DocumentsIcon;
   }

   public CollectionClient getCollection() {
      return col;
   }

   public AdminNode[] getChildren() {
      List list = new ArrayList();

      try {
         String[] docs = col.listKeys();
         for ( int j = 0; j < docs.length; j++ )
            list.add(new DocumentNode(this, col, docs[j]));
      }
      catch ( Exception e ) {
         // No Resources
      }

      return (AdminNode[])list.toArray(EmptyAdminNodes);
   }

   public MenuItem[] getMenu() {
      return Menu;
   }

   public int menuAction(int action) {
      Admin admin = Admin.getInstance();

      switch ( action ) {
         case ACTION_IMPORT: {
            try {
               JFileChooser fc = new JFileChooser();
               fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
               fc.setMultiSelectionEnabled(true);
               int result = fc.showDialog(admin, "Import");
               if ( result == JFileChooser.APPROVE_OPTION ) {
                  admin.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                  Stopwatch sw = new Stopwatch(true);
                  boolean docs = col.getCollectionType() == CollectionClient.TYPE_DOCUMENTS;
                  int count = 0;
                  int error = 0;
                  File[] files = fc.getSelectedFiles();
                  for ( int i = 0; i < files.length; i++ ) {
                     File file = files[i];
                     String docName = file.getName();
                     try {
                        if ( docs )
                           importDoc(docName, file);
                        else
                           importBin(docName, file);
                        count++;
                     }
                     catch ( Exception e ) {
                        error++;
                        admin.addMessage("Couldn't import '" + docName + "': "+e.getMessage());
                     }
                  }
                  sw.stop();
                  if ( error > 0 )
                     admin.addMessage(count+" Document(s) imported ("+error+" errors): "+sw.toString());
                  else
                     admin.setStatus(count+" Document(s) imported: "+sw.toString());
                  admin.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
               }
               return REFRESH_CHILDREN;
            }
            catch ( dbXMLException e ) {
               admin.addMessage(e.getMessage());
            }
         }
      }
      return REFRESH_NONE;
   }

   private void importBin(String docName, File file) throws Exception {
      FileInputStream fis = new FileInputStream(file);
      byte[] b = new byte[(int)file.length()];
      fis.read(b);
      fis.close();
      col.setValue(docName, b);
   }

   private void importDoc(String docName, File file) throws Exception {
      FileInputStream fis = new FileInputStream(file);
      BufferedInputStream bis = new BufferedInputStream(fis, 4096);
      InputStreamReader isr = new InputStreamReader(bis, "UTF8");
      char[] c = new char[(int)file.length()];
      int size = isr.read(c);
      isr.close();
      col.setDocumentAsText(docName, new String(c, 0, size));
   }

}
TOP

Related Classes of com.dbxml.db.admin.nodes.DocumentsNode

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.