[Major] Moved the auditing trail to the ElementMaps themselves

This means that we can't access the ElementMaps from the Realms, but
must use the TX (this was better anyhow)
This commit is contained in:
Robert von Burg 2014-08-24 12:45:14 +02:00
parent 9ca21c29f8
commit a7f7cae641
5 changed files with 80 additions and 75 deletions

View File

@ -15,12 +15,12 @@
*/
package li.strolch.persistence.postgresql;
import li.strolch.persistence.api.ModificationResult;
import li.strolch.persistence.api.TransactionResult;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface DaoCommand {
public void doComand(ModificationResult modificationResult);
public void doComand(TransactionResult txResult);
}

View File

@ -50,7 +50,7 @@ public class PostgreSqlAuditDao implements AuditDao {
@Override
public boolean hasElement(String type, Long id) {
String sql = "select count(*) from audits where element_type = ? and id = ?";
String sql = "select count(*) from audits where element_type = ? and id = ?"; //$NON-NLS-1$
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
statement.setString(1, type);
@ -64,18 +64,18 @@ public class PostgreSqlAuditDao implements AuditDao {
if (numberOfElements == 1)
return true;
String msg = MessageFormat.format("Non unique number of elements with type {0} and id {1}", type, id);
String msg = MessageFormat.format("Non unique number of elements with type {0} and id {1}", type, id); //$NON-NLS-1$
throw new StrolchPersistenceException(msg);
}
} catch (SQLException e) {
throw new StrolchPersistenceException("Failed to query size due to: " + e.getMessage(), e);
throw new StrolchPersistenceException("Failed to query size due to: " + e.getMessage(), e); //$NON-NLS-1$
}
}
@Override
public long querySize(DateRange dateRange) {
String sql = "select count(*) from audits where date between ? and ?";
String sql = "select count(*) from audits where date between ? and ?"; //$NON-NLS-1$
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
statement.setDate(1, new Date(dateRange.getFromDate().getTime()), Calendar.getInstance());
@ -87,13 +87,13 @@ public class PostgreSqlAuditDao implements AuditDao {
}
} catch (SQLException e) {
throw new StrolchPersistenceException("Failed to query size due to: " + e.getMessage(), e);
throw new StrolchPersistenceException("Failed to query size due to: " + e.getMessage(), e); //$NON-NLS-1$
}
}
@Override
public long querySize(String type, DateRange dateRange) {
String sql = "select count(*) from audits where element_type = ? and date between ? and ?";
String sql = "select count(*) from audits where element_type = ? and date between ? and ?"; //$NON-NLS-1$
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
statement.setString(1, type);
@ -106,7 +106,7 @@ public class PostgreSqlAuditDao implements AuditDao {
}
} catch (SQLException e) {
throw new StrolchPersistenceException("Failed to query size due to: " + e.getMessage(), e);
throw new StrolchPersistenceException("Failed to query size due to: " + e.getMessage(), e); //$NON-NLS-1$
}
}
@ -114,15 +114,15 @@ public class PostgreSqlAuditDao implements AuditDao {
public Set<String> queryTypes() {
Set<String> keySet = new HashSet<>();
String sql = "select distinct element_type from audits";
String sql = "select distinct element_type from audits"; //$NON-NLS-1$
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
try (ResultSet result = statement.executeQuery()) {
while (result.next()) {
keySet.add(result.getString("element_type"));
keySet.add(result.getString("element_type")); //$NON-NLS-1$
}
}
} catch (SQLException e) {
throw new StrolchPersistenceException("Failed to query types due to: " + e.getMessage(), e);
throw new StrolchPersistenceException("Failed to query types due to: " + e.getMessage(), e); //$NON-NLS-1$
}
return keySet;
@ -131,7 +131,7 @@ public class PostgreSqlAuditDao implements AuditDao {
@Override
public Audit queryBy(String type, Long id) {
String sql = "select id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type from audits where element_type = ? and id = ?";
String sql = "select id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type from audits where element_type = ? and id = ?"; //$NON-NLS-1$
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
statement.setString(1, type);
@ -143,18 +143,18 @@ public class PostgreSqlAuditDao implements AuditDao {
}
Audit audit = auditFrom(result);
if (result.next())
throw new StrolchPersistenceException("Non unique result for query: " + sql);
throw new StrolchPersistenceException("Non unique result for query: " + sql); //$NON-NLS-1$
return audit;
}
} catch (SQLException e) {
throw new StrolchPersistenceException("Failed to query types due to: " + e.getMessage(), e);
throw new StrolchPersistenceException("Failed to query types due to: " + e.getMessage(), e); //$NON-NLS-1$
}
}
@Override
public List<Audit> queryAll(String type, DateRange dateRange) {
List<Audit> list = new ArrayList<>();
String sql = "select id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type from audits where element_type = ? and date between ? and ?";
String sql = "select id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type from audits where element_type = ? and date between ? and ?"; //$NON-NLS-1$
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
statement.setString(1, type);
@ -168,7 +168,7 @@ public class PostgreSqlAuditDao implements AuditDao {
}
} catch (SQLException e) {
throw new StrolchPersistenceException("Failed to query types due to: " + e.getMessage(), e);
throw new StrolchPersistenceException("Failed to query types due to: " + e.getMessage(), e); //$NON-NLS-1$
}
return list;
@ -176,19 +176,19 @@ public class PostgreSqlAuditDao implements AuditDao {
@Override
public void save(Audit audit) {
String sql = "insert into audits (id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?::access_type)";
try (PreparedStatement preparedStatement = tx.getConnection().prepareStatement(sql)) {
String sql = "insert into audits (id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?::access_type)"; //$NON-NLS-1$
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
setAuditFields(audit, preparedStatement);
int count = preparedStatement.executeUpdate();
if (count != 1) {
throw new StrolchPersistenceException(MessageFormat.format(
"Expected to create 1 record, but created {0} for audit {2}", count, audit.getId()));
"Expected to create 1 record, but created {0} for audit {2}", count, audit.getId())); //$NON-NLS-1$
}
} catch (SQLException e) {
throw new StrolchPersistenceException(MessageFormat.format("Failed to update Audit {0} due to {1}", audit,
throw new StrolchPersistenceException(MessageFormat.format("Failed to update Audit {0} due to {1}", audit, //$NON-NLS-1$
e.getLocalizedMessage()), e);
}
}
@ -202,8 +202,8 @@ public class PostgreSqlAuditDao implements AuditDao {
@Override
public void update(Audit audit) {
String sql = "update audits set id = ?, username = ?, firstname = ?, lastname = ?, date = ?, element_type = ?, element_accessed = ?, new_version = ?, action = ?, access_type = ?::access_type where id = ?";
try (PreparedStatement preparedStatement = tx.getConnection().prepareStatement(sql)) {
String sql = "update audits set id = ?, username = ?, firstname = ?, lastname = ?, date = ?, element_type = ?, element_accessed = ?, new_version = ?, action = ?, access_type = ?::access_type where id = ?"; //$NON-NLS-1$
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
setAuditFields(audit, preparedStatement);
preparedStatement.setLong(11, audit.getId());
@ -211,11 +211,11 @@ public class PostgreSqlAuditDao implements AuditDao {
int count = preparedStatement.executeUpdate();
if (count != 1) {
throw new StrolchPersistenceException(MessageFormat.format(
"Expected to update 1 record, but updated {0} for audit {2}", count, audit.getId()));
"Expected to update 1 record, but updated {0} for audit {2}", count, audit.getId())); //$NON-NLS-1$
}
} catch (SQLException e) {
throw new StrolchPersistenceException(MessageFormat.format("Failed to update Audit {0} due to {1}", audit,
throw new StrolchPersistenceException(MessageFormat.format("Failed to update Audit {0} due to {1}", audit, //$NON-NLS-1$
e.getLocalizedMessage()), e);
}
}
@ -229,20 +229,20 @@ public class PostgreSqlAuditDao implements AuditDao {
@Override
public void remove(Audit audit) {
String sql = "delete from audits where id = ?";
String sql = "delete from audits where id = ?"; //$NON-NLS-1$
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
preparedStatement.setLong(1, audit.getId());
int count = preparedStatement.executeUpdate();
if (count != 1) {
String msg = "Expected to delete 1 audit with id {0} but deleted {1} elements!";
String msg = "Expected to delete 1 audit with id {0} but deleted {1} elements!"; //$NON-NLS-1$
msg = MessageFormat.format(msg, audit.getId(), count);
throw new StrolchPersistenceException(msg);
}
} catch (SQLException e) {
throw new StrolchPersistenceException(MessageFormat.format("Failed to remove {0} due to {2}",
throw new StrolchPersistenceException(MessageFormat.format("Failed to remove {0} due to {2}", //$NON-NLS-1$
audit.getId(), e.getLocalizedMessage()), e);
}
}
@ -256,7 +256,7 @@ public class PostgreSqlAuditDao implements AuditDao {
@Override
public long removeAll(String type, DateRange dateRange) {
String sql = "delete from audits where element_type = ? and date between ? and ?";
String sql = "delete from audits where element_type = ? and date between ? and ?"; //$NON-NLS-1$
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
preparedStatement.setString(1, type);
@ -267,7 +267,7 @@ public class PostgreSqlAuditDao implements AuditDao {
return modCount;
} catch (SQLException e) {
throw new StrolchPersistenceException(MessageFormat.format("Failed to remove all elements due to {0}",
throw new StrolchPersistenceException(MessageFormat.format("Failed to remove all elements due to {0}", //$NON-NLS-1$
e.getLocalizedMessage()), e);
}
}

View File

@ -26,7 +26,6 @@ import java.util.List;
import java.util.Set;
import li.strolch.model.StrolchElement;
import li.strolch.persistence.api.ModificationResult;
import li.strolch.persistence.api.StrolchDao;
import li.strolch.persistence.api.StrolchPersistenceException;
import li.strolch.persistence.api.TransactionResult;
@ -224,9 +223,9 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
public void save(final T res) {
this.commands.add(new DaoCommand() {
@Override
public void doComand(ModificationResult modificationResult) {
public void doComand(TransactionResult txResult) {
internalSave(res);
modificationResult.getCreated().add(res);
txResult.incCreated(1);
}
});
}
@ -235,11 +234,11 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
public void saveAll(final List<T> elements) {
this.commands.add(new DaoCommand() {
@Override
public void doComand(ModificationResult modificationResult) {
public void doComand(TransactionResult txResult) {
for (T element : elements) {
internalSave(element);
}
modificationResult.getCreated().addAll(elements);
txResult.incCreated(elements.size());
}
});
}
@ -248,9 +247,9 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
public void update(final T element) {
this.commands.add(new DaoCommand() {
@Override
public void doComand(ModificationResult modificationResult) {
public void doComand(TransactionResult txResult) {
internalUpdate(element);
modificationResult.getUpdated().add(element);
txResult.incUpdated(1);
}
});
}
@ -259,11 +258,11 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
public void updateAll(final List<T> elements) {
this.commands.add(new DaoCommand() {
@Override
public void doComand(ModificationResult modificationResult) {
public void doComand(TransactionResult txResult) {
for (T element : elements) {
internalUpdate(element);
}
modificationResult.getUpdated().addAll(elements);
txResult.incUpdated(elements.size());
}
});
}
@ -272,9 +271,9 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
public void remove(final T element) {
this.commands.add(new DaoCommand() {
@Override
public void doComand(ModificationResult modificationResult) {
public void doComand(TransactionResult txResult) {
internalRemove(element);
modificationResult.getDeleted().add(element);
txResult.incDeleted(1);
}
});
}
@ -283,11 +282,11 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
public void removeAll(final List<T> elements) {
this.commands.add(new DaoCommand() {
@Override
public void doComand(ModificationResult modificationResult) {
public void doComand(TransactionResult txResult) {
for (T element : elements) {
internalRemove(element);
}
modificationResult.getDeleted().addAll(elements);
txResult.incDeleted(elements.size());
}
});
}
@ -299,8 +298,9 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
this.commands.add(new DaoCommand() {
@Override
public void doComand(ModificationResult modificationResult) {
public void doComand(TransactionResult txResult) {
internalRemoveAll(toRemove);
txResult.incDeleted(toRemove);
}
});
@ -314,8 +314,9 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
this.commands.add(new DaoCommand() {
@Override
public void doComand(ModificationResult modificationResult) {
public void doComand(TransactionResult txResult) {
internalRemoveAllBy(toRemove, type);
txResult.incDeleted(toRemove);
}
});
@ -384,10 +385,8 @@ public abstract class PostgresqlDao<T extends StrolchElement> implements Strolch
}
void commit(TransactionResult txResult) {
ModificationResult modificationResult = new ModificationResult(getClassName());
txResult.addModificationResult(modificationResult);
for (DaoCommand command : this.commands) {
command.doComand(modificationResult);
command.doComand(txResult);
}
}

View File

@ -33,7 +33,7 @@ import java.util.Map;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.persistence.api.ModificationResult;
import li.strolch.persistence.api.StrolchTransaction;
@ -96,17 +96,17 @@ public class ObserverUpdateTest {
}
@Override
public void update(String key, List<StrolchElement> elements) {
public void update(String key, List<StrolchRootElement> elements) {
getModificationResult(key).getUpdated().addAll(elements);
}
@Override
public void remove(String key, List<StrolchElement> elements) {
public void remove(String key, List<StrolchRootElement> elements) {
getModificationResult(key).getDeleted().addAll(elements);
}
@Override
public void add(String key, List<StrolchElement> elements) {
public void add(String key, List<StrolchRootElement> elements) {
getModificationResult(key).getCreated().addAll(elements);
}
}
@ -120,17 +120,17 @@ public class ObserverUpdateTest {
runtimeMock.getContainer().getComponent(ObserverHandler.class).registerObserver(Tags.RESOURCE, observer);
PrivilegeHandler privilegeHandler = runtimeMock.getAgent().getContainer().getPrivilegeHandler();
Certificate certificate = privilegeHandler.authenticate("test", "test".getBytes());
Certificate certificate = privilegeHandler.authenticate("test", "test".getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
// create order
Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType", new Date(), State.CREATED); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = runtimeMock.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {
try (StrolchTransaction tx = runtimeMock.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) { //$NON-NLS-1$
tx.getOrderMap().add(tx, newOrder);
}
// create resource
Resource newResource = createResource("MyTestResource", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = runtimeMock.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test");) {
try (StrolchTransaction tx = runtimeMock.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test");) { //$NON-NLS-1$
tx.getResourceMap().add(tx, newResource);
}

View File

@ -40,6 +40,12 @@ import ch.eitchnet.privilege.model.Certificate;
public class RealmTest extends AbstractModelTest {
private static final String TESTUSER2 = "testuser2"; //$NON-NLS-1$
private static final String TESTUSER1 = "testuser1"; //$NON-NLS-1$
private static final String SECOND = "second"; //$NON-NLS-1$
private static final String TEST = "test"; //$NON-NLS-1$
private static final String FIRST = "first"; //$NON-NLS-1$
public static final String RUNTIME_PATH = "target/realmtest/"; //$NON-NLS-1$
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
public static final String CONFIG_SRC = "src/test/resources/realmtest"; //$NON-NLS-1$
@ -54,8 +60,8 @@ public class RealmTest extends AbstractModelTest {
@BeforeClass
public static void beforeClass() throws SQLException {
dropSchema("jdbc:postgresql://localhost/testdb1", "testuser1", "test");
dropSchema("jdbc:postgresql://localhost/testdb2", "testuser2", "test");
dropSchema("jdbc:postgresql://localhost/testdb1", TESTUSER1, TEST); //$NON-NLS-1$
dropSchema("jdbc:postgresql://localhost/testdb2", TESTUSER2, TEST); //$NON-NLS-1$
File rootPath = new File(RUNTIME_PATH);
File configSrc = new File(CONFIG_SRC);
@ -67,52 +73,52 @@ public class RealmTest extends AbstractModelTest {
@Before
public void before() {
this.realmName = "second";
this.realmName = SECOND;
}
@Test
public void testDifferentRealms() {
String expectedId1 = "@realmTestId1";
String expectedId2 = "@realmTestId2";
String type = "Bla";
String expectedId1 = "@realmTestId1"; //$NON-NLS-1$
String expectedId2 = "@realmTestId2"; //$NON-NLS-1$
String type = "Bla"; //$NON-NLS-1$
PrivilegeHandler privilegeHandler = runtimeMock.getAgent().getContainer().getPrivilegeHandler();
Certificate certificate = privilegeHandler.authenticate("test", "test".getBytes());
Certificate certificate = privilegeHandler.authenticate(TEST, TEST.getBytes());
{
StrolchRealm firstRealm = runtimeMock.getRealm("first");
StrolchRealm firstRealm = runtimeMock.getRealm(FIRST);
assertEquals(DataStoreMode.TRANSACTIONAL, firstRealm.getMode());
Resource expectedRes1 = ModelGenerator.createResource(expectedId1, "Bla bla", type);
try (StrolchTransaction tx = firstRealm.openTx(certificate, "test")) {
Resource expectedRes1 = ModelGenerator.createResource(expectedId1, "Bla bla", type); //$NON-NLS-1$
try (StrolchTransaction tx = firstRealm.openTx(certificate, TEST)) {
tx.getResourceMap().add(tx, expectedRes1);
}
try (StrolchTransaction tx = firstRealm.openTx(certificate, "test")) {
try (StrolchTransaction tx = firstRealm.openTx(certificate, TEST)) {
Resource res = tx.getResourceMap().getBy(tx, type, expectedId1);
assertEquals("Should find object previously added in same realm!", expectedRes1, res);
assertEquals("Should find object previously added in same realm!", expectedRes1, res); //$NON-NLS-1$
}
}
{
StrolchRealm secondRealm = runtimeMock.getRealm("second");
StrolchRealm secondRealm = runtimeMock.getRealm(SECOND);
assertEquals(DataStoreMode.TRANSACTIONAL, secondRealm.getMode());
Resource expectedRes2 = ModelGenerator.createResource(expectedId2, "Bla bla", type);
try (StrolchTransaction tx = secondRealm.openTx(certificate, "test")) {
Resource expectedRes2 = ModelGenerator.createResource(expectedId2, "Bla bla", type); //$NON-NLS-1$
try (StrolchTransaction tx = secondRealm.openTx(certificate, TEST)) {
tx.getResourceMap().add(tx, expectedRes2);
}
try (StrolchTransaction tx = secondRealm.openTx(certificate, "test")) {
try (StrolchTransaction tx = secondRealm.openTx(certificate, TEST)) {
Resource res = tx.getResourceMap().getBy(tx, type, expectedId2);
assertEquals("Should find object previously added in same realm!", expectedRes2, res);
assertEquals("Should find object previously added in same realm!", expectedRes2, res); //$NON-NLS-1$
}
}
{
StrolchRealm secondRealm = runtimeMock.getRealm("second");
try (StrolchTransaction tx = secondRealm.openTx(certificate, "test")) {
StrolchRealm secondRealm = runtimeMock.getRealm(SECOND);
try (StrolchTransaction tx = secondRealm.openTx(certificate, TEST)) {
Resource res = tx.getResourceMap().getBy(tx, type, expectedId1);
assertNull("Should not find object added in differenct realm!", res);
assertNull("Should not find object added in differenct realm!", res); //$NON-NLS-1$
}
}
}