Skip to content

Commit

Permalink
Override equals and hashCode in all entities, remove projections, add @…
Browse files Browse the repository at this point in the history
…PreAuthorize to all methods in repositories, add ordering by id in findAll methods, add EntityGraphs to repositories, deal with NullPointerException in JWT service, add formatted messages in logger, fix tests
  • Loading branch information
Nazjara committed Feb 13, 2017
1 parent 37dd643 commit a4dc019
Show file tree
Hide file tree
Showing 41 changed files with 473 additions and 518 deletions.
2 changes: 1 addition & 1 deletion modules/core/src/main/java/io/smsc/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@PropertySource(value = "classpath:application.properties")
@PropertySource(value = "classpath:${smsc.database.dialect:hsqldb}.properties")
@ComponentScan("io.smsc")
class Application {
public class Application {

public static void main(String[] args) {
// Solution of JCE problem for JDK 9 (reflection is not more needed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ResponseEntity<JWTAuthenticationResponse> token(@RequestBody JWTAuthentic
return new ResponseEntity<>(token, HttpStatus.OK);
}
} catch (Exception ex) {
LOG.info("Some exception occurred", ex);
LOG.debug("Some exception occurred", ex);
// going to send error
}
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Credentials are invalid. Please enter valid username and password");
Expand Down Expand Up @@ -87,7 +87,7 @@ public ResponseEntity<JWTRefreshTokenResponse> token(@RequestBody JWTRefreshToke
return new ResponseEntity<>(token, HttpStatus.OK);
}
} catch (Exception ex) {
LOG.info("Some exception occurred", ex);
LOG.debug("Some exception occurred", ex);
// going to send error
}
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Refresh or expired access token is invalid. Please enter valid tokens");
Expand Down
26 changes: 26 additions & 0 deletions modules/core/src/main/java/io/smsc/model/CustomerUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,32 @@ public void setCustomer(Customer customer) {
this.customer = customer;
}

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

CustomerUser that = (CustomerUser) o;

if (!getId().equals(that.getId())) return false;
if (!getUsername().equals(that.getUsername())) return false;
if (!getFirstname().equals(that.getFirstname())) return false;
if (!getSurname().equals(that.getSurname())) return false;
if (!getEmail().equals(that.getEmail())) return false;
return getCreated().equals(that.getCreated());
}

@Override
public int hashCode() {
int result = getId().hashCode();
result = 31 * result + getUsername().hashCode();
result = 31 * result + getFirstname().hashCode();
result = 31 * result + getSurname().hashCode();
result = 31 * result + getEmail().hashCode();
result = 31 * result + getCreated().hashCode();
return result;
}

