Server-side Listeners

There may be times when you want to add your own server-side logic to further customize a report. For example, you may want to apply an additional validation that can't be enforced on the client, or you may want to prepopulate a filter.

Let's attach a listener to the BookstoreReportTutorial. First, we will add a new class called BookstoreReportTutorialListener to the com.mybooks.web package.

These are the steps to add a class:

  1. In Package Explorer on the left side of Studio...

  2. Expand Bookstore

  3. Expand private-src

  4. Right click on com.mybooks.web

  5. Select New → Class

  6. Type "BookstoreReportTutorialListener" in the Name field.

  7. Click Finish.

For the moment, we'll keep the code simple - we'll override one of the hook methods in the base class and just log some information.

package com.mybooks.web;
import org.json.JSONArray;
import org.json.JSONObject;
import com.onenetwork.platform.integ.rest.report.BaseReportResourceListener;
import com.onenetwork.platform.tools.log.PlatformLogger;
public class BookstoreReportTutorialListener extends BaseReportResourceListener {
private static PlatformLogger LOG = PlatformLogger.get(BookstoreReportTutorialListener.class);
@Override
public JSONObject beforeParseFilters(JSONObject filters) {
LOG.info("BookstoreReportTutorialListener.beforeParseFilters: " + filters);
return super.beforeParseFilters(filters);
}
}

After adding the class, build your project with Ant and restart your server. (Remember, code changes typically require an ant build + server restart.) Finally, add the following ReportJsonListenerClass tag to your report and submit it:

<Report columnQuantity="2">
<Name>BookstoreReportTutorial</Name>
<ReportJsonListenerClass>com.mybooks.web.BookstoreReportTutorialListener</ReportJsonListenerClass>

Now if you execute the report, you should see a log along the following lines in your Platform Server:

17:54:18,371  INFO (tp-0.0.0.0-80-8) [oreReportTutorialListener] BookstoreReportTutorialListener.beforeParseFilters: {}

If you actually provide a filter value in the UI, you will see that information coming through. For example, type "Learning" in the Title filter:

18:00:28,857  INFO (tp-0.0.0.0-80-8) [oreReportTutorialListener] BookstoreReportTutorialListener.beforeParseFilters: {"Undefined$Quantity Sold_OP":"Equal","ZBKS$Book$Title":"Learning"}

Using this callback we can now add some custom logic. For example, let's assume that we want to prevent them from entering a filter with more than 10 characters. We can add such a validation:

public JSONObject beforeParseFilters(JSONObject filters) {
LOG.info("BookstoreReportTutorialListener.beforeParseFilters: " + filters);
String title = filters.optString("ZBKS$Book$Title");
if (title != null && title.length() > 10) {
JSONArray errors = new JSONArray();
errors.put(new JSONObject().put("id", "ZBKS$Book$Title").put("msg", "Cannot accept a Title filter of more than 10 characters"));
throw new RuntimeException(errors.toString());
}
return super.beforeParseFilters(filters);
}

images/download/attachments/144836774/listenerError-version-1-modificationdate-1655932420000-api-v2.jpg

See the javadocs for BaseReportResourceListener for more information on the hooks available and the usage patterns for validations, data manipulation, etc.