|
1 | 1 | package org.woehlke.greenshop.admin.web.taxes; |
2 | 2 |
|
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.data.domain.Page; |
| 6 | +import org.springframework.data.domain.PageRequest; |
| 7 | +import org.springframework.data.domain.Pageable; |
| 8 | +import org.springframework.data.domain.Sort; |
3 | 9 | import org.springframework.stereotype.Controller; |
4 | 10 | import org.springframework.ui.Model; |
| 11 | +import org.springframework.validation.BindingResult; |
5 | 12 | import org.springframework.web.bind.annotation.*; |
6 | 13 | import org.woehlke.greenshop.admin.AdminMenuCategory; |
| 14 | +import org.woehlke.greenshop.admin.entities.TaxClass; |
7 | 15 | import org.woehlke.greenshop.admin.entities.TaxRate; |
| 16 | +import org.woehlke.greenshop.admin.entities.TaxZone; |
| 17 | +import org.woehlke.greenshop.admin.service.TaxClassService; |
8 | 18 | import org.woehlke.greenshop.admin.service.TaxRateService; |
| 19 | +import org.woehlke.greenshop.admin.service.TaxZoneService; |
9 | 20 |
|
10 | 21 | import javax.inject.Inject; |
| 22 | +import javax.validation.Valid; |
| 23 | +import java.util.Date; |
11 | 24 | import java.util.List; |
12 | 25 |
|
13 | 26 | /** |
|
16 | 29 | @Controller |
17 | 30 | public class TaxRateController { |
18 | 31 |
|
| 32 | + private static final Logger logger = LoggerFactory.getLogger(TaxRateController.class); |
| 33 | + |
19 | 34 | @Inject |
20 | 35 | private TaxRateService taxRateService; |
21 | 36 |
|
| 37 | + @Inject |
| 38 | + private TaxZoneService taxZoneService; |
| 39 | + |
| 40 | + @Inject |
| 41 | + private TaxClassService taxClassService; |
| 42 | + |
| 43 | + private final static int PAGE_SIZE = 20; |
| 44 | + |
| 45 | + private final static String FIRST_PAGE = "0"; |
| 46 | + |
22 | 47 | @RequestMapping(value = "/admin/taxRates", method = RequestMethod.GET) |
23 | | - public String taxRates(Model model){ |
| 48 | + public String taxRates(@RequestParam(value="page",defaultValue=FIRST_PAGE) int page, Model model){ |
24 | 49 | int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal(); |
25 | 50 | model.addAttribute("menuCategory",menuCategory); |
26 | | - List<TaxRate> taxRates = taxRateService.findAllTaxRates(); |
| 51 | + Pageable pageRequest = new PageRequest(page,PAGE_SIZE, Sort.Direction.ASC, "priority"); |
| 52 | + Page<TaxRate> taxRates = taxRateService.findAll(pageRequest); |
27 | 53 | model.addAttribute("taxRates",taxRates); |
28 | 54 | TaxRate thisTaxRate = null; |
29 | | - if(taxRates.size()>0){ |
| 55 | + if(taxRates.getContent().size()>0){ |
30 | 56 | thisTaxRate = taxRates.iterator().next(); |
31 | 57 | } |
32 | 58 | model.addAttribute("thisTaxRate",thisTaxRate); |
33 | 59 | return "admin/taxes/taxRates"; |
34 | 60 | } |
35 | 61 |
|
36 | 62 | @RequestMapping(value = "/admin/taxRates/{taxRateId}", method = RequestMethod.GET) |
37 | | - public String taxRateId(@PathVariable long taxRateId, Model model){ |
| 63 | + public String taxRateId(@PathVariable long taxRateId, |
| 64 | + @RequestParam(value="page",defaultValue=FIRST_PAGE) int page, |
| 65 | + Model model) { |
| 66 | + int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal(); |
| 67 | + model.addAttribute("menuCategory", menuCategory); |
| 68 | + Pageable pageRequest = new PageRequest(page, PAGE_SIZE, Sort.Direction.ASC, "priority"); |
| 69 | + Page<TaxRate> taxRates = taxRateService.findAll(pageRequest); |
| 70 | + model.addAttribute("taxRates", taxRates); |
| 71 | + TaxRate thisTaxRate = null; |
| 72 | + if (taxRates.getContent().size() > 0) { |
| 73 | + thisTaxRate = taxRateService.findById(taxRateId); |
| 74 | + } |
| 75 | + model.addAttribute("thisTaxRate", thisTaxRate); |
| 76 | + return "admin/taxes/taxRates"; |
| 77 | + } |
| 78 | + |
| 79 | + @RequestMapping(value = "/admin/taxRates/insert", method = RequestMethod.GET) |
| 80 | + public String taxRateInsertForm(@RequestParam(value="page",defaultValue=FIRST_PAGE) int page, Model model){ |
38 | 81 | int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal(); |
39 | 82 | model.addAttribute("menuCategory",menuCategory); |
40 | | - List<TaxRate> taxRates = taxRateService.findAllTaxRates(); |
| 83 | + Pageable pageRequest = new PageRequest(page,PAGE_SIZE, Sort.Direction.ASC, "priority"); |
| 84 | + Page<TaxRate> taxRates = taxRateService.findAll(pageRequest); |
41 | 85 | model.addAttribute("taxRates",taxRates); |
42 | | - TaxRate thisTaxRate = null; |
43 | | - if(taxRates.size()>0){ |
44 | | - thisTaxRate = taxRateService.findTaxRateById(taxRateId); |
| 86 | + TaxRate thisTaxRate = new TaxRate(); |
| 87 | + model.addAttribute("thisTaxRate",thisTaxRate); |
| 88 | + List<TaxZone> taxZones = taxZoneService.findAll(); |
| 89 | + model.addAttribute("taxZones",taxZones); |
| 90 | + List<TaxClass> taxClasses = taxClassService.findAll(); |
| 91 | + model.addAttribute("taxClasses",taxClasses); |
| 92 | + return "admin/taxes/taxRatesInsertForm"; |
| 93 | + } |
| 94 | + |
| 95 | + @RequestMapping(value = "/admin/taxRates/insert", method = RequestMethod.POST) |
| 96 | + public String taxRateInsertPerform(@RequestParam(value="page",defaultValue=FIRST_PAGE) int page, |
| 97 | + @Valid TaxRate thisTaxRate, BindingResult result, Model model){ |
| 98 | + logger.info("TaxRate: "+thisTaxRate.toString()); |
| 99 | + if(result.hasErrors()){ |
| 100 | + int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal(); |
| 101 | + model.addAttribute("menuCategory",menuCategory); |
| 102 | + Pageable pageRequest = new PageRequest(page,PAGE_SIZE, Sort.Direction.ASC, "priority"); |
| 103 | + Page<TaxRate> taxRates = taxRateService.findAll(pageRequest); |
| 104 | + model.addAttribute("taxRates",taxRates); |
| 105 | + model.addAttribute("thisTaxRate",thisTaxRate); |
| 106 | + return "admin/taxes/taxRatesInsertForm"; |
| 107 | + } else { |
| 108 | + taxRateService.create(thisTaxRate); |
| 109 | + return "redirect:/admin/taxRates/"+thisTaxRate.getId()+ "?page="+page; |
45 | 110 | } |
| 111 | + } |
| 112 | + |
| 113 | + @RequestMapping(value = "/admin/taxRates/{taxRateId}/edit", method = RequestMethod.GET) |
| 114 | + public String taxRateEditForm(@PathVariable long taxRateId, |
| 115 | + @RequestParam(value="page",defaultValue=FIRST_PAGE) int page, |
| 116 | + Model model){ |
| 117 | + int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal(); |
| 118 | + model.addAttribute("menuCategory",menuCategory); |
| 119 | + TaxRate thisTaxRate = taxRateService.findById(taxRateId); |
46 | 120 | model.addAttribute("thisTaxRate",thisTaxRate); |
47 | | - return "admin/taxes/taxRates"; |
| 121 | + Pageable pageRequest = new PageRequest(page,PAGE_SIZE, Sort.Direction.ASC, "priority"); |
| 122 | + Page<TaxRate> taxRates = taxRateService.findAll(pageRequest); |
| 123 | + model.addAttribute("taxRates",taxRates); |
| 124 | + List<TaxZone> taxZones = taxZoneService.findAll(); |
| 125 | + model.addAttribute("taxZones",taxZones); |
| 126 | + List<TaxClass> taxClasses = taxClassService.findAll(); |
| 127 | + model.addAttribute("taxClasses",taxClasses); |
| 128 | + return "admin/taxes/taxRatesEditForm"; |
| 129 | + } |
| 130 | + |
| 131 | + @RequestMapping(value = "/admin/taxRates/{taxRateId}/edit", method = RequestMethod.POST) |
| 132 | + public String taxRateEditSave(@PathVariable long taxRateId, |
| 133 | + @RequestParam(value="page",defaultValue=FIRST_PAGE) int page, |
| 134 | + @Valid TaxRate thisTaxRate, BindingResult result, Model model){ |
| 135 | + logger.info("TaxRate: "+thisTaxRate.toString()); |
| 136 | + if(result.hasErrors()){ |
| 137 | + int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal(); |
| 138 | + model.addAttribute("menuCategory",menuCategory); |
| 139 | + model.addAttribute("thisTaxRate",thisTaxRate); |
| 140 | + Pageable pageRequest = new PageRequest(page,PAGE_SIZE, Sort.Direction.ASC, "priority"); |
| 141 | + Page<TaxRate> taxRates = taxRateService.findAll(pageRequest); |
| 142 | + model.addAttribute("taxRates",taxRates); |
| 143 | + return "admin/taxes/taxRatesEditForm"; |
| 144 | + } else { |
| 145 | + TaxRate loadedTaxRate = taxRateService.findById(taxRateId); |
| 146 | + loadedTaxRate.setLastModified(new Date()); |
| 147 | + loadedTaxRate.setPriority(thisTaxRate.getPriority()); |
| 148 | + loadedTaxRate.setTaxRate(thisTaxRate.getTaxRate()); |
| 149 | + loadedTaxRate.setTaxClass(thisTaxRate.getTaxClass()); |
| 150 | + loadedTaxRate.setTaxZone(thisTaxRate.getTaxZone()); |
| 151 | + taxRateService.update(loadedTaxRate); |
| 152 | + return "redirect:/admin/taxRates/"+taxRateId+"?page="+page; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @RequestMapping(value = "/admin/taxRates/{taxRateId}/delete", method = RequestMethod.GET) |
| 157 | + public String taxRateDeleteForm(@PathVariable long taxRateId, |
| 158 | + @RequestParam(value="page",defaultValue=FIRST_PAGE) int page, |
| 159 | + Model model){ |
| 160 | + int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal(); |
| 161 | + model.addAttribute("menuCategory",menuCategory); |
| 162 | + TaxRate thisTaxRate = taxRateService.findById(taxRateId); |
| 163 | + model.addAttribute("thisTaxRate",thisTaxRate); |
| 164 | + Pageable pageRequest = new PageRequest(page,PAGE_SIZE, Sort.Direction.ASC, "priority"); |
| 165 | + Page<TaxRate> taxRates = taxRateService.findAll(pageRequest); |
| 166 | + model.addAttribute("taxRates",taxRates); |
| 167 | + return "admin/taxes/taxRatesDeleteForm"; |
| 168 | + } |
| 169 | + |
| 170 | + @RequestMapping(value = "/admin/taxRates/{taxRateId}/delete", method = RequestMethod.POST) |
| 171 | + public String taxRateDeleteSave(@PathVariable long taxRateId, |
| 172 | + @RequestParam(value="page",defaultValue=FIRST_PAGE) int page, |
| 173 | + Model model){ |
| 174 | + TaxRate thisTaxRate = taxRateService.findById(taxRateId); |
| 175 | + taxRateService.delete(thisTaxRate); |
| 176 | + return "redirect:/admin/taxRates?page="+page; |
48 | 177 | } |
49 | 178 | } |
0 commit comments