Skip to content

Commit

Permalink
Use Builder pattern for building Person and Tithe Entities
Browse files Browse the repository at this point in the history
- Create Builder class
- Create Head of family and family members, when a new family is created
- Create a new Graphql schema for Person mutation input through family
- Removed @NotNull constraint in PersonMutationInput for family field
  • Loading branch information
getwithashish committed Sep 28, 2023
1 parent 2fd02a9 commit ef8b007
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 78 deletions.
13 changes: 11 additions & 2 deletions Tithe-Spring/src/main/java/com/tithe/entity/FamilyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package com.tithe.entity;


import java.util.List;

import jakarta.persistence.CascadeType;
Expand All @@ -13,6 +14,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -21,6 +23,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;


/**
* @author Ashish Sam T George
*
Expand All @@ -29,7 +32,8 @@
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "family_table", uniqueConstraints = @UniqueConstraint(columnNames = {"familyName", "phone"}))
@Table(name = "family_table", uniqueConstraints = @UniqueConstraint(columnNames = { "familyName",
"head_of_family_id", "phone" }))
public class FamilyEntity {

@Id
Expand All @@ -51,7 +55,12 @@ public class FamilyEntity {
@JoinColumn(name = "koottayma_id")
private KoottaymaEntity koottayma;

@OneToMany(mappedBy = "family")
@NotNull(message = "Head of Family does not exist")
@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "head_of_family_id")
private PersonEntity headOfFamily;

@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "family")
private List<PersonEntity> persons;

@OneToMany(mappedBy = "family")
Expand Down
107 changes: 107 additions & 0 deletions Tithe-Spring/src/main/java/com/tithe/entity/PersonBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
*
*/
package com.tithe.entity;


import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.tithe.model.GenderEnum;
import com.tithe.utils.ObjectValidation;


/**
* @author Ashish Sam T George
*
*/
public class PersonBuilder {

private Long personId;
private String baptismName;
private String personName;
private FamilyEntity family;
private RelationEntity relation;
private GenderEnum gender;
private LocalDate dob;
private String phone;
private List<TitheEntity> tithes;
private Boolean moved = false;
private List<EducationEntity> educations;
private List<OccupationEntity> occupations;
private Boolean active = true;

public PersonBuilder personId(Long personId) {
this.personId = personId;
return this;
}

public PersonBuilder baptismName(String baptismName) {
this.baptismName = baptismName;
return this;
}

public PersonBuilder personName(String personName) {
this.personName = personName;
return this;
}

public PersonBuilder family(FamilyEntity family) {
this.family = family;
return this;
}

public PersonBuilder relation(RelationEntity relation) {
this.relation = relation;
return this;
}

public PersonBuilder gender(GenderEnum gender) {
this.gender = gender;
return this;
}

public PersonBuilder dob(LocalDate dob) {
this.dob = dob;
return this;
}

public PersonBuilder phone(String phone) {
this.phone = phone;
return this;
}

// public PersonBuilder tithes(List<TitheEntity> tithes) {
// this.tithes = tithes;
// return this;
// }

public PersonBuilder moved(Boolean moved) {
this.moved = moved;
return this;
}

public PersonBuilder educations(List<EducationEntity> educations) {
this.educations = educations;
return this;
}

public PersonBuilder occupations(List<OccupationEntity> occupations) {
this.occupations = occupations;
return this;
}

public PersonBuilder active(Boolean active) {
this.active = active;
return this;
}

public PersonEntity build() {
PersonEntity person = new PersonEntity(personId, baptismName, personName, family, relation, gender, dob, phone,
tithes, moved, educations, occupations, active);
return person;
}

}
5 changes: 3 additions & 2 deletions Tithe-Spring/src/main/java/com/tithe/entity/PersonEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public class PersonEntity {
@NotBlank(message = "Name of Person is empty or null")
private String personName;

@NotNull(message = "Family does not exist")
@ManyToOne(cascade = CascadeType.PERSIST)
// @NotNull(message = "Family does not exist")
// @ManyToOne(cascade = CascadeType.PERSIST)
@ManyToOne
@JoinColumn(name = "family_id")
private FamilyEntity family;

Expand Down
98 changes: 98 additions & 0 deletions Tithe-Spring/src/main/java/com/tithe/entity/TitheBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
*
*/
package com.tithe.entity;


import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import com.tithe.model.TitheMutationInput;


/**
* @author Ashish Sam T George
*
*/
public class TitheBuilder {

private Long titheId;
private Double titheAmount;
private PersonEntity person;
private FamilyEntity family;
private KoottaymaEntity koottayma;
private ParishEntity parish;
private ForaneEntity forane;
private LocalDate timeStamp;

public TitheBuilder titheId(Long titheId) {
this.titheId = titheId;
return this;
}

public TitheBuilder titheAmount(Double titheAmount) {
this.titheAmount = titheAmount;
return this;
}

public TitheBuilder person(PersonEntity person) {
this.person = person;
return this;
}

public TitheBuilder family(FamilyEntity family) {
this.family = family;
return this;
}

public TitheBuilder koottayma(KoottaymaEntity koottayma) {
this.koottayma = koottayma;
return this;
}

public TitheBuilder parish(ParishEntity parish) {
this.parish = parish;
return this;
}

public TitheBuilder forane(ForaneEntity forane) {
this.forane = forane;
return this;
}

public TitheBuilder timeStamp(LocalDate timeStamp) {
this.timeStamp = timeStamp;
return this;
}

public TitheEntity build() {
TitheEntity tithe = new TitheEntity(titheId, titheAmount, person, family, koottayma, parish,
forane, timeStamp);
return tithe;
}

public List<TitheEntity> buildTithe(PersonEntity person,
List<TitheMutationInput> titheMutationInputs) {
List<TitheEntity> tithes = new ArrayList<>();
if (titheMutationInputs != null && titheMutationInputs.size() != 0) {
// objectValidation.validateObjects(titheMutationInputs);

for (TitheMutationInput titheMutationInput : titheMutationInputs) {
// New Builder object for each tithe
TitheBuilder titheBuilder = new TitheBuilder();
TitheEntity tithe = titheBuilder.titheAmount(titheMutationInput.getTitheAmount())
.person(person)
.family(person.getFamily())
.koottayma(person.getFamily().getKoottayma())
.parish(person.getFamily().getKoottayma().getParish())
.forane(person.getFamily().getKoottayma().getParish().getForane())
.timeStamp(titheMutationInput.getTimeStamp())
.build();
tithes.add(tithe);
}
}
return tithes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
package com.tithe.model;

import java.util.List;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
Expand All @@ -26,6 +28,11 @@ public class FamilyMutationInput {
@Positive(message = "Id of Koottayma should be valid")
@NotNull(message = "Id of Koottayma should not be empty or null")
Long koottaymaId;

@NotNull(message = "Head of Family should not be empty or null")
PersonMutationInput headOfFamily;

List<PersonMutationInput> persons;

Boolean active = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class PersonMutationInput {
@NotBlank(message = "Name of Person should not be empty or null")
String personName;

@Positive(message = "Id of Family should be valid")
@NotNull(message = "Id of Family should not be empty or null")
// @Positive(message = "Id of Family should be valid")
// @NotNull(message = "Id of Family should not be empty or null")
Long familyId;

@Positive(message = "Id of Relation should be valid")
Expand Down
Loading

0 comments on commit ef8b007

Please sign in to comment.