Setup UserDao
Setup basic method to access and update data in DB
This commit is contained in:
parent
ef981aee4b
commit
b617aadbba
Binary file not shown.
|
@ -5,7 +5,7 @@ android {
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.example.monnthic.monnthicmobile"
|
applicationId "com.example.monnthic.monnthicmobile"
|
||||||
multiDexEnabled true
|
multiDexEnabled true
|
||||||
minSdkVersion 26
|
minSdkVersion 19
|
||||||
targetSdkVersion 26
|
targetSdkVersion 26
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "1.0"
|
versionName "1.0"
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class DatabaseHelper {
|
||||||
private static final String DATABASE_NAME = "monnethic";
|
private static final String DATABASE_NAME = "monnethic";
|
||||||
private static final String DATABASE_USER = "";
|
private static final String DATABASE_USER = "";
|
||||||
private static final String DATABASE_PWD = "";
|
private static final String DATABASE_PWD = "";
|
||||||
private final static String DATABASE_URL = "jdbc:postgresql://37.187.101.44:5432/"+DATABASE_NAME;
|
private final static String DATABASE_URL = "jdbc:postgresql:///"+DATABASE_NAME;
|
||||||
|
|
||||||
|
|
||||||
public ConnectionSource setupDatabaseConnection(){
|
public ConnectionSource setupDatabaseConnection(){
|
||||||
|
|
|
@ -5,8 +5,8 @@ import com.j256.ormlite.table.DatabaseTable;
|
||||||
|
|
||||||
@DatabaseTable(tableName = "T_TEST")
|
@DatabaseTable(tableName = "T_TEST")
|
||||||
public class DatabaseTest {
|
public class DatabaseTest {
|
||||||
public static final String NAME_FIELD_NAME = "name";
|
private static final String NAME_FIELD_NAME = "name";
|
||||||
public static final String ID_FIELD_NAME = "id";
|
private static final String ID_FIELD_NAME = "id";
|
||||||
|
|
||||||
@DatabaseField(columnName = ID_FIELD_NAME, id = true)
|
@DatabaseField(columnName = ID_FIELD_NAME, id = true)
|
||||||
private int id;
|
private int id;
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
package monnethic.mobile.database;
|
|
||||||
|
|
||||||
public class DatabaseUserDao {
|
|
||||||
}
|
|
|
@ -1,5 +1,7 @@
|
||||||
package monnethic.mobile.user;
|
package monnethic.mobile.user;
|
||||||
|
|
||||||
|
import com.j256.ormlite.field.DatabaseField;
|
||||||
|
import com.j256.ormlite.table.DatabaseTable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,15 +9,112 @@ import java.util.Date;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
//Class User which represent an User in the postgresDB
|
||||||
|
@DatabaseTable(tableName = "T_USER")
|
||||||
public class User {
|
public class User {
|
||||||
private int userId;
|
@DatabaseField(canBeNull = false)
|
||||||
private String name;
|
private String name;
|
||||||
|
@DatabaseField(canBeNull = false)
|
||||||
private String firstname;
|
private String firstname;
|
||||||
|
@DatabaseField(id = true)
|
||||||
private String email;
|
private String email;
|
||||||
|
@DatabaseField(canBeNull = false)
|
||||||
private String password;
|
private String password;
|
||||||
private Date creationDate;
|
@DatabaseField(canBeNull = false)
|
||||||
private Date modificationDate;
|
private long creation_date;
|
||||||
|
@DatabaseField(canBeNull = false)
|
||||||
|
private long modification_date;
|
||||||
|
@DatabaseField(canBeNull = false)
|
||||||
private boolean verified;
|
private boolean verified;
|
||||||
|
@DatabaseField(canBeNull = false)
|
||||||
private boolean approved;
|
private boolean approved;
|
||||||
|
|
||||||
|
//Constructors
|
||||||
|
//Default constructor for ORMLite
|
||||||
|
public User() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(String name, String firstname, String email, String password) {
|
||||||
|
this.name = name;
|
||||||
|
this.firstname = firstname;
|
||||||
|
this.email = email;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(String name, String firstname, String email, String password, long creation_date, long modification_date, boolean verified, boolean approved) {
|
||||||
|
this.name = name;
|
||||||
|
this.firstname = firstname;
|
||||||
|
this.email = email;
|
||||||
|
this.password = password;
|
||||||
|
this.creation_date = creation_date;
|
||||||
|
this.modification_date = modification_date;
|
||||||
|
this.verified = verified;
|
||||||
|
this.approved = approved;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Getters and Setters
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstname() {
|
||||||
|
return firstname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstname(String firstname) {
|
||||||
|
this.firstname = firstname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCreation_date() {
|
||||||
|
return creation_date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreation_date(long creation_date) {
|
||||||
|
this.creation_date = creation_date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getModification_date() {
|
||||||
|
return modification_date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModification_date(long modification_date) {
|
||||||
|
this.modification_date = modification_date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVerified() {
|
||||||
|
return verified;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVerified(boolean verified) {
|
||||||
|
this.verified = verified;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isApproved() {
|
||||||
|
return approved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApproved(boolean approved) {
|
||||||
|
this.approved = approved;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
119
app/src/main/java/monnethic/mobile/user/UserDao.java
Normal file
119
app/src/main/java/monnethic/mobile/user/UserDao.java
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
package monnethic.mobile.user;
|
||||||
|
|
||||||
|
import com.j256.ormlite.dao.Dao;
|
||||||
|
import com.j256.ormlite.dao.DaoManager;
|
||||||
|
import com.j256.ormlite.stmt.PreparedQuery;
|
||||||
|
import com.j256.ormlite.stmt.QueryBuilder;
|
||||||
|
import com.j256.ormlite.stmt.UpdateBuilder;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import monnethic.mobile.database.DatabaseHelper;
|
||||||
|
|
||||||
|
//Class to communicate with database with ORMLite
|
||||||
|
public class UserDao {
|
||||||
|
private DatabaseHelper dbh = new DatabaseHelper();
|
||||||
|
private Dao<User, String> userDao;
|
||||||
|
|
||||||
|
public Dao createUserDaoConnection(){
|
||||||
|
try {
|
||||||
|
return DaoManager.createDao(dbh.setupDatabaseConnection(),User.class);
|
||||||
|
}catch (Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addUser(User user)throws Exception {
|
||||||
|
userDao = createUserDaoConnection();
|
||||||
|
userDao.create(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkApprovedUser(String email) throws Exception {
|
||||||
|
userDao = createUserDaoConnection();
|
||||||
|
QueryBuilder<User, String> queryBuilder = userDao.queryBuilder();
|
||||||
|
queryBuilder.where().eq("email",email);
|
||||||
|
PreparedQuery<User> preparedQuery = queryBuilder.prepare();
|
||||||
|
User user = userDao.queryForFirst(preparedQuery);
|
||||||
|
return user.isApproved();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkVerifiedUser(String email) throws Exception {
|
||||||
|
userDao = createUserDaoConnection();
|
||||||
|
QueryBuilder<User, String> queryBuilder = userDao.queryBuilder();
|
||||||
|
queryBuilder.where().eq("email",email);
|
||||||
|
PreparedQuery<User> preparedQuery = queryBuilder.prepare();
|
||||||
|
User user = userDao.queryForFirst(preparedQuery);
|
||||||
|
return user.isVerified();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean updateUserPassword(String email, String password) throws Exception {
|
||||||
|
userDao = createUserDaoConnection();
|
||||||
|
UpdateBuilder<User, String> updateBuilder = userDao.updateBuilder();
|
||||||
|
updateBuilder.updateColumnValue("password",password);
|
||||||
|
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||||
|
updateBuilder.updateColumnValue("modification_date",timestamp.getTime());
|
||||||
|
updateBuilder.where().eq("email",email);
|
||||||
|
updateBuilder.update();
|
||||||
|
|
||||||
|
if(checkUserPassword(email, password)){
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean verifyUserExist(String email) throws Exception {
|
||||||
|
userDao = createUserDaoConnection();
|
||||||
|
QueryBuilder<User, String> queryBuilder = userDao.queryBuilder();
|
||||||
|
queryBuilder.where().eq("email",email);
|
||||||
|
PreparedQuery<User> preparedQuery = queryBuilder.prepare();
|
||||||
|
User user = userDao.queryForFirst(preparedQuery);
|
||||||
|
|
||||||
|
if(user==null){
|
||||||
|
return false;
|
||||||
|
}else{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkUserPassword(String email, String password) throws Exception {
|
||||||
|
User user = getUser(email);
|
||||||
|
if(password.equals(user.getPassword())){
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getUser(String email) throws Exception {
|
||||||
|
if(!verifyUserExist(email)){
|
||||||
|
return null;
|
||||||
|
}else{
|
||||||
|
userDao = createUserDaoConnection();
|
||||||
|
QueryBuilder<User, String> queryBuilder = userDao.queryBuilder();
|
||||||
|
queryBuilder.where().eq("email",email);
|
||||||
|
PreparedQuery<User> preparedQuery = queryBuilder.prepare();
|
||||||
|
return userDao.queryForFirst(preparedQuery);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void approveUser (String email) throws Exception{
|
||||||
|
userDao = createUserDaoConnection();
|
||||||
|
UpdateBuilder<User, String> updateBuilder = userDao.updateBuilder();
|
||||||
|
updateBuilder.updateColumnValue("approved",true);
|
||||||
|
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||||
|
updateBuilder.updateColumnValue("modification_date",timestamp.getTime());
|
||||||
|
updateBuilder.where().eq("email",email);
|
||||||
|
updateBuilder.update();
|
||||||
|
}
|
||||||
|
public void verifyUser (String email) throws Exception{
|
||||||
|
userDao = createUserDaoConnection();
|
||||||
|
UpdateBuilder<User, String> updateBuilder = userDao.updateBuilder();
|
||||||
|
updateBuilder.updateColumnValue("verified",true);
|
||||||
|
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||||
|
updateBuilder.updateColumnValue("modification_date",timestamp.getTime());
|
||||||
|
updateBuilder.where().eq("email",email);
|
||||||
|
updateBuilder.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
151
app/src/test/java/monnethic/mobile/test/user/UserDaoTest.java
Normal file
151
app/src/test/java/monnethic/mobile/test/user/UserDaoTest.java
Normal file
|
@ -0,0 +1,151 @@
|
||||||
|
package monnethic.mobile.test.user;
|
||||||
|
|
||||||
|
import com.j256.ormlite.dao.Dao;
|
||||||
|
import com.j256.ormlite.table.TableUtils;
|
||||||
|
|
||||||
|
import org.junit.FixMethodOrder;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runners.MethodSorters;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import monnethic.mobile.user.User;
|
||||||
|
import monnethic.mobile.user.UserDao;
|
||||||
|
|
||||||
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
|
public class UserDaoTest {
|
||||||
|
private UserDao udao = new UserDao();
|
||||||
|
private final String USER_EMAIL = "thomas.marshal@gmail.com";
|
||||||
|
private final String USER_PASSWORD = "avcde";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestACreateTable(){
|
||||||
|
try{
|
||||||
|
Dao<User, String> userDao;
|
||||||
|
userDao = udao.createUserDaoConnection();
|
||||||
|
TableUtils.createTable(userDao);
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestBInsertUser(){
|
||||||
|
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||||
|
User user = new User("marshal","thomas",USER_EMAIL,USER_PASSWORD,timestamp.getTime(),timestamp.getTime(),false,false);
|
||||||
|
try{
|
||||||
|
udao.addUser(user);
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestCGetGeneratedUser(){
|
||||||
|
try{
|
||||||
|
User uTest = udao.getUser(USER_EMAIL);
|
||||||
|
if(uTest==null){
|
||||||
|
System.out.println("User don't Exist");
|
||||||
|
}else {
|
||||||
|
System.out.println(uTest.getEmail());
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestDCheckApprovedUser(){
|
||||||
|
try{
|
||||||
|
boolean response = udao.checkApprovedUser(USER_EMAIL);
|
||||||
|
if(response){
|
||||||
|
System.out.println("Approved");
|
||||||
|
}else{
|
||||||
|
System.out.println("Not Approved");
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestECheckVerifiedUser(){
|
||||||
|
try{
|
||||||
|
boolean response = udao.checkVerifiedUser(USER_EMAIL);
|
||||||
|
if(response){
|
||||||
|
System.out.println("Verified");
|
||||||
|
}else{
|
||||||
|
System.out.println("Not Verified");
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestFCheckUserPassword(){
|
||||||
|
try{
|
||||||
|
boolean response = udao.checkUserPassword(USER_EMAIL,USER_PASSWORD);
|
||||||
|
if(response){
|
||||||
|
System.out.println("Password OK");
|
||||||
|
}else{
|
||||||
|
System.out.println("Password Failed");
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestGUpdateUserPassword(){
|
||||||
|
String newPassword = "newPassword";
|
||||||
|
try{
|
||||||
|
boolean response = udao.updateUserPassword(USER_EMAIL,newPassword);
|
||||||
|
if(response){
|
||||||
|
System.out.println("Password Changed");
|
||||||
|
}else{
|
||||||
|
System.out.println("Password Update Failed");
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestHApproveVerifyUser(){
|
||||||
|
try{
|
||||||
|
udao.approveUser(USER_EMAIL);
|
||||||
|
udao.verifyUser(USER_EMAIL);
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestICheckApprovedUser(){
|
||||||
|
try{
|
||||||
|
boolean response = udao.checkApprovedUser(USER_EMAIL);
|
||||||
|
if(response){
|
||||||
|
System.out.println("Approved");
|
||||||
|
}else{
|
||||||
|
System.out.println("Not Approved");
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestJCheckVerifiedUser(){
|
||||||
|
try{
|
||||||
|
boolean response = udao.checkVerifiedUser(USER_EMAIL);
|
||||||
|
if(response){
|
||||||
|
System.out.println("Verified");
|
||||||
|
}else{
|
||||||
|
System.out.println("Not Verified");
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue