From e3922d1b98db8b7e9faa8f51eb11560c3d744e73 Mon Sep 17 00:00:00 2001 From: Kala09 <59877518+Kala09@users.noreply.github.com> Date: Fri, 5 Jun 2020 15:09:45 +0530 Subject: [PATCH] Added src code --- pom.xml | 104 ++++++++++++++++++ .../healthTracker/Application.java | 34 ++++++ .../controller/ErrorController.java | 24 ++++ .../controller/LogoutController.java | 30 +++++ .../controller/PatientController.java | 80 ++++++++++++++ .../controller/WelcomeController.java | 30 +++++ .../healthTracker/model/Disease.java | 48 ++++++++ .../healthTracker/model/Patient.java | 99 +++++++++++++++++ .../repository/PatientRepository.java | 10 ++ .../security/SecurityConfiguration.java | 30 +++++ .../healthTracker/service/PatientService.java | 39 +++++++ src/main/resources/application.properties | 2 + src/main/webapp/WEB-INF/jsp/bar.jsp | 46 ++++++++ .../webapp/WEB-INF/jsp/common/footer.jspf | 19 ++++ .../webapp/WEB-INF/jsp/common/header.jspf | 27 +++++ .../webapp/WEB-INF/jsp/common/navigation.jspf | 16 +++ src/main/webapp/WEB-INF/jsp/edit-patient.jsp | 50 +++++++++ src/main/webapp/WEB-INF/jsp/error.jsp | 6 + src/main/webapp/WEB-INF/jsp/new-patient.jsp | 49 +++++++++ src/main/webapp/WEB-INF/jsp/patients.jsp | 53 +++++++++ src/main/webapp/WEB-INF/jsp/welcome.jsp | 13 +++ 21 files changed, 809 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/quicksilver/healthTracker/Application.java create mode 100644 src/main/java/com/quicksilver/healthTracker/controller/ErrorController.java create mode 100644 src/main/java/com/quicksilver/healthTracker/controller/LogoutController.java create mode 100644 src/main/java/com/quicksilver/healthTracker/controller/PatientController.java create mode 100644 src/main/java/com/quicksilver/healthTracker/controller/WelcomeController.java create mode 100644 src/main/java/com/quicksilver/healthTracker/model/Disease.java create mode 100644 src/main/java/com/quicksilver/healthTracker/model/Patient.java create mode 100644 src/main/java/com/quicksilver/healthTracker/repository/PatientRepository.java create mode 100644 src/main/java/com/quicksilver/healthTracker/security/SecurityConfiguration.java create mode 100644 src/main/java/com/quicksilver/healthTracker/service/PatientService.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/webapp/WEB-INF/jsp/bar.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/common/footer.jspf create mode 100644 src/main/webapp/WEB-INF/jsp/common/header.jspf create mode 100644 src/main/webapp/WEB-INF/jsp/common/navigation.jspf create mode 100644 src/main/webapp/WEB-INF/jsp/edit-patient.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/error.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/new-patient.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/patients.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/welcome.jsp diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..432e278 --- /dev/null +++ b/pom.xml @@ -0,0 +1,104 @@ + + + 4.0.0 + + com.quicksilver.healthTracker + Health-Tracker + 1.0.0-SNAPSHOT + war + + HealthTracker + + + org.springframework.boot + spring-boot-starter-parent + 2.1.7.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + javax.servlet + jstl + + + + org.webjars + bootstrap + 3.3.6 + + + + org.webjars + bootstrap-datepicker + 1.0.1 + + + + org.webjars + jquery + 1.9.1 + + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + org.apache.tomcat.embed + tomcat-embed-jasper + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + runtime + + + com.h2database + h2 + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/src/main/java/com/quicksilver/healthTracker/Application.java b/src/main/java/com/quicksilver/healthTracker/Application.java new file mode 100644 index 0000000..470e9d3 --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/Application.java @@ -0,0 +1,34 @@ +package com.quicksilver.healthTracker; + +import com.quicksilver.healthTracker.model.Patient; +import com.quicksilver.healthTracker.repository.PatientRepository; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +@SpringBootApplication +@EnableAutoConfiguration +@ComponentScan(basePackages={"com.quicksilver.healthTracker"}) +@EnableJpaRepositories(basePackages="com.quicksilver.healthTracker.repository") +public class Application extends SpringBootServletInitializer { + @Autowired + private PatientRepository repository; + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + @Bean + InitializingBean seedData() { + return () -> { + repository.save(new Patient("Sheldon", "Cooper", "sheldon@gmail.com", "Fever", 8L)); + repository.save(new Patient("Amy","Fowler","amy@abc.com", "Headache", 3L)); + repository.save(new Patient("Meg","March","meg@gmail.com","Cold", 5L)); + + }; + } +} diff --git a/src/main/java/com/quicksilver/healthTracker/controller/ErrorController.java b/src/main/java/com/quicksilver/healthTracker/controller/ErrorController.java new file mode 100644 index 0000000..4d7cd2f --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/controller/ErrorController.java @@ -0,0 +1,24 @@ +package com.quicksilver.healthTracker.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; + +@Controller("error") +public class ErrorController { + + @ExceptionHandler(Exception.class) + public ModelAndView handleException + (HttpServletRequest request, Exception ex){ + ModelAndView mv = new ModelAndView(); + + mv.addObject("exception", ex.getLocalizedMessage()); + mv.addObject("url", request.getRequestURL()); + + mv.setViewName("error"); + return mv; + } + +} diff --git a/src/main/java/com/quicksilver/healthTracker/controller/LogoutController.java b/src/main/java/com/quicksilver/healthTracker/controller/LogoutController.java new file mode 100644 index 0000000..07abc1d --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/controller/LogoutController.java @@ -0,0 +1,30 @@ +package com.quicksilver.healthTracker.controller; + +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Controller +public class LogoutController { + + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public String logout(HttpServletRequest request, + HttpServletResponse response) { + + Authentication authentication = SecurityContextHolder.getContext() + .getAuthentication(); + + if (authentication != null) { + new SecurityContextLogoutHandler().logout(request, response, + authentication); + } + + return "redirect:/"; + } +} diff --git a/src/main/java/com/quicksilver/healthTracker/controller/PatientController.java b/src/main/java/com/quicksilver/healthTracker/controller/PatientController.java new file mode 100644 index 0000000..ca935c3 --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/controller/PatientController.java @@ -0,0 +1,80 @@ +package com.quicksilver.healthTracker.controller; + +import com.quicksilver.healthTracker.model.Patient; +import com.quicksilver.healthTracker.service.PatientService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; + +@Controller +public class PatientController { + + private PatientService service; + + @Autowired + public PatientController(PatientService service) { + this.service = service; + } + + @GetMapping("/list-patients") + public String showAllPatients(Model model) { + model.addAttribute("patients", service.findAll()); + return "patients"; + } + + @GetMapping("/new-patient") + public String showPatientCreationForm(Model model) { + model.addAttribute("patient", new Patient()); + return "new-patient"; + } + + @PostMapping(value = "/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + public String addNewPatient(@Valid @ModelAttribute Patient patient, BindingResult result, Model model) { + if (result.hasErrors()) { + return "new-patient"; + } + service.save(patient); + return "redirect:/list-patients"; + } + + @GetMapping("/{id}") + public String showPatientById(@PathVariable Long id, Model model) { + Patient patient = service.findById(id) + .orElseThrow(() -> new IllegalArgumentException("Invalid patient Id:" + id)); + model.addAttribute("patient", patient); + return "edit-patient"; + } + + @PostMapping("/{id}/update") + public String updatePatient(@PathVariable Long id, @Valid @ModelAttribute Patient patient, BindingResult result, Model model) { + if (result.hasErrors()) { + return "edit-patient"; + } + service.findById(id) + .orElseThrow(() -> new IllegalArgumentException("Invalid patient Id:" + id)); + service.save(patient); + return "redirect:/list-patients"; + } + + @PostMapping("/{id}/delete") + public String deletePatient(@PathVariable Long id, Model model) { + service.findById(id) + .orElseThrow(() -> new IllegalArgumentException("Invalid patient Id:" + id)); + service.deleteById(id); + return "redirect:/list-patients"; + } + @GetMapping("/{id}/bar") + public String showBar(@PathVariable Long id, Model model) { + Patient patient = service.findById(id) + .orElseThrow(() -> new IllegalArgumentException("Invalid patient Id:" + id)); + //todo conver it to json/gson + model.addAttribute("patient", patient); + return "bar"; + } + +} diff --git a/src/main/java/com/quicksilver/healthTracker/controller/WelcomeController.java b/src/main/java/com/quicksilver/healthTracker/controller/WelcomeController.java new file mode 100644 index 0000000..f6eb792 --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/controller/WelcomeController.java @@ -0,0 +1,30 @@ +package com.quicksilver.healthTracker.controller; + +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +public class WelcomeController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String showWelcomePage(ModelMap model) { + model.put("name", getLoggedinUserName()); + return "welcome"; + } + + private String getLoggedinUserName() { + Object principal = SecurityContextHolder.getContext() + .getAuthentication().getPrincipal(); + + if (principal instanceof UserDetails) { + return ((UserDetails) principal).getUsername(); + } + + return principal.toString(); + } + +} diff --git a/src/main/java/com/quicksilver/healthTracker/model/Disease.java b/src/main/java/com/quicksilver/healthTracker/model/Disease.java new file mode 100644 index 0000000..392eb80 --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/model/Disease.java @@ -0,0 +1,48 @@ +package com.quicksilver.healthTracker.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.validation.constraints.NotBlank; + +@Entity +public class Disease { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + @NotBlank(message = "Disease is mandatory") + private String disease; + + public Disease() {} + + public Disease(String disease) { + this.disease = disease; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDisease() { + return disease; + } + + public void setDisease(String disease) { + this.disease = disease; + } + + @Override + public String toString() { + return "disease: {" + + "id=" + id + + ", disease='" + disease + '\'' + + '}'; + } +} diff --git a/src/main/java/com/quicksilver/healthTracker/model/Patient.java b/src/main/java/com/quicksilver/healthTracker/model/Patient.java new file mode 100644 index 0000000..49ab07e --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/model/Patient.java @@ -0,0 +1,99 @@ +package com.quicksilver.healthTracker.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.validation.constraints.NotBlank; + +@Entity +public class Patient { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + @NotBlank(message = "First Name is mandatory") + private String firstName; + + @NotBlank(message = "Last Name is mandatory") + private String lastName; + + @NotBlank(message = "Email is mandatory") + private String email; + + @NotBlank(message = "Disease is mandatory") + private String disease; + + private long days; + + public Patient(String firstName, String lastName, String email, String disease, long days) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + this.disease = disease; + this.days = days; + + } + + public Patient() { + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getDisease() { + return disease; + } + + public void setDisease(String disease) { + this.disease = disease; + } + + public long getDays() { + return days; + } + + public void setDays(long days) { + this.days = days; + } + + @Override + public String toString() { + return "patient: {" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + '}'; + } +} diff --git a/src/main/java/com/quicksilver/healthTracker/repository/PatientRepository.java b/src/main/java/com/quicksilver/healthTracker/repository/PatientRepository.java new file mode 100644 index 0000000..a255f83 --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/repository/PatientRepository.java @@ -0,0 +1,10 @@ +package com.quicksilver.healthTracker.repository; + +import com.quicksilver.healthTracker.model.Patient; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PatientRepository extends CrudRepository { + +} diff --git a/src/main/java/com/quicksilver/healthTracker/security/SecurityConfiguration.java b/src/main/java/com/quicksilver/healthTracker/security/SecurityConfiguration.java new file mode 100644 index 0000000..b2cf603 --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/security/SecurityConfiguration.java @@ -0,0 +1,30 @@ +package com.quicksilver.healthTracker.security; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.password.NoOpPasswordEncoder; + +@Configuration +public class SecurityConfiguration extends WebSecurityConfigurerAdapter { + @Autowired + public void configureGlobalSecurity(AuthenticationManagerBuilder auth) + throws Exception { + auth.inMemoryAuthentication() + .passwordEncoder(NoOpPasswordEncoder.getInstance()) + .withUser("admin").password("admin") + .roles("USER", "ADMIN"); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests().antMatchers("/login", "/h2-console/**").permitAll() + .antMatchers("/", "/*todo*/**").access("hasRole('USER')").and() + .formLogin(); + + http.csrf().disable(); + http.headers().frameOptions().disable(); + } +} diff --git a/src/main/java/com/quicksilver/healthTracker/service/PatientService.java b/src/main/java/com/quicksilver/healthTracker/service/PatientService.java new file mode 100644 index 0000000..7cd7c46 --- /dev/null +++ b/src/main/java/com/quicksilver/healthTracker/service/PatientService.java @@ -0,0 +1,39 @@ +package com.quicksilver.healthTracker.service; + +import com.quicksilver.healthTracker.model.Patient; +import com.quicksilver.healthTracker.repository.PatientRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +@Service +public class PatientService { + + private PatientRepository repository; + + @Autowired + public PatientService(PatientRepository repository) { + this.repository = repository; + } + + public List findAll() { + return StreamSupport.stream(repository.findAll().spliterator(), false) + .collect(Collectors.toList()); + } + + public Optional findById(Long id) { + return repository.findById(id); + } + + public Patient save(Patient patient) { + return repository.save(patient); + } + + public void deleteById(Long id) { + repository.deleteById(id); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..78c12a8 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.mvc.view.prefix: /WEB-INF/jsp/ +spring.mvc.view.suffix: .jsp \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/bar.jsp b/src/main/webapp/WEB-INF/jsp/bar.jsp new file mode 100644 index 0000000..d013e99 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/bar.jsp @@ -0,0 +1,46 @@ +<%@ page import="java.util.*" %> + + +<%@ include file="common/header.jspf"%> +<%@ include file="common/navigation.jspf"%> + + + + + + +Patient:${patient.firstName} + + +Disease: ${patient.disease} Date :<%= (new java.util.Date()).toLocaleString()%> + + +<%@ include file="common/footer.jspf"%> + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/common/footer.jspf b/src/main/webapp/WEB-INF/jsp/common/footer.jspf new file mode 100644 index 0000000..d49d34c --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/common/footer.jspf @@ -0,0 +1,19 @@ + + + + + + + +