Search in sources :

Example 6 with Collection

use of org.xmldb.api.base.Collection in project exist by eXist-db.

the class ClientFrame method moveAction.

private void moveAction(final ActionEvent ev) {
    final ResourceDescriptor[] res = getSelectedResources();
    PrettyXmldbURI[] collections;
    // get an array of collection paths
    try {
        final Collection root = client.getCollection(XmldbURI.ROOT_COLLECTION);
        final List<PrettyXmldbURI> alCollections = getCollections(root, new ArrayList<>());
        collections = new PrettyXmldbURI[alCollections.size()];
        alCollections.toArray(collections);
    } catch (final XMLDBException e) {
        showErrorMessage(e.getMessage(), e);
        return;
    }
    // prompt the user for a destination collection from the list
    // $NON-NLS-1$ //$NON-NLS-2$
    final Object val = JOptionPane.showInputDialog(this, Messages.getString("ClientFrame.111"), Messages.getString("ClientFrame.112"), JOptionPane.QUESTION_MESSAGE, null, collections, collections[0]);
    if (val == null) {
        return;
    }
    final XmldbURI destinationPath = ((PrettyXmldbURI) val).getTargetURI();
    final Runnable moveTask = () -> {
        try {
            final EXistCollectionManagementService service = (EXistCollectionManagementService) // $NON-NLS-1$ //$NON-NLS-2$
            client.current.getService("CollectionManagementService", "1.0");
            for (ResourceDescriptor re : res) {
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                setStatus(Messages.getString("ClientFrame.115") + re.getName() + Messages.getString("ClientFrame.116") + destinationPath + Messages.getString("ClientFrame.117"));
                if (re.isCollection()) {
                    service.move(re.getName(), destinationPath, null);
                } else {
                    service.moveResource(re.getName(), destinationPath, null);
                }
            }
            client.reloadCollection();
        } catch (final XMLDBException e) {
            showErrorMessage(e.getMessage(), e);
        }
        // $NON-NLS-1$
        setStatus(Messages.getString("ClientFrame.118"));
    };
    client.newClientThread("move", moveTask).start();
}
Also used : Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException)

Example 7 with Collection

use of org.xmldb.api.base.Collection in project exist by eXist-db.

the class ClientFrame method exportAction.

