Biblioteca Java - Blame information for rev 9

Subversion Repositories:
Rev:
Rev Author Line No. Line
9 mihai 1 package com.linkscreens.inventory;
2  
3 import com.linkscreens.inventory.entity.FilterParameters;
4 import com.linkscreens.inventory.entity.InventoryItem;
5 import com.linkscreens.inventory.service.InventoryService;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.beans.factory.annotation.Qualifier;
8 import org.springframework.stereotype.Controller;
9 import org.springframework.ui.Model;
10 import org.springframework.validation.BindingResult;
11 import org.springframework.validation.Validator;
12 import org.springframework.validation.annotation.Validated;
13 import org.springframework.web.bind.WebDataBinder;
14 import org.springframework.web.bind.annotation.*;
15  
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18 import java.io.*;
19 import java.util.LinkedHashMap;
20  
21 /**
22  *
23  */
24 @Controller
25 public class InventoryController {
26  
27     private InventoryService inventoryService;
28  
29     private static final LinkedHashMap<String, String> years;
30  
31     private static final LinkedHashMap<String, String> months;
32  
33     static
34     {
35         years = new LinkedHashMap<String, String>();
36         years.put("2010", "2010");years.put("2011", "2011");years.put("2012", "2012");
37         years.put("2013", "2013");years.put("2014", "2014");years.put("2015", "2015");
38         years.put("2016", "2016");years.put("2017", "2017");years.put("2018", "2018");
39     }
40  
41     static
42     {
43         months = new LinkedHashMap<String, String>();
44         months.put("01", "ianuarie");months.put("02", "februarie");months.put("03", "martie");months.put("04", "aprilie");
45         months.put("05", "mai");months.put("06", "iunie");months.put("07", "iulie");months.put("08", "august");
46         months.put("09", "septembrie");months.put("10", "octombrie");months.put("11", "noiembrie");months.put("12", "decembrie");
47     }
48  
49     @Autowired
50     @Qualifier("itemValidator")
51     private Validator validator;
52  
53     @InitBinder
54     private void initBinder(WebDataBinder binder) {
55         if (binder != null && binder.getTarget() != null && binder.getTarget().getClass() != null
56             && binder.getTarget().getClass().equals(InventoryItem.class)) {
57             binder.setValidator(validator);
58         }
59     }
60  
61     @Autowired(required=true)
62     @Qualifier(value="inventoryService")
63     public void setInventoryService(InventoryService ps){
64         this.inventoryService = ps;
65     }
66  
67     @ModelAttribute("item")
68     public InventoryItem createItemModel() {
69         // ModelAttribute value should be same as used in the empSave.jsp
70         return new InventoryItem();
71     }
72  
73     @RequestMapping(value = "/items", method = RequestMethod.GET)
74     public String listItems(Model model) {
75         model.addAttribute("item", new InventoryItem());
76         model.addAttribute("filter", new FilterParameters());
77         model.addAttribute("years", years);
78         model.addAttribute("months", months);
79         model.addAttribute("listItems", this.inventoryService.getAllInventoryItems());
80         return "item";
81     }
82  
83     @RequestMapping(value = "/items/filter", method = RequestMethod.POST, params = { "filterOnly" })
84     public String listFilteredItems(@ModelAttribute("filter") FilterParameters filter, Model model, @RequestParam String filterOnly) {
85  
86         model.addAttribute("item", new InventoryItem());
87         model.addAttribute("filter", filter);
88         model.addAttribute("years", years);
89         model.addAttribute("months", months);
90         model.addAttribute("listItems", this.inventoryService.geFilteredInventoryItems(filter));
91         return "item";
92     }
93  
94     @RequestMapping(value = "/items/filter", method = RequestMethod.POST, params = { "download" })
95     public String downloadPDF(@ModelAttribute("filter") FilterParameters filter, Model model, @RequestParam String download,
96         HttpServletRequest request, HttpServletResponse response) {
97         model.addAttribute("item", new InventoryItem());
98         model.addAttribute("filter", filter);
99         model.addAttribute("years", years);
100         model.addAttribute("months", months);
101         model.addAttribute("listItems", this.inventoryService.geFilteredInventoryItems(filter));
102         String fileName =
103             this.inventoryService.generatePDFFile(filter, (File) request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), true);
104         ByteArrayOutputStream baos = null;
105         OutputStream os = null;
106         if (fileName != null && !fileName.isEmpty()) {
107             try {
108                 baos = convertPDFToByteArrayOutputStream(fileName);
109                 os = response.getOutputStream();
110                 baos.writeTo(os);
111                 os.flush();
112  
113                 return "item";
114             } catch (IOException e) {
115                 e.printStackTrace();
116                 return "item";
117             }  finally {
118                 if (baos != null) {
119                     try {
120                         baos.close();
121                     } catch (IOException e) {
122                         e.printStackTrace();
123                     }
124                 }
125                 if (os != null) {
126                     try {
127                         os.close();
128                     } catch (IOException e) {
129                         e.printStackTrace();
130                     }
131                 }
132             }
133         }
134         return "item";
135     }
136  
137     @RequestMapping(value = "/items/filter", method = RequestMethod.POST, params = { "downloadNormalized" })
138     public String downloadPDFNormalized(@ModelAttribute("filter") FilterParameters filter, Model model, @RequestParam String downloadNormalized,
139                               HttpServletRequest request, HttpServletResponse response) {
140         model.addAttribute("item", new InventoryItem());
141         model.addAttribute("filter", filter);
142         model.addAttribute("years", years);
143         model.addAttribute("months", months);
144         model.addAttribute("listItems", this.inventoryService.geFilteredInventoryItems(filter));
145         String fileName =
146                 this.inventoryService.generatePDFFile(filter, (File) request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), false);
147         ByteArrayOutputStream baos = null;
148         OutputStream os = null;
149         if (fileName != null && !fileName.isEmpty()) {
150             try {
151                 baos = convertPDFToByteArrayOutputStream(fileName);
152                 os = response.getOutputStream();
153                 baos.writeTo(os);
154                 os.flush();
155  
156                 return "item";
157             } catch (IOException e) {
158                 e.printStackTrace();
159                 return "item";
160             }  finally {
161                 if (baos != null) {
162                     try {
163                         baos.close();
164                     } catch (IOException e) {
165                         e.printStackTrace();
166                     }
167                 }
168                 if (os != null) {
169                     try {
170                         os.close();
171                     } catch (IOException e) {
172                         e.printStackTrace();
173                     }
174                 }
175             }
176         }
177         return "item";
178     }
179  
180     //For add and update person both
181     @RequestMapping(value= "/item/add", method = RequestMethod.POST)
182     public String addItem(@ModelAttribute("item") @Validated InventoryItem item, BindingResult bindingResult, Model model){
183         model.addAttribute("filter", new FilterParameters());
184         model.addAttribute("years", years);
185         model.addAttribute("months", months);
186         if (bindingResult.hasErrors()) {
187             System.out.println("Returning items.jsp page");
188             return "item";
189         }
190         if(item.getId() == null || item.getId() == 0l){
191             //new item, add it
192             item.setAnLunaPrimaRata(item.getAnPrimaRata() + "/" + item.getLunaPrimaRata());
193             item.setAnLunaAdaugare(item.getAnAdaugare() + "/" + item.getLunaAdaugare());
194             item.setDeleted(false);
195             this.inventoryService.addNewInventoryItem(item);
196         }else{
197             //existing item, call update
198             this.inventoryService.updateInventoryItem(item);
199         }
200  
201         return "redirect:/items";
202  
203     }
204  
205     @RequestMapping("/remove/{id}")
206     public String removeItem(@PathVariable("id") long id){
207         this.inventoryService.deleteItem(id);
208         return "redirect:/items";
209     }
210  
211     @RequestMapping("/edit/{id}")
212     public String editItem(@PathVariable("id") long id, Model model){
213         model.addAttribute("filter", new FilterParameters());
214         InventoryItem toEdit = this.inventoryService.getItemById(id);
215         model.addAttribute("item", toEdit);
216         model.addAttribute("years", years);
217         model.addAttribute("months", months);
218         model.addAttribute("listItems", this.inventoryService.getAllInventoryItems());
219         return "item";
220     }
221  
222     private static ByteArrayOutputStream convertPDFToByteArrayOutputStream(String fileName) {
223  
224         InputStream inputStream = null;
225         ByteArrayOutputStream baos = new ByteArrayOutputStream();
226         try {
227  
228             inputStream = new FileInputStream(fileName);
229  
230             byte[] buffer = new byte[1024];
231             baos = new ByteArrayOutputStream();
232  
233             int bytesRead;
234             while ((bytesRead = inputStream.read(buffer)) != -1) {
235                 baos.write(buffer, 0, bytesRead);
236             }
237  
238         } catch (FileNotFoundException e) {
239             e.printStackTrace();
240         } catch (IOException e) {
241             e.printStackTrace();
242         } finally {
243             if (inputStream != null) {
244                 try {
245                     inputStream.close();
246                 } catch (IOException e) {
247                     e.printStackTrace();
248                 }
249             }
250         }
251         return baos;
252     }
253 }