Skip to content

Commit 78b23a5

Browse files
author
eugenp
committed
work on principal and user dto services
1 parent d1f48d0 commit 78b23a5

15 files changed

Lines changed: 82 additions & 40 deletions

File tree

‎src/main/java/org/rest/common/event/ResourceCreatedEvent.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public ResourceCreatedEvent( final Class< T > clazz, final UriComponentsBuilder
2020
Preconditions.checkNotNull( uriBuilderToSet );
2121
Preconditions.checkNotNull( responseToSet );
2222
Preconditions.checkNotNull( idOfNewResourceToSet );
23-
23+
2424
this.uriBuilder = uriBuilderToSet;
2525
this.response = responseToSet;
2626
this.idOfNewResource = idOfNewResourceToSet;

‎src/main/java/org/rest/persistence/service/AbstractService.java‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ public void deleteAll(){
7676
this.getDao().deleteAll();
7777
}
7878
@Override
79-
public void delete( final List< T > entities ){
80-
Preconditions.checkNotNull( entities );
81-
82-
this.getDao().delete( entities );
83-
}
84-
@Override
8579
public void delete( final long id ){
8680
this.getDao().delete( id );
8781
}

‎src/main/java/org/rest/persistence/service/IService.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public interface IService< T extends IEntity >{
2727

2828
void delete( final long id );
2929

30-
void delete( final List< T > entities );
31-
3230
void deleteAll();
3331

3432
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.rest.sec.dto;
2+
3+
import org.rest.sec.model.Principal;
4+
5+
import com.google.common.base.Function;
6+
7+
public final class PrincipalToUserFunction implements Function< Principal, User >{
8+
9+
@Override
10+
public final User apply( final Principal principal ){
11+
return new User( principal );
12+
}
13+
14+
}

‎src/main/java/org/rest/sec/dto/User.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class User implements IEntity{
2121
private Long id;
2222

2323
private String name;
24+
private String password;
2425

2526
/* Marshalling */
2627
// - note: this gets rid of the collection entirely
@@ -32,10 +33,11 @@ public class User implements IEntity{
3233
public User(){
3334
super();
3435
}
35-
public User( final String nameToSet ){
36+
public User( final String nameToSet, final String passwordToSet ){
3637
super();
3738

3839
name = nameToSet;
40+
password = passwordToSet;
3941
}
4042
public User( final Principal principal ){
4143
super();
@@ -62,6 +64,13 @@ public void setName( final String nameToSet ){
6264
name = nameToSet;
6365
}
6466

67+
public String getPassword(){
68+
return password;
69+
}
70+
public void setPassword( final String passwordToSet ){
71+
password = passwordToSet;
72+
}
73+
6574
public Set< Role > getRoles(){
6675
return roles;
6776
}

‎src/main/java/org/rest/sec/model/Principal.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ public class Principal implements IEntity{
3838
public Principal(){
3939
super();
4040
}
41-
public Principal( final String nameToSet, final String passwordToSet ){
41+
public Principal( final String nameToSet, final String passwordToSet, final Set< Role > rolesToSet ){
4242
super();
4343

4444
name = nameToSet;
4545
password = passwordToSet;
46+
roles = rolesToSet;
4647
}
4748

4849
// API

‎src/main/java/org/rest/sec/persistence/service/impl/dto/UserServiceImpl.java‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
import java.util.List;
44

5+
import org.rest.sec.dto.PrincipalToUserFunction;
56
import org.rest.sec.dto.User;
67
import org.rest.sec.model.Principal;
8+
import org.rest.sec.model.Role;
79
import org.rest.sec.persistence.service.IPrincipalService;
810
import org.rest.sec.persistence.service.dto.IUserService;
911
import org.springframework.beans.factory.annotation.Autowired;
1012
import org.springframework.data.domain.Page;
13+
import org.springframework.data.domain.PageImpl;
14+
import org.springframework.data.domain.PageRequest;
15+
import org.springframework.data.domain.Sort;
1116
import org.springframework.stereotype.Service;
1217
import org.springframework.transaction.annotation.Transactional;
1318

19+
import com.google.common.collect.Lists;
20+
import com.google.common.collect.Sets;
21+
1422
@Service
1523
@Transactional
1624
public class UserServiceImpl implements IUserService{
@@ -24,6 +32,8 @@ public UserServiceImpl(){
2432

2533
// API
2634

35+
// find - one
36+
2737
@Override
2838
public User findByName( final String name ){
2939
final Principal principal = principalService.findByName( name );
@@ -32,42 +42,59 @@ public User findByName( final String name ){
3242

3343
@Override
3444
public User findOne( final long id ){
35-
return null;
45+
final Principal principal = principalService.findOne( id );
46+
return new User( principal );
3647
}
3748

49+
// find - many
50+
3851
@Override
3952
public List< User > findAll(){
40-
return null;
53+
final List< Principal > allPrincipalEntities = principalService.findAll();
54+
final List< User > allUsers = Lists.transform( allPrincipalEntities, new PrincipalToUserFunction() );
55+
56+
return allUsers;
4157
}
4258

4359
@Override
4460
public Page< User > findPaginated( final int page, final int size, final String sortBy ){
45-
return null;
61+
final Page< Principal > principalsPaginated = principalService.findPaginated( page, size, sortBy );
62+
final List< User > usersPaginated = Lists.transform( principalsPaginated.getContent(), new PrincipalToUserFunction() );
63+
64+
Sort sortInfo = null;
65+
if( sortBy != null ){
66+
sortInfo = new Sort( sortBy );
67+
}
68+
return new PageImpl< User >( usersPaginated, new PageRequest( page, size, sortInfo ), principalsPaginated.getTotalElements() );
4669
}
4770

71+
// create
72+
4873
@Override
4974
public User create( final User entity ){
50-
return null;
75+
final Principal newPrincipalEntity = new Principal( entity.getName(), entity.getPassword(), Sets.<Role> newHashSet() );
76+
principalService.create( newPrincipalEntity );
77+
entity.setId( newPrincipalEntity.getId() );
78+
return entity;
5179
}
5280

81+
// update
82+
5383
@Override
5484
public void update( final User entity ){
55-
//
85+
throw new UnsupportedOperationException();
5686
}
5787

58-
@Override
59-
public void delete( final long id ){
60-
//
61-
}
88+
// delete
6289

6390
@Override
64-
public void delete( final List< User > entities ){
65-
//
91+
public void delete( final long id ){
92+
principalService.delete( id );
6693
}
6794

6895
@Override
6996
public void deleteAll(){
70-
//
97+
principalService.deleteAll();
7198
}
7299

73100
}

‎src/main/java/org/rest/sec/persistence/setup/SecuritySetup.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,9 @@ final void createPrincipals(){
101101
final void createPrincipalIfNotExisting( final String loginName, final String pass, final Set< Role > roles ){
102102
final Principal entityByName = principalService.findByName( loginName );
103103
if( entityByName == null ){
104-
final Principal entity = buildPrincipal( loginName, pass, roles );
104+
final Principal entity = new Principal( loginName, pass, roles );
105105
principalService.create( entity );
106106
}
107107
}
108-
final Principal buildPrincipal( final String name, final String pass, final Set< Role > roles ){
109-
final Principal principal = new Principal( name, pass );
110-
principal.setRoles( roles );
111-
return principal;
112-
}
113108

114109
}

‎src/main/java/org/rest/sec/web/controller/AuthenticationController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public AuthenticationController(){
3535
public User getAuthentication(){
3636
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
3737

38-
final User authenticationResource = new User( authentication.getName() );
38+
final User authenticationResource = new User( authentication.getName(), null );
3939

4040
final Function< GrantedAuthority, Privilege > springAuthorityToPrivilegeFunction = new Function< GrantedAuthority, Privilege >(){
4141
@Override

‎src/main/java/org/rest/web/common/AbstractController.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ protected final void createInternal( final T resource, final UriComponentsBuilde
124124
throw new ConflictException( dataEx );
125125
}
126126

127-
eventPublisher.publishEvent( new ResourceCreatedEvent< T >( clazz, uriBuilder, response, resource.getId() ) );
127+
// - note: mind the autoboxing and potential NPE when the resource has null id at this point (likely when working with DTOs)
128+
final ResourceCreatedEvent< T > resourceCreatedEvent = new ResourceCreatedEvent< T >( clazz, uriBuilder, response, resource.getId() );
129+
eventPublisher.publishEvent( resourceCreatedEvent );
128130
}
129131

130132
// update

0 commit comments

Comments
 (0)