Xoetrope
View

Introduction tutorial - Saving data from the model - Output the model
SUMMARY  Being able to manipulate the data in the model is a vital part of any application and with XUI there are numerous ways of doing this. This step introduces some basic model manipulation techniques but this should be seen as a very limited way of saving model data

FURTHER READING  Model manipulation is covered in many more steps in the Advanced Tutorial as well as the Carousel tutorial.

When the user has entered all of the data into the application it might be of use to save the data into a file which can be either opened for editing again or sent to a mortgage provider as an application for credit. Start by creating the XPage file finish.xml

Listing 1 - finish.xml
<XPage class="net.xoetrope.mortgage.Finish"> 
  <Components> 
    <Label x="136" y="66" w="371" h="97" style="Heading" 
	content="Your application is being processed." opaque="true" /> 
  </Components> 
</XPage>

Again the finance.xml file needs to be modified to specify the next file in the sequence.

Listing 2 - finance.xml
<XPage class="net.xoetrope.mortgage.Finance" next="finish">
    ...

Now the class net.xoetrope.mortgage.Finish as referenced by the class attribute of the XML needs to be created.

Listing 3 - Finish.java
package net.xoetrope.mortgage;

import net.xoetrope.xui.XPage;

import java.io.*;
import net.xoetrope.data.*;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.data.*;

public class Finish extends XPage
{
  public void pageActivated() {
    try {
      String path = System.getProperty( "user.dir" ) + File.separator + "save.xml";
      FileOutputStream fos = new FileOutputStream( path );
      OutputStreamWriter osw = new OutputStreamWriter( fos, "UTF8" );
      BufferedWriter bw = new BufferedWriter( osw );
      XDataSource.outputModel( bw, ( ( XModel ) rootModel.get( "mortapp" ) ) );
      bw.flush();
      bw.close();
    }
    catch (Exception e) {
      System.out.println( "error" );
    }

  }
}

Before going through the code in detail, the pageActivated method needs a bit of explanation. This is one of the methods which is called automatically by the XUI framework during the lifecycle of an XPage. The pageActivated event is called whenever the page is activated. Because XPage classes are cached by the XUI framework the pageCreated is called only once upon creation of the XPage whereas the pageActivated event is called each time the XPage is activated.

Now for the code within the pageActivated event. The first three lines setup a BufferedWriter object. It's the next line that's of most interest to us. The rootModel variable of the pageActivated is a reference to the root node of the XUI model of the current project. This is the main storage structure for the XUI framework and it is important to know how it works if you intend to develop real world applications with XUI.

The rootModel.get( "mortapp" ) call retrieves a reference to the model node which is storing the application data. Separating the data in this way makes it convenient when the entered data needs to be saved.

The code XDataSource.outputModel() simply saves the XModel to the BufferedWriter. Here is an example of the saved data...

Listing 4 - The resulting file
<data id="mortapp"> 
  <data id="customer"> 
    <data value="Joe" id="firstname" /> 
    <data value="Bloggs" id="surname" /> 
    <data value="21/01/1972" id="dob" /> 
  </data> 
  <data id="finance"> 
    <data value="195000" id="propvalue" /> 
    <data value="140000" id="mortamt" /> 
  </data> 
</data>

In order to recap so far, we have created an application with three styled pages within a frameset with some business logic which saves data to a file and all this with less than 70 lines of Java code. Now, that does not mean that we think Java should be avoided. On the contrary, Java is a very versatile, powerful and full featured language, but screen creation really is a trivial task and that is why their definition resides very comfortably in XML. Not only that, but the screen layout is easier to visualise when looking at and XML file than it's equivalent in Java code.

After compiling Finish.java, execute the run.bat file and the application will appear as in the screenshot.

Log in or register to download the source code for this step.