From 5cb195bba5511e8cb4660f83101b3ce8ce17d4e4 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Fri, 26 Apr 2019 18:56:13 +0530 Subject: [PATCH] Added OpsTree Spring Java Application --- Dockerfile | 11 + README.md | 4 +- pom.xml | 453 ++++++++++++++++++ .../java/com/sample/bean/EmployeeBean.java | 40 ++ src/main/java/com/sample/bean/FileBean.java | 19 + .../sample/controller/EmployeeController.java | 132 +++++ src/main/java/com/sample/dao/EmployeeDao.java | 16 + .../java/com/sample/dao/EmployeeDaoImpl.java | 35 ++ src/main/java/com/sample/dao/ImageDao.java | 16 + .../java/com/sample/dao/ImageDaoImpl.java | 38 ++ src/main/java/com/sample/model/Employee.java | 75 +++ src/main/java/com/sample/model/Image.java | 55 +++ .../com/sample/service/EmployeeService.java | 17 + .../sample/service/EmployeeServiceImpl.java | 45 ++ .../java/com/sample/service/ImageService.java | 16 + .../com/sample/service/ImageServiceImpl.java | 53 ++ .../com/sample/servlet/DownloadServlet.java | 89 ++++ .../com/sample/servlet/FileUploadServlet.java | 91 ++++ src/main/resources/database.properties | 8 + src/main/resources/sdnext-servlet.xml | 57 +++ src/main/webapp/META-INF/MANIFEST.MF | 3 + .../webapp/WEB-INF/config/sdnext-servlet.xml | 57 +++ src/main/webapp/WEB-INF/views/addEmployee.jsp | 68 +++ .../webapp/WEB-INF/views/employeesList.jsp | 36 ++ src/main/webapp/WEB-INF/views/fileUpload.jsp | 30 ++ src/main/webapp/WEB-INF/views/index.jsp | 16 + src/main/webapp/WEB-INF/views/listImages.jsp | 33 ++ src/main/webapp/WEB-INF/web.xml | 53 ++ src/main/webapp/index.html | 3 + .../com/sample/bean/EmployeeBeanTest.java | 16 + .../service/EmployeeServiceImplTest.java | 47 ++ 31 files changed, 1631 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100644 pom.xml create mode 100644 src/main/java/com/sample/bean/EmployeeBean.java create mode 100644 src/main/java/com/sample/bean/FileBean.java create mode 100644 src/main/java/com/sample/controller/EmployeeController.java create mode 100644 src/main/java/com/sample/dao/EmployeeDao.java create mode 100644 src/main/java/com/sample/dao/EmployeeDaoImpl.java create mode 100644 src/main/java/com/sample/dao/ImageDao.java create mode 100644 src/main/java/com/sample/dao/ImageDaoImpl.java create mode 100644 src/main/java/com/sample/model/Employee.java create mode 100644 src/main/java/com/sample/model/Image.java create mode 100644 src/main/java/com/sample/service/EmployeeService.java create mode 100644 src/main/java/com/sample/service/EmployeeServiceImpl.java create mode 100644 src/main/java/com/sample/service/ImageService.java create mode 100644 src/main/java/com/sample/service/ImageServiceImpl.java create mode 100644 src/main/java/com/sample/servlet/DownloadServlet.java create mode 100644 src/main/java/com/sample/servlet/FileUploadServlet.java create mode 100644 src/main/resources/database.properties create mode 100644 src/main/resources/sdnext-servlet.xml create mode 100644 src/main/webapp/META-INF/MANIFEST.MF create mode 100644 src/main/webapp/WEB-INF/config/sdnext-servlet.xml create mode 100644 src/main/webapp/WEB-INF/views/addEmployee.jsp create mode 100644 src/main/webapp/WEB-INF/views/employeesList.jsp create mode 100644 src/main/webapp/WEB-INF/views/fileUpload.jsp create mode 100644 src/main/webapp/WEB-INF/views/index.jsp create mode 100644 src/main/webapp/WEB-INF/views/listImages.jsp create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/main/webapp/index.html create mode 100644 src/test/java/com/sample/bean/EmployeeBeanTest.java create mode 100644 src/test/java/com/sample/service/EmployeeServiceImplTest.java diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..f911b820 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM maven:3.3-jdk-8 as builder +COPY . /usr/src/mymaven/ +WORKDIR /usr/src/mymaven/ +RUN mvn clean package + +FROM tomcat:7-jre7-alpine +MAINTAINER "opstree " +RUN rm -rf /usr/local/tomcat/webapps/* +COPY --from=builder /usr/src/mymaven/target/Spring3HibernateApp.war /usr/local/tomcat/webapps/ROOT.war +WORKDIR /usr/local/tomcat/webapps/ +EXPOSE 8080 diff --git a/README.md b/README.md index 84a27769..7f07e929 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# spring3hibernate \ No newline at end of file +# Continous-Integration + +I am a utility will contain Spring3Hibernate APP. \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..54c99c23 --- /dev/null +++ b/pom.xml @@ -0,0 +1,453 @@ + + + 4.0.0 + Spring3HibernateApp + Spring3HibernateApp + war + + + + scm:git:git@github.com:OpsTree/ContinuousIntegration.git + + HEAD + + + + + + central + central + http://192.168.0.37:8081/artifactory/ext-release-local + + + snapshots + libs-snapshot-local + http://192.168.0.37:8081/artifactory/ext-snapshot-local + + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 3.0.1 + + + Max + + Low + + true + + + + + + + + + + org.apache.maven.plugins + maven-release-plugin + 2.5.1 + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.14 + + + org.codehaus.mojo + findbugs-maven-plugin + 3.0.1 + + + Max + + false + + Low + + true + + ${project.build.directory}/findbugs + + + + + analyze-compile + compile + + check + + + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.5.1 + + + xml + + + + + package + + cobertura + + + + + + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + maven-war-plugin + 2.1.1 + + Spring3HibernateApp + + + + org.apache.tomcat.maven + tomcat6-maven-plugin + 2.2 + + + deploy-service + + + deploy + + + + + + target/Spring3HibernateApp.war + / + true + 11011 + + + + + 1.8-SNAPSHOT + Spring3HibernateApp + http://maven.apache.org + + + org.mockito + mockito-all + 1.9.5 + + + + javax.activation + activation + 1.1 + + + antlr + antlr + 2.7.6 + + + aopalliance + aopalliance + 1.0 + + + asm + asm + 1.5.3 + + + asm + asm-attrs + 1.5.3 + + + cglib + cglib + 2.1_3 + + + commons-beanutils + commons-beanutils + 1.7.0 + + + commons-io + commons-io + 1.1 + + + commons-logging + commons-logging + 1.1.1 + + + commons-lang + commons-lang + 2.5 + + + commons-collections + commons-collections + 2.1.1 + + + commons-fileupload + commons-fileupload + 1.1.1 + + + org.apache.commons + commons-email + 1.1 + + + commons-digester + commons-digester + 1.8 + + + dom4j + dom4j + 1.6.1 + + + dumbster + dumbster + 1.6 + + + ehcache + ehcache + 1.2.3 + + + icu4j + icu4j + 2.6.1 + + + javassist + javassist + 3.4.GA + + + javax.xml.bind + jaxb-api + 2.1 + + + javax.xml + jaxb-impl + 2.1 + + + jaxen + jaxen + 1.1.1 + + + jdom + jdom + 1.0 + + + javax.transaction + jta + 1.1 + + + jstl + jstl + 1.2 + + + log4j + log4j + 1.2.14 + + + org.apache.lucene + lucene-core + 2.3.2 + + + org.apache.lucene + lucene-highlighter + 2.0.0 + + + javax.mail + mail + 1.4 + + + org.slf4j + slf4j-api + 1.5.6 + + + mysql + mysql-connector-java + 5.0.5 + + + org.slf4j + slf4j-log4j12 + 1.5.6 + + + javax.persistence + persistence-api + 1.0 + + + quartz + quartz + 1.5.2 + + + javax.xml.stream + stax-api + 1.0-2 + + + javax.validation + validation-api + 1.0.0.GA + + + xalan + xalan + 2.6.0 + + + xerces + xercesImpl + 2.6.2 + + + xml-apis + xml-apis + 1.3.02 + + + xerces + xmlParserAPIs + 2.6.2 + + + xom + xom + 1.0 + + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + org.hibernate + hibernate-annotations + 3.3.1.GA + + + org.hibernate + hibernate-entitymanager + 3.3.2.GA + + + org.hibernate + hibernate-validator + 4.0.2.GA + + + org.hibernate + hibernate + 3.2.6.ga + + + org.hibernate.common + hibernate-commons-annotations + 4.0.0.Final + + + org.hibernate + hibernate-search + 3.0.0.GA + + + + + org.springframework + spring-webmvc + 3.0.1.RELEASE + + + org.springframework + spring-context + 3.0.1.RELEASE + + + org.springframework + spring-core + 3.0.1.RELEASE + + + org.springframework + spring-beans + 3.0.1.RELEASE + + + org.springframework + spring-aop + 3.0.1.RELEASE + + + org.springframework + spring-tx + 3.0.1.RELEASE + + + org.springframework + spring-orm + 3.0.1.RELEASE + + + org.springframework + spring-expression + 3.0.1.RELEASE + + + org.springframework + spring-asm + 3.0.1.RELEASE + + + junit + junit + 4.4 + + + + diff --git a/src/main/java/com/sample/bean/EmployeeBean.java b/src/main/java/com/sample/bean/EmployeeBean.java new file mode 100644 index 00000000..044c0396 --- /dev/null +++ b/src/main/java/com/sample/bean/EmployeeBean.java @@ -0,0 +1,40 @@ +package com.sample.bean; + +public class EmployeeBean { + private Integer id; + private String Name; + private Integer age; + private Long salary; + private String address; + + public Long getSalary() { + return salary; + } + public void setSalary(Long salary) { + this.salary = salary; + } + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + public String getName() { + return Name; + } + public void setName(String name) { + this.Name = name; + } + public Integer getAge() { + return age; + } + public void setAge(Integer age) { + this.age = age; + } + public String getAddress() { + return address; + } + public void setAddress(String address) { + this.address = address; + } +} diff --git a/src/main/java/com/sample/bean/FileBean.java b/src/main/java/com/sample/bean/FileBean.java new file mode 100644 index 00000000..cd49261d --- /dev/null +++ b/src/main/java/com/sample/bean/FileBean.java @@ -0,0 +1,19 @@ +package com.sample.bean; + +public class FileBean { + private Integer id; + private String Name; + + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + public String getName() { + return Name; + } + public void setName(String name) { + this.Name = name; + } + } diff --git a/src/main/java/com/sample/controller/EmployeeController.java b/src/main/java/com/sample/controller/EmployeeController.java new file mode 100644 index 00000000..d65f9b78 --- /dev/null +++ b/src/main/java/com/sample/controller/EmployeeController.java @@ -0,0 +1,132 @@ +package com.sample.controller; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +import com.sample.bean.EmployeeBean; +import com.sample.bean.FileBean; +import com.sample.model.Employee; +import com.sample.service.EmployeeService; +import com.sample.service.ImageService; + +@Controller +public class EmployeeController { + + @Autowired + private EmployeeService employeeService; + + @Autowired + private ImageService imageService; + + @RequestMapping(value = "/save", method = RequestMethod.POST) + public ModelAndView saveEmployee(@ModelAttribute("command") EmployeeBean employeeBean, + BindingResult result) { + Employee employee = prepareModel(employeeBean); + employeeService.addEmployee(employee); + return new ModelAndView("redirect:/add.html"); + } + + @RequestMapping(value="/employees", method = RequestMethod.GET) + public ModelAndView listEmployees() { + Map model = new HashMap(); + model.put("employees", prepareListofBean(employeeService.listEmployeess())); + return new ModelAndView("employeesList", model); + } + + @RequestMapping(value="/listImages", method = RequestMethod.GET) + public ModelAndView listImages() { + Map model = new HashMap(); + model.put("images", imageService.listImages()); + return new ModelAndView("listImages", model); + } + + + @RequestMapping(value = "/add", method = RequestMethod.GET) + public ModelAndView addEmployee(@ModelAttribute("command") EmployeeBean employeeBean, + BindingResult result) { + Map model = new HashMap(); + model.put("employees", prepareListofBean(employeeService.listEmployeess())); + return new ModelAndView("addEmployee", model); + } + + @RequestMapping(value = "/fileUpload", method = RequestMethod.GET) + public ModelAndView fileUpload(@ModelAttribute("command") FileBean fileBean, + BindingResult result) { + Map model = new HashMap(); + return new ModelAndView("fileUpload", model); + } + + + @RequestMapping(value = "/index", method = RequestMethod.GET) + public ModelAndView welcome() { + return new ModelAndView("index"); + } + + @RequestMapping(value = "/delete", method = RequestMethod.GET) + public ModelAndView editEmployee(@ModelAttribute("command") EmployeeBean employeeBean, + BindingResult result) { + employeeService.deleteEmployee(prepareModel(employeeBean)); + Map model = new HashMap(); + model.put("employee", null); + model.put("employees", prepareListofBean(employeeService.listEmployeess())); + return new ModelAndView("addEmployee", model); + } + + @RequestMapping(value = "/edit", method = RequestMethod.GET) + public ModelAndView deleteEmployee(@ModelAttribute("command") EmployeeBean employeeBean, + BindingResult result) { + Map model = new HashMap(); + model.put("employee", prepareEmployeeBean(employeeService.getEmployee(employeeBean.getId()))); + model.put("employees", prepareListofBean(employeeService.listEmployeess())); + return new ModelAndView("addEmployee", model); + } + + private Employee prepareModel(EmployeeBean employeeBean){ + Employee employee = new Employee(); + employee.setEmpAddress(employeeBean.getAddress()); + employee.setEmpAge(employeeBean.getAge()); + employee.setEmpName(employeeBean.getName()); + employee.setSalary(employeeBean.getSalary()); + employee.setEmpId(employeeBean.getId()); + employeeBean.setId(null); + return employee; + } + + private List prepareListofBean(List employees){ + List beans = null; + if(employees != null && !employees.isEmpty()){ + beans = new ArrayList(); + EmployeeBean bean = null; + for(Employee employee : employees){ + bean = new EmployeeBean(); + bean.setName(employee.getEmpName()); + bean.setId(employee.getEmpId()); + bean.setAddress(employee.getEmpAddress()); + bean.setSalary(employee.getSalary()); + bean.setAge(employee.getEmpAge()); + beans.add(bean); + } + } + return beans; + } + + private EmployeeBean prepareEmployeeBean(Employee employee){ + EmployeeBean bean = new EmployeeBean(); + bean.setAddress(employee.getEmpAddress()); + bean.setAge(employee.getEmpAge()); + bean.setName(employee.getEmpName()); + bean.setSalary(employee.getSalary()); + bean.setId(employee.getEmpId()); + return bean; + } +} diff --git a/src/main/java/com/sample/dao/EmployeeDao.java b/src/main/java/com/sample/dao/EmployeeDao.java new file mode 100644 index 00000000..8075940a --- /dev/null +++ b/src/main/java/com/sample/dao/EmployeeDao.java @@ -0,0 +1,16 @@ +package com.sample.dao; + +import java.util.List; + +import com.sample.model.Employee; + +public interface EmployeeDao { + + public void addEmployee(Employee employee); + + public List listEmployeess(); + + public Employee getEmployee(int empid); + + public void deleteEmployee(Employee employee); +} diff --git a/src/main/java/com/sample/dao/EmployeeDaoImpl.java b/src/main/java/com/sample/dao/EmployeeDaoImpl.java new file mode 100644 index 00000000..8d94eb27 --- /dev/null +++ b/src/main/java/com/sample/dao/EmployeeDaoImpl.java @@ -0,0 +1,35 @@ +package com.sample.dao; + +import java.util.List; + +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import com.sample.model.Employee; + + +@Repository("employeeDao") +public class EmployeeDaoImpl implements EmployeeDao { + + @Autowired + private SessionFactory sessionFactory; + + public void addEmployee(Employee employee) { + sessionFactory.getCurrentSession().saveOrUpdate(employee); + } + + @SuppressWarnings("unchecked") + public List listEmployeess() { + return (List) sessionFactory.getCurrentSession().createCriteria(Employee.class).list(); + } + + public Employee getEmployee(int empid) { + return (Employee) sessionFactory.getCurrentSession().get(Employee.class, empid); + } + + public void deleteEmployee(Employee employee) { + sessionFactory.getCurrentSession().createQuery("DELETE FROM Employee WHERE empid = "+employee.getEmpId()).executeUpdate(); + } + +} diff --git a/src/main/java/com/sample/dao/ImageDao.java b/src/main/java/com/sample/dao/ImageDao.java new file mode 100644 index 00000000..24a68014 --- /dev/null +++ b/src/main/java/com/sample/dao/ImageDao.java @@ -0,0 +1,16 @@ +package com.sample.dao; + +import java.util.List; + +import com.sample.model.Image; + +public interface ImageDao { + + public void saveImage(Image image); + + public List listImages(); + + public Image getImage(int imgid); + + +} diff --git a/src/main/java/com/sample/dao/ImageDaoImpl.java b/src/main/java/com/sample/dao/ImageDaoImpl.java new file mode 100644 index 00000000..ea3e5cf7 --- /dev/null +++ b/src/main/java/com/sample/dao/ImageDaoImpl.java @@ -0,0 +1,38 @@ +package com.sample.dao; + +import java.util.List; + +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import com.sample.model.Image; + + +@Repository("imageDao") +public class ImageDaoImpl implements ImageDao { + + @Autowired + private SessionFactory sessionFactory; + + @Transactional(propagation = Propagation.REQUIRED, readOnly = false) + public void saveImage(Image image) { + getSessionFactory().getCurrentSession().saveOrUpdate(image); + } + + @SuppressWarnings("unchecked") + public List listImages() { + return (List) getSessionFactory().getCurrentSession().createCriteria(Image.class).list(); + } + + public Image getImage(int imgid) { + return (Image) getSessionFactory().getCurrentSession().get(Image.class, imgid); + } + + private SessionFactory getSessionFactory() { + + return sessionFactory; + } +} diff --git a/src/main/java/com/sample/model/Employee.java b/src/main/java/com/sample/model/Employee.java new file mode 100644 index 00000000..fab6d2c9 --- /dev/null +++ b/src/main/java/com/sample/model/Employee.java @@ -0,0 +1,75 @@ +package com.sample.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="Employee") +public class Employee implements Serializable{ + + private static final long serialVersionUID = -723583058586873479L; + + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + @Column(name = "empid") + private Integer empId; + + @Column(name="empname") + private String empName; + + @Column(name="empaddress") + private String empAddress; + + @Column(name="salary") + private Long salary; + + @Column(name="empAge") + private Integer empAge; + + public Integer getEmpId() { + return empId; + } + + public void setEmpId(Integer empId) { + this.empId = empId; + } + + public String getEmpName() { + return empName; + } + + public void setEmpName(String empName) { + this.empName = empName; + } + + public String getEmpAddress() { + return empAddress; + } + + public void setEmpAddress(String empAddress) { + this.empAddress = empAddress; + } + + public Long getSalary() { + return salary; + } + + public void setSalary(Long salary) { + this.salary = salary; + } + + public Integer getEmpAge() { + return empAge; + } + + public void setEmpAge(Integer empAge) { + this.empAge = empAge; + } + +} diff --git a/src/main/java/com/sample/model/Image.java b/src/main/java/com/sample/model/Image.java new file mode 100644 index 00000000..170ba7d1 --- /dev/null +++ b/src/main/java/com/sample/model/Image.java @@ -0,0 +1,55 @@ +package com.sample.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="Images") +public class Image implements Serializable{ + + private static final long serialVersionUID = -723583058586873479L; + + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + @Column(name = "id") + private Integer id; + + @Column(name="imagename") + private String imageName; + + /** + * @return the id + */ + public Integer getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * @return the imageName + */ + public String getImageName() { + return imageName; + } + + /** + * @param imageName the imageName to set + */ + public void setImageName(String imageName) { + this.imageName = imageName; + } + + +} diff --git a/src/main/java/com/sample/service/EmployeeService.java b/src/main/java/com/sample/service/EmployeeService.java new file mode 100644 index 00000000..9066be55 --- /dev/null +++ b/src/main/java/com/sample/service/EmployeeService.java @@ -0,0 +1,17 @@ +package com.sample.service; + +import java.util.List; + +import com.sample.model.Employee; + + +public interface EmployeeService { + + public void addEmployee(Employee employee); + + public List listEmployeess(); + + public Employee getEmployee(int empid); + + public void deleteEmployee(Employee employee); +} diff --git a/src/main/java/com/sample/service/EmployeeServiceImpl.java b/src/main/java/com/sample/service/EmployeeServiceImpl.java new file mode 100644 index 00000000..a6663be4 --- /dev/null +++ b/src/main/java/com/sample/service/EmployeeServiceImpl.java @@ -0,0 +1,45 @@ +package com.sample.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import com.sample.dao.EmployeeDao; +import com.sample.model.Employee; + +@Service("employeeService") +@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) +public class EmployeeServiceImpl implements EmployeeService { + + @Autowired + private EmployeeDao employeeDao; + + @Transactional(propagation = Propagation.REQUIRED, readOnly = false) + public void addEmployee(Employee employee) { + try{ + + } catch (Exception e){ + + } + employeeDao.addEmployee(employee); + } + + public List listEmployeess() { + return employeeDao.listEmployeess(); + } + + public Employee getEmployee(int empid) { + return employeeDao.getEmployee(empid); + } + + public void deleteEmployee(Employee employee) { + employeeDao.deleteEmployee(employee); + } + + protected void setEmployeeDao(EmployeeDao employeeDao) { + this.employeeDao = employeeDao; + } +} diff --git a/src/main/java/com/sample/service/ImageService.java b/src/main/java/com/sample/service/ImageService.java new file mode 100644 index 00000000..6fb06a4c --- /dev/null +++ b/src/main/java/com/sample/service/ImageService.java @@ -0,0 +1,16 @@ +package com.sample.service; + +import java.util.List; + +import com.sample.model.Image; + + +public interface ImageService { + + public void saveImage(Image image); + + public List listImages(); + + public Image getImage(int imgid); + +} diff --git a/src/main/java/com/sample/service/ImageServiceImpl.java b/src/main/java/com/sample/service/ImageServiceImpl.java new file mode 100644 index 00000000..ad86da29 --- /dev/null +++ b/src/main/java/com/sample/service/ImageServiceImpl.java @@ -0,0 +1,53 @@ +package com.sample.service; + +import java.util.List; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + + +import com.sample.dao.ImageDao; +import com.sample.model.Image; + +@Service("imageService") +@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) +public class ImageServiceImpl implements ImageService { + + @Autowired + private ImageDao imageDao; + + @Transactional(propagation = Propagation.REQUIRED, readOnly = false) + public void saveImage(Image image) { + try{ + + } catch (Exception e){ + + } + getImageDao().saveImage(image); + } + + public List listImages() { + return getImageDao().listImages(); + } + + public Image getImage(int imgid) { + return getImageDao().getImage(imgid); + } + + + protected void setImageDao(ImageDao imageDao) { + this.imageDao = imageDao; + } + + private ImageDao getImageDao(){ + if(imageDao == null) { + BeanFactory beanfactory = new ClassPathXmlApplicationContext("sdnext-servlet.xml"); + imageDao = (ImageDao) beanfactory.getBean("imageDao"); + } + return imageDao; + } +} diff --git a/src/main/java/com/sample/servlet/DownloadServlet.java b/src/main/java/com/sample/servlet/DownloadServlet.java new file mode 100644 index 00000000..2c637b19 --- /dev/null +++ b/src/main/java/com/sample/servlet/DownloadServlet.java @@ -0,0 +1,89 @@ +package com.sample.servlet; + +import java.io.File; +import java.io.FileInputStream; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + + +import java.util.Properties; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.core.io.ClassPathResource; + +public class DownloadServlet extends HttpServlet { + + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + + String name = request.getParameter("name"); + // reads input file from an absolute path + String filePath = getUploadDir() + File.separator + name; + File downloadFile = new File(filePath); + FileInputStream inStream = new FileInputStream(downloadFile); + + // obtains ServletContext + ServletContext context = getServletContext(); + + // gets MIME type of the file + String mimeType = context.getMimeType(filePath); + if (mimeType == null) { + // set to binary type if MIME mapping not found + mimeType = "application/octet-stream"; + } + System.out.println("MIME type: " + mimeType); + + // modifies response + response.setContentType(mimeType); + response.setContentLength((int) downloadFile.length()); + + // forces download + String headerKey = "Content-Disposition"; + String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); + response.setHeader(headerKey, headerValue); + + // obtains response's output stream + OutputStream outStream = response.getOutputStream(); + + byte[] buffer = new byte[4096]; + int bytesRead = -1; + + while ((bytesRead = inStream.read(buffer)) != -1) { + outStream.write(buffer, 0, bytesRead); + } + + inStream.close(); + outStream.close(); + } + + private String getUploadDir() { + String uploadDir = null; + InputStream in = null; + try{ + in = new ClassPathResource("database.properties").getInputStream(); + Properties prop = new Properties(); + + prop.load(in); + uploadDir = prop.getProperty("upload.dir"); + } + catch (IOException e) { + + e.printStackTrace(); + } finally { + if(in != null) + try { + in.close(); + } catch (IOException e) { + //do nothing + } + } + return uploadDir; + } +} \ No newline at end of file diff --git a/src/main/java/com/sample/servlet/FileUploadServlet.java b/src/main/java/com/sample/servlet/FileUploadServlet.java new file mode 100644 index 00000000..5d5d5d39 --- /dev/null +++ b/src/main/java/com/sample/servlet/FileUploadServlet.java @@ -0,0 +1,91 @@ +package com.sample.servlet; + import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import java.util.Properties; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.springframework.core.io.ClassPathResource; + +import com.sample.model.Image; +import com.sample.service.ImageService; +import com.sample.service.ImageServiceImpl; + +/** + * Servlet to handle File upload request from Client + */ +public class FileUploadServlet extends HttpServlet { + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + String name = ""; + //process only if its multipart content + if(ServletFileUpload.isMultipartContent(request)){ + try { + List multiparts = new ServletFileUpload( + new DiskFileItemFactory()).parseRequest(request); + + for(FileItem item : multiparts){ + if(!item.isFormField()){ + name = new File(item.getName()).getName(); + item.write( new File(getUploadDir() + File.separator + name)); + } + } + + //File uploaded successfully + request.setAttribute("message", "File Uploaded Successfully"); + + //Saving Image + Image i = new Image(); + i.setImageName(name); + ImageService service = new ImageServiceImpl(); + service.saveImage(i); + + } catch (Exception ex) { + request.setAttribute("message", "File Upload Failed due to " + ex); + + } + + }else{ + request.setAttribute("message", + "Sorry this Servlet only handles file upload request"); + } + + + response.sendRedirect(request.getContextPath() + "/index.html"); + + } + + private String getUploadDir() { + String uploadDir = null; + InputStream in = null; + try{ + in = new ClassPathResource("database.properties").getInputStream(); + Properties prop = new Properties(); + + prop.load(in); + uploadDir = prop.getProperty("upload.dir"); + } + catch (IOException e) { + + e.printStackTrace(); + } finally { + if(in != null) + try { + in.close(); + } catch (IOException e) { + //do nothing + } + } + return uploadDir; + } +} \ No newline at end of file diff --git a/src/main/resources/database.properties b/src/main/resources/database.properties new file mode 100644 index 00000000..d328ecf5 --- /dev/null +++ b/src/main/resources/database.properties @@ -0,0 +1,8 @@ +database.driver=com.mysql.jdbc.Driver +database.url=jdbc:mysql://mysql.okts-test:3306/employeedb +database.user=root +database.password=password +hibernate.dialect=org.hibernate.dialect.MySQLDialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=update +upload.dir=c:/uploads \ No newline at end of file diff --git a/src/main/resources/sdnext-servlet.xml b/src/main/resources/sdnext-servlet.xml new file mode 100644 index 00000000..eed904e3 --- /dev/null +++ b/src/main/resources/sdnext-servlet.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + com.sample.model.Employee + com.sample.model.Image + + + + + ${hibernate.dialect} + ${hibernate.show_sql} + ${hibernate.hbm2ddl.auto} + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/META-INF/MANIFEST.MF b/src/main/webapp/META-INF/MANIFEST.MF new file mode 100644 index 00000000..5e949512 --- /dev/null +++ b/src/main/webapp/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/src/main/webapp/WEB-INF/config/sdnext-servlet.xml b/src/main/webapp/WEB-INF/config/sdnext-servlet.xml new file mode 100644 index 00000000..62f6121a --- /dev/null +++ b/src/main/webapp/WEB-INF/config/sdnext-servlet.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + com.sample.model.Employee + com.sample.model.Image + + + + + ${hibernate.dialect} + ${hibernate.show_sql} + ${hibernate.hbm2ddl.auto} + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/views/addEmployee.jsp b/src/main/webapp/WEB-INF/views/addEmployee.jsp new file mode 100644 index 00000000..85d40fcf --- /dev/null +++ b/src/main/webapp/WEB-INF/views/addEmployee.jsp @@ -0,0 +1,68 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> + + + + + Sample Webapp + + +

Add Employee Data

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Employee ID:
Employee Name:
Employee Age:
Employee Salary:
Employee Address:
+
+ + +

List Employees

+ + + + + + + + + + + + + + + + + + + + +
Employee IDEmployee NameEmployee AgeEmployee SalaryEmployee AddressActions on Row
Edit | Delete
+
+

List of Employees

+ + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/views/employeesList.jsp b/src/main/webapp/WEB-INF/views/employeesList.jsp new file mode 100644 index 00000000..b756b622 --- /dev/null +++ b/src/main/webapp/WEB-INF/views/employeesList.jsp @@ -0,0 +1,36 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> + + + +All Employees + + +

List Employees

+

Add More Employee

+ + + + + + + + + + + + + + + + + + + + + +
Employee IDEmployee NameEmployee AgeEmployee SalaryEmployee Address
Edit | Delete
+
+ + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/views/fileUpload.jsp b/src/main/webapp/WEB-INF/views/fileUpload.jsp new file mode 100644 index 00000000..f6c61bee --- /dev/null +++ b/src/main/webapp/WEB-INF/views/fileUpload.jsp @@ -0,0 +1,30 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> + + + + + Sample Webapp + + + +

+ Please select an image file to upload(Max Size 1 MB) +

+ + + + + + + + +
+
+
+ + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/views/index.jsp b/src/main/webapp/WEB-INF/views/index.jsp new file mode 100644 index 00000000..b0fc5019 --- /dev/null +++ b/src/main/webapp/WEB-INF/views/index.jsp @@ -0,0 +1,16 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + + Sample Webapp CRUD Example + + +

Sample WebApp CRUD Example for CI

+

1. List of Employees

+

2. Add Employee

+

3. Upload File

+

4. List Images

+ + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/views/listImages.jsp b/src/main/webapp/WEB-INF/views/listImages.jsp new file mode 100644 index 00000000..01601274 --- /dev/null +++ b/src/main/webapp/WEB-INF/views/listImages.jsp @@ -0,0 +1,33 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> + + + +All Images + + +

List of images

+

Add More Images

+ + + + + + + + + + + + + + + + + + +
Image NameActions
Download
+
+ + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..407170e1 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,53 @@ + + + + + sdnext + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + /WEB-INF/config/sdnext-servlet.xml + + 1 + + + + FileUploadServlet + com.sample.servlet.FileUploadServlet + + + + + + DownloadServlet + com.sample.servlet.DownloadServlet + + + + + + + + FileUploadServlet + /FileUploadServlet + + + + + DownloadServlet + /DownloadServlet + + + + + sdnext + *.html + + + + index.html + + + \ No newline at end of file diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html new file mode 100644 index 00000000..da247f47 --- /dev/null +++ b/src/main/webapp/index.html @@ -0,0 +1,3 @@ + +hello + \ No newline at end of file diff --git a/src/test/java/com/sample/bean/EmployeeBeanTest.java b/src/test/java/com/sample/bean/EmployeeBeanTest.java new file mode 100644 index 00000000..a3ba3d82 --- /dev/null +++ b/src/test/java/com/sample/bean/EmployeeBeanTest.java @@ -0,0 +1,16 @@ +package com.sample.bean; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class EmployeeBeanTest { + + @Test + public void testGetterSetter() { + EmployeeBean employeeBean = new EmployeeBean(); + employeeBean.setName("name"); + assertEquals("Setter not returning right name", "name", employeeBean.getName()); + } + +} diff --git a/src/test/java/com/sample/service/EmployeeServiceImplTest.java b/src/test/java/com/sample/service/EmployeeServiceImplTest.java new file mode 100644 index 00000000..df731487 --- /dev/null +++ b/src/test/java/com/sample/service/EmployeeServiceImplTest.java @@ -0,0 +1,47 @@ +package com.sample.service; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import com.sample.dao.EmployeeDao; +import com.sample.model.Employee; + +@RunWith(MockitoJUnitRunner.class) +public class EmployeeServiceImplTest { + + @Mock + EmployeeDao employeeDao; + private EmployeeServiceImpl employeeService; + + @Before + public void setup() { + employeeService = new EmployeeServiceImpl(); + } + + @Test + public void testEmployeeNotAvailable() { + org.mockito.Mockito.when(employeeDao.getEmployee(1)).thenReturn(null); + employeeService.setEmployeeDao(employeeDao); + Employee employee = employeeService.getEmployee(1); + + assertNull(employee); + } + + @Test + public void testEmployeeAvailable() { + employeeService = new EmployeeServiceImpl(); + Employee dummyEmployee = new Employee(); + dummyEmployee.setEmpId(1); + dummyEmployee.setEmpName("Sandy"); + org.mockito.Mockito.when(employeeDao.getEmployee(1)).thenReturn(dummyEmployee); + employeeService.setEmployeeDao(employeeDao); + Employee employee = employeeService.getEmployee(1); + + assertEquals("Name didn't matched", "Sandy", employee.getEmpName()); + } +}