forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
265 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package org.baeldung; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import org.baeldung.persistence.dao.RoleRepository; | ||
import org.baeldung.persistence.dao.UserRepository; | ||
import org.baeldung.persistence.model.Role; | ||
import org.baeldung.persistence.model.User; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
@@ -13,13 +18,23 @@ public class Setup { | |
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private RoleRepository roleRepository; | ||
|
||
@PostConstruct | ||
private void setupData() { | ||
final User user = new User(); | ||
user.setFirstName("john"); | ||
user.setLastName("doe"); | ||
user.setEmail("[email protected]"); | ||
userRepository.save(user); | ||
Role roleUser = new Role("ROLE_USER"); | ||
roleUser = roleRepository.save(roleUser); | ||
Role roleAdmin = new Role("ROLE_ADMIN"); | ||
roleAdmin = roleRepository.save(roleAdmin); | ||
|
||
final User userJohn = new User("john", "[email protected]"); | ||
userJohn.setRoles(new HashSet<Role>(Arrays.asList(roleUser, roleAdmin))); | ||
userRepository.save(userJohn); | ||
|
||
final User userTom = new User("tom", "[email protected]"); | ||
userTom.setRoles(new HashSet<Role>(Arrays.asList(roleUser))); | ||
userRepository.save(userTom); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
spring-katharsis/src/main/java/org/baeldung/persistence/dao/RoleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.baeldung.persistence.dao; | ||
|
||
import org.baeldung.persistence.model.Role; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface RoleRepository extends JpaRepository<Role, Long> { | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...ng-katharsis/src/main/java/org/baeldung/persistence/katharsis/RoleResourceRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.baeldung.persistence.katharsis; | ||
|
||
import io.katharsis.queryParams.RequestParams; | ||
import io.katharsis.repository.ResourceRepository; | ||
|
||
import org.baeldung.persistence.dao.RoleRepository; | ||
import org.baeldung.persistence.model.Role; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class RoleResourceRepository implements ResourceRepository<Role, Long> { | ||
|
||
@Autowired | ||
private RoleRepository roleRepository; | ||
|
||
@Override | ||
public Role findOne(Long id, RequestParams params) { | ||
return roleRepository.findOne(id); | ||
} | ||
|
||
@Override | ||
public Iterable<Role> findAll(RequestParams params) { | ||
return roleRepository.findAll(); | ||
} | ||
|
||
@Override | ||
public Iterable<Role> findAll(Iterable<Long> ids, RequestParams params) { | ||
return roleRepository.findAll(ids); | ||
} | ||
|
||
@Override | ||
public <S extends Role> S save(S entity) { | ||
return roleRepository.save(entity); | ||
} | ||
|
||
@Override | ||
public void delete(Long id) { | ||
roleRepository.delete(id); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...is/src/main/java/org/baeldung/persistence/katharsis/UserToRoleRelationshipRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.baeldung.persistence.katharsis; | ||
|
||
import io.katharsis.queryParams.RequestParams; | ||
import io.katharsis.repository.RelationshipRepository; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.baeldung.persistence.dao.RoleRepository; | ||
import org.baeldung.persistence.dao.UserRepository; | ||
import org.baeldung.persistence.model.Role; | ||
import org.baeldung.persistence.model.User; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class UserToRoleRelationshipRepository implements RelationshipRepository<User, Long, Role, Long> { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private RoleRepository roleRepository; | ||
|
||
@Override | ||
public void setRelation(User User, Long roleId, String fieldName) { | ||
// not for many-to-many | ||
} | ||
|
||
@Override | ||
public void setRelations(User user, Iterable<Long> roleIds, String fieldName) { | ||
final Set<Role> roles = new HashSet<Role>(); | ||
roles.addAll(roleRepository.findAll(roleIds)); | ||
user.setRoles(roles); | ||
userRepository.save(user); | ||
} | ||
|
||
@Override | ||
public void addRelations(User user, Iterable<Long> roleIds, String fieldName) { | ||
final Set<Role> roles = user.getRoles(); | ||
roles.addAll(roleRepository.findAll(roleIds)); | ||
user.setRoles(roles); | ||
userRepository.save(user); | ||
} | ||
|
||
@Override | ||
public void removeRelations(User user, Iterable<Long> roleIds, String fieldName) { | ||
final Set<Role> roles = user.getRoles(); | ||
roles.removeAll(roleRepository.findAll(roleIds)); | ||
user.setRoles(roles); | ||
userRepository.save(user); | ||
} | ||
|
||
@Override | ||
public Role findOneTarget(Long sourceId, String fieldName, RequestParams requestParams) { | ||
// not for many-to-many | ||
return null; | ||
} | ||
|
||
@Override | ||
public Iterable<Role> findManyTargets(Long sourceId, String fieldName, RequestParams requestParams) { | ||
final User user = userRepository.findOne(sourceId); | ||
return user.getRoles(); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
spring-katharsis/src/main/java/org/baeldung/persistence/model/Role.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.baeldung.persistence.model; | ||
|
||
import io.katharsis.resource.annotations.JsonApiId; | ||
import io.katharsis.resource.annotations.JsonApiResource; | ||
import io.katharsis.resource.annotations.JsonApiToMany; | ||
|
||
import java.util.Set; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.ManyToMany; | ||
|
||
@Entity | ||
@JsonApiResource(type = "roles") | ||
public class Role { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
@JsonApiId | ||
private Long id; | ||
|
||
@Column(nullable = false, unique = true) | ||
private String name; | ||
|
||
@ManyToMany(mappedBy = "roles") | ||
@JsonApiToMany | ||
private Set<User> users; | ||
|
||
// | ||
public Role() { | ||
super(); | ||
} | ||
|
||
public Role(String name) { | ||
super(); | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
// | ||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + (id == null ? 0 : id.hashCode()); | ||
result = prime * result + (name == null ? 0 : name.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
final Role other = (Role) obj; | ||
if (id == null) { | ||
if (other.id != null) | ||
return false; | ||
} else if (!id.equals(other.id)) | ||
return false; | ||
if (name == null) { | ||
if (other.name != null) | ||
return false; | ||
} else if (!name.equals(other.name)) | ||
return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder builder = new StringBuilder(); | ||
builder.append("Role [id=").append(id).append(", name=").append(name).append("]"); | ||
return builder.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters