[New] working on rewrite

This commit is contained in:
Robert von Burg 2013-10-06 12:32:57 +02:00
parent bacc6d72fd
commit f81dec8939
16 changed files with 958 additions and 16 deletions

View File

@ -21,6 +21,7 @@
*/
package ch.eitchnet.xmlpers.api;
import ch.eitchnet.utils.helper.StringHelper;
public class PersistenceContext<T> {
@ -79,4 +80,20 @@ public class PersistenceContext<T> {
public void setParserFactory(ParserFactory<T> parserFactory) {
this.parserFactory = parserFactory;
}
public boolean hasSubType() {
return !StringHelper.isEmpty(this.subType);
}
@Override
public PersistenceContext<T> clone() {
PersistenceContext<T> 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;
}
}

View File

@ -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";
}

View File

@ -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<String> 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

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
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 <eitch@eitchnet.ch>
*
*/
public class BookDomParser implements DomParser<Book> {
@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
}
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
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 <eitch@eitchnet.ch>
*
*/
public class BookParserFactory implements ParserFactory<Book> {
@Override
public DomParser<Book> getDomParser() {
return new BookDomParser();
}
@Override
public SaxParser<Book> getSaxParser() {
return new BookSaxParser();
}
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
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 <eitch@eitchnet.ch>
*
*/
public class BookSaxParser implements SaxParser<Book> {
@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
}
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
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 <eitch@eitchnet.ch>
*
*/
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<String> keySet = this.objectFilter.keySet();
if (keySet.isEmpty())
return;
for (String key : keySet) {
List<Object> 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<Object> context = persistenceContextFactory.createPersistenceContext(object);
this.fileDao.performDelete(context);
}
}
List<Object> 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<Object> context = persistenceContextFactory.createPersistenceContext(object);
this.fileDao.performUpdate(context);
}
}
List<Object> 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<Object> 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;
}
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
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 <eitch@eitchnet.ch>
*
*/
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;
}
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.xmlpers.test.impl.rewrite;
import java.util.Set;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
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<String> queryTypeSet() {
assertNotClosed();
throw new UnsupportedOperationException("Not yet implemented!");
}
public Set<String> queryTypeSet(String type) {
assertNotClosed();
throw new UnsupportedOperationException("Not yet implemented!");
}
public Set<String> queryKeySet(String type) {
assertNotClosed();
throw new UnsupportedOperationException("Not yet implemented!");
}
public Set<String> 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!");
}
}

View File

