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

com.minlessika.membership.integration.RestContactPerson 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.NullAwareJsonObjectBuilder;
import com.minlessika.http.client.RestClient;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.UUID;
import javax.json.Json;
import javax.json.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Unchecked;

public final class RestContactPerson implements ContactPerson {

    private final Unchecked response;

    private final Organization organization;

    private final RestClient client;

    public RestContactPerson(final RestClient client, final JsonObject json, final Organization organization) {
        this.response = new Unchecked<>(
            new Sticky<>(
                () -> json
            )
        );
        this.client = client;
        this.organization = organization;
    }

    public RestContactPerson(final RestClient client, final Organization organization, final UUID uid) {
        this.response = new Unchecked<>(
            new Sticky<>(
                () -> client.get(
                    "/api/organization/contact/person",
                    ImmutableMap.of(
                        "org", organization.uid().toString(),
                        "id", uid.toString()
                    )
                ).asJsonObject()
            )
        );
        this.client = client;
        this.organization = organization;
    }

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

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

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

    @Override
    public Organization company() {
        return this.organization;
    }

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

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

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

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

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

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

    @Override
    public ContactNature nature() {
        return ContactNature.PERSON;
    }

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

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

    @Override
    public void update(
        String name, MonthDay birthDay, Integer birthYear, String birthPlace, Sex sex, String position, String civility, String photo,
        String addressLine1, String addressLine2, String phone1, String phone2, String whatsApp, String email, String city,
        String stateOrProvince, Double shoeSize, Double height
    ) {
        final JsonObject body = new NullAwareJsonObjectBuilder(Json.createObjectBuilder())
            .add("name", name)
            .add("birthDay", birthDay == null ? "" : birthDay.format(DateTimeFormatter.ofPattern("dd/MM")))
            .add("birthYear", Optional.ofNullable(birthYear).orElse(0))
            .add("birthPlace", birthPlace)
            .add("shoeSize", Optional.ofNullable(shoeSize).orElse(0.0))
            .add("height", Optional.ofNullable(height).orElse(0.0))
            .add("sex", sex.name())
            .add("position", position)
            .add("photo", photo)
            .add("email", email)
            .add("phone1", phone1)
            .add("phone2", phone2)
            .add("whatsApp", whatsApp)
            .add("addressLine1", addressLine1)
            .add("addressLine2", addressLine2)
            .add("city", city)
            .add("stateOrProvince", stateOrProvince)
            .add("civility", civility)
            .build();
        this.client.put(
            "/api/organization/contact/person",
            ImmutableMap.of(
                "org", this.organization.uid().toString(),
                "id", this.uid().toString()
            ),
            body
        );
    }

    @Override
    public MonthDay birthDay() {
        try {
            if (StringUtils.isBlank(this.response.value().getString("birthDay", ""))) {
                return null;
            } else {
                return MonthDay.parse(this.response.value().getString("birthDay"), DateTimeFormatter.ofPattern("dd/MM"));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer birthYear() {
        try {
            final int birthYear = this.response.value().getJsonNumber("birthYear").intValue();
            if (birthYear == 0) {
                return null;
            } else {
                return birthYear;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

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

    @Override
    public Sex sex() {
        try {
            return Sex.valueOf(this.response.value().getString("sex"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

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

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

    @Override
    public Double shoeSize() {
        try {
            final double size = this.response.value().getJsonNumber("shoeSize").doubleValue();
            if (size == 0) {
                return null;
            } else {
                return size;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Double height() {
        try {
            final double height = this.response.value().getJsonNumber("height").doubleValue();
            if (height == 0) {
                return null;
            } else {
                return height;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy