Customizing Rating Value Transformation

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.

  1. Create an OSGi component class that implements RatingsDataTransformer:

    @Component
    public class DummyRatingsDataTransformer implements RatingsDataTransformer {...
    
  2. In this class, implement the transformRatingsData method. Note that it contains the RatingsType variables fromRatingsType and toRatingsType:

    @Override
    public ActionableDynamicQuery.PerformActionMethod transformRatingsData(
            final RatingsType fromRatingsType, final RatingsType toRatingsType)
        throws PortalException {
    
    }
    
  3. In the transformRatingsData method, implement the interface ActionableDynamicQuery.PerformActionMethod as an anonymous inner class:

    return new ActionableDynamicQuery.PerformActionMethod() {
    
    };
    
  4. In the anonymous ActionableDynamicQuery.PerformActionMethod implementation, implement the performAction 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. The if statement uses the fromRatingsType and toRatingsType values to specify that the transformation only occurs when going from likes to stars. The transformation is performed via RatingsEntry and its -LocalServiceUtil. After getting a RatingsEntry object, its setScore method sets the rating score to 0. The RatingsEntryLocalServiceUtil method updateRatingsEntry then updates the RatingsEntry 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);
                }
            }
        };
    }

}

Rating Value Transformation

Implementing Rating Type Selection

Rating Assets

« Implementing Rating Type SelectionFlagging Inappropriate Asset Content »
Was this article helpful?
0 out of 0 found this helpful