Rating Content

graphics/rate_content.png

VCE uses the DRW framework to allow developers to implement this feature.

The Java class that provides this feature is com.ndc.qualification.ajax.QualificationPluginAjax. Developers can access various methods of this class using DWR. Please have a look at the JavaDoc of the class shipped with the distribution to get an overview of the methods that can be invoked.

The method we are the most interested in has the signature: StarRating submitStarRating(MetaData, Integer). Now, to be able to invoke this method using Javascript in the requesting user's browser, we need to perform the following steps:

  1. Define the bean in the Spring context. This can be achieved by adding the following XML segment in the spring bean definition XML (Sample configuration can be found in $VCE_HOME/misc/contrib/publication/WEB-INF/community-plugin-beans.xml).

    <bean id="qualificationPluginAjaxBean" class="com.ndc.qualification.ajax.QualificationPluginAjax" />
    <bean id="qualificationPluginAjaxInterceptor" class="com.ndc.auth.filter.ajax.SecurityInterceptor" />
    <bean id="qualificationPluginAjax" class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="target" ref="qualificationPluginAjaxBean" />
      <property name="interceptorNames">
        <idref local="qualificationPluginAjaxInterceptor" />
      </property>
    </bean>

    Note that the first bean declaration is of the class we are interested in. If we had declared the bean as follows: <bean id="qualificationPluginAjax" class="com.ndc.qualification.ajax.QualificationPluginAjax"/> and had left the other two bean declarations out, it would have worked as well. However, we then would not have any security constraints on the requesting user. Therefore, we have added the security interceptor to intercept method calls made to our bean qualificationPluginAjaxBean and named the proxy class qualificationPluginAjax.

  2. Allow method invocation through DWR. Since we will be calling the method submitStarRating, we need to enable this method by adding the following XML segment in the DWR configuration XML (in the demo web application it would be /WEB-INF/dwr.xml).

    <create creator="spring" javascript="QualificationPluginAjax">
      <param name="beanName" value="qualificationPluginAjax" />
      <include method="submitStarRating" />
      <include method="submitFlagging" />
      <include method="submitFavorite" />
      <include method="deleteFavorite" />
    </create>

    We have now enabled invocation of the above methods through DWR. Note the beanName property. The value is qualificationPluginAjax which refers to the proxy class that we declared earlier. Also note the value of the javascript attribute of the create element. DWR will bind the javascript variable needed to invoke the method with the name QualificationPluginAjax.

  3. Include the Javascript and invoke rating. To do this, our JSP first needs to refer to the JS file that declares the QualificationPluginAjax methods.

    <script type='text/javascript'
      src='<util:valueof param="publication.url"/>dwr/interface/QualificationPluginAjax.js'></script>
    

    Once the JS is included, we can write JS code as follows:

    function submitStarRating(articleId, userId, rating, element) {
    	try {
    		var meta = { userId:userId, articleId:articleId, sectionId:null, publicationId:null };
    		QualificationPluginAjax.submitStarRating( meta, rating,
          {
            callback : function(rating) {
              submitStarRatingCallBack( rating, element );
            },
            exceptionHandler : submitStarRatingException
          }
        );
    	} catch( ex ) {
    		// JavaScript exception
    	}
    	return false;
    }
    
    function submitStarRatingCallBack(rating, element) {
    	if (rating != null) {
    		alert("you rated: " + rating);
    	}
    }
    
    function submitStarRatingException(errorString, exception) {
    	alert("unable to rate article, errror-message: " + exception.javaClassName);
    }

The line of our interest is: QualificationPluginAjax.submitStarRating(meta, rating, ...). This call will:

  1. Serialize the JS variable and send the request to the server

  2. The DWR servlet on the server side will create the necessary Java objects

  3. The DWR servlet will invoke the Java method submitStarRating

  4. The DWR servlet will send response to the invoking JS code with the return value of the Java method