/**
*
* singleton object - more statics can be pulled out?
*/
package org.hyperdata.scute.main;
import java.io.IOException;
import org.hyperdata.scute.autosave.ModelSaver;
import org.hyperdata.scute.rdf.ModelContainer;
import org.hyperdata.scute.rdf.RdfUtils;
import org.hyperdata.scute.system.Log;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
/**
* The Class Config.
*
* @author danny
*/
public class Config extends ModelContainer {
public static String HELPSET;
// filenames all set up on init(DATA_DIR)
public static String DATA_DIR = "data/";
public static String ASSEMBLER_FILENAME;
public static String SPARQL_FILENAME;
public static String CONFIG_FILENAME;
public static String WORKING_MODEL_FILENAME;
public static String RDFXML_TEMP;
public static String TURTLE_TEMP;
public static String SCRATCH_FILENAME;
public static String ENDPOINTS_MODEL;
public static String CONFIG_URI = "http://purl.org/stuff/scute/application/config";
public static String WORKING_MODEL_URI = "http://purl.org/stuff/scute/application/working";
public static String CONFIG_MODEL_URI = "http://purl.org/stuff/scute/application/config";
public static String VERSION_STRING = "Version 0.5 Beta";
public static Syntax SPARQL_SYNTAX = Syntax.syntaxSPARQL_11;
/** The base uri. */
public static String baseUri = "http://purl.org/stuff/scute/";
public static Resource scuteResource;
/** The model. */
public static Model model;
static {
model = ModelFactory.createDefaultModel();
scuteResource = model
.createResource("http://purl.org/stuff/scute/application");
}
// system until clean shutdown
/** The loaded. */
private static boolean loaded = false;
/** The CONFIG format. */
private static String CONFIG_FORMAT = "Turtle";
/** The self. */
public static Config self; // seems a good name for a singleton
private static String SCUTE_HOME;
/**
* Instantiates a new config.
*/
private Config() {
setModel(model);
setModelFilename(CONFIG_FILENAME);
setModelURI(CONFIG_MODEL_URI);
}
// only for bootstrapping, sets & saves default values
/**
* The main method.
*
* @param args
* the arguments
*/
// public static void main(String[] args) {
// // Config config = new Config();
// // self.setDefaults();
// // self.saveNow();
// }
/**
* Sets the defaults.
*/
public void setDefaults() {
setValue("defaultFileFormat", "Turtle");
setValue("modelSaveDelay", "20000");
// setValue("modelSavePeriod", "60000");
setValue("textSaveDelay", "4000");
// setValue("textSavePeriod", "6000");
setValue("sync", "true");
setValue("selectedView", "Turtle");
}
/**
* Gets the value.
*
* @param propName
* the prop name
* @return the value
*/
public String getValue(String propName) {
if (!loaded) {
loaded = true;
load();
}
Property property = model.createProperty(baseUri + propName);
Literal valueNode = model.getRequiredProperty(scuteResource, property)
.getLiteral();
// System.out.println("getting value " + propName + " = "
// + valueNode.getString());
return valueNode.getString();
}
/**
* Sets the value.
*
* @param propName
* the prop name
* @param value
* the value
*/
public void setValue(String propName, String value) {
// System.out.println("setting value " + propName + " = " + value);
Property property = model.createProperty(baseUri + propName);
Literal valueNode = model.createLiteral(value);
scuteResource.removeAll(property);
scuteResource.addProperty(property, valueNode);
}
/**
* Gets the identifying comment.
*
* @param syntax
* the syntax
* @return the identifying comment
*/
public String getIdentifyingComment(String syntax) {
if (syntax.equals("RDF/XML"))
return "<!-- RDF/XML Syntax (autogenerated - leave this line intact) -->\n\n";
return "# Turtle Syntax (autogenerated - leave this line intact)\n\n";
}
// is needed?
/**
* Save soon.
*/
public void saveSoon() {
Thread t = new Thread(new ModelSaver(this));
t.start(); // Start the thread
}
/**
* Save now.
*/
public void saveNow() {
// System.out.println("saving config");
(new ModelSaver(this)).save();
}
/**
* Load.
*/
public void load() {
try {
RdfUtils.load(model, CONFIG_FILENAME, CONFIG_FORMAT);
System.out.println("CONFIG_FILENAME="+CONFIG_FILENAME);
} catch (IOException exception) {
// do error popup
Log.exception(exception);
System.exit(1);
}
}
// ///////////////////////////////
// accessors for hardcoded values
// /////////////////////////////////////
// accessor methods for Config.model
/**
* Gets the default file format.
*
* @return the default file format
*/
public String getDefaultFileFormat() {
return getValue("defaultFileFormat");
}
/**
* Gets the model save delay.
*
* @return the model save delay
*/
public long getModelSaveDelay() {
return Long.parseLong(getValue("modelSaveDelay"));
}
/**
* Gets the model save period.
*
* @return the model save period
*/
// public long getModelSavePeriod() {
// return Long.parseLong(getValue("modelSavePeriod"));
// }
/**
* Gets the text save delay.
*
* @return the text save delay
*/
public long getTextSaveDelay() {
return Long.parseLong(getValue("textSaveDelay"));
}
/**
* Gets the text save period.
*
* @return the text save period
*/
// public long getTextSavePeriod() {
// return Long.parseLong(getValue("textSavePeriod"));
// }
/**
* Gets the sync.
*
* @return the sync
*/
public boolean getSync() {
boolean boo = Boolean.parseBoolean(getValue("sync"));
// System.out.println("Boolean.parseBoolean(getValue(sync)); "+boo);
return boo;
}
/**
* Sets the sync.
*
* @param b
* the new sync
*/
public void setSync(boolean b) {
if (b) {
setValue("sync", "true");
} else {
setValue("sync", "false");
}
}
/**
* Gets the selected tab.
*
* @return the selected tab
*/
public String getSelectedView() {
return getValue("selectedView");
}
/**
* Sets the selected tab.
*
* @param tabIndex
* the new selected tab
*/
public void setSelectedView(String viewName) {
setValue("selectedView", viewName);
}
/**
* Shown by Help -> About
*
* @return
*/
public static String getAboutString() {
return "Scute "+VERSION_STRING;
}
public static void init(String string) {
SCUTE_HOME = string;
DATA_DIR = SCUTE_HOME + "data/";
ASSEMBLER_FILENAME = DATA_DIR + "tdb-assembler.ttl";
SPARQL_FILENAME = DATA_DIR + "sparql-temp.txt";
CONFIG_FILENAME = DATA_DIR + "config.ttl";
WORKING_MODEL_FILENAME = DATA_DIR + "working.ttl";
RDFXML_TEMP = DATA_DIR + "temp.rdf";
TURTLE_TEMP = DATA_DIR + "temp.ttl";
SCRATCH_FILENAME = DATA_DIR + "temp.txt";
ENDPOINTS_MODEL = DATA_DIR + "endpoints.ttl";
HELPSET = SCUTE_HOME + "doc/www/Scute.hs";
self = new Config();
}
}