Files
alfresco-community-repo/source/java/org/alfresco/repo/rating/script/ScriptRatingService.java
Raluca Munteanu 8674e2bfc8 Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
      125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@125781 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-04-26 12:48:49 +00:00

179 lines
6.3 KiB
Java

package org.alfresco.repo.rating.script;
import java.util.Date;
import java.util.Set;
import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.rating.Rating;
import org.alfresco.service.cmr.rating.RatingService;
import org.alfresco.service.cmr.rating.RatingServiceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Script object representing the rating service.
*
* @author Neil McErlean
* @since 3.4
*/
public class ScriptRatingService extends BaseScopableProcessorExtension
{
private static Log logger = LogFactory.getLog(ScriptRatingService.class);
/** The Services registry */
private ServiceRegistry serviceRegistry;
private RatingService ratingService;
/**
* Set the service registry
*
* @param serviceRegistry the service registry.
*/
public void setServiceRegistry(ServiceRegistry serviceRegistry)
{
this.serviceRegistry = serviceRegistry;
this.ratingService = serviceRegistry.getRatingService();
}
/**
* Gets the names for rating schemes currently in the system.
* @return String[]
*/
public String[] getRatingSchemeNames()
{
Set<String> schemeNames = ratingService.getRatingSchemes().keySet();
String[] result = new String[0];
result = schemeNames.toArray(result);
return result;
}
/**
* Gets the minimum allowed rating for the specified rating scheme.
*
* @param ratingSchemeName String
* @return float
*/
public float getMin(String ratingSchemeName)
{
return ratingService.getRatingScheme(ratingSchemeName).getMinRating();
}
/**
* Gets the maximum allowed rating for the specified rating scheme.
*
* @param ratingSchemeName String
* @return float
*/
public float getMax(String ratingSchemeName)
{
return ratingService.getRatingScheme(ratingSchemeName).getMaxRating();
}
/**
* This method checks whether self-rating is allowed for the specified rating scheme.
* If self-rating is allowed in the specified scheme, then the cm:creator of a node can apply a rating,
* otherwise they cannot.
*
* @param ratingSchemeName the rating scheme bean name.
* @return <code>true</code> if users can rate their own content, else <code>false</code>.
*/
public boolean isSelfRatingAllowed(String ratingSchemeName)
{
return ratingService.getRatingScheme(ratingSchemeName).isSelfRatingAllowed();
}
/**
* Applies the given rating to the specified node using the specified ratingScheme.
* It is the responsibility of the caller to ensure that the rating scheme exists
* and that the rating is within the limits defined for that scheme.
* <p/>Furthermore, only one rating scheme per user per target node is supported. Any attempt
* by one user to apply a second rating in a different scheme will result in a {@link RatingServiceException}.
*
* @param node ScriptNode
* @param rating float
* @param ratingSchemeName String
* @throws RatingServiceException
* @see ScriptRatingService#getMin(String)
* @see ScriptRatingService#getMax(String)
*/
public void applyRating(ScriptNode node, float rating, String ratingSchemeName)
{
ratingService.applyRating(node.getNodeRef(), rating, ratingSchemeName);
}
/**
* Removes any rating by the current user in the specified scheme from the specified
* noderef.
* @param node ScriptNode
* @param ratingSchemeName String
*/
public void removeRating(ScriptNode node, String ratingSchemeName)
{
ratingService.removeRatingByCurrentUser(node.getNodeRef(), ratingSchemeName);
}
/**
* Gets the rating applied to the specified node in the specified scheme by
* the currently authenticated user.
* @param node ScriptNode
* @param ratingSchemeName String
* @return rating if there is one, else -1.
* TODO -1 could be a valid rating.
*/
public float getRating(ScriptNode node, String ratingSchemeName)
{
final Rating ratingByCurrentUser = ratingService.getRatingByCurrentUser(node.getNodeRef(), ratingSchemeName);
return ratingByCurrentUser == null ? -1f : ratingByCurrentUser.getScore();
}
/**
* Gets the rating applied date for the specified node in the specified scheme by
* the currently authenticated user.
* @param node ScriptNode
* @param ratingSchemeName String
* @return rating applied date if there is one, else <code>null</code>
*/
public Date getRatingAppliedAt(ScriptNode node, String ratingSchemeName)
{
final Rating ratingByCurrentUser = ratingService.getRatingByCurrentUser(node.getNodeRef(), ratingSchemeName);
return ratingByCurrentUser == null ? null : ratingByCurrentUser.getAppliedAt();
}
/**
* Gets the number of ratings applied to the specified node by all users in the specified
* scheme.
* @param node ScriptNode
* @param ratingSchemeName String
* @return int
*/
public int getRatingsCount(ScriptNode node, String ratingSchemeName)
{
return ratingService.getRatingsCount(node.getNodeRef(), ratingSchemeName);
}
/**
* Gets the total (sum) rating by all users on the specified node in the specified scheme.
* @param node ScriptNode
* @param ratingSchemeName String
* @return float
*/
public float getTotalRating(ScriptNode node, String ratingSchemeName)
{
return ratingService.getTotalRating(node.getNodeRef(), ratingSchemeName);
}
/**
* Gets the average (mean) rating by all users on the specified node in the specified scheme.
* @param node ScriptNode
* @param ratingSchemeName * @param ratingSchemeName String
* @return float
*/
public float getAverageRating(ScriptNode node, String ratingSchemeName)
{
return ratingService.getAverageRating(node.getNodeRef(), ratingSchemeName);
}
}