From f81dec893979662ca3855a5cc263e7d5667d6a12 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Sun, 6 Oct 2013 12:32:57 +0200 Subject: [PATCH] [New] working on rewrite --- .../xmlpers/api/PersistenceContext.java | 17 ++ .../xmlpers/api/XmlPersistenceConstants.java | 6 +- .../impl/XmlPersistenceTransactionImpl.java | 5 +- .../test/impl/rewrite/BookDomParser.java | 59 +++++++ .../test/impl/rewrite/BookParserFactory.java | 45 +++++ .../test/impl/rewrite/BookSaxParser.java | 62 +++++++ .../DefaultPersistenceTransaction.java | 148 +++++++++++++++++ .../rewrite/DefaultXmlPersistenceManager.java | 63 +++++++ .../test/impl/rewrite/MetadataDao.java | 84 ++++++++++ .../xmlpers/test/impl/rewrite/ObjectDao.java | 155 ++++++++++++++++-- .../test/impl/rewrite/ObjectDaoTest.java | 81 +++++++++ .../rewrite/PersistenceContextFactory.java | 39 +++++ .../impl/rewrite/PersistenceTransaction.java | 39 +++++ .../TestPersistenceContextFactory.java | 101 ++++++++++++ .../impl/rewrite/XmlPersistenceManager.java | 32 ++++ .../rewrite/XmlPersistenceManagerLoader.java | 38 +++++ 16 files changed, 958 insertions(+), 16 deletions(-) create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookDomParser.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookParserFactory.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookSaxParser.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/DefaultPersistenceTransaction.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/DefaultXmlPersistenceManager.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/MetadataDao.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/ObjectDaoTest.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/PersistenceContextFactory.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/PersistenceTransaction.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/TestPersistenceContextFactory.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/XmlPersistenceManager.java create mode 100644 src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/XmlPersistenceManagerLoader.java diff --git a/src/main/java/ch/eitchnet/xmlpers/api/PersistenceContext.java b/src/main/java/ch/eitchnet/xmlpers/api/PersistenceContext.java index 17e49e9bb..e50021892 100644 --- a/src/main/java/ch/eitchnet/xmlpers/api/PersistenceContext.java +++ b/src/main/java/ch/eitchnet/xmlpers/api/PersistenceContext.java @@ -21,6 +21,7 @@ */ package ch.eitchnet.xmlpers.api; +import ch.eitchnet.utils.helper.StringHelper; public class PersistenceContext { @@ -79,4 +80,20 @@ public class PersistenceContext { public void setParserFactory(ParserFactory parserFactory) { this.parserFactory = parserFactory; } + + public boolean hasSubType() { + return !StringHelper.isEmpty(this.subType); + } + + @Override + public PersistenceContext clone() { + PersistenceContext clone = new PersistenceContext<>(); + clone.type = this.type; + clone.subType = this.subType; + clone.id = this.id; + clone.ioMode = this.ioMode; + clone.parserFactory = this.parserFactory; + clone.object = this.object; + return clone; + } } \ No newline at end of file diff --git a/src/main/java/ch/eitchnet/xmlpers/api/XmlPersistenceConstants.java b/src/main/java/ch/eitchnet/xmlpers/api/XmlPersistenceConstants.java index 3fd3266e4..f1968952e 100644 --- a/src/main/java/ch/eitchnet/xmlpers/api/XmlPersistenceConstants.java +++ b/src/main/java/ch/eitchnet/xmlpers/api/XmlPersistenceConstants.java @@ -28,8 +28,8 @@ package ch.eitchnet.xmlpers.api; public class XmlPersistenceConstants { private static final String PROP_PREFIX = "ch.eitchnet.xmlpers."; - public static final String PROP_VERBOSE = "ch.eitchnet.xmlpers." + "verbose"; - public static final String PROP_BASEPATH = "ch.eitchnet.xmlpers." + "basePath"; - public static final String PROP_DAO_FACTORY_CLASS = "ch.eitchnet.xmlpers." + "daoFactoryClass"; + public static final String PROP_VERBOSE = PROP_PREFIX + "verbose"; + public static final String PROP_BASEPATH = PROP_PREFIX + "basePath"; + public static final String PROP_DAO_FACTORY_CLASS = PROP_PREFIX + "daoFactoryClass"; public static final String PROP_XML_IO_MOD = PROP_PREFIX + "ioMode"; } diff --git a/src/main/java/ch/eitchnet/xmlpers/impl/XmlPersistenceTransactionImpl.java b/src/main/java/ch/eitchnet/xmlpers/impl/XmlPersistenceTransactionImpl.java index a8cdbb7ac..6b27aa0bd 100644 --- a/src/main/java/ch/eitchnet/xmlpers/impl/XmlPersistenceTransactionImpl.java +++ b/src/main/java/ch/eitchnet/xmlpers/impl/XmlPersistenceTransactionImpl.java @@ -25,6 +25,7 @@ import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ch.eitchnet.utils.helper.StringHelper; import ch.eitchnet.utils.objectfilter.ObjectFilter; import ch.eitchnet.xmlpers.api.XmlPersistenceDao; import ch.eitchnet.xmlpers.api.XmlPersistenceDaoFactory; @@ -126,6 +127,7 @@ public class XmlPersistenceTransactionImpl implements XmlPersistenceTransaction public void commit() { try { + long start = System.nanoTime(); if (this.verbose) XmlPersistenceTransactionImpl.logger.info("Committing TX..."); Set keySet = this.objectFilter.keySet(); @@ -178,7 +180,8 @@ public class XmlPersistenceTransactionImpl implements XmlPersistenceTransaction } } - XmlPersistenceTransactionImpl.logger.info("Completed TX"); + long end = System.nanoTime(); + logger.info("Completed TX in " + StringHelper.formatNanoDuration(end - start)); //$NON-NLS-1$ } finally { // clean up diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookDomParser.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookDomParser.java new file mode 100644 index 000000000..35c584904 --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookDomParser.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import org.w3c.dom.Document; + +import ch.eitchnet.xmlpers.api.DomParser; +import ch.eitchnet.xmlpers.test.impl.Book; + +/** + * @author Robert von Burg + * + */ +public class BookDomParser implements DomParser { + + @Override + public Book getObject() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setObject(Book object) { + // TODO Auto-generated method stub + + } + + @Override + public Document toDom() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void fromDom(Document document) { + // TODO Auto-generated method stub + + } + +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookParserFactory.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookParserFactory.java new file mode 100644 index 000000000..01ab00143 --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookParserFactory.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import ch.eitchnet.xmlpers.api.DomParser; +import ch.eitchnet.xmlpers.api.ParserFactory; +import ch.eitchnet.xmlpers.api.SaxParser; +import ch.eitchnet.xmlpers.test.impl.Book; + +/** + * @author Robert von Burg + * + */ +public class BookParserFactory implements ParserFactory { + + @Override + public DomParser getDomParser() { + return new BookDomParser(); + } + + @Override + public SaxParser getSaxParser() { + return new BookSaxParser(); + } + +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookSaxParser.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookSaxParser.java new file mode 100644 index 000000000..ab4d012f5 --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/BookSaxParser.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import javax.xml.stream.XMLStreamException; + +import org.xml.sax.helpers.DefaultHandler; + +import ch.eitchnet.xmlpers.api.SaxParser; +import ch.eitchnet.xmlpers.impl.XmlPersistenceStreamWriter; +import ch.eitchnet.xmlpers.test.impl.Book; + +/** + * @author Robert von Burg + * + */ +public class BookSaxParser implements SaxParser { + + @Override + public Book getObject() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setObject(Book object) { + // TODO Auto-generated method stub + + } + + @Override + public DefaultHandler getDefaultHandler() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void write(XmlPersistenceStreamWriter xmlWriter) throws XMLStreamException { + // TODO Auto-generated method stub + + } + +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/DefaultPersistenceTransaction.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/DefaultPersistenceTransaction.java new file mode 100644 index 000000000..27bdaf396 --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/DefaultPersistenceTransaction.java @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import java.util.List; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ch.eitchnet.utils.helper.StringHelper; +import ch.eitchnet.utils.objectfilter.ObjectFilter; +import ch.eitchnet.xmlpers.api.PersistenceContext; + +/** + * @author Robert von Burg + * + */ +public class DefaultPersistenceTransaction implements PersistenceTransaction { + + private static final Logger logger = LoggerFactory.getLogger(DefaultPersistenceTransaction.class); + + private final FileDao fileDao; + private final ObjectDao objectDao; + private final MetadataDao metadataDao; + private final ObjectFilter objectFilter; + private final boolean verbose; + + private boolean closed; + + public DefaultPersistenceTransaction(FileDao fileDao, boolean verbose) { + this.fileDao = fileDao; + this.verbose = verbose; + this.objectFilter = new ObjectFilter(); + this.objectDao = new ObjectDao(this, this.fileDao, this.objectFilter); + this.metadataDao = new MetadataDao(this, this.fileDao); + } + + @Override + public ObjectDao getObjectDao() { + return this.objectDao; + } + + @Override + public MetadataDao getMetadataDao() { + return this.metadataDao; + } + + @Override + public void rollback() { + this.closed = true; + this.objectDao.rollback(); + this.objectFilter.clearCache(); + } + + @Override + public void commit(PersistenceContextFactory persistenceContextFactory) { + + try { + + long start = System.nanoTime(); + if (this.verbose) + logger.info("Committing TX..."); //$NON-NLS-1$ + + Set keySet = this.objectFilter.keySet(); + if (keySet.isEmpty()) + return; + for (String key : keySet) { + + List removed = this.objectFilter.getRemoved(key); + if (removed.isEmpty()) { + if (this.verbose) + logger.info("No objects removed in this tx."); //$NON-NLS-1$ + } else { + if (this.verbose) + logger.info(removed.size() + " objects removed in this tx."); //$NON-NLS-1$ + + for (Object object : removed) { + PersistenceContext context = persistenceContextFactory.createPersistenceContext(object); + this.fileDao.performDelete(context); + } + } + + List updated = this.objectFilter.getUpdated(key); + if (updated.isEmpty()) { + if (this.verbose) + logger.info("No objects updated in this tx."); //$NON-NLS-1$ + } else { + if (this.verbose) + logger.info(updated.size() + " objects updated in this tx."); //$NON-NLS-1$ + + for (Object object : updated) { + + PersistenceContext context = persistenceContextFactory.createPersistenceContext(object); + this.fileDao.performUpdate(context); + } + } + + List added = this.objectFilter.getAdded(key); + if (added.isEmpty()) { + if (this.verbose) + logger.info("No objects added in this tx."); //$NON-NLS-1$ + } else { + if (this.verbose) + logger.info(added.size() + " objects added in this tx."); //$NON-NLS-1$ + + for (Object object : added) { + + PersistenceContext context = persistenceContextFactory.createPersistenceContext(object); + this.fileDao.performCreate(context); + } + } + } + + long end = System.nanoTime(); + logger.info("Completed TX in " + StringHelper.formatNanoDuration(end - start)); //$NON-NLS-1$ + + } finally { + // clean up + this.objectFilter.clearCache(); + this.closed = true; + } + } + + @Override + public boolean isClosed() { + return this.closed; + } +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/DefaultXmlPersistenceManager.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/DefaultXmlPersistenceManager.java new file mode 100644 index 000000000..eb5963ff2 --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/DefaultXmlPersistenceManager.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ch.eitchnet.utils.helper.PropertiesHelper; +import ch.eitchnet.xmlpers.api.XmlPersistenceConstants; +import ch.eitchnet.xmlpers.impl.XmlPersistenceHandlerImpl; + +/** + * @author Robert von Burg + * + */ +public class DefaultXmlPersistenceManager implements XmlPersistenceManager { + + protected static final Logger logger = LoggerFactory.getLogger(XmlPersistenceHandlerImpl.class); + + protected boolean initialized; + protected boolean verbose; + + public void initialize(Properties properties) { + if (this.initialized) + throw new IllegalStateException("Already initialized!"); //$NON-NLS-1$ + + // get properties + String context = XmlPersistenceHandlerImpl.class.getSimpleName(); + boolean verbose = PropertiesHelper.getPropertyBool(properties, context, XmlPersistenceConstants.PROP_VERBOSE, + Boolean.FALSE).booleanValue(); + + this.verbose = verbose; + } + + @Override + public PersistenceTransaction openTx() { + + PersistenceTransaction tx = null; + + return tx; + } +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/MetadataDao.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/MetadataDao.java new file mode 100644 index 000000000..617057bda --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/MetadataDao.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import java.util.Set; + +/** + * @author Robert von Burg + * + */ +public class MetadataDao { + + private final DefaultPersistenceTransaction tx; + private final FileDao fileDao; + + public MetadataDao(DefaultPersistenceTransaction tx, FileDao fileDao) { + this.tx = tx; + this.fileDao = fileDao; + } + + public Set queryTypeSet() { + assertNotClosed(); + throw new UnsupportedOperationException("Not yet implemented!"); + } + + public Set queryTypeSet(String type) { + assertNotClosed(); + throw new UnsupportedOperationException("Not yet implemented!"); + } + + public Set queryKeySet(String type) { + assertNotClosed(); + throw new UnsupportedOperationException("Not yet implemented!"); + } + + public Set queryKeySet(String type, String subType) { + assertNotClosed(); + throw new UnsupportedOperationException("Not yet implemented!"); + } + + public long queryTypeSize() { + assertNotClosed(); + throw new UnsupportedOperationException("Not yet implemented!"); + } + + public long querySubTypeSize(String type) { + assertNotClosed(); + throw new UnsupportedOperationException("Not yet implemented!"); + } + + public long querySize(String type) { + assertNotClosed(); + throw new UnsupportedOperationException("Not yet implemented!"); + } + + public long querySize(String type, String subType) { + assertNotClosed(); + throw new UnsupportedOperationException("Not yet implemented!"); + } + + private void assertNotClosed() { + if (this.tx.isClosed()) + throw new IllegalStateException("Transaction has been closed and thus no operation can be performed!"); + } +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/ObjectDao.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/ObjectDao.java index cb5a9581b..6f554efcd 100644 --- a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/ObjectDao.java +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/ObjectDao.java @@ -21,33 +21,164 @@ */ package ch.eitchnet.xmlpers.test.impl.rewrite; +import java.text.MessageFormat; +import java.util.ArrayList; import java.util.List; import java.util.Set; +import ch.eitchnet.utils.helper.StringHelper; +import ch.eitchnet.utils.objectfilter.ObjectFilter; import ch.eitchnet.xmlpers.api.PersistenceContext; /** * @author Robert von Burg - * + * */ -public class ObjectDao { - - public void add(PersistenceContext context){} +public class ObjectDao { - public void update(PersistenceContext context){} + private final PersistenceTransaction tx; + private final ObjectFilter objectFilter; + private final FileDao fileDao; + private boolean closed; - public void remove(PersistenceContext context){} + public ObjectDao(PersistenceTransaction tx, FileDao fileDao, ObjectFilter objectFilter) { + this.tx = tx; + this.fileDao = fileDao; + this.objectFilter = objectFilter; + } - public void removeById(PersistenceContext context){} + public void add(Object object) { + assertNotClosed(); + assertNotNull(object); + this.objectFilter.add(object); + } - public void removeAll(PersistenceContext context){} + public void update(Object object) { + assertNotClosed(); + assertNotNull(object); + this.objectFilter.update(object); + } - public T queryById(PersistenceContext context){return null;} + public void remove(Object object) { + assertNotClosed(); + assertNotNull(object); + this.objectFilter.remove(object); + } - public List queryAll(PersistenceContext context){return null;} + public void removeById(PersistenceContext context) { + assertNotClosed(); + assertHasType(context); + assertHasId(context); + this.objectFilter.remove(context.clone()); + } - public Set queryKeySet(PersistenceContext context){return null;} + public void removeAll(PersistenceContext context) { + assertNotClosed(); + assertHasType(context); - public long querySize(PersistenceContext context){return 0L;} + Set keySet = queryKeySet(context); + for (String id : keySet) { + PersistenceContext clone = context.clone(); + clone.setId(id); + this.objectFilter.remove(clone); + } + } + public T queryById(PersistenceContext context) { + assertNotClosed(); + assertHasType(context); + assertHasId(context); + this.fileDao.performRead(context); + return context.getObject(); + } + + public List queryAll(PersistenceContext context) { + assertNotClosed(); + assertHasType(context); + + MetadataDao metadataDao = this.tx.getMetadataDao(); + Set keySet; + if (context.hasSubType()) + keySet = metadataDao.queryKeySet(context.getType(), context.getSubType()); + else + keySet = metadataDao.queryKeySet(context.getType()); + + List result = new ArrayList<>(); + PersistenceContext readContext = context.clone(); + for (String id : keySet) { + readContext.setId(id); + this.fileDao.performRead(readContext); + assertObjectRead(readContext); + result.add(readContext.getObject()); + } + + return result; + } + + public Set queryKeySet(PersistenceContext context) { + assertNotClosed(); + assertHasType(context); + + assertNotClosed(); + assertHasType(context); + + MetadataDao metadataDao = this.tx.getMetadataDao(); + Set keySet; + if (context.hasSubType()) + keySet = metadataDao.queryKeySet(context.getType(), context.getSubType()); + else + keySet = metadataDao.queryKeySet(context.getType()); + + return keySet; + } + + public long querySize(PersistenceContext context) { + assertNotClosed(); + assertHasType(context); + + assertNotClosed(); + assertHasType(context); + + MetadataDao metadataDao = this.tx.getMetadataDao(); + long size; + if (context.hasSubType()) + size = metadataDao.querySize(context.getType(), context.getSubType()); + else + size = metadataDao.querySize(context.getType()); + + return size; + } + + public void rollback() { + this.objectFilter.clearCache(); + this.closed = true; + } + + private void assertNotNull(Object object) { + if (object == null) + throw new RuntimeException("Object may not be null!"); //$NON-NLS-1$ + } + + private void assertObjectRead(PersistenceContext context) { + if (context.getObject() == null) { + String msg = "Failed to read object with for {0} / {1} / {2}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, context.getType(), context.getSubType(), context.getId()); + throw new RuntimeException(msg); + } + } + + private void assertHasType(PersistenceContext context) { + if (StringHelper.isEmpty(context.getType())) + throw new RuntimeException("Type is always needed on a persistence context!"); //$NON-NLS-1$ + } + + private void assertHasId(PersistenceContext context) { + if (StringHelper.isEmpty(context.getId())) + throw new RuntimeException("Id is not set on persistence context!"); //$NON-NLS-1$ + } + + private void assertNotClosed() { + if (this.closed || this.tx.isClosed()) + throw new IllegalStateException("Transaction has been closed and thus no operation can be performed!"); //$NON-NLS-1$ + } } diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/ObjectDaoTest.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/ObjectDaoTest.java new file mode 100644 index 000000000..07c21d77d --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/ObjectDaoTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import static ch.eitchnet.xmlpers.test.model.ModelBuilder.createResource; + +import java.util.Properties; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ch.eitchnet.xmlpers.test.model.Resource; + +/** + * @author Robert von Burg + * + */ +public class ObjectDaoTest { + + private static final Logger logger = LoggerFactory.getLogger(ObjectDaoTest.class); + + private PersistenceContextFactory persistenceContextFactory; + private XmlPersistenceManager persistenceManager; + private PersistenceTransaction tx; + + @BeforeClass + public static void beforeClass() { + } + + @Before + public void setUp() { + + this.persistenceContextFactory = new TestPersistenceContextFactory(); + + Properties properties = new Properties(); + this.persistenceManager = XmlPersistenceManagerLoader.load(properties); + } + + @After + public void tearDown() { + if (this.tx != null) { + this.tx.rollback(); + } + } + + @Test + @Ignore + public void testObjectDao() { + + this.tx = this.persistenceManager.openTx(); + + Resource resource = createResource(); + + this.tx.getObjectDao().add(resource); + this.tx.commit(this.persistenceContextFactory); + } +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/PersistenceContextFactory.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/PersistenceContextFactory.java new file mode 100644 index 000000000..8c299be3b --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/PersistenceContextFactory.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import ch.eitchnet.xmlpers.api.PersistenceContext; + +/** + * @author Robert von Burg + * + */ +public interface PersistenceContextFactory { + + public PersistenceContext createPersistenceContext(String type, String subType, String id); + + public PersistenceContext createPersistenceContext(String type, String subType); + + public PersistenceContext createPersistenceContext(String type); + + public PersistenceContext createPersistenceContext(T t); +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/PersistenceTransaction.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/PersistenceTransaction.java new file mode 100644 index 000000000..388b6a0f9 --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/PersistenceTransaction.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +/** + * @author Robert von Burg + * + */ +public interface PersistenceTransaction { + + public void commit(PersistenceContextFactory persistenceContextFactory); + + public void rollback(); + + public boolean isClosed(); + + public ObjectDao getObjectDao(); + + public MetadataDao getMetadataDao(); +} \ No newline at end of file diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/TestPersistenceContextFactory.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/TestPersistenceContextFactory.java new file mode 100644 index 000000000..f83531824 --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/TestPersistenceContextFactory.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import ch.eitchnet.xmlpers.api.ParserFactory; +import ch.eitchnet.xmlpers.api.PersistenceContext; +import ch.eitchnet.xmlpers.test.impl.Book; +import ch.eitchnet.xmlpers.test.impl.TestConstants; +import ch.eitchnet.xmlpers.test.model.Resource; + +/** + * @author Robert von Burg + * + */ +public class TestPersistenceContextFactory implements PersistenceContextFactory { + + @Override + public PersistenceContext createPersistenceContext(String type, String subType, String id) { + return buildPersistenceContext(type, subType, id); + } + + @Override + public PersistenceContext createPersistenceContext(String type, String subType) { + return buildPersistenceContext(type, subType, null); + } + + @Override + public PersistenceContext createPersistenceContext(String type) { + return buildPersistenceContext(type, null, null); + } + + @Override + public PersistenceContext createPersistenceContext(T t) { + if (t == null) + throw new RuntimeException("T may not be null!"); + + PersistenceContext context; + + if (t instanceof Resource) { + Resource resource = (Resource) t; + context = buildPersistenceContext(TestConstants.TYPE_RES, resource.getType(), resource.getId()); + } else if (t instanceof Book) { + context = buildPersistenceContext(TestConstants.TYPE_BOOK, null, ((Book) t).getId().toString()); + } else { + throw new UnsupportedOperationException("Handling of " + t.getClass().getName() + " is not implemented!"); + } + + context.setObject(t); + return context; + } + + private PersistenceContext buildPersistenceContext(String type, String subType, String id) { + + PersistenceContext context = new PersistenceContext<>(); + + context.setType(type); + context.setSubType(subType); + context.setId(id); + + context.setParserFactory(this. getParserFactoryInstance(type)); + + return context; + } + + /** + * @param type + * @return + */ + @SuppressWarnings("unchecked") + private ParserFactory getParserFactoryInstance(String type) { + + ParserFactory parserFactory; + if (type.equals(TestConstants.TYPE_RES)) + parserFactory = (ParserFactory) new ResourceParserFactory(); + else if (type.equals(TestConstants.TYPE_BOOK)) + parserFactory = (ParserFactory) new BookParserFactory(); + else + throw new UnsupportedOperationException("No ParserFactory can be returned for type " + type); + + return parserFactory; + } +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/XmlPersistenceManager.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/XmlPersistenceManager.java new file mode 100644 index 000000000..e57d02ca8 --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/XmlPersistenceManager.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + + +/** + * @author Robert von Burg + * + */ +public interface XmlPersistenceManager { + + public PersistenceTransaction openTx(); +} diff --git a/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/XmlPersistenceManagerLoader.java b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/XmlPersistenceManagerLoader.java new file mode 100644 index 000000000..4fbc34527 --- /dev/null +++ b/src/test/java/ch/eitchnet/xmlpers/test/impl/rewrite/XmlPersistenceManagerLoader.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package ch.eitchnet.xmlpers.test.impl.rewrite; + +import java.util.Properties; + +/** + * @author Robert von Burg + * + */ +public class XmlPersistenceManagerLoader { + + public static XmlPersistenceManager load(Properties properties) { + + DefaultXmlPersistenceManager persistenceManager = new DefaultXmlPersistenceManager(); + persistenceManager.initialize(properties); + return persistenceManager; + } +}