Exercise 3. Recording Changes

What This Exercise Is About

Using the Purchase Order model, you will make the same modifications to instance data as in Exercise 2, but this time you will record the changes and serialize the change description.

This is to demonstrate how you can record changes made to instance data, rather than simply recording the resulting instance, in the event that you're modifying a large file or want to create a service that saves time and bandwidth by sending only deltas rather than entire instances. To keep things simple, we will continue to use the Exercises project started in the previous exercise. If you did not complete the exercise, you can copy the solution into your Exercises project and start from there.

Primer PO model


What You Should Be Able To Do

At the end of the lab, you should be able to:

Required Materials

Exercise Instructions

This exercise is carried out entirely using the Eclipse Software Development Kit (SDK) version 3.2 with the Eclipse Modeling Framework (EMF) 2.2 installed into it. The exercise instructions refer to this product as either Eclipse or as "the workbench."

In your workspace, there should be a EMF_Workshop/Exercise3_Recording_Changes folder that contains a Java file named Exercise3.java. This is the class you will modify to perform the work noted above.

The solution to this exercise is available as a complete project in the EMF_Workshop/Solution3 folder.


Directions

Step A: Verify workbench configuration

  1. See Exercise 1, Step A.

Step B: Setup

  1. You will need to complete Exercises 1 and 2 before proceeding with this exercise. The solutions can also be copied from the Solution1 and Solution2 folders, respectively. Notice that you'll need to create the four projects -- com.example.po, com.example.po.edit, com.example.po.editor, and Exercises -- at least as simple projects. Then, you can use the Navigator view to copy the contents of the solutions' projects, including the .project and .classpath files.
  2. Copy Exercise3.java from the Exercise 3 source folder into Exercises/src/exercises.

Step C: Record Changes

  1. Open Exercise3.java, in Exercises/src/exercises.
  2. Work your way through the exercise by filling in the missing code for the first three of the TODO comments. As before, if you run into trouble or are pressed for time, you can copy code from the JPages folder under the Exercise 3 source folder.
  3. Run your code. There are many ways to do this.
  4.   <?xml version="1.0" encoding="ASCII"?>
      <change:ChangeDescription xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:change="http://www.eclipse.org/emf/2003/Change" 
        xmlns:po="http://www.example.com/po">
        <objectChanges key="po.xml#//@order">
          <value featureName="items">
            <listChanges index="1" referenceValues="#//@objectsToAttach.0"/>
          </value>
          <value featureName="comment" dataValue="For Scarlet's Birthday"/>
        </objectChanges>
        <objectsToAttach xsi:type="po:Item" partNum="STV999876">
          <productName>Plasma Television</productName>
          <quantity>1</quantity>
          <price>499.0</price>
          <po:comment>Fragile</po:comment>
          <shipDate>2006-03-23</shipDate>
        </objectsToAttach>
      </change:ChangeDescription>

      ---


Step D: Apply And Reverse Changes And Save Change Description To File

  1. Continue filling in the missing code for the next two TODO comments.
  2. Run again from the Run menu, the Run button, a right-click, or hit CTRL-F11 (Run last launch configuration). The output below continues from the output above.
  3.   <?xml version="1.0" encoding="ASCII"?>
      <change:ChangeDescription xmlns:change="http://www.eclipse.org/emf/2003/Change">
        <objectChanges key="po.xml#//@order">
          <value featureName="items">
            <listChanges kind="REMOVE" index="1"/>
          </value>
          <value featureName="comment" dataValue="Plasma Television (STV999876) 
            is discontinued and has been removed from the order."/>
        </objectChanges>
      </change:ChangeDescription>

      <?xml version="1.0" encoding="UTF-8"?>
      <po:order xmlns:po="http://www.example.com/po" orderDate="2006-03-20">
        <shipTo>
          <name>Scarlet O'Hara</name>
          <street>321 Backwoods Lane</street>
          <city>Louisville</city>
          <state>AL</state>
          <zip>67655</zip>
        </shipTo>
        <billTo>
          <name>Rhett Butler</name>
          <street>123 Iditarod Lane</street>
          <city>Nome</city>
          <state>AK</state>
          <zip>34582</zip>
        </billTo>
        <po:comment>For Scarlet's Birthday</po:comment>
        <items partNum="SWH123456">
          <productName>Wireless Headphones</productName>
          <quantity>2</quantity>
          <price>75.0</price>
          <po:comment>Backordered</po:comment>
          <shipDate>2006-03-23</shipDate>
        </items>
        <items partNum="STV999876">
          <productName>Plasma Television</productName>
          <quantity>1</quantity>
          <price>499.0</price>
          <po:comment>Fragile</po:comment>
          <shipDate>2006-03-23</shipDate>
        </items>
      </po:order>

      ---

  4. Refresh the data directory to see the new change.xml file.

Step E: Apply Reversed Changes

  1. Continue filling in the missing code for the last two TODO comments.
  2. Run your code. Once again, the output below continues from the two outputs above.
  3.   <?xml version="1.0" encoding="ASCII"?>
      <change:ChangeDescription xmlns:change="http://www.eclipse.org/emf/2003/Change"/>

      <?xml version="1.0" encoding="UTF-8"?>
      <po:order xmlns:po="http://www.example.com/po" orderDate="2006-03-20">
        <shipTo>
          <name>Scarlet O'Hara</name>
          <street>321 Backwoods Lane</street>
          <city>Louisville</city>
          <state>AL</state>
          <zip>67655</zip>
        </shipTo>
        <billTo>
          <name>Rhett Butler</name>
          <street>123 Iditarod Lane</street>
          <city>Nome</city>
          <state>AK</state>
          <zip>34582</zip>
        </billTo>
        <po:comment>Plasma Television (STV999876) is discontinued and has been 
          removed from the order.</po:comment>
        <items partNum="SWH123456">
          <productName>Wireless Headphones</productName>
          <quantity>2</quantity>
          <price>75.0</price>
          <po:comment>Backordered</po:comment>
          <shipDate>2006-03-23</shipDate>
        </items>
      </po:order>


Summary

You have learned how to record changes made to an instance of an EMF model, and seen how the result is a description of the reverse delta. You then applied and reversed the change description to obtain the forward delta, and serialized that result.