Search in sources :

Example 1 with RatingServiceException

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());
    }
}
Also used : RatingServiceException(org.alfresco.service.cmr.rating.RatingServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)

Example 2 with RatingServiceException

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;
}
Also used : RatingServiceException(org.alfresco.service.cmr.rating.RatingServiceException) RatingScheme(org.alfresco.service.cmr.rating.RatingScheme) Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) Extend(org.alfresco.traitextender.Extend)

Example 3 with RatingServiceException

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);
}
Also used : RatingServiceException(org.alfresco.service.cmr.rating.RatingServiceException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RatingScheme(org.alfresco.service.cmr.rating.RatingScheme) Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Date(java.util.Date)

Example 4 with RatingServiceException

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());
    }
}
Also used : RatingServiceException(org.alfresco.service.cmr.rating.RatingServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName)

Aggregations

RatingServiceException (org.alfresco.service.cmr.rating.RatingServiceException)4 QName (org.alfresco.service.namespace.QName)3 Serializable (java.io.Serializable)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 RatingScheme (org.alfresco.service.cmr.rating.RatingScheme)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 Extend (org.alfresco.traitextender.Extend)1