int prmIndex;
List<RelationMember> relationMemberList;
// We can't write an entity with a null timestamp.
if (relation.getTimestamp() == null) {
throw new OsmosisRuntimeException("Relation " + relation.getId() + " does not have a timestamp set.");
}
// Add or update the user in the database.
userManager.addOrUpdateUser(relation.getUser());
// Create the changeset in the database.
changesetManager.addChangesetIfRequired(relation.getChangesetId(), relation.getUser());
relationMemberList = relation.getMembers();
// If this is a deletion, the entity is not visible.
visible = !action.equals(ChangeAction.Delete);
// Create the prepared statements for relation creation if necessary.
if (insertRelationStatement == null) {
insertRelationStatement = statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_RELATION));
updateRelationStatement = statementContainer.add(dbCtx.prepareStatement(UPDATE_SQL_RELATION));
selectRelationCountStatement = statementContainer.add(dbCtx.prepareStatement(SELECT_SQL_RELATION_COUNT));
insertRelationCurrentStatement = statementContainer
.add(dbCtx.prepareStatement(INSERT_SQL_RELATION_CURRENT));
updateRelationCurrentStatement = statementContainer
.add(dbCtx.prepareStatement(UPDATE_SQL_RELATION_CURRENT));
selectRelationCurrentCountStatement = statementContainer.add(dbCtx
.prepareStatement(SELECT_SQL_RELATION_CURRENT_COUNT));
insertRelationTagStatement = statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_RELATION_TAG));
deleteRelationTagStatement = statementContainer.add(dbCtx.prepareStatement(DELETE_SQL_RELATION_TAG));
insertRelationTagCurrentStatement = statementContainer.add(dbCtx
.prepareStatement(INSERT_SQL_RELATION_TAG_CURRENT));
deleteRelationTagCurrentStatement = statementContainer.add(dbCtx
.prepareStatement(DELETE_SQL_RELATION_TAG_CURRENT));
switch (dbCtx.getDatabaseType()) {
case POSTGRESQL:
insertRelationMemberStatement =
statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_RELATION_MEMBER_PGSQL));
break;
case MYSQL:
insertRelationMemberStatement =
statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_RELATION_MEMBER_MYSQL));
break;
default:
throw new OsmosisRuntimeException("Unknown database type " + dbCtx.getDatabaseType() + ".");
}
deleteRelationMemberStatement = statementContainer.add(dbCtx.prepareStatement(DELETE_SQL_RELATION_MEMBER));
switch (dbCtx.getDatabaseType()) {
case POSTGRESQL:
insertRelationMemberCurrentStatement =
statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_RELATION_MEMBER_CURRENT_PGSQL));
break;
case MYSQL:
insertRelationMemberCurrentStatement =
statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_RELATION_MEMBER_CURRENT_MYSQL));
break;
default:
throw new OsmosisRuntimeException("Unknown database type " + dbCtx.getDatabaseType() + ".");
}
deleteRelationMemberCurrentStatement = statementContainer.add(dbCtx
.prepareStatement(DELETE_SQL_RELATION_MEMBER_CURRENT));
}
// Remove the existing tags of the relation history item.
try {
prmIndex = 1;
deleteRelationTagStatement.setLong(prmIndex++, relation.getId());
deleteRelationTagStatement.setInt(prmIndex++, relation.getVersion());
deleteRelationTagStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to delete relation history tags for relation with id="
+ relation.getId() + ".", e);
}
// Remove the existing relation members of the relation history item.
try {
prmIndex = 1;
deleteRelationMemberStatement.setLong(prmIndex++, relation.getId());
deleteRelationMemberStatement.setInt(prmIndex++, relation.getVersion());
deleteRelationMemberStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to delete relation history members for relation with id="
+ relation.getId() + ".", e);
}
// Update the relation if it already exists in the history table, otherwise insert it.
try {
exists = checkIfEntityHistoryExists(selectRelationCountStatement, relation.getId(), relation.getVersion());
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to check if current relation with id=" + relation.getId()
+ " exists.", e);
}
if (exists) {
// Update the relation in the history table.
try {
prmIndex = 1;
updateRelationStatement.setTimestamp(prmIndex++, new Timestamp(relation.getTimestamp().getTime()));
updateRelationStatement.setBoolean(prmIndex++, visible);
updateRelationStatement.setLong(prmIndex++, relation.getChangesetId());
updateRelationStatement.setLong(prmIndex++, relation.getId());
updateRelationStatement.setInt(prmIndex++, relation.getVersion());
updateRelationStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException(
"Unable to update history relation with id=" + relation.getId() + ".", e);
}
} else {
// Insert the new relation into the history table.
try {
prmIndex = 1;
insertRelationStatement.setLong(prmIndex++, relation.getId());
insertRelationStatement.setInt(prmIndex++, relation.getVersion());
insertRelationStatement.setTimestamp(prmIndex++, new Timestamp(relation.getTimestamp().getTime()));
insertRelationStatement.setBoolean(prmIndex++, visible);
insertRelationStatement.setLong(prmIndex++, relation.getChangesetId());
insertRelationStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException(
"Unable to insert history relation with id=" + relation.getId() + ".", e);
}
}
// Insert the tags of the new relation into the history table.
for (Tag tag : relation.getTags()) {
try {
prmIndex = 1;
insertRelationTagStatement.setLong(prmIndex++, relation.getId());
insertRelationTagStatement.setInt(prmIndex++, relation.getVersion());
insertRelationTagStatement.setString(prmIndex++, tag.getKey());
insertRelationTagStatement.setString(prmIndex++, tag.getValue());
insertRelationTagStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert history relation tag with id=" + relation.getId()
+ " and key=(" + tag.getKey() + ").", e);
}
}
// Insert the members of the new relation into the history table.
for (int i = 0; i < relationMemberList.size(); i++) {
RelationMember relationMember;
relationMember = relationMemberList.get(i);
try {
prmIndex = 1;
insertRelationMemberStatement.setLong(prmIndex++, relation.getId());
insertRelationMemberStatement.setInt(prmIndex++, relation.getVersion());
insertRelationMemberStatement.setString(prmIndex++, memberTypeRenderer.render(relationMember
.getMemberType()));
insertRelationMemberStatement.setLong(prmIndex++, relationMember.getMemberId());
insertRelationMemberStatement.setString(prmIndex++, relationMember.getMemberRole());
insertRelationMemberStatement.setInt(prmIndex++, i + 1);
insertRelationMemberStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert history relation member with relation id="
+ relation.getId() + ", member type=" + relationMember.getMemberId() + " and member id="
+ relationMember.getMemberId() + ".", e);
}
}
if (populateCurrentTables) {
// Delete the existing relation tags from the current table.
try {
deleteRelationTagCurrentStatement.setLong(1, relation.getId());
deleteRelationTagCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to delete current relation tags with id=" + relation.getId()
+ ".", e);
}
// Delete the existing relation members from the current table.
try {
deleteRelationMemberCurrentStatement.setLong(1, relation.getId());
deleteRelationMemberCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to delete current relation members with id="
+ relation.getId() + ".", e);
}
// Update the relation if it already exists in the current table, otherwise insert it.
try {
exists = checkIfEntityExists(selectRelationCurrentCountStatement, relation.getId());
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to check if current relation with id=" + relation.getId()
+ " exists.", e);
}
if (exists) {
// Update the relation in the current table.
try {
prmIndex = 1;
updateRelationCurrentStatement.setInt(prmIndex++, relation.getVersion());
updateRelationCurrentStatement.setTimestamp(prmIndex++, new Timestamp(relation.getTimestamp()
.getTime()));
updateRelationCurrentStatement.setBoolean(prmIndex++, visible);
updateRelationCurrentStatement.setLong(prmIndex++, relation.getChangesetId());
updateRelationCurrentStatement.setLong(prmIndex++, relation.getId());
updateRelationCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to update current relation with id=" + relation.getId()
+ ".", e);
}
} else {
// Insert the new node into the current table.
try {
prmIndex = 1;
insertRelationCurrentStatement.setLong(prmIndex++, relation.getId());
insertRelationCurrentStatement.setInt(prmIndex++, relation.getVersion());
insertRelationCurrentStatement.setTimestamp(prmIndex++, new Timestamp(relation.getTimestamp()
.getTime()));
insertRelationCurrentStatement.setBoolean(prmIndex++, visible);
insertRelationCurrentStatement.setLong(prmIndex++, relation.getChangesetId());
insertRelationCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert current relation with id=" + relation.getId()
+ ".", e);
}
}
// Insert the tags of the new relation into the current table.
for (Tag tag : relation.getTags()) {
try {
prmIndex = 1;
insertRelationTagCurrentStatement.setLong(prmIndex++, relation.getId());
insertRelationTagCurrentStatement.setString(prmIndex++, tag.getKey());
insertRelationTagCurrentStatement.setString(prmIndex++, tag.getValue());
insertRelationTagCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert current relation tag with id="
+ relation.getId() + " and key=(" + tag.getKey() + ").", e);
}
}
// Insert the members of the new relation into the current table.
for (int i = 0; i < relationMemberList.size(); i++) {
RelationMember relationMember;
relationMember = relationMemberList.get(i);
try {
prmIndex = 1;
insertRelationMemberCurrentStatement.setLong(prmIndex++, relation.getId());
insertRelationMemberCurrentStatement.setString(prmIndex++, memberTypeRenderer.render(relationMember
.getMemberType()));
insertRelationMemberCurrentStatement.setLong(prmIndex++, relationMember.getMemberId());
insertRelationMemberCurrentStatement.setString(prmIndex++, relationMember.getMemberRole());
insertRelationMemberCurrentStatement.setInt(prmIndex++, i + 1);
insertRelationMemberCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert current relation member with relation id="
+ relation.getId() + ", member type=" + relationMember.getMemberId() + " and member id="
+ relationMember.getMemberId() + ".", e);
}
}
}