11package org .woehlke .greenshop .admin .web .taxes ;
22
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
35import org .springframework .data .domain .Page ;
46import org .springframework .data .domain .PageRequest ;
57import org .springframework .data .domain .Pageable ;
68import org .springframework .data .domain .Sort ;
79import org .springframework .stereotype .Controller ;
810import org .springframework .ui .Model ;
11+ import org .springframework .validation .BindingResult ;
912import org .springframework .web .bind .annotation .PathVariable ;
1013import org .springframework .web .bind .annotation .RequestMapping ;
1114import org .springframework .web .bind .annotation .RequestMethod ;
1215import org .springframework .web .bind .annotation .RequestParam ;
1316import org .woehlke .greenshop .admin .AdminMenuCategory ;
17+ import org .woehlke .greenshop .customer .entities .Country ;
18+ import org .woehlke .greenshop .customer .service .CountryService ;
1419import org .woehlke .greenshop .customer .service .ZoneService ;
1520import org .woehlke .greenshop .customer .entities .Zone ;
1621
1722import javax .inject .Inject ;
23+ import javax .validation .Valid ;
24+ import java .util .List ;
1825
1926/**
2027 * Created by tw on 30.01.15.
2128 */
2229@ Controller
2330public class ZoneController {
2431
32+ private static final Logger logger = LoggerFactory .getLogger (ZoneController .class );
33+
2534 @ Inject
2635 private ZoneService zoneService ;
2736
37+ @ Inject
38+ private CountryService countryService ;
2839
2940 private final static int PAGE_SIZE = 20 ;
3041
@@ -34,8 +45,8 @@ public class ZoneController {
3445 public String zones (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page , Model model ){
3546 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
3647 model .addAttribute ("menuCategory" ,menuCategory );
37- Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "country.name" );
38- Page <Zone > zones = zoneService .findAllZones (pageRequest );
48+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "country.name" , "name" );
49+ Page <Zone > zones = zoneService .findAll (pageRequest );
3950 model .addAttribute ("zones" ,zones );
4051 Zone thisZone = null ;
4152 if (zones .getContent ().size ()>0 ){
@@ -50,11 +61,106 @@ public String zoneId(@RequestParam(value="page",defaultValue=FIRST_PAGE) int pag
5061 @ PathVariable long zoneId , Model model ){
5162 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
5263 model .addAttribute ("menuCategory" ,menuCategory );
53- Zone thisZone = zoneService .findZoneById (zoneId );
64+ Zone thisZone = zoneService .findById (zoneId );
5465 model .addAttribute ("thisZone" ,thisZone );
55- Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "country.name" );
56- Page <Zone > zones = zoneService .findAllZones (pageRequest );
66+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "country.name" , "name" );
67+ Page <Zone > zones = zoneService .findAll (pageRequest );
5768 model .addAttribute ("zones" ,zones );
5869 return "admin/taxes/zones" ;
5970 }
71+
72+ @ RequestMapping (value = "/admin/zones/insert" , method = RequestMethod .GET )
73+ public String zonesInsertForm (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page , Model model ){
74+ int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
75+ model .addAttribute ("menuCategory" ,menuCategory );
76+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "country.name" ,"name" );
77+ Page <Zone > zones = zoneService .findAll (pageRequest );
78+ model .addAttribute ("zones" ,zones );
79+ Zone thisZone = new Zone ();
80+ model .addAttribute ("thisZone" ,thisZone );
81+ List <Country > countries = countryService .findAllCountriesOrderByName ();
82+ model .addAttribute ("countries" , countries );
83+ return "admin/taxes/zonesInsertForm" ;
84+ }
85+
86+ @ RequestMapping (value = "/admin/zones/insert" , method = RequestMethod .POST )
87+ public String zonesInsertPerform (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
88+ @ Valid Zone thisZone , BindingResult result , Model model ){
89+ logger .info ("Zone: " +thisZone .toString ());
90+ if (result .hasErrors ()){
91+ int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
92+ model .addAttribute ("menuCategory" ,menuCategory );
93+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "country.name" ,"name" );
94+ Page <Zone > zones = zoneService .findAll (pageRequest );
95+ model .addAttribute ("zones" ,zones );
96+ model .addAttribute ("thisZone" ,thisZone );
97+ List <Country > countries = countryService .findAllCountriesOrderByName ();
98+ model .addAttribute ("countries" , countries );
99+ return "admin/taxes/countriesInsertForm" ;
100+ } else {
101+ zoneService .createZone (thisZone );
102+ return "redirect:/admin/zones/" +thisZone .getId ()+ "?page=" +page ;
103+ }
104+ }
105+
106+ @ RequestMapping (value = "/admin/zones/{zoneId}/edit" , method = RequestMethod .GET )
107+ public String zonesEditForm (@ PathVariable long zoneId ,
108+ @ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
109+ Model model ){
110+ int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
111+ model .addAttribute ("menuCategory" ,menuCategory );
112+ Zone thisZone = zoneService .findById (zoneId );
113+ model .addAttribute ("thisZone" ,thisZone );
114+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "country.name" ,"name" );
115+ Page <Zone > zones = zoneService .findAll (pageRequest );
116+ model .addAttribute ("zones" ,zones );
117+ List <Country > countries = countryService .findAllCountriesOrderByName ();
118+ model .addAttribute ("countries" ,countries );
119+ return "admin/taxes/zonesEditForm" ;
120+ }
121+
122+ @ RequestMapping (value = "/admin/zones/{zoneId}/edit" , method = RequestMethod .POST )
123+ public String zonesEditSave (@ PathVariable long zoneId ,
124+ @ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
125+ @ Valid Zone thisZone , BindingResult result , Model model ){
126+ logger .info ("Zone: " +thisZone .toString ());
127+ if (result .hasErrors ()){
128+ int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
129+ model .addAttribute ("menuCategory" ,menuCategory );
130+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "country.name" ,"name" );
131+ Page <Zone > zones = zoneService .findAll (pageRequest );
132+ model .addAttribute ("zones" ,zones );
133+ model .addAttribute ("thisZone" ,thisZone );
134+ List <Country > countries = countryService .findAllCountriesOrderByName ();
135+ model .addAttribute ("countries" ,countries );
136+ return "admin/taxes/zonesEditForm" ;
137+ } else {
138+ thisZone .setId (zoneId );
139+ zoneService .update (thisZone );
140+ return "redirect:/admin/zones/" +zoneId +"?page=" +page ;
141+ }
142+ }
143+
144+ @ RequestMapping (value = "/admin/zones/{zoneId}/delete" , method = RequestMethod .GET )
145+ public String zonesDeleteForm (@ PathVariable long zoneId ,
146+ @ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
147+ Model model ){
148+ int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
149+ model .addAttribute ("menuCategory" ,menuCategory );
150+ Zone thisZone = zoneService .findById (zoneId );
151+ model .addAttribute ("thisZone" ,thisZone );
152+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "country.name" ,"name" );
153+ Page <Zone > zones = zoneService .findAll (pageRequest );
154+ model .addAttribute ("zones" ,zones );
155+ return "admin/taxes/zonesDeleteForm" ;
156+ }
157+
158+ @ RequestMapping (value = "/admin/zones/{zoneId}/delete" , method = RequestMethod .POST )
159+ public String zonesDeleteSave (@ PathVariable long zoneId ,
160+ @ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
161+ Model model ){
162+ Zone thisZone = zoneService .findById (zoneId );
163+ zoneService .delete (thisZone );
164+ return "redirect:/admin/zones?page=" +page ;
165+ }
60166}
0 commit comments