Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.39 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2017 Hawaii Information Consortium
  3. * 201 Merchant St #1805, Honolulu, HI 96813
  4. * All rights reserved.
  5. *
  6. * This software is the confidential and proprietary information
  7. * of Hawaii Information Consortium. ("Confidential Information"). You
  8. * shall not disclose such Confidential Information and shall use
  9. * it only in accordance with the terms of the license agreement
  10. * you entered into with HIC.
  11. */
  12.  
  13. package gov.ehawaii.gohunt.lottery.model.hunt;
  14.  
  15. import gov.ehawaii.gohunt.lottery.model.hunt.session.HuntSession;
  16. import gov.ehawaii.gohunt.lottery.model.hunt.unitRule.HuntUnitRule;
  17. import gov.ehawaii.gohunt.lottery.model.huntName.HuntName;
  18. import gov.ehawaii.gohunt.lottery.model.lottery.HuntLottery;
  19. import gov.ehawaii.gohunt.lottery.model.permit.PermitApplication;
  20. import gov.ehawaii.gohunt.lottery.model.season.Season;
  21. import org.hibernate.annotations.SortNatural;
  22.  
  23. import javax.annotation.Nonnull;
  24. import javax.persistence.*;
  25. import java.io.Serializable;
  26. import java.time.Clock;
  27. import java.time.LocalDateTime;
  28. import java.util.Collection;
  29. import java.util.Comparator;
  30. import java.util.List;
  31. import java.util.Objects;
  32. import java.util.SortedSet;
  33. import java.util.TreeSet;
  34. import java.util.function.Supplier;
  35. import java.util.stream.Stream;
  36.  
  37. import static gov.ehawaii.gohunt.lottery.model.huntName.HuntName.aHuntName;
  38. import static java.util.Comparator.naturalOrder;
  39. import static java.util.stream.Collectors.toList;
  40.  
  41. @Entity
  42. @Table(name = "LOTT_HUNTS")
  43. @SuppressWarnings("unused")
  44. public class Hunt implements Comparable<Hunt>, Serializable {
  45.  
  46. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  47. @JoinColumn(name = "HUNT_NAME_ID")
  48. private HuntName huntName;
  49.  
  50. @Column(name = "ID")
  51. @Id
  52. @GeneratedValue
  53. private Long id;
  54.  
  55. @Column(name = "LANAI_PERMITS_AWARDED_ON")
  56. private LocalDateTime lanaiPermitsAwardedOn;
  57.  
  58. @OneToMany(mappedBy = "hunt")
  59. @SortNatural
  60. private SortedSet<HuntLottery> lotteries;
  61.  
  62. @Column(name = "PERMIT_APP_BEGINS")
  63. private LocalDateTime permitAppBegins;
  64.  
  65. @Column(name = "PERMIT_APP_ENDS")
  66. private LocalDateTime permitAppEnds;
  67.  
  68. @OneToMany(fetch = FetchType.LAZY, mappedBy = "hunt", orphanRemoval = true)
  69. @SortNatural
  70. private SortedSet<PermitApplication> permitApplications;
  71.  
  72. @Column(name = "PERMIT_REQUIRED", nullable = false)
  73. private boolean permitRequired;
  74.  
  75. @Column(name = "REGULAR_PERMITS_AWARDED_ON")
  76. private LocalDateTime regularPermitsAwardedOn;
  77.  
  78. @JoinColumn(name = "SEASON_ID", nullable = false)
  79. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  80. private Season season;
  81.  
  82. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "hunt", orphanRemoval = true)
  83. @SortNatural
  84. private SortedSet<HuntSession> sessions;
  85.  
  86. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "hunt", orphanRemoval = true)
  87. @SortNatural
  88. private SortedSet<HuntUnitRule> unitRules;
  89.  
  90. @Column(name = "UNITS_IDENTICAL", nullable = false)
  91. private boolean unitsIdentical;
  92.  
  93. /**
  94. * JPA dedicated constructor.
  95. */
  96. Hunt() {
  97. super();
  98. }
  99.  
  100. public static Assembler aHunt() {
  101. return new Assembler();
  102. }
  103.  
  104. @Override
  105. public int compareTo(final @Nonnull Hunt o) {
  106. return Comparator.comparing(Hunt::getHuntName).thenComparing(Hunt::getId, Comparator.nullsLast(naturalOrder())).compare(this, o);
  107. }
  108.  
  109. public HuntStatus computeStatusAt(final Clock clock) {
  110. final LocalDateTime now = LocalDateTime.now(clock);
  111. final LocalDateTime permitsStart = this.getPermitAppBegins();
  112. final LocalDateTime permitsEnd = this.getPermitAppEnds();
  113.  
  114. final LocalDateTime startDate = getHuntStartDate();
  115. final LocalDateTime endDate = getHuntEndDate();
  116.  
  117. // NOW < permits application starts
  118. if (Objects.nonNull(permitsStart) && now.isBefore(permitsStart)) {
  119. return HuntStatus.ANNOUNCED;
  120. }
  121.  
  122. // permit application starts < NOW < permit applications ends
  123. if (Objects.nonNull(permitsEnd) && now.isBefore(permitsEnd) && Objects.nonNull(permitsStart)) {
  124. return HuntStatus.APPLICATIONS_OPEN;
  125. }
  126.  
  127. // permit applications ends < NOW < session first date
  128. if ((Objects.nonNull(startDate) && now.isBefore(startDate)) && (Objects.nonNull(permitsEnd) && now.isAfter(permitsEnd))) {
  129. return HuntStatus.APPLICATIONS_CLOSED;
  130. }
  131.  
  132. // session first date < NOW < session last date
  133. if (Objects.nonNull(startDate) && (now.isEqual(startDate) || now.isAfter(startDate)) && Objects.nonNull(endDate) && now.isBefore(endDate)) {
  134. return HuntStatus.IN_PROGRESS;
  135. }
  136.  
  137. // session last date < NOW
  138. if (Objects.nonNull(endDate) && now.isAfter(endDate)) {
  139. return HuntStatus.ENDED;
  140. }
  141. // permit applications end < NOW and no session is added
  142. if (Objects.isNull(startDate) && Objects.nonNull(permitsEnd) && now.isAfter(permitsEnd)) {
  143. return HuntStatus.APPLICATIONS_CLOSED;
  144. }
  145.  
  146. return HuntStatus.ANNOUNCED;
  147. }
  148.  
  149. @Override
  150. public boolean equals(final Object o) {
  151. if (this == o) return true;
  152. if (o == null || getClass() != o.getClass()) return false;
  153. final Hunt hunt = (Hunt) o;
  154. return Objects.equals(huntName, hunt.huntName) &&
  155. Objects.equals(id, hunt.id);
  156. }
  157.  
  158. private LocalDateTime getHuntEndDate() {
  159. if (getSessions().size() > 0) {
  160. return sessions.last().getEndDateTime();
  161. }
  162. return null; // phase 2
  163. }
  164.  
  165. @Nonnull
  166. public HuntName getHuntName() {
  167. return huntName;
  168. }
  169.  
  170. public LocalDateTime getHuntStartDate() {
  171. if (getSessions().size() > 0) {
  172. return sessions.first().getStartDateTime();
  173. }
  174. return null;// phase 2
  175. }
  176.  
  177. public Long getId() {
  178. return id;
  179. }
  180.  
  181. public LocalDateTime getLanaiPermitsAwardedOn() {
  182. return lanaiPermitsAwardedOn;
  183. }
  184.  
  185. public SortedSet<HuntLottery> getLotteries() {
  186. return lotteries;
  187. }
  188.  
  189. @Nonnull
  190. public String getName() {
  191. return huntName.getName();
  192. }
  193.  
  194. public LocalDateTime getPermitAppBegins() {
  195. return permitAppBegins;
  196. }
  197.  
  198. public LocalDateTime getPermitAppEnds() {
  199. return permitAppEnds;
  200. }
  201.  
  202. /**
  203. * @return the permitApplications
  204. */
  205. public SortedSet<PermitApplication> getPermitApplications() {
  206. return permitApplications;
  207. }
  208.  
  209. public LocalDateTime getRegularPermitsAwardedOn() {
  210. return regularPermitsAwardedOn;
  211. }
  212.  
  213. @Nonnull
  214. public Season getSeason() {
  215. return season;
  216. }
  217.  
  218. public SortedSet<HuntSession> getSessions() {
  219. return Objects.isNull(sessions) ? new TreeSet<>() : sessions;
  220. }
  221.  
  222. public SortedSet<HuntUnitRule> getUnitRules() {
  223. return Objects.isNull(unitRules) ? new TreeSet<>() : unitRules;
  224. }
  225.  
  226. @Override
  227. public int hashCode() {
  228. return Objects.hash(huntName, id);
  229. }
  230.  
  231. @Nonnull
  232. public boolean isPermitRequired() {
  233. return permitRequired;
  234. }
  235.  
  236. @Nonnull
  237. public boolean isUnitsIdentical() {
  238. return unitsIdentical;
  239. }
  240.  
  241. @Override
  242. public String toString() {
  243. return String.format("'%s'%s", huntName, (null == id) ? "" : String.format(" [%d]", id));
  244. }
  245.  
  246. public void updateName(final HuntName huntName) {
  247. this.huntName = huntName;
  248. }
  249.  
  250. public void updatePermitInfo(final boolean permitRequired, final LocalDateTime permitAppBegins,
  251. final LocalDateTime permitAppEnds, final LocalDateTime regularPermitsAwardedOn,
  252. final LocalDateTime lanaiPermitsAwardedOn) {
  253. this.lanaiPermitsAwardedOn = lanaiPermitsAwardedOn;
  254. this.permitRequired = permitRequired;
  255. this.permitAppBegins = permitAppBegins;
  256. this.permitAppEnds = permitAppEnds;
  257. this.regularPermitsAwardedOn = regularPermitsAwardedOn;
  258. }
  259.  
  260. // TODO: refactor me into Builder
  261. public static final class Assembler implements Supplier<Hunt> {
  262.  
  263. private final Hunt hunt;
  264.  
  265. Assembler(final Hunt hunt) {
  266. super();
  267. this.hunt = hunt;
  268. }
  269.  
  270. Assembler() {
  271. super();
  272. this.hunt = new Hunt();
  273. }
  274.  
  275. public Assembler basedOn(final Hunt precursor) {
  276. return new Assembler(precursor);
  277. }
  278.  
  279. @Override
  280. public Hunt get() {
  281. return hunt;
  282. }
  283.  
  284. public Assembler havingId(final Long huntId) {
  285. this.hunt.id = huntId;
  286. return this;
  287. }
  288.  
  289. @SuppressWarnings("synthetic-access")
  290. public Hunt.Assembler named(final HuntName huntName) {
  291. this.hunt.huntName = huntName;
  292. return this;
  293. }
  294.  
  295. public Assembler named(final String huntName) {
  296. this.hunt.huntName = aHuntName().named(huntName)
  297. .get();
  298. return this;
  299. }
  300.  
  301. public Hunt.Assembler withLanaiPermitsAwardedOn(final LocalDateTime lanaiPermitsAwardedOn) {
  302. this.hunt.lanaiPermitsAwardedOn = lanaiPermitsAwardedOn;
  303. return this;
  304. }
  305.  
  306. public Hunt.Assembler withLotteries(final List<HuntLottery> huntLotteries) {
  307. if (this.hunt.lotteries == null) {
  308. this.hunt.lotteries = new TreeSet<>(huntLotteries);
  309. } else {
  310. this.hunt.lotteries.clear();
  311. if (huntLotteries != null) {
  312. this.hunt.lotteries.addAll(huntLotteries);
  313. }
  314. }
  315. return this;
  316. }
  317.  
  318. public Assembler withLotteries(final HuntLottery first, final HuntLottery... rest) {
  319. return withLotteries(Stream.concat(Stream.of(first), Stream.of(rest))
  320. .collect(toList()));
  321. }
  322.  
  323. public Assembler withPermitAppPeriod(final LocalDateTime start, final LocalDateTime end) {
  324. this.hunt.permitAppBegins = start;
  325. this.hunt.permitAppEnds = end;
  326. return this;
  327. }
  328.  
  329. public Hunt.Assembler withPermitRequired(final boolean permitRequired) {
  330. this.hunt.permitRequired = permitRequired;
  331. return this;
  332. }
  333.  
  334. public Hunt.Assembler withRegularPermitsAwardedOn(final LocalDateTime regularPermitsAwardedOn) {
  335. this.hunt.regularPermitsAwardedOn = regularPermitsAwardedOn;
  336. return this;
  337. }
  338.  
  339. public Hunt.Assembler withSeason(final Season season) {
  340. this.hunt.season = season;
  341. return this;
  342. }
  343.  
  344. public Hunt.Assembler withSessions(final Collection<HuntSession> huntSessions) {
  345. if (this.hunt.sessions == null) {
  346. this.hunt.sessions = new TreeSet<>(huntSessions);
  347. } else {
  348. this.hunt.sessions.clear();
  349. if (huntSessions != null) {
  350. this.hunt.sessions.addAll(huntSessions);
  351. }
  352. }
  353. return this;
  354. }
  355.  
  356. public Assembler withSessions(final HuntSession first, final HuntSession... rest) {
  357. return withSessions(Stream.concat(Stream.of(first), Stream.of(rest))
  358. .collect(toList()));
  359. }
  360.  
  361. public Hunt.Assembler withUnitRules(final List<HuntUnitRule> unitRules) {
  362. if (this.hunt.unitRules == null) {
  363. this.hunt.unitRules = new TreeSet<>(unitRules);
  364. } else {
  365. this.hunt.unitRules.clear();
  366. if (unitRules != null) {
  367. this.hunt.unitRules.addAll(unitRules);
  368. }
  369. }
  370. return this;
  371. }
  372.  
  373. public Hunt.Assembler withUnitRules(final HuntUnitRule first, final HuntUnitRule... rest) {
  374. return withUnitRules(Stream.concat(Stream.of(first), Stream.of(rest))
  375. .collect(toList()));
  376. }
  377.  
  378. public Hunt.Assembler withUnitsIdentical(final boolean unitsIdentical) {
  379. this.hunt.unitsIdentical = unitsIdentical;
  380. return this;
  381. }
  382. }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement