Skip to content

Commit

Permalink
Merge pull request #10 from ivannov/master
Browse files Browse the repository at this point in the history
Format and refactor the code
  • Loading branch information
ivannov committed Jan 27, 2015
2 parents 496a587 + f513728 commit 87fe54d
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 200 deletions.
66 changes: 33 additions & 33 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>slbedu</groupId>
<artifactId>library</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>slbedu</groupId>
<artifactId>library</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<build>
<finalName>library</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<build>
<finalName>library</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.10.1.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencies>

</project>
2 changes: 1 addition & 1 deletion src/main/java/slbedu/library/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void borrowBook(String userName, String author, String title) {
}

private static void registerUser(String userName, String password,
String email, Date dateOfBirth) {
String email, Date dateOfBirth) {
boolean userExistng = userDAO.findUserByName(userName) != null;
if (userExistng) {
System.out.println("User already exists");
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/slbedu/library/dao/UserDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public void addUser(User user) {
}
}
}
public boolean isUserExistng(String userName, String password){

public boolean validateUserCredentials(String userName, String password) {
String txtQuery = "SELECT u FROM User u WHERE u.userName=:userName AND u.password=:password";
TypedQuery<User> query = em.createQuery(txtQuery, User.class);
query.setParameter("userName", userName);
query.setParameter("password", getHashedPassword(password));
return queryUser(query) != null;
}

public User findUserByName(String userName) {
String txtQuery = "SELECT u FROM User u WHERE u.userName = :userName";
TypedQuery<User> query = em.createQuery(txtQuery, User.class);
Expand All @@ -50,8 +50,8 @@ private User queryUser(TypedQuery<User> query) {
return null;
}
}
private String getHashedPassword(String password){

private String getHashedPassword(String password) {
try {
MessageDigest mda = MessageDigest.getInstance("SHA-512");
password = new String(mda.digest(password.getBytes()));
Expand Down
208 changes: 104 additions & 104 deletions src/main/java/slbedu/library/model/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,110 +11,110 @@

@Entity
@NamedQueries({
@NamedQuery(name = "findByAuthorAndTitle", query = "SELECT b FROM Book b WHERE b.title = :title AND b.author = :author"),
@NamedQuery(name = "getAllBooks", query = "SELECT b FROM Book b") })
@NamedQuery(name = "findByAuthorAndTitle", query = "SELECT b FROM Book b WHERE b.title = :title AND b.author = :author"),
@NamedQuery(name = "getAllBooks", query = "SELECT b FROM Book b")})
public class Book implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String title;

private String author;

private String isbn;

private int amount;

public Book() {
}

public Book(String title, String author, String isbn, int amount) {
super();
this.title = title;
this.author = author;
this.isbn = isbn;
this.amount = amount;
}

public Long getId() {
return this.id;
}

public void setId(final Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

@Override
public String toString() {
String result = getClass().getSimpleName() + " ";
if (title != null && !title.trim().isEmpty())
result += "title: " + title;
if (author != null && !author.trim().isEmpty())
result += ", author: " + author;
if (isbn != null && !isbn.trim().isEmpty())
result += ", isbn: " + isbn;
result += ", amount: " + amount;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Book)) {
return false;
}
Book other = (Book) obj;
if (id != null) {
if (!id.equals(other.id)) {
return false;
}
}
return true;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String title;

private String author;

private String isbn;

private int amount;

public Book() {
}

public Book(String title, String author, String isbn, int amount) {
super();
this.title = title;
this.author = author;
this.isbn = isbn;
this.amount = amount;
}

public Long getId() {
return this.id;
}

public void setId(final Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

@Override
public String toString() {
String result = getClass().getSimpleName() + " ";
if (title != null && !title.trim().isEmpty())
result += "title: " + title;
if (author != null && !author.trim().isEmpty())
result += ", author: " + author;
if (isbn != null && !isbn.trim().isEmpty())
result += ", isbn: " + isbn;
result += ", amount: " + amount;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Book)) {
return false;
}
Book other = (Book) obj;
if (id != null) {
if (!id.equals(other.id)) {
return false;
}
}
return true;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
}
Loading

0 comments on commit 87fe54d

Please sign in to comment.