Biblioteca Java - Rev 9
Subversion Repositories:
(root)/Spring/SpringWebMVC/InventoryHibernate/src/main/java/com/linkscreens/inventory/InventoryController.java
package com.linkscreens.inventory;
import com.linkscreens.inventory.entity.FilterParameters;
import com.linkscreens.inventory.entity.InventoryItem;
import com.linkscreens.inventory.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.LinkedHashMap;
/**
*
*/
@Controller
public class InventoryController {
private InventoryService inventoryService;
private static final LinkedHashMap<String, String> years;
private static final LinkedHashMap<String, String> months;
static
{
years = new LinkedHashMap<String, String>();
years.put("2010", "2010");years.put("2011", "2011");years.put("2012", "2012");
years.put("2013", "2013");years.put("2014", "2014");years.put("2015", "2015");
years.put("2016", "2016");years.put("2017", "2017");years.put("2018", "2018");
}
static
{
months = new LinkedHashMap<String, String>();
months.put("01", "ianuarie");months.put("02", "februarie");months.put("03", "martie");months.put("04", "aprilie");
months.put("05", "mai");months.put("06", "iunie");months.put("07", "iulie");months.put("08", "august");
months.put("09", "septembrie");months.put("10", "octombrie");months.put("11", "noiembrie");months.put("12", "decembrie");
}
@Autowired
@Qualifier("itemValidator")
private Validator validator;
@InitBinder
private void initBinder(WebDataBinder binder) {
if (binder != null && binder.getTarget() != null && binder.getTarget().getClass() != null
&& binder.getTarget().getClass().equals(InventoryItem.class)) {
binder.setValidator(validator);
}
}
@Autowired(required=true)
@Qualifier(value="inventoryService")
public void setInventoryService(InventoryService ps){
this.inventoryService = ps;
}
@ModelAttribute("item")
public InventoryItem createItemModel() {
// ModelAttribute value should be same as used in the empSave.jsp
return new InventoryItem();
}
@RequestMapping(value = "/items", method = RequestMethod.GET)
public String listItems(Model model) {
model.addAttribute("item", new InventoryItem());
model.addAttribute("filter", new FilterParameters());
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.getAllInventoryItems());
return "item";
}
@RequestMapping(value = "/items/filter", method = RequestMethod.POST, params = { "filterOnly" })
public String listFilteredItems(@ModelAttribute("filter") FilterParameters filter, Model model, @RequestParam String filterOnly) {
model.addAttribute("item", new InventoryItem());
model.addAttribute("filter", filter);
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.geFilteredInventoryItems(filter));
return "item";
}
@RequestMapping(value = "/items/filter", method = RequestMethod.POST, params = { "download" })
public String downloadPDF(@ModelAttribute("filter") FilterParameters filter, Model model, @RequestParam String download,
HttpServletRequest request, HttpServletResponse response) {
model.addAttribute("item", new InventoryItem());
model.addAttribute("filter", filter);
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.geFilteredInventoryItems(filter));
String fileName =
this.inventoryService.generatePDFFile(filter, (File) request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), true);
ByteArrayOutputStream baos = null;
OutputStream os = null;
if (fileName != null && !fileName.isEmpty()) {
try {
baos = convertPDFToByteArrayOutputStream(fileName);
os = response.getOutputStream();
baos.writeTo(os);
os.flush();
return "item";
} catch (IOException e) {
e.printStackTrace();
return "item";
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "item";
}
@RequestMapping(value = "/items/filter", method = RequestMethod.POST, params = { "downloadNormalized" })
public String downloadPDFNormalized(@ModelAttribute("filter") FilterParameters filter, Model model, @RequestParam String downloadNormalized,
HttpServletRequest request, HttpServletResponse response) {
model.addAttribute("item", new InventoryItem());
model.addAttribute("filter", filter);
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.geFilteredInventoryItems(filter));
String fileName =
this.inventoryService.generatePDFFile(filter, (File) request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), false);
ByteArrayOutputStream baos = null;
OutputStream os = null;
if (fileName != null && !fileName.isEmpty()) {
try {
baos = convertPDFToByteArrayOutputStream(fileName);
os = response.getOutputStream();
baos.writeTo(os);
os.flush();
return "item";
} catch (IOException e) {
e.printStackTrace();
return "item";
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "item";
}
//For add and update person both
@RequestMapping(value= "/item/add", method = RequestMethod.POST)
public String addItem(@ModelAttribute("item") @Validated InventoryItem item, BindingResult bindingResult, Model model){
model.addAttribute("filter", new FilterParameters());
model.addAttribute("years", years);
model.addAttribute("months", months);
if (bindingResult.hasErrors()) {
System.out.println("Returning items.jsp page");
return "item";
}
if(item.getId() == null || item.getId() == 0l){
//new item, add it
item.setAnLunaPrimaRata(item.getAnPrimaRata() + "/" + item.getLunaPrimaRata());
item.setAnLunaAdaugare(item.getAnAdaugare() + "/" + item.getLunaAdaugare());
item.setDeleted(false);
this.inventoryService.addNewInventoryItem(item);
}else{
//existing item, call update
this.inventoryService.updateInventoryItem(item);
}
return "redirect:/items";
}
@RequestMapping("/remove/{id}")
public String removeItem(@PathVariable("id") long id){
this.inventoryService.deleteItem(id);
return "redirect:/items";
}
@RequestMapping("/edit/{id}")
public String editItem(@PathVariable("id") long id, Model model){
model.addAttribute("filter", new FilterParameters());
InventoryItem toEdit = this.inventoryService.getItemById(id);
model.addAttribute("item", toEdit);
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.getAllInventoryItems());
return "item";
}
private static ByteArrayOutputStream convertPDFToByteArrayOutputStream(String fileName) {
InputStream inputStream = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
inputStream = new FileInputStream(fileName);
byte[] buffer = new byte[1024];
baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return baos;
}
}
import com.linkscreens.inventory.entity.FilterParameters;
import com.linkscreens.inventory.entity.InventoryItem;
import com.linkscreens.inventory.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.LinkedHashMap;
/**
*
*/
@Controller
public class InventoryController {
private InventoryService inventoryService;
private static final LinkedHashMap<String, String> years;
private static final LinkedHashMap<String, String> months;
static
{
years = new LinkedHashMap<String, String>();
years.put("2010", "2010");years.put("2011", "2011");years.put("2012", "2012");
years.put("2013", "2013");years.put("2014", "2014");years.put("2015", "2015");
years.put("2016", "2016");years.put("2017", "2017");years.put("2018", "2018");
}
static
{
months = new LinkedHashMap<String, String>();
months.put("01", "ianuarie");months.put("02", "februarie");months.put("03", "martie");months.put("04", "aprilie");
months.put("05", "mai");months.put("06", "iunie");months.put("07", "iulie");months.put("08", "august");
months.put("09", "septembrie");months.put("10", "octombrie");months.put("11", "noiembrie");months.put("12", "decembrie");
}
@Autowired
@Qualifier("itemValidator")
private Validator validator;
@InitBinder
private void initBinder(WebDataBinder binder) {
if (binder != null && binder.getTarget() != null && binder.getTarget().getClass() != null
&& binder.getTarget().getClass().equals(InventoryItem.class)) {
binder.setValidator(validator);
}
}
@Autowired(required=true)
@Qualifier(value="inventoryService")
public void setInventoryService(InventoryService ps){
this.inventoryService = ps;
}
@ModelAttribute("item")
public InventoryItem createItemModel() {
// ModelAttribute value should be same as used in the empSave.jsp
return new InventoryItem();
}
@RequestMapping(value = "/items", method = RequestMethod.GET)
public String listItems(Model model) {
model.addAttribute("item", new InventoryItem());
model.addAttribute("filter", new FilterParameters());
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.getAllInventoryItems());
return "item";
}
@RequestMapping(value = "/items/filter", method = RequestMethod.POST, params = { "filterOnly" })
public String listFilteredItems(@ModelAttribute("filter") FilterParameters filter, Model model, @RequestParam String filterOnly) {
model.addAttribute("item", new InventoryItem());
model.addAttribute("filter", filter);
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.geFilteredInventoryItems(filter));
return "item";
}
@RequestMapping(value = "/items/filter", method = RequestMethod.POST, params = { "download" })
public String downloadPDF(@ModelAttribute("filter") FilterParameters filter, Model model, @RequestParam String download,
HttpServletRequest request, HttpServletResponse response) {
model.addAttribute("item", new InventoryItem());
model.addAttribute("filter", filter);
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.geFilteredInventoryItems(filter));
String fileName =
this.inventoryService.generatePDFFile(filter, (File) request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), true);
ByteArrayOutputStream baos = null;
OutputStream os = null;
if (fileName != null && !fileName.isEmpty()) {
try {
baos = convertPDFToByteArrayOutputStream(fileName);
os = response.getOutputStream();
baos.writeTo(os);
os.flush();
return "item";
} catch (IOException e) {
e.printStackTrace();
return "item";
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "item";
}
@RequestMapping(value = "/items/filter", method = RequestMethod.POST, params = { "downloadNormalized" })
public String downloadPDFNormalized(@ModelAttribute("filter") FilterParameters filter, Model model, @RequestParam String downloadNormalized,
HttpServletRequest request, HttpServletResponse response) {
model.addAttribute("item", new InventoryItem());
model.addAttribute("filter", filter);
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.geFilteredInventoryItems(filter));
String fileName =
this.inventoryService.generatePDFFile(filter, (File) request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), false);
ByteArrayOutputStream baos = null;
OutputStream os = null;
if (fileName != null && !fileName.isEmpty()) {
try {
baos = convertPDFToByteArrayOutputStream(fileName);
os = response.getOutputStream();
baos.writeTo(os);
os.flush();
return "item";
} catch (IOException e) {
e.printStackTrace();
return "item";
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "item";
}
//For add and update person both
@RequestMapping(value= "/item/add", method = RequestMethod.POST)
public String addItem(@ModelAttribute("item") @Validated InventoryItem item, BindingResult bindingResult, Model model){
model.addAttribute("filter", new FilterParameters());
model.addAttribute("years", years);
model.addAttribute("months", months);
if (bindingResult.hasErrors()) {
System.out.println("Returning items.jsp page");
return "item";
}
if(item.getId() == null || item.getId() == 0l){
//new item, add it
item.setAnLunaPrimaRata(item.getAnPrimaRata() + "/" + item.getLunaPrimaRata());
item.setAnLunaAdaugare(item.getAnAdaugare() + "/" + item.getLunaAdaugare());
item.setDeleted(false);
this.inventoryService.addNewInventoryItem(item);
}else{
//existing item, call update
this.inventoryService.updateInventoryItem(item);
}
return "redirect:/items";
}
@RequestMapping("/remove/{id}")
public String removeItem(@PathVariable("id") long id){
this.inventoryService.deleteItem(id);
return "redirect:/items";
}
@RequestMapping("/edit/{id}")
public String editItem(@PathVariable("id") long id, Model model){
model.addAttribute("filter", new FilterParameters());
InventoryItem toEdit = this.inventoryService.getItemById(id);
model.addAttribute("item", toEdit);
model.addAttribute("years", years);
model.addAttribute("months", months);
model.addAttribute("listItems", this.inventoryService.getAllInventoryItems());
return "item";
}
private static ByteArrayOutputStream convertPDFToByteArrayOutputStream(String fileName) {
InputStream inputStream = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
inputStream = new FileInputStream(fileName);
byte[] buffer = new byte[1024];
baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return baos;
}
}