Skip to content

Commit

Permalink
Added favorite information to extJS resource get operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
axl8713 committed Jan 29, 2025
1 parent 458465f commit 0503d50
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
import javax.xml.bind.annotation.XmlRootElement;

/**
* An extended version of the {@link Resource} class that includes additional permission flags to
* indicate whether the resource can be edited, deleted, or copied.
* An extended version of the {@link Resource} class that includes additional flags to indicate
* whether the resource can be edited, deleted, or copied and if the resource is a user favorite.
*/
@XmlRootElement(name = "Resource")
public class ExtResource extends Resource {

@XmlElement private boolean canEdit;
@XmlElement private boolean canDelete;
@XmlElement private boolean canCopy;
@XmlElement private boolean isFavorite;

public ExtResource() {}

Expand All @@ -37,6 +38,7 @@ private ExtResource(Builder builder) {
this.canEdit = builder.canEdit;
this.canDelete = builder.canDelete;
this.canCopy = builder.canCopy;
this.isFavorite = builder.isFavorite;
}

public boolean isCanEdit() {
Expand All @@ -51,6 +53,10 @@ public boolean isCanCopy() {
return canCopy;
}

public boolean isFavorite() {
return isFavorite;
}

public static Builder builder(Resource resource) {
return new Builder(resource);
}
Expand All @@ -60,6 +66,7 @@ public static class Builder {
private boolean canEdit;
private boolean canDelete;
private boolean canCopy;
private boolean isFavorite;

private Builder(Resource resource) {
this.resource = resource;
Expand All @@ -80,6 +87,11 @@ public Builder withCanCopy(boolean canCopy) {
return this;
}

public Builder withIsFavorite(boolean isFavorite) {
this.isFavorite = isFavorite;
return this;
}

public ExtResource build() {
return new ExtResource(this);
}
Expand All @@ -91,11 +103,14 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
ExtResource that = (ExtResource) o;
return canEdit == that.canEdit && canDelete == that.canDelete && canCopy == that.canCopy;
return canEdit == that.canEdit
&& canDelete == that.canDelete
&& canCopy == that.canCopy
&& isFavorite == that.isFavorite;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), canEdit, canDelete, canCopy);
return Objects.hash(super.hashCode(), canEdit, canDelete, canCopy, isFavorite);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

/**
* An extended version of the {@link ShortResource} class that includes {@link ShortAttributeList}
* and {@link SecurityRuleList} for the resource.
* and {@link SecurityRuleList} for the resource along its favourite status.
*/
@XmlRootElement(name = "ShortResource")
public class ExtShortResource extends ShortResource {

@XmlElement private ShortAttributeList attributeList;
@XmlElement private SecurityRuleList securityRuleList;
@XmlElement private TagList tagList;
@XmlElement private boolean isFavorite;

public ExtShortResource() {}

Expand All @@ -35,6 +36,7 @@ private ExtShortResource(Builder builder) {
this.attributeList = builder.attributeList;
this.securityRuleList = builder.securityRuleList;
this.tagList = builder.tagList;
this.isFavorite = builder.isFavorite;
}

public ShortAttributeList getAttributeList() {
Expand All @@ -49,15 +51,20 @@ public TagList getTagList() {
return tagList;
}

public boolean isFavorite() {
return isFavorite;
}

public static Builder builder(ShortResource resource) {
return new Builder(resource);
}

public static class Builder {
private final ShortResource shortResource;
public ShortAttributeList attributeList;
public SecurityRuleList securityRuleList;
public TagList tagList;
private ShortAttributeList attributeList;
private SecurityRuleList securityRuleList;
private TagList tagList;
private boolean isFavorite;

private Builder(ShortResource shortResource) {
this.shortResource = shortResource;
Expand All @@ -78,6 +85,11 @@ public Builder withTagList(TagList tagList) {
return this;
}

public Builder withIsFavorite(boolean isFavorite) {
this.isFavorite = isFavorite;
return this;
}

public ExtShortResource build() {
return new ExtShortResource(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,6 @@ private List<Resource> filterOutUnavailableResources(List<Resource> resources, U

userService.fetchSecurityRules(user);

// resourceService.(user);

return resources.stream()
.filter(r -> resourcePermissionService.isResourceAvailableForUser(r, user))
.collect(Collectors.toList());
Expand All @@ -414,6 +412,7 @@ private List<Resource> filterOutUnavailableResources(List<Resource> resources, U
private List<ExtResource> convertToExtResources(List<Resource> foundResources, User user) {

userService.fetchSecurityRules(user);
userService.fetchFavorites(user);

return foundResources.stream()
.map(r -> convertToExtResource(r, user))
Expand All @@ -431,9 +430,19 @@ private ExtResource convertToExtResource(Resource resource, User user) {
extResourceBuilder.withCanEdit(true).withCanDelete(true);
}

if (user != null) {
extResourceBuilder.withIsFavorite(isResourceUserFavorite(resource, user));
}

return extResourceBuilder.build();
}

private boolean isResourceUserFavorite(Resource resource, User user) {
return user.getFavorites().stream()
.map(Resource::getId)
.anyMatch(id -> id.equals(resource.getId()));
}

/**
* @param success
* @param count
Expand Down Expand Up @@ -693,6 +702,7 @@ public ExtShortResource getExtResource(

User authUser = extractAuthUser(sc);
userService.fetchSecurityRules(authUser);
userService.fetchFavorites(authUser);

if (!resourcePermissionService.canUserReadResource(authUser, id)) {
throw new ForbiddenErrorWebEx("Resource is protected");
Expand All @@ -708,6 +718,7 @@ public ExtShortResource getExtResource(
.withAttributes(createShortAttributeList(resource.getAttribute()))
.withSecurityRules(new SecurityRuleList(resource.getSecurity()))
.withTagList(createTagList(resource.getTags()))
.withIsFavorite(isResourceUserFavorite(resource, authUser))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,105 @@ public void testExtResourcesList_withTags() throws Exception {
}
}

@Test
public void testExtResourcesList_favoritesOnly() throws Exception {
final String CAT0_NAME = "CAT000";

long userId = restCreateUser("u0", Role.USER, null, "p0");
SecurityContext sc = new SimpleSecurityContext(userId);

createCategory(CAT0_NAME);

long favoriteResourceId =
restCreateResource("favourite_resource", "", CAT0_NAME, userId, true);
restCreateResource("other_resource", "", CAT0_NAME, userId, true);

favoriteService.addFavorite(userId, favoriteResourceId);

{
ExtResourceList response =
restExtJsService.getExtResourcesList(
sc,
0,
100,
new Sort("", ""),
false,
false,
false,
true,
new AndFilter());

List<ExtResource> resources = response.getList();
assertEquals(1, resources.size());
ExtResource resource = resources.get(0);
assertEquals(favoriteResourceId, resource.getId().longValue());
assertEquals(1, response.getCount());
}

{
ExtResourceList response =
restExtJsService.getExtResourcesList(
sc,
0,
100,
new Sort("", ""),
false,
false,
false,
false,
new AndFilter());

List<ExtResource> resources = response.getList();
assertEquals(2, resources.size());
assertEquals(2, response.getCount());
}
}

@Test
public void testExtResourcesList_withFavoriteInformation() throws Exception {
final String CAT0_NAME = "CAT000";

long userId = restCreateUser("u0", Role.USER, null, "p0");
SecurityContext sc = new SimpleSecurityContext(userId);

createCategory(CAT0_NAME);

long favoriteResourceId =
restCreateResource("favourite_resource", "", CAT0_NAME, userId, true);
long nonFavoriteResourceId =
restCreateResource("other_resource", "", CAT0_NAME, userId, true);

favoriteService.addFavorite(userId, favoriteResourceId);

{
ExtResourceList response =
restExtJsService.getExtResourcesList(
sc,
0,
100,
new Sort("", ""),
false,
false,
false,
false,
new AndFilter());

List<ExtResource> resources = response.getList();
ExtResource favoriteResource =
resources.stream()
.filter(r -> r.getId().equals(favoriteResourceId))
.findFirst()
.orElseThrow();
assertTrue(favoriteResource.isFavorite());
ExtResource nonFavoriteResource =
resources.stream()
.filter(r -> r.getId().equals(nonFavoriteResourceId))
.findFirst()
.orElseThrow();
assertFalse(nonFavoriteResource.isFavorite());
}
}

@Test
public void testGetExtResource_userOwnedWithAttributesInformation() throws Exception {
final String CAT0_NAME = "CAT000";
Expand Down Expand Up @@ -1775,56 +1874,33 @@ public void testGetExtResource_withTags() throws Exception {
}

@Test
public void testExtResourcesList_favoritesOnly() throws Exception {
public void testGetExtResource_withFavoriteInformation() throws Exception {
final String CAT0_NAME = "CAT000";

long userId = restCreateUser("u0", Role.USER, null, "p0");
SecurityContext sc = new SimpleSecurityContext(userId);
SecurityContext userSecurityContext = new SimpleSecurityContext(userId);

createCategory(CAT0_NAME);

long favoriteResourceId =
restCreateResource("favourite_resource", "", CAT0_NAME, userId, true);
restCreateResource("other_resource", "", CAT0_NAME, userId, true);
long nonFavoriteResourceId =
restCreateResource("other_resource", "", CAT0_NAME, userId, true);

favoriteService.addFavorite(userId, favoriteResourceId);

{
ExtResourceList response =
restExtJsService.getExtResourcesList(
sc,
0,
100,
new Sort("", ""),
false,
false,
false,
true,
new AndFilter());

List<ExtResource> resources = response.getList();
assertEquals(1, resources.size());
ExtResource resource = resources.get(0);
assertEquals(favoriteResourceId, resource.getId().longValue());
assertEquals(1, response.getCount());
ExtShortResource response =
restExtJsService.getExtResource(
userSecurityContext, favoriteResourceId, false, false, true);
assertTrue(response.isFavorite());
}

{
ExtResourceList response =
restExtJsService.getExtResourcesList(
sc,
0,
100,
new Sort("", ""),
false,
false,
false,
false,
new AndFilter());

List<ExtResource> resources = response.getList();
assertEquals(2, resources.size());
assertEquals(2, response.getCount());
ExtShortResource response =
restExtJsService.getExtResource(
userSecurityContext, nonFavoriteResourceId, false, false, true);
assertFalse(response.isFavorite());
}
}

Expand Down

0 comments on commit 0503d50

Please sign in to comment.