Skip to content

Patch1 #3

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
6 changes: 1 addition & 5 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,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="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
3 changes: 3 additions & 0 deletions src/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/.idea/libraries/lib.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/java7-struts1-jquery-bootstrap3-web-project.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/main/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lib" level="project" />
</component>
</module>
1 change: 1 addition & 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.common.invalidEmail=Please enter valid email

user.not-found.error=User not found
user.username-taken.error=Username's already taken
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/com/my_app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand All @@ -23,6 +24,8 @@
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.CompanyRepository;
import com.my_app.repo.impl.CompanyRepositoryImpl;

public class App {

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 Down Expand Up @@ -115,20 +119,37 @@ 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, EMAIL 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 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", "admin@admin.com", cityRepository.findById((long) this.random.nextInt(49) + 1)));

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

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

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

}
79 changes: 79 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,79 @@
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 address) {
this.address = address;
}

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("Company [id=%s, name=%s, address=%s, vatNumber=%s, city=%s]", id, name, address, vatNumber, 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 @@ -4,23 +4,26 @@ public class User {
private Long id;
private String username;
private String password;
private String email;
private City city;

public User() {
}

public User(String username, String password, City city) {
public User(String username, String password, String email, City city) {
super();
this.username = username;
this.password = password;
this.email = email;
this.city = city;
}

public User(Long id, String username, String password, City city) {
public User(Long id, String username, String password, String email, City city) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.city = city;
}

Expand All @@ -47,6 +50,14 @@ public String getPassword() {
public void setPassword(String password) {
this.password = password;
}

public String getEmail() {
return email;
}

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

public City getCity() {
return city;
Expand All @@ -62,7 +73,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, email=%s, city=%s]", id, username, password, email, city);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 totalCompaniesCount = companyService.findAll().size();

if (totalCompaniesCount - 1 != 0) {

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

if (StringUtils.isNotBlank(companyIdStr)) {
final Long userId = Long.valueOf(companyIdStr);

final Company company = companyService.findById(userId);

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