Skip to content

Commit 8b016ec

Browse files
admin: Country
1 parent 94e3f19 commit 8b016ec

File tree

10 files changed

+346
-35
lines changed

10 files changed

+346
-35
lines changed

src/main/java/org/woehlke/greenshop/admin/AdminLocationTaxesController.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import org.springframework.stereotype.Controller;
66
import org.springframework.ui.Model;
77
import org.springframework.validation.BindingResult;
8+
import org.springframework.validation.ObjectError;
89
import org.springframework.web.bind.annotation.*;
910
import org.woehlke.greenshop.admin.entities.TaxClass;
1011
import org.woehlke.greenshop.admin.entities.TaxRate;
1112
import org.woehlke.greenshop.admin.entities.TaxZone;
1213
import org.woehlke.greenshop.admin.entities.TaxZone2Zone;
1314
import org.woehlke.greenshop.admin.model.NewSubZoneInfoBean;
1415
import org.woehlke.greenshop.customer.CustomerService;
16+
import org.woehlke.greenshop.customer.entities.AddressFormat;
1517
import org.woehlke.greenshop.customer.entities.Country;
1618
import org.woehlke.greenshop.customer.entities.Zone;
1719

@@ -60,6 +62,87 @@ public String countries(@PathVariable long countryId, Model model){
6062
return "admin/countries";
6163
}
6264

