package org.dbwiki.web.server;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.Properties;
import java.util.Vector;
import org.dbwiki.data.database.Database;
import org.dbwiki.data.io.ImportHandler;
import org.dbwiki.data.io.XMLDocumentImportReader;
import org.dbwiki.data.schema.DatabaseSchema;
import org.dbwiki.driver.rdbms.SQLVersionIndex;
import org.dbwiki.exception.WikiException;
import org.dbwiki.exception.WikiFatalException;
import org.dbwiki.user.User;
public class WikiServerStandalone extends WikiServer {
private Vector<DatabaseWikiStandalone> _wikiListing;
/*
* Constructors
*/
public WikiServerStandalone(Properties properties) throws org.dbwiki.exception.WikiException {
super(properties);
}
/**
* Initialize list of DatabaseWikis from database
* @param con
* @throws SQLException
* @throws WikiException
*/
protected void getWikiListing (Connection con) throws SQLException, WikiException {
_wikiListing = new Vector<DatabaseWikiStandalone>();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM " + RelationDatabase + " " +
" WHERE " + RelDatabaseColIsActive + " = " + RelDatabaseColIsActiveValTrue + " " +
"ORDER BY " + RelDatabaseColTitle);
while (rs.next()) {
int id = rs.getInt(RelDatabaseColID);
String name = rs.getString(RelDatabaseColName);
String title = rs.getString(RelDatabaseColTitle);
int layoutVersion = rs.getInt(RelDatabaseColLayout);
int templateVersion = rs.getInt(RelDatabaseColTemplate);
int styleSheetVersion = rs.getInt(RelDatabaseColCSS);
int urlDecodingVersion = RelConfigFileColFileVersionValUnknown;
if (org.dbwiki.lib.JDBC.hasColumn(rs, RelDatabaseColURLDecoding)) {
urlDecodingVersion = rs.getInt(RelDatabaseColURLDecoding);
}
int authenticationMode = rs.getInt(RelDatabaseColAuthentication);
int autoSchemaChanges = rs.getInt(RelDatabaseColAutoSchemaChanges);
ConfigSetting setting = new ConfigSetting(layoutVersion, templateVersion, styleSheetVersion, urlDecodingVersion);
_wikiListing.add(new DatabaseWikiStandalone(id, name, title, authenticationMode, autoSchemaChanges, setting, _connector, this));
}
rs.close();
stmt.close();
}
/*
* Getters
*/
/** Get the DatabaseWiki with index i */
public DatabaseWiki get(int index) {
return _wikiListing.get(index);
}
/**
*
* @return The number of DatabaseWikis
*/
public int size() {
return _wikiListing.size();
}
/** Creates new database with a given schema and import given data into it
* TODO #import Move this into a separate class, to factor out common functionality with DatabaseImport
* TODO #server Split this into server-level and data-level stuff.
* @param name - string identifying database tables
* @param title - human readable title
* @param path - path to entries in the document
* @param resource
* @param databaseSchema
* @param user
* @param authenticationMode
* @param autoSchemaChanges
* @throws NumberFormatException
* @throws WikiException
* @throws SQLException
*/
public void registerDatabase(String name, String title, String path, URL resource, DatabaseSchema databaseSchema, User user, int authenticationMode, int autoSchemaChanges)
throws NumberFormatException, WikiException, SQLException {
Connection con = _connector.getConnection();
int wikiID = -1;
SQLVersionIndex versionIndex = new SQLVersionIndex(con, name, users(), true);
CreateDatabaseRecord r = new CreateDatabaseRecord(name,title,authenticationMode,autoSchemaChanges,databaseSchema,user);
con.setAutoCommit(false);
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
try {
wikiID = r.createDatabase(con, versionIndex);
con.commit();
DatabaseWikiStandalone wiki = new DatabaseWikiStandalone(wikiID, name, title, autoSchemaChanges, authenticationMode,_connector, this,
con, versionIndex);
_wikiListing.add(wiki);
Collections.sort(_wikiListing);
//
// Import data into created database wiki if the user specified an import resource.
//
if (resource != null) {
Database database = wiki.database();
// Note that database.schema() is a copy of databaseSchema that has been read back from the database
// after being loaded in when we created new database above.
// We should really deal with the target path separately, e.g. via extra text field
XMLDocumentImportReader reader = new XMLDocumentImportReader(resource,
database.schema(),
path, user, false, false);
ImportHandler importHandler = database.createImportHandler(con);
reader.setImportHandler(importHandler);
reader.start();
}
} catch (java.sql.SQLException sqlException) {
con.rollback();
con.close();
throw new WikiFatalException(sqlException);
}
con.commit();
con.close();
}
public void sortWikiListing() {
Collections.sort(_wikiListing);
}
}