use of org.alfresco.service.cmr.rating.RatingServiceException in project alfresco-remote-api by Alfresco.
the class FiveStarRatingScheme method applyRating.
@Override
public void applyRating(NodeRef nodeRef, Object rating) {
try {
Float ratingServiceRating = getRatingServiceRating(rating);
ratingService.applyRating(nodeRef, ratingServiceRating, getRatingServiceName());
} catch (RatingServiceException e) {
throw new InvalidArgumentException(e.getMessage());
}
}
use of org.alfresco.service.cmr.rating.RatingServiceException in project alfresco-repository by Alfresco.
the class RatingServiceImpl method getRatingRollup.
/*
* (non-Javadoc)
* @see org.alfresco.service.cmr.rating.RatingService#getRatingRollup(org.alfresco.service.cmr.repository.NodeRef, java.lang.String, java.lang.String)
*/
@Extend(traitAPI = RatingServiceTrait.class, extensionAPI = RatingServiceExtension.class)
public Serializable getRatingRollup(NodeRef targetNode, String ratingSchemeName, String ratingRollupName) {
RatingScheme scheme = schemeRegistry.getRatingSchemes().get(ratingSchemeName);
if (scheme == null) {
throw new RatingServiceException("Cannot retrieve rollup. Unrecognized rating scheme " + ratingSchemeName);
}
QName rollupAspectName = ratingNamingConventions.getRollupAspectNameFor(scheme);
Serializable result = null;
// If the rated node has the rollup aspect applied
if (nodeService.hasAspect(targetNode, rollupAspectName)) {
QName rollupPropertyName = ratingNamingConventions.getRollupPropertyNameFor(scheme, ratingRollupName);
result = nodeService.getProperty(targetNode, rollupPropertyName);
}
return result;
}
use of org.alfresco.service.cmr.rating.RatingServiceException in project alfresco-repository by Alfresco.
the class RatingServiceImpl method applyRating.
private void applyRating(NodeRef targetNode, float rating, String ratingSchemeName, final String userName) throws RatingServiceException {
if (log.isDebugEnabled()) {
StringBuilder msg = new StringBuilder();
msg.append("Applying rating ").append(rating).append(" in scheme ").append(ratingSchemeName).append(" as user ").append(userName).append(" on ").append(targetNode);
log.debug(msg.toString());
}
// Sanity check the rating scheme being used and the rating being applied.
final RatingScheme ratingScheme = this.getRatingScheme(ratingSchemeName);
if (ratingScheme == null) {
throw new RatingServiceException("Unrecognised rating scheme: " + ratingSchemeName);
}
if (rating < ratingScheme.getMinRating() || rating > ratingScheme.getMaxRating()) {
throw new RatingServiceException("Rating " + rating + " violates range for " + ratingScheme);
}
// To support rolling up rating totals, counts etc, we use aspects named by convention.
// Before we start writing data into the db, we'll check that an aspect has been defined
// with the expected name.
QName rollupAspectName = ratingNamingConventions.getRollupAspectNameFor(ratingScheme);
final boolean rollupAspectIsDefined = dictionaryService.getAspect(rollupAspectName) != null;
// to the modified, modifier properties on the rated node.
if (nodeService.hasAspect(targetNode, ContentModel.ASPECT_RATEABLE) == false) {
behaviourFilter.disableBehaviour(targetNode, ContentModel.ASPECT_AUDITABLE);
try {
// Add the cm:rateable aspect if it's not there already.
nodeService.addAspect(targetNode, ContentModel.ASPECT_RATEABLE, null);
// We'll also add the rollup aspect specific for this rating scheme - if one has been defined in the content model.
if (rollupAspectIsDefined) {
nodeService.addAspect(targetNode, rollupAspectName, null);
}
} finally {
behaviourFilter.enableBehaviour(targetNode, ContentModel.ASPECT_AUDITABLE);
}
}
// We're looking for child cm:rating nodes whose assoc qname matches the current user & rating scheme.
// i.e. we're looking for previously applied ratings by this user in this scheme.
// See RatingNamingConventionsUtil.java for details.
final QName assocQName = ratingNamingConventions.getRatingAssocNameFor(userName, ratingScheme.getName());
List<ChildAssociationRef> myRatingChildren = nodeService.getChildAssocs(targetNode, ContentModel.ASSOC_RATINGS, assocQName);
if (myRatingChildren.isEmpty()) {
// There are no previous ratings from this user/scheme combination, so we create a new cm:rating child node.
Map<QName, Serializable> ratingProps = new HashMap<QName, Serializable>();
ratingProps.put(ContentModel.PROP_RATING_SCORE, rating);
ratingProps.put(ContentModel.PROP_RATED_AT, new Date());
ratingProps.put(ContentModel.PROP_RATING_SCHEME, ratingSchemeName);
behaviourFilter.disableBehaviour(targetNode, ContentModel.ASPECT_AUDITABLE);
try {
nodeService.createNode(targetNode, ContentModel.ASSOC_RATINGS, assocQName, ContentModel.TYPE_RATING, ratingProps);
} finally {
behaviourFilter.enableBehaviour(targetNode, ContentModel.ASPECT_AUDITABLE);
}
} else {
// There are previous ratings by this user/ratingScheme combination.
NodeRef myPreviousRatingsNode = myRatingChildren.get(0).getChildRef();
Map<QName, Serializable> ratingProps = new HashMap<QName, Serializable>();
ratingProps.put(ContentModel.PROP_RATING_SCHEME, ratingSchemeName);
ratingProps.put(ContentModel.PROP_RATING_SCORE, rating);
ratingProps.put(ContentModel.PROP_RATED_AT, new Date());
nodeService.setProperties(myPreviousRatingsNode, ratingProps);
}
// Now that we have applied the rating, we need to recalculate the rollup properties.
recalculateRatingRollups(targetNode, ratingScheme);
}
use of org.alfresco.service.cmr.rating.RatingServiceException in project alfresco-remote-api by Alfresco.
the class LikesRatingScheme method applyRating.
@Override
public void applyRating(NodeRef nodeRef, Object rating) {
try {
Float ratingServiceRating = getRatingServiceRating(rating);
ratingService.applyRating(nodeRef, ratingServiceRating, getRatingServiceName());
QName nodeType = nodeService.getType(nodeRef);
boolean isContainer = dictionaryService.isSubClass(nodeType, ContentModel.TYPE_FOLDER) && !dictionaryService.isSubClass(nodeType, ContentModel.TYPE_SYSTEM_FOLDER);
postActivity(nodeRef, isContainer ? ActivityType.FOLDER_LIKED : ActivityType.FILE_LIKED);
} catch (RatingServiceException e) {
throw new InvalidArgumentException(e.getMessage());
}
}
Aggregations