Simexplorer-si-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- 1607 discussions
r41 - in trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database: . lucene
by glandais@users.labs.libre-entreprise.org 05 Dec '07
by glandais@users.labs.libre-entreprise.org 05 Dec '07
05 Dec '07
Author: glandais
Date: 2007-12-05 15:23:47 +0000 (Wed, 05 Dec 2007)
New Revision: 41
Added:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/DatabaseConstants.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneHits.java
Removed:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java
Modified:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java
Log:
Refactoring database
Modified: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java 2007-12-05 15:23:08 UTC (rev 40)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java 2007-12-05 15:23:47 UTC (rev 41)
@@ -5,44 +5,44 @@
import org.cemagref.simexplorer.si.storage.entities.LoggableElement;
-public abstract class Database {
+public abstract class Database implements DatabaseConstants {
- public Database() throws Throwable {
+ public Database() throws Exception {
super();
init();
}
- protected abstract void init() throws Throwable;
+ protected abstract void init() throws Exception;
// Create / Update
- public abstract void pushElement(LoggableElement element) throws Throwable;
+ public abstract void pushElement(LoggableElement element) throws Exception;
// Read
public abstract LoggableElement findElement(String uuid, Integer majorVersion,
- Integer minorVersion) throws Throwable;
+ Integer minorVersion) throws Exception;
public abstract LoggableElement getElementLatestVersion(String uuid)
- throws Throwable;
+ throws Exception;
- public abstract Set<LoggableElement> findElementsById(String uuid) throws Throwable;
+ public abstract Set<LoggableElement> findElementsById(String uuid) throws Exception;
- public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties) throws Throwable;
+ public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties) throws Exception;
- public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties, Class entityClass) throws Throwable;
+ public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties, Class entityClass) throws Exception;
// Delete
- public void deleteElement(LoggableElement element) throws Throwable {
+ public void deleteElement(LoggableElement element) throws Exception {
deleteElement(element.getUuid(), element.getMajorVersion(), element
.getMinorVersion());
}
- public abstract void deleteElement(String uuid) throws Throwable;
+ public abstract void deleteElements(String uuid) throws Exception;
public abstract void deleteElement(String uuid, Integer majorVersion,
- Integer minorVersion) throws Throwable;
+ Integer minorVersion) throws Exception;
}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/DatabaseConstants.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/DatabaseConstants.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/DatabaseConstants.java 2007-12-05 15:23:47 UTC (rev 41)
@@ -0,0 +1,16 @@
+package org.cemagref.simexplorer.si.storage.database;
+
+public interface DatabaseConstants {
+
+ public static final String KEY_CLASSNAME = "simexplorer.classname";
+ public static final String KEY_UUID = "simexplorer.uuid";
+ public static final String KEY_NAME = "simexplorer.name";
+ public static final String KEY_HASH = "simexplorer.hash";
+ public static final String KEY_DESCRIPTION = "simexplorer.description";
+ public static final String KEY_MAJORVERSION = "simexplorer.version.major";
+ public static final String KEY_MINORVERSION = "simexplorer.version.minor";
+ public static final String KEY_TYPE = "simexplorer.type";
+ public static final String KEY_CREATIONDATE = "simexplorer.creationdate";
+ public static final String KEY_XML = "simexplorer.xml";
+
+}
Deleted: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java 2007-12-05 15:23:08 UTC (rev 40)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java 2007-12-05 15:23:47 UTC (rev 41)
@@ -1,11 +0,0 @@
-package org.cemagref.simexplorer.si.storage.database.lucene;
-
-public interface LuceneConstants {
-
- public static final String KEY_CLASSNAME = "simexplorer.classname";
- public static final String KEY_UUID = "simexplorer.uuid";
- public static final String KEY_DESCRIPTION = "simexplorer.description";
- public static final String KEY_MAJORVERSION = "simexplorer.version.major";
- public static final String KEY_MINORVERSION = "simexplorer.version.minor";
-
-}
Modified: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java 2007-12-05 15:23:08 UTC (rev 40)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java 2007-12-05 15:23:47 UTC (rev 41)
@@ -1,7 +1,6 @@
package org.cemagref.simexplorer.si.storage.database.lucene;
import java.io.ByteArrayInputStream;
-import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
@@ -23,58 +22,56 @@
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
-import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
-import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TermQuery;
-import org.cemagref.simexplorer.si.storage.dao.ElementDAOFactory;
-import org.cemagref.simexplorer.si.storage.dao.factories.LoggableElementFactory;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
import org.cemagref.simexplorer.si.storage.database.Database;
import org.cemagref.simexplorer.si.storage.entities.LoggableElement;
+import org.cemagref.simexplorer.si.storage.factories.LoggableElementFactory;
import org.cemagref.simexplorer.si.storage.tools.Version;
import org.w3c.dom.Element;
-public class LuceneDatabase extends Database implements LuceneConstants {// implements
- // LuceneDatabaseInterface
- // {
+public class LuceneDatabase extends Database {
- private static Analyzer analyser;
- private static IndexWriter writer;
- private static Searcher searcher;
- private static boolean initok = false;
+ private Analyzer analyser;
+ private String indexDirectory;
+ private IndexWriter writer;
+ private boolean initok = false;
- public LuceneDatabase() throws Throwable {
+ public LuceneDatabase() throws Exception {
super();
}
- protected void init() throws Throwable {
+ protected void init() throws Exception {
if (!initok) {
analyser = new PerFieldAnalyzerWrapper(new SimpleAnalyzer());
// analyser.addAnalyzer("firstname", new KeywordAnalyzer());
// analyser.addAnalyzer("lastname", new KeywordAnalyzer());
- String directory = "./index/";
+ indexDirectory = "./index/";
boolean create = false;
- if (!IndexReader.indexExists(directory)) {
- // si l'index n'existe pas, on force la creation
+ if (!IndexReader.indexExists(indexDirectory)) {
create = true;
}
- writer = new IndexWriter(directory, analyser, create);
- searcher = new IndexSearcher(directory);
+
+ Directory dir = FSDirectory.getDirectory(indexDirectory);
+
+ writer = new IndexWriter(dir, true, analyser, create);
initok = true;
}
}
@Override
- public void pushElement(LoggableElement element) throws Throwable {
- LoggableElementFactory elementFactory = (LoggableElementFactory) ElementDAOFactory
+ public void pushElement(LoggableElement element) throws Exception {
+ LoggableElementFactory elementFactory = (LoggableElementFactory) LoggableElementFactory
.getFactory(element.getClass());
Document document = saveLuceneElement(elementFactory, element);
@@ -86,57 +83,62 @@
}
writer.addDocument(document);
+ writer.flush();
}
@Override
public LoggableElement findElement(String uuid, Integer majorVersion,
- Integer minorVersion) throws Throwable {
+ Integer minorVersion) throws Exception {
+ LoggableElement result = null;
Map<String, String> properties = new HashMap<String, String>();
properties.put(KEY_UUID, uuid);
properties.put(KEY_MAJORVERSION, majorVersion.toString());
properties.put(KEY_MINORVERSION, minorVersion.toString());
- Hits hits = findHits(properties);
- if (hits.length() == 0) {
- return null;
- } else {
- return convertDocumentToElement(hits.doc(0));
+ LuceneHits hits = findHits(properties);
+ if (hits.getHits().length() != 0) {
+ result = convertDocumentToElement(hits.getHits().doc(0));
}
+ hits.closeSearcher();
+
+ return result;
}
@Override
public LoggableElement getElementLatestVersion(String uuid)
- throws Throwable {
+ throws Exception {
LoggableElement element = null;
Map<String, String> properties = new HashMap<String, String>();
properties.put(KEY_UUID, uuid);
- Hits hits = findHits(properties);
- if (hits.length() > 0) {
+ LuceneHits hits = findHits(properties);
+ if (hits.getHits().length() > 0) {
List<Version> versions = new ArrayList<Version>();
- for (int i = 0; i < hits.length(); i++) {
- Document doc = hits.doc(i);
+ for (int i = 0; i < hits.getHits().length(); i++) {
+ Document doc = hits.getHits().doc(i);
Version version = new Version();
version.setMajorVersion(new Integer(doc.get(KEY_MAJORVERSION)));
version.setMinorVersion(new Integer(doc.get(KEY_MINORVERSION)));
version.setVersionnedObject(doc);
+ versions.add(version);
}
Collections.sort(versions);
Collections.reverse(versions);
Document latestDoc = (Document) versions.get(0)
.getVersionnedObject();
- LoggableElementFactory elementFactory = (LoggableElementFactory) ElementDAOFactory
+ LoggableElementFactory elementFactory = (LoggableElementFactory) LoggableElementFactory
.getFactory(latestDoc.get(KEY_CLASSNAME));
element = loadLuceneElement(elementFactory, latestDoc);
}
+ hits.closeSearcher();
return element;
}
@Override
- public Set<LoggableElement> findElementsById(String uuid) throws Throwable {
+ public Set<LoggableElement> findElementsById(String uuid) throws Exception {
Map<String, String> properties = new HashMap<String, String>();
properties.put(KEY_UUID, uuid);
return convertHitsToElements(findHits(properties));
@@ -144,107 +146,122 @@
@Override
public Set<LoggableElement> findElementsByProperties(
- Map<String, String> properties) throws Throwable {
+ Map<String, String> properties) throws Exception {
return convertHitsToElements(findHits(properties));
}
@Override
public Set<LoggableElement> findElementsByProperties(
- Map<String, String> properties, Class entityClass) throws Throwable {
+ Map<String, String> properties, Class entityClass) throws Exception {
Map<String, String> props = new HashMap<String, String>();
props.putAll(properties);
props.put(KEY_CLASSNAME, entityClass.getName());
return convertHitsToElements(findHits(properties));
}
- public void deleteElement(String uuid) throws Throwable {
+ public void deleteElements(String uuid) throws Exception {
writer.deleteDocuments(new Term(KEY_UUID, uuid));
+ writer.flush();
}
public void deleteElement(String uuid, Integer majorVersion,
- Integer minorVersion) throws Throwable {
+ Integer minorVersion) throws Exception {
Term[] terms = new Term[3];
terms[0] = new Term(KEY_UUID, uuid);
terms[1] = new Term(KEY_MAJORVERSION, uuid);
terms[2] = new Term(KEY_MINORVERSION, uuid);
writer.deleteDocuments(terms);
+ writer.flush();
}
- private Hits findHits(Map<String, String> properties) throws Throwable {
+ private LuceneHits findHits(Map<String, String> properties)
+ throws Exception {
+ Searcher searcher = new IndexSearcher(indexDirectory);
+
// FIXME disablecoords?
BooleanQuery query = new BooleanQuery(false);
for (Entry<String, String> kv : properties.entrySet()) {
query.add(new TermQuery(new Term(kv.getKey(), kv.getValue())),
BooleanClause.Occur.MUST);
}
- return searcher.search(query);
+
+ LuceneHits luceneHits = new LuceneHits(searcher.search(query), searcher);
+ return luceneHits;
}
private LoggableElement convertDocumentToElement(Document document)
- throws Throwable {
+ throws Exception {
String className = document.get(KEY_CLASSNAME);
- LoggableElementFactory factory = (LoggableElementFactory) ElementDAOFactory
+ LoggableElementFactory factory = (LoggableElementFactory) LoggableElementFactory
.getFactory(className);
return loadLuceneElement(factory, document);
}
- private Set<LoggableElement> convertHitsToElements(Hits hits)
- throws Throwable {
+ private Set<LoggableElement> convertHitsToElements(LuceneHits hits)
+ throws Exception {
Set<LoggableElement> elements = new HashSet<LoggableElement>();
- for (int i = 0; i < hits.length(); i++) {
- Document document = hits.doc(i);
+ for (int i = 0; i < hits.getHits().length(); i++) {
+ Document document = hits.getHits().doc(i);
elements.add(convertDocumentToElement(document));
}
+ hits.closeSearcher();
return elements;
}
- private LoggableElement loadLuceneElement(LoggableElementFactory factory, Document document) throws Throwable {
- String strXMLElement = document.get("xmlElement");
+ private LoggableElement loadLuceneElement(LoggableElementFactory factory,
+ Document document) throws Exception {
+ String strXMLElement = document.get(KEY_XML);
InputStream is = new ByteArrayInputStream(strXMLElement.getBytes());
+
org.w3c.dom.Document xmlDocument = factory.getBuilder().parse(is);
-
- LoggableElement element = factory.loadXMLElement((Element) xmlDocument.getFirstChild());
-
+
+ LoggableElement element = factory.loadXMLElement((Element) xmlDocument
+ .getFirstChild());
+
return element;
}
-
- private Document saveLuceneElement(LoggableElementFactory factory, LoggableElement element) throws Throwable {
+
+ private Document saveLuceneElement(LoggableElementFactory factory,
+ LoggableElement element) throws Exception {
Document document = new Document();
-
+
// FIXME save searchable/metadata fields
- document.add(new Field(KEY_UUID, element.getUuid().getBytes(),
- Field.Store.YES));
- document.add(new Field(KEY_DESCRIPTION, element.getDescription().getBytes(),
- Field.Store.YES));
- document.add(new Field(KEY_CLASSNAME, element.getClass().getName().getBytes(),
- Field.Store.YES));
- document.add(new Field(KEY_MINORVERSION, element.getMinorVersion().toString().getBytes(),
- Field.Store.YES));
- document.add(new Field(KEY_MAJORVERSION, element.getMajorVersion().toString().getBytes(),
- Field.Store.YES));
-
- org.w3c.dom.Document xmlDocument = factory.getBuilder().newDocument();
- // Propriétés du DOM
- xmlDocument.setXmlVersion("1.0");
- xmlDocument.setXmlStandalone(true);
- // Création de l'arborescence du DOM
- Element racine = xmlDocument.createElement(element.getClass().getName());
- factory.saveXMLElement(xmlDocument, racine, element);
- xmlDocument.appendChild(racine);
- DOMSource domSource = new DOMSource(xmlDocument);
- StringWriter writer = new StringWriter();
- StreamResult result = new StreamResult(writer);
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- transformer.transform(domSource, result);
- String stringResult = writer.toString();
- document.add(new Field("xmlElement", stringResult, Field.Store.COMPRESS, Field.Index.NO));
-
+ document.add(new Field(KEY_UUID, element.getUuid(), Field.Store.YES,
+ Field.Index.UN_TOKENIZED));
+ document.add(new Field(KEY_DESCRIPTION, element.getDescription(),
+ Field.Store.YES, Field.Index.UN_TOKENIZED));
+ document.add(new Field(KEY_CLASSNAME, element.getClass().getName(),
+ Field.Store.YES, Field.Index.UN_TOKENIZED));
+ document.add(new Field(KEY_MINORVERSION, element.getMinorVersion()
+ .toString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
+ document.add(new Field(KEY_MAJORVERSION, element.getMajorVersion()
+ .toString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
+
+ org.w3c.dom.Document xmlDocument = factory.getBuilder().newDocument();
+ // Propriétés du DOM
+ xmlDocument.setXmlVersion("1.0");
+ xmlDocument.setXmlStandalone(true);
+ // Création de l'arborescence du DOM
+ Element racine = xmlDocument.createElement(element.getClass()
+ .getSimpleName());
+ factory.saveXMLElement(xmlDocument, racine, element);
+ xmlDocument.appendChild(racine);
+ DOMSource domSource = new DOMSource(xmlDocument);
+ StringWriter writer = new StringWriter();
+ StreamResult result = new StreamResult(writer);
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer transformer = tf.newTransformer();
+ transformer.transform(domSource, result);
+ String stringResult = writer.toString();
+ document.add(new Field(KEY_XML, stringResult,
+ Field.Store.COMPRESS, Field.Index.NO));
+
return document;
}
- public void shutDown() throws Throwable {
+ public void shutDown() throws Exception {
writer.close();
- }
+ }
+
}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneHits.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneHits.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneHits.java 2007-12-05 15:23:47 UTC (rev 41)
@@ -0,0 +1,37 @@
+package org.cemagref.simexplorer.si.storage.database.lucene;
+
+import org.apache.lucene.search.Hits;
+import org.apache.lucene.search.Searcher;
+
+public class LuceneHits {
+
+ private Hits hits;
+ private Searcher searcher;
+
+ public LuceneHits(Hits hits, Searcher searcher) {
+ super();
+ this.hits = hits;
+ this.searcher = searcher;
+ }
+
+ public void closeSearcher() throws Exception {
+ searcher.close();
+ }
+
+ public Hits getHits() {
+ return hits;
+ }
+
+ public void setHits(Hits hits) {
+ this.hits = hits;
+ }
+
+ public Searcher getSearcher() {
+ return searcher;
+ }
+
+ public void setSearcher(Searcher searcher) {
+ this.searcher = searcher;
+ }
+
+}
1
0
r40 - trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test
by glandais@users.labs.libre-entreprise.org 05 Dec '07
by glandais@users.labs.libre-entreprise.org 05 Dec '07
05 Dec '07
Author: glandais
Date: 2007-12-05 15:23:08 +0000 (Wed, 05 Dec 2007)
New Revision: 40
Modified:
trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test/LuceneDatabaseTestCase.java
Log:
JUnit tests
Modified: trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test/LuceneDatabaseTestCase.java
===================================================================
--- trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test/LuceneDatabaseTestCase.java 2007-12-05 15:22:53 UTC (rev 39)
+++ trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test/LuceneDatabaseTestCase.java 2007-12-05 15:23:08 UTC (rev 40)
@@ -1,9 +1,13 @@
package org.cemagref.simexplorer.si.storage.test;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
import java.util.UUID;
import junit.framework.TestCase;
+import org.cemagref.simexplorer.si.storage.database.DatabaseConstants;
import org.cemagref.simexplorer.si.storage.database.lucene.LuceneDatabase;
import org.cemagref.simexplorer.si.storage.entities.ExplorationData;
import org.cemagref.simexplorer.si.storage.entities.LoggableElement;
@@ -14,100 +18,270 @@
protected void setUp() throws Exception {
super.setUp();
- try {
- database = new LuceneDatabase();
- } catch (Throwable e) {
- throw new Exception(e);
- }
+ database = new LuceneDatabase();
}
protected void tearDown() throws Exception {
super.tearDown();
- try {
- database.shutDown();
- } catch (Throwable e) {
- throw new Exception(e);
- }
+ database.shutDown();
}
- public final void testPushElement() {
- try {
- String description1 = UUID.randomUUID().toString();
- String uuid1 = UUID.randomUUID().toString();
- Integer majorVersion1 = 5;
- Integer minorVersion1 = 12;
- ExplorationData explorationData1 = new ExplorationData();
- explorationData1.setUuid(uuid1);
- explorationData1.setMajorVersion(majorVersion1);
- explorationData1.setMinorVersion(minorVersion1);
- explorationData1.setDescription(description1);
- database.pushElement(explorationData1);
-
- LoggableElement element = database.findElement(uuid1, majorVersion1, minorVersion1);
- assertNotNull(element);
- assertEquals(description1, element.getDescription());
+ public final void testPushFindElement() throws Exception {
+ String description1 = UUID.randomUUID().toString();
+ String uuid1 = UUID.randomUUID().toString();
+ Integer majorVersion1 = 5;
+ Integer minorVersion1 = 12;
+ ExplorationData explorationData1 = new ExplorationData();
+ explorationData1.setUuid(uuid1);
+ explorationData1.setMajorVersion(majorVersion1);
+ explorationData1.setMinorVersion(minorVersion1);
+ explorationData1.setDescription(description1);
+ database.pushElement(explorationData1);
- String description2 = UUID.randomUUID().toString();
- ExplorationData explorationData2 = new ExplorationData();
- explorationData2.setUuid(uuid1);
- explorationData2.setMajorVersion(majorVersion1);
- explorationData2.setMinorVersion(minorVersion1);
- explorationData2.setDescription(description2);
- database.pushElement(explorationData2);
-
- assertEquals(description2, database.findElement(uuid1, majorVersion1, minorVersion1).getDescription());
-
- String description3 = UUID.randomUUID().toString();
- Integer minorVersion3 = minorVersion1 + 1;
- ExplorationData explorationData3 = new ExplorationData();
- explorationData3.setUuid(uuid1);
- explorationData3.setMajorVersion(majorVersion1);
- explorationData3.setMinorVersion(minorVersion3);
- explorationData3.setDescription(description3);
- database.pushElement(explorationData3);
+ LoggableElement element = database.findElement(uuid1, majorVersion1,
+ minorVersion1);
+ assertNotNull(element);
+ assertEquals(description1, element.getDescription());
- assertEquals(description3, database.findElement(uuid1, majorVersion1, minorVersion3).getDescription());
-
- } catch (Throwable e) {
- fail(e.toString());
- }
- }
+ String description2 = UUID.randomUUID().toString();
+ ExplorationData explorationData2 = new ExplorationData();
+ explorationData2.setUuid(uuid1);
+ explorationData2.setMajorVersion(majorVersion1);
+ explorationData2.setMinorVersion(minorVersion1);
+ explorationData2.setDescription(description2);
+ database.pushElement(explorationData2);
- public final void testFindElement() {
- fail("Not yet implemented"); // TODO
- }
+ assertEquals(description2, database.findElement(uuid1, majorVersion1,
+ minorVersion1).getDescription());
- public final void testGetElementLatestVersion() {
- fail("Not yet implemented"); // TODO
+ String description3 = UUID.randomUUID().toString();
+ Integer minorVersion3 = minorVersion1 + 1;
+ ExplorationData explorationData3 = new ExplorationData();
+ explorationData3.setUuid(uuid1);
+ explorationData3.setMajorVersion(majorVersion1);
+ explorationData3.setMinorVersion(minorVersion3);
+ explorationData3.setDescription(description3);
+ database.pushElement(explorationData3);
+
+ assertEquals(description3, database.findElement(uuid1, majorVersion1,
+ minorVersion3).getDescription());
}
- public final void testFindElementsById() {
- fail("Not yet implemented"); // TODO
+ public final void testGetElementLatestVersion() throws Exception {
+ String description1 = UUID.randomUUID().toString();
+ String uuid1 = UUID.randomUUID().toString();
+ Integer majorVersion1 = 5;
+ Integer minorVersion1 = 12;
+ ExplorationData explorationData1 = new ExplorationData();
+ explorationData1.setUuid(uuid1);
+ explorationData1.setMajorVersion(majorVersion1);
+ explorationData1.setMinorVersion(minorVersion1);
+ explorationData1.setDescription(description1);
+ database.pushElement(explorationData1);
+
+ String description2 = UUID.randomUUID().toString();
+ Integer majorVersion2 = 5;
+ Integer minorVersion2 = minorVersion1 + 1;
+ ExplorationData explorationData2 = new ExplorationData();
+ explorationData2.setUuid(uuid1);
+ explorationData2.setMajorVersion(majorVersion2);
+ explorationData2.setMinorVersion(minorVersion2);
+ explorationData2.setDescription(description2);
+ database.pushElement(explorationData2);
+
+ assertEquals(description2, database.getElementLatestVersion(uuid1).getDescription());
+
+ String description3 = UUID.randomUUID().toString();
+ Integer majorVersion3 = majorVersion1 + 1;
+ Integer minorVersion3 = 0;
+ ExplorationData explorationData3 = new ExplorationData();
+ explorationData3.setUuid(uuid1);
+ explorationData3.setMajorVersion(majorVersion3);
+ explorationData3.setMinorVersion(minorVersion3);
+ explorationData3.setDescription(description3);
+ database.pushElement(explorationData3);
+
+ assertEquals(description3, database.getElementLatestVersion(uuid1).getDescription());
}
- public final void testFindElementsByPropertiesMapOfStringString() {
- fail("Not yet implemented"); // TODO
+ public final void testFindElementsById() throws Exception {
+ String description1 = UUID.randomUUID().toString();
+ String uuid1 = UUID.randomUUID().toString();
+ Integer majorVersion1 = 5;
+ Integer minorVersion1 = 12;
+ ExplorationData explorationData1 = new ExplorationData();
+ explorationData1.setUuid(uuid1);
+ explorationData1.setMajorVersion(majorVersion1);
+ explorationData1.setMinorVersion(minorVersion1);
+ explorationData1.setDescription(description1);
+ database.pushElement(explorationData1);
+
+ String description2 = UUID.randomUUID().toString();
+ Integer majorVersion2 = 5;
+ Integer minorVersion2 = minorVersion1 + 1;
+ ExplorationData explorationData2 = new ExplorationData();
+ explorationData2.setUuid(uuid1);
+ explorationData2.setMajorVersion(majorVersion2);
+ explorationData2.setMinorVersion(minorVersion2);
+ explorationData2.setDescription(description2);
+ database.pushElement(explorationData2);
+
+ String description3 = UUID.randomUUID().toString();
+ Integer majorVersion3 = majorVersion1 + 1;
+ Integer minorVersion3 = 0;
+ ExplorationData explorationData3 = new ExplorationData();
+ explorationData3.setUuid(uuid1);
+ explorationData3.setMajorVersion(majorVersion3);
+ explorationData3.setMinorVersion(minorVersion3);
+ explorationData3.setDescription(description3);
+ database.pushElement(explorationData3);
+
+ Set<LoggableElement> elements = database.findElementsById(uuid1);
+ assertEquals(3, elements.size());
+
}
- public final void testFindElementsByPropertiesMapOfStringStringClass() {
- fail("Not yet implemented"); // TODO
+ public final void testFindElementsByPropertiesMapOfStringString() throws Exception {
+
+ String description1 = UUID.randomUUID().toString();
+ String uuid1 = UUID.randomUUID().toString();
+ Integer majorVersion1 = 5;
+ Integer minorVersion1 = 12;
+ ExplorationData explorationData1 = new ExplorationData();
+ explorationData1.setUuid(uuid1);
+ explorationData1.setMajorVersion(majorVersion1);
+ explorationData1.setMinorVersion(minorVersion1);
+ explorationData1.setDescription(description1);
+ database.pushElement(explorationData1);
+
+ String description2 = UUID.randomUUID().toString();
+ Integer majorVersion2 = 5;
+ Integer minorVersion2 = minorVersion1 + 1;
+ ExplorationData explorationData2 = new ExplorationData();
+ explorationData2.setUuid(uuid1);
+ explorationData2.setMajorVersion(majorVersion2);
+ explorationData2.setMinorVersion(minorVersion2);
+ explorationData2.setDescription(description2);
+ database.pushElement(explorationData2);
+
+ String description3 = UUID.randomUUID().toString();
+ Integer majorVersion3 = majorVersion1 + 1;
+ Integer minorVersion3 = 0;
+ ExplorationData explorationData3 = new ExplorationData();
+ explorationData3.setUuid(uuid1);
+ explorationData3.setMajorVersion(majorVersion3);
+ explorationData3.setMinorVersion(minorVersion3);
+ explorationData3.setDescription(description3);
+ database.pushElement(explorationData3);
+
+ // TODO add some other classes in db
+
+ Map<String, String> properties = new HashMap<String, String>();
+ properties.put(DatabaseConstants.KEY_UUID, uuid1);
+ properties.put(DatabaseConstants.KEY_MAJORVERSION, "5");
+
+ Set<LoggableElement> elements = database.findElementsByProperties(properties);
+ assertEquals(2, elements.size());
+
}
- public final void testDeleteElementString() {
- fail("Not yet implemented"); // TODO
+ public final void testFindElementsByPropertiesMapOfStringStringClass() throws Exception {
+
+ String description1 = UUID.randomUUID().toString();
+ String uuid1 = UUID.randomUUID().toString();
+ Integer majorVersion1 = 5;
+ Integer minorVersion1 = 12;
+ ExplorationData explorationData1 = new ExplorationData();
+ explorationData1.setUuid(uuid1);
+ explorationData1.setMajorVersion(majorVersion1);
+ explorationData1.setMinorVersion(minorVersion1);
+ explorationData1.setDescription(description1);
+ database.pushElement(explorationData1);
+
+ String description2 = UUID.randomUUID().toString();
+ Integer majorVersion2 = 5;
+ Integer minorVersion2 = minorVersion1 + 1;
+ ExplorationData explorationData2 = new ExplorationData();
+ explorationData2.setUuid(uuid1);
+ explorationData2.setMajorVersion(majorVersion2);
+ explorationData2.setMinorVersion(minorVersion2);
+ explorationData2.setDescription(description2);
+ database.pushElement(explorationData2);
+
+ String description3 = UUID.randomUUID().toString();
+ Integer majorVersion3 = majorVersion1 + 1;
+ Integer minorVersion3 = 0;
+ ExplorationData explorationData3 = new ExplorationData();
+ explorationData3.setUuid(uuid1);
+ explorationData3.setMajorVersion(majorVersion3);
+ explorationData3.setMinorVersion(minorVersion3);
+ explorationData3.setDescription(description3);
+ database.pushElement(explorationData3);
+
+ Map<String, String> properties = new HashMap<String, String>();
+ properties.put(DatabaseConstants.KEY_UUID, uuid1);
+ properties.put(DatabaseConstants.KEY_MAJORVERSION, "5");
+
+ Set<LoggableElement> elements = database.findElementsByProperties(properties, ExplorationData.class);
+ assertEquals(2, elements.size());
+
}
- public final void testDeleteElementStringIntegerInteger() {
- fail("Not yet implemented"); // TODO
+ public final void testDeleteElement() throws Exception {
+
+ String description1 = UUID.randomUUID().toString();
+ String uuid1 = UUID.randomUUID().toString();
+ Integer majorVersion1 = 5;
+ Integer minorVersion1 = 12;
+ ExplorationData explorationData1 = new ExplorationData();
+ explorationData1.setUuid(uuid1);
+ explorationData1.setMajorVersion(majorVersion1);
+ explorationData1.setMinorVersion(minorVersion1);
+ explorationData1.setDescription(description1);
+ database.pushElement(explorationData1);
+
+ database.deleteElement(explorationData1);
+ assertNull(database.findElement(uuid1, majorVersion1, minorVersion1));
+
}
- public final void testLuceneDatabase() {
- try {
- LuceneDatabase database = new LuceneDatabase();
- } catch (Throwable e) {
- fail(e.toString());
- }
-
+ public final void testDeleteElements() throws Exception {
+
+ String description1 = UUID.randomUUID().toString();
+ String uuid1 = UUID.randomUUID().toString();
+ Integer majorVersion1 = 5;
+ Integer minorVersion1 = 12;
+ ExplorationData explorationData1 = new ExplorationData();
+ explorationData1.setUuid(uuid1);
+ explorationData1.setMajorVersion(majorVersion1);
+ explorationData1.setMinorVersion(minorVersion1);
+ explorationData1.setDescription(description1);
+ database.pushElement(explorationData1);
+
+ String description2 = UUID.randomUUID().toString();
+ Integer majorVersion2 = 5;
+ Integer minorVersion2 = minorVersion1 + 1;
+ ExplorationData explorationData2 = new ExplorationData();
+ explorationData2.setUuid(uuid1);
+ explorationData2.setMajorVersion(majorVersion2);
+ explorationData2.setMinorVersion(minorVersion2);
+ explorationData2.setDescription(description2);
+ database.pushElement(explorationData2);
+
+ String description3 = UUID.randomUUID().toString();
+ Integer majorVersion3 = majorVersion1 + 1;
+ Integer minorVersion3 = 0;
+ ExplorationData explorationData3 = new ExplorationData();
+ explorationData3.setUuid(uuid1);
+ explorationData3.setMajorVersion(majorVersion3);
+ explorationData3.setMinorVersion(minorVersion3);
+ explorationData3.setDescription(description3);
+ database.pushElement(explorationData3);
+
+ database.deleteElements(uuid1);
+
+ assertEquals(0, database.findElementsById(uuid1).size());
+
}
}
1
0
r39 - in trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage: . factories
by glandais@users.labs.libre-entreprise.org 05 Dec '07
by glandais@users.labs.libre-entreprise.org 05 Dec '07
05 Dec '07
Author: glandais
Date: 2007-12-05 15:22:53 +0000 (Wed, 05 Dec 2007)
New Revision: 39
Added:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/CodeFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ComponentFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ConstantFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ConstantValueFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/DescriptorFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/EntityFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationApplicationFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationDataFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/LibraryFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/LoggableElementFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/RepositoryFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ResultFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/StructureFactory.java
Removed:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/DescriptorFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationApplicationFactory.java
Log:
Refactoring factories
Copied: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories (from rev 31, trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories)
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/CodeFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/CodeFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/CodeFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,32 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Code;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class CodeFactory<E extends Code> extends EntityFactory<Code> {
+
+ public static final String KEY_LANGUAGE = "language";
+ public static final String KEY_CODE = "code";
+
+ @Override
+ public Code createInstance() {
+ return new Code();
+ }
+
+ @Override
+ public Code loadXMLElement(Element xmlElement) throws Exception {
+ Code code = super.loadXMLElement(xmlElement);
+ code.setLanguage(getProperty(xmlElement, KEY_LANGUAGE));
+ code.setCode(getProperty(xmlElement, KEY_CODE));
+ return code;
+ }
+
+ @Override
+ public void saveXMLElement(Document document, Element xmlElement,
+ Code element) throws Exception {
+ setProperty(document, xmlElement, KEY_LANGUAGE, element.getLanguage());
+ setProperty(document, xmlElement, KEY_CODE, element.getCode());
+ }
+
+}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ComponentFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ComponentFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ComponentFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,58 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Code;
+import org.cemagref.simexplorer.si.storage.entities.Component;
+import org.cemagref.simexplorer.si.storage.entities.Constant;
+import org.cemagref.simexplorer.si.storage.entities.Library;
+import org.cemagref.simexplorer.si.storage.entities.Structure;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class ComponentFactory<E extends Component> extends
+ LoggableElementFactory<Component> {
+
+ public static final String KEY_CONSTANTS = "constants";
+ public static final String KEY_CONSTANT_NODE = "constant";
+ public static final String KEY_STRUCTURE = "structures";
+ public static final String KEY_STRUCTURE_NODE = "structure";
+ public static final String KEY_CODES = "codes";
+ public static final String KEY_CODE_NODE = "code";
+ public static final String KEY_LIBRARIES = "libraries";
+ public static final String KEY_LIBRARY_NODE = "library";
+
+ @Override
+ public Component createInstance() {
+ return new Component();
+ }
+
+ @Override
+ public Component loadXMLElement(Element xmlElement) throws Exception {
+ Component component = super.loadXMLElement(xmlElement);
+
+ component.setConstants(loadCollection(KEY_CONSTANTS, KEY_CONSTANT_NODE,
+ Constant.class, xmlElement));
+ component.setStructures(loadCollection(KEY_STRUCTURE,
+ KEY_STRUCTURE_NODE, Structure.class, xmlElement));
+ component.setCodes(loadCollection(KEY_CODES, KEY_CODE_NODE, Code.class,
+ xmlElement));
+ component.setLibraries(loadCollection(KEY_LIBRARIES, KEY_LIBRARY_NODE,
+ Library.class, xmlElement));
+
+ return component;
+ }
+
+ @Override
+ public void saveXMLElement(Document document, Element xmlElement,
+ Component element) throws Exception {
+ saveCollection(element.getConstants(), KEY_CONSTANTS,
+ KEY_CONSTANT_NODE, Constant.class, document, xmlElement);
+ saveCollection(element.getStructures(), KEY_STRUCTURE,
+ KEY_STRUCTURE_NODE, Structure.class, document, xmlElement);
+ saveCollection(element.getCodes(), KEY_CODES, KEY_CODE_NODE,
+ Code.class, document, xmlElement);
+ saveCollection(element.getLibraries(), KEY_LIBRARIES, KEY_LIBRARY_NODE,
+ Library.class, document, xmlElement);
+ super.saveXMLElement(document, xmlElement, element);
+ }
+
+}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ConstantFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ConstantFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ConstantFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,32 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Constant;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class ConstantFactory<E extends Constant> extends EntityFactory<Constant> {
+
+ public static final String KEY_NAME = "name";
+ public static final String KEY_TYPE = "type";
+
+ @Override
+ public Constant createInstance() {
+ return new Constant();
+ }
+
+ @Override
+ public Constant loadXMLElement(Element xmlElement) throws Exception {
+ Constant constant = super.loadXMLElement(xmlElement);
+ constant.setName(getProperty(xmlElement, KEY_NAME));
+ constant.setType(Class.forName(getProperty(xmlElement, KEY_TYPE)));
+ return constant;
+ }
+
+ @Override
+ public void saveXMLElement(Document document, Element xmlElement,
+ Constant element) throws Exception {
+ setProperty(document, xmlElement, KEY_NAME, element.getName());
+ setProperty(document, xmlElement, KEY_TYPE, element.getClass().getName());
+ }
+
+}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ConstantValueFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ConstantValueFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ConstantValueFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,40 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Constant;
+import org.cemagref.simexplorer.si.storage.entities.ConstantValue;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+
+public class ConstantValueFactory<E extends ConstantValue> extends EntityFactory<ConstantValue> {
+
+ public static final String KEY_CONSTANT = "constant";
+ public static final String KEY_VALUE = "value";
+
+ @Override
+ public ConstantValue createInstance() {
+ return new ConstantValue();
+ }
+
+ @Override
+ public ConstantValue loadXMLElement(Element xmlElement) throws Exception {
+ ConstantValue constantValue = super.loadXMLElement(xmlElement);
+ constantValue.setConstant((Constant) getFactory(Constant.class).loadXMLElement(getElementByTagName(xmlElement, KEY_CONSTANT)));
+ constantValue.setValue(getProperty(xmlElement, KEY_VALUE));
+ return constantValue;
+ }
+
+ @Override
+ public void saveXMLElement(Document document, Element xmlElement,
+ ConstantValue element) throws Exception {
+
+ Element xmlConstant = document.createElement(KEY_CONSTANT);
+ getFactory(Constant.class).saveXMLElement(document, xmlElement, element.getConstant());
+ xmlElement.appendChild(xmlConstant);
+
+ setProperty(document, xmlElement, KEY_VALUE, element.getValue());
+ }
+
+
+
+}
Deleted: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/DescriptorFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/DescriptorFactory.java 2007-12-03 17:37:30 UTC (rev 31)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/DescriptorFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -1,26 +0,0 @@
-package org.cemagref.simexplorer.si.storage.dao.factories;
-
-import org.cemagref.simexplorer.si.storage.dao.EntityFactory;
-import org.cemagref.simexplorer.si.storage.entities.Descriptor;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-
-public class DescriptorFactory<E extends Descriptor> extends EntityFactory<Descriptor> {
-
- public Descriptor createInstance() {
- return new Descriptor();
- }
-
- public Descriptor loadXMLElement(Element xmlElement) throws Throwable {
- Descriptor descriptor = createInstance();
- descriptor.setName(xmlElement.getAttribute("name"));
- descriptor.setValue(xmlElement.getAttribute("value"));
- return descriptor;
- }
-
- public void saveXMLElement(org.w3c.dom.Document document, Element xmlElement, Descriptor element) throws Throwable {
- xmlElement.setAttribute("name", element.getName());
- xmlElement.setAttribute("value", element.getValue());
- }
-
-}
Copied: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/DescriptorFactory.java (from rev 35, trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/DescriptorFactory.java)
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/DescriptorFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/DescriptorFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,28 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Descriptor;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+public class DescriptorFactory<E extends Descriptor> extends EntityFactory<Descriptor> {
+
+ public static final String KEY_NAME = "name";
+ public static final String KEY_VALUE = "value";
+
+ public Descriptor createInstance() {
+ return new Descriptor();
+ }
+
+ public Descriptor loadXMLElement(Element xmlElement) throws Exception {
+ Descriptor descriptor = super.loadXMLElement(xmlElement);
+ descriptor.setName(xmlElement.getAttribute(KEY_NAME));
+ descriptor.setValue(xmlElement.getAttribute(KEY_VALUE));
+ return descriptor;
+ }
+
+ public void saveXMLElement(org.w3c.dom.Document document, Element xmlElement, Descriptor element) throws Exception {
+ xmlElement.setAttribute(KEY_NAME, element.getName());
+ xmlElement.setAttribute(KEY_VALUE, element.getValue());
+ }
+
+}
Copied: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/EntityFactory.java (from rev 35, trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/EntityFactory.java)
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/EntityFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/EntityFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,146 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.cemagref.simexplorer.si.storage.entities.Entity;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+
+public abstract class EntityFactory<E extends Entity> {
+
+ private final static String PACKAGE_DAO = "org.cemagref.simexplorer.si.storage.factories";
+
+ private static Map<String, EntityFactory> factories = new HashMap<String, EntityFactory>();
+
+ public static EntityFactory getFactory(String entityClassName)
+ throws Exception {
+ EntityFactory entityFactory = factories.get(entityClassName);
+ if (entityFactory == null) {
+
+ StringTokenizer st = new StringTokenizer(entityClassName, ".");
+ String simpleClassName = null;
+ while (st.hasMoreTokens()) {
+ simpleClassName = st.nextToken();
+ }
+
+ String elementFactoryClassName = simpleClassName + "Factory";
+
+ Class elementFactoryClass = Class.forName(PACKAGE_DAO + "."
+ + elementFactoryClassName);
+
+ entityFactory = (EntityFactory) elementFactoryClass.newInstance();
+ factories.put(entityClassName, entityFactory);
+ }
+
+ return entityFactory;
+ }
+
+ /**
+ * @param entityClass
+ * @return
+ * @throws Throwable
+ */
+ public static EntityFactory getFactory(Class entityClass) throws Exception {
+ return getFactory(entityClass.getName());
+ }
+
+ public abstract E createInstance();
+
+ public E loadXMLElement(Element xmlElement) throws Exception {
+ E element = createInstance();
+ return element;
+ }
+
+ protected Set<Element> getElementsByTagName(Element xmlElement,
+ String tagName) {
+ Set<Element> elements = new HashSet<Element>();
+
+ NodeList nodes = xmlElement.getChildNodes();
+ for (int i = 0; i < nodes.getLength(); i++) {
+ Node node = nodes.item(i);
+ if (node instanceof Element
+ && tagName.equals(((Element) node).getTagName())) {
+ elements.add((Element) node);
+ }
+ }
+ return elements;
+ }
+
+ protected Element getElementByTagName(Element xmlElement, String tagName) {
+ Set<Element> elements = getElementsByTagName(xmlElement, tagName);
+ if (elements.size() > 0) {
+ return elements.iterator().next();
+ } else {
+ return null;
+ }
+ }
+
+ protected void setProperty(Document document, Element xmlElement,
+ String tagName, Object value) {
+ if (value != null) {
+ Element xmlProperty = document.createElement(tagName);
+ Text xmlDescriptionText = document.createTextNode(value.toString());
+ xmlProperty.appendChild(xmlDescriptionText);
+ xmlElement.appendChild(xmlProperty);
+ }
+ }
+
+ protected String getProperty(Element xmlElement, String tagName) {
+ Element element = getElementByTagName(xmlElement, tagName);
+ if (element != null) {
+ return element.getFirstChild().getNodeValue();
+ } else {
+ return null;
+ }
+ }
+
+ public abstract void saveXMLElement(Document document, Element xmlElement,
+ E element) throws Exception;
+
+ public Set loadCollection(String tagSetName, String tagName, Class clazz,
+ Element xmlElement) throws Exception {
+ Set entities = new HashSet();
+
+ Element xmlCollection = getElementByTagName(xmlElement, tagSetName);
+ if (xmlCollection != null) {
+ EntityFactory elementFactory = getFactory(clazz);
+
+ Set<Element> list = getElementsByTagName(xmlCollection, tagName);
+ for (Iterator iterator = list.iterator(); iterator.hasNext();) {
+ Element element = (Element) iterator.next();
+ Entity entity = elementFactory.loadXMLElement(element);
+ entities.add(entity);
+ }
+ }
+
+ return entities;
+ }
+
+ public void saveCollection(Set entities, String tagSetName, String tagName,
+ Class clazz, Document document, Element xmlElement)
+ throws Exception {
+ if (entities != null && entities.size() > 0) {
+ Element xmlCollection = document.createElement(tagSetName);
+ EntityFactory elementFactory = getFactory(clazz);
+ for (Object entity : entities) {
+ Element childElement = document.createElement(tagName);
+ // TODO
+ // if (entity instanceof LoggableElement) {
+ // elementFactory.saveXMLElementByReference
+ elementFactory.saveXMLElement(document, childElement,
+ (Entity) entity);
+ xmlCollection.appendChild(childElement);
+ }
+ xmlElement.appendChild(xmlCollection);
+ }
+ }
+
+}
Deleted: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationApplicationFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationApplicationFactory.java 2007-12-03 17:37:30 UTC (rev 31)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationApplicationFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -1,35 +0,0 @@
-package org.cemagref.simexplorer.si.storage.dao.factories;
-
-import java.util.Set;
-
-import org.cemagref.simexplorer.si.storage.dao.ElementDAOFactory;
-import org.cemagref.simexplorer.si.storage.dao.EntityFactory;
-import org.cemagref.simexplorer.si.storage.dao.LoggableElementFactory;
-import org.cemagref.simexplorer.si.storage.entities.Component;
-import org.cemagref.simexplorer.si.storage.entities.Entity;
-import org.cemagref.simexplorer.si.storage.entities.ExplorationApplication;
-import org.cemagref.simexplorer.si.storage.entities.ExplorationData;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-
-public class ExplorationApplicationFactory extends LoggableElementFactory<ExplorationApplication> {
-
- @Override
- public void saveXMLElement(Document document, Element xmlElement, ExplorationApplication element) throws Throwable {
- super.saveXMLElement(document, xmlElement, element);
- saveCollection(element.getExplorations(), "data", "explorationData", ExplorationData.class.getClass(), document, xmlElement);
- saveCollection(element.getComponents(), "components", "component", Component.class.getClass(), document, xmlElement);
- }
-
- @Override
- public ExplorationApplication loadXMLElement(Element xmlElement) throws Throwable {
- ExplorationApplication explorationApplication = super.loadXMLElement(xmlElement);
- return explorationApplication;
- }
-
- public ExplorationApplication createInstance() {
- return new ExplorationApplication();
- }
-
-}
Copied: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationApplicationFactory.java (from rev 35, trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationApplicationFactory.java)
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationApplicationFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationApplicationFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,44 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Component;
+import org.cemagref.simexplorer.si.storage.entities.ExplorationApplication;
+import org.cemagref.simexplorer.si.storage.entities.ExplorationData;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class ExplorationApplicationFactory extends
+ LoggableElementFactory<ExplorationApplication> {
+
+ public static final String KEY_DATA = "data";
+ public static final String KEY_DATA_NODE = "explorationdata";
+ public static final String KEY_COMPONENTS = "components";
+ public static final String KEY_COMPONENT_NODE = "component";
+
+ @Override
+ public void saveXMLElement(Document document, Element xmlElement,
+ ExplorationApplication element) throws Exception {
+ super.saveXMLElement(document, xmlElement, element);
+ saveCollection(element.getExplorations(), KEY_DATA, KEY_DATA_NODE,
+ ExplorationData.class.getClass(), document, xmlElement);
+ saveCollection(element.getComponents(), KEY_COMPONENTS,
+ KEY_COMPONENT_NODE, Component.class.getClass(), document,
+ xmlElement);
+ }
+
+ @Override
+ public ExplorationApplication loadXMLElement(Element xmlElement)
+ throws Exception {
+ ExplorationApplication explorationApplication = super
+ .loadXMLElement(xmlElement);
+ explorationApplication.setExplorations(loadCollection(KEY_DATA,
+ KEY_DATA_NODE, ExplorationData.class.getClass(), xmlElement));
+ explorationApplication.setComponents(loadCollection(KEY_COMPONENTS,
+ KEY_COMPONENT_NODE, Component.class.getClass(), xmlElement));
+ return explorationApplication;
+ }
+
+ public ExplorationApplication createInstance() {
+ return new ExplorationApplication();
+ }
+
+}
Copied: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationDataFactory.java (from rev 35, trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationDataFactory.java)
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationDataFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ExplorationDataFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,44 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Constant;
+import org.cemagref.simexplorer.si.storage.entities.ConstantValue;
+import org.cemagref.simexplorer.si.storage.entities.ExplorationData;
+import org.cemagref.simexplorer.si.storage.entities.Result;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class ExplorationDataFactory extends
+ LoggableElementFactory<ExplorationData> {
+
+ public static final String KEY_RESULT = "result";
+ public static final String KEY_VALUES = "values";
+ public static final String KEY_VALUE = "value";
+
+ @Override
+ public ExplorationData loadXMLElement(Element xmlElement) throws Exception {
+ ExplorationData explorationData = super.loadXMLElement(xmlElement);
+ explorationData.setResult((Result) getFactory(Result.class).loadXMLElement(
+ getElementByTagName(xmlElement, KEY_RESULT)));
+ explorationData.setValuesMap(loadCollection(KEY_VALUES, KEY_VALUE, ConstantValue.class, xmlElement));
+ return explorationData;
+ }
+
+ @Override
+ public void saveXMLElement(Document document, Element xmlElement,
+ ExplorationData element) throws Exception {
+ super.saveXMLElement(document, xmlElement, element);
+
+ Element xmlResult = document.createElement(KEY_RESULT);
+ getFactory(Result.class).saveXMLElement(document, xmlResult, element.getResult());
+ xmlElement.appendChild(xmlResult);
+
+ saveCollection(element.getValuesMap(), KEY_VALUES, KEY_VALUE, ConstantValue.class, document, xmlElement);
+
+ }
+
+ @Override
+ public ExplorationData createInstance() {
+ return new ExplorationData();
+ }
+
+}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/LibraryFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/LibraryFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/LibraryFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,12 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Library;
+
+public class LibraryFactory extends LoggableElementFactory<Library> {
+
+ @Override
+ public Library createInstance() {
+ return new Library();
+ }
+
+}
Copied: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/LoggableElementFactory.java (from rev 35, trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/LoggableElementFactory.java)
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/LoggableElementFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/LoggableElementFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,65 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import java.util.Date;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.cemagref.simexplorer.si.storage.database.DatabaseConstants;
+import org.cemagref.simexplorer.si.storage.entities.LoggableElement;
+import org.w3c.dom.Element;
+
+public abstract class LoggableElementFactory<E extends LoggableElement> extends
+ EntityFactory<E> implements DatabaseConstants {
+
+ private static final String KEY_DESCRIPTION = "description";
+
+ private static DocumentBuilder documentBuilder = null;
+
+ public static synchronized DocumentBuilder getBuilder() throws Exception {
+ if (documentBuilder == null) {
+ DocumentBuilderFactory factory = DocumentBuilderFactory
+ .newInstance();
+ documentBuilder = factory.newDocumentBuilder();
+ return documentBuilder;
+ }
+ return documentBuilder;
+ }
+
+ @Override
+ public E loadXMLElement(Element xmlElement) throws Exception {
+ E element = super.loadXMLElement(xmlElement);
+ element.setUuid(getProperty(xmlElement, KEY_UUID));
+ element.setName(getProperty(xmlElement, KEY_NAME));
+ element.setType(getProperty(xmlElement, KEY_TYPE));
+ element.setDescription(getProperty(xmlElement, KEY_DESCRIPTION));
+
+ // FIXME Convert
+ /*
+ element.setMajorVersion(new Integer(getProperty(xmlElement, KEY_MAJORVERSION)));
+ element.setMinorVersion(new Integer(getProperty(xmlElement, KEY_MINORVERSION)));
+ element.setCreationDate(new Date(getProperty(xmlElement, KEY_CREATIONDATE)));
+ */
+
+ element.setHash(getProperty(xmlElement, KEY_HASH));
+ return element;
+ }
+
+ @Override
+ public void saveXMLElement(org.w3c.dom.Document document,
+ Element xmlElement, E element) throws Exception {
+
+ setProperty(document, xmlElement, KEY_UUID, element.getUuid());
+ setProperty(document, xmlElement, KEY_NAME, element.getName());
+ setProperty(document, xmlElement, KEY_TYPE, element.getType());
+ setProperty(document, xmlElement, KEY_DESCRIPTION, element.getDescription());
+ /*
+ setProperty(document, xmlElement, KEY_CREATIONDATE, element.getCreationDate());
+ setProperty(document, xmlElement, KEY_MAJORVERSION, element.getMajorVersion());
+ setProperty(document, xmlElement, KEY_MINORVERSION, element.getMinorVersion());
+ */
+ setProperty(document, xmlElement, KEY_HASH, element.getHash());
+
+ }
+
+}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/RepositoryFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/RepositoryFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/RepositoryFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,20 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Repository;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class RepositoryFactory<E extends Repository> extends EntityFactory<Repository> {
+
+ @Override
+ public Repository createInstance() {
+ return new Repository();
+ }
+
+ @Override
+ public void saveXMLElement(Document document, Element xmlElement,
+ Repository element) throws Exception {
+
+ }
+
+}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ResultFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ResultFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/ResultFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,20 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Result;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class ResultFactory<E extends Result> extends EntityFactory<Result> {
+
+ @Override
+ public Result createInstance() {
+ return new Result();
+ }
+
+ @Override
+ public void saveXMLElement(Document document, Element xmlElement,
+ Result element) throws Exception {
+
+ }
+
+}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/StructureFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/StructureFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/factories/StructureFactory.java 2007-12-05 15:22:53 UTC (rev 39)
@@ -0,0 +1,7 @@
+package org.cemagref.simexplorer.si.storage.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.Structure;
+
+public abstract class StructureFactory<E extends Structure> extends EntityFactory<Structure> {
+
+}
1
0
r38 - in trunk/simexplorer-si-storage: . src src/test src/test/org src/test/org/cemagref src/test/org/cemagref/simexplorer src/test/org/cemagref/simexplorer/si src/test/org/cemagref/simexplorer/si/storage src/test/org/cemagref/simexplorer/si/storage/test
by glandais@users.labs.libre-entreprise.org 04 Dec '07
by glandais@users.labs.libre-entreprise.org 04 Dec '07
04 Dec '07
Author: glandais
Date: 2007-12-04 18:32:09 +0000 (Tue, 04 Dec 2007)
New Revision: 38
Added:
trunk/simexplorer-si-storage/src/test/
trunk/simexplorer-si-storage/src/test/org/
trunk/simexplorer-si-storage/src/test/org/cemagref/
trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/
trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/
trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/
trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test/
trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test/LuceneDatabaseTestCase.java
Modified:
trunk/simexplorer-si-storage/.classpath
Log:
JUnit tests
Modified: trunk/simexplorer-si-storage/.classpath
===================================================================
--- trunk/simexplorer-si-storage/.classpath 2007-12-04 18:31:49 UTC (rev 37)
+++ trunk/simexplorer-si-storage/.classpath 2007-12-04 18:32:09 UTC (rev 38)
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/java"/>
+ <classpathentry kind="src" path="src/test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.12/log4j-1.2.12.jar"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
Added: trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test/LuceneDatabaseTestCase.java
===================================================================
--- trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test/LuceneDatabaseTestCase.java (rev 0)
+++ trunk/simexplorer-si-storage/src/test/org/cemagref/simexplorer/si/storage/test/LuceneDatabaseTestCase.java 2007-12-04 18:32:09 UTC (rev 38)
@@ -0,0 +1,113 @@
+package org.cemagref.simexplorer.si.storage.test;
+
+import java.util.UUID;
+
+import junit.framework.TestCase;
+
+import org.cemagref.simexplorer.si.storage.database.lucene.LuceneDatabase;
+import org.cemagref.simexplorer.si.storage.entities.ExplorationData;
+import org.cemagref.simexplorer.si.storage.entities.LoggableElement;
+
+public class LuceneDatabaseTestCase extends TestCase {
+
+ private LuceneDatabase database;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ try {
+ database = new LuceneDatabase();
+ } catch (Throwable e) {
+ throw new Exception(e);
+ }
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ try {
+ database.shutDown();
+ } catch (Throwable e) {
+ throw new Exception(e);
+ }
+ }
+
+ public final void testPushElement() {
+ try {
+ String description1 = UUID.randomUUID().toString();
+ String uuid1 = UUID.randomUUID().toString();
+ Integer majorVersion1 = 5;
+ Integer minorVersion1 = 12;
+ ExplorationData explorationData1 = new ExplorationData();
+ explorationData1.setUuid(uuid1);
+ explorationData1.setMajorVersion(majorVersion1);
+ explorationData1.setMinorVersion(minorVersion1);
+ explorationData1.setDescription(description1);
+ database.pushElement(explorationData1);
+
+ LoggableElement element = database.findElement(uuid1, majorVersion1, minorVersion1);
+ assertNotNull(element);
+ assertEquals(description1, element.getDescription());
+
+ String description2 = UUID.randomUUID().toString();
+ ExplorationData explorationData2 = new ExplorationData();
+ explorationData2.setUuid(uuid1);
+ explorationData2.setMajorVersion(majorVersion1);
+ explorationData2.setMinorVersion(minorVersion1);
+ explorationData2.setDescription(description2);
+ database.pushElement(explorationData2);
+
+ assertEquals(description2, database.findElement(uuid1, majorVersion1, minorVersion1).getDescription());
+
+ String description3 = UUID.randomUUID().toString();
+ Integer minorVersion3 = minorVersion1 + 1;
+ ExplorationData explorationData3 = new ExplorationData();
+ explorationData3.setUuid(uuid1);
+ explorationData3.setMajorVersion(majorVersion1);
+ explorationData3.setMinorVersion(minorVersion3);
+ explorationData3.setDescription(description3);
+ database.pushElement(explorationData3);
+
+ assertEquals(description3, database.findElement(uuid1, majorVersion1, minorVersion3).getDescription());
+
+ } catch (Throwable e) {
+ fail(e.toString());
+ }
+ }
+
+ public final void testFindElement() {
+ fail("Not yet implemented"); // TODO
+ }
+
+ public final void testGetElementLatestVersion() {
+ fail("Not yet implemented"); // TODO
+ }
+
+ public final void testFindElementsById() {
+ fail("Not yet implemented"); // TODO
+ }
+
+ public final void testFindElementsByPropertiesMapOfStringString() {
+ fail("Not yet implemented"); // TODO
+ }
+
+ public final void testFindElementsByPropertiesMapOfStringStringClass() {
+ fail("Not yet implemented"); // TODO
+ }
+
+ public final void testDeleteElementString() {
+ fail("Not yet implemented"); // TODO
+ }
+
+ public final void testDeleteElementStringIntegerInteger() {
+ fail("Not yet implemented"); // TODO
+ }
+
+ public final void testLuceneDatabase() {
+ try {
+ LuceneDatabase database = new LuceneDatabase();
+ } catch (Throwable e) {
+ fail(e.toString());
+ }
+
+ }
+
+}
1
0
r37 - in trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage: . tools
by glandais@users.labs.libre-entreprise.org 04 Dec '07
by glandais@users.labs.libre-entreprise.org 04 Dec '07
04 Dec '07
Author: glandais
Date: 2007-12-04 18:31:49 +0000 (Tue, 04 Dec 2007)
New Revision: 37
Added:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/tools/
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/tools/Version.java
Log:
Version comparable class
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/tools/Version.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/tools/Version.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/tools/Version.java 2007-12-04 18:31:49 UTC (rev 37)
@@ -0,0 +1,50 @@
+package org.cemagref.simexplorer.si.storage.tools;
+
+public class Version implements Comparable<Version> {
+
+ private Integer majorVersion;
+ private Integer minorVersion;
+ private Object versionnedObject;
+
+ public int compareTo(Version v) {
+ if (this.getMajorVersion() != null && v.getMajorVersion() != null) {
+ if (this.getMajorVersion() == v.getMajorVersion()) {
+ if (this.getMinorVersion() != null
+ && v.getMinorVersion() != null) {
+ if (this.getMinorVersion() != v.getMinorVersion()) {
+ return this.getMinorVersion().compareTo(
+ v.getMinorVersion());
+ }
+ }
+ } else {
+ return this.getMajorVersion().compareTo(v.getMajorVersion());
+ }
+ }
+ return 0;
+ }
+
+ public Integer getMajorVersion() {
+ return majorVersion;
+ }
+
+ public void setMajorVersion(Integer majorVersion) {
+ this.majorVersion = majorVersion;
+ }
+
+ public Integer getMinorVersion() {
+ return minorVersion;
+ }
+
+ public void setMinorVersion(Integer minorVersion) {
+ this.minorVersion = minorVersion;
+ }
+
+ public Object getVersionnedObject() {
+ return versionnedObject;
+ }
+
+ public void setVersionnedObject(Object versionnedObject) {
+ this.versionnedObject = versionnedObject;
+ }
+
+}
1
0
r36 - in trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage: . database database/lucene
by glandais@users.labs.libre-entreprise.org 04 Dec '07
by glandais@users.labs.libre-entreprise.org 04 Dec '07
04 Dec '07
Author: glandais
Date: 2007-12-04 18:31:27 +0000 (Tue, 04 Dec 2007)
New Revision: 36
Added:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java
Log:
Database interface and Lucene implementation
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java 2007-12-04 18:31:27 UTC (rev 36)
@@ -0,0 +1,48 @@
+package org.cemagref.simexplorer.si.storage.database;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.cemagref.simexplorer.si.storage.entities.LoggableElement;
+
+public abstract class Database {
+
+ public Database() throws Throwable {
+ super();
+ init();
+ }
+
+ protected abstract void init() throws Throwable;
+
+ // Create / Update
+
+ public abstract void pushElement(LoggableElement element) throws Throwable;
+
+ // Read
+
+ public abstract LoggableElement findElement(String uuid, Integer majorVersion,
+ Integer minorVersion) throws Throwable;
+
+ public abstract LoggableElement getElementLatestVersion(String uuid)
+ throws Throwable;
+
+ public abstract Set<LoggableElement> findElementsById(String uuid) throws Throwable;
+
+ public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties) throws Throwable;
+
+ public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties, Class entityClass) throws Throwable;
+
+ // Delete
+
+ public void deleteElement(LoggableElement element) throws Throwable {
+ deleteElement(element.getUuid(), element.getMajorVersion(), element
+ .getMinorVersion());
+ }
+
+ public abstract void deleteElement(String uuid) throws Throwable;
+
+ public abstract void deleteElement(String uuid, Integer majorVersion,
+ Integer minorVersion) throws Throwable;
+
+
+}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java 2007-12-04 18:31:27 UTC (rev 36)
@@ -0,0 +1,11 @@
+package org.cemagref.simexplorer.si.storage.database.lucene;
+
+public interface LuceneConstants {
+
+ public static final String KEY_CLASSNAME = "simexplorer.classname";
+ public static final String KEY_UUID = "simexplorer.uuid";
+ public static final String KEY_DESCRIPTION = "simexplorer.description";
+ public static final String KEY_MAJORVERSION = "simexplorer.version.major";
+ public static final String KEY_MINORVERSION = "simexplorer.version.minor";
+
+}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java 2007-12-04 18:31:27 UTC (rev 36)
@@ -0,0 +1,250 @@
+package org.cemagref.simexplorer.si.storage.database.lucene;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.PerFieldAnalyzerWrapper;
+import org.apache.lucene.analysis.SimpleAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.CorruptIndexException;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.Hits;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Searcher;
+import org.apache.lucene.search.TermQuery;
+import org.cemagref.simexplorer.si.storage.dao.ElementDAOFactory;
+import org.cemagref.simexplorer.si.storage.dao.factories.LoggableElementFactory;
+import org.cemagref.simexplorer.si.storage.database.Database;
+import org.cemagref.simexplorer.si.storage.entities.LoggableElement;
+import org.cemagref.simexplorer.si.storage.tools.Version;
+import org.w3c.dom.Element;
+
+public class LuceneDatabase extends Database implements LuceneConstants {// implements
+ // LuceneDatabaseInterface
+ // {
+
+ private static Analyzer analyser;
+ private static IndexWriter writer;
+ private static Searcher searcher;
+ private static boolean initok = false;
+
+ public LuceneDatabase() throws Throwable {
+ super();
+ }
+
+ protected void init() throws Throwable {
+ if (!initok) {
+ analyser = new PerFieldAnalyzerWrapper(new SimpleAnalyzer());
+ // analyser.addAnalyzer("firstname", new KeywordAnalyzer());
+ // analyser.addAnalyzer("lastname", new KeywordAnalyzer());
+
+ String directory = "./index/";
+
+ boolean create = false;
+ if (!IndexReader.indexExists(directory)) {
+ // si l'index n'existe pas, on force la creation
+ create = true;
+ }
+ writer = new IndexWriter(directory, analyser, create);
+ searcher = new IndexSearcher(directory);
+ initok = true;
+ }
+ }
+
+ @Override
+ public void pushElement(LoggableElement element) throws Throwable {
+ LoggableElementFactory elementFactory = (LoggableElementFactory) ElementDAOFactory
+ .getFactory(element.getClass());
+ Document document = saveLuceneElement(elementFactory, element);
+
+ LoggableElement oldElement = findElement(element.getUuid(), element
+ .getMajorVersion(), element.getMinorVersion());
+ if (oldElement != null) {
+ // update
+ deleteElement(element);
+ }
+
+ writer.addDocument(document);
+ }
+
+ @Override
+ public LoggableElement findElement(String uuid, Integer majorVersion,
+ Integer minorVersion) throws Throwable {
+
+ Map<String, String> properties = new HashMap<String, String>();
+ properties.put(KEY_UUID, uuid);
+ properties.put(KEY_MAJORVERSION, majorVersion.toString());
+ properties.put(KEY_MINORVERSION, minorVersion.toString());
+
+ Hits hits = findHits(properties);
+ if (hits.length() == 0) {
+ return null;
+ } else {
+ return convertDocumentToElement(hits.doc(0));
+ }
+ }
+
+ @Override
+ public LoggableElement getElementLatestVersion(String uuid)
+ throws Throwable {
+ LoggableElement element = null;
+
+ Map<String, String> properties = new HashMap<String, String>();
+ properties.put(KEY_UUID, uuid);
+ Hits hits = findHits(properties);
+ if (hits.length() > 0) {
+ List<Version> versions = new ArrayList<Version>();
+ for (int i = 0; i < hits.length(); i++) {
+ Document doc = hits.doc(i);
+ Version version = new Version();
+ version.setMajorVersion(new Integer(doc.get(KEY_MAJORVERSION)));
+ version.setMinorVersion(new Integer(doc.get(KEY_MINORVERSION)));
+ version.setVersionnedObject(doc);
+ }
+ Collections.sort(versions);
+ Collections.reverse(versions);
+
+ Document latestDoc = (Document) versions.get(0)
+ .getVersionnedObject();
+ LoggableElementFactory elementFactory = (LoggableElementFactory) ElementDAOFactory
+ .getFactory(latestDoc.get(KEY_CLASSNAME));
+ element = loadLuceneElement(elementFactory, latestDoc);
+ }
+
+ return element;
+ }
+
+ @Override
+ public Set<LoggableElement> findElementsById(String uuid) throws Throwable {
+ Map<String, String> properties = new HashMap<String, String>();
+ properties.put(KEY_UUID, uuid);
+ return convertHitsToElements(findHits(properties));
+ }
+
+ @Override
+ public Set<LoggableElement> findElementsByProperties(
+ Map<String, String> properties) throws Throwable {
+ return convertHitsToElements(findHits(properties));
+ }
+
+ @Override
+ public Set<LoggableElement> findElementsByProperties(
+ Map<String, String> properties, Class entityClass) throws Throwable {
+ Map<String, String> props = new HashMap<String, String>();
+ props.putAll(properties);
+ props.put(KEY_CLASSNAME, entityClass.getName());
+ return convertHitsToElements(findHits(properties));
+ }
+
+ public void deleteElement(String uuid) throws Throwable {
+ writer.deleteDocuments(new Term(KEY_UUID, uuid));
+ }
+
+ public void deleteElement(String uuid, Integer majorVersion,
+ Integer minorVersion) throws Throwable {
+ Term[] terms = new Term[3];
+ terms[0] = new Term(KEY_UUID, uuid);
+ terms[1] = new Term(KEY_MAJORVERSION, uuid);
+ terms[2] = new Term(KEY_MINORVERSION, uuid);
+ writer.deleteDocuments(terms);
+ }
+
+ private Hits findHits(Map<String, String> properties) throws Throwable {
+ // FIXME disablecoords?
+ BooleanQuery query = new BooleanQuery(false);
+ for (Entry<String, String> kv : properties.entrySet()) {
+ query.add(new TermQuery(new Term(kv.getKey(), kv.getValue())),
+ BooleanClause.Occur.MUST);
+ }
+ return searcher.search(query);
+ }
+
+ private LoggableElement convertDocumentToElement(Document document)
+ throws Throwable {
+ String className = document.get(KEY_CLASSNAME);
+ LoggableElementFactory factory = (LoggableElementFactory) ElementDAOFactory
+ .getFactory(className);
+ return loadLuceneElement(factory, document);
+ }
+
+ private Set<LoggableElement> convertHitsToElements(Hits hits)
+ throws Throwable {
+ Set<LoggableElement> elements = new HashSet<LoggableElement>();
+ for (int i = 0; i < hits.length(); i++) {
+ Document document = hits.doc(i);
+ elements.add(convertDocumentToElement(document));
+ }
+ return elements;
+ }
+
+ private LoggableElement loadLuceneElement(LoggableElementFactory factory, Document document) throws Throwable {
+ String strXMLElement = document.get("xmlElement");
+
+ InputStream is = new ByteArrayInputStream(strXMLElement.getBytes());
+ org.w3c.dom.Document xmlDocument = factory.getBuilder().parse(is);
+
+ LoggableElement element = factory.loadXMLElement((Element) xmlDocument.getFirstChild());
+
+ return element;
+ }
+
+ private Document saveLuceneElement(LoggableElementFactory factory, LoggableElement element) throws Throwable {
+ Document document = new Document();
+
+ // FIXME save searchable/metadata fields
+ document.add(new Field(KEY_UUID, element.getUuid().getBytes(),
+ Field.Store.YES));
+ document.add(new Field(KEY_DESCRIPTION, element.getDescription().getBytes(),
+ Field.Store.YES));
+ document.add(new Field(KEY_CLASSNAME, element.getClass().getName().getBytes(),
+ Field.Store.YES));
+ document.add(new Field(KEY_MINORVERSION, element.getMinorVersion().toString().getBytes(),
+ Field.Store.YES));
+ document.add(new Field(KEY_MAJORVERSION, element.getMajorVersion().toString().getBytes(),
+ Field.Store.YES));
+
+ org.w3c.dom.Document xmlDocument = factory.getBuilder().newDocument();
+ // Propriétés du DOM
+ xmlDocument.setXmlVersion("1.0");
+ xmlDocument.setXmlStandalone(true);
+ // Création de l'arborescence du DOM
+ Element racine = xmlDocument.createElement(element.getClass().getName());
+ factory.saveXMLElement(xmlDocument, racine, element);
+ xmlDocument.appendChild(racine);
+ DOMSource domSource = new DOMSource(xmlDocument);
+ StringWriter writer = new StringWriter();
+ StreamResult result = new StreamResult(writer);
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer transformer = tf.newTransformer();
+ transformer.transform(domSource, result);
+ String stringResult = writer.toString();
+ document.add(new Field("xmlElement", stringResult, Field.Store.COMPRESS, Field.Index.NO));
+
+ return document;
+ }
+
+ public void shutDown() throws Throwable {
+ writer.close();
+ }
+}
1
0
r35 - in trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao: . factories
by glandais@users.labs.libre-entreprise.org 04 Dec '07
by glandais@users.labs.libre-entreprise.org 04 Dec '07
04 Dec '07
Author: glandais
Date: 2007-12-04 18:30:44 +0000 (Tue, 04 Dec 2007)
New Revision: 35
Added:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/EntityFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationDataFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/LoggableElementFactory.java
Removed:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/EntityFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/LoggableElementFactory.java
Modified:
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/ElementDAOFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/DescriptorFactory.java
trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationApplicationFactory.java
Log:
Refactoring factories
Modified: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/ElementDAOFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/ElementDAOFactory.java 2007-12-03 17:39:05 UTC (rev 34)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/ElementDAOFactory.java 2007-12-04 18:30:44 UTC (rev 35)
@@ -2,36 +2,46 @@
import java.util.HashMap;
import java.util.Map;
+import java.util.StringTokenizer;
+import org.cemagref.simexplorer.si.storage.dao.factories.EntityFactory;
+import org.cemagref.simexplorer.si.storage.entities.Entity;
+
public class ElementDAOFactory {
- private final static String PACKAGE_DAO = "org.cemagref.simexplorer.si.storage.dao";
- private final static String PACKAGE_DAO_FACTORIES = "org.cemagref.simexplorer.si.storage.dao.factories";
+ private final static String PACKAGE_DAO = "org.cemagref.simexplorer.si.storage.dao.factories";
private static Map<String, EntityFactory> factories = new HashMap<String, EntityFactory>();
-
- public static EntityFactory getFactory(Class entityClass) throws Throwable {
- EntityFactory elementFactory;
- String elementFactoryClassName = entityClass.getSimpleName()
- + "Factory";
- Class elementFactoryClass = Class.forName(PACKAGE_DAO + "."
- + elementFactoryClassName);
- if (elementFactoryClass == null) {
- elementFactoryClass = Class.forName(PACKAGE_DAO_FACTORIES + "."
+
+ public static EntityFactory getFactory(String entityClassName)
+ throws Throwable {
+ EntityFactory entityFactory = factories.get(entityClassName);
+ if (entityFactory == null) {
+
+ StringTokenizer st = new StringTokenizer(entityClassName, ".");
+ String simpleClassName = null;
+ while (st.hasMoreTokens()) {
+ simpleClassName = st.nextToken();
+ }
+
+ String elementFactoryClassName = simpleClassName + "Factory";
+
+ Class elementFactoryClass = Class.forName(PACKAGE_DAO + "."
+ elementFactoryClassName);
+
+ entityFactory = (EntityFactory) elementFactoryClass.newInstance();
+ factories.put(entityClassName, entityFactory);
}
-
- if (elementFactoryClass == null) {
- return null;
- }
-
- elementFactory = factories.get(elementFactoryClassName);
- if (elementFactory == null) {
- elementFactory = (EntityFactory) elementFactoryClass.newInstance();
- factories.put(elementFactoryClassName, elementFactory);
- }
- return elementFactory;
+ return entityFactory;
}
+ /**
+ * @param entityClass
+ * @return
+ * @throws Throwable
+ */
+ public static EntityFactory getFactory(Class entityClass) throws Throwable {
+ return getFactory(entityClass.getName());
+ }
}
Deleted: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/EntityFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/EntityFactory.java 2007-12-03 17:39:05 UTC (rev 34)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/EntityFactory.java 2007-12-04 18:30:44 UTC (rev 35)
@@ -1,41 +0,0 @@
-package org.cemagref.simexplorer.si.storage.dao;
-
-import java.util.Set;
-
-import org.cemagref.simexplorer.si.storage.entities.Entity;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-
-public abstract class EntityFactory<E extends Entity> {
-
- public E loadXMLElement(Element xmlElement) throws Throwable {
- E element = createInstance();
- return element;
- }
-
- public abstract void saveXMLElement(Document document, Element xmlElement,
- E element) throws Throwable;
-
- public abstract E createInstance();
-
- public Set<Entity> loadCollection() {
- return null;
- }
-
- public void saveCollection(Set entities, String tagSetName,
- String tagName, Class clazz, Document document, Element xmlElement)
- throws Throwable {
- if (entities != null && entities.size() > 0) {
- Element xmlCollection = document.createElement(tagSetName);
- EntityFactory elementFactory = (EntityFactory) ElementDAOFactory
- .getFactory(clazz);
- for (Object entity : entities) {
- Element childElement = document.createElement(tagName);
- elementFactory.saveXMLElement(document, childElement, (Entity) entity);
- xmlCollection.appendChild(childElement);
- }
- xmlElement.appendChild(xmlCollection);
- }
- }
-
-}
Deleted: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/LoggableElementFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/LoggableElementFactory.java 2007-12-03 17:39:05 UTC (rev 34)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/LoggableElementFactory.java 2007-12-04 18:30:44 UTC (rev 35)
@@ -1,89 +0,0 @@
-package org.cemagref.simexplorer.si.storage.dao;
-
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.io.StringWriter;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.cemagref.simexplorer.si.storage.entities.LoggableElement;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-
-public abstract class LoggableElementFactory<E extends LoggableElement>
- extends EntityFactory<E> {
-
- private DocumentBuilder documentBuilder = null;
-
- private DocumentBuilder getBuilder() throws Throwable {
- if (documentBuilder == null) {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- documentBuilder = factory.newDocumentBuilder();
- return documentBuilder;
- }
- return documentBuilder;
- }
-
- @Override
- public E loadXMLElement(Element xmlElement) throws Throwable {
- E element = super.loadXMLElement(xmlElement);
- // FIXME load all properties
-
- return element;
- }
-
- @Override
- public void saveXMLElement(org.w3c.dom.Document document, Element xmlElement,
- E element) throws Throwable {
- // FIXME save all fields
- Element xmlDescription = document.createElement("description");
- xmlDescription.setNodeValue(element.getDescription());
- xmlElement.appendChild(xmlDescription);
- }
-
- public E loadLuceneElement(Document document) throws Throwable {
- String strXMLElement = document.get("xmlElement");
-
- InputStream is = new ByteArrayInputStream(strXMLElement.getBytes());
- org.w3c.dom.Document xmlDocument = getBuilder().parse(is);
-
- // FIXME check first element, document is a not an element
- E element = loadXMLElement((Element) xmlDocument.getFirstChild());
-
- return element;
- }
-
- public Document saveLuceneElement(E element) throws Throwable {
- Document document = new Document();
-
- // FIXME save searchable/metadata fields
- document.add(new Field("description", element.getDescription(),
- Field.Store.YES, Field.Index.UN_TOKENIZED));
-
- org.w3c.dom.Document xmlDocument = getBuilder().newDocument();
- // Propriétés du DOM
- xmlDocument.setXmlVersion("1.0");
- xmlDocument.setXmlStandalone(true);
- // Création de l'arborescence du DOM
- Element racine = xmlDocument.createElement(element.getClass().getName());
- saveXMLElement(xmlDocument, racine, element);
- DOMSource domSource = new DOMSource(xmlDocument);
- StringWriter writer = new StringWriter();
- StreamResult result = new StreamResult(writer);
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- transformer.transform(domSource, result);
- String stringResult = writer.toString();
- document.add(new Field("xmlElement", stringResult, Field.Store.COMPRESS, Field.Index.NO));
-
- return document;
- }
-
-}
Modified: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/DescriptorFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/DescriptorFactory.java 2007-12-03 17:39:05 UTC (rev 34)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/DescriptorFactory.java 2007-12-04 18:30:44 UTC (rev 35)
@@ -1,26 +1,28 @@
package org.cemagref.simexplorer.si.storage.dao.factories;
-import org.cemagref.simexplorer.si.storage.dao.EntityFactory;
import org.cemagref.simexplorer.si.storage.entities.Descriptor;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class DescriptorFactory<E extends Descriptor> extends EntityFactory<Descriptor> {
+ public static final String KEY_NAME = "name";
+ public static final String KEY_VALUE = "value";
+
public Descriptor createInstance() {
return new Descriptor();
}
public Descriptor loadXMLElement(Element xmlElement) throws Throwable {
Descriptor descriptor = createInstance();
- descriptor.setName(xmlElement.getAttribute("name"));
- descriptor.setValue(xmlElement.getAttribute("value"));
+ descriptor.setName(xmlElement.getAttribute(KEY_NAME));
+ descriptor.setValue(xmlElement.getAttribute(KEY_VALUE));
return descriptor;
}
public void saveXMLElement(org.w3c.dom.Document document, Element xmlElement, Descriptor element) throws Throwable {
- xmlElement.setAttribute("name", element.getName());
- xmlElement.setAttribute("value", element.getValue());
+ xmlElement.setAttribute(KEY_NAME, element.getName());
+ xmlElement.setAttribute(KEY_VALUE, element.getValue());
}
}
Copied: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/EntityFactory.java (from rev 31, trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/EntityFactory.java)
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/EntityFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/EntityFactory.java 2007-12-04 18:30:44 UTC (rev 35)
@@ -0,0 +1,90 @@
+package org.cemagref.simexplorer.si.storage.dao.factories;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.cemagref.simexplorer.si.storage.dao.ElementDAOFactory;
+import org.cemagref.simexplorer.si.storage.entities.Entity;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+public abstract class EntityFactory<E extends Entity> {
+
+ public abstract E createInstance();
+
+ public E loadXMLElement(Element xmlElement) throws Throwable {
+ E element = createInstance();
+ return element;
+ }
+
+ protected Set<Element> getElementsByTagName(Element xmlElement,
+ String tagName) {
+ Set<Element> elements = new HashSet<Element>();
+
+ NodeList nodes = xmlElement.getChildNodes();
+ for (int i = 0; i < nodes.getLength(); i++) {
+ Node node = nodes.item(i);
+ if (node instanceof Element
+ && tagName.equals(((Element) node).getTagName())) {
+ elements.add((Element) node);
+ }
+ }
+ return elements;
+ }
+
+ protected Element getElementByTagName(Element xmlElement,String tagName) {
+ Set<Element> elements = getElementsByTagName(xmlElement, tagName);
+ if (elements.size() > 0) {
+ return elements.iterator().next();
+ } else {
+ return null;
+ }
+ }
+
+ public abstract void saveXMLElement(Document document, Element xmlElement,
+ E element) throws Throwable;
+
+ public Set loadCollection(String tagSetName, String tagName, Class clazz,
+ Element xmlElement) throws Throwable {
+ Set entities = new HashSet();
+
+ Element xmlCollection = getElementByTagName(xmlElement, tagSetName);
+ if (xmlCollection != null) {
+ EntityFactory elementFactory = (EntityFactory) ElementDAOFactory
+ .getFactory(clazz);
+
+ Set<Element> list = getElementsByTagName(xmlCollection, tagName);
+ for (Iterator iterator = list.iterator(); iterator.hasNext();) {
+ Element element = (Element) iterator.next();
+ Entity entity = elementFactory.loadXMLElement(element);
+ entities.add(entity);
+ }
+ }
+
+ return entities;
+ }
+
+ public void saveCollection(Set entities, String tagSetName, String tagName,
+ Class clazz, Document document, Element xmlElement)
+ throws Throwable {
+ if (entities != null && entities.size() > 0) {
+ Element xmlCollection = document.createElement(tagSetName);
+ EntityFactory elementFactory = (EntityFactory) ElementDAOFactory
+ .getFactory(clazz);
+ for (Object entity : entities) {
+ Element childElement = document.createElement(tagName);
+ // TODO
+ // if (entity instanceof LoggableElement) {
+ // elementFactory.saveXMLElementByReference
+ elementFactory.saveXMLElement(document, childElement,
+ (Entity) entity);
+ xmlCollection.appendChild(childElement);
+ }
+ xmlElement.appendChild(xmlCollection);
+ }
+ }
+
+}
Modified: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationApplicationFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationApplicationFactory.java 2007-12-03 17:39:05 UTC (rev 34)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationApplicationFactory.java 2007-12-04 18:30:44 UTC (rev 35)
@@ -1,30 +1,39 @@
package org.cemagref.simexplorer.si.storage.dao.factories;
-import java.util.Set;
-
-import org.cemagref.simexplorer.si.storage.dao.ElementDAOFactory;
-import org.cemagref.simexplorer.si.storage.dao.EntityFactory;
-import org.cemagref.simexplorer.si.storage.dao.LoggableElementFactory;
import org.cemagref.simexplorer.si.storage.entities.Component;
-import org.cemagref.simexplorer.si.storage.entities.Entity;
import org.cemagref.simexplorer.si.storage.entities.ExplorationApplication;
import org.cemagref.simexplorer.si.storage.entities.ExplorationData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-public class ExplorationApplicationFactory extends LoggableElementFactory<ExplorationApplication> {
+public class ExplorationApplicationFactory extends
+ LoggableElementFactory<ExplorationApplication> {
+ public static final String KEY_DATA = "data";
+ public static final String KEY_DATA_NODE = "explorationdata";
+ public static final String KEY_COMPONENTS = "components";
+ public static final String KEY_COMPONENT_NODE = "component";
+
@Override
- public void saveXMLElement(Document document, Element xmlElement, ExplorationApplication element) throws Throwable {
+ public void saveXMLElement(Document document, Element xmlElement,
+ ExplorationApplication element) throws Throwable {
super.saveXMLElement(document, xmlElement, element);
- saveCollection(element.getExplorations(), "data", "explorationData", ExplorationData.class.getClass(), document, xmlElement);
- saveCollection(element.getComponents(), "components", "component", Component.class.getClass(), document, xmlElement);
+ saveCollection(element.getExplorations(), KEY_DATA, KEY_DATA_NODE,
+ ExplorationData.class.getClass(), document, xmlElement);
+ saveCollection(element.getComponents(), KEY_COMPONENTS,
+ KEY_COMPONENT_NODE, Component.class.getClass(), document,
+ xmlElement);
}
@Override
- public ExplorationApplication loadXMLElement(Element xmlElement) throws Throwable {
- ExplorationApplication explorationApplication = super.loadXMLElement(xmlElement);
+ public ExplorationApplication loadXMLElement(Element xmlElement)
+ throws Throwable {
+ ExplorationApplication explorationApplication = super
+ .loadXMLElement(xmlElement);
+ explorationApplication.setExplorations(loadCollection(KEY_DATA,
+ KEY_DATA_NODE, ExplorationData.class.getClass(), xmlElement));
+ explorationApplication.setComponents(loadCollection(KEY_COMPONENTS,
+ KEY_COMPONENT_NODE, Component.class.getClass(), xmlElement));
return explorationApplication;
}
Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationDataFactory.java
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationDataFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/ExplorationDataFactory.java 2007-12-04 18:30:44 UTC (rev 35)
@@ -0,0 +1,13 @@
+package org.cemagref.simexplorer.si.storage.dao.factories;
+
+import org.cemagref.simexplorer.si.storage.entities.ExplorationData;
+
+public class ExplorationDataFactory extends
+ LoggableElementFactory<ExplorationData> {
+
+ @Override
+ public ExplorationData createInstance() {
+ return new ExplorationData();
+ }
+
+}
Copied: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/LoggableElementFactory.java (from rev 31, trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/LoggableElementFactory.java)
===================================================================
--- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/LoggableElementFactory.java (rev 0)
+++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/dao/factories/LoggableElementFactory.java 2007-12-04 18:30:44 UTC (rev 35)
@@ -0,0 +1,48 @@
+package org.cemagref.simexplorer.si.storage.dao.factories;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.cemagref.simexplorer.si.storage.database.lucene.LuceneConstants;
+import org.cemagref.simexplorer.si.storage.entities.LoggableElement;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+
+public abstract class LoggableElementFactory<E extends LoggableElement> extends
+ EntityFactory<E> implements LuceneConstants {
+
+ private static final String KEY_DESCRIPTION = "description";
+
+ private static DocumentBuilder documentBuilder = null;
+
+ public static synchronized DocumentBuilder getBuilder() throws Throwable {
+ if (documentBuilder == null) {
+ DocumentBuilderFactory factory = DocumentBuilderFactory
+ .newInstance();
+ documentBuilder = factory.newDocumentBuilder();
+ return documentBuilder;
+ }
+ return documentBuilder;
+ }
+
+ @Override
+ public E loadXMLElement(Element xmlElement) throws Throwable {
+ E element = super.loadXMLElement(xmlElement);
+ // FIXME load all properties
+ element.setDescription(getElementByTagName(xmlElement, KEY_DESCRIPTION)
+ .getNodeValue());
+
+ return element;
+ }
+
+ @Override
+ public void saveXMLElement(org.w3c.dom.Document document,
+ Element xmlElement, E element) throws Throwable {
+ // FIXME save all fields
+ Element xmlDescription = document.createElement(KEY_DESCRIPTION);
+ Text xmlDescriptionText = document.createTextNode(element.getDescription());
+ xmlDescription.appendChild(xmlDescriptionText);
+ xmlElement.appendChild(xmlDescription);
+ }
+
+}
1
0
03 Dec '07
Author: glandais
Date: 2007-12-03 17:39:05 +0000 (Mon, 03 Dec 2007)
New Revision: 34
Modified:
trunk/simexplorer-si-storage/
Log:
Property changes on: trunk/simexplorer-si-storage
___________________________________________________________________
Name: svn:ignore
+ bin
target
1
0
r33 - in trunk/simexplorer-si-storage: . .settings
by glandais@users.labs.libre-entreprise.org 03 Dec '07
by glandais@users.labs.libre-entreprise.org 03 Dec '07
03 Dec '07
Author: glandais
Date: 2007-12-03 17:38:11 +0000 (Mon, 03 Dec 2007)
New Revision: 33
Added:
trunk/simexplorer-si-storage/.classpath
trunk/simexplorer-si-storage/.project
trunk/simexplorer-si-storage/.settings/
trunk/simexplorer-si-storage/.settings/org.eclipse.jdt.core.prefs
Log:
Eclipse settings
Added: trunk/simexplorer-si-storage/.classpath
===================================================================
--- trunk/simexplorer-si-storage/.classpath (rev 0)
+++ trunk/simexplorer-si-storage/.classpath 2007-12-03 17:38:11 UTC (rev 33)
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src/java"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.12/log4j-1.2.12.jar"/>
+ <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
+ <classpathentry kind="var" path="M2_REPO/avalon-framework/avalon-framework/4.1.3/avalon-framework-4.1.3.jar"/>
+ <classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1/commons-logging-1.1.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/apache/lucene/lucene-core/2.2.0/lucene-core-2.2.0.jar" sourcepath="/LIB_SRC/lucene-2.2.0-src.zip"/>
+ <classpathentry kind="var" path="M2_REPO/javax/servlet/servlet-api/2.3/servlet-api-2.3.jar"/>
+ <classpathentry kind="var" path="M2_REPO/logkit/logkit/1.0.1/logkit-1.0.1.jar"/>
+ <classpathentry kind="output" path="target/classes"/>
+</classpath>
Added: trunk/simexplorer-si-storage/.project
===================================================================
--- trunk/simexplorer-si-storage/.project (rev 0)
+++ trunk/simexplorer-si-storage/.project 2007-12-03 17:38:11 UTC (rev 33)
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>simexplorer-si-storage</name>
+ <comment>simexplorersi stockage</comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
Added: trunk/simexplorer-si-storage/.settings/org.eclipse.jdt.core.prefs
===================================================================
--- trunk/simexplorer-si-storage/.settings/org.eclipse.jdt.core.prefs (rev 0)
+++ trunk/simexplorer-si-storage/.settings/org.eclipse.jdt.core.prefs 2007-12-03 17:38:11 UTC (rev 33)
@@ -0,0 +1,5 @@
+#Mon Dec 03 10:11:02 GMT+01:00 2007
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.source=1.5
1
0
03 Dec '07
Author: glandais
Date: 2007-12-03 17:37:52 +0000 (Mon, 03 Dec 2007)
New Revision: 32
Added:
trunk/simexplorer-si-storage/pom.xml
trunk/simexplorer-si-storage/profiles.xml
Log:
Maven2 config files
Added: trunk/simexplorer-si-storage/pom.xml
===================================================================
--- trunk/simexplorer-si-storage/pom.xml (rev 0)
+++ trunk/simexplorer-si-storage/pom.xml 2007-12-03 17:37:52 UTC (rev 32)
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <!--The version of maven's project object model-->
+ <modelVersion>4.0.0</modelVersion>
+
+ <!--lutinproject.xml-->
+ <parent>
+ <groupId>lutinlib</groupId>
+ <artifactId>lutinproject</artifactId>
+ <version>2.2</version>
+ </parent>
+
+ <!--A unique name for this project-->
+ <artifactId>simexplorersi</artifactId>
+ <name>simexplorersi-stockage</name>
+
+ <!--ejb, jar, war...-->
+ <packaging>jar</packaging>
+
+ <!--Version-->
+ <version>1.0.0</version>
+
+ <!--Description-->
+ <description>simexplorersi stockage</description>
+ <inceptionYear>2007</inceptionYear>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>${maven.src.dir}/resources</directory>
+ <includes>
+ <include>**/*</include>
+ </includes>
+ </resource>
+ </resources>
+ </build>
+
+ <!--Librairies-->
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.lucene</groupId>
+ <artifactId>lucene-core</artifactId>
+ <version>2.2.0</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
Added: trunk/simexplorer-si-storage/profiles.xml
===================================================================
--- trunk/simexplorer-si-storage/profiles.xml (rev 0)
+++ trunk/simexplorer-si-storage/profiles.xml 2007-12-03 17:37:52 UTC (rev 32)
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<settings>
+ <profiles>
+ <profile>
+ <id>project</id>
+
+ <activation>
+ <activeByDefault>true</activeByDefault>
+ </activation>
+
+ <properties>
+ <!--Source base dir-->
+ <maven.src.dir>${basedir}/target/src-build</maven.src.dir>
+
+ <!--Test-->
+ <maven.test.skip>true</maven.test.skip>
+
+ <!--Compile with ?-->
+ <maven.compile.source>1.5</maven.compile.source>
+ <maven.compile.target>1.5</maven.compile.target>
+
+ <!--Verbose-->
+ <maven.verbose>false</maven.verbose>
+ </properties>
+ </profile>
+ </profiles>
+</settings>
1
0