Skip to content

Commit

Permalink
Error message display internationalized. CLI update, now works with s…
Browse files Browse the repository at this point in the history
…pring injection
  • Loading branch information
prenaudat committed Feb 9, 2015
1 parent 10ddd95 commit ef79540
Show file tree
Hide file tree
Showing 32 changed files with 336 additions and 331 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ target/
*.jar
*.war
*.ear
c:/*
Empty file.
Empty file removed c:/tmp/rest-demo.log
Empty file.
13 changes: 10 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
<bonecp.version>0.8.0.RELEASE</bonecp.version>
<spring.version>4.1.4.RELEASE</spring.version>
<slf4j.version>1.7.10</slf4j.version>

<commons-validator.version>1.4.1</commons-validator.version>
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version>
<javax-validation.version>1.1.0.Final</javax-validation.version>

</properties>

Expand Down Expand Up @@ -133,12 +135,17 @@
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<version>${javax-validation.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>${commons-validator.version}</version>
</dependency>
</dependencies>
<build>
Expand Down
113 changes: 61 additions & 52 deletions src/main/java/com/excilys/computerdatabase/cli/Client.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package com.excilys.computerdatabase.cli;

import java.time.LocalDate;
import java.util.Locale;
import java.util.Scanner;

import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import org.apache.commons.validator.GenericValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.excilys.computerdatabase.model.Company;
import com.excilys.computerdatabase.model.Computer;
import com.excilys.computerdatabase.pagination.Page;
import com.excilys.computerdatabase.service.impl.CompanyDBService;
import com.excilys.computerdatabase.service.impl.ComputerDBService;
import com.excilys.computerdatabase.validator.Validator;
import com.excilys.computerdatabase.service.impl.CompanyDBServiceImpl;
import com.excilys.computerdatabase.service.impl.ComputerDBServiceImpl;

/**
* @author excilys
*
*/
public class Client {
//Database Services
private ComputerDBService computerDBService;
private CompanyDBService companyDBService;
//Page index for Company and Computer Directory
// Database Services
private ComputerDBServiceImpl computerDBService;
private CompanyDBServiceImpl companyDBService;
// Page index for Company and Computer Directory
Page page;
private Boolean loop;
private Scanner sc;

private static final String FAIL = "You failed to select an available option, please try again";
private static final String MAIN_MENU = "A) List computers \nB) List companies \nC) Detailed computer view \nD) Create a computer \nE) Update a computer \nF) Delete a computer \nG) Change page size \nH) Delete Company+Computers";
//Logger for this class

// Logger for this class

/**
* Display page of Computers and calls Computer Menu
Expand All @@ -35,8 +39,7 @@ public class Client {
public void getComputerList() {
Boolean innerLoop = true;
while (innerLoop) {
System.out.println(computerDBService.getPage(
page.getPageNumber()));
System.out.println(computerDBService.getPage(page.getPageNumber()));
innerLoop = getComputerListMenu();
}
}
Expand All @@ -50,7 +53,7 @@ public void getComputerById() {
while (detailedLoop) {
System.out.println("Id of computer to visualize?");
final String id = sc.nextLine();
detailedLoop = !Validator.isValidNumber(id);
detailedLoop = GenericValidator.isLong(id);
if (!detailedLoop) {
System.out.println(computerDBService.get(Integer.parseInt(id)));
detailedLoop = getComputerMenu(Integer.parseInt(id));
Expand All @@ -69,9 +72,9 @@ public void deleteComputer() {
System.out
.println("Enter id of computer would you like to delete?");
id = sc.nextLine();
deleteLoop = !Validator.isValidNumber(id);
deleteLoop = GenericValidator.isLong(id);
}
computerDBService.remove(Long.parseLong(id));
computerDBService.remove(id);
}

/**
Expand All @@ -84,8 +87,8 @@ public void updateComputer() {
case "a":
Boolean displayLoop = true;
while (displayLoop) {
System.out.println(computerDBService.getPage(
page.getPageNumber()));
System.out.println(computerDBService.getPage(page
.getPageNumber()));
displayLoop = getComputerListMenu();
}
break;
Expand All @@ -95,15 +98,15 @@ public void updateComputer() {
while (idValidation) {
System.out.println("Id of computer to update?");
input = sc.nextLine();
idValidation = !Validator.isValidNumber(input);
idValidation = GenericValidator.isLong(input);
}
Boolean nameLoop = true;
Computer c = computerDBService.get(Long.parseLong(input));
while (nameLoop) {
System.out.println("Current name is : " + c.getName());
System.out.println("New name?");
String name = sc.nextLine();
nameLoop = !Validator.isValidString(name);
nameLoop = !GenericValidator.minLength(name, 1);
if (!nameLoop) {
c.setName(name);
}
Expand All @@ -115,7 +118,8 @@ public void updateComputer() {
System.out
.println("New introduction year? Format : YYYY-MM-DD");
String introduced = sc.nextLine();
introducedLoop = !Validator.isValidDate(introduced);
introducedLoop = !GenericValidator.isDate(introduced,
Locale.FRANCE);
if (!introducedLoop) {
c.setIntroduced(LocalDate.parse(introduced));
}
Expand All @@ -127,7 +131,8 @@ public void updateComputer() {
System.out
.println("New introduction year? Format : YYYY-MM-DD");
String discontinued = sc.nextLine();
discontinuedLoop = !Validator.isValidDate(discontinued);
discontinuedLoop = !GenericValidator.isDate(discontinued,
Locale.FRANCE);
if (!discontinuedLoop) {
c.setDiscontinued(LocalDate.parse(discontinued));
}
Expand All @@ -136,10 +141,10 @@ public void updateComputer() {
while (companyLoop) {
System.out.println("new company ID?");
String id = sc.nextLine();
companyLoop = !Validator.isValidNumber(id);
companyLoop = !GenericValidator.isLong(id);
if (!companyLoop) {
c.setCompany(new Company.CompanyBuilder().id(
Long.parseLong(id)).build());
c.setCompany(new Company.Builder().id(Long.parseLong(id))
.build());
}
}
computerDBService.update(c);
Expand All @@ -148,9 +153,8 @@ public void updateComputer() {

}


/**
*Create computer
* Create computer
*
*/
public void createComputer() {
Expand All @@ -159,7 +163,7 @@ public void createComputer() {
while (nameLoop) {
System.out.println("New name?");
String name = sc.nextLine();
nameLoop = !Validator.isValidString(name);
nameLoop = !GenericValidator.minLength(name, 1);
if (!nameLoop) {
c.name(name);
}
Expand All @@ -168,7 +172,8 @@ public void createComputer() {
while (introducedLoop) {
System.out.println("Introduction year? Format : YYYY-MM-DD");
String introduced = sc.nextLine();
introducedLoop = !Validator.isValidDate(introduced);
introducedLoop = !GenericValidator
.isDate(introduced, Locale.FRANCE);
if (!introducedLoop) {
c.introduced(LocalDate.parse(introduced));
}
Expand All @@ -177,7 +182,8 @@ public void createComputer() {
while (discontinuedLoop) {
System.out.println("Discontinuation year? Format : YYYY-MM-DD");
String discontinued = sc.nextLine();
discontinuedLoop = !Validator.isValidDate(discontinued);
discontinuedLoop = !GenericValidator.isDate(discontinued,
Locale.FRANCE);
if (!discontinuedLoop) {
c.discontinued(LocalDate.parse(discontinued));
}
Expand All @@ -186,18 +192,17 @@ public void createComputer() {
while (companyLoop) {
System.out.println("Company ID?");
String id = sc.nextLine();
companyLoop = !Validator.isValidNumber(id);
companyLoop = !GenericValidator.isLong(id);
if (!companyLoop) {
c.company(new Company.CompanyBuilder().id(Long.parseLong(id))
.build());
c.company(new Company.Builder().id(Long.parseLong(id)).build());
}
}
computerDBService.save(c.build());
}


/**
* Displays menu after a computer is displayed
*
* @param index
* @return
*/
Expand Down Expand Up @@ -228,9 +233,10 @@ private Boolean getComputerMenu(int index) {
return outerLoop;
}


/**
* Displays menu after displaying computer List. User can change pages or return to previous menu
* Displays menu after displaying computer List. User can change pages or
* return to previous menu
*
* @return
*/
private Boolean getComputerListMenu() {
Expand All @@ -248,13 +254,13 @@ private Boolean getComputerListMenu() {
page.setPageNumber(0);
break;
case "b":
page.setPageNumber(page.getPageNumber()+1);
page.setPageNumber(page.getPageNumber() + 1);
outerLoop = true;
innerLoop = false;
break;
case "c":
if (page.getPageNumber() > page.getSize()) {
page.setPageNumber(page.getPageNumber()-1);
page.setPageNumber(page.getPageNumber() - 1);
outerLoop = true;
innerLoop = false;
} else {
Expand All @@ -273,7 +279,6 @@ private Boolean getComputerListMenu() {
return outerLoop;
}


/**
* Returns list of companies
*
Expand Down Expand Up @@ -303,13 +308,13 @@ private Boolean getCompanyListMenu(int index) {
page.setPageNumber(0);
break;
case "b":
page.setPageNumber(page.getPageNumber()+1);
page.setPageNumber(page.getPageNumber() + 1);
innerLoop = false;
outerLoop = true;
break;
case "c":
if (page.getPageNumber() > page.getSize()) {
page.setPageNumber(page.getPageNumber()- page.getSize());
page.setPageNumber(page.getPageNumber() - page.getSize());
innerLoop = false;
outerLoop = true;
} else {
Expand All @@ -326,7 +331,6 @@ private Boolean getCompanyListMenu(int index) {
return outerLoop;
}


/**
* Display page size and modify.size
*/
Expand All @@ -336,7 +340,7 @@ public void changePageSize() {
System.out.println("Current page size is " + page.getSize()
+ ".\nChange page size to :");
String input = sc.nextLine();
pageSizeLoop = !Validator.isValidNumber(input);
pageSizeLoop = !GenericValidator.isInt(input);
if (!pageSizeLoop) {
page.setSize(Integer.parseInt(input));
System.out.printf("Page size changed to %d \n", page.getSize());
Expand All @@ -352,28 +356,32 @@ public void deleteCompanyAndComputers() {
while (massDelete) {
System.out.println("Please select ID of company to delete ");
String input = sc.nextLine();
massDelete = !Validator.isValidNumber(input);
massDelete = !GenericValidator.isLong(input);
if (!massDelete) {
computerDBService.remove(Long.parseLong(input));
computerDBService.remove(input);
}
}
}

/**
*Instantiate DBservices, associated counters
* Instantiate DBservices, associated counters
*/
public void init() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext (this);
computerDBService = new ComputerDBService();
companyDBService = new CompanyDBService();
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"context.xml");
computerDBService = (ComputerDBServiceImpl) context
.getBean("computerService");
companyDBService = (CompanyDBServiceImpl) context
.getBean("companyService");
sc = new Scanner(System.in);
loop = true;
page = new Page();
mainMenu();
context.close();
}


/**
* Display main menu loop
* Display main menu loop
*/
public void mainMenu() {
while (loop) {
Expand Down Expand Up @@ -418,13 +426,14 @@ public void fail() {
}

/**
* Execution start point: Instantiate Client and start it.
* @param args
* Execution start point: Instantiate Client and start it.
*
* @param args
*/
public static void main(final String[] args) {
Client client = new Client();
client.init();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
import org.springframework.web.servlet.view.RedirectView;

import com.excilys.computerdatabase.dto.ComputerDTO;
import com.excilys.computerdatabase.mapper.dto.impl.ComputerDTOMapper;
import com.excilys.computerdatabase.mapper.dto.impl.ComputerDTOMapperImpl;
import com.excilys.computerdatabase.model.Company;
import com.excilys.computerdatabase.model.Computer;
import com.excilys.computerdatabase.service.impl.CompanyDBService;
import com.excilys.computerdatabase.service.impl.ComputerDBService;
import com.excilys.computerdatabase.service.impl.CompanyDBServiceImpl;
import com.excilys.computerdatabase.service.impl.ComputerDBServiceImpl;
import com.excilys.computerdatabase.validator.ComputerDTOValidator;
import com.excilys.computerdatabase.validator.Validator;

/**
* Manage /addComputer Display and persist computer for adding
Expand All @@ -34,10 +33,10 @@
@Controller
public class AddComputer {
@Autowired
ComputerDBService computerDBService;
ComputerDBServiceImpl computerDBService;
@Autowired
CompanyDBService companyDBService;
ComputerDTOMapper computerDTOMapper = new ComputerDTOMapper();
CompanyDBServiceImpl companyDBService;
ComputerDTOMapperImpl computerDTOMapper = new ComputerDTOMapperImpl();

@InitBinder("computerDTO")
protected void initComputerDTOBinder(WebDataBinder binder) {
Expand Down
Loading

0 comments on commit ef79540

Please sign in to comment.