Skip to content

Commit 3b6b796

Browse files
admin: zones
1 parent fb3e770 commit 3b6b796

File tree

9 files changed

+339
-25
lines changed

9 files changed

+339
-25
lines changed

src/main/java/org/woehlke/greenshop/admin/web/taxes/TaxZoneController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public String taxZoneInsertSave(@PathVariable long taxZoneId,
258258
Country country = countryService.findCountryById(newSubZoneInfoBean.getZone_country_id());
259259
Zone subZone = null;
260260
if(newSubZoneInfoBean.getZone_id()!=null){
261-
subZone = zoneService.findZoneById(newSubZoneInfoBean.getZone_id());
261+
subZone = zoneService.findById(newSubZoneInfoBean.getZone_id());
262262
}
263263
TaxZone2Zone newTaxZone2Zone = new TaxZone2Zone();
264264
newTaxZone2Zone.setDateAdded(new Date());
@@ -327,7 +327,7 @@ public String taxZoneWithZoneIdEditPerform(@PathVariable long taxZoneId,
327327
Country country = countryService.findCountryById(newSubZoneInfoBean.getZone_country_id());
328328
Zone subZone = null;
329329
if(newSubZoneInfoBean.getZone_id()!=null){
330-
subZone = zoneService.findZoneById(newSubZoneInfoBean.getZone_id());
330+
subZone = zoneService.findById(newSubZoneInfoBean.getZone_id());
331331
}
332332
thisZone.setZoneCountry(country);
333333
thisZone.setZone(subZone);

src/main/java/org/woehlke/greenshop/admin/web/taxes/ZoneController.java

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
package org.woehlke.greenshop.admin.web.taxes;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35
import org.springframework.data.domain.Page;
46
import org.springframework.data.domain.PageRequest;
57
import org.springframework.data.domain.Pageable;
68
import org.springframework.data.domain.Sort;
79
import org.springframework.stereotype.Controller;
810
import org.springframework.ui.Model;
11+
import org.springframework.validation.BindingResult;
912
import org.springframework.web.bind.annotation.PathVariable;
1013
import org.springframework.web.bind.annotation.RequestMapping;
1114
import org.springframework.web.bind.annotation.RequestMethod;
1215
import org.springframework.web.bind.annotation.RequestParam;
1316
import org.woehlke.greenshop.admin.AdminMenuCategory;
17+
import org.woehlke.greenshop.customer.entities.Country;
18+
import org.woehlke.greenshop.customer.service.CountryService;
1419
import org.woehlke.greenshop.customer.service.ZoneService;
1520
import org.woehlke.greenshop.customer.entities.Zone;
1621

1722
import 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
2330
public 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
}

src/main/java/org/woehlke/greenshop/customer/entities/Zone.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.woehlke.greenshop.customer.entities;
22

3+
import org.hibernate.validator.constraints.Length;
4+
35
import javax.persistence.Column;
46
import javax.persistence.Entity;
57
import javax.persistence.GeneratedValue;
@@ -32,10 +34,12 @@ public class Zone {
3234
@ManyToOne
3335
@JoinColumn(name="zone_country_id")
3436
private Country country;
35-
37+
38+
@Length(max = 32)
3639
@Column(name="zone_name")
3740
private String name;
38-
41+
42+
@Length(max = 255)
3943
@Column(name="zone_code")
4044
private String code;
4145

@@ -76,6 +80,28 @@ public String toString() {
7680
return "Zone [id=" + id + ", country=" + country + ", name=" + name
7781
+ ", code=" + code + "]";
7882
}
79-
80-
83+
84+
@Override
85+
public boolean equals(Object o) {
86+
if (this == o) return true;
87+
if (!(o instanceof Zone)) return false;
88+
89+
Zone zone = (Zone) o;
90+
91+
if (code != null ? !code.equals(zone.code) : zone.code != null) return false;
92+
if (country != null ? !country.equals(zone.country) : zone.country != null) return false;
93+
if (id != null ? !id.equals(zone.id) : zone.id != null) return false;
94+
if (name != null ? !name.equals(zone.name) : zone.name != null) return false;
95+
96+
return true;
97+
}
98+
99+
@Override
100+
public int hashCode() {
101+
int result = id != null ? id.hashCode() : 0;
102+
result = 31 * result + (country != null ? country.hashCode() : 0);
103+
result = 31 * result + (name != null ? name.hashCode() : 0);
104+
result = 31 * result + (code != null ? code.hashCode() : 0);
105+
return result;
106+
}
81107
}

src/main/java/org/woehlke/greenshop/customer/service/ZoneService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
*/
1313
public interface ZoneService {
1414

15-
Page<Zone> findAllZones(Pageable pageRequest);
16-
17-
Zone findZoneById(long zoneId);
15+
Page<Zone> findAll(Pageable pageRequest);
1816

1917
Map<Long, List<Zone>> getZoneMap();
2018

19+
void createZone(Zone thisZone);
20+
21+
Zone findById(long zoneId);
22+
23+
void update(Zone thisZone);
24+
25+
void delete(Zone thisZone);
2126
}

src/main/java/org/woehlke/greenshop/customer/service/ZoneServiceImpl.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,10 @@ public class ZoneServiceImpl implements ZoneService {
2525
private ZoneRepository zoneRepository;
2626

2727
@Override
28-
public Page<Zone> findAllZones(Pageable pageRequest) {
28+
public Page<Zone> findAll(Pageable pageRequest) {
2929
return zoneRepository.findAll(pageRequest);
3030
}
3131

32-
@Override
33-
public Zone findZoneById(long zoneId) {
34-
return zoneRepository.findOne(zoneId);
35-
}
36-
3732
@Override
3833
public Map<Long, List<Zone>> getZoneMap() {
3934
List<Zone> zones = zoneRepository.findAll();
@@ -53,4 +48,27 @@ public Map<Long, List<Zone>> getZoneMap() {
5348
return zoneMap;
5449
}
5550

51+
@Override
52+
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
53+
public void createZone(Zone thisZone) {
54+
thisZone=zoneRepository.save(thisZone);
55+
}
56+
57+
@Override
58+
public Zone findById(long zoneId) {
59+
return zoneRepository.findOne(zoneId);
60+
}
61+
62+
@Override
63+
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
64+
public void update(Zone thisZone) {
65+
thisZone=zoneRepository.save(thisZone);
66+
}
67+
68+
@Override
69+
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
70+
public void delete(Zone thisZone) {
71+
zoneRepository.delete(thisZone);
72+
}
73+
5674
}

src/main/webapp/WEB-INF/jsp/admin/taxes/zones.jsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2">
2525
<c:import url="taxes/zonesPager.jsp" />
2626
<tr>
27-
<td class="smallText" colspan="2" align="right"><span class="tdbLink"><a id="tdb1" href="http://localhost/oscommerce2/admin/zones.php?page=1&action=new">New Zone</a></span><script type="text/javascript">$("#tdb1").button({icons:{primary:"ui-icon-plus"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script></td>
27+
<td class="smallText" colspan="2" align="right"><span class="tdbLink"><a id="tdb1" href="<c:url value="/admin/zones/insert"/>">New Zone</a></span><script type="text/javascript">$("#tdb1").button({icons:{primary:"ui-icon-plus"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script></td>
2828
</tr>
2929
</table></td>
3030
</tr>
@@ -37,7 +37,7 @@
3737
</table>
3838
<table border="0" width="100%" cellspacing="0" cellpadding="2">
3939
<tr>
40-
<td align="center" class="infoBoxContent"><span class="tdbLink"><a id="tdb2" href="http://localhost/oscommerce2/admin/zones.php?page=1&cID=102&action=edit">Edit</a></span><script type="text/javascript">$("#tdb2").button({icons:{primary:"ui-icon-document"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script><span class="tdbLink"><a id="tdb3" href="http://localhost/oscommerce2/admin/zones.php?page=1&cID=102&action=delete">Delete</a></span><script type="text/javascript">$("#tdb3").button({icons:{primary:"ui-icon-trash"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script></td>
40+
<td align="center" class="infoBoxContent"><span class="tdbLink"><a id="tdb2" href="<c:url value="/admin/zones/${thisZone.id}/edit?page=${zones.number}"/>">Edit</a></span><script type="text/javascript">$("#tdb2").button({icons:{primary:"ui-icon-document"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script><span class="tdbLink"><a id="tdb3" href="<c:url value="/admin/zones/${thisZone.id}/delete?page=${zones.number}"/>">Delete</a></span><script type="text/javascript">$("#tdb3").button({icons:{primary:"ui-icon-trash"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script></td>
4141
</tr>
4242
<tr>
4343
<td class="infoBoxContent"><br />Zones Name:<br />${thisZone.name} (${thisZone.code})</td>
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
11
<%@ include file="/WEB-INF/layout/taglibs.jsp"%>
2-
<h1>Delete</h1>
2+
<table border="0" width="100%" cellspacing="0" cellpadding="2">
3+
<tr>
4+
<td><table border="0" width="100%" cellspacing="0" cellpadding="0">
5+
<tr>
6+
<td class="pageHeading">Zones</td>
7+
<td class="pageHeading" align="right"><img src="resources/admin/images/pixel_trans.gif" border="0" alt="" width="57" height="40" /></td>
8+
</tr>
9+
</table></td>
10+
</tr>
11+
<tr>
12+
<td><table border="0" width="100%" cellspacing="0" cellpadding="0">
13+
<tr>
14+
<td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
15+
<tr class="dataTableHeadingRow">
16+
<td class="dataTableHeadingContent">Country</td>
17+
<td class="dataTableHeadingContent">Zones</td>
18+
<td class="dataTableHeadingContent" align="center">Code</td>
19+
<td class="dataTableHeadingContent" align="right">Action&nbsp;</td>
20+
</tr>
21+
<c:import url="taxes/zonesDataTable.jsp" />
22+
<tr>
23+
<td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2">
24+
<c:import url="taxes/zonesPager.jsp" />
25+
</table></td>
26+
</tr>
27+
</table></td>
28+
<td width="25%" valign="top">
29+
<table border="0" width="100%" cellspacing="0" cellpadding="2">
30+
<tr class="infoBoxHeading">
31+
<td class="infoBoxHeading"><strong>Delete Zone</strong></td>
32+
</tr>
33+
</table>
34+
<form name="zones" method="post">
35+
<table border="0" width="100%" cellspacing="0" cellpadding="2">
36+
<tr>
37+
<td class="infoBoxContent">Are you sure you want to delete this zone?</td>
38+
</tr>
39+
<tr>
40+
<td class="infoBoxContent"><br /><strong>${thisZone.name}</strong></td>
41+
</tr>
42+
<tr>
43+
<td align="center" class="infoBoxContent"><br /><span class="tdbLink"><button id="tdb1" type="submit">Delete</button></span><script type="text/javascript">$("#tdb1").button({icons:{primary:"ui-icon-trash"}}).addClass("ui-priority-primary").parent().removeClass("tdbLink");</script><span class="tdbLink"><a id="tdb2" href="<c:url value="/admin/zones/${thisZone.id}?page=${zones.number}" />">Cancel</a></span><script type="text/javascript">$("#tdb2").button({icons:{primary:"ui-icon-close"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script></td>
44+
</tr>
45+
</table>
46+
</form>
47+
</td>
48+
</tr>
49+
</table></td>
50+
</tr>
51+
</table>

0 commit comments

Comments
 (0)