private static final Log log = LogFactory.getLog(XUpdate.class);
public boolean execute(Hashtable table) throws Exception {
Collection col = null;
try {
if ((String) table.get(XMLTools.COLLECTION) == null) {
System.out.println("ERROR : Collection name and switch required");
return false;
}
if ((String) table.get(XMLTools.FILE_PATH) == "") {
System.out.println("ERROR : Path to file containing XUpdate to execute required");
return false;
}
String name = (String) table.get(XMLTools.NAME_OF);
// Nomalize the collection URI
String colstring = normalizeCollectionURI((String) table.get(XMLTools.COLLECTION),
(String) table.get(XMLTools.LOCAL));
// Read in the XUpdate commands from the file
StringBuffer commands = new StringBuffer();
File file = new File((String) table.get(XMLTools.FILE_PATH));
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
commands.append(line);
}
reader.close();
// Get the collection reference for the requested collection
col = DatabaseManager.getCollection(colstring);
if (col == null) {
System.out.println("ERROR : Collection not found!");
return false;
}
// To run XUpdate commands we use the XUpdateQueryService
XUpdateQueryService service = null;
service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
long result = 0;
// See if we're updating one document or the whole collection.
if (name == null) {
result = service.update(commands.toString());
}
else {
result = service.updateResource(name, commands.toString());
}
System.out.println(result + " documents updated");
} catch (Exception e) {
System.out.println("ERROR : " + e.getMessage());
if (table.get(XMLTools.VERBOSE).equals("true")) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
return false;
} finally {
if (col != null) {
col.close();
}
}
return true;
}