All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.minlessika.membership.integration.RestMeasureUnit Maven / Gradle / Ivy

Go to download

It's a library to help developers to integration membership services to another project.

There is a newer version: 0.3.1
Show newest version
package com.minlessika.membership.integration;

import com.google.common.collect.ImmutableMap;
import com.minlessika.http.client.RestClient;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import javax.json.Json;
import javax.json.JsonObject;

public final class RestMeasureUnit implements MeasureUnit {

    private final AtomicReference response;

    private final Organization organization;

    private final RestClient client;

    public RestMeasureUnit(final JsonObject json, final Organization organization, final RestClient client) {
        this.response = new AtomicReference<>(json);
        this.client = client;
        this.organization = organization;
    }

    @Override
    public UUID id() {
        return UUID.fromString(this.response.get().getString("guid"));
    }

    @Override
    public String shortname() {
        try {
            return this.response.get().getString("shortname");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String fullName() {
        try {
            return this.response.get().getString("fullName");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public MeasureUnitType type() {
        try {
            return MeasureUnitType.get(this.response.get().getInt("type"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(final String shortName, final String fullName, final MeasureUnitType type) {
        final JsonObject body = Json.createObjectBuilder()
            .add("shortname", shortName)
            .add("fullName", fullName)
            .add("type", type.id())
            .build();
        try {
            this.client.put(
                "/api/organization/measure-unit",
                ImmutableMap.of(
                    "org", organization.uid().toString(),
                    "id", this.id().toString()
                ),
                body
            );
            this.response.set(
                this.client.get(
                    "/api/organization/measure-unit",
                    ImmutableMap.of(
                        "org", this.organization.uid().toString(),
                        "id", this.id().toString()
                    )
                ).asJsonObject()
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy