Skip to content

Commit 6905052

Browse files
committed
/participant/add: add interface for creating buyers/sellers.
Fix #507
1 parent 8ff62fa commit 6905052

30 files changed

+643
-4
lines changed

NEWS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- (functionality) add interface for viewing series sales (contributed by Sergey Chechenev)
1515
- (functionality) name of category/country in Russian now are optional fields
1616
- (functionality) preview now is generated after uploading an image
17+
- (functionality) add interface for adding buyers and sellers
1718

1819
0.3
1920
- (functionality) implemented possibility to user to add series to his collection

src/main/java/ru/mystamps/web/Db.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public static final class Country {
2727
public static final int NAME_LENGTH = 50;
2828
}
2929

30+
public static final class TransactionParticipant {
31+
public static final int NAME_LENGTH = 50;
32+
public static final int URL_LENGTH = 255;
33+
}
34+
3035
public static final class Series {
3136
public static final int COMMENT_LENGTH = 255;
3237
}

src/main/java/ru/mystamps/web/Url.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public final class Url {
6767
public static final String GET_IMAGE_PAGE = "/image/{id}";
6868
public static final String GET_IMAGE_PREVIEW_PAGE = "/image/preview/{id}";
6969

70+
public static final String ADD_PARTICIPANT_PAGE = "/participant/add";
71+
7072
public static final String FORBIDDEN_PAGE = "/error/403";
7173
public static final String NOT_FOUND_PAGE = "/error/404";
7274
public static final String INTERNAL_ERROR_PAGE = "/error/500";
@@ -123,6 +125,7 @@ public static Map<String, String> asMap(boolean serveContentFromSingleHost) {
123125
map.put("LOGOUT_PAGE", LOGOUT_PAGE);
124126
map.put("ACTIVATE_ACCOUNT_PAGE", ACTIVATE_ACCOUNT_PAGE);
125127
map.put("REGISTRATION_PAGE", REGISTRATION_PAGE);
128+
map.put("ADD_PARTICIPANT_PAGE", ADD_PARTICIPANT_PAGE);
126129
map.put("ADD_SERIES_PAGE", ADD_SERIES_PAGE);
127130
map.put("ADD_SERIES_ASK_PAGE", ADD_SERIES_ASK_PAGE);
128131
map.put("INFO_SERIES_PAGE", INFO_SERIES_PAGE);

src/main/java/ru/mystamps/web/config/ControllersConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public ErrorController getErrorController() {
7878
return new ErrorController(servicesConfig.getSiteService());
7979
}
8080

81+
@Bean
82+
public ParticipantController getParticipantController() {
83+
return new ParticipantController(servicesConfig.getTransactionParticipantService());
84+
}
85+
8186
@Bean
8287
public RobotsTxtController getRobotsTxtController() {
8388
return new RobotsTxtController();
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.controller;
19+
20+
import javax.validation.Valid;
21+
22+
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
23+
import org.springframework.stereotype.Controller;
24+
import org.springframework.validation.BindingResult;
25+
import org.springframework.web.bind.WebDataBinder;
26+
import org.springframework.web.bind.annotation.GetMapping;
27+
import org.springframework.web.bind.annotation.InitBinder;
28+
import org.springframework.web.bind.annotation.PostMapping;
29+
30+
import lombok.RequiredArgsConstructor;
31+
32+
import ru.mystamps.web.Url;
33+
import ru.mystamps.web.model.AddParticipantForm;
34+
import ru.mystamps.web.service.TransactionParticipantService;
35+
36+
import static ru.mystamps.web.controller.ControllerUtils.redirectTo;
37+
38+
@Controller
39+
@RequiredArgsConstructor
40+
public class ParticipantController {
41+
42+
private final TransactionParticipantService participantService;
43+
44+
@InitBinder("addParticipantForm")
45+
protected void initBinder(WebDataBinder binder) {
46+
StringTrimmerEditor editor = new StringTrimmerEditor(true);
47+
binder.registerCustomEditor(String.class, "name", editor);
48+
binder.registerCustomEditor(String.class, "url", editor);
49+
}
50+
51+
@GetMapping(Url.ADD_PARTICIPANT_PAGE)
52+
public AddParticipantForm showForm() {
53+
return new AddParticipantForm();
54+
}
55+
56+
@PostMapping(Url.ADD_PARTICIPANT_PAGE)
57+
public String processInput(@Valid AddParticipantForm form, BindingResult result) {
58+
if (result.hasErrors()) {
59+
return null;
60+
}
61+
62+
participantService.add(form);
63+
64+
return redirectTo(Url.INDEX_PAGE);
65+
}
66+
67+
}

src/main/java/ru/mystamps/web/dao/TransactionParticipantDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
import java.util.List;
2121

22+
import ru.mystamps.web.dao.dto.AddParticipantDbDto;
2223
import ru.mystamps.web.dao.dto.EntityWithIdDto;
2324

2425
public interface TransactionParticipantDao {
26+
void add(AddParticipantDbDto participant);
2527
List<EntityWithIdDto> findAllAsEntityWithIdDto();
2628
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.dao.dto;
19+
20+
import lombok.Getter;
21+
import lombok.Setter;
22+
import lombok.ToString;
23+
24+
@Getter
25+
@Setter
26+
@ToString
27+
public class AddParticipantDbDto {
28+
private String name;
29+
private String url;
30+
}

src/main/java/ru/mystamps/web/dao/impl/JdbcTransactionParticipantDao.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,47 @@
1717
*/
1818
package ru.mystamps.web.dao.impl;
1919

20+
import java.util.HashMap;
2021
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.apache.commons.lang3.Validate;
2125

2226
import org.springframework.beans.factory.annotation.Value;
2327
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2428

2529
import lombok.RequiredArgsConstructor;
2630

2731
import ru.mystamps.web.dao.TransactionParticipantDao;
32+
import ru.mystamps.web.dao.dto.AddParticipantDbDto;
2833
import ru.mystamps.web.dao.dto.EntityWithIdDto;
2934

3035
@RequiredArgsConstructor
3136
public class JdbcTransactionParticipantDao implements TransactionParticipantDao {
3237

3338
private final NamedParameterJdbcTemplate jdbcTemplate;
3439

40+
@Value("${transaction_participant.create}")
41+
private String addParticipantSql;
42+
3543
@Value("${transaction_participants.find_all}")
3644
private String findAllParticipantsSql;
3745

46+
@Override
47+
public void add(AddParticipantDbDto participant) {
48+
Map<String, Object> params = new HashMap<>();
49+
params.put("name", participant.getName());
50+
params.put("url", participant.getUrl());
51+
52+
int affected = jdbcTemplate.update(addParticipantSql, params);
53+
54+
Validate.validState(
55+
affected == 1,
56+
"Unexpected number of affected rows after creation of participant: %d",
57+
affected
58+
);
59+
}
60+
3861
@Override
3962
public List<EntityWithIdDto> findAllAsEntityWithIdDto() {
4063
return jdbcTemplate.query(findAllParticipantsSql, RowMappers::forEntityWithIdDto);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.model;
19+
20+
import javax.validation.constraints.Size;
21+
22+
import org.hibernate.validator.constraints.NotEmpty;
23+
import org.hibernate.validator.constraints.URL;
24+
25+
import lombok.Getter;
26+
import lombok.Setter;
27+
28+
import ru.mystamps.web.service.dto.AddParticipantDto;
29+
30+
import static ru.mystamps.web.validation.ValidationRules.PARTICIPANT_NAME_MAX_LENGTH;
31+
import static ru.mystamps.web.validation.ValidationRules.PARTICIPANT_NAME_MIN_LENGTH;
32+
import static ru.mystamps.web.validation.ValidationRules.PARTICIPANT_URL_MAX_LENGTH;
33+
34+
@Getter
35+
@Setter
36+
public class AddParticipantForm implements AddParticipantDto {
37+
38+
@NotEmpty
39+
@Size.List({
40+
@Size(min = PARTICIPANT_NAME_MIN_LENGTH, message = "{value.too-short}"),
41+
@Size(max = PARTICIPANT_NAME_MAX_LENGTH, message = "{value.too-long}")
42+
})
43+
private String name;
44+
45+
@URL
46+
@Size(max = PARTICIPANT_URL_MAX_LENGTH, message = "{value.too-long}")
47+
private String url;
48+
49+
}

src/main/java/ru/mystamps/web/service/TransactionParticipantService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.util.List;
2121

2222
import ru.mystamps.web.dao.dto.EntityWithIdDto;
23+
import ru.mystamps.web.service.dto.AddParticipantDto;
2324

2425
public interface TransactionParticipantService {
26+
void add(AddParticipantDto dto);
2527
List<EntityWithIdDto> findAllBuyers();
2628
List<EntityWithIdDto> findAllSellers();
2729
}

src/main/java/ru/mystamps/web/service/TransactionParticipantServiceImpl.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,46 @@
1919

2020
import java.util.List;
2121

22+
import org.apache.commons.lang3.Validate;
23+
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
2227
import org.springframework.transaction.annotation.Transactional;
2328

2429
import org.springframework.security.access.prepost.PreAuthorize;
2530

2631
import lombok.RequiredArgsConstructor;
2732

2833
import ru.mystamps.web.dao.TransactionParticipantDao;
34+
import ru.mystamps.web.dao.dto.AddParticipantDbDto;
2935
import ru.mystamps.web.dao.dto.EntityWithIdDto;
36+
import ru.mystamps.web.service.dto.AddParticipantDto;
3037
import ru.mystamps.web.support.spring.security.HasAuthority;
3138

3239
@RequiredArgsConstructor
3340
public class TransactionParticipantServiceImpl implements TransactionParticipantService {
41+
private static final Logger LOG =
42+
LoggerFactory.getLogger(TransactionParticipantServiceImpl.class);
3443

3544
private final TransactionParticipantDao transactionParticipantDao;
3645

46+
@Override
47+
@Transactional
48+
@PreAuthorize(HasAuthority.ADD_PARTICIPANT)
49+
public void add(AddParticipantDto dto) {
50+
Validate.isTrue(dto != null, "DTO must be non null");
51+
Validate.isTrue(dto.getName() != null, "Name must be non null");
52+
53+
AddParticipantDbDto participant = new AddParticipantDbDto();
54+
participant.setName(dto.getName());
55+
participant.setUrl(dto.getUrl());
56+
57+
transactionParticipantDao.add(participant);
58+
59+
LOG.info("Participant with name '{}' has been created", participant.getName());
60+
}
61+
3762
@Override
3863
@Transactional(readOnly = true)
3964
@PreAuthorize(HasAuthority.ADD_SERIES_SALES)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service.dto;
19+
20+
public interface AddParticipantDto {
21+
String getName();
22+
String getUrl();
23+
}

src/main/java/ru/mystamps/web/support/spring/security/Authority.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
public final class Authority {
2525
public static final GrantedAuthority ADD_COMMENTS_TO_SERIES = new SimpleGrantedAuthority(StringAuthority.ADD_COMMENTS_TO_SERIES);
2626
public static final GrantedAuthority ADD_IMAGES_TO_SERIES = new SimpleGrantedAuthority(StringAuthority.ADD_IMAGES_TO_SERIES);
27+
public static final GrantedAuthority ADD_PARTICIPANT = new SimpleGrantedAuthority(StringAuthority.ADD_PARTICIPANT);
2728
public static final GrantedAuthority ADD_SERIES_SALES = new SimpleGrantedAuthority(StringAuthority.ADD_SERIES_SALES);
2829
public static final GrantedAuthority MANAGE_TOGGLZ = new SimpleGrantedAuthority(StringAuthority.MANAGE_TOGGLZ);
2930
public static final GrantedAuthority CREATE_CATEGORY = new SimpleGrantedAuthority(StringAuthority.CREATE_CATEGORY);

src/main/java/ru/mystamps/web/support/spring/security/CustomUserDetailsService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private static Collection<? extends GrantedAuthority> getAuthorities(UserDetails
7777
authorities.add(Authority.ADD_COMMENTS_TO_SERIES);
7878
authorities.add(Authority.ADD_IMAGES_TO_SERIES);
7979
authorities.add(Authority.VIEW_SITE_EVENTS);
80+
authorities.add(Authority.ADD_PARTICIPANT);
8081
authorities.add(Authority.ADD_SERIES_SALES);
8182
authorities.add(Authority.VIEW_SERIES_SALES);
8283
authorities.add(Authority.MANAGE_TOGGLZ);

src/main/java/ru/mystamps/web/support/spring/security/HasAuthority.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
@SuppressWarnings({ "checkstyle:linelength", "PMD.AvoidDuplicateLiterals" })
2121
public final class HasAuthority {
22+
public static final String ADD_PARTICIPANT = "hasAuthority('" + StringAuthority.ADD_PARTICIPANT + "')";
2223
public static final String ADD_SERIES_SALES = "hasAuthority('" + StringAuthority.ADD_SERIES_SALES + "')";
2324
public static final String CREATE_SERIES = "hasAuthority('" + StringAuthority.CREATE_SERIES + "')";
2425
public static final String CREATE_CATEGORY = "hasAuthority('" + StringAuthority.CREATE_CATEGORY + "')";

src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ protected void configure(HttpSecurity http) throws Exception {
7373
.authorizeRequests()
7474
.mvcMatchers(Url.ADD_CATEGORY_PAGE).hasAuthority(StringAuthority.CREATE_CATEGORY)
7575
.mvcMatchers(Url.ADD_COUNTRY_PAGE).hasAuthority(StringAuthority.CREATE_COUNTRY)
76+
.mvcMatchers(Url.ADD_PARTICIPANT_PAGE).hasAuthority(StringAuthority.ADD_PARTICIPANT)
7677
.mvcMatchers(Url.ADD_SERIES_PAGE).hasAuthority(StringAuthority.CREATE_SERIES)
7778
.mvcMatchers(Url.SITE_EVENTS_PAGE).hasAuthority(StringAuthority.VIEW_SITE_EVENTS)
7879
.mvcMatchers(Url.SUGGEST_SERIES_COUNTRY).hasAuthority(StringAuthority.CREATE_SERIES)

src/main/java/ru/mystamps/web/support/spring/security/StringAuthority.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public final class StringAuthority {
2121
public static final String ADD_COMMENTS_TO_SERIES = "ADD_COMMENTS_TO_SERIES";
2222
public static final String ADD_IMAGES_TO_SERIES = "ADD_IMAGES_TO_SERIES";
23+
public static final String ADD_PARTICIPANT = "ADD_PARTICIPANT";
2324
public static final String ADD_SERIES_SALES = "ADD_SERIES_SALES";
2425
public static final String MANAGE_TOGGLZ = "MANAGE_TOGGLZ";
2526
public static final String CREATE_CATEGORY = "CREATE_CATEGORY";

src/main/java/ru/mystamps/web/validation/ValidationRules.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public final class ValidationRules {
6363
public static final int MAX_SERIES_COMMENT_LENGTH = Db.Series.COMMENT_LENGTH;
6464
public static final String CATALOG_NUMBERS_REGEXP = "[1-9][0-9]{0,3}(,[1-9][0-9]{0,3})*";
6565

66+
public static final int PARTICIPANT_NAME_MIN_LENGTH = 3;
67+
public static final int PARTICIPANT_NAME_MAX_LENGTH = Db.TransactionParticipant.NAME_LENGTH;
68+
public static final int PARTICIPANT_URL_MAX_LENGTH = Db.TransactionParticipant.URL_LENGTH;
69+
6670
/** Maximum uploading image size in kilobytes. */
6771
public static final long MAX_IMAGE_SIZE = 500;
6872

0 commit comments

Comments
 (0)