use of org.mozilla.jss.asn1.Tag in project osmosis by openstreetmap.
the class ChangeWriter method write.
/**
* Writes the specified way change to the database.
*
* @param way The way to be written.
* @param action The change to be applied.
*/
public void write(Way way, ChangeAction action) {
boolean visible;
boolean exists;
int prmIndex;
List<WayNode> nodeReferenceList;
assertEntityHasTimestamp(way);
// Add or update the user in the database.
userManager.addOrUpdateUser(way.getUser());
// Create the changeset in the database.
changesetManager.addChangesetIfRequired(way.getChangesetId(), way.getUser());
nodeReferenceList = way.getWayNodes();
// If this is a deletion, the entity is not visible.
visible = !action.equals(ChangeAction.Delete);
// Create the prepared statements for way creation if necessary.
if (insertWayStatement == null) {
insertWayStatement = statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_WAY));
updateWayStatement = statementContainer.add(dbCtx.prepareStatement(UPDATE_SQL_WAY));
selectWayCountStatement = statementContainer.add(dbCtx.prepareStatement(SELECT_SQL_WAY_COUNT));
insertWayCurrentStatement = statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_WAY_CURRENT));
updateWayCurrentStatement = statementContainer.add(dbCtx.prepareStatement(UPDATE_SQL_WAY_CURRENT));
selectWayCurrentCountStatement = statementContainer.add(dbCtx.prepareStatement(SELECT_SQL_WAY_CURRENT_COUNT));
insertWayTagStatement = statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_WAY_TAG));
deleteWayTagStatement = statementContainer.add(dbCtx.prepareStatement(DELETE_SQL_WAY_TAG));
insertWayTagCurrentStatement = statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_WAY_TAG_CURRENT));
deleteWayTagCurrentStatement = statementContainer.add(dbCtx.prepareStatement(DELETE_SQL_WAY_TAG_CURRENT));
insertWayNodeStatement = statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_WAY_NODE));
deleteWayNodeStatement = statementContainer.add(dbCtx.prepareStatement(DELETE_SQL_WAY_NODE));
insertWayNodeCurrentStatement = statementContainer.add(dbCtx.prepareStatement(INSERT_SQL_WAY_NODE_CURRENT));
deleteWayNodeCurrentStatement = statementContainer.add(dbCtx.prepareStatement(DELETE_SQL_WAY_NODE_CURRENT));
}
// Remove the existing tags of the way history item.
try {
prmIndex = 1;
deleteWayTagStatement.setLong(prmIndex++, way.getId());
deleteWayTagStatement.setInt(prmIndex++, way.getVersion());
deleteWayTagStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to delete way history tags for way with id=" + way.getId() + ".", e);
}
// Remove the existing way nodes of the way history item.
try {
prmIndex = 1;
deleteWayNodeStatement.setLong(prmIndex++, way.getId());
deleteWayNodeStatement.setInt(prmIndex++, way.getVersion());
deleteWayNodeStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to delete way history nodes for way with id=" + way.getId() + ".", e);
}
// Update the way if it already exists in the history table, otherwise insert it.
try {
exists = checkIfEntityHistoryExists(selectWayCountStatement, way.getId(), way.getVersion());
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to check if current way with id=" + way.getId() + " exists.", e);
}
if (exists) {
// Update the way in the history table.
try {
prmIndex = 1;
updateWayStatement.setTimestamp(prmIndex++, new Timestamp(way.getTimestamp().getTime()));
updateWayStatement.setBoolean(prmIndex++, visible);
updateWayStatement.setLong(prmIndex++, way.getChangesetId());
updateWayStatement.setLong(prmIndex++, way.getId());
updateWayStatement.setInt(prmIndex++, way.getVersion());
updateWayStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to update history way with id=" + way.getId() + ".", e);
}
} else {
// Insert the new way into the history table.
try {
prmIndex = 1;
insertWayStatement.setLong(prmIndex++, way.getId());
insertWayStatement.setInt(prmIndex++, way.getVersion());
insertWayStatement.setTimestamp(prmIndex++, new Timestamp(way.getTimestamp().getTime()));
insertWayStatement.setBoolean(prmIndex++, visible);
insertWayStatement.setLong(prmIndex++, way.getChangesetId());
insertWayStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert history way with id=" + way.getId() + ".", e);
}
}
// Insert the tags of the new way into the history table.
for (Tag tag : way.getTags()) {
try {
prmIndex = 1;
insertWayTagStatement.setLong(prmIndex++, way.getId());
insertWayTagStatement.setInt(prmIndex++, way.getVersion());
insertWayTagStatement.setString(prmIndex++, tag.getKey());
insertWayTagStatement.setString(prmIndex++, tag.getValue());
insertWayTagStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert history way tag with id=" + way.getId() + " and key=(" + tag.getKey() + ").", e);
}
}
// Insert the nodes of the new way into the history table.
for (int i = 0; i < nodeReferenceList.size(); i++) {
WayNode nodeReference;
nodeReference = nodeReferenceList.get(i);
try {
prmIndex = 1;
insertWayNodeStatement.setLong(prmIndex++, way.getId());
insertWayNodeStatement.setInt(prmIndex++, way.getVersion());
insertWayNodeStatement.setLong(prmIndex++, nodeReference.getNodeId());
insertWayNodeStatement.setLong(prmIndex++, i + 1);
insertWayNodeStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert history way node with way id=" + way.getId() + " and node id=" + nodeReference.getNodeId() + ".", e);
}
}
if (populateCurrentTables) {
// Delete the existing way tags from the current table.
try {
deleteWayTagCurrentStatement.setLong(1, way.getId());
deleteWayTagCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to delete current way tags with id=" + way.getId() + ".", e);
}
// Delete the existing way nodes from the current table.
try {
deleteWayNodeCurrentStatement.setLong(1, way.getId());
deleteWayNodeCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to delete current way nodes with id=" + way.getId() + ".", e);
}
// Update the node if it already exists in the current table, otherwise insert it.
try {
exists = checkIfEntityExists(selectWayCurrentCountStatement, way.getId());
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to check if current way with id=" + way.getId() + " exists.", e);
}
if (exists) {
// Update the way in the current table.
try {
prmIndex = 1;
updateWayCurrentStatement.setInt(prmIndex++, way.getVersion());
updateWayCurrentStatement.setTimestamp(prmIndex++, new Timestamp(way.getTimestamp().getTime()));
updateWayCurrentStatement.setBoolean(prmIndex++, visible);
updateWayCurrentStatement.setLong(prmIndex++, way.getChangesetId());
updateWayCurrentStatement.setLong(prmIndex++, way.getId());
updateWayCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to update current way with id=" + way.getId() + ".", e);
}
} else {
// Insert the new way into the current table.
try {
prmIndex = 1;
insertWayCurrentStatement.setLong(prmIndex++, way.getId());
insertWayCurrentStatement.setInt(prmIndex++, way.getVersion());
insertWayCurrentStatement.setTimestamp(prmIndex++, new Timestamp(way.getTimestamp().getTime()));
insertWayCurrentStatement.setBoolean(prmIndex++, visible);
insertWayCurrentStatement.setLong(prmIndex++, way.getChangesetId());
insertWayCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert current way with id=" + way.getId() + ".", e);
}
}
// Insert the tags of the new way into the current table.
for (Tag tag : way.getTags()) {
try {
prmIndex = 1;
insertWayTagCurrentStatement.setLong(prmIndex++, way.getId());
insertWayTagCurrentStatement.setString(prmIndex++, tag.getKey());
insertWayTagCurrentStatement.setString(prmIndex++, tag.getValue());
insertWayTagCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert current way tag with id=" + way.getId() + " and key=(" + tag.getKey() + ").", e);
}
}
// Insert the nodes of the new way into the current table.
for (int i = 0; i < nodeReferenceList.size(); i++) {
WayNode nodeReference;
nodeReference = nodeReferenceList.get(i);
try {
prmIndex = 1;
insertWayNodeCurrentStatement.setLong(prmIndex++, way.getId());
insertWayNodeCurrentStatement.setLong(prmIndex++, nodeReference.getNodeId());
insertWayNodeCurrentStatement.setLong(prmIndex++, i);
insertWayNodeCurrentStatement.execute();
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to insert current way node with way id=" + way.getId() + " and node id=" + nodeReference.getNodeId() + ".", e);
}
}
}
}
use of org.mozilla.jss.asn1.Tag in project osmosis by openstreetmap.
the class ApidbWriter method populateEntityTagParameters.
/**
* Sets tag values as bind variable parameters to a tag insert query.
*
* @param statement The prepared statement to add the values to.
* @param initialIndex The offset index of the first variable to set.
* @param dbEntityTag The entity tag containing the data to be inserted.
*/
private void populateEntityTagParameters(PreparedStatement statement, int initialIndex, DbFeatureHistory<DbFeature<Tag>> dbEntityTag) {
int prmIndex;
Tag tag;
prmIndex = initialIndex;
tag = dbEntityTag.getFeature().getFeature();
try {
statement.setLong(prmIndex++, dbEntityTag.getFeature().getEntityId());
statement.setString(prmIndex++, tag.getKey());
statement.setString(prmIndex++, tag.getValue());
statement.setInt(prmIndex++, dbEntityTag.getVersion());
} catch (SQLException e) {
throw new OsmosisRuntimeException("Unable to set a prepared statement parameter for an entity tag.", e);
}
}
use of org.mozilla.jss.asn1.Tag in project osmosis by openstreetmap.
the class DatasetDriver method process.
/**
* {@inheritDoc}
*/
@Override
public void process(Dataset dataset) {
try (DatasetContext dsCtx = dataset.createReader()) {
EntityManager<Node> nodeManager = dsCtx.getNodeManager();
OsmUser user;
Node node;
// Create the user for edits to be performed under. This is an existing user with an
// updated name.
user = new OsmUser(10, "user10b");
// Modify node 1 to add a new tag.
node = nodeManager.getEntity(1).getWriteableInstance();
node.setUser(user);
node.getTags().add(new Tag("change", "new tag"));
nodeManager.modifyEntity(node);
// Delete node 6.
nodeManager.removeEntity(6);
// Add node 7 using the NONE user.
node = new Node(new CommonEntityData(7, 16, buildDate("2008-01-02 18:19:20"), OsmUser.NONE, 93), -11, -12);
node.getTags().addAll(Arrays.asList(new Tag[] { new Tag("created_by", "Me7"), new Tag("change", "new node") }));
nodeManager.addEntity(node);
dsCtx.complete();
}
}
use of org.mozilla.jss.asn1.Tag in project osmosis by openstreetmap.
the class CopyFilesetBuilder method buildTags.
private PGHStore buildTags(Entity entity) {
PGHStore tags;
tags = new PGHStore();
for (Tag tag : entity.getTags()) {
tags.put(tag.getKey(), tag.getValue());
}
return tags;
}
use of org.mozilla.jss.asn1.Tag in project osmosis by openstreetmap.
the class EntityMapper method populateCommonEntityParameters.
/**
* Sets common entity values as bind variable parameters to an entity insert query.
*
* @param args
* The bind variable arguments to be updated.
* @param entity
* The entity containing the data to be inserted.
*/
protected void populateCommonEntityParameters(Map<String, Object> args, Entity entity) {
Map<String, String> tags;
// We can't write an entity with a null timestamp.
if (entity.getTimestamp() == null) {
throw new OsmosisRuntimeException("Entity(" + entity.getType() + ") " + entity.getId() + " does not have a timestamp set.");
}
tags = new HashMap<String, String>(entity.getTags().size());
for (Tag tag : entity.getTags()) {
tags.put(tag.getKey(), tag.getValue());
}
args.put("id", entity.getId());
args.put("version", entity.getVersion());
args.put("userId", entity.getUser().getId());
args.put("timestamp", new Timestamp(entity.getTimestamp().getTime()));
args.put("changesetId", entity.getChangesetId());
args.put("tags", tags);
}
Aggregations