com.minlessika.membership.integration.RestMeasureUnits Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of membership-integration Show documentation
Show all versions of membership-integration Show documentation
It's a library to help developers to integration membership services to another project.
package com.minlessika.membership.integration;
import com.google.common.collect.ImmutableMap;
import com.minlessika.http.client.RestClient;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
public final class RestMeasureUnits implements MeasureUnits {
private final Organization organization;
private final RestClient client;
public RestMeasureUnits(final RestClient client, final Organization organization) {
this.organization = organization;
this.client = client;
}
@Override
public MeasureUnit getArticleUnit() {
return new RestMeasureUnit(
this.client.get(
"/api/organization/measure-unit-by-shortname",
ImmutableMap.of(
"org", this.organization.uid().toString(),
"shortname", "U"
)
).asJsonObject(),
this.organization,
this.client
);
}
@Override
public MeasureUnit add(final String shortName, final String fullName, final MeasureUnitType type) {
return this.add(UUID.randomUUID(), shortName, fullName, type);
}
@Override
public MeasureUnit add(final UUID id, final String shortName, final String fullName, final MeasureUnitType type) {
final JsonObject body = Json.createObjectBuilder()
.add("id", id.toString())
.add("shortname", shortName)
.add("fullName", fullName)
.add("type", type.id())
.build();
final UUID uid = UUID.fromString(
this.client.post(
"/api/organization/measure-unit",
ImmutableMap.of("org", this.organization.uid().toString()),
body
).get().asJsonObject().getString("guid")
);
return this.get(uid);
}
@Override
public MeasureUnit get(final UUID id) {
return new RestMeasureUnit(
this.client.get(
"/api/organization/measure-unit",
ImmutableMap.of(
"org", this.organization.uid().toString(),
"id", id.toString()
)
).asJsonObject(),
this.organization,
this.client
);
}
@Override
public boolean contains(final String filter) {
return this.client.get(
"/api/organization/measure-unit/contains",
ImmutableMap.of(
"org", this.organization.uid().toString(),
"filter", filter
)
).asJsonObject().getBoolean("value");
}
@Override
public Iterable iterate() {
final JsonArray json = this.client.get(
"/api/organization/measure-units",
ImmutableMap.of(
"org", this.organization.uid().toString()
)
).asJsonArray();
final List items = new LinkedList<>();
for (int pos = 0; pos < json.size(); pos++) {
items.add(
new RestMeasureUnit(
json.get(pos).asJsonObject(),
this.organization,
this.client
)
);
}
return items;
}
}