Skip to content

Qualificação | Projeto GitHub | IEFP - my changes #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/apache-tomcat-7.0.109">
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="lib" path="C:/Program Files (x86)/H2/bin/h2-2.2.224.jar"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
10 changes: 9 additions & 1 deletion src/main/java/MessageResources.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ form.field.pre-choose=Must choose an option for the {0} first

error.common.required=Required
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.email-format.error=Email with incorrect format

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.name-taken.error=Name's already taken
company.not-found.error=Company not found
company.delete.at-least-one.error=Must keep at least one company
company.create.success=Company "{0}" was created successfully
company.update.success=Company "{0}" was update successfully
company.delete.success=Company "{0}" was deleted successfully
32 changes: 29 additions & 3 deletions src/main/java/com/my_app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -81,6 +84,7 @@ private void initDb() {
try (final Connection conn = this.dataSource.getConnection()) {
initDbCountriesAndCities(conn);
initDbUsers(conn);
initDbCompany(conn);
} catch (SQLException e) {
throw new AppGenericException("Error trying to insert initial db data", e);
}
Expand Down Expand Up @@ -115,20 +119,42 @@ 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),"admin@gmail.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+"@gmail.com"));
}

Logger.debug("All users created: {}", userRepository.findAll());
}

/**
*
* @param conn
* @throws SQLException
*/
private void initDbCompany(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, ADDRESS VARCHAR(255) NOT NULL, VATNUMBER VARCHAR(255) NOT NULL, CITY_ID 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);

for (int i = 1; i < 11; i++) {
companyRepository.save(new Company("company" +i , "address company" +i, "vat number"+i, cityRepository.findById((long) this.random.nextInt(49) + 1)));
}

Logger.debug("All companies created: {}", companyRepository.findAll());
}

}
1 change: 0 additions & 1 deletion src/main/java/com/my_app/db/DataSourceFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class DataSourceFactory {

public DataSource create() {
final JdbcDataSource ds = new JdbcDataSource();

ds.setURL("jdbc:h2:mem:my_app;DB_CLOSE_DELAY=-1");
ds.setUser("sa");
ds.setPassword("sa");
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/com/my_app/model/Company.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.my_app.model;

public class Company {
private Long id;
private String name;
private String address;
private String vatNumber;
private City city;

public Company() {
}

public Company( String name,String address, String vatNumber, City city) {
super();
this.name = name;
this.address=address;
this.vatNumber = vatNumber;
this.city = city;
}

public Company(Long id, String name,String address, String vatNumber, City city) {
super();
this.id = id;
this.name = name;
this.address=address;
this.vatNumber = vatNumber;
this.city = city;
}

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 getAddress() {
return address;
}

public void setAddress(String endereco) {
this.address = endereco;
}

public String getVatNumber() {
return vatNumber;
}

public void setVatNumber(String vatNumber) {
this.vatNumber = vatNumber;
}

public City getCity() {
return city;
}

public void setCity(City city) {
this.city = city;
}

public boolean isNew() {
return this.id == null;
}

@Override
public String toString() {
return String.format("Companies [id=%s, name=%s,address=%s, vatNumber=%s, city=%s]", id, name,address, vatNumber, city);
}

}
18 changes: 15 additions & 3 deletions src/main/java/com/my_app/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ public class User {
private String username;
private String password;
private City city;
private String email;

public User() {
}

public User(String username, String password, City city) {
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) {
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;//validarEmail(email);
}

public Long getId() {
Expand Down Expand Up @@ -56,13 +59,22 @@ public void setCity(City city) {
this.city = city;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public boolean isNew() {
return this.id == null;
}

@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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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.service.CompanyService;
import com.my_app.service.factory.CompanyServiceFactory;
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 totalCompanisCount = companyService.findAll().size();

if (totalCompanisCount - 1 != 0) {

final String companyIdStr = req.getParameter("companyId");

if (StringUtils.isNotBlank(companyIdStr)) {

final Long companyId = Long.valueOf(companyIdStr);

final Company company = companyService.findById(companyId);

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");
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/my_app/page/company/list/CompanyListAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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.factory.CompanyServiceFactory;
import com.my_app.utils.LoginUtils;

public class CompanyListAction 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();
}

}
Loading