How to navigate and manipulate the data in a view at runtime.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on using Java-based views in the Programmer's Guide |
The example lists employees in a selected department. It shows how to query the database, navigate the rows, get and set cell values, and update the database. A choice control filters which department is shown in the view. A slider control lets you change all the displayed salaries by a percentage rate. You can permanently save the changes with a Save button.
This code illustrates how to set up a default department in the department choice control.
protected void formLoaded() { // Select the first department cmbxDepartments.setSelectedIndex(0); }
setSelectedIndex()
assigning zero as the index.
This code illustrates how to translate a selection made by you from the choice control into a department id by which the employee data is filtered.
private void handle_cmbxDepartments_valueChanged(AgoPropertyChangeEvent evt) { try { // Get the view to refresh the list of employees // for the department selected vwViewManipulation.query("employees.deptid =" + cmbxDepartments.getValue()); // Set the slider back to 0 sldrRate.setValue(0); // Display 0% lblRate.setText("0%"); } catch (AgoApiException e) { agDialog.displayError(e); return; } }
setValue()
and resets the slider's label text field to display zero percent.
The following code shows how the slider control is handled when its value is changed.
private void handle_sldrRate_valueChanged(AgoPropertyChangeEvent evt) { // User has changed the slider value, so get the new value Integer intRate = new Integer(sldrRate.getValue()); // Display the new silder value lblRate.setText(intRate.toString() + "%"); // Re-calculate the salaries in the view changeRate(); }
getValue()
on the slider and stores it in a new integer.
toString()
in order to display the new rate in the slider's label text field.
changeRate()
to display the proposed change to each employee salary shown in the view. The changeRate()
method is described below.
The following code illustrates a custom method which changes the displayed salaries for the employees of the selected department. This method is called whenever you moves the rate percentage slider. The changes are not permanent until you clicks the Save button.
public void changeRate() { // We may already have calculated new salaries based on // some other percentage, so refresh the view so that we have // the current salary for each employee try { vwViewManipulation.query("employees.deptid = " + cmbxDepartments.getValue()); } catch (AgoApiException e) { agDialog.displayError(" **** " ,e); return; } // Get the slider value to be used as a percentage increase // in salary double dbRate = sldrRate.getValue() * 1.0; // Now that the view has been refreshed, we need to get // a cursor pointing to the first row in the view AgiRowCursor employees = vwViewManipulation.getSelectedRowCursor(); // Got a row cursor? We won't if the department has // no employees, so return if (employees == null) return; try { // Loop through all the rows and calculate the new // salary for each employee if (employees.gotoFirst()) do { // Get the employee's current salary BigDecimal bdSalary = (BigDecimal) employees.getProperty("salary"); // Got a salary? if (bdSalary != null) { // Yes, so apply the increase double dbNewSalary = ((dbRate / 100) + 1.00) * bdSalary.doubleValue(); // New salary as a BigDecimal bdSalary = new BigDecimal(dbNewSalary); // Update the view with the employee's new salary employees.setProperty("salary", bdSalary); } } while (employees.gotoNext()); } catch (AgoApiException e) { agDialog.displayError(e); return; } }
getValue()
.
gotoFirst()
.
gotoNext()
no longer returns an entry.
getProperty("salary")
to get the old salary, then changes it to a BigDecimal.
doubleValue()
and saves it as a new BigDecimal.
setProperty("salary", salary)
in you's temporary copy of the database.
updateRows()
is called when the user clicks the Save button.
The following code illustrates changes are actually updated in the database.
private void handle_jbtnSave_actionPerformed(ActionEvent evt) { // Save changes to the database try { vwViewManipulation.updateRows(); } catch (AgoApiException e) { agDialog.displayError(e); return; } }
updateRows()
to forward the changes to the database server.