Skip to content

Completed the exercise #6

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 5 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
26 changes: 13 additions & 13 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?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="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">
<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="output" path="build/classes"/>
</classpath>
<?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.7"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<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="output" path="build/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_BUILDER_ENABLED" value="false"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_DISABLED_BUILDER" value="org.eclipse.wst.validation.validationbuilder"/>
<mapAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS"/>
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
</launchConfiguration>
7 changes: 6 additions & 1 deletion .project
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value>&lt;project&gt;/.externalToolBuilders/org.eclipse.wst.validation.validationbuilder.launch</value>
</dictionary>
</arguments>
</buildCommand>
</buildSpec>
Expand Down
20 changes: 10 additions & 10 deletions .settings/org.eclipse.wst.common.project.facet.core.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="apache-tomcat-7.0.109"/>
<fixed facet="jst.web"/>
<fixed facet="wst.jsdt.web"/>
<fixed facet="java"/>
<installed facet="java" version="1.7"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v7.0"/>
<fixed facet="jst.web"/>
<fixed facet="wst.jsdt.web"/>
<fixed facet="java"/>
<installed facet="java" version="1.7"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
8 changes: 8 additions & 0 deletions .settings/org.eclipse.wst.validation.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DELEGATES_PREFERENCE=delegateValidatorList
USER_BUILD_PREFERENCE=enabledBuildValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator;
USER_MANUAL_PREFERENCE=enabledManualValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator;
USER_PREFERENCE=overrideGlobalPreferencestruedisableAllValidationtrueversion1.3.0.v202308161955
eclipse.preferences.version=1
override=true
suspend=true
vf.version=3
8 changes: 8 additions & 0 deletions src/main/java/MessageResources.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ form.field.pre-choose=Must choose an option for the {0} first

error.common.required=Required
error.password.wrong=Wrong password
error.email.wrong=Email is invalid

user.not-found.error=User not found
user.username-taken.error=Username's already taken
Expand All @@ -12,3 +13,10 @@ user.delete.at-least-one.error=Must keep at least one user
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
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
36 changes: 33 additions & 3 deletions src/main/java/com/my_app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,30 @@
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.CountryRepository;
import com.my_app.repo.UserRepository;
import com.my_app.repo.CompanyRepository;
import com.my_app.repo.impl.CityRepositoryImpl;
import com.my_app.repo.impl.CountryRepositoryImpl;
import com.my_app.repo.impl.UserRepositoryImpl;
import com.my_app.repo.impl.CompanyRepositoryImpl;

public class App {

private static final String CREATE_STATEMENT = ""
+ "CREATE TABLE \"COMPANY\" ("
+ " ID IDENTITY NOT NULL PRIMARY KEY, "
+ " NAME VARCHAR(255) NOT NULL, "
+ " ADDRESS VARCHAR(255) NOT NULL, "
+ " VAT VARCHAR(255), "
+ " CITY_ID BIGINT NOT NULL, "
+ " FOREIGN KEY (CITY_ID) REFERENCES CITY(ID)"
+ ")";

private static App instance;

private final Context context;
Expand Down Expand Up @@ -81,11 +94,28 @@ 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);
}
}

private void initDbCompanies(Connection conn) throws SQLException {
try (final Statement stmt = conn.createStatement()) {
stmt.executeUpdate(CREATE_STATEMENT);
}

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, "Street " + i, "vat" + i, cityRepository.findById((long) this.random.nextInt(49) + 1)));
}

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

private void initDbCountriesAndCities(Connection conn) throws SQLException {
try (final Statement stmt = conn.createStatement()) {
stmt.executeUpdate(
Expand Down Expand Up @@ -115,17 +145,17 @@ 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), 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@example.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 + "@example.com"));
}

Logger.debug("All users created: {}", userRepository.findAll());
Expand Down
86 changes: 86 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,86 @@
package com.my_app.model;

import java.io.Serializable;

public class Company implements Serializable {

private static final long serialVersionUID = 5540957701837095980L;

private Long id;

private String name;

private String address;

private String vat;

private City city;

public Company() {
super();
}

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

public Company(Long id, String name, String address, String vat, City city) {
super();
this.id = id;
this.name = name;
this.address = address;
this.vat = vat;
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 address) {
this.address = address;
}

public String getVat() {
return vat;
}

public void setVat(String vat) {
this.vat = vat;
}

public City getCity() {
return city;
}

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

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

}
17 changes: 14 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;
}

public Long getId() {
Expand Down Expand Up @@ -56,13 +59,21 @@ 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,73 @@
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"));

if (companyService.hasCompanies()) {

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

}
Loading