Search in sources :

Example 1 with NotNull

use of me.retrodaredevil.solarthing.annotations.NotNull in project solarthing by wildmountainfarms.

the class SimplePacketGroupParser method parse.

@NotNull
public PacketGroup parse(ObjectNode objectNode) throws PacketParseException {
    JsonNode dateMillisNode = objectNode.get("dateMillis");
    if (dateMillisNode == null) {
        throw new PacketParseException("'dateMillis' does not exist for objectNode=" + objectNode);
    }
    if (!dateMillisNode.isNumber()) {
        throw new PacketParseException("'dateMillis' is not a number! dateMillisNode=" + dateMillisNode);
    }
    long dateMillis = dateMillisNode.asLong();
    JsonNode packetsNode = objectNode.get("packets");
    if (packetsNode == null) {
        throw new PacketParseException("'packets' does not exist for objectNode=" + objectNode);
    }
    if (!packetsNode.isArray()) {
        throw new PacketParseException("'packets' is not an array! packetsNode=" + packetsNode);
    }
    List<Packet> packetList = new ArrayList<>();
    for (JsonNode jsonPacket : packetsNode) {
        DocumentedPacket packet = null;
        try {
            packet = mapper.convertValue(jsonPacket, DocumentedPacket.class);
        } catch (IllegalArgumentException ex) {
            errorHandler.handleError(ex);
        }
        if (packet != null) {
            packetList.add(packet);
        }
    }
    return PacketGroups.createPacketGroup(packetList, dateMillis);
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) DocumentedPacket(me.retrodaredevil.solarthing.packets.DocumentedPacket) DocumentedPacket(me.retrodaredevil.solarthing.packets.DocumentedPacket) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) NotNull(me.retrodaredevil.solarthing.annotations.NotNull)

Example 2 with NotNull

use of me.retrodaredevil.solarthing.annotations.NotNull 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)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayList (java.util.ArrayList)2 NotNull (me.retrodaredevil.solarthing.annotations.NotNull)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 CouchDbException (me.retrodaredevil.couchdbjava.exception.CouchDbException)1 JsonData (me.retrodaredevil.couchdbjava.json.JsonData)1 StringJsonData (me.retrodaredevil.couchdbjava.json.StringJsonData)1 ViewQueryParamsBuilder (me.retrodaredevil.couchdbjava.request.ViewQueryParamsBuilder)1 ViewResponse (me.retrodaredevil.couchdbjava.response.ViewResponse)1 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)1 NotFoundSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException)1 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)1 UnauthorizedSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UnauthorizedSolarThingDatabaseException)1 UpdateConflictSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException)1 DocumentedPacket (me.retrodaredevil.solarthing.packets.DocumentedPacket)1 Packet (me.retrodaredevil.solarthing.packets.Packet)1 StoredAlterPacket (me.retrodaredevil.solarthing.type.alter.StoredAlterPacket)1