Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2017 Hawaii Information Consortium
- * 201 Merchant St #1805, Honolulu, HI 96813
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Hawaii Information Consortium. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with HIC.
- */
- package gov.ehawaii.gohunt.lottery.model.hunt;
- import gov.ehawaii.gohunt.lottery.model.hunt.session.HuntSession;
- import gov.ehawaii.gohunt.lottery.model.hunt.unitRule.HuntUnitRule;
- import gov.ehawaii.gohunt.lottery.model.huntName.HuntName;
- import gov.ehawaii.gohunt.lottery.model.lottery.HuntLottery;
- import gov.ehawaii.gohunt.lottery.model.permit.PermitApplication;
- import gov.ehawaii.gohunt.lottery.model.season.Season;
- import org.hibernate.annotations.SortNatural;
- import javax.annotation.Nonnull;
- import javax.persistence.*;
- import java.io.Serializable;
- import java.time.Clock;
- import java.time.LocalDateTime;
- import java.util.Collection;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Objects;
- import java.util.SortedSet;
- import java.util.TreeSet;
- import java.util.function.Supplier;
- import java.util.stream.Stream;
- import static gov.ehawaii.gohunt.lottery.model.huntName.HuntName.aHuntName;
- import static java.util.Comparator.naturalOrder;
- import static java.util.stream.Collectors.toList;
- @Entity
- @Table(name = "LOTT_HUNTS")
- @SuppressWarnings("unused")
- public class Hunt implements Comparable<Hunt>, Serializable {
- @ManyToOne(fetch = FetchType.EAGER, optional = false)
- @JoinColumn(name = "HUNT_NAME_ID")
- private HuntName huntName;
- @Column(name = "ID")
- @Id
- @GeneratedValue
- private Long id;
- @Column(name = "LANAI_PERMITS_AWARDED_ON")
- private LocalDateTime lanaiPermitsAwardedOn;
- @OneToMany(mappedBy = "hunt")
- @SortNatural
- private SortedSet<HuntLottery> lotteries;
- @Column(name = "PERMIT_APP_BEGINS")
- private LocalDateTime permitAppBegins;
- @Column(name = "PERMIT_APP_ENDS")
- private LocalDateTime permitAppEnds;
- @OneToMany(fetch = FetchType.LAZY, mappedBy = "hunt", orphanRemoval = true)
- @SortNatural
- private SortedSet<PermitApplication> permitApplications;
- @Column(name = "PERMIT_REQUIRED", nullable = false)
- private boolean permitRequired;
- @Column(name = "REGULAR_PERMITS_AWARDED_ON")
- private LocalDateTime regularPermitsAwardedOn;
- @JoinColumn(name = "SEASON_ID", nullable = false)
- @ManyToOne(fetch = FetchType.EAGER, optional = false)
- private Season season;
- @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "hunt", orphanRemoval = true)
- @SortNatural
- private SortedSet<HuntSession> sessions;
- @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "hunt", orphanRemoval = true)
- @SortNatural
- private SortedSet<HuntUnitRule> unitRules;
- @Column(name = "UNITS_IDENTICAL", nullable = false)
- private boolean unitsIdentical;
- /**
- * JPA dedicated constructor.
- */
- Hunt() {
- super();
- }
- public static Assembler aHunt() {
- return new Assembler();
- }
- @Override
- public int compareTo(final @Nonnull Hunt o) {
- return Comparator.comparing(Hunt::getHuntName).thenComparing(Hunt::getId, Comparator.nullsLast(naturalOrder())).compare(this, o);
- }
- public HuntStatus computeStatusAt(final Clock clock) {
- final LocalDateTime now = LocalDateTime.now(clock);
- final LocalDateTime permitsStart = this.getPermitAppBegins();
- final LocalDateTime permitsEnd = this.getPermitAppEnds();
- final LocalDateTime startDate = getHuntStartDate();
- final LocalDateTime endDate = getHuntEndDate();
- // NOW < permits application starts
- if (Objects.nonNull(permitsStart) && now.isBefore(permitsStart)) {
- return HuntStatus.ANNOUNCED;
- }
- // permit application starts < NOW < permit applications ends
- if (Objects.nonNull(permitsEnd) && now.isBefore(permitsEnd) && Objects.nonNull(permitsStart)) {
- return HuntStatus.APPLICATIONS_OPEN;
- }
- // permit applications ends < NOW < session first date
- if ((Objects.nonNull(startDate) && now.isBefore(startDate)) && (Objects.nonNull(permitsEnd) && now.isAfter(permitsEnd))) {
- return HuntStatus.APPLICATIONS_CLOSED;
- }
- // session first date < NOW < session last date
- if (Objects.nonNull(startDate) && (now.isEqual(startDate) || now.isAfter(startDate)) && Objects.nonNull(endDate) && now.isBefore(endDate)) {
- return HuntStatus.IN_PROGRESS;
- }
- // session last date < NOW
- if (Objects.nonNull(endDate) && now.isAfter(endDate)) {
- return HuntStatus.ENDED;
- }
- // permit applications end < NOW and no session is added
- if (Objects.isNull(startDate) && Objects.nonNull(permitsEnd) && now.isAfter(permitsEnd)) {
- return HuntStatus.APPLICATIONS_CLOSED;
- }
- return HuntStatus.ANNOUNCED;
- }
- @Override
- public boolean equals(final Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- final Hunt hunt = (Hunt) o;
- return Objects.equals(huntName, hunt.huntName) &&
- Objects.equals(id, hunt.id);
- }
- private LocalDateTime getHuntEndDate() {
- if (getSessions().size() > 0) {
- return sessions.last().getEndDateTime();
- }
- return null; // phase 2
- }
- @Nonnull
- public HuntName getHuntName() {
- return huntName;
- }
- public LocalDateTime getHuntStartDate() {
- if (getSessions().size() > 0) {
- return sessions.first().getStartDateTime();
- }
- return null;// phase 2
- }
- public Long getId() {
- return id;
- }
- public LocalDateTime getLanaiPermitsAwardedOn() {
- return lanaiPermitsAwardedOn;
- }
- public SortedSet<HuntLottery> getLotteries() {
- return lotteries;
- }
- @Nonnull
- public String getName() {
- return huntName.getName();
- }
- public LocalDateTime getPermitAppBegins() {
- return permitAppBegins;
- }
- public LocalDateTime getPermitAppEnds() {
- return permitAppEnds;
- }
- /**
- * @return the permitApplications
- */
- public SortedSet<PermitApplication> getPermitApplications() {
- return permitApplications;
- }
- public LocalDateTime getRegularPermitsAwardedOn() {
- return regularPermitsAwardedOn;
- }
- @Nonnull
- public Season getSeason() {
- return season;
- }
- public SortedSet<HuntSession> getSessions() {
- return Objects.isNull(sessions) ? new TreeSet<>() : sessions;
- }
- public SortedSet<HuntUnitRule> getUnitRules() {
- return Objects.isNull(unitRules) ? new TreeSet<>() : unitRules;
- }
- @Override
- public int hashCode() {
- return Objects.hash(huntName, id);
- }
- @Nonnull
- public boolean isPermitRequired() {
- return permitRequired;
- }
- @Nonnull
- public boolean isUnitsIdentical() {
- return unitsIdentical;
- }
- @Override
- public String toString() {
- return String.format("'%s'%s", huntName, (null == id) ? "" : String.format(" [%d]", id));
- }
- public void updateName(final HuntName huntName) {
- this.huntName = huntName;
- }
- public void updatePermitInfo(final boolean permitRequired, final LocalDateTime permitAppBegins,
- final LocalDateTime permitAppEnds, final LocalDateTime regularPermitsAwardedOn,
- final LocalDateTime lanaiPermitsAwardedOn) {
- this.lanaiPermitsAwardedOn = lanaiPermitsAwardedOn;
- this.permitRequired = permitRequired;
- this.permitAppBegins = permitAppBegins;
- this.permitAppEnds = permitAppEnds;
- this.regularPermitsAwardedOn = regularPermitsAwardedOn;
- }
- // TODO: refactor me into Builder
- public static final class Assembler implements Supplier<Hunt> {
- private final Hunt hunt;
- Assembler(final Hunt hunt) {
- super();
- this.hunt = hunt;
- }
- Assembler() {
- super();
- this.hunt = new Hunt();
- }
- public Assembler basedOn(final Hunt precursor) {
- return new Assembler(precursor);
- }
- @Override
- public Hunt get() {
- return hunt;
- }
- public Assembler havingId(final Long huntId) {
- this.hunt.id = huntId;
- return this;
- }
- @SuppressWarnings("synthetic-access")
- public Hunt.Assembler named(final HuntName huntName) {
- this.hunt.huntName = huntName;
- return this;
- }
- public Assembler named(final String huntName) {
- this.hunt.huntName = aHuntName().named(huntName)
- .get();
- return this;
- }
- public Hunt.Assembler withLanaiPermitsAwardedOn(final LocalDateTime lanaiPermitsAwardedOn) {
- this.hunt.lanaiPermitsAwardedOn = lanaiPermitsAwardedOn;
- return this;
- }
- public Hunt.Assembler withLotteries(final List<HuntLottery> huntLotteries) {
- if (this.hunt.lotteries == null) {
- this.hunt.lotteries = new TreeSet<>(huntLotteries);
- } else {
- this.hunt.lotteries.clear();
- if (huntLotteries != null) {
- this.hunt.lotteries.addAll(huntLotteries);
- }
- }
- return this;
- }
- public Assembler withLotteries(final HuntLottery first, final HuntLottery... rest) {
- return withLotteries(Stream.concat(Stream.of(first), Stream.of(rest))
- .collect(toList()));
- }
- public Assembler withPermitAppPeriod(final LocalDateTime start, final LocalDateTime end) {
- this.hunt.permitAppBegins = start;
- this.hunt.permitAppEnds = end;
- return this;
- }
- public Hunt.Assembler withPermitRequired(final boolean permitRequired) {
- this.hunt.permitRequired = permitRequired;
- return this;
- }
- public Hunt.Assembler withRegularPermitsAwardedOn(final LocalDateTime regularPermitsAwardedOn) {
- this.hunt.regularPermitsAwardedOn = regularPermitsAwardedOn;
- return this;
- }
- public Hunt.Assembler withSeason(final Season season) {
- this.hunt.season = season;
- return this;
- }
- public Hunt.Assembler withSessions(final Collection<HuntSession> huntSessions) {
- if (this.hunt.sessions == null) {
- this.hunt.sessions = new TreeSet<>(huntSessions);
- } else {
- this.hunt.sessions.clear();
- if (huntSessions != null) {
- this.hunt.sessions.addAll(huntSessions);
- }
- }
- return this;
- }
- public Assembler withSessions(final HuntSession first, final HuntSession... rest) {
- return withSessions(Stream.concat(Stream.of(first), Stream.of(rest))
- .collect(toList()));
- }
- public Hunt.Assembler withUnitRules(final List<HuntUnitRule> unitRules) {
- if (this.hunt.unitRules == null) {
- this.hunt.unitRules = new TreeSet<>(unitRules);
- } else {
- this.hunt.unitRules.clear();
- if (unitRules != null) {
- this.hunt.unitRules.addAll(unitRules);
- }
- }
- return this;
- }
- public Hunt.Assembler withUnitRules(final HuntUnitRule first, final HuntUnitRule... rest) {
- return withUnitRules(Stream.concat(Stream.of(first), Stream.of(rest))
- .collect(toList()));
- }
- public Hunt.Assembler withUnitsIdentical(final boolean unitsIdentical) {
- this.hunt.unitsIdentical = unitsIdentical;
- return this;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement