From 701e1461bc3857ab5ac3254c7f0f058b8485c368 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 4 Oct 2016 14:43:54 +0200 Subject: [PATCH] Renaming packages: making clear I am using MVC patterns. JUST IN CASE THEY DO NOT REALIZE!!! --- .../com/prueba/controllers/rest/ApiController.java | 2 +- src/main/java/com/prueba/model/dao/AccountDao.java | 121 +++++++++++++++++++++ .../prueba/model/dao/ApplicationResourceDao.java | 59 ++++++++++ .../com/prueba/model/domain/AccountResource.java | 46 ++++++++ .../model/domain/ApplicationResourceResource.java | 25 +++++ .../com/prueba/persistence/dao/AccountDao.java | 121 --------------------- .../persistence/dao/ApplicationResourceDao.java | 59 ---------- .../prueba/persistence/domain/AccountResource.java | 46 -------- .../domain/ApplicationResourceResource.java | 25 ----- .../com/prueba/services/impl/ApiServiceImpl.java | 4 +- .../services/impl/AuthorizationServicesImpl.java | 4 +- .../com/prueba/services/impl/LoginServiceImpl.java | 2 +- .../persistence/dao/AccountDaoIntegrationTest.java | 3 +- 13 files changed, 259 insertions(+), 258 deletions(-) create mode 100644 src/main/java/com/prueba/model/dao/AccountDao.java create mode 100644 src/main/java/com/prueba/model/dao/ApplicationResourceDao.java create mode 100644 src/main/java/com/prueba/model/domain/AccountResource.java create mode 100644 src/main/java/com/prueba/model/domain/ApplicationResourceResource.java delete mode 100644 src/main/java/com/prueba/persistence/dao/AccountDao.java delete mode 100644 src/main/java/com/prueba/persistence/dao/ApplicationResourceDao.java delete mode 100644 src/main/java/com/prueba/persistence/domain/AccountResource.java delete mode 100644 src/main/java/com/prueba/persistence/domain/ApplicationResourceResource.java diff --git a/src/main/java/com/prueba/controllers/rest/ApiController.java b/src/main/java/com/prueba/controllers/rest/ApiController.java index 5f7b4e6..3eeb5ad 100644 --- a/src/main/java/com/prueba/controllers/rest/ApiController.java +++ b/src/main/java/com/prueba/controllers/rest/ApiController.java @@ -13,7 +13,7 @@ import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.ObjectMapper; import com.prueba.core.context.util.AntPathMatcher; import com.prueba.core.web.controller.Controller; -import com.prueba.persistence.domain.AccountResource; +import com.prueba.model.domain.AccountResource; import com.prueba.services.impl.ApiServiceImpl; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; diff --git a/src/main/java/com/prueba/model/dao/AccountDao.java b/src/main/java/com/prueba/model/dao/AccountDao.java new file mode 100644 index 0000000..3308d85 --- /dev/null +++ b/src/main/java/com/prueba/model/dao/AccountDao.java @@ -0,0 +1,121 @@ +package com.prueba.model.dao; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.sql.DataSource; + +import com.prueba.core.context.integration.database.DataBaseAccess; +import com.prueba.core.context.integration.database.impl.DataBaseAccessImpl; +import com.prueba.core.context.web.application.ApplicationWebContext; +import com.prueba.model.domain.AccountResource; + +public class AccountDao { + private static final String CODE = "CODE"; + private static final String NAME = "NAME"; + private static final String SURNAME = "SURNAME"; + private static final String PASSWORD = "PASSWORD"; + private static final String APP_ROLE_CODE = "APPLICATION_ROLE_CODE"; + + public AccountResource findByCode(String accountCode) { + final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); + final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl(dataSource); + + final List> results = + dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ?", + answer -> + { + final List> result = new ArrayList<>(); + while (answer.next()) { + final Map row = new HashMap<>(); + row.put(CODE, answer.getString(CODE)); + row.put(NAME, answer.getString(NAME)); + row.put(SURNAME, answer.getString(SURNAME)); + row.put(PASSWORD, answer.getString(PASSWORD)); + row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE)); + result.add(row); + } + + return result; + }, + preparedStatement -> { + preparedStatement.setString(1, accountCode); + }); + + AccountResource account = null; + if (!results.isEmpty()) { + final Map row = results.get(0); + + account = new AccountResource(row.get(CODE), row.get(NAME), + row.get(SURNAME), null, row.get(APP_ROLE_CODE)); + } + + return account; + } + + public void create(AccountResource account) { + final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); + final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); + + dataBaseAccess.executeUpdate( + "INSERT INTO ACCOUNT VALUES (?, ?, ?, ?, ?)", + preparedStatement -> { + preparedStatement.setString(1, account.getCode()); + preparedStatement.setString(2, account.getName()); + preparedStatement.setString(3, account.getSurname()); + preparedStatement.setString(4, account.getPassword()); + preparedStatement.setString(5, account.getRole()); + }); + + } + + public void deleteByCode(String code) { + final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); + final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); + + dataBaseAccess.executeUpdate("DELETE FROM ACCOUNT WHERE CODE = ?", + preparedStatement -> { + preparedStatement.setString(1, code); + }); + + } + + public AccountResource findByCodeAndPassword(String username, String password) { + final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); + final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); + + final List> results = + dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ? AND PASSWORD = ?", + answer -> + { + final List> result = new ArrayList<>(); + while (answer.next()) { + final Map row = new HashMap<>(); + row.put(CODE, answer.getString(CODE)); + row.put(NAME, answer.getString(NAME)); + row.put(SURNAME, answer.getString(SURNAME)); + row.put(PASSWORD, answer.getString(PASSWORD)); + row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE)); + result.add(row); + } + + return result; + }, + preparedStatement -> { + preparedStatement.setString(1, username); + preparedStatement.setString(2, password); + }); + + AccountResource account = null; + if (!results.isEmpty()) { + final Map row = results.get(0); + + account = new AccountResource(row.get(CODE), row.get(NAME), + row.get(SURNAME), null, row.get(APP_ROLE_CODE)); + } + + return account; + } +} diff --git a/src/main/java/com/prueba/model/dao/ApplicationResourceDao.java b/src/main/java/com/prueba/model/dao/ApplicationResourceDao.java new file mode 100644 index 0000000..5bef49b --- /dev/null +++ b/src/main/java/com/prueba/model/dao/ApplicationResourceDao.java @@ -0,0 +1,59 @@ +package com.prueba.model.dao; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.sql.DataSource; + +import com.prueba.core.context.integration.database.impl.DataBaseAccessImpl; +import com.prueba.core.context.web.application.ApplicationWebContext; +import com.prueba.model.domain.ApplicationResourceResource; + +public class ApplicationResourceDao { + private static final String URL_PATTERN = "URL_PATTERN"; + private static final String HTTP_METHOD = "HTTP_METHOD"; + + public List findURLsByUserName(String userName) { + final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); + final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); + + final List> results = dataBaseAccess.executeQuery("" + + "SELECT APP_RES.URL_PATTERN, APP_RES.HTTP_METHOD FROM APPLICATION_ROLE APP_ROLE " + + "INNER JOIN APPLICATION_RESOURCE_APPLICATION_ROLE APP_RES_APP_ROLE ON APP_ROLE.CODE = APP_RES_APP_ROLE.APPLICATION_ROLE_CODE " + + "INNER JOIN APPLICATION_RESOURCE APP_RES ON APP_RES.URL_PATTERN = APP_RES_APP_ROLE.APPLICATION_RESOURCE_URL_PATTERN " + + "INNER JOIN ACCOUNT ACC ON ACC.APPLICATION_ROLE_CODE = APP_ROLE.CODE " + + "WHERE ACC.CODE = ? ", + answer -> + { + final List> result = new ArrayList<>(); + while (answer.next()) { + final Map row = new HashMap<>(); + String urlPatternValue = answer.getString(URL_PATTERN); + String httpMethodValue = answer.getString(HTTP_METHOD); + row.put(URL_PATTERN, urlPatternValue); + row.put(HTTP_METHOD, httpMethodValue); + result.add(row); + } + + return result; + }, + preparedStatement -> { + preparedStatement.setString(1, userName); + }); + + final List applicationResources = new ArrayList<>(); + if (!results.isEmpty()) { + results.forEach(row -> + { + ApplicationResourceResource resource = + new ApplicationResourceResource(row.get(URL_PATTERN), row.get(HTTP_METHOD)); + applicationResources.add(resource); + }); + } + + return applicationResources; + } +} + diff --git a/src/main/java/com/prueba/model/domain/AccountResource.java b/src/main/java/com/prueba/model/domain/AccountResource.java new file mode 100644 index 0000000..2315327 --- /dev/null +++ b/src/main/java/com/prueba/model/domain/AccountResource.java @@ -0,0 +1,46 @@ +package com.prueba.model.domain; + +public class AccountResource { + private final String code; + private final String name; + private final String surname; + private final String password; + private final String role; + + public AccountResource() { + this.code = null; + this.name = null; + this.surname = null; + this.password = null; + this.role = null; + } + + public AccountResource(String code, String name, String surname, String password, String role) { + this.code = code; + this.name = name; + this.surname = surname; + this.password = password; + this.role = role; + } + + public String getCode() { + return code; + } + + public String getName() { + return name; + } + + public String getSurname() { + return surname; + } + + public String getPassword() { + return password; + } + + public String getRole() { + return role; + } + +} diff --git a/src/main/java/com/prueba/model/domain/ApplicationResourceResource.java b/src/main/java/com/prueba/model/domain/ApplicationResourceResource.java new file mode 100644 index 0000000..d8958ac --- /dev/null +++ b/src/main/java/com/prueba/model/domain/ApplicationResourceResource.java @@ -0,0 +1,25 @@ +package com.prueba.model.domain; + +public class ApplicationResourceResource { + private final String urlPattern; + private final String httpMethod; + + public ApplicationResourceResource() { + this.urlPattern = null; + this.httpMethod = null; + } + + public ApplicationResourceResource(String urlPattern, String httpMethod) { + this.urlPattern = urlPattern; + this.httpMethod = httpMethod; + } + + public String getUrlPattern() { + return urlPattern; + } + + public String getHttpMethod() { + return httpMethod; + } + +} diff --git a/src/main/java/com/prueba/persistence/dao/AccountDao.java b/src/main/java/com/prueba/persistence/dao/AccountDao.java deleted file mode 100644 index 19bf2d9..0000000 --- a/src/main/java/com/prueba/persistence/dao/AccountDao.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.prueba.persistence.dao; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.sql.DataSource; - -import com.prueba.core.context.integration.database.DataBaseAccess; -import com.prueba.core.context.integration.database.impl.DataBaseAccessImpl; -import com.prueba.core.context.web.application.ApplicationWebContext; -import com.prueba.persistence.domain.AccountResource; - -public class AccountDao { - private static final String CODE = "CODE"; - private static final String NAME = "NAME"; - private static final String SURNAME = "SURNAME"; - private static final String PASSWORD = "PASSWORD"; - private static final String APP_ROLE_CODE = "APPLICATION_ROLE_CODE"; - - public AccountResource findByCode(String accountCode) { - final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); - final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl(dataSource); - - final List> results = - dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ?", - answer -> - { - final List> result = new ArrayList<>(); - while (answer.next()) { - final Map row = new HashMap<>(); - row.put(CODE, answer.getString(CODE)); - row.put(NAME, answer.getString(NAME)); - row.put(SURNAME, answer.getString(SURNAME)); - row.put(PASSWORD, answer.getString(PASSWORD)); - row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE)); - result.add(row); - } - - return result; - }, - preparedStatement -> { - preparedStatement.setString(1, accountCode); - }); - - AccountResource account = null; - if (!results.isEmpty()) { - final Map row = results.get(0); - - account = new AccountResource(row.get(CODE), row.get(NAME), - row.get(SURNAME), null, row.get(APP_ROLE_CODE)); - } - - return account; - } - - public void create(AccountResource account) { - final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); - final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); - - dataBaseAccess.executeUpdate( - "INSERT INTO ACCOUNT VALUES (?, ?, ?, ?, ?)", - preparedStatement -> { - preparedStatement.setString(1, account.getCode()); - preparedStatement.setString(2, account.getName()); - preparedStatement.setString(3, account.getSurname()); - preparedStatement.setString(4, account.getPassword()); - preparedStatement.setString(5, account.getRole()); - }); - - } - - public void deleteByCode(String code) { - final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); - final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); - - dataBaseAccess.executeUpdate("DELETE FROM ACCOUNT WHERE CODE = ?", - preparedStatement -> { - preparedStatement.setString(1, code); - }); - - } - - public AccountResource findByCodeAndPassword(String username, String password) { - final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); - final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); - - final List> results = - dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ? AND PASSWORD = ?", - answer -> - { - final List> result = new ArrayList<>(); - while (answer.next()) { - final Map row = new HashMap<>(); - row.put(CODE, answer.getString(CODE)); - row.put(NAME, answer.getString(NAME)); - row.put(SURNAME, answer.getString(SURNAME)); - row.put(PASSWORD, answer.getString(PASSWORD)); - row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE)); - result.add(row); - } - - return result; - }, - preparedStatement -> { - preparedStatement.setString(1, username); - preparedStatement.setString(2, password); - }); - - AccountResource account = null; - if (!results.isEmpty()) { - final Map row = results.get(0); - - account = new AccountResource(row.get(CODE), row.get(NAME), - row.get(SURNAME), null, row.get(APP_ROLE_CODE)); - } - - return account; - } -} diff --git a/src/main/java/com/prueba/persistence/dao/ApplicationResourceDao.java b/src/main/java/com/prueba/persistence/dao/ApplicationResourceDao.java deleted file mode 100644 index fc352ab..0000000 --- a/src/main/java/com/prueba/persistence/dao/ApplicationResourceDao.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.prueba.persistence.dao; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.sql.DataSource; - -import com.prueba.core.context.integration.database.impl.DataBaseAccessImpl; -import com.prueba.core.context.web.application.ApplicationWebContext; -import com.prueba.persistence.domain.ApplicationResourceResource; - -public class ApplicationResourceDao { - private static final String URL_PATTERN = "URL_PATTERN"; - private static final String HTTP_METHOD = "HTTP_METHOD"; - - public List findURLsByUserName(String userName) { - final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); - final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); - - final List> results = dataBaseAccess.executeQuery("" - + "SELECT APP_RES.URL_PATTERN, APP_RES.HTTP_METHOD FROM APPLICATION_ROLE APP_ROLE " - + "INNER JOIN APPLICATION_RESOURCE_APPLICATION_ROLE APP_RES_APP_ROLE ON APP_ROLE.CODE = APP_RES_APP_ROLE.APPLICATION_ROLE_CODE " - + "INNER JOIN APPLICATION_RESOURCE APP_RES ON APP_RES.URL_PATTERN = APP_RES_APP_ROLE.APPLICATION_RESOURCE_URL_PATTERN " - + "INNER JOIN ACCOUNT ACC ON ACC.APPLICATION_ROLE_CODE = APP_ROLE.CODE " - + "WHERE ACC.CODE = ? ", - answer -> - { - final List> result = new ArrayList<>(); - while (answer.next()) { - final Map row = new HashMap<>(); - String urlPatternValue = answer.getString(URL_PATTERN); - String httpMethodValue = answer.getString(HTTP_METHOD); - row.put(URL_PATTERN, urlPatternValue); - row.put(HTTP_METHOD, httpMethodValue); - result.add(row); - } - - return result; - }, - preparedStatement -> { - preparedStatement.setString(1, userName); - }); - - final List applicationResources = new ArrayList<>(); - if (!results.isEmpty()) { - results.forEach(row -> - { - ApplicationResourceResource resource = - new ApplicationResourceResource(row.get(URL_PATTERN), row.get(HTTP_METHOD)); - applicationResources.add(resource); - }); - } - - return applicationResources; - } -} - diff --git a/src/main/java/com/prueba/persistence/domain/AccountResource.java b/src/main/java/com/prueba/persistence/domain/AccountResource.java deleted file mode 100644 index bcac09d..0000000 --- a/src/main/java/com/prueba/persistence/domain/AccountResource.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.prueba.persistence.domain; - -public class AccountResource { - private final String code; - private final String name; - private final String surname; - private final String password; - private final String role; - - public AccountResource() { - this.code = null; - this.name = null; - this.surname = null; - this.password = null; - this.role = null; - } - - public AccountResource(String code, String name, String surname, String password, String role) { - this.code = code; - this.name = name; - this.surname = surname; - this.password = password; - this.role = role; - } - - public String getCode() { - return code; - } - - public String getName() { - return name; - } - - public String getSurname() { - return surname; - } - - public String getPassword() { - return password; - } - - public String getRole() { - return role; - } - -} diff --git a/src/main/java/com/prueba/persistence/domain/ApplicationResourceResource.java b/src/main/java/com/prueba/persistence/domain/ApplicationResourceResource.java deleted file mode 100644 index 7d528c2..0000000 --- a/src/main/java/com/prueba/persistence/domain/ApplicationResourceResource.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.prueba.persistence.domain; - -public class ApplicationResourceResource { - private final String urlPattern; - private final String httpMethod; - - public ApplicationResourceResource() { - this.urlPattern = null; - this.httpMethod = null; - } - - public ApplicationResourceResource(String urlPattern, String httpMethod) { - this.urlPattern = urlPattern; - this.httpMethod = httpMethod; - } - - public String getUrlPattern() { - return urlPattern; - } - - public String getHttpMethod() { - return httpMethod; - } - -} diff --git a/src/main/java/com/prueba/services/impl/ApiServiceImpl.java b/src/main/java/com/prueba/services/impl/ApiServiceImpl.java index 312bd30..7dad01c 100644 --- a/src/main/java/com/prueba/services/impl/ApiServiceImpl.java +++ b/src/main/java/com/prueba/services/impl/ApiServiceImpl.java @@ -1,7 +1,7 @@ package com.prueba.services.impl; -import com.prueba.persistence.dao.AccountDao; -import com.prueba.persistence.domain.AccountResource; +import com.prueba.model.dao.AccountDao; +import com.prueba.model.domain.AccountResource; public class ApiServiceImpl { diff --git a/src/main/java/com/prueba/services/impl/AuthorizationServicesImpl.java b/src/main/java/com/prueba/services/impl/AuthorizationServicesImpl.java index d30f61d..a5d372d 100644 --- a/src/main/java/com/prueba/services/impl/AuthorizationServicesImpl.java +++ b/src/main/java/com/prueba/services/impl/AuthorizationServicesImpl.java @@ -7,8 +7,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.prueba.core.context.util.AntPathMatcher; -import com.prueba.persistence.dao.ApplicationResourceDao; -import com.prueba.persistence.domain.ApplicationResourceResource; +import com.prueba.model.domain.ApplicationResourceResource; +import com.prueba.model.dao.ApplicationResourceDao; public class AuthorizationServicesImpl { private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationServicesImpl.class); diff --git a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java index 6594337..ecb9a49 100644 --- a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java +++ b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java @@ -10,7 +10,7 @@ import java.util.UUID; import com.prueba.core.context.security.persistence.SessionInfo; import com.prueba.core.context.security.persistence.Sessions; import com.prueba.core.context.security.persistence.context.SessionContext; -import com.prueba.persistence.dao.AccountDao; +import com.prueba.model.dao.AccountDao; import com.prueba.view.login.LoginFormImpl; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; diff --git a/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java b/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java index bd168c5..b62e88f 100644 --- a/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java +++ b/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java @@ -11,7 +11,8 @@ import org.junit.Test; import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext; import com.prueba.core.context.integration.liquibase.impl.LiquibaseContext; -import com.prueba.persistence.domain.AccountResource; +import com.prueba.model.dao.AccountDao; +import com.prueba.model.domain.AccountResource; public class AccountDaoIntegrationTest { private AccountDao accountDao; -- 2.1.4