@ -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 <eitch@eitchnet.ch>
*
*
*/
public class ObjectDao<T> {
public void add(PersistenceContext<T> context){}
public class ObjectDao {
public void update(PersistenceContext<T> context){}
private final PersistenceTransaction tx;
private final ObjectFilter objectFilter;
private final FileDao fileDao;
private boolean closed;
public void remove(PersistenceContext<T> context){}
public ObjectDao(PersistenceTransaction tx, FileDao fileDao, ObjectFilter objectFilter) {
this.tx = tx;
this.fileDao = fileDao;
this.objectFilter = objectFilter;
}
public void removeById(PersistenceContext<T> context){}
public void add(Object object) {
assertNotClosed();
assertNotNull(object);
this.objectFilter.add(object);
}
public void removeAll(PersistenceContext<T> context){}
public <T> void update(Object object) {
assertNotClosed();
assertNotNull(object);
this.objectFilter.update(object);
}
public T queryById(PersistenceContext<T> context){return null;}
public <T> void remove(Object object) {
assertNotClosed();
assertNotNull(object);
this.objectFilter.remove(object);
}
public List<T> queryAll(PersistenceContext<T> context){return null;}
public <T> void removeById(PersistenceContext<T> context) {
assertNotClosed();
assertHasType(context);
assertHasId(context);
this.objectFilter.remove(context.clone());
}
public Set<String> queryKeySet(PersistenceContext<T> context){return null;}
public <T> void removeAll(PersistenceContext<T> context) {
assertNotClosed();
assertHasType(context);
public long querySize(PersistenceContext<T> context){return 0L;}
Set<String> keySet = queryKeySet(context);
for (String id : keySet) {
PersistenceContext<T> clone = context.clone();
clone.setId(id);
this.objectFilter.remove(clone);
}
}
public <T> T queryById(PersistenceContext<T> context) {
assertNotClosed();
assertHasType(context);
assertHasId(context);
this.fileDao.performRead(context);
return context.getObject();
}
public <T> List<T> queryAll(PersistenceContext<T> context) {
assertNotClosed();
assertHasType(context);
MetadataDao metadataDao = this.tx.getMetadataDao();
Set<String> keySet;
if (context.hasSubType())
keySet = metadataDao.queryKeySet(context.getType(), context.getSubType());
else
keySet = metadataDao.queryKeySet(context.getType());
List<T> result = new ArrayList<>();
PersistenceContext<T> readContext = context.clone();
for (String id : keySet) {
readContext.setId(id);
this.fileDao.performRead(readContext);
assertObjectRead(readContext);
result.add(readContext.getObject());
}
return result;
}
public <T> Set<String> queryKeySet(PersistenceContext<T> context) {
assertNotClosed();
assertHasType(context);
assertNotClosed();
assertHasType(context);
MetadataDao metadataDao = this.tx.getMetadataDao();
Set<String> keySet;
if (context.hasSubType())
keySet = metadataDao.queryKeySet(context.getType(), context.getSubType());
else
keySet = metadataDao.queryKeySet(context.getType());
return keySet;
}
public <T> long querySize(PersistenceContext<T> 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 <T> void assertNotNull(Object object) {
if (object == null)
throw new RuntimeException("Object may not be null!"); //$NON-NLS-1$
}
private <T> void assertObjectRead(PersistenceContext<T> 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 <T> void assertHasType(PersistenceContext<T> context) {
if (StringHelper.isEmpty(context.getType()))
throw new RuntimeException("Type is always needed on a persistence context!"); //$NON-NLS-1$
}
private <T> void assertHasId(PersistenceContext<T> 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$
}
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
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 <eitch@eitchnet.ch>
*
*/
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);
}
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.xmlpers.test.impl.rewrite;
import ch.eitchnet.xmlpers.api.PersistenceContext;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public interface PersistenceContextFactory {
public <T> PersistenceContext<T> createPersistenceContext(String type, String subType, String id);
public <T> PersistenceContext<T> createPersistenceContext(String type, String subType);
public <T> PersistenceContext<T> createPersistenceContext(String type);
public <T> PersistenceContext<T> createPersistenceContext(T t);
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.xmlpers.test.impl.rewrite;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public interface PersistenceTransaction {
public void commit(PersistenceContextFactory persistenceContextFactory);
public void rollback();
public boolean isClosed();
public ObjectDao getObjectDao();
public MetadataDao getMetadataDao();
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
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 <eitch@eitchnet.ch>
*
*/
public class TestPersistenceContextFactory implements PersistenceContextFactory {
@Override
public <T> PersistenceContext<T> createPersistenceContext(String type, String subType, String id) {
return buildPersistenceContext(type, subType, id);
}
@Override
public <T> PersistenceContext<T> createPersistenceContext(String type, String subType) {
return buildPersistenceContext(type, subType, null);
}
@Override
public <T> PersistenceContext<T> createPersistenceContext(String type) {
return buildPersistenceContext(type, null, null);
}
@Override
public <T> PersistenceContext<T> createPersistenceContext(T t) {
if (t == null)
throw new RuntimeException("T may not be null!");
PersistenceContext<T> 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 <T> PersistenceContext<T> buildPersistenceContext(String type, String subType, String id) {
PersistenceContext<T> context = new PersistenceContext<>();
context.setType(type);
context.setSubType(subType);
context.setId(id);
context.setParserFactory(this.<T> getParserFactoryInstance(type));
return context;
}
/**
* @param type
* @return
*/
@SuppressWarnings("unchecked")
private <T> ParserFactory<T> getParserFactoryInstance(String type) {
ParserFactory<T> parserFactory;
if (type.equals(TestConstants.TYPE_RES))
parserFactory = (ParserFactory<T>) new ResourceParserFactory();
else if (type.equals(TestConstants.TYPE_BOOK))
parserFactory = (ParserFactory<T>) new BookParserFactory();
else
throw new UnsupportedOperationException("No ParserFactory can be returned for type " + type);
return parserFactory;
}
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.xmlpers.test.impl.rewrite;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public interface XmlPersistenceManager {
public PersistenceTransaction openTx();
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
package ch.eitchnet.xmlpers.test.impl.rewrite;
import java.util.Properties;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class XmlPersistenceManagerLoader {
public static XmlPersistenceManager load(Properties properties) {
DefaultXmlPersistenceManager persistenceManager = new DefaultXmlPersistenceManager();
persistenceManager.initialize(properties);
return persistenceManager;
}
}