int ownerId = context.getUserSession().getUserIdAsInt();
Profile userProfile = context.getUserSession().getProfile();
if (userProfile == null) {
throw new OperationNotAllowedEx("Unauthorized user attempted to list editable metadata ");
}
Specifications<Metadata> spec;
// if the user is an admin, return all metadata
if(userProfile == Profile.Administrator) {
spec = where(MetadataSpecs.isHarvested(false));
} else if(userProfile == Profile.Reviewer || userProfile == Profile.UserAdmin) {
final List<UserGroup> groups = context.getBean(UserGroupRepository.class).findAll(UserGroupSpecs.hasUserId(ownerId));
List<Integer> groupIds = Lists.transform(groups, new Function<UserGroup, Integer>() {
@Nullable
@Override
public Integer apply(@Nonnull UserGroup input) {
return input.getId().getGroupId();
}
});
spec = where(MetadataSpecs.isHarvested(false)).and(MetadataSpecs.isOwnedByOneOfFollowingGroups(groupIds));
// if the user is a reviewer, return all metadata of the user's groups
} else if(userProfile == Profile.Editor) {
spec = where(MetadataSpecs.isOwnedByUser(ownerId)).and(MetadataSpecs.isHarvested(false));
// if the user is an editor, return metadata owned by this user
} else {
throw new OperationNotAllowedEx("Unauthorized user " + ownerId + " attempted to list editable metadata ");
}
// Sorting
String sortBy = sortByParameter(params);