private void exportAction(final ActionEvent ev) {
    if (fileman.getSelectedRowCount() == 0) {
        return;
    }
    final int[] rows = fileman.getSelectedRows();
    for (final int row : rows) {
        final ResourceDescriptor desc = resources.getRow(fileman.convertRowIndexToModel(row));
        if (desc.isCollection()) {
            continue;
        }
        final JFileChooser chooser = new JFileChooser(preferences.get("directory.last", System.getProperty("user.dir")));
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setSelectedFile(Paths.get(desc.getName().getCollectionPath()).toFile());
        if (chooser.showDialog(this, "Select file for export") == JFileChooser.APPROVE_OPTION) {
            preferences.put("directory.last", chooser.getCurrentDirectory().getAbsolutePath());
            final Path file = chooser.getSelectedFile().toPath();
            if (Files.exists(file) && JOptionPane.showConfirmDialog(this, "File exists. Overwrite?", "Overwrite?", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                return;
            }
            final Resource resource;
            final SAXSerializer contentSerializer;
            try {
                final Collection collection = client.getCollection();
                resource = collection.getResource(desc.getName().toString());
                if (resource instanceof ExtendedResource) {
                    try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(file))) {
                        ((ExtendedResource) resource).getContentIntoAStream(os);
                    }
                } else {
                    contentSerializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
                    try (final Writer writer = Files.newBufferedWriter(file, UTF_8)) {
                        // write resource to contentSerializer
                        contentSerializer.setOutput(writer, properties);
                        ((EXistResource) resource).setLexicalHandler(contentSerializer);
                        ((XMLResource) resource).getContentAsSAX(contentSerializer);
                    } finally {
                        SerializerPool.getInstance().returnObject(contentSerializer);
                    }
                }
            // TODO finally close os
            } catch (final Exception e) {
                System.err.println("An exception occurred" + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
Also used : Path(java.nio.file.Path) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLResource(org.xmldb.api.modules.XMLResource) URISyntaxException(java.net.URISyntaxException) XMLDBException(org.xmldb.api.base.XMLDBException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) BadLocationException(javax.swing.text.BadLocationException) SAXException(org.xml.sax.SAXException) Collection(org.xmldb.api.base.Collection) SAXSerializer(org.exist.util.serializer.SAXSerializer)

Example 8 with Collection

use of org.xmldb.api.base.Collection in project exist by eXist-db.

the class ClientFrame method copyAction.

private void copyAction(final ActionEvent ev) {
    final ResourceDescriptor[] res = getSelectedResources();
    PrettyXmldbURI[] collections;
    // get an array of collection paths
    try {
        final Collection root = client.getCollection(XmldbURI.ROOT_COLLECTION);
        final List<PrettyXmldbURI> alCollections = getCollections(root, new ArrayList<>());
        collections = new PrettyXmldbURI[alCollections.size()];
        alCollections.toArray(collections);
    } catch (final XMLDBException e) {
        showErrorMessage(e.getMessage(), e);
        return;
    }
    // prompt the user for a destination collection from the list
    // $NON-NLS-1$ //$NON-NLS-2$
    final Object val = JOptionPane.showInputDialog(this, Messages.getString("ClientFrame.128"), Messages.getString("ClientFrame.129"), JOptionPane.QUESTION_MESSAGE, null, collections, collections[0]);
    if (val == null) {
        return;
    }
    final XmldbURI destinationPath = ((PrettyXmldbURI) val).getTargetURI();
    final Runnable moveTask = () -> {
        try {
            final EXistCollectionManagementService service = (EXistCollectionManagementService) // $NON-NLS-1$ //$NON-NLS-2$
            client.current.getService("CollectionManagementService", "1.0");
            for (ResourceDescriptor re : res) {
                // TODO
                // what happens if the source and destination paths are the same?
                // we need to check and prompt the user to either skip or choose a new name
                // this function can copy multiple resources/collections selected by the user,
                // so may need to prompt the user multiple times? is in this thread the correct
                // place to do it? also need to do something similar for moveAction()
                // 
                // Its too late and brain hurts - deliriumsky
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                setStatus(Messages.getString("ClientFrame.132") + re.getName() + Messages.getString("ClientFrame.133") + destinationPath + Messages.getString("ClientFrame.134"));
                if (re.isCollection()) {
                    service.copy(re.getName(), destinationPath, null);
                } else {
                    service.copyResource(re.getName(), destinationPath, null);
                }
            }
            client.reloadCollection();
        } catch (final XMLDBException e) {
            showErrorMessage(e.getMessage(), e);
        }
        // $NON-NLS-1$
        setStatus(Messages.getString("ClientFrame.135"));
    };
    client.newClientThread("move", moveTask).start();
}
Also used : Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException)

Example 9 with Collection

use of org.xmldb.api.base.Collection in project exist by eXist-db.

the class ClientFrame method getCollections.

private ArrayList<PrettyXmldbURI> getCollections(final Collection root, final ArrayList<PrettyXmldbURI> collectionsList) throws XMLDBException {
    collectionsList.add(new PrettyXmldbURI(XmldbURI.create(root.getName())));
    final String[] childCollections = root.listChildCollections();
    Collection child = null;
    for (String childCollection : childCollections) {
        try {
            child = root.getChildCollection(childCollection);
        } catch (final XMLDBException xmldbe) {
            if (xmldbe.getCause() instanceof PermissionDeniedException) {
                continue;
            } else {
                throw xmldbe;
            }
        } catch (Exception npe) {
            System.out.println("Corrupted resource/collection skipped: " + child != null ? child.getName() != null ? child.getName() : "unknown" : "unknown");
            continue;
        }
        try {
            getCollections(child, collectionsList);
        } catch (Exception ee) {
            System.out.println("Corrupted resource/collection skipped: " + child != null ? child.getName() != null ? child.getName() : "unknown" : "unknown");
            continue;
        }
    }
    return collectionsList;
}
Also used : Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) XMLDBException(org.xmldb.api.base.XMLDBException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) BadLocationException(javax.swing.text.BadLocationException) SAXException(org.xml.sax.SAXException)

Example 10 with Collection

use of org.xmldb.api.base.Collection in project exist by eXist-db.

the class Main method restoreWithoutGui.

private static void restoreWithoutGui(final String username, final String password, final Optional<String> dbaPassword, final Path f, final XmldbURI uri, final boolean rebuildRepo, final boolean quiet, final boolean overwriteApps) {
    final AggregatingConsoleRestoreServiceTaskListener listener = new AggregatingConsoleRestoreServiceTaskListener(quiet);
    try {
        final Collection collection = DatabaseManager.getCollection(uri.toString(), username, password);
        final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
        service.restore(f.toAbsolutePath().toString(), dbaPassword.orElse(null), listener, overwriteApps);
    } catch (final XMLDBException e) {
        listener.error(e.getMessage());
    }
    if (listener.hasProblems()) {
        System.err.println(listener.getAllProblems());
    }
    if (rebuildRepo) {
        System.out.println("Rebuilding application repository ...");
        System.out.println("URI: " + uri);
        try {
            final Collection root = DatabaseManager.getCollection(uri.toString(), username, dbaPassword.orElse(password));
            if (root != null) {
                ClientFrame.repairRepository(root);
                System.out.println("Application repository rebuilt successfully.");
            } else {
                System.err.println("Failed to retrieve root collection: " + uri);
            }
        } catch (final XMLDBException e) {
            reportError(e);
            System.err.println("Rebuilding application repository failed!");
        }
    } else {
        System.out.println("\nIf you restored collections inside /db/apps, you may want\n" + "to rebuild the application repository. To do so, run the following query\n" + "as admin:\n\n" + "import module namespace repair=\"http://exist-db.org/xquery/repo/repair\"\n" + "at \"resource:org/exist/xquery/modules/expathrepo/repair.xql\";\n" + "repair:clean-all(),\n" + "repair:repair()\n");
    }
}
Also used : Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException)

Aggregations

Collection (org.xmldb.api.base.Collection)345 XMLResource (org.xmldb.api.modules.XMLResource)140 Test (org.junit.Test)115 Resource (org.xmldb.api.base.Resource)111 UserManagementService (org.exist.xmldb.UserManagementService)91 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)85 BinaryResource (org.xmldb.api.modules.BinaryResource)80 XMLDBException (org.xmldb.api.base.XMLDBException)68 ResourceSet (org.xmldb.api.base.ResourceSet)55 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)48 XPathQueryService (org.xmldb.api.modules.XPathQueryService)31 EXistResource (org.exist.xmldb.EXistResource)25 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)20 Before (org.junit.Before)20 URISyntaxException (java.net.URISyntaxException)18 Path (java.nio.file.Path)18 InputStream (java.io.InputStream)17 BuildException (org.apache.tools.ant.BuildException)14 XmldbURI (org.exist.xmldb.XmldbURI)13 Account (org.exist.security.Account)10