diff --git a/src/main/java/MessageResources.properties b/src/main/java/MessageResources.properties index a1e7aa0..a34ee31 100644 --- a/src/main/java/MessageResources.properties +++ b/src/main/java/MessageResources.properties @@ -8,7 +8,19 @@ error.password.wrong=Wrong password user.not-found.error=User not found user.username-taken.error=Username's already taken user.delete.at-least-one.error=Must keep at least one user +user.mail.invalid=Email Format Invalid user.create.success=User "{0}" was created successfully user.update.success=User "{0}" was update successfully user.delete.success=User "{0}" was deleted successfully + + +company.not-found.error= Company not found +user.delete.at-least-one.error=Must keep at least one Company +company.delete.success=Company "{0}" was deleted successfully + +company.create.success=Company "{0}" was created successfully +company.update.success=Company "{0}" was update successfully +company.name-taken.error=name's already taken +error.iva.zero.or.negative= IVA must be a positive number and biggest than zero + diff --git a/src/main/java/com/my_app/App.java b/src/main/java/com/my_app/App.java index 17e71c8..f721356 100644 --- a/src/main/java/com/my_app/App.java +++ b/src/main/java/com/my_app/App.java @@ -15,12 +15,15 @@ import com.my_app.db.DataSourceFactory; import com.my_app.exception.AppGenericException; import com.my_app.model.City; +import com.my_app.model.Company; import com.my_app.model.Country; import com.my_app.model.User; import com.my_app.repo.CityRepository; +import com.my_app.repo.CompanyRepository; import com.my_app.repo.CountryRepository; import com.my_app.repo.UserRepository; import com.my_app.repo.impl.CityRepositoryImpl; +import com.my_app.repo.impl.CompanyRepositoryImpl; import com.my_app.repo.impl.CountryRepositoryImpl; import com.my_app.repo.impl.UserRepositoryImpl; @@ -81,6 +84,7 @@ private void initDb() { try (final Connection conn = this.dataSource.getConnection()) { initDbCountriesAndCities(conn); initDbUsers(conn); + initDbCompanies(conn); } catch (SQLException e) { throw new AppGenericException("Error trying to insert initial db data", e); } @@ -96,6 +100,9 @@ private void initDbCountriesAndCities(Connection conn) throws SQLException { stmt.executeUpdate( "CREATE TABLE CITY (ID IDENTITY NOT NULL PRIMARY KEY, NAME VARCHAR(255) UNIQUE NOT NULL, COUNTRY_ID BIGINT NOT NULL, FOREIGN KEY (COUNTRY_ID) REFERENCES COUNTRY(ID))"); } + + + final CountryRepository countryRepository = new CountryRepositoryImpl(conn); final CityRepository cityRepository = new CityRepositoryImpl(conn, countryRepository); @@ -115,20 +122,47 @@ private void initDbCountriesAndCities(Connection conn) throws SQLException { private void initDbUsers(Connection conn) throws SQLException { try (final Statement stmt = conn.createStatement()) { stmt.executeUpdate( - "CREATE TABLE \"USER\" (ID IDENTITY NOT NULL PRIMARY KEY, USERNAME VARCHAR(255) UNIQUE NOT NULL, PASSWORD VARCHAR(255) NOT NULL, CITY_ID BIGINT NOT NULL, FOREIGN KEY (CITY_ID) REFERENCES CITY(ID))"); + "CREATE TABLE \"USER\" (ID IDENTITY NOT NULL PRIMARY KEY, USERNAME VARCHAR(255) UNIQUE NOT NULL, PASSWORD VARCHAR(255) NOT NULL, CITY_ID BIGINT NOT NULL, EMAIL VARCHAR(255) NOT NULL,FOREIGN KEY (CITY_ID) REFERENCES CITY(ID))"); } final CityRepository cityRepository = new CityRepositoryImpl(conn, new CountryRepositoryImpl(conn)); final UserRepository userRepository = new UserRepositoryImpl(conn, cityRepository); - userRepository.save(new User("admin", "admin", cityRepository.findById((long) this.random.nextInt(49) + 1))); + userRepository.save(new User("admin", "admin", cityRepository.findById((long) this.random.nextInt(49) + 1),"usu@mail.com")); for (int i = 1; i < 11; i++) { userRepository.save( - new User("user" + i, "user" + i, cityRepository.findById((long) this.random.nextInt(49) + 1))); + new User("user" + i, "user" + i, cityRepository.findById((long) this.random.nextInt(49) + 1),"user"+i+"@mail.com")); } Logger.debug("All users created: {}", userRepository.findAll()); } + + + + + private void initDbCompanies(Connection conn) throws SQLException { + try (final Statement stmt = conn.createStatement()) { + stmt.executeUpdate( + "CREATE TABLE \"COMPANY\" (ID IDENTITY NOT NULL PRIMARY KEY, NAME VARCHAR(255) UNIQUE NOT NULL, ADRESS VARCHAR(255) NOT NULL, IVA INT NOT NULL, CITY_ID BIGINT NOT NULL, COUNTRY BIGINT NOT NULL,FOREIGN KEY (CITY_ID) REFERENCES CITY(ID))"); + } + + final CityRepository cityRepository = new CityRepositoryImpl(conn, new CountryRepositoryImpl(conn)); + final CompanyRepository companyRepository = new CompanyRepositoryImpl(conn, cityRepository); + + //userRepository.save(new User("admin", "admin", cityRepository.findById((long) this.random.nextInt(49) + 1),"usu@mail.com")); + + for (int i = 1; i < 11; i++) { + + Long aux = Long.valueOf(i); + Country country = new Country(aux, "Country" + i); + + companyRepository.save( + new Company("company" + i, "useradress" + i, i ,cityRepository.findById((long) this.random.nextInt(49) + 1),country.getId())); + } + + Logger.debug("All companies created: {}", companyRepository.findAll()); + } + } diff --git a/src/main/java/com/my_app/model/Company.java b/src/main/java/com/my_app/model/Company.java new file mode 100644 index 0000000..239fa44 --- /dev/null +++ b/src/main/java/com/my_app/model/Company.java @@ -0,0 +1,127 @@ +package com.my_app.model; + +public class Company { + + + private Long id; + private String name; + private String adress; + private int iva; + private City city; + private Long country; + + + //default construct + public Company(){ + + + + } + + + /* + * public Company(Long id, String name, String adress, int iva, City city, Long + * country) { super(); this.id = id; this.name = name; this.adress = adress; + * this.iva = iva; this.city = city; this.country = country; } + */ + + + + public Company(Long id, String name, String adress, int iva, City city, Long country) { + super(); + this.id = id; + this.name = name; + this.adress = adress; + this.iva = iva; + this.city = city; + this.country = country; + } + + + + + + + public Company(String name, String adress, int iva, City city, Long country) { + super(); + this.name = name; + this.adress = adress; + this.iva = iva; + this.city = city; + this.country=country; + } + + + + + public Long getId() { + return id; + } + + + public void setId(Long id) { + this.id = id; + } + + + public String getName() { + return name; + } + + + public void setName(String name) { + this.name = name; + } + + + public String getAdress() { + return adress; + } + + + public void setAdress(String adress) { + this.adress = adress; + } + + + public int getIva() { + return iva; + } + + + public void setIva(int iva) { + this.iva = iva; + } + + + public City getCity() { + return city; + } + + + public void setCity(City city) { + this.city = city; + } + + + public Long getCountry() { + return country; + } + + + public void setCountry(Long country) { + this.country = country; + } + + public boolean isNew() { + return this.id == null; + } + + + @Override + public String toString() { + return String.format("Company [id=%s, name=%s, adress=%s, iva=%s, city=%s, country=%s]", id, name, adress, iva, + city, country); + } + +} diff --git a/src/main/java/com/my_app/model/User.java b/src/main/java/com/my_app/model/User.java index 05b2167..e950702 100644 --- a/src/main/java/com/my_app/model/User.java +++ b/src/main/java/com/my_app/model/User.java @@ -5,23 +5,43 @@ public class User { private String username; private String password; private City city; + + // inclusão de novo atributo email solicitado pelo exercicio 5 + private String email; + + public User() { } - + + + public User(String username, String password, City city) { super(); this.username = username; this.password = password; this.city = city; + + } + + //inclusão do parametros email no construtor + + public User(String username, String password, City city, String email) { + super(); + this.username = username; + this.password = password; + this.city = city; + this.email = email; } - public User(Long id, String username, String password, City city) { + //inclusão do parametros email no construtor + public User(Long id, String username, String password, City city,String email) { super(); this.id = id; this.username = username; this.password = password; this.city = city; + this.email = email; } public Long getId() { @@ -55,6 +75,18 @@ public City getCity() { public void setCity(City city) { this.city = city; } + + + // getters and setters do novo atributo email + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + public boolean isNew() { return this.id == null; @@ -62,7 +94,7 @@ public boolean isNew() { @Override public String toString() { - return String.format("User [id=%s, username=%s, password=%s, city=%s]", id, username, password, city); + return String.format("User [id=%s, username=%s, password=%s, city=%s, email=%s]", id, username, password, city, email); } } diff --git a/src/main/java/com/my_app/page/company/delete/CompanyDeleteAction.java b/src/main/java/com/my_app/page/company/delete/CompanyDeleteAction.java new file mode 100644 index 0000000..a06f19f --- /dev/null +++ b/src/main/java/com/my_app/page/company/delete/CompanyDeleteAction.java @@ -0,0 +1,78 @@ +package com.my_app.page.company.delete; + +import java.sql.Connection; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionErrors; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.apache.struts.action.ActionMessage; +import org.apache.struts.action.ActionMessages; + +import com.my_app.model.Company; +import com.my_app.model.User; +import com.my_app.service.CompanyService; +import com.my_app.service.UserService; +import com.my_app.service.factory.CompanyServiceFactory; +import com.my_app.service.factory.UserServiceFactory; +import com.my_app.utils.LoginUtils; + +public class CompanyDeleteAction extends Action { + + @Override + public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest req, + HttpServletResponse res) throws Exception { + + if (LoginUtils.isUserNotLoggedIn(req.getSession())) { + return mapping.findForward("actionLoginRedir"); + } + + final ActionMessages actionMessages = new ActionMessages(); + final ActionErrors actionErrors = new ActionErrors(); + + final CompanyService companyService = new CompanyServiceFactory().create((Connection) req.getAttribute("conn")); + + final int totalcompanysCount = companyService.findAll().size(); + + if (totalcompanysCount - 1 != 0) { + + final String companyIdStr = req.getParameter("companyId"); + + if (StringUtils.isNotBlank(companyIdStr)) { + final Long compId = Long.valueOf(companyIdStr); + + final Company company = companyService.findById(compId); + + if (company != null) { + companyService.delete(company); + + actionMessages.add("topMsgs", new ActionMessage("company.delete.success", company.getName())); + } else { + actionErrors.add("topErrors", new ActionMessage("company.not-found.error")); + } + } else { + actionErrors.add("topErrors", new ActionMessage("company.not-found.error")); + } + + } else { + actionErrors.add("topErrors", new ActionMessage("company.delete.at-least-one.error")); + this.saveErrors(req, actionErrors); + } + + if (!actionMessages.isEmpty()) { + req.setAttribute("actionMessages", actionMessages); + this.saveMessages(req, actionMessages); + } else if (!actionErrors.isEmpty()) { + req.setAttribute("actionErrors", actionErrors); + this.saveErrors(req, actionErrors); + } + + return mapping.findForward("actionCompanies"); + } + +} diff --git a/src/main/java/com/my_app/page/company/list/CompanyrListAction.java b/src/main/java/com/my_app/page/company/list/CompanyrListAction.java new file mode 100644 index 0000000..38dd149 --- /dev/null +++ b/src/main/java/com/my_app/page/company/list/CompanyrListAction.java @@ -0,0 +1,38 @@ +package com.my_app.page.company.list; + +import java.sql.Connection; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; + +import com.my_app.service.CompanyService; +import com.my_app.service.UserService; +import com.my_app.service.factory.CompanyServiceFactory; +import com.my_app.service.factory.UserServiceFactory; +import com.my_app.utils.LoginUtils; + +public class CompanyrListAction extends Action { + + @Override + public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest req, + HttpServletResponse res) throws Exception { + + if (LoginUtils.isUserNotLoggedIn(req.getSession())) { + return mapping.findForward("actionLoginRedir"); + } + + final CompanyService companyService = new CompanyServiceFactory().create((Connection) req.getAttribute("conn")); + + req.setAttribute("companies", companyService.findAll()); + + return mapping.getInputForward(); + + //return mapping.findForward("actionCompanies"); + } + +} diff --git a/src/main/java/com/my_app/page/company/save/CompanySaveAction.java b/src/main/java/com/my_app/page/company/save/CompanySaveAction.java new file mode 100644 index 0000000..40b5f47 --- /dev/null +++ b/src/main/java/com/my_app/page/company/save/CompanySaveAction.java @@ -0,0 +1,123 @@ +package com.my_app.page.company.save; + +import java.sql.Connection; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.apache.struts.action.ActionMessage; +import org.tinylog.Logger; + +import com.my_app.model.Company; +import com.my_app.page.company.save.service.CompanySaveService; +import com.my_app.page.company.save.service.CompanySaveServiceFactory; +import com.my_app.utils.LoginUtils; + +public class CompanySaveAction extends Action { + + @Override + public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest req, + HttpServletResponse res) throws Exception { + final Connection conn = (Connection) req.getAttribute("conn"); + final CompanySaveService companySaveService = new CompanySaveServiceFactory().create(conn); + + final CompanySaveForm form = (CompanySaveForm) actionForm; + + Logger.debug("entrou no action companysave"); + + req.setAttribute("form", form); + + if (LoginUtils.isUserNotLoggedIn(req.getSession())) { + Logger.debug("entrou no action companysave login"); + return mapping.findForward("loginRedir"); + } else if ("save".equals(form.getAction())) { + Logger.debug("entrou no action companysave save"); + return executeSaveAction(mapping, form, req, res, companySaveService); + } else if ("validate".equals(form.getAction())) { + Logger.debug("entrou no action companysave valide"); + return executeValidateAction(mapping, form, req, res, companySaveService); + } else { + Logger.debug("entrou no action companysave no metodo executeformaction"); + return this.executeFormAction(mapping, form, req, res, companySaveService); + } + + + } + + private ActionForward executeFormAction(ActionMapping mapping, CompanySaveForm form, HttpServletRequest req, + HttpServletResponse res, CompanySaveService companySaveService) { + + Logger.debug("entrou no action companysave executeFormAction"); + + if (form.isFormInit()) { + companySaveService.formInit(form); + form.setFormInit(false); + } + + companySaveService.setRequestAttrs(form, req); + + + + return mapping.findForward("formcompany"); + } + + private ActionForward executeValidateAction(ActionMapping mapping, CompanySaveForm form, HttpServletRequest req, + HttpServletResponse res, CompanySaveService companySaveService) { + + if (companySaveService.validate(form)) { + form.getActionMessages().add("topMsgs", new ActionMessage("form.validation.success", form.getname())); + } else { + form.getActionErrors().add("topMsgs", new ActionMessage("form.validation.error")); + } + + if (!form.getActionMessages().isEmpty()) { + req.setAttribute("actionMessages", form.getActionMessages()); + this.saveMessages(req, form.getActionMessages()); + } else if (!form.getActionErrors().isEmpty()) { + req.setAttribute("actionErrors", form.getActionErrors()); + this.saveErrors(req, form.getActionErrors()); + } + + req.setAttribute("validated", true); + + return this.executeFormAction(mapping, form, req, res, companySaveService); + } + + private ActionForward executeSaveAction(ActionMapping mapping, CompanySaveForm form, HttpServletRequest req, + HttpServletResponse res, CompanySaveService companySaveService) { + + ActionForward actionForward; + + if (companySaveService.validate(form)) { + final Company company = companySaveService.saveCompany(form); + + if (form.isNewUser()) { + form.getActionMessages().add("topMsgs", new ActionMessage("company.create.success", company.getName())); + } else { + form.getActionMessages().add("topMsgs", new ActionMessage("company.update.success", company.getName())); + } + + actionForward = mapping.findForward("actionCompanies"); + } else { + form.getActionErrors().add("topMsgs", new ActionMessage("form.validation.error")); + req.setAttribute("validated", true); + + actionForward = this.executeFormAction(mapping, form, req, res, companySaveService); + } + + if (!form.getActionMessages().isEmpty()) { + req.setAttribute("actionMessages", form.getActionMessages()); + this.saveMessages(req, form.getActionMessages()); + } else if (!form.getActionErrors().isEmpty()) { + req.setAttribute("actionErrors", form.getActionErrors()); + this.saveErrors(req, form.getActionErrors()); + } + + return actionForward; + } + +} diff --git a/src/main/java/com/my_app/page/company/save/CompanySaveForm.java b/src/main/java/com/my_app/page/company/save/CompanySaveForm.java new file mode 100644 index 0000000..d728932 --- /dev/null +++ b/src/main/java/com/my_app/page/company/save/CompanySaveForm.java @@ -0,0 +1,93 @@ +package com.my_app.page.company.save; + +import com.my_app.model.BaseActionForm; + +public class CompanySaveForm extends BaseActionForm { + + + private static final long serialVersionUID = 5775129190004388967L; + private Long companyId; + private String name; + private String adress; + private Long countryId; + private Long cityId; + private int iva; + + private String originalUsername; + + private transient boolean formInit = true; + + public Long getCompanyId() { + return companyId; + } + + public void setCompanyId(Long userId) { + this.companyId = userId; + } + + public String getname() { + return name; + } + + public void setname(String name) { + this.name = name; + } + + public String getAdress() { + return adress; + } + + public void setAdress(String adress) { + this.adress = adress; + } + + public Long getCountryId() { + return countryId; + } + + public void setCountryId(Long countryId) { + this.countryId = countryId; + } + + public Long getCityId() { + return cityId; + } + + public void setCityId(Long cityId) { + this.cityId = cityId; + } + + + public int getIva() { + return iva; + } + + public void setIva(int iva) { + this.iva = iva; + } + + + public String getOriginalUsername() + { return originalUsername; } + + public void setOriginalUsername(String originalUsername) { + this.originalUsername = originalUsername; } + + + public boolean isFormInit() { + return formInit; + } + + public void setFormInit(boolean formInit) { + this.formInit = formInit; + } + + public boolean isNewUser() { + return this.companyId == null || this.companyId == 0; + } + + public static long getSerialversionuid() { + return serialVersionUID; + } + +} diff --git a/src/main/java/com/my_app/page/company/save/mapper/CompanySaveFormToCompanyMapper.java b/src/main/java/com/my_app/page/company/save/mapper/CompanySaveFormToCompanyMapper.java new file mode 100644 index 0000000..e7c2d60 --- /dev/null +++ b/src/main/java/com/my_app/page/company/save/mapper/CompanySaveFormToCompanyMapper.java @@ -0,0 +1,21 @@ +package com.my_app.page.company.save.mapper; + +import com.my_app.model.City; +import com.my_app.model.Company; +import com.my_app.model.Country; +import com.my_app.model.User; +import com.my_app.page.company.save.CompanySaveForm; +import com.my_app.page.user.save.UserSaveForm; + +public class CompanySaveFormToCompanyMapper { + + public Company toCompany(CompanySaveForm form) { + + final City city = new City(form.getCityId()); + Country country = new Country(); + country.setId(form.getCountryId()); + + return new Company(Long.valueOf(0).equals(form.getCompanyId()) ? null : form.getCompanyId(), form.getname(), + form.getAdress(),form.getIva(), city, form.getCountryId()); + } +} diff --git a/src/main/java/com/my_app/page/company/save/mapper/CompanyToCompanySaveFormMapper.java b/src/main/java/com/my_app/page/company/save/mapper/CompanyToCompanySaveFormMapper.java new file mode 100644 index 0000000..e680d55 --- /dev/null +++ b/src/main/java/com/my_app/page/company/save/mapper/CompanyToCompanySaveFormMapper.java @@ -0,0 +1,24 @@ +package com.my_app.page.company.save.mapper; + +import com.my_app.model.Company; +import com.my_app.page.company.save.CompanySaveForm; + +public class CompanyToCompanySaveFormMapper { + + public CompanySaveForm toCompanySaveForm(Company company) { + return this.mapTo(company, new CompanySaveForm()); + } + + public CompanySaveForm mapTo(Company company, CompanySaveForm form) { + + form.setCompanyId(company.getId()); + form.setname(company.getName()); + form.setAdress(company.getAdress()); + form.setCountryId(company.getCity().getCountry().getId()); + form.setCityId(company.getCity().getId()); + form.setIva(company.getIva()); + + return form; + } + +} diff --git a/src/main/java/com/my_app/page/company/save/service/CompanySaveService.java b/src/main/java/com/my_app/page/company/save/service/CompanySaveService.java new file mode 100644 index 0000000..f9f1bec --- /dev/null +++ b/src/main/java/com/my_app/page/company/save/service/CompanySaveService.java @@ -0,0 +1,16 @@ +package com.my_app.page.company.save.service; + +import javax.servlet.http.HttpServletRequest; + +import com.my_app.model.Company; +import com.my_app.page.company.save.CompanySaveForm; + +public interface CompanySaveService { + public boolean validate(CompanySaveForm form); + + public Company saveCompany(CompanySaveForm form); + + public void formInit(CompanySaveForm form); + + public void setRequestAttrs(CompanySaveForm form, HttpServletRequest req); +} diff --git a/src/main/java/com/my_app/page/company/save/service/CompanySaveServiceFactory.java b/src/main/java/com/my_app/page/company/save/service/CompanySaveServiceFactory.java new file mode 100644 index 0000000..5c5b218 --- /dev/null +++ b/src/main/java/com/my_app/page/company/save/service/CompanySaveServiceFactory.java @@ -0,0 +1,18 @@ +package com.my_app.page.company.save.service; + +import java.sql.Connection; + +import com.my_app.service.factory.CityServiceFactory; +import com.my_app.service.factory.CompanyServiceFactory; +import com.my_app.service.factory.ConnectionBasedServiceFactory; +import com.my_app.service.factory.CountryServiceFactory; +import com.my_app.service.factory.UserServiceFactory; + +public class CompanySaveServiceFactory implements ConnectionBasedServiceFactory { + + @Override + public CompanySaveService create(Connection conn) { + return new CompanySaveServiceImpl(new CompanyServiceFactory().create(conn), new CountryServiceFactory().create(conn), + new CityServiceFactory().create(conn)); + } +} diff --git a/src/main/java/com/my_app/page/company/save/service/CompanySaveServiceImpl.java b/src/main/java/com/my_app/page/company/save/service/CompanySaveServiceImpl.java new file mode 100644 index 0000000..b20c9f5 --- /dev/null +++ b/src/main/java/com/my_app/page/company/save/service/CompanySaveServiceImpl.java @@ -0,0 +1,101 @@ +package com.my_app.page.company.save.service; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.lang3.StringUtils; +import org.apache.struts.action.ActionMessage; + +import com.my_app.model.Company; +import com.my_app.page.company.save.CompanySaveForm; +import com.my_app.page.company.save.mapper.CompanySaveFormToCompanyMapper; +import com.my_app.page.company.save.mapper.CompanyToCompanySaveFormMapper; +import com.my_app.service.CityService; +import com.my_app.service.CompanyService; +import com.my_app.service.CountryService; +import com.my_app.utils.UserUtils; + +public class CompanySaveServiceImpl implements CompanySaveService { + + final CompanyService companyService; + final CountryService countryService; + final CityService cityService; + + public CompanySaveServiceImpl(CompanyService companyService, CountryService countryService, CityService cityService) { + super(); + this.companyService = companyService; + this.countryService = countryService; + this.cityService = cityService; + } + + @Override + public void formInit(CompanySaveForm form) { + if (!form.isNewUser()) { + final Company company = this.companyService.findById(form.getCompanyId()); + + new CompanyToCompanySaveFormMapper().mapTo(company, form); + form.setOriginalUsername(form.getname()); + } + } + + @Override + public boolean validate(CompanySaveForm form) { + boolean isValid = true; + + if (StringUtils.isBlank(form.getname())) { + isValid = false; + form.getActionErrors().add("name", new ActionMessage("error.common.required")); + } else if ((form.isNewUser() || (!form.isNewUser() + && !form.getOriginalUsername().equals(UserUtils.normalizeUsername(form.getname())))) + && companyService.findByName(form.getname()) != null) { + isValid = false; + form.getActionErrors().add("name", new ActionMessage("company.name-taken.error")); + } + + if (StringUtils.isBlank(form.getAdress())) { + isValid = false; + form.getActionErrors().add("adress", new ActionMessage("error.common.required")); + } + + if (form.getCountryId() == null || form.getCountryId() == 0) { + isValid = false; + form.getActionErrors().add("country", new ActionMessage("error.common.required")); + } + + if (form.getCityId() == null || form.getCityId() == 0) { + isValid = false; + + if (form.getCountryId() == null || form.getCountryId() == 0) { + form.getActionErrors().add("city", new ActionMessage("form.field.pre-choose", "Country")); + } else { + form.getActionErrors().add("city", new ActionMessage("error.common.required")); + } + } + + + + if ((form.getIva() < 0) || (form.getIva() == 0)) { + isValid = false; + form.getActionErrors().add("iva", new ActionMessage("error.iva.zero.or.negative")); + + } + + + + return isValid; + } + + @Override + public Company saveCompany(CompanySaveForm form) { + return this.companyService.save(new CompanySaveFormToCompanyMapper().toCompany(form)); + } + + @Override + public void setRequestAttrs(CompanySaveForm form, HttpServletRequest req) { + req.setAttribute("countries", this.countryService.findAll()); + + if (form.getCountryId() != null && form.getCountryId() > 0) { + req.setAttribute("cities", this.cityService.findAllByCountryId(form.getCountryId())); + } + } + +} diff --git a/src/main/java/com/my_app/page/logout/ActionLogout.java b/src/main/java/com/my_app/page/logout/ActionLogout.java new file mode 100644 index 0000000..71c8273 --- /dev/null +++ b/src/main/java/com/my_app/page/logout/ActionLogout.java @@ -0,0 +1,37 @@ +package com.my_app.page.logout; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.tinylog.Logger; + +import com.my_app.AppConstants; + +public class ActionLogout extends Action { + + + + + @Override + public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws Exception { + + // pegar a sessão e invalidar + HttpSession sessao = request.getSession(); + Logger.debug("sessão antes do invalidate", sessao.toString()); + sessao.invalidate(); + //talvez seja redundante, testar + request.getSession().setAttribute(AppConstants.SESSION_ATTR_KEY_IS_LOGGED_IN, false); + + Logger.debug("sessão apos o invalidate", sessao.toString()); + + + return mapping.findForward("logoutPage"); + } + +} diff --git a/src/main/java/com/my_app/page/user/save/UserSaveAction.java b/src/main/java/com/my_app/page/user/save/UserSaveAction.java index 3cb9c12..a2e63c1 100644 --- a/src/main/java/com/my_app/page/user/save/UserSaveAction.java +++ b/src/main/java/com/my_app/page/user/save/UserSaveAction.java @@ -10,6 +10,7 @@ import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; +import org.tinylog.Logger; import com.my_app.model.User; import com.my_app.page.user.save.service.UserSaveService; @@ -27,20 +28,28 @@ public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpS final UserSaveForm form = (UserSaveForm) actionForm; req.setAttribute("form", form); + + Logger.debug("entrou no action usersave execute"); if (LoginUtils.isUserNotLoggedIn(req.getSession())) { + Logger.debug("entrou no action usersave login"); return mapping.findForward("loginRedir"); } else if ("save".equals(form.getAction())) { + Logger.debug("entrou no action usersave save"); return executeSaveAction(mapping, form, req, res, userSaveService); } else if ("validate".equals(form.getAction())) { + Logger.debug("entrou no action usersave validate"); return executeValidateAction(mapping, form, req, res, userSaveService); } else { + Logger.debug("entrou no action usersave form"); return this.executeFormAction(mapping, form, req, res, userSaveService); } } private ActionForward executeFormAction(ActionMapping mapping, UserSaveForm form, HttpServletRequest req, HttpServletResponse res, UserSaveService userSaveService) { + + Logger.debug("entrou no action usersave executeFormAction"); if (form.isFormInit()) { userSaveService.formInit(form); diff --git a/src/main/java/com/my_app/page/user/save/UserSaveForm.java b/src/main/java/com/my_app/page/user/save/UserSaveForm.java index ebd2622..0377903 100644 --- a/src/main/java/com/my_app/page/user/save/UserSaveForm.java +++ b/src/main/java/com/my_app/page/user/save/UserSaveForm.java @@ -10,6 +10,7 @@ public class UserSaveForm extends BaseActionForm { private String password; private Long countryId; private Long cityId; + private String email; private String originalUsername; @@ -54,6 +55,15 @@ public Long getCityId() { public void setCityId(Long cityId) { this.cityId = cityId; } + + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } public String getOriginalUsername() { return originalUsername; @@ -78,5 +88,6 @@ public boolean isNewUser() { public static long getSerialversionuid() { return serialVersionUID; } + } diff --git a/src/main/java/com/my_app/page/user/save/mapper/UserSaveFormToUserMapper.java b/src/main/java/com/my_app/page/user/save/mapper/UserSaveFormToUserMapper.java index 762d42f..f175078 100644 --- a/src/main/java/com/my_app/page/user/save/mapper/UserSaveFormToUserMapper.java +++ b/src/main/java/com/my_app/page/user/save/mapper/UserSaveFormToUserMapper.java @@ -11,6 +11,6 @@ public User toUser(UserSaveForm form) { final City city = new City(form.getCityId()); return new User(Long.valueOf(0).equals(form.getUserId()) ? null : form.getUserId(), form.getUsername(), - form.getPassword(), city); + form.getPassword(), city, form.getEmail()); } } diff --git a/src/main/java/com/my_app/page/user/save/mapper/UserToUserSaveFormMapper.java b/src/main/java/com/my_app/page/user/save/mapper/UserToUserSaveFormMapper.java index 008c0be..ab44081 100644 --- a/src/main/java/com/my_app/page/user/save/mapper/UserToUserSaveFormMapper.java +++ b/src/main/java/com/my_app/page/user/save/mapper/UserToUserSaveFormMapper.java @@ -15,6 +15,7 @@ public UserSaveForm mapTo(User user, UserSaveForm form) { form.setUsername(user.getUsername()); form.setCountryId(user.getCity().getCountry().getId()); form.setCityId(user.getCity().getId()); + form.setEmail(user.getEmail()); return form; } diff --git a/src/main/java/com/my_app/page/user/save/service/UserSaveServiceImpl.java b/src/main/java/com/my_app/page/user/save/service/UserSaveServiceImpl.java index 3d41923..ac13469 100644 --- a/src/main/java/com/my_app/page/user/save/service/UserSaveServiceImpl.java +++ b/src/main/java/com/my_app/page/user/save/service/UserSaveServiceImpl.java @@ -70,6 +70,22 @@ public boolean validate(UserSaveForm form) { form.getActionErrors().add("city", new ActionMessage("error.common.required")); } } + + + // validação do novo atributo email, solicitado no exercicio 5 + + if ((StringUtils.isBlank(form.getEmail())) || (form.getEmail() == null)) { + isValid = false; + form.getActionErrors().add("email", new ActionMessage("error.common.required")); + + } else if(! UserUtils.validaEmailTopLevel(form.getEmail())) { + isValid = false; + form.getActionErrors().add("email", new ActionMessage("user.mail.invalid")); + + + } + + return isValid; } diff --git a/src/main/java/com/my_app/page/util/ActionPrivate.java b/src/main/java/com/my_app/page/util/ActionPrivate.java new file mode 100644 index 0000000..1c4bc7b --- /dev/null +++ b/src/main/java/com/my_app/page/util/ActionPrivate.java @@ -0,0 +1,43 @@ +package com.my_app.page.util; + +import java.util.Enumeration; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.tinylog.Logger; + +import com.my_app.AppConstants; +import com.my_app.utils.LoginUtils; + +public class ActionPrivate extends Action { + + + + + @Override + public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws Exception { + + + Logger.debug("entrou no action private"); + + Enumeration param = request.getSession().getAttributeNames(); + Logger.debug("parameter names sessio" + param.asIterator().toString()); + + if (LoginUtils.isUserLoggedIn(request.getSession())) { + return mapping.findForward("privatePage"); + } else { + return mapping.findForward("actionLoginRedir"); + } + + + //return mapping.findForward("privatePage"); + } + +} diff --git a/src/main/java/com/my_app/page/util/ActionPublic.java b/src/main/java/com/my_app/page/util/ActionPublic.java new file mode 100644 index 0000000..57d09ac --- /dev/null +++ b/src/main/java/com/my_app/page/util/ActionPublic.java @@ -0,0 +1,31 @@ +package com.my_app.page.util; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.tinylog.Logger; + +import com.my_app.AppConstants; + +public class ActionPublic extends Action { + + + + + @Override + public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws Exception { + + + Logger.debug("entrou no action public"); + + + return mapping.findForward("publicPage"); + } + +} diff --git a/src/main/java/com/my_app/repo/CompanyRepository.java b/src/main/java/com/my_app/repo/CompanyRepository.java new file mode 100644 index 0000000..a97b698 --- /dev/null +++ b/src/main/java/com/my_app/repo/CompanyRepository.java @@ -0,0 +1,14 @@ +package com.my_app.repo; + +import java.util.List; + +import com.my_app.model.City; +import com.my_app.model.Company; +import com.my_app.model.Country; + +public interface CompanyRepository extends Repository { + + Company findByName(String name); + + +} diff --git a/src/main/java/com/my_app/repo/impl/CompanyRepositoryImpl.java b/src/main/java/com/my_app/repo/impl/CompanyRepositoryImpl.java new file mode 100644 index 0000000..65b3fe6 --- /dev/null +++ b/src/main/java/com/my_app/repo/impl/CompanyRepositoryImpl.java @@ -0,0 +1,168 @@ +package com.my_app.repo.impl; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; + +import com.my_app.exception.AppGenericException; +import com.my_app.model.Company; +import com.my_app.model.User; +import com.my_app.repo.CityRepository; +import com.my_app.repo.CompanyRepository; +import com.my_app.repo.CountryRepository; +import com.my_app.utils.UserUtils; + +public class CompanyRepositoryImpl implements CompanyRepository { + + private final Connection conn; + private final CityRepository cityRepository; + //private final CountryRepository countryRepository; + + + public CompanyRepositoryImpl(Connection conn, CityRepository cityRepository) { + + super(); + this.conn = conn; + this.cityRepository = cityRepository; + //this.countryRepository = countryRepository; + + + } + + @Override + public Company save(Company o) { + + if (o.isNew()) { + return this.create(o); + } else { + return this.update(o); + } + //return null; + } + + + + @Override + public Company findById(Long id) { + + try (final PreparedStatement stmt = this.conn + .prepareStatement("SELECT ID, NAME, ADRESS,IVA, CITY_ID, COUNTRY FROM \"COMPANY\" WHERE ID = ?")) { + + stmt.setLong(1, id); + + try (final ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return new Company(rs.getLong(1), rs.getString(2), rs.getString(3),rs.getInt(4), + this.cityRepository.findById(rs.getLong(5)),rs.getLong(6)); + } + } + + return null; + } catch (SQLException e) { + throw new AppGenericException("Error while querying by id for Company", e); + } + } + + @Override + public List findAll() { + + try (final PreparedStatement stmt = this.conn + .prepareStatement("SELECT ID, NAME, ADRESS,IVA, CITY_ID , COUNTRY FROM \"COMPANY\""); + final ResultSet rs = stmt.executeQuery()) { + + final List companies = new LinkedList<>(); + + while (rs.next()) { + companies.add(new Company(rs.getLong(1), rs.getString(2), rs.getString(3),rs.getInt(4), + this.cityRepository.findById(rs.getLong(4)),rs.getLong(5))); + } + + return companies; + } catch (SQLException e) { + throw new AppGenericException("Error while querying for all companies", e); + } + } + + @Override + public void deleteById(Long id) { + try (final PreparedStatement stmt = this.conn.prepareStatement("DELETE \"COMPANY\" WHERE ID = ?")) { + + stmt.setLong(1, id); + + stmt.executeUpdate(); + } catch (SQLException e) { + throw new AppGenericException("Error while deleting for COMPANY", e); + } + } + + @Override + public void delete(Company o) { + this.deleteById(o.getId()); + + } + + @Override + public Company findByName(String name) { + try (final PreparedStatement stmt = this.conn + .prepareStatement("SELECT ID, NAME, ADRESS,IVA, CITY_ID , COUNTRY FROM \"COMPANY\" WHERE NAME = ?")) { + + stmt.setString(1, name); + + try (final ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return new Company(rs.getLong(1), rs.getString(2), rs.getString(3),rs.getInt(4), + this.cityRepository.findById(rs.getLong(4)),rs.getLong(5)); + } + } + + return null; + } catch (SQLException e) { + throw new AppGenericException("Error while querying by name for Company", e); + } + } + + + private Company create(Company company) { + try (final PreparedStatement stmt = this.conn + .prepareStatement("INSERT INTO \"COMPANY\" (NAME, ADRESS,IVA, CITY_ID, COUNTRY) VALUES (?, ?, ?,?,?)")) { + + stmt.setString(1,company.getName()); + stmt.setString(2, company.getAdress()); + stmt.setInt(3, company.getIva()); + stmt.setLong(4, company.getCity().getId()); + stmt.setLong(5, company.getCountry()); + + + stmt.executeUpdate(); + + return this.findByName(company.getName()); + } catch (SQLException e) { + throw new AppGenericException("Error while inserting Company", e); + } + } + + private Company update(Company company) { + try (final PreparedStatement stmt = this.conn + .prepareStatement("UPDATE \"COMPANY\" SET NAME = ?, ADRESS = ?,IVA=?, CITY_ID = ? , COUNTRY = ? WHERE ID = ?")) { + + + stmt.setString(1,company.getName()); + stmt.setString(2, company.getAdress()); + stmt.setInt(3, company.getIva()); + stmt.setLong(4, company.getCity().getId()); + stmt.setLong(5, company.getCountry()); + stmt.setLong(6, company.getId()); + + + stmt.executeUpdate(); + + return this.findByName(company.getName()); + } catch (SQLException e) { + throw new AppGenericException("Error while updating Company", e); + } + } + +} diff --git a/src/main/java/com/my_app/repo/impl/UserRepositoryImpl.java b/src/main/java/com/my_app/repo/impl/UserRepositoryImpl.java index 5a8d13d..11fc6cf 100644 --- a/src/main/java/com/my_app/repo/impl/UserRepositoryImpl.java +++ b/src/main/java/com/my_app/repo/impl/UserRepositoryImpl.java @@ -35,11 +35,12 @@ public User save(User user) { private User create(User user) { try (final PreparedStatement stmt = this.conn - .prepareStatement("INSERT INTO \"USER\" (USERNAME, PASSWORD, CITY_ID) VALUES (?, ?, ?)")) { + .prepareStatement("INSERT INTO \"USER\" (USERNAME, PASSWORD, CITY_ID, EMAIL) VALUES (?, ?, ?,?)")) { stmt.setString(1, UserUtils.normalizeUsername(user.getUsername())); stmt.setString(2, user.getPassword()); stmt.setLong(3, user.getCity().getId()); + stmt.setString(4, user.getEmail()); stmt.executeUpdate(); @@ -51,12 +52,13 @@ private User create(User user) { private User update(User user) { try (final PreparedStatement stmt = this.conn - .prepareStatement("UPDATE \"USER\" SET USERNAME = ?, PASSWORD = ?, CITY_ID = ? WHERE ID = ?")) { + .prepareStatement("UPDATE \"USER\" SET USERNAME = ?, PASSWORD = ?, CITY_ID = ? , EMAIL = ? WHERE ID = ?")) { stmt.setString(1, UserUtils.normalizeUsername(user.getUsername())); stmt.setString(2, user.getPassword()); stmt.setLong(3, user.getCity().getId()); - stmt.setLong(4, user.getId()); + stmt.setString(4, user.getEmail()); + stmt.setLong(5, user.getId()); stmt.executeUpdate(); @@ -69,14 +71,14 @@ private User update(User user) { @Override public User findById(Long id) { try (final PreparedStatement stmt = this.conn - .prepareStatement("SELECT ID, USERNAME, PASSWORD, CITY_ID FROM \"USER\" WHERE ID = ?")) { + .prepareStatement("SELECT ID, USERNAME, PASSWORD, CITY_ID, EMAIL FROM \"USER\" WHERE ID = ?")) { stmt.setLong(1, id); try (final ResultSet rs = stmt.executeQuery()) { if (rs.next()) { return new User(rs.getLong(1), rs.getString(2), rs.getString(3), - this.cityRepository.findById(rs.getLong(4))); + this.cityRepository.findById(rs.getLong(4)),rs.getString(5)); } } @@ -89,14 +91,14 @@ public User findById(Long id) { @Override public User findByUsername(String username) { try (final PreparedStatement stmt = this.conn - .prepareStatement("SELECT ID, USERNAME, PASSWORD, CITY_ID FROM \"USER\" WHERE USERNAME = ?")) { + .prepareStatement("SELECT ID, USERNAME, PASSWORD, CITY_ID , EMAIL FROM \"USER\" WHERE USERNAME = ?")) { stmt.setString(1, UserUtils.normalizeUsername(username)); try (final ResultSet rs = stmt.executeQuery()) { if (rs.next()) { return new User(rs.getLong(1), rs.getString(2), rs.getString(3), - this.cityRepository.findById(rs.getLong(4))); + this.cityRepository.findById(rs.getLong(4)),rs.getString(5)); } } @@ -109,14 +111,14 @@ public User findByUsername(String username) { @Override public List findAll() { try (final PreparedStatement stmt = this.conn - .prepareStatement("SELECT ID, USERNAME, PASSWORD, CITY_ID FROM \"USER\""); + .prepareStatement("SELECT ID, USERNAME, PASSWORD, CITY_ID , EMAIL FROM \"USER\""); final ResultSet rs = stmt.executeQuery()) { final List users = new LinkedList<>(); while (rs.next()) { users.add(new User(rs.getLong(1), rs.getString(2), rs.getString(3), - this.cityRepository.findById(rs.getLong(4)))); + this.cityRepository.findById(rs.getLong(4)),rs.getString(5))); } return users; diff --git a/src/main/java/com/my_app/service/CompanyService.java b/src/main/java/com/my_app/service/CompanyService.java new file mode 100644 index 0000000..5f38b38 --- /dev/null +++ b/src/main/java/com/my_app/service/CompanyService.java @@ -0,0 +1,12 @@ +package com.my_app.service; + +import com.my_app.model.Company; + + +public interface CompanyService extends BaseService { + + Company findByName(String name); + + + +} diff --git a/src/main/java/com/my_app/service/factory/CompanyServiceFactory.java b/src/main/java/com/my_app/service/factory/CompanyServiceFactory.java new file mode 100644 index 0000000..6b9104f --- /dev/null +++ b/src/main/java/com/my_app/service/factory/CompanyServiceFactory.java @@ -0,0 +1,21 @@ +package com.my_app.service.factory; + +import java.sql.Connection; + +import com.my_app.repo.impl.CityRepositoryImpl; +import com.my_app.repo.impl.CompanyRepositoryImpl; +import com.my_app.repo.impl.CountryRepositoryImpl; +import com.my_app.service.CompanyService; +import com.my_app.service.UserService; +import com.my_app.service.impl.CompanyServiceImpl; +import com.my_app.service.impl.UserServiceImpl; + +public class CompanyServiceFactory implements ConnectionBasedServiceFactory { + + @Override + public CompanyService create(Connection conn) { + return new CompanyServiceImpl( + new CompanyRepositoryImpl(conn, new CityRepositoryImpl(conn, new CountryRepositoryImpl(conn)))); + } + +} diff --git a/src/main/java/com/my_app/service/impl/CompanyServiceImpl.java b/src/main/java/com/my_app/service/impl/CompanyServiceImpl.java new file mode 100644 index 0000000..dbaf9a8 --- /dev/null +++ b/src/main/java/com/my_app/service/impl/CompanyServiceImpl.java @@ -0,0 +1,51 @@ +package com.my_app.service.impl; + +import java.util.List; + +import com.my_app.model.Company; +import com.my_app.repo.CompanyRepository; +import com.my_app.service.CompanyService; + +public class CompanyServiceImpl implements CompanyService { + + private final CompanyRepository companyRepository; + + public CompanyServiceImpl(CompanyRepository userRepository) { + this.companyRepository = userRepository; + } + + @Override + public Company save(Company o) { + return this.companyRepository.save(o); + } + + @Override + public Company findById(Long id) { + return this.companyRepository.findById(id); + } + + @Override + public List findAll() { + return this.companyRepository.findAll(); + } + + @Override + public void deleteById(Long id) { + this.companyRepository.deleteById(id); + + } + + @Override + public void delete(Company o) { + + this.companyRepository.delete(o); + //this.companyRepository.deleteById(o.getId()); + } + + @Override + public Company findByName(String name) { + return this.companyRepository.findByName(name); + } + + +} diff --git a/src/main/java/com/my_app/utils/UserUtils.java b/src/main/java/com/my_app/utils/UserUtils.java index d163006..59e756e 100644 --- a/src/main/java/com/my_app/utils/UserUtils.java +++ b/src/main/java/com/my_app/utils/UserUtils.java @@ -3,6 +3,9 @@ import static org.apache.commons.lang3.StringUtils.lowerCase; import static org.apache.commons.lang3.StringUtils.replace; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class UserUtils { private UserUtils() { @@ -11,5 +14,17 @@ private UserUtils() { public static String normalizeUsername(String username) { return replace(lowerCase(username), " ", "_"); } + + + public static boolean validaEmailTopLevel(String email) { + + String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; + + Pattern pattern = Pattern.compile(regex); + + Matcher matcher = pattern.matcher(email); + + return matcher.matches() ; + } } diff --git a/src/main/webapp/WEB-INF/struts-config.xml b/src/main/webapp/WEB-INF/struts-config.xml index 12cd624..8cbbec8 100644 --- a/src/main/webapp/WEB-INF/struts-config.xml +++ b/src/main/webapp/WEB-INF/struts-config.xml @@ -6,18 +6,24 @@ + + + + + + - + @@ -31,6 +37,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/company/list.jsp b/src/main/webapp/company/list.jsp new file mode 100644 index 0000000..5abd441 --- /dev/null +++ b/src/main/webapp/company/list.jsp @@ -0,0 +1,79 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%> +<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%> +<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%> + + + + + + +Companies - MyApp + + + + +
+

Companies

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IdNameAdressIvaCityCountryActions
No Companies
${company.id}${company.name}${company.adress}${company.iva}${company.city.name}${company.city.country.name} +
+ +
+
+ New +
+ + + + \ No newline at end of file diff --git a/src/main/webapp/company/save/formcompany.jsp b/src/main/webapp/company/save/formcompany.jsp new file mode 100644 index 0000000..5019fe6 --- /dev/null +++ b/src/main/webapp/company/save/formcompany.jsp @@ -0,0 +1,107 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%> +<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%> +<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%> + + + + + + +Company - MyApp + + + + +
+ +

${form.newUser ? 'New' : 'Update'} Company ${form.newUser ? '' : form.name}

+ + + + + + + + +
+
+
+ + + + + +
+ + +
+ +
+
+ +
+ + +
+ +
+
+ +
+ + +
+ +
+
+ +
+ + +
+ +
+
+ +
+ + +
+ +
+
+ + + Back + + +
+
+
+
+ + + + + + \ No newline at end of file diff --git a/src/main/webapp/company/save/js/formcompany.js b/src/main/webapp/company/save/js/formcompany.js new file mode 100644 index 0000000..e280def --- /dev/null +++ b/src/main/webapp/company/save/js/formcompany.js @@ -0,0 +1,74 @@ +/** + * + */ + +let form = { + $form: $('form#company-save-fome'), + $action: $('input[name="action"]'), + + company: { + $id: $('input#company-id'), + $name: $('input#company-name'), + $adress: $('input#company-adress'), + $country: $('select#company-country'), + $city: $('select#company-city'), + $iva:$('input#company-iva') + }, + + btn: { + $validate: $('button.btn-validate'), + $save: $('button.btn-save') + }, + +} + + +let btnValidateClickHandler = (event) => { + event.preventDefault(); + console.debug('btn validate clicked'); + form.$action.val('validate'); + form.$form.submit(); +} + +let btnSaveClickHandler = (event) => { + event.preventDefault(); + console.debug('btn save clicked'); + form.$action.val('save'); + form.$form.submit(); +} + +let countryChangeHandler = () => { + let $country = form.company.$country; + let $city = form.company.$city; + + $city.prop('disabled', true).val('').empty().append($(``)); + + if ($country.val()) { + let countryId = Number($country.val()); + + cityService.findAllByCountryId(countryId).done((cities) => { + console.debug(cities); + + if (cities != null && cities.length > 0) { + $city.prop('disabled', false); + cities.forEach((city) => { + $city.append($(``)); + }); + } + }) + } +} + +let main = () => { + + form.btn.$validate.on('click', btnValidateClickHandler); + form.btn.$save.on('click', btnSaveClickHandler); + + form.company.$country.on('change', countryChangeHandler) + + console.debug('form.js initialized'); +} + +$(() => { + main(); +}) \ No newline at end of file diff --git a/src/main/webapp/home.jsp b/src/main/webapp/home.jsp index 22922db..2445742 100644 --- a/src/main/webapp/home.jsp +++ b/src/main/webapp/home.jsp @@ -18,11 +18,20 @@

Welcome to MyApp

Choose an area

-
+ + + + + +
diff --git a/src/main/webapp/img/1.jpg b/src/main/webapp/img/1.jpg new file mode 100644 index 0000000..580951e Binary files /dev/null and b/src/main/webapp/img/1.jpg differ diff --git a/src/main/webapp/img/2.jpeg b/src/main/webapp/img/2.jpeg new file mode 100644 index 0000000..38fb909 Binary files /dev/null and b/src/main/webapp/img/2.jpeg differ diff --git a/src/main/webapp/img/3.jpg b/src/main/webapp/img/3.jpg new file mode 100644 index 0000000..78fc2a5 Binary files /dev/null and b/src/main/webapp/img/3.jpg differ diff --git a/src/main/webapp/img/4.jpg b/src/main/webapp/img/4.jpg new file mode 100644 index 0000000..e2d1c75 Binary files /dev/null and b/src/main/webapp/img/4.jpg differ diff --git a/src/main/webapp/incl/header.jsp b/src/main/webapp/incl/header.jsp index 92be318..03d57b8 100644 --- a/src/main/webapp/incl/header.jsp +++ b/src/main/webapp/incl/header.jsp @@ -16,24 +16,57 @@
  • Default
  • With sidebar
  • Full-width fluid
  • - + + + + + + + + + + + +
  • Companies
  • + + +
    -
  • + + +
  • + Login -
  • + + + + +
    diff --git a/src/main/webapp/js/my-app.js b/src/main/webapp/js/my-app.js index 5db7113..0bf15ed 100644 --- a/src/main/webapp/js/my-app.js +++ b/src/main/webapp/js/my-app.js @@ -4,18 +4,18 @@ class MyApp { init() { console.debug('MyApp initialized...'); this.applyLinkClickLoadingSplash(); - } + }; applyLinkClickLoadingSplash() { $('a[href!="#"], button[type="submit"]').click(() => { this.loading(true); }); - } + }; - loading(enable) { + loading (enable) { this.#$loadingSlash.prop('hidden', !(enable | this.#$loadingSlash.is(':hidden'))); - } -} + }; +}; let myApp = new MyApp(); diff --git a/src/main/webapp/logout.jsp b/src/main/webapp/logout.jsp new file mode 100644 index 0000000..f6d5a24 --- /dev/null +++ b/src/main/webapp/logout.jsp @@ -0,0 +1,5 @@ +<%@page import="org.apache.catalina.core.ApplicationContext"%> +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> +<% +response.sendRedirect(application.getContextPath() + "/login.do"); +%> \ No newline at end of file diff --git a/src/main/webapp/private.jsp b/src/main/webapp/private.jsp new file mode 100644 index 0000000..16564bc --- /dev/null +++ b/src/main/webapp/private.jsp @@ -0,0 +1,44 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%> +<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%> + + + + + + +Private Page - MyApp + + + + +
    + +

    Página Privada

    + +
    +
    +

    Panel Privado de Usuario

    +
    +
    Dados de Usuário
    + +
      +
    • Nome: ${username} +
    • +
    • Email: admin@admin.com +
    • +
    + + +
    + +
    + + + + \ No newline at end of file diff --git a/src/main/webapp/public.jsp b/src/main/webapp/public.jsp new file mode 100644 index 0000000..9f49f83 --- /dev/null +++ b/src/main/webapp/public.jsp @@ -0,0 +1,85 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%> +<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%> + + + + + + +Public Page - MyApp + + + + +
    + +
    +

    Página Pública !

    +

    ...

    +

    Learn more

    +
    + +
    + +
    + + + +
    + +
    +
    + ... +
    +

    Thumbnail label

    +

    ...

    +

    Button Button

    +
    +
    +
    + +
    +
    + ... +
    +

    Thumbnail label

    +

    ...

    +

    Button Button

    +
    +
    +
    + + +
    +
    + ... +
    +

    Thumbnail label

    +

    ...

    +

    Button Button

    +
    +
    +
    + +
    +
    + ... +
    +

    Thumbnail label

    +

    ...

    +

    Button Button

    +
    +
    +
    +
    + + + + +
    + + + + \ No newline at end of file diff --git a/src/main/webapp/user/list.jsp b/src/main/webapp/user/list.jsp index 72bded5..cb52882 100644 --- a/src/main/webapp/user/list.jsp +++ b/src/main/webapp/user/list.jsp @@ -39,6 +39,7 @@ Username Country City + Email Actions @@ -56,6 +57,7 @@ ${user.username} ${user.city.country.name} ${user.city.name} + ${user.email}
    diff --git a/src/main/webapp/user/save/form.jsp b/src/main/webapp/user/save/form.jsp index 3a3f1b4..670b9a8 100644 --- a/src/main/webapp/user/save/form.jsp +++ b/src/main/webapp/user/save/form.jsp @@ -81,6 +81,16 @@
    + + + +
    + + +
    + +
    +
    Back diff --git a/src/main/webapp/user/save/js/form.js b/src/main/webapp/user/save/js/form.js index 1585047..e878d0e 100644 --- a/src/main/webapp/user/save/js/form.js +++ b/src/main/webapp/user/save/js/form.js @@ -11,7 +11,8 @@ let form = { $username: $('input#user-username'), $password: $('input#user-password'), $country: $('select#user-country'), - $city: $('select#user-city') + $city: $('select#user-city'), + $email:$('input#user-email') }, btn: {