//--- Service
//---
//--------------------------------------------------------------------------
public Element exec(Element params, ServiceContext context) throws Exception {
GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
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);
Sort order = null;
if(sortBy.equals("date")) {
order = new Sort(Sort.Direction.DESC, Metadata_.dataInfo + "." + MetadataDataInfo_.changeDate);
} else if(sortBy.equals("popularity")) {
order = new Sort(Sort.Direction.DESC, Metadata_.dataInfo + "." + MetadataDataInfo_.popularity);
} else if(sortBy.equals("rating")) {
order = new Sort(Sort.Direction.DESC, Metadata_.dataInfo + "." + MetadataDataInfo_.rating);
} else {
throw new IllegalArgumentException("Unknown sortBy parameter: "+sortBy);
}
List<Metadata> metadataList = context.getBean(MetadataRepository.class).findAll(spec, order);
_response = new Element("response");
for (Metadata rec : metadataList) {
String id = "" + rec.getId();
boolean forEditing = false, withValidationErrors = false, keepXlinkAttributes = false;
Element md = gc.getBean(DataManager.class).getMetadata(context, id, forEditing, withValidationErrors, keepXlinkAttributes);
_response.addContent(md);
}
Element currentSortBySelect = new Element(SORT_BY);
currentSortBySelect.setText(sortBy);