Search in sources :

Example 1 with DocumentData

use of me.retrodaredevil.couchdbjava.response.DocumentData in project solarthing by wildmountainfarms.

the class CouchDbSetupMain method createUserIfNotExists.

private void createUserIfNotExists(String username, SolarThingDatabaseType.UserType userType) throws CouchDbException {
    CouchDbDatabase usersDatabase = instance.getUsersDatabase();
    String documentId = "org.couchdb.user:" + username;
    DocumentData userDocumentData = null;
    try {
        userDocumentData = usersDatabase.getDocument(documentId);
    } catch (CouchDbNotFoundException ignored) {
    }
    final UserEntry user;
    if (userDocumentData != null) {
        try {
            user = CouchDbJacksonUtil.readValue(MAPPER, userDocumentData.getJsonData(), UserEntry.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Could not parse user document data. Please report this bug", e);
        }
    } else {
        user = null;
    }
    if (user != null) {
        out.println("User: " + username + " exists! Continuing.");
    } else {
        out.println("Please enter a password for '" + username + "'.");
        String password = prompt.promptUserPassword(userType);
        final JsonData jsonData;
        try {
            jsonData = new StringJsonData(MAPPER.writeValueAsString(new UserEntry(username, password)));
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Couldn't serialize json! Report this!", e);
        }
        usersDatabase.createIfNotExists();
        usersDatabase.putDocument(documentId, jsonData);
    }
}
Also used : StringJsonData(me.retrodaredevil.couchdbjava.json.StringJsonData) DocumentData(me.retrodaredevil.couchdbjava.response.DocumentData) CouchDbNotFoundException(me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException) CouchDbDatabase(me.retrodaredevil.couchdbjava.CouchDbDatabase) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonData(me.retrodaredevil.couchdbjava.json.JsonData) StringJsonData(me.retrodaredevil.couchdbjava.json.StringJsonData)

Example 2 with DocumentData

use of me.retrodaredevil.couchdbjava.response.DocumentData 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 3 with DocumentData

use of me.retrodaredevil.couchdbjava.response.DocumentData in project solarthing by wildmountainfarms.

the class CouchDbSolarThingDatabase method queryMetadata.

@Override
@Nullable
public VersionedPacket<RootMetaPacket> queryMetadata(UpdateToken updateToken) throws SolarThingDatabaseException {
    DocumentData data = queryDocument(closedDatabase, RootMetaPacket.DOCUMENT_ID, updateToken);
    if (data == null) {
        return null;
    }
    JsonData jsonData = data.getJsonData();
    try {
        RootMetaPacket packet = CouchDbJacksonUtil.readValue(metaObjectMapper, jsonData, RootMetaPacket.class);
        return new VersionedPacket<>(packet, new RevisionUpdateToken(data.getRevision()));
    } catch (JsonProcessingException e) {
        throw new SolarThingDatabaseException("Invalid meta! Failed to parse!", e);
    }
}
Also used : DocumentData(me.retrodaredevil.couchdbjava.response.DocumentData) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) RootMetaPacket(me.retrodaredevil.solarthing.type.closed.meta.RootMetaPacket) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonData(me.retrodaredevil.couchdbjava.json.JsonData) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) Nullable(me.retrodaredevil.solarthing.annotations.Nullable)

Example 4 with DocumentData

use of me.retrodaredevil.couchdbjava.response.DocumentData in project solarthing by wildmountainfarms.

the class CouchDbSolarThingDatabase method queryAuthorized.

@Override
@Nullable
public VersionedPacket<AuthorizationPacket> queryAuthorized(UpdateToken updateToken) throws SolarThingDatabaseException {
    DocumentData data = queryDocument(closedDatabase, AuthorizationPacket.DOCUMENT_ID, updateToken);
    if (data == null) {
        return null;
    }
    JsonData jsonData = data.getJsonData();
    try {
        AuthorizationPacket packet = CouchDbJacksonUtil.readValue(simpleObjectMapper, jsonData, AuthorizationPacket.class);
        return new VersionedPacket<>(packet, new RevisionUpdateToken(data.getRevision()));
    } catch (JsonProcessingException e) {
        throw new SolarThingDatabaseException("Invalid authorization packet! Failed to parse!", e);
    }
}
Also used : DocumentData(me.retrodaredevil.couchdbjava.response.DocumentData) AuthorizationPacket(me.retrodaredevil.solarthing.type.closed.authorization.AuthorizationPacket) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonData(me.retrodaredevil.couchdbjava.json.JsonData) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) Nullable(me.retrodaredevil.solarthing.annotations.Nullable)

Aggregations

JsonData (me.retrodaredevil.couchdbjava.json.JsonData)4 DocumentData (me.retrodaredevil.couchdbjava.response.DocumentData)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 StringJsonData (me.retrodaredevil.couchdbjava.json.StringJsonData)2 Nullable (me.retrodaredevil.solarthing.annotations.Nullable)2 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)2 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)2 CouchDbDatabase (me.retrodaredevil.couchdbjava.CouchDbDatabase)1 CouchDbException (me.retrodaredevil.couchdbjava.exception.CouchDbException)1 CouchDbNotFoundException (me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException)1 AuthorizationPacket (me.retrodaredevil.solarthing.type.closed.authorization.AuthorizationPacket)1 RootMetaPacket (me.retrodaredevil.solarthing.type.closed.meta.RootMetaPacket)1