To customize rating value transformation, you must create an OSGi component that
implements
RatingsDataTransformer
.
The steps here show you how. For a detailed explanation of these steps and
rating value transformation, see
Rating Value Transformation.
-
Create an OSGi component class that implements
RatingsDataTransformer
:@Component public class DummyRatingsDataTransformer implements RatingsDataTransformer {...
-
In this class, implement the
transformRatingsData
method. Note that it contains theRatingsType
variablesfromRatingsType
andtoRatingsType
:@Override public ActionableDynamicQuery.PerformActionMethod transformRatingsData( final RatingsType fromRatingsType, final RatingsType toRatingsType) throws PortalException { }
-
In the
transformRatingsData
method, implement the interfaceActionableDynamicQuery.PerformActionMethod
as an anonymous inner class:return new ActionableDynamicQuery.PerformActionMethod() { };
-
In the anonymous
ActionableDynamicQuery.PerformActionMethod
implementation, implement theperformAction
method to perform your transformation:@Override public void performAction(Object object) throws PortalException { if (fromRatingsType.getValue().equals(RatingsType.LIKE) && toRatingsType.getValue().equals(RatingsType.STARS)) { RatingsEntry ratingsEntry = (RatingsEntry) object; ratingsEntry.setScore(0); RatingsEntryLocalServiceUtil.updateRatingsEntry( ratingsEntry); } }
This example irreversibly transforms the rating type from likes to stars, resetting the value to
0
. Theif
statement uses thefromRatingsType
andtoRatingsType
values to specify that the transformation only occurs when going from likes to stars. The transformation is performed viaRatingsEntry
and its-LocalServiceUtil
. After getting aRatingsEntry
object, itssetScore
method sets the rating score to0
. TheRatingsEntryLocalServiceUtil
methodupdateRatingsEntry
then updates theRatingsEntry
in the database.
Here’s the complete class for this example:
@Component
public class DummyRatingsDataTransformer implements RatingsDataTransformer {
@Override
public ActionableDynamicQuery.PerformActionMethod transformRatingsData(
final RatingsType fromRatingsType, final RatingsType toRatingsType)
throws PortalException {
return new ActionableDynamicQuery.PerformActionMethod() {
@Override
public void performAction(Object object)
throws PortalException {
if (fromRatingsType.getValue().equals(RatingsType.LIKE) &&
toRatingsType.getValue().equals(RatingsType.STARS)) {
RatingsEntry ratingsEntry = (RatingsEntry) object;
ratingsEntry.setScore(0);
RatingsEntryLocalServiceUtil.updateRatingsEntry(
ratingsEntry);
}
}
};
}
}