22
33import org .slf4j .Logger ;
44import 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 ;
59import org .springframework .stereotype .Controller ;
610import org .springframework .ui .Model ;
711import org .springframework .validation .BindingResult ;
8- import org .springframework .web .bind .annotation .ModelAttribute ;
9- import org .springframework .web .bind .annotation .PathVariable ;
10- import org .springframework .web .bind .annotation .RequestMapping ;
11- import org .springframework .web .bind .annotation .RequestMethod ;
12+ import org .springframework .web .bind .annotation .*;
1213import org .woehlke .greenshop .admin .AdminMenuCategory ;
1314import org .woehlke .greenshop .admin .entities .TaxZone ;
1415import org .woehlke .greenshop .admin .entities .TaxZone2Zone ;
@@ -46,15 +47,20 @@ public class TaxZoneController {
4647 @ Inject
4748 private TaxZone2ZoneService taxZone2ZoneService ;
4849
50+ private final static int PAGE_SIZE = 20 ;
51+
52+ private final static String FIRST_PAGE = "0" ;
53+
4954 @ RequestMapping (value = "/admin/taxZones" , method = RequestMethod .GET )
50- public String taxZones (Model model ){
55+ public String taxZones (@ RequestParam ( value = "page" , defaultValue = FIRST_PAGE ) int page , Model model ){
5156 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
5257 model .addAttribute ("menuCategory" ,menuCategory );
53- List <TaxZone > taxZones = taxZoneService .findAllTaxZones ();
58+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
59+ Page <TaxZone > taxZones = taxZoneService .findAllTaxZones (pageRequest );
5460 model .addAttribute ("taxZones" ,taxZones );
5561 TaxZone thisTaxZone = null ;
5662 int numberOfZones = 0 ;
57- if (taxZones .size ()>0 ){
63+ if (taxZones .getContent (). size ()>0 ){
5864 thisTaxZone = taxZones .iterator ().next ();
5965 numberOfZones = taxZone2ZoneService .getNumberOfZonesForTaxZone (thisTaxZone );
6066 }
@@ -64,10 +70,12 @@ public String taxZones(Model model){
6470 }
6571
6672 @ RequestMapping (value = "/admin/taxZones/{taxZoneId}" , method = RequestMethod .GET )
67- public String taxZoneId (@ PathVariable long taxZoneId , Model model ){
73+ public String taxZoneId (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
74+ @ PathVariable long taxZoneId , Model model ){
6875 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
6976 model .addAttribute ("menuCategory" ,menuCategory );
70- List <TaxZone > taxZones = taxZoneService .findAllTaxZones ();
77+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
78+ Page <TaxZone > taxZones = taxZoneService .findAllTaxZones (pageRequest );
7179 model .addAttribute ("taxZones" ,taxZones );
7280 TaxZone thisTaxZone = taxZoneService .findTaxZoneById (taxZoneId );
7381 int numberOfZones = taxZone2ZoneService .getNumberOfZonesForTaxZone (thisTaxZone );
@@ -77,21 +85,24 @@ public String taxZoneId(@PathVariable long taxZoneId, Model model){
7785 }
7886
7987 @ RequestMapping (value = "/admin/taxZones/insert" , method = RequestMethod .GET )
80- public String taxZonesInsertForm (Model model ){
88+ public String taxZonesInsertForm (@ RequestParam ( value = "page" , defaultValue = FIRST_PAGE ) int page , Model model ){
8189 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
8290 model .addAttribute ("menuCategory" ,menuCategory );
83- List <TaxZone > taxZones = taxZoneService .findAllTaxZones ();
91+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
92+ Page <TaxZone > taxZones = taxZoneService .findAllTaxZones (pageRequest );
8493 model .addAttribute ("taxZones" ,taxZones );
8594 TaxZone thisTaxZone = new TaxZone ();
8695 model .addAttribute ("thisTaxZone" ,thisTaxZone );
8796 return "admin/taxZonesInsertForm" ;
8897 }
8998
9099 @ RequestMapping (value = "/admin/taxZones/insert" , method = RequestMethod .POST )
91- public String taxZonesInsertSave (@ Valid TaxZone thisTaxZone , BindingResult result , Model model ){
100+ public String taxZonesInsertSave (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
101+ @ Valid TaxZone thisTaxZone , BindingResult result , Model model ){
92102 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
93103 model .addAttribute ("menuCategory" ,menuCategory );
94- List <TaxZone > taxZones = taxZoneService .findAllTaxZones ();
104+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
105+ Page <TaxZone > taxZones = taxZoneService .findAllTaxZones (pageRequest );
95106 model .addAttribute ("taxZones" ,taxZones );
96107 if (result .hasErrors ()){
97108 logger .info (result .toString ());
@@ -102,15 +113,17 @@ public String taxZonesInsertSave(@Valid TaxZone thisTaxZone, BindingResult resul
102113 thisTaxZone .setDateAdded (new Date ());
103114 thisTaxZone = taxZoneService .createTaxZone (thisTaxZone );
104115 model .addAttribute ("thisTaxZone" ,thisTaxZone );
105- return "redirect:/admin/taxZones/" +thisTaxZone .getId ();
116+ return "redirect:/admin/taxZones/" +thisTaxZone .getId ()+ "?page=" + page ;
106117 }
107118 }
108119
109120 @ RequestMapping (value = "/admin/taxZones/delete/{taxZoneId}" , method = RequestMethod .GET )
110- public String taxZoneDeleteForm (@ PathVariable long taxZoneId , Model model ){
121+ public String taxZoneDeleteForm (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
122+ @ PathVariable long taxZoneId , Model model ){
111123 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
112124 model .addAttribute ("menuCategory" ,menuCategory );
113- List <TaxZone > taxZones = taxZoneService .findAllTaxZones ();
125+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
126+ Page <TaxZone > taxZones = taxZoneService .findAllTaxZones (pageRequest );
114127 model .addAttribute ("taxZones" ,taxZones );
115128 TaxZone thisTaxZone = taxZoneService .findTaxZoneById (taxZoneId );
116129 int numberOfZones = taxZone2ZoneService .getNumberOfZonesForTaxZone (thisTaxZone );
@@ -120,26 +133,30 @@ public String taxZoneDeleteForm(@PathVariable long taxZoneId, Model model){
120133 }
121134
122135 @ RequestMapping (value = "/admin/taxZones/delete/{taxZoneId}" , method = RequestMethod .POST )
123- public String taxZoneDeletePerform (@ PathVariable long taxZoneId ,
136+ public String taxZoneDeletePerform (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
137+ @ PathVariable long taxZoneId ,
124138 @ Valid TaxZone thisTaxZone ,
125139 BindingResult result ,
126140 Model model ){
127141 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
128142 model .addAttribute ("menuCategory" ,menuCategory );
129- List <TaxZone > taxZones = taxZoneService .findAllTaxZones ();
143+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
144+ Page <TaxZone > taxZones = taxZoneService .findAllTaxZones (pageRequest );
130145 model .addAttribute ("taxZones" ,taxZones );
131146 thisTaxZone = taxZoneService .findTaxZoneById (taxZoneId );
132147 if (thisTaxZone != null ) {
133148 taxZoneService .deleteTaxZones (thisTaxZone );
134149 }
135- return "redirect:/admin/taxZones" ;
150+ return "redirect:/admin/taxZones?page=" + page ;
136151 }
137152
138153 @ RequestMapping (value = "/admin/taxZones/edit/{taxZoneId}" , method = RequestMethod .GET )
139- public String taxZoneEditForm (@ PathVariable long taxZoneId , Model model ){
154+ public String taxZoneEditForm (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
155+ @ PathVariable long taxZoneId , Model model ){
140156 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
141157 model .addAttribute ("menuCategory" ,menuCategory );
142- List <TaxZone > taxZones = taxZoneService .findAllTaxZones ();
158+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
159+ Page <TaxZone > taxZones = taxZoneService .findAllTaxZones (pageRequest );
143160 model .addAttribute ("taxZones" ,taxZones );
144161 TaxZone thisTaxZone = taxZoneService .findTaxZoneById (taxZoneId );
145162 int numberOfZones = taxZone2ZoneService .getNumberOfZonesForTaxZone (thisTaxZone );
@@ -149,20 +166,22 @@ public String taxZoneEditForm(@PathVariable long taxZoneId, Model model){
149166 }
150167
151168 @ RequestMapping (value = "/admin/taxZones/edit/{taxZoneId}" , method = RequestMethod .POST )
152- public String taxZoneEditPerform (@ PathVariable long taxZoneId ,
169+ public String taxZoneEditPerform (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
170+ @ PathVariable long taxZoneId ,
153171 @ Valid TaxZone thisTaxZone ,
154172 BindingResult result ,
155173 Model model ){
156174 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
157175 model .addAttribute ("menuCategory" ,menuCategory );
158- List <TaxZone > taxZones = taxZoneService .findAllTaxZones ();
176+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
177+ Page <TaxZone > taxZones = taxZoneService .findAllTaxZones (pageRequest );
159178 model .addAttribute ("taxZones" ,taxZones );
160179 if (result .hasErrors ()) {
161180 model .addAttribute ("thisTaxZone" ,thisTaxZone );
162181 return "admin/taxZonesEditForm" ;
163182 } else {
164183 taxZoneService .updateTaxZone (thisTaxZone );
165- return "redirect:/admin/taxZones/" +taxZoneId ;
184+ return "redirect:/admin/taxZones/" +taxZoneId + "?page=" + page ;
166185 }
167186 }
168187
0 commit comments