GeezerVison is showing github.com — hotfix · vgalloy/computer-database@70a5de8
File treeExpand file treeCollapse file tree Open diff view settings binding/src/main/java/com/excilys/computerdatabase/dto java/com/excilys/computerdatabase/persistence/dao
service/src/main/java/com/excilys/computerdatabase/service/services java/com/excilys/computerdatabase
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: binding/src/main/java/com/excilys/computerdatabase/dto/model/ComputerDto.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,11 +25,11 @@ public class ComputerDto implements Serializable { | 25 | 25 | private String name; | 26 | 26 |
| 27 | 27 | /** The introduced. */ | 28 | | - @Date(message = "{error.invalid.introduced.date} {date.format}") | | 28 | + @Date(message = "{error.invalid.introduced.date} {date.condition}") | 29 | 29 | private String introduced; | 30 | 30 |
| 31 | 31 | /** The discontinued. */ | 32 | | - @Date(message = "{error.invalid.discontined.date} {date.format}") | | 32 | + @Date(message = "{error.invalid.discontined.date} {date.condition}") | 33 | 33 | private String discontinued; | 34 | 34 |
| 35 | 35 | /** The company id. */ | |
Collapse file Copy file name to clipboardExpand all lines: binding/src/main/java/com/excilys/computerdatabase/dto/validator/DateValidator.java +30-16Lines changed: 30 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | */ | 5 | 5 | package com.excilys.computerdatabase.dto.validator; | 6 | 6 |
| | 7 | +import java.time.LocalDateTime; | | 8 | +import java.time.format.DateTimeFormatter; | 7 | 9 | import java.util.Locale; | 8 | 10 |
| 9 | 11 | import javax.validation.ConstraintValidator; | | 19 | 21 | */ | 20 | 22 | @Component | 21 | 23 | public class DateValidator implements ConstraintValidator<Date, String> { | 22 | | - | | 24 | + | 23 | 25 | /** The message source. */ | 24 | 26 | @Autowired | 25 | 27 | private MessageSource messageSource; | 26 | 28 |
| 27 | | - /* (non-Javadoc) | 28 | | - * @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation) | | 29 | + /* | | 30 | + * (non-Javadoc) | | 31 | + * | | 32 | + * @see | | 33 | + * javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation | | 34 | + * ) | 29 | 35 | */ | 30 | 36 | @Override | 31 | 37 | public void initialize(Date constraintAnnotation) { | 32 | 38 | } | 33 | 39 |
| 34 | | - /* (non-Javadoc) | 35 | | - * @see javax.validation.ConstraintValidator#isValid(java.lang.Object, javax.validation.ConstraintValidatorContext) | | 40 | + /* | | 41 | + * (non-Javadoc) | | 42 | + * | | 43 | + * @see javax.validation.ConstraintValidator#isValid(java.lang.Object, | | 44 | + * javax.validation.ConstraintValidatorContext) | 36 | 45 | */ | 37 | 46 | @Override | 38 | 47 | public boolean isValid(String value, ConstraintValidatorContext context) { | @@ -42,23 +51,29 @@ public boolean isValid(String value, ConstraintValidatorContext context) { | 42 | 51 | if (value.isEmpty()) { | 43 | 52 | return true; | 44 | 53 | } | 45 | | - if (isCorrectDate(value, getPattern())) { | 46 | | - return true; | 47 | | - } | 48 | | - return false; | | 54 | + return isCorrectDate(value, getPattern()); | 49 | 55 | } | 50 | 56 |
| 51 | 57 | /** | 52 | 58 | * Checks if is correct date. | 53 | 59 | * | 54 | | - * @param date the date | 55 | | - * @param pattern the pattern | | 60 | + * @param date | | 61 | + * the date | | 62 | + * @param pattern | | 63 | + * the pattern | 56 | 64 | * @return true, if is correct date | 57 | 65 | */ | 58 | 66 | private boolean isCorrectDate(String date, String pattern) { | 59 | | - return org.apache.commons.validator.routines.DateValidator.getInstance().isValid(date, pattern); | | 67 | + if (org.apache.commons.validator.routines.DateValidator.getInstance().isValid(date, pattern)) { | | 68 | + LocalDateTime localDateTime = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(pattern)); | | 69 | + LocalDateTime localDateTimeAfter = LocalDateTime.parse("1970-01-01 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | | 70 | + LocalDateTime localDateTimeBefore = LocalDateTime.parse("2038-01-01 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | | 71 | + return localDateTime.isAfter(localDateTimeAfter) && localDateTime.isBefore(localDateTimeBefore); | | 72 | + } else { | | 73 | + return false; | | 74 | + } | 60 | 75 | } | 61 | | - | | 76 | + | 62 | 77 | /** | 63 | 78 | * Gets the pattern. | 64 | 79 | * | @@ -67,10 +82,9 @@ private boolean isCorrectDate(String date, String pattern) { | 67 | 82 | private String getPattern() { | 68 | 83 | String str; | 69 | 84 | Locale userLocale = LocaleContextHolder.getLocale(); | 70 | | - if(messageSource != null) { | | 85 | + if (messageSource != null) { | 71 | 86 | str = messageSource.getMessage("date.pattern", null, userLocale); | 72 | | - } | 73 | | - else { | | 87 | + } else { | 74 | 88 | str = "yyyy-MM-dd HH:mm:ss"; | 75 | 89 | } | 76 | 90 | return str; | |
Collapse file Copy file name to clipboardExpand all lines: persistence/src/main/java/com/excilys/computerdatabase/persistence/dao/CommonDao.java +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,9 +26,7 @@ public interface CommonDao<T> { | 26 | 26 | * | 27 | 27 | * @param t the t | 28 | 28 | */ | 29 | | - public default void update(T t) { | 30 | | - throw new UnsupportedOperationException(); | 31 | | - } | | 29 | + public void update(T t); | 32 | 30 |
| 33 | 31 | /** | 34 | 32 | * Gets the all. | @@ -59,7 +57,7 @@ public default void update(T t) { | 59 | 57 | * Gets the by id. | 60 | 58 | * | 61 | 59 | * @param id the id | 62 | | - * @return the by id | | 60 | + * @return the object by id | 63 | 61 | */ | 64 | 62 | public T getById(Long id); | 65 | 63 |
| |
Collapse file Copy file name to clipboardExpand all lines: persistence/src/main/java/com/excilys/computerdatabase/persistence/dao/impl/CompanyDaoImpl.java +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ public class CompanyDaoImpl implements CompanyDao { | 29 | 29 |
| 30 | 30 | @Override | 31 | 31 | public void create(Company t) { | | 32 | + log.info("Company update : {}", t); | 32 | 33 | Session session = sf.getCurrentSession(); | 33 | 34 | if (!Validator.isCompanyCorrect(t)) { | 34 | 35 | log.warn(Validator.INVALID_COMPANY); | @@ -40,12 +41,14 @@ public void create(Company t) { | 40 | 41 |
| 41 | 42 | @Override | 42 | 43 | public List<Company> getAll(SortCriteria sortCriteria) { | | 44 | + log.info("Company getAll"); | 43 | 45 | Session session = sf.getCurrentSession(); | 44 | 46 | return session.createCriteria(Company.class).addOrder(Order.asc("name")).list(); | 45 | 47 | } | 46 | 48 |
| 47 | 49 | @Override | 48 | 50 | public List<Company> getAll(Long from, Long to, SortCriteria sortCriteria) { | | 51 | + log.info("Company update : {} to {}", from , to); | 49 | 52 | Session session = sf.getCurrentSession(); | 50 | 53 | if (!Validator.isDateFromToCorrect(from, to)) { | 51 | 54 | log.warn(Validator.INVALID_BOUND); | @@ -59,6 +62,7 @@ public List<Company> getAll(Long from, Long to, SortCriteria sortCriteria) { | 59 | 62 |
| 60 | 63 | @Override | 61 | 64 | public Long getNumberOfElement() { | | 65 | + log.info("Company getNumberOfElement"); | 62 | 66 | Session session = sf.getCurrentSession(); | 63 | 67 | return (Long) session.createCriteria(Company.class) | 64 | 68 | .setProjection(Projections.rowCount()) | @@ -68,6 +72,7 @@ public Long getNumberOfElement() { | 68 | 72 |
| 69 | 73 | @Override | 70 | 74 | public void delete(Long id) { | | 75 | + log.info("Company delete : {}", id); | 71 | 76 | Session session = sf.getCurrentSession(); | 72 | 77 | if (!Validator.isIdCorrect(id)) { | 73 | 78 | log.warn(Validator.INVALID_COMPANY_ID); | @@ -82,6 +87,7 @@ public void delete(Long id) { | 82 | 87 |
| 83 | 88 | @Override | 84 | 89 | public Company getById(Long id) { | | 90 | + log.info("Company getById : {}", id); | 85 | 91 | Session session = sf.getCurrentSession(); | 86 | 92 | if (!Validator.isIdCorrect(id)) { | 87 | 93 | log.warn(Validator.INVALID_COMPANY_ID); | @@ -90,4 +96,15 @@ public Company getById(Long id) { | 90 | 96 | return (Company) session.get(Company.class, id); | 91 | 97 | } | 92 | 98 |
| | 99 | + @Override | | 100 | + public void update(Company t) { | | 101 | + log.info("Company update : {}", t); | | 102 | + Session session = sf.getCurrentSession(); | | 103 | + if (!Validator.isCompanyCorrect(t)) { | | 104 | + log.warn(Validator.INVALID_COMPANY); | | 105 | + throw new DaoException(Validator.INVALID_COMPANY); | | 106 | + } | | 107 | + session.merge(t); | | 108 | + } | | 109 | + | 93 | 110 | } |
Collapse file Copy file name to clipboardExpand all lines: persistence/src/main/resources/script/5-TEST_DB.sql +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,15 +36,17 @@ constraint pk_authorities primary key (user_role_id)); | 36 | 36 | alter table computer add constraint fk_computer_company_1 foreign key (company_id) references company (id) on delete restrict on update restrict; | 37 | 37 | create index ix_computer_company_1 on computer (company_id); | 38 | 38 |
| | 39 | +alter table authorities add constraint fk_authorities_users foreign key(username) references users(username) on delete CASCADE on update restrict; | | 40 | +create index ix_role_username on authorities (username,role); | 39 | 41 |
| 40 | | -INSERT INTO users(username,password,enabled) VALUES ('admin','$2a$10$cwXZKDV44c3cFl0ac0QU0OyIMMkSuUC9gEifqDuwzmc8QSAVbhatS', TRUE); | 41 | | -INSERT INTO users(username,password,enabled) VALUES ('user','$2a$10$H0eNB7HcZI0V049EetfGxu4hK07vZLCkaZkcQOv6/aZCGLI7TFSQW', TRUE); | | 42 | +#INSERT INTO users(username,password,enabled) VALUES ('admin','$2a$10$cwXZKDV44c3cFl0ac0QU0OyIMMkSuUC9gEifqDuwzmc8QSAVbhatS', TRUE); | | 43 | +#INSERT INTO users(username,password,enabled) VALUES ('user','$2a$10$H0eNB7HcZI0V049EetfGxu4hK07vZLCkaZkcQOv6/aZCGLI7TFSQW', TRUE); | 42 | 44 | INSERT INTO users(username,password,enabled) VALUES ('all','$2a$10$UA.m4MEeB5dFfwtZZO.0B.a.dLJuzuB05gclWOjS/a9UiZ8dZI/2S', TRUE); | 43 | 45 |
| 44 | 46 |
| 45 | | -INSERT INTO authorities (username, role) VALUES ('admin', 'ROLE_USER'); | 46 | | -INSERT INTO authorities (username, role) VALUES ('admin', 'ROLE_ADMIN'); | 47 | | -INSERT INTO authorities (username, role) VALUES ('user', 'ROLE_USER'); | | 47 | +#INSERT INTO authorities (username, role) VALUES ('admin', 'ROLE_USER'); | | 48 | +#INSERT INTO authorities (username, role) VALUES ('admin', 'ROLE_ADMIN'); | | 49 | +#INSERT INTO authorities (username, role) VALUES ('user', 'ROLE_USER'); | 48 | 50 | INSERT INTO authorities (username, role) VALUES ('all', 'ROLE_USER'); | 49 | 51 | INSERT INTO authorities (username, role) VALUES ('all', 'ROLE_ADMIN'); | 50 | 52 | INSERT INTO authorities (username, role) VALUES ('all', 'ROLE_SUPER_ADMIN'); | |
Collapse file Copy file name to clipboardExpand all lines: service/src/main/java/com/excilys/computerdatabase/service/services/CommonService.java +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -79,9 +79,7 @@ public default List<T> list(Long from, Long to) { | 79 | 79 | * | 80 | 80 | * @param t the t | 81 | 81 | */ | 82 | | - public default void update(T t) { | 83 | | - throw new UnsupportedOperationException(); | 84 | | - } | | 82 | + public void update(T t); | 85 | 83 |
| 86 | 84 | /** | 87 | 85 | * Delete. | |
Collapse file Copy file name to clipboardExpand all lines: service/src/main/java/com/excilys/computerdatabase/service/services/impl/CompanyServiceImpl.java +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -123,6 +123,16 @@ private void deleteCompany(Long id) { | 123 | 123 | companyDao.delete(id); | 124 | 124 | } | 125 | 125 |
| | 126 | + @Override | | 127 | + @Transactional(readOnly=false) | | 128 | + public void update(Company t) { | | 129 | + log.info("Company update : {}", t); | | 130 | + if (!Validator.isCompanyCorrect(t)) { | | 131 | + throw new ServiceException(Validator.INVALID_COMPANY); | | 132 | + } | | 133 | + companyDao.update(t); | | 134 | + } | | 135 | + | 126 | 136 | /** | 127 | 137 | * Gets the company dao. | 128 | 138 | * | |
Collapse file Copy file name to clipboardExpand all lines: webapp/src/main/java/com/excilys/computerdatabase/controler/computer/ComputerAdd.java +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | */ | 4 | 4 | package com.excilys.computerdatabase.controler.computer; | 5 | 5 |
| | 6 | +import java.util.Locale; | | 7 | + | 6 | 8 | import org.slf4j.Logger; | 7 | 9 | import org.slf4j.LoggerFactory; | 8 | 10 | import org.springframework.beans.factory.annotation.Autowired; | | 11 | +import org.springframework.context.i18n.LocaleContextHolder; | 9 | 12 | import org.springframework.stereotype.Controller; | 10 | 13 | import org.springframework.ui.Model; | 11 | 14 | import org.springframework.web.bind.annotation.RequestMapping; | 12 | 15 | import org.springframework.web.bind.annotation.RequestMethod; | 13 | 16 | import org.springframework.web.bind.annotation.SessionAttributes; | 14 | 17 |
| 15 | 18 | import com.excilys.computerdatabase.controler.AbstractController; | | 19 | +import com.excilys.computerdatabase.page.creator.AbstractPageCreator; | 16 | 20 | import com.excilys.computerdatabase.page.creator.computer.AddPageCreator; | 17 | 21 | import com.excilys.computerdatabase.page.model.ComputerPage; | 18 | 22 | import com.excilys.computerdatabase.session.AddComputerSession; | @@ -49,6 +53,7 @@ private String getAddComputerPage(Model model) { | 49 | 53 | ComputerPage page = addPageCreator.getPageFromGetRequest(); | 50 | 54 | if (addComputerSession.getComputerDto() != null) { | 51 | 55 | page.setComputerDto(addComputerSession.getComputerDto()); | | 56 | + AbstractPageCreator.pageConverter(page, Locale.ENGLISH, LocaleContextHolder.getLocaleContext().getLocale()); | 52 | 57 | model.addAttribute("org.springframework.validation.BindingResult.addComputerDto", addComputerSession.getBindingResult()); | 53 | 58 | } | 54 | 59 | model.addAttribute("page", page); | |
Collapse file Copy file name to clipboardExpand all lines: webapp/src/main/java/com/excilys/computerdatabase/controler/computer/ComputerControler.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -81,14 +81,14 @@ public String addComputer(@Valid @ModelAttribute("addComputerDto") ComputerDto c | 81 | 81 | log.info("Servlet : [POST] computer-add {}", computerDto); | 82 | 82 |
| 83 | 83 | ComputerPage page = addPageCreator.getPageFromPostRequest(computerDto); | 84 | | - addComputerSession.setComputerDto(page.getComputerDto()); | 85 | | - addComputerSession.setBindingResult(bindingResult); | 86 | 84 |
| 87 | 85 | if (bindingResult.hasErrors()) { | 88 | 86 | log.info("Wrong input"); | 89 | 87 | return REDIRECT + COMPUTER + VIEW + ADD; | 90 | 88 | } else { | 91 | 89 | AbstractPageCreator.pageConverter(page, LocaleContextHolder.getLocaleContext().getLocale(), Locale.ENGLISH); | | 90 | + addComputerSession.setComputerDto(page.getComputerDto()); | | 91 | + addComputerSession.setBindingResult(bindingResult); | 92 | 92 | Computer computer = computerDtoMapper.mapToModel(page.getComputerDto()); | 93 | 93 | computerService.create(computer); | 94 | 94 | log.info("Computer added : " + computer); | |
Collapse file Copy file name to clipboardExpand all lines: webapp/src/main/java/com/excilys/computerdatabase/controler/global/GlobalErrorHandler.java +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | */ | 5 | 5 | package com.excilys.computerdatabase.controler.global; | 6 | 6 |
| | 7 | +import org.springframework.beans.factory.annotation.Autowired; | 7 | 8 | import org.springframework.http.HttpStatus; | 8 | 9 | import org.springframework.web.bind.annotation.ControllerAdvice; | 9 | 10 | import org.springframework.web.bind.annotation.ExceptionHandler; | 10 | 11 | import org.springframework.web.bind.annotation.ResponseStatus; | 11 | 12 | import org.springframework.web.servlet.ModelAndView; | 12 | 13 |
| | 14 | +import com.excilys.computerdatabase.session.AddComputerSession; | | 15 | + | 13 | 16 | /** | 14 | 17 | * The Class GlobalErrorHandler. | 15 | 18 | */ | 16 | 19 | @ControllerAdvice | 17 | 20 | public class GlobalErrorHandler { | 18 | | - | | 21 | + @Autowired | | 22 | + private AddComputerSession addComputerSession; | 19 | 23 | /** | 20 | 24 | * Handler. | 21 | 25 | * | @@ -25,6 +29,9 @@ public class GlobalErrorHandler { | 25 | 29 | @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | 26 | 30 | @ExceptionHandler(Exception.class) | 27 | 31 | public ModelAndView handler(Exception e) { | | 32 | + e.printStackTrace(); | | 33 | + addComputerSession.setComputerDto(null); | | 34 | + addComputerSession.setBindingResult(null); | 28 | 35 | ModelAndView modelAndView = new ModelAndView("500"); | 29 | 36 | modelAndView.addObject("exception", e); | 30 | 37 | return modelAndView; | |
|