Skip to content

Commit

Permalink
Adding customer role
Browse files Browse the repository at this point in the history
  • Loading branch information
agoncal committed Aug 27, 2015
1 parent 2c2651b commit 38bc31e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
15 changes: 12 additions & 3 deletions src/main/forge/generate.fsh
Expand Up @@ -24,7 +24,7 @@ rest-setup --jaxrsVersion 2.0 ;

# Setup Arquillian
# ############
arquillian-setup --arquillianVersion 1.1.5.Final --testFramework junit --testFrameworkVersion 4.11 --containerAdapter wildfly-remote --containerAdapterVersion 8.1.0.Final ;
arquillian-setup --arquillianVersion 1.1.8.Final --testFramework junit --testFrameworkVersion 4.12 --containerAdapter wildfly-remote --containerAdapterVersion 8.2.0.Final ;



Expand All @@ -46,8 +46,15 @@ constraint-new-annotation --named Price ;
# TODO Command java-new-package-info to create a new package-info.java class [FORGE-2071]
# Package Vetoed
# ############
# java-new-package-info --targetPackage org.agoncal.application.petstore.model ;
# java-add-annotation --annotation javax.enterprise.inject.Vetoed --targetClass org.agoncal.application.petstore.model.package-info ;
java-new-package --named org.agoncal.application.petstore.model --createPackageInfo ;
# java-add-annotation --annotation javax.enterprie.inject.Vetoed --targetClass org.agoncal.application.petstore.model.package-info ;


# User role enumeration
# ############
java-new-enum --named UserRole --targetPackage ~.model ;
java-new-enum-const USER ;
java-new-enum-const ADMIN ;


# Country entity
Expand Down Expand Up @@ -101,6 +108,8 @@ jpa-new-field --named telephone ;
jpa-new-field --named email ;
jpa-new-field --named login --length 10 --not-nullable ;
jpa-new-field --named password --length 256 --not-nullable ;
jpa-new-field --named uuid --length 256 ;
jpa-new-field --named UserRole --type ~.model.UserRole --columnName user_role ;
jpa-new-field --named dateOfBirth --type java.util.Date --temporalType DATE --columnName date_of_birth ;
jpa-new-field --named age --type java.lang.Integer --transient ;
# Address embeddable
Expand Down
59 changes: 41 additions & 18 deletions src/main/java/org/agoncal/application/petstore/model/Customer.java
Expand Up @@ -16,7 +16,6 @@

import org.agoncal.application.petstore.constraints.Email;
import org.agoncal.application.petstore.constraints.Login;
import org.agoncal.application.petstore.exceptions.ValidationException;

import sun.misc.BASE64Encoder;

Expand All @@ -27,7 +26,9 @@
@Entity
@NamedQueries({
@NamedQuery(name = Customer.FIND_BY_LOGIN, query = "SELECT c FROM Customer c WHERE c.login = :login"),
@NamedQuery(name = Customer.FIND_BY_EMAIL, query = "SELECT c FROM Customer c WHERE c.email = :email"),
@NamedQuery(name = Customer.FIND_BY_LOGIN_PASSWORD, query = "SELECT c FROM Customer c WHERE c.login = :login AND c.password = :password"),
@NamedQuery(name = Customer.FIND_BY_UUID, query = "SELECT c FROM Customer c WHERE c.uuid = :uuid"),
@NamedQuery(name = Customer.FIND_ALL, query = "SELECT c FROM Customer c")
})
@XmlRootElement
Expand Down Expand Up @@ -72,6 +73,12 @@ public class Customer implements Serializable
@Size(min = 1, max = 256)
private String password;

@Column(length = 256)
@Size(min = 1, max = 256)
private String uuid;

private UserRole role;

@Column(name = "date_of_birth")
@Temporal(TemporalType.DATE)
@Past
Expand All @@ -91,6 +98,8 @@ public class Customer implements Serializable
public static final String FIND_BY_LOGIN = "Customer.findByLogin";
public static final String FIND_BY_LOGIN_PASSWORD = "Customer.findByLoginAndPassword";
public static final String FIND_ALL = "Customer.findAll";
public static final String FIND_BY_EMAIL = "Customer.findByEmail";
public static final String FIND_BY_UUID = "Customer.findByUUID";

// ======================================
// = Constructors =
Expand Down Expand Up @@ -142,6 +151,12 @@ public void calculateAge()
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + adjust;
}

@PrePersist
private void digestPassword()
{
password = digestPassword(password);
}

// ======================================
// = Business methods =
// ======================================
Expand All @@ -167,23 +182,6 @@ public String digestPassword(String plainTextPassword)
}
}

/**
* Given a password, this method then checks if it matches the user
*
* @param pwd Password
* @throws ValidationException thrown if the password is empty or different than the one store in database
*/
public void matchPassword(String pwd)
{
if (pwd == null || "".equals(pwd))
throw new ValidationException("Invalid password");
String digestedPwd = digestPassword(pwd);

// The password entered by the customer is not the same stored in database
if (!digestedPwd.equals(password))
throw new ValidationException("Passwords don't match");
}

// ======================================
// = Getters & setters =
// ======================================
Expand Down Expand Up @@ -218,6 +216,26 @@ public void setLogin(String login)
this.login = login;
}

public UserRole getRole()
{
return role;
}

public void setRole(UserRole role)
{
this.role = role;
}

public String getUuid()
{
return uuid;
}

public void setUuid(String uuid)
{
this.uuid = uuid;
}

public String getPassword()
{
return password;
Expand Down Expand Up @@ -248,6 +266,11 @@ public void setLastName(String lastName)
this.lastName = lastName;
}

public String getFullName()
{
return firstName + " " + lastName;
}

public String getTelephone()
{
return telephone;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/agoncal/application/petstore/model/UserRole.java
@@ -0,0 +1,13 @@
package org.agoncal.application.petstore.model;

/**
* @author Antonio Goncalves - http://www.antoniogoncalves.org --
*/
public enum UserRole
{
// ======================================
// = Attributes =
// ======================================

USER, ADMIN
}

0 comments on commit 38bc31e

Please sign in to comment.