Adding a Preferences Form

Viewpoint
Teradata Viewpoint is Teradata's strategic and innovative SOV (single operational view) for Teradata DB, Aster, and HDP Hadoop systems management and monitoring that enables Teradata's Unified Data Architecture (UDA).
Teradata Employee

Adding a Preferences Form

A preferences form allows the user to customize some aspect of a portlet instance. A portlet instance is a portlet that has been added to a page by a user. If a user adds the same portlet to a page twice, there will be two instances of the same portlet on the page. Each instance remembers its own preferences.

In this example, we are going to allow the user to override the default salutation with their preferred name.

The following diagram illustrates the rendering of the preferences form:

And this diagram illustrates the processing of the form when the user clicks the OK button:

The first step is to add a name field to the HelloPreferences class that was generated for us. This class is used to store any portlet preferences that we wish to save between user sessions. Fields that should be saved should be marked with the @Preference annotation.

Please add the text shown within the comment blocks below to your HelloPreferences class:

Hello\src\java\com\teradata\portlets\hello\model\HelloPreferences.java

public class HelloPreferences extends PreferencesModel
{
/**
* serial UID
*/
private static final long serialVersionUID = 6960306072306995665L;

//***Add following code section***/

/**
* name
*/
@Preference private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

//***End add code section***/
}

In the preferences view, we need to prompt the user for their preferred name. We will add an input box to the preferences form which allows the user to specify a new name. In the preferences.jsp file, replace the "[preferences]" flag with the text in the comment block:

Hello\web\WEB-INF\portlet-jsp\preferences.jsp

<form:form id="${context}_form" commandName="preferences" method="post" action="${formAction}">
<input type="hidden" name="modal" value="${context}"/>
<input type="hidden" name="role" value="${role}" />
<input type="hidden" name="user" value="${user}" />

<!-- form content goes here -->

<!-- Add following code section -->

<br/>
Your preferred name: <form:input path="name"/>

<!-- End add code section -->

<br/>
<form:errors path="*" cssStyle="color:red"/>
<br/>
...

Next, we need to modify the preferences controller to use the user's username as the default name:

Hello\src\java\com\teradata\portlets\hello\controllers\HelloPreferencesController.java

   /**
* This method builds the object that contains the data that is rendered in the
* form. It loads the portlet preferences from the database and populates the
* preferences model.
*/
public Object initCommandObject(ControllerContext ctx)
{
HelloPreferences preferences = new HelloPreferences();
preferences = (HelloPreferences)ctx.loadPreferences("Hello", preferences);

//*** Add following code section ***/

if ((preferences != null) && (preferences.getName() == null))
{
preferences.setName(ctx.getRemoteUser());
}

//*** End add code section ***/

return preferences;
}

You have now completed the preferences form. However, we still need to modify the view controller to fetch the preferences and use the preferred name if it is available. Make the following changes to HelloViewController.java:

Hello\src\java\com\teradata\portlets\hello\controllers\HelloViewController.java

import com.teradata.portlets.hello.service.HelloManager;
import com.teradata.portlets.util.TdPortletUtils;
import com.teradata.portlets.hello.model.HelloModel;

//*** Add following code section ***/
import com.teradata.portlets.hello.model.HelloPreferences;
//*** End add code section ***/

/**
* This method handles requests to render the summary page. It provides any data needed
* by summary.jsp as view objects.
*/
public void summary(ControllerContext ctx)
{
// set 'summary.jsp' as the view for this page.
ctx.setViewName("summary");

// Retrieve the user name, store it in a HelloModel object,
// and add the HelloModel as a view object
HelloModel model = new HelloModel();

//*** Add following code section ***/

HelloPreferences preferences = new HelloPreferences();
preferences = (HelloPreferences) ctx.loadPreferences("Hello", preferences);
if ((preferences != null) && (preferences.getName() != null))
{
model.setSalutation(preferences.getName());
}
else
{
model.setSalutation(ctx.getRemoteUser());
}

//*** End add code section ***/

ctx.addViewObject("helloData", model);
}

You are now done with the preferences implementation. After you redeploy your portlet, click on the preferences icon to view the preferences page:

After modifying your preferences, your summary page should display your preferred name:

Tags (2)