/src/main/java/joch/armada/api/domain/Reservation.java

https://bitbucket.org/jochristo/dumpster-springboot-2.0 · Java · 154 lines · 113 code · 27 blank · 14 comment · 1 complexity · 5110f09a29c44b83f40c8c7828144436 MD5 · raw file

  1. package joch.armada.api.domain;
  2. import com.fasterxml.jackson.annotation.JsonIgnore;
  3. import com.fasterxml.jackson.annotation.JsonInclude;
  4. import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.Set;
  8. import javax.validation.ConstraintViolation;
  9. import javax.validation.Validation;
  10. import javax.validation.Validator;
  11. import javax.validation.ValidatorFactory;
  12. import javax.validation.constraints.NotNull;
  13. import joch.armada.api.Constants.EntityTypeName;
  14. import joch.armada.api.exception.ParameterConstraintViolationException;
  15. import org.joko.core.jsonapi.annotations.JAPIAttribute;
  16. import org.joko.core.jsonapi.annotations.JAPIIdentifier;
  17. import org.joko.core.jsonapi.annotations.JAPIRelationship;
  18. import org.joko.core.jsonapi.annotations.JAPIType;
  19. import org.joko.core.jsonapi.serializers.JsonDateFormatSerializer;
  20. import org.mongodb.morphia.annotations.Entity;
  21. import org.springframework.data.annotation.Id;
  22. import org.springframework.data.mongodb.core.mapping.DBRef;
  23. import org.springframework.data.mongodb.core.mapping.Document;
  24. import org.springframework.data.mongodb.core.mapping.Field;
  25. /**
  26. *
  27. * @author ic
  28. */
  29. @Document(collection = EntityTypeName.RESERVATIONS)
  30. @JAPIType(type = EntityTypeName.RESERVATIONS)
  31. @Entity(value = EntityTypeName.RESERVATIONS)
  32. @JsonInclude(JsonInclude.Include.NON_NULL)
  33. public class Reservation
  34. {
  35. @Id
  36. @Field
  37. @JAPIIdentifier
  38. @JsonIgnore
  39. private String id;
  40. @Field
  41. @JsonSerialize(using = JsonDateFormatSerializer.class)
  42. @JAPIAttribute
  43. private Date createdAt = new Date();
  44. @Field
  45. @JsonSerialize(using = JsonDateFormatSerializer.class)
  46. @JAPIAttribute
  47. private Date updatedAt;
  48. @Field
  49. @JAPIRelationship(relationship = Placement.class, selfLink = "")
  50. @DBRef(lazy = true, db = "db_dumpster_app")
  51. @NotNull(message = "placements relationships field is required")
  52. private List<Placement> placements;
  53. @Field
  54. @JAPIRelationship(relationship = Customer.class, selfLink = "")
  55. @DBRef(lazy = true, db = "db_dumpster_app")
  56. private Customer customer;
  57. @Field
  58. @JAPIAttribute
  59. private boolean deleted = false;
  60. /**
  61. * Updates current instance attributes with given object.
  62. * @param updated
  63. * @return
  64. */
  65. public Reservation updateInstance(Reservation updated)
  66. {
  67. this.setCustomer(updated.getCustomer());
  68. this.setPlacements(updated.getPlacements());
  69. this.updatedAt = new Date();
  70. // validate
  71. this.validate();
  72. return this;
  73. }
  74. public Reservation newInstance(){
  75. this.validate();
  76. return this;
  77. }
  78. /**
  79. * Bean validation against custom annotation rules
  80. */
  81. public void validate()
  82. {
  83. // bean validation
  84. ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
  85. Validator validator = vf.getValidator();
  86. Set<ConstraintViolation<Reservation>> violations = validator.validate(this);
  87. if (!violations.isEmpty()) {
  88. throw new ParameterConstraintViolationException(violations);
  89. }
  90. }
  91. public List<Placement> getPlacements() {
  92. return placements;
  93. }
  94. public void setPlacements(List<Placement> placements) {
  95. this.placements = placements;
  96. }
  97. public String getId() {
  98. return id;
  99. }
  100. public void setId(String id) {
  101. this.id = id;
  102. }
  103. public Customer getCustomer() {
  104. return customer;
  105. }
  106. public void setCustomer(Customer customer) {
  107. this.customer = customer;
  108. }
  109. public Date getCreatedAt() {
  110. return createdAt;
  111. }
  112. public void setCreatedAt(Date createdAt) {
  113. this.createdAt = createdAt;
  114. }
  115. public Date getUpdatedAt() {
  116. return updatedAt;
  117. }
  118. public void setUpdatedAt(Date updatedAt) {
  119. this.updatedAt = updatedAt;
  120. }
  121. public boolean isDeleted() {
  122. return deleted;
  123. }
  124. public void setDeleted(boolean deleted) {
  125. this.deleted = deleted;
  126. }
  127. }