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();
}
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();
}
}
}
}
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();
}
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;
}
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");
}
}
Aggregations