/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: AddCollection.java 525295 2007-04-03 21:52:05Z vgritsenko $
*/
package org.apache.xindice.tools.command;
import org.apache.xindice.client.xmldb.services.CollectionManager;
import org.apache.xindice.tools.XMLTools;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import java.util.Hashtable;
/**
* AddCollection.java is designed to let the user create
* new Collections and Nested Collections within the Database.
*
* @version $Revision: 525295 $, $Date: 2007-04-03 17:52:05 -0400 (Tue, 03 Apr 2007) $
*/
public class AddCollection extends Command {
/**
* Creates collection
*/
public boolean execute(Hashtable table) throws Exception {
Collection col = null;
Collection tempcol = null;
try {
// Verify that that collection passed in is not null
if (table.get(XMLTools.COLLECTION) != null && table.get(XMLTools.NAME_OF) != null) {
// Get a Collection reference to pass on to individual commands
String colstring = normalizeCollectionURI((String) table.get(XMLTools.COLLECTION),
(String) table.get(XMLTools.LOCAL));
col = DatabaseManager.getCollection(colstring);
if (col == null) {
System.out.println("ERROR : Collection not found!");
return false;
}
// Create an instance of the collection manager and create the collection
CollectionManager colman = (CollectionManager) col.getService("CollectionManager", XMLDBAPIVERSION);
String colPath = (String) table.get(XMLTools.NAME_OF);
String colName = "";
int idx = colPath.lastIndexOf("/");
if (idx != -1) {
colName = colPath.substring(idx + 1);
} else if (idx == -1) {
colName = colPath;
}
if (colName.equals("")) {
System.out.println("Cannot create a NULL collection");
return false;
}
Document doc = new DocumentImpl();
Element colEle = doc.createElement("collection");
colEle.setAttribute("name", colName);
// FIXME Make this configurable
colEle.setAttribute("compressed", "true");
colEle.setAttribute("inline-metadata", "true");
doc.appendChild(colEle);
Element filEle = doc.createElement("filer");
String filerClass = "org.apache.xindice.core.filer.BTreeFiler";
// see if user specified filer type
if (table.containsKey(XMLTools.FILER)) {
String filer = (String) table.get(XMLTools.FILER);
if ("HashFiler".equals(filer)) {
filerClass = "org.apache.xindice.core.filer.HashFiler";
} else if (!"BTreeFiler".equals(filer)) {
System.out.println("Unknown filer: " + filer);
return false;
}
}
filEle.setAttribute("class", filerClass);
if (table.containsKey(XMLTools.PAGE_SIZE)) {
filEle.setAttribute(XMLTools.PAGE_SIZE, (String) table.get(XMLTools.PAGE_SIZE));
}
if (table.containsKey(XMLTools.MAX_KEY_SIZE)) {
filEle.setAttribute(XMLTools.MAX_KEY_SIZE, (String) table.get(XMLTools.MAX_KEY_SIZE));
}
if (table.containsKey(XMLTools.PAGE_COUNT)) {
filEle.setAttribute(XMLTools.PAGE_COUNT, (String) table.get(XMLTools.PAGE_COUNT));
}
colEle.appendChild(filEle);
tempcol = colman.createCollection(colPath, doc);
System.out.println("Created : " + table.get(XMLTools.COLLECTION) + "/" + colPath);
} else {
System.out.println("ERROR : Collection Context and New Collection name required");
}
} finally {
// Release the collection objects
if (col != null) {
col.close();
}
if (tempcol != null) {
tempcol.close();
}
}
return true;
}
}