@Override
public String toString() {
return "CustomerUser{" +
Expand Down
56 changes: 42 additions & 14 deletions modules/core/src/main/java/io/smsc/model/Role.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.smsc.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.NotEmpty;

Expand Down Expand Up @@ -34,22 +33,33 @@ public class Role extends BaseEntity {
@Pattern(regexp = "[A-Z_]+", message = "{role.name.validation}")
private String name;

@ManyToMany(mappedBy = "roles")
@ManyToMany(cascade =
{
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.REFRESH,
CascadeType.PERSIST
},
targetEntity = User.class)
@JoinTable(
name = "USER_ROLE",
joinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID"),
inverseJoinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "ID")
)
@OrderBy("id asc")
@JsonBackReference()
private Set<User> users;

/**
* This method is used for removing all links on Role entity from
* appropriate User entities before entity is removed. Without
* it deleting entity can cause <code>ConstraintViolationException<code/>
*/
@PreRemove
private void removeRolesFromUsers() {
for (User user : users) {
user.getRoles().remove(this);
}
}
// /**
// * This method is used for removing all links on Role entity from
// * appropriate User entities before entity is removed. Without
// * it deleting entity can cause <code>ConstraintViolationException<code/>
// */
// @PreRemove
// private void removeRolesFromUsers() {
// for (User user : users) {
// user.getRoles().remove(this);
// }
// }

@JsonIgnore
public boolean isNew() {
Expand Down Expand Up @@ -80,6 +90,24 @@ public void setUsers(Set<User> users) {
this.users = users;
}

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

Role role = (Role) o;

if (!getId().equals(role.getId())) return false;
return getName().equals(role.getName());
}

@Override
public int hashCode() {
int result = getId().hashCode();
result = 31 * result + getName().hashCode();
return result;
}

@Override
public String toString() {
return "Role{" +
Expand Down
47 changes: 46 additions & 1 deletion modules/core/src/main/java/io/smsc/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ public class User extends BaseEntity {
@Column(name = "BLOCKED", nullable = false)
private Boolean blocked = false;

@ManyToMany(fetch = FetchType.EAGER)
@ManyToMany(cascade =
{
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.REFRESH,
CascadeType.PERSIST
},
targetEntity = Role.class)
@JoinTable(
name = "USER_ROLE",
joinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "ID"),
Expand All @@ -83,6 +90,18 @@ public class User extends BaseEntity {
@OrderBy("id asc")
private Set<Role> roles;

// /**
// * This method is used for removing all links on User entity from
// * appropriate Role entities before entity is removed. Without
// * it deleting entity can cause <code>ConstraintViolationException<code/>
// */
// @PreRemove
// private void removeRolesFromUsers() {
// for (Role role : roles) {
// role.getUsers().remove(this);
// }
// }

@OneToMany(
mappedBy = "user",
cascade = CascadeType.ALL,
Expand Down Expand Up @@ -198,6 +217,32 @@ public void setDashboards(Set<Dashboard> dashboards) {
this.dashboards = dashboards;
}

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

User user = (User) o;

if (!getId().equals(user.getId())) return false;
if (!getUsername().equals(user.getUsername())) return false;
if (!getFirstname().equals(user.getFirstname())) return false;
if (!getSurname().equals(user.getSurname())) return false;
if (!getEmail().equals(user.getEmail())) return false;
return getCreated().equals(user.getCreated());
}

@Override
public int hashCode() {
int result = getId().hashCode();
result = 31 * result + getUsername().hashCode();
result = 31 * result + getFirstname().hashCode();
result = 31 * result + getSurname().hashCode();
result = 31 * result + getEmail().hashCode();
result = 31 * result + getCreated().hashCode();
return result;
}

@Override
public String toString() {
return "User{" +
Expand Down
18 changes: 18 additions & 0 deletions modules/core/src/main/java/io/smsc/model/acl/AclClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public void setAclObjectIdentities(Set<AclObjectIdentity> aclObjectIdentities) {
this.aclObjectIdentities = aclObjectIdentities;
}

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

AclClass aclClass = (AclClass) o;

if (!getId().equals(aclClass.getId())) return false;
return getClassName().equals(aclClass.getClassName());
}

@Override
public int hashCode() {
int result = getId().hashCode();
result = 31 * result + getClassName().hashCode();
return result;
}

@Override
public String toString() {
return "AclClass{" +
Expand Down
26 changes: 26 additions & 0 deletions modules/core/src/main/java/io/smsc/model/acl/AclEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,32 @@ public void setAuditFailure(Boolean auditFailure) {
this.auditFailure = auditFailure;
}

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

AclEntry aclEntry = (AclEntry) o;

if (!getId().equals(aclEntry.getId())) return false;
if (!aceOrder.equals(aclEntry.aceOrder)) return false;
if (!getMask().equals(aclEntry.getMask())) return false;
if (!getGranting().equals(aclEntry.getGranting())) return false;
if (!getAuditSuccess().equals(aclEntry.getAuditSuccess())) return false;
return getAuditFailure().equals(aclEntry.getAuditFailure());
}

@Override
public int hashCode() {
int result = getId().hashCode();
result = 31 * result + aceOrder.hashCode();
result = 31 * result + getMask().hashCode();
result = 31 * result + getGranting().hashCode();
result = 31 * result + getAuditSuccess().hashCode();
result = 31 * result + getAuditFailure().hashCode();
return result;
}

@Override
public String toString() {
return "AclEntry{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ public void setAclEntries(Set<AclEntry> aclEntries) {
this.aclEntries = aclEntries;
}

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

AclObjectIdentity that = (AclObjectIdentity) o;

if (!getId().equals(that.getId())) return false;
if (!getObjectIdIdentity().equals(that.getObjectIdIdentity())) return false;
return getEntriesInheriting().equals(that.getEntriesInheriting());
}

@Override
public int hashCode() {
int result = getId().hashCode();
result = 31 * result + getObjectIdIdentity().hashCode();
result = 31 * result + getEntriesInheriting().hashCode();
return result;
}

@Override
public String toString() {
return "AclObjectIdentity{" +
Expand Down
20 changes: 20 additions & 0 deletions modules/core/src/main/java/io/smsc/model/acl/AclSid.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ public void setAclObjectIdentities(Set<AclObjectIdentity> aclObjectIdentities) {
this.aclObjectIdentities = aclObjectIdentities;
}

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

AclSid aclSid = (AclSid) o;

if (!getId().equals(aclSid.getId())) return false;
if (!getPrincipal().equals(aclSid.getPrincipal())) return false;
return getSid().equals(aclSid.getSid());
}

@Override
public int hashCode() {
int result = getId().hashCode();
result = 31 * result + getPrincipal().hashCode();
result = 31 * result + getSid().hashCode();
return result;
}

@Override
public String toString() {
return "AclSid{" +
Expand Down
30 changes: 30 additions & 0 deletions modules/core/src/main/java/io/smsc/model/customer/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,36 @@ public void setCustomerUsers(Set<CustomerUser> customerUsers) {
this.customerUsers = customerUsers;
}

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

Customer customer = (Customer) o;

if (!getId().equals(customer.getId())) return false;
if (!getCompanyName().equals(customer.getCompanyName())) return false;
if (!getStreet().equals(customer.getStreet())) return false;
if (!getStreet2().equals(customer.getStreet2())) return false;
if (!getPostcode().equals(customer.getPostcode())) return false;
if (!getCountry().equals(customer.getCountry())) return false;
if (!getCity().equals(customer.getCity())) return false;
return getVatid() != null ? getVatid().equals(customer.getVatid()) : customer.getVatid() == null;
}

@Override
public int hashCode() {
int result = getId().hashCode();
result = 31 * result + getCompanyName().hashCode();
result = 31 * result + getStreet().hashCode();
result = 31 * result + getStreet2().hashCode();
result = 31 * result + getPostcode().hashCode();
result = 31 * result + getCountry().hashCode();
result = 31 * result + getCity().hashCode();
result = 31 * result + (getVatid() != null ? getVatid().hashCode() : 0);
return result;
}

@Override
public String toString() {
return "Customer{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,38 @@ public void setSalutation(Salutation salutation) {
this.salutation = salutation;
}

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

CustomerContact contact = (CustomerContact) o;

if (!getId().equals(contact.getId())) return false;
if (!getFirstname().equals(contact.getFirstname())) return false;
if (!getSurname().equals(contact.getSurname())) return false;
if (!getPhone().equals(contact.getPhone())) return false;
if (!getMobilePhone().equals(contact.getMobilePhone())) return false;
if (!getFax().equals(contact.getFax())) return false;
if (!getEmailAddress().equals(contact.getEmailAddress())) return false;
if (getType() != contact.getType()) return false;
return getSalutation() == contact.getSalutation();
}

@Override
public int hashCode() {
int result = getId().hashCode();
result = 31 * result + getFirstname().hashCode();
result = 31 * result + getSurname().hashCode();
result = 31 * result + getPhone().hashCode();
result = 31 * result + getMobilePhone().hashCode();
result = 31 * result + getFax().hashCode();
result = 31 * result + getEmailAddress().hashCode();
result = 31 * result + getType().hashCode();
result = 31 * result + getSalutation().hashCode();
return result;
}

@Override
public String toString() {
return "CustomerContact{" +
Expand Down

0 comments on commit a4dc019

Please sign in to comment.