Search in sources :

Example 6 with CouchDbException

use of me.retrodaredevil.couchdbjava.exception.CouchDbException in project solarthing by wildmountainfarms.

the class CouchDbMillisDatabase method getPacketCollection.

@Override
public VersionedPacket<StoredPacketGroup> getPacketCollection(String documentId) throws SolarThingDatabaseException {
    final DocumentData documentData;
    try {
        documentData = database.getDocument(documentId);
    } catch (CouchDbException e) {
        throw ExceptionUtil.createFromCouchDbException("Could not get packet collection with document ID: " + documentId, e);
    }
    JsonData jsonData = documentData.getJsonData();
    return jsonDataToStoredPacketGroup(jsonData);
}
Also used : DocumentData(me.retrodaredevil.couchdbjava.response.DocumentData) CouchDbException(me.retrodaredevil.couchdbjava.exception.CouchDbException) JsonData(me.retrodaredevil.couchdbjava.json.JsonData) StringJsonData(me.retrodaredevil.couchdbjava.json.StringJsonData)

Example 7 with CouchDbException

use of me.retrodaredevil.couchdbjava.exception.CouchDbException in project solarthing by wildmountainfarms.

the class CouchDbAlterDatabase method queryAll.

@Override
@NotNull
public List<VersionedPacket<StoredAlterPacket>> queryAll(String sourceId) throws SolarThingDatabaseException {
    final ViewResponse allDocs;
    try {
        allDocs = database.allDocs(new ViewQueryParamsBuilder().includeDocs(true).build());
    } catch (CouchDbException e) {
        throw ExceptionUtil.createFromCouchDbException(e);
    }
    List<ViewResponse.DocumentEntry> rows = allDocs.getRows();
    List<VersionedPacket<StoredAlterPacket>> r = new ArrayList<>(rows.size());
    for (ViewResponse.DocumentEntry row : rows) {
        if (row.getId().startsWith("_")) {
            // ignore design documents
            continue;
        }
        // Since we're using _all_docs with include_docs=true, we have to use the doc, since the value is just the ID for _all_docs
        JsonData jsonData = row.getDoc();
        final JsonNode jsonNode;
        try {
            jsonNode = CouchDbJacksonUtil.getNodeFrom(jsonData);
        } catch (JsonProcessingException e) {
            throw new SolarThingDatabaseException("We couldn't parse some of the data into JSON. This should never happen", e);
        }
        if (!jsonNode.isObject()) {
            throw new SolarThingDatabaseException("Something must be wrong with _all_docs!");
        }
        ObjectNode objectNode = (ObjectNode) jsonNode;
        final StoredAlterPacket storedAlterPacket;
        try {
            storedAlterPacket = mapper.treeToValue(objectNode, StoredAlterPacket.class);
            ;
        } catch (JsonProcessingException e) {
            throw new SolarThingDatabaseException("Could not parse. JsonData: " + jsonData.getJson(), e);
        }
        // String documentId = objectNode.get("_id").asText();
        String documentRevision = objectNode.get("_rev").asText();
        VersionedPacket<StoredAlterPacket> versionedPacket = new VersionedPacket<>(storedAlterPacket, new RevisionUpdateToken(documentRevision));
        r.add(versionedPacket);
    }
    return r;
}
Also used : CouchDbException(me.retrodaredevil.couchdbjava.exception.CouchDbException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonData(me.retrodaredevil.couchdbjava.json.JsonData) StringJsonData(me.retrodaredevil.couchdbjava.json.StringJsonData) NotFoundSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException) UnauthorizedSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UnauthorizedSolarThingDatabaseException) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) ViewResponse(me.retrodaredevil.couchdbjava.response.ViewResponse) ViewQueryParamsBuilder(me.retrodaredevil.couchdbjava.request.ViewQueryParamsBuilder) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NotNull(me.retrodaredevil.solarthing.annotations.NotNull)

Example 8 with CouchDbException

use of me.retrodaredevil.couchdbjava.exception.CouchDbException in project solarthing by wildmountainfarms.

the class CouchDbAlterDatabase method delete.

@Override
public void delete(String documentId, UpdateToken updateToken) throws SolarThingDatabaseException {
    RevisionUpdateToken revisionUpdateToken = CouchDbSolarThingDatabase.checkUpdateToken(updateToken);
    String revision = revisionUpdateToken.getRevision();
    try {
        database.deleteDocument(documentId, revision);
    } catch (CouchDbUnauthorizedException e) {
        throw new UnauthorizedSolarThingDatabaseException(e);
    } catch (CouchDbUpdateConflictException e) {
        throw new UpdateConflictSolarThingDatabaseException("Update conflict on delete. Must not be latest revision. documentId: " + documentId + " revision: " + revision, e);
    } catch (CouchDbNotFoundException e) {
        throw new NotFoundSolarThingDatabaseException("(Not found) Could not delete documentId: " + documentId + " revision: " + revision, e);
    } catch (CouchDbException e) {
        throw new SolarThingDatabaseException("Could not delete documentId: " + documentId + " revision: " + revision, e);
    }
}
Also used : CouchDbUnauthorizedException(me.retrodaredevil.couchdbjava.exception.CouchDbUnauthorizedException) CouchDbException(me.retrodaredevil.couchdbjava.exception.CouchDbException) CouchDbNotFoundException(me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) UnauthorizedSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UnauthorizedSolarThingDatabaseException) NotFoundSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException) CouchDbUpdateConflictException(me.retrodaredevil.couchdbjava.exception.CouchDbUpdateConflictException) NotFoundSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException) UnauthorizedSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UnauthorizedSolarThingDatabaseException) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)

Aggregations

CouchDbException (me.retrodaredevil.couchdbjava.exception.CouchDbException)8 JsonData (me.retrodaredevil.couchdbjava.json.JsonData)6 StringJsonData (me.retrodaredevil.couchdbjava.json.StringJsonData)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)3 ArrayList (java.util.ArrayList)2 CouchDbNotFoundException (me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException)2 CouchDbUpdateConflictException (me.retrodaredevil.couchdbjava.exception.CouchDbUpdateConflictException)2 DocumentResponse (me.retrodaredevil.couchdbjava.response.DocumentResponse)2 ViewResponse (me.retrodaredevil.couchdbjava.response.ViewResponse)2 NotFoundSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException)2 UnauthorizedSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UnauthorizedSolarThingDatabaseException)2 UpdateConflictSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ArgumentValidationException (com.lexicalscope.jewel.cli.ArgumentValidationException)1 HelpRequestedException (com.lexicalscope.jewel.cli.HelpRequestedException)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 CouchDbCodeException (me.retrodaredevil.couchdbjava.exception.CouchDbCodeException)1