Skip to content

Windson Melo #5

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 2 commits 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
12 changes: 12 additions & 0 deletions src/main/java/MessageResources.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

40 changes: 37 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);
initDbCompanies(conn);
} catch (SQLException e) {
throw new AppGenericException("Error trying to insert initial db data", e);
}
Expand All @@ -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);
Expand All @@ -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());
}


}
127 changes: 127 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,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);
}

}
38 changes: 35 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,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() {
Expand Down Expand Up @@ -55,14 +75,26 @@ 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;
}

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

}
Loading