Search in sources :

Example 1 with NoSuchRecordException

use of org.openstreetmap.osmosis.pgsimple.common.NoSuchRecordException in project osmosis by openstreetmap.

the class ChangeWriter method writeUser.

/**
 * Writes the specified user to the database.
 *
 * @param user
 *            The user to write.
 */
private void writeUser(OsmUser user) {
    // Entities without a user assigned should not be written.
    if (!OsmUser.NONE.equals(user)) {
        // run.
        if (!userSet.contains(user.getId())) {
            int userId;
            OsmUser existingUser;
            userId = user.getId();
            try {
                existingUser = userDao.getUser(userId);
                if (!user.equals(existingUser)) {
                    userDao.updateUser(user);
                }
            } catch (NoSuchRecordException e) {
                userDao.addUser(user);
            }
            userSet.add(user.getId());
        }
    }
}
Also used : OsmUser(org.openstreetmap.osmosis.core.domain.v0_6.OsmUser) NoSuchRecordException(org.openstreetmap.osmosis.pgsimple.common.NoSuchRecordException)

Example 2 with NoSuchRecordException

use of org.openstreetmap.osmosis.pgsimple.common.NoSuchRecordException in project osmosis by openstreetmap.

the class PostgreSqlEntityManager method writeUser.

/**
 * Writes the specified user to the database.
 *
 * @param user
 *            The user to write.
 */
private void writeUser(OsmUser user) {
    // Entities without a user assigned should not be written.
    if (!OsmUser.NONE.equals(user)) {
        // run.
        if (!userSet.contains(user.getId())) {
            int userId;
            OsmUser existingUser;
            userId = user.getId();
            try {
                existingUser = userDao.getUser(userId);
                if (!user.equals(existingUser)) {
                    userDao.updateUser(user);
                }
            } catch (NoSuchRecordException e) {
                userDao.addUser(user);
            }
            userSet.add(user.getId());
        }
    }
}
Also used : OsmUser(org.openstreetmap.osmosis.core.domain.v0_6.OsmUser) NoSuchRecordException(org.openstreetmap.osmosis.pgsimple.common.NoSuchRecordException)

Example 3 with NoSuchRecordException

use of org.openstreetmap.osmosis.pgsimple.common.NoSuchRecordException in project osmosis by openstreetmap.

the class EntityDao method getEntity.

/**
 * Loads the specified entity from the database.
 *
 * @param entityId
 *            The unique identifier of the entity.
 * @return The loaded entity.
 */
public T getEntity(long entityId) {
    T entity;
    if (getStatement == null) {
        getStatement = prepareStatement(entityMapper.getSqlSelect(true, true));
    }
    try {
        getStatement.setLong(1, entityId);
        try (ResultSet resultSet = getStatement.executeQuery()) {
            if (!resultSet.next()) {
                throw new NoSuchRecordException(entityMapper.getEntityName() + " " + entityId + " doesn't exist.");
            }
            entity = entityMapper.parseRecord(resultSet);
        }
        for (DbFeature<Tag> dbTag : tagDao.getAll(entityId)) {
            entity.getTags().add(dbTag.getFeature());
        }
        // Add the type specific features.
        loadFeatures(entityId, entity);
        return entity;
    } catch (SQLException e) {
        throw new OsmosisRuntimeException("Query failed for " + entityMapper.getEntityName() + " " + entityId + ".", e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag) NoSuchRecordException(org.openstreetmap.osmosis.pgsimple.common.NoSuchRecordException) OsmosisRuntimeException(org.openstreetmap.osmosis.core.OsmosisRuntimeException)

Example 4 with NoSuchRecordException

use of org.openstreetmap.osmosis.pgsimple.common.NoSuchRecordException in project osmosis by openstreetmap.

the class UserDao method getUser.

/**
 * Loads the specified way from the database.
 *
 * @param userId
 *            The unique identifier of the user.
 * @return The loaded user.
 */
public OsmUser getUser(long userId) {
    ResultSet resultSet = null;
    OsmUser user;
    if (selectUserStatement == null) {
        selectUserStatement = prepareStatement(SELECT_USER);
    }
    try {
        selectUserStatement.setLong(1, userId);
        resultSet = selectUserStatement.executeQuery();
        if (!resultSet.next()) {
            throw new NoSuchRecordException("User " + userId + " doesn't exist.");
        }
        user = buildUser(resultSet);
        resultSet.close();
        resultSet = null;
        return user;
    } catch (SQLException e) {
        throw new OsmosisRuntimeException("Query failed for user " + userId + ".");
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                // We are already in an error condition so log and continue.
                LOG.log(Level.WARNING, "Unable to close the result set.", e);
            }
        }
    }
}
Also used : OsmUser(org.openstreetmap.osmosis.core.domain.v0_6.OsmUser) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) NoSuchRecordException(org.openstreetmap.osmosis.pgsimple.common.NoSuchRecordException) OsmosisRuntimeException(org.openstreetmap.osmosis.core.OsmosisRuntimeException)

Aggregations

NoSuchRecordException (org.openstreetmap.osmosis.pgsimple.common.NoSuchRecordException)4 OsmUser (org.openstreetmap.osmosis.core.domain.v0_6.OsmUser)3 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 OsmosisRuntimeException (org.openstreetmap.osmosis.core.OsmosisRuntimeException)2 Tag (org.openstreetmap.osmosis.core.domain.v0_6.Tag)1