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

org.apache.james.jmap.model.VacationResponse Maven / Gradle / Ivy

/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/

package org.apache.james.jmap.model;

import java.time.ZonedDateTime;
import java.util.Objects;
import java.util.Optional;

import org.apache.james.jmap.api.vacation.Vacation;
import org.apache.james.jmap.api.vacation.VacationPatch;
import org.apache.james.jmap.json.OptionalZonedDateTimeDeserializer;
import org.apache.james.jmap.json.OptionalZonedDateTimeSerializer;
import org.apache.james.util.ValuePatch;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;

@JsonDeserialize(builder = VacationResponse.Builder.class)
public class VacationResponse {

    public static Builder builder() {
        return new Builder();
    }

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {
        private ValuePatch id = ValuePatch.keep();
        private ValuePatch isEnabled = ValuePatch.keep();
        private ValuePatch fromDate = ValuePatch.keep();
        private ValuePatch toDate = ValuePatch.keep();
        private ValuePatch subject = ValuePatch.keep();
        private ValuePatch textBody = ValuePatch.keep();
        private ValuePatch htmlBody = ValuePatch.keep();
        private Optional isActivated = Optional.empty();

        public Builder id(String id) {
            this.id = ValuePatch.modifyTo(id);
            return this;
        }

        @JsonProperty("isEnabled")
        public Builder enabled(boolean enabled) {
            isEnabled = ValuePatch.modifyTo(enabled);
            return this;
        }

        @JsonProperty("isActivated")
        public Builder activated(Optional activated) {
            Preconditions.checkNotNull(activated);
            this.isActivated = activated;
            return this;
        }

        @JsonIgnore
        public Builder activated(boolean activated) {
            return activated(Optional.of(activated));
        }

        @JsonDeserialize(using = OptionalZonedDateTimeDeserializer.class)
        public Builder fromDate(Optional fromDate) {
            this.fromDate = ValuePatch.ofOptional(fromDate);
            return this;
        }

        @JsonDeserialize(using = OptionalZonedDateTimeDeserializer.class)
        public Builder toDate(Optional toDate) {
            this.toDate = ValuePatch.ofOptional(toDate);
            return this;
        }

        public Builder textBody(Optional textBody) {
            this.textBody = ValuePatch.ofOptional(textBody);
            return this;
        }

        public Builder subject(Optional subject) {
            this.subject = ValuePatch.ofOptional(subject);
            return this;
        }

        public Builder htmlBody(Optional htmlBody) {
            this.htmlBody = ValuePatch.ofOptional(htmlBody);
            return this;
        }

        public Builder fromVacation(Vacation vacation) {
            this.id = ValuePatch.modifyTo(Vacation.ID);
            this.isEnabled = ValuePatch.modifyTo(vacation.isEnabled());
            this.fromDate = ValuePatch.ofOptional(vacation.getFromDate());
            this.toDate = ValuePatch.ofOptional(vacation.getToDate());
            this.textBody = ValuePatch.ofOptional(vacation.getTextBody());
            this.subject = ValuePatch.ofOptional(vacation.getSubject());
            this.htmlBody = ValuePatch.ofOptional(vacation.getHtmlBody());
            return this;
        }

        public VacationResponse build() {
            return new VacationResponse(id, isEnabled, fromDate, toDate, textBody, subject, htmlBody, isActivated);
        }
    }

    private final ValuePatch id;
    private final ValuePatch isEnabled;
    private final ValuePatch fromDate;
    private final ValuePatch toDate;
    private final ValuePatch subject;
    private final ValuePatch textBody;
    private final ValuePatch htmlBody;
    private final Optional isActivated;

    private VacationResponse(ValuePatch id, ValuePatch isEnabled, ValuePatch fromDate, ValuePatch toDate,
                             ValuePatch textBody, ValuePatch subject, ValuePatch htmlBody, Optional isActivated) {
        this.id = id;
        this.isEnabled = isEnabled;
        this.fromDate = fromDate;
        this.toDate = toDate;
        this.textBody = textBody;
        this.subject = subject;
        this.htmlBody = htmlBody;
        this.isActivated = isActivated;
    }

    public String getId() {
        return id.get();
    }

    @JsonProperty("isEnabled")
    public boolean isEnabled() {
        return isEnabled.get();
    }

    @JsonSerialize(using = OptionalZonedDateTimeSerializer.class)
    public Optional getFromDate() {
        return fromDate.toOptional();
    }

    @JsonSerialize(using = OptionalZonedDateTimeSerializer.class)
    public Optional getToDate() {
        return toDate.toOptional();
    }

    public Optional getTextBody() {
        return textBody.toOptional();
    }

    public Optional getSubject() {
        return subject.toOptional();
    }

    public Optional getHtmlBody() {
        return htmlBody.toOptional();
    }

    @JsonIgnore
    public boolean isValid() {
        return isMissingOrGoodValue() && !isEnabled.isRemoved();
    }

    @JsonIgnore
    private boolean isMissingOrGoodValue() {
        return id.isKept() || id.toOptional().equals(Optional.of(Vacation.ID));
    }

    @JsonIgnore
    public VacationPatch getPatch() {
        return VacationPatch.builder()
            .fromDate(fromDate)
            .toDate(toDate)
            .htmlBody(htmlBody)
            .textBody(textBody)
            .subject(subject)
            .isEnabled(isEnabled)
            .build();
    }

    @JsonProperty("isActivated")
    public Optional isActivated() {
        return isActivated;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        VacationResponse that = (VacationResponse) o;

        return Objects.equals(this.id, that.id)
            && Objects.equals(this.isEnabled, that.isEnabled)
            && Objects.equals(this.fromDate, that.fromDate)
            && Objects.equals(this.toDate, that.toDate)
            && Objects.equals(this.textBody, that.textBody)
            && Objects.equals(this.subject, that.subject)
            && Objects.equals(this.htmlBody, that.htmlBody)
            && Objects.equals(this.isActivated, that.isActivated);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, isEnabled, fromDate, toDate, textBody, subject, htmlBody, isActivated);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
            .add("id", id)
            .add("fromDate", fromDate)
            .add("toDate", toDate)
            .add("isActivated", isActivated)
            .toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy