package de.retest.recheck.persistence;

import static de.retest.recheck.XmlTransformerUtil.getXmlTransformer;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Set;

import de.retest.persistence.xml.XmlFolderPersistence;
import de.retest.recheck.RecheckAdapter;
import de.retest.ui.descriptors.RootElement;
import de.retest.ui.descriptors.SutState;

public class RecheckSutState {

	private static final XmlFolderPersistence<SutState> sutStatePersistence =
			new XmlFolderPersistence<>( getXmlTransformer() );

	private RecheckSutState() {

	}

	public static SutState convert( final Object toVerify, final RecheckAdapter adapter ) {
		final Set<RootElement> converted = adapter.convert( toVerify );
		if ( converted == null || converted.isEmpty() ) {
			throw new IllegalStateException( "Cannot check empty state!" );
		}
		return new SutState( converted );
	}

	public static SutState createNew( final File file, final SutState actual ) {
		try {
			sutStatePersistence.save( file.toURI(), actual );
		} catch ( final IOException e ) {
			throw new UncheckedIOException( "Could not save sut state '" + actual + "' to '" + file + "'.", e );
		}
		return new SutState( new ArrayList<>() );
	}

	public static SutState loadExpected( final File file ) {
		if ( !file.exists() ) {
			return null;
		}
		// Folder could exist, but not the retest.xml...
		if ( !new File( file, sutStatePersistence.getXmlFileName() ).exists() ) {
			return null;
		}
		try {
			return sutStatePersistence.load( file.toURI() );
		} catch ( final IOException e ) {
			throw new UncheckedIOException( "Could not load sut state from '" + file + "'.", e );
		}
	}
}

@@ -262,33 +262,49 @@
 est.
-persistence.xml.XmlFolder
+Properties;%0Aimport de.retest.persistence.
 Pers
@@ -310,16 +310,23 @@
 sistence
+Factory
 ;%0Aimport
@@ -502,25 +502,16 @@
 c final 
-XmlFolder
 Persiste
@@ -517,57 +517,41 @@
 ence
-%3CSutState%3E sutStatePersistence =%0A%09%09%09new XmlFolder
+Factory persistenceFactory = new 
 Pers
@@ -557,18 +557,23 @@
 sistence
-%3C%3E
+Factory
 ( getXml
@@ -1031,24 +1031,38 @@
 ry %7B%0A%09%09%09
-sutState
+persistenceFactory.get
 Persiste
@@ -1064,16 +1064,18 @@
 sistence
+()
 .save( f
@@ -1472,44 +1472,40 @@
 le, 
-sutStatePersistence.getXmlFileName()
+Properties.DEFAULT_XML_FILE_NAME
  ).e
@@ -1558,16 +1558,41 @@
 urn 
-s
+(S
 utState
+) persistenceFactory.get
 Pers
@@ -1598,16 +1598,18 @@
 sistence
+()
 .load( f

Use factory instead of specific persistence