65+
@RequestMapping(value = "/admin/countries/insert", method = RequestMethod.GET)
66+
public String countriesInsertForm(Model model){
67+
int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal();
68+
model.addAttribute("menuCategory",menuCategory);
69+
List<Country> countries = customerService.findAllCountriesOrderByName();
70+
model.addAttribute("countries",countries);
71+
Country thisCountry = new Country();
72+
model.addAttribute("thisCountry",thisCountry);
73+
List<AddressFormat> addressFormats = adminService.findAllAddressFormat();
74+
model.addAttribute("addressFormats",addressFormats);
75+
return "admin/countriesInsertForm";
76+
}
77+
78+
@RequestMapping(value = "/admin/countries/insert", method = RequestMethod.POST)
79+
public String countriesInsertPerform(@Valid Country thisCountry, BindingResult result, Model model){
80+
logger.info("Country: "+thisCountry.toString());
81+
if(result.hasErrors()){
82+
int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal();
83+
model.addAttribute("menuCategory",menuCategory);
84+
List<Country> countries = customerService.findAllCountriesOrderByName();
85+
model.addAttribute("countries",countries);
86+
model.addAttribute("thisCountry",thisCountry);
87+
List<AddressFormat> addressFormats = adminService.findAllAddressFormat();
88+
model.addAttribute("addressFormats",addressFormats);
89+
return "admin/countriesInsertForm";
90+
} else {
91+
adminService.createCountry(thisCountry);
92+
return "redirect:/admin/countries/"+thisCountry.getId();
93+
}
94+
}
95+
96+
@RequestMapping(value = "/admin/countries/{countryId}/edit", method = RequestMethod.GET)
97+
public String countriesEditForm(@PathVariable long countryId, Model model){
98+
int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal();
99+
model.addAttribute("menuCategory",menuCategory);
100+
Country thisCountry = customerService.findCountryById(countryId);
101+
model.addAttribute("thisCountry",thisCountry);
102+
List<Country> countries = customerService.findAllCountriesOrderByName();
103+
model.addAttribute("countries",countries);
104+
List<AddressFormat> addressFormats = adminService.findAllAddressFormat();
105+
model.addAttribute("addressFormats",addressFormats);
106+
return "admin/countriesEditForm";
107+
}
108+
109+
@RequestMapping(value = "/admin/countries/{countryId}/edit", method = RequestMethod.POST)
110+
public String countriesEditSave(@PathVariable long countryId, @Valid Country thisCountry, BindingResult result, Model model){
111+
logger.info("Country: "+thisCountry.toString());
112+
if(result.hasErrors()){
113+
int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal();
114+
model.addAttribute("menuCategory",menuCategory);
115+
List<Country> countries = customerService.findAllCountriesOrderByName();
116+
model.addAttribute("countries",countries);
117+
model.addAttribute("thisCountry",thisCountry);
118+
List<AddressFormat> addressFormats = adminService.findAllAddressFormat();
119+
model.addAttribute("addressFormats",addressFormats);
120+
return "admin/countriesEditForm";
121+
} else {
122+
thisCountry.setId(countryId);
123+
adminService.updateCountry(thisCountry);
124+
return "redirect:/admin/countries/"+countryId;
125+
}
126+
}
127+
128+
@RequestMapping(value = "/admin/countries/{countryId}/delete", method = RequestMethod.GET)
129+
public String countriesDeleteForm(@PathVariable long countryId, Model model){
130+
int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal();
131+
model.addAttribute("menuCategory",menuCategory);
132+
Country thisCountry = customerService.findCountryById(countryId);
133+
model.addAttribute("thisCountry",thisCountry);
134+
List<Country> countries = customerService.findAllCountriesOrderByName();
135+
model.addAttribute("countries",countries);
136+
return "admin/countriesDeleteForm";
137+
}
138+
139+
@RequestMapping(value = "/admin/countries/{countryId}/delete", method = RequestMethod.POST)
140+
public String countriesDeleteSave(@PathVariable long countryId, Model model){
141+
Country thisCountry = customerService.findCountryById(countryId);
142+
adminService.deleteCountry(thisCountry);
143+
return "redirect:/admin/countries";
144+
}
145+
63146
@RequestMapping(value = "/admin/zones", method = RequestMethod.GET)
64147
public String zones(Model model){
65148
int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal();

src/main/java/org/woehlke/greenshop/admin/AdminService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.woehlke.greenshop.catalog.model.ReviewProduct;
1111
import org.woehlke.greenshop.checkout.entities.OrderStatus;
1212
import org.woehlke.greenshop.checkout.entities.OrderStatusId;
13+
import org.woehlke.greenshop.customer.entities.AddressFormat;
14+
import org.woehlke.greenshop.customer.entities.Country;
1315
import org.woehlke.greenshop.customer.entities.Customer;
1416
import org.woehlke.greenshop.customer.entities.Zone;
1517
import org.woehlke.greenshop.customer.model.CustomerBean;
@@ -94,4 +96,11 @@ public interface AdminService extends UserDetailsService {
9496

9597
Map<Long, List<Zone>> getZoneMap();
9698

99+
void createCountry(Country thisCountry);
100+
101+
List<AddressFormat> findAllAddressFormat();
102+
103+
void updateCountry(Country thisCountry);
104+
105+
void deleteCountry(Country thisCountry);
97106
}

src/main/java/org/woehlke/greenshop/admin/AdminServiceImpl.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@
1919
import org.woehlke.greenshop.checkout.repository.OrderRepository;
2020
import org.woehlke.greenshop.checkout.repository.OrderStatusRepository;
2121
import org.woehlke.greenshop.checkout.repository.OrderTotalRepository;
22-
import org.woehlke.greenshop.customer.entities.Customer;
23-
import org.woehlke.greenshop.customer.entities.CustomerInfo;
24-
import org.woehlke.greenshop.customer.entities.Zone;
22+
import org.woehlke.greenshop.customer.entities.*;
2523
import org.woehlke.greenshop.customer.model.CustomerBean;
26-
import org.woehlke.greenshop.customer.repository.CustomerInfoRepository;
27-
import org.woehlke.greenshop.customer.repository.CustomerRepository;
28-
import org.woehlke.greenshop.customer.repository.ZoneRepository;
24+
import org.woehlke.greenshop.customer.repository.*;
2925

3026
import javax.inject.Inject;
3127
import javax.inject.Named;
@@ -98,6 +94,12 @@ public class AdminServiceImpl implements AdminService {
9894
@Inject
9995
private OrderTotalRepository orderTotalRepository;
10096

97+
@Inject
98+
private CountryRepository countryRepository;
99+
100+
@Inject
101+
private AddressFormatRepository addressFormatRepository;
102+
101103

102104
@Override
103105
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
@@ -386,4 +388,27 @@ public Map<Long, List<Zone>> getZoneMap() {
386388
}
387389
return zoneMap;
388390
}
391+
392+
@Override
393+
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
394+
public void createCountry(Country thisCountry) {
395+
countryRepository.save(thisCountry);
396+
}
397+
398+
@Override
399+
public List<AddressFormat> findAllAddressFormat() {
400+
return addressFormatRepository.findAll();
401+
}
402+
403+
@Override
404+
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
405+
public void updateCountry(Country thisCountry) {
406+
countryRepository.save(thisCountry);
407+
}
408+
409+
@Override
410+
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
411+
public void deleteCountry(Country thisCountry) {
412+
countryRepository.delete(thisCountry);
413+
}
389414
}

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

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

3+
import org.hibernate.annotations.NotFound;
4+
import org.hibernate.annotations.NotFoundAction;
5+
import org.hibernate.validator.constraints.Length;
6+
37
import javax.persistence.Column;
48
import javax.persistence.Entity;
59
import javax.persistence.GeneratedValue;
@@ -32,24 +36,23 @@ public class Country {
3236
@GeneratedValue(strategy=GenerationType.AUTO)
3337
@Column(name="countries_id",columnDefinition = "INT(11)")
3438
private Long id;
35-
36-
@Max(128)
39+
40+
@Length(min=1,max=128)
3741
@Column(name="countries_name")
3842
@NotNull
3943
private String name;
40-
41-
@Max(2)
42-
@Min(2)
44+
45+
@Length(min=2,max=2)
4346
@Column(name="countries_iso_code_2",length=2,columnDefinition = "char(2)")
4447
@NotNull
4548
private String isoCode2;
46-
47-
@Max(3)
48-
@Min(3)
49+
50+
@Length(min=3,max=3)
4951
@Column(name="countries_iso_code_3",length=3,columnDefinition = "char(3)")
5052
@NotNull
5153
private String isoCode3;
52-
54+
55+
@NotFound(action= NotFoundAction.IGNORE)
5356
@ManyToOne
5457
@JoinColumn(name="address_format_id")
5558
private AddressFormat addressFormat;

src/main/webapp/WEB-INF/jsp/admin/admin-tiles.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@
6363
<put-attribute name="bodyContent" value="/WEB-INF/jsp/admin/countries.jsp" />
6464
</definition>
6565

66+
<definition name="admin/countriesInsertForm" extends="backend">
67+
<put-attribute name="bodyContent" value="/WEB-INF/jsp/admin/countriesInsertForm.jsp" />
68+
</definition>
69+
70+
<definition name="admin/countriesEditForm" extends="backend">
71+
<put-attribute name="bodyContent" value="/WEB-INF/jsp/admin/countriesEditForm.jsp" />
72+
</definition>
73+
74+
<definition name="admin/countriesDeleteForm" extends="backend">
75+
<put-attribute name="bodyContent" value="/WEB-INF/jsp/admin/countriesDeleteForm.jsp" />
76+
</definition>
77+
6678
<definition name="admin/zones" extends="backend">
6779
<put-attribute name="bodyContent" value="/WEB-INF/jsp/admin/zones.jsp" />
6880
</definition>

src/main/webapp/WEB-INF/jsp/admin/countries.jsp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,15 @@
1818
<td class="dataTableHeadingContent" align="center" colspan="2">ISO Codes</td>
1919
<td class="dataTableHeadingContent" align="right">Action&nbsp;</td>
2020
</tr>
21-
<c:forEach var="country" items="${countries}">
22-
<c:if test="${country.id == thisCountry.id}">
23-
<tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='http://localhost/oscommerce2/admin/countries.php?page=1&cID=1&action=edit'">
24-
<td class="dataTableContent">${country.name}</td>
25-
<td class="dataTableContent" align="center" width="40">${country.isoCode2}</td>
26-
<td class="dataTableContent" align="center" width="40">${country.isoCode3}</td>
27-
<td class="dataTableContent" align="right"><img src="resources/admin/images/icon_arrow_right.gif" border="0" alt="" />&nbsp;</td>
28-
</tr>
29-
</c:if>
30-
<c:if test="${country.id != thisCountry.id}">
31-
<tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='<c:url value="/admin/countries/${country.id}"/>'">
32-
<td class="dataTableContent">${country.name}</td>
33-
<td class="dataTableContent" align="center" width="40">${country.isoCode2}</td>
34-
<td class="dataTableContent" align="center" width="40">${country.isoCode3}</td>
35-
<td class="dataTableContent" align="right"><a href="<c:url value="/admin/countries/${country.id}"/>"><img src="resources/admin/images/icon_info.gif" border="0" alt="Info" title="Info" /></a>&nbsp;</td>
36-
</tr>
37-
</c:if>
38-
</c:forEach>
21+
<c:import url="countriesDataTable.jsp" />
3922
<tr>
4023
<td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2">
4124
<tr>
4225
<td class="smallText" valign="top">Displaying <strong>1</strong> to <strong>20</strong> (of <strong>239</strong> countries)</td>
4326
<td class="smallText" align="right"><form name="pages" action="http://localhost/oscommerce2/admin/countries.php" method="get">&lt;&lt;&nbsp;&nbsp;Page <select name="page" onchange="this.form.submit();"><option value="1" selected="selected">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option></select> of 12&nbsp;&nbsp;<a href="http://localhost/oscommerce2/admin/countries.php?page=2" class="splitPageLink">&gt;&gt;</a></form></td>
4427
</tr>
4528
<tr>
46-
<td class="smallText" colspan="2" align="right"><span class="tdbLink"><a id="tdb1" href="http://localhost/oscommerce2/admin/countries.php?page=1&action=new">New Country</a></span><script type="text/javascript">$("#tdb1").button({icons:{primary:"ui-icon-plus"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script></td>
29+
<td class="smallText" colspan="2" align="right"><span class="tdbLink"><a id="tdb1" href="<c:url value="/admin/countries/insert"/>">New Country</a></span><script type="text/javascript">$("#tdb1").button({icons:{primary:"ui-icon-plus"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script></td>
4730
</tr>
4831
</table></td>
4932
</tr>
@@ -56,7 +39,7 @@
5639
</table>
5740
<table border="0" width="100%" cellspacing="0" cellpadding="2">
5841
<tr>
59-
<td align="center" class="infoBoxContent"><span class="tdbLink"><a id="tdb2" href="http://localhost/oscommerce2/admin/countries.php?page=1&cID=1&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/countries.php?page=1&cID=1&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>
42+
<td align="center" class="infoBoxContent"><span class="tdbLink"><a id="tdb2" href="<c:url value="/admin/countries/${thisCountry.id}/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="<c:url value="/admin/countries/${thisCountry.id}/delete"/>">Delete</a></span><script type="text/javascript">$("#tdb3").button({icons:{primary:"ui-icon-trash"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script></td>
6043
</tr>
6144
<tr>
6245
<td class="infoBoxContent"><br />Name:<br />${thisCountry.name}</td>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<%@ include file="/WEB-INF/layout/taglibs.jsp"%>
2+
<c:forEach var="country" items="${countries}">
3+
<c:if test="${country.id == thisCountry.id}">
4+
<tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='http://localhost/oscommerce2/admin/countries.php?page=1&cID=1&action=edit'">
5+
<td class="dataTableContent">${country.name}</td>
6+
<td class="dataTableContent" align="center" width="40">${country.isoCode2}</td>
7+
<td class="dataTableContent" align="center" width="40">${country.isoCode3}</td>
8+
<td class="dataTableContent" align="right"><img src="resources/admin/images/icon_arrow_right.gif" border="0" alt="" />&nbsp;</td>
9+
</tr>
10+
</c:if>
11+
<c:if test="${country.id != thisCountry.id}">
12+
<tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='<c:url value="/admin/countries/${country.id}"/>'">
13+
<td class="dataTableContent">${country.name}</td>
14+
<td class="dataTableContent" align="center" width="40">${country.isoCode2}</td>
15+
<td class="dataTableContent" align="center" width="40">${country.isoCode3}</td>
16+
<td class="dataTableContent" align="right"><a href="<c:url value="/admin/countries/${country.id}"/>"><img src="resources/admin/images/icon_info.gif" border="0" alt="Info" title="Info" /></a>&nbsp;</td>
17+
</tr>
18+
</c:if>
19+
</c:forEach>

0 commit comments

Comments
 (0)