Flex Data Services for .NET with WebORB
Part 3 - Handling data updates on the server-side
Search:
home / articles / flex integration  

OVERVIEW
This is part 3 of the series, parts 1 and 2 are available here and here. This part reviews the backend code handling data updates in the Contact Manager example. You can invoke the data update functionality by adding new contacts and modifying or deleting existing ones.

If you have any comments about the article, or have any questions about the product and the integration of Flex Data Management Services in WebORB, post them to the our interest group at: http://groups.yahoo.com/group/flashorb/

CODE REVIEW - UPDATING DATA

As with reading data from a database (reviewed in Part2), data assembler (Examples.Flex.contact.ContactAssembler in this example) is responsible for handling of create, update and delete requests. WebORB uses a special type to encapsulate information about any of the above listed requests. To handle change requests data assembler class must declare a public method accepting the Weborb.Data.ChangeObject class as an argument. In the code below WebORB will invoke the syncContacts method whenever a client application adds, modifies or deletes contact records in the database:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

using System;
using
System.Collections;
using
System.ComponentModel;
using
Weborb.Data;
using
Weborb.Service;

namespace
Examples.Flex.contact
{
 
public class ContactAssembler
  {

  .......[ section omitted ]......

  public
void syncContacts( ChangeObject
change )
  {
   
if( change.IsUpdate )
      doUpdate( change );
   
else if( change.IsCreate )
      doCreate( change );
   
else if( change.IsDelete )
      doDelete( change );
  }

 
private void doUpdate( ChangeObject co )
  {
   
ContactDAO dao = new ContactDAO();
    dao.update( (
Contact) co.NewVersion, (Contact) co.OldVersion );
  }

 
private void doCreate( ChangeObject co )
  {
   
ContactDAO dao = new ContactDAO();
   
Contact contact = dao.create( (Contact) co.NewVersion );
    co.NewVersion = contact;
  }

 
private void doDelete( ChangeObject co )
  {
   
ContactDAO dao = new ContactDAO();
    dao.delete( (
Contact) co.OldVersion );
  }
  }
}

The 'change' object passed into the syncContacts method contains information about the type of change requested by the client application. There are three supported types of changes: data update, data creation and deletion. Depending on the type of request, the ChangeObject object contains the corresponding data (lines 26, 32 and 39).

The code above delegates requests to an instance of the ContactDAO class shown below. The class executes a corresponding SQL query for each change operation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

using System;
using
System.Collections;
using
System.Text;
using
System.Data;
using
System.Data.OleDb;
using
Weborb.Data;

namespace
Examples.Flex.contact
{
 
public class ContactDAO
  {
   
private static string connectionString = "cnx string goes here";

    public Contact create( Contact contact )
    {
      validate( contact );
     
OleDbConnection connection = new OleDbConnection( connectionString );
     
try
      {
       
String query = "INSERT INTO contact (firstName, lastName, address, city, " +  
                       "zip, state, phone) VALUES (?, ?, ?, ?, ?, ?, ?)"
;
       
OleDbCommand command = new OleDbCommand( query, connection );
        command.Parameters.AddWithValue(
"firstName", contact.firstName );
        command.Parameters.AddWithValue(
"lastName", contact.lastName );
        command.Parameters.AddWithValue(
"address", contact.address == null ?
                                        
DBNull.Value : (object) contact.address );
        command.Parameters.AddWithValue(
"city", contact.city == null ?
                                        
DBNull.Value : (object) contact.city );
        command.Parameters.AddWithValue(
"zip", contact.zip == null ?
                                       
 DBNull.Value : (object) contact.zip );
        command.Parameters.AddWithValue(
"state", contact.state == null ?
                                        
DBNull.Value : (object) contact.state );
        command.Parameters.AddWithValue(
"phone", contact.phone == null ?
                                      
DBNull.Value : (object) contact.phone );
        connection.Open();
        command.ExecuteNonQuery();
        command =
new OleDbCommand( "select @@identity", connection );
        contact.contactId = (
int) command.ExecuteScalar();
      }
     
finally
      {
        connection.Close();
      }
     
return contact;
    }

   
public void update( Contact newVersion, Contact previousVersion )
    {
      validate( newVersion );
   
  OleDbConnection connection = new OleDbConnection( connectionString );
     
try
      {
       
String query = "UPDATE contact SET firstName=?, lastName=?, address=?, " +
                       "city=?, zip=?, state=?, phone=? WHERE contactId=?"
;
       
OleDbCommand command = new OleDbCommand( query, connection );
        command.Parameters.AddWithValue(
"firstName", newVersion.firstName );
        command.Parameters.AddWithValue(
"lastName", newVersion.lastName );
        command.Parameters.AddWithValue(
"address", newVersion.address == null ?
                                        
DBNull.Value : (object) newVersion.address );
        command.Parameters.AddWithValue(
"city", newVersion.city == null ?
                                        
DBNull.Value : (object) newVersion.city );
        command.Parameters.AddWithValue(
"zip", newVersion.zip == null ?
                                        
DBNull.Value : (object) newVersion.zip );
        command.Parameters.AddWithValue(
"state", newVersion.state == null ?
                                        
DBNull.Value : (object) newVersion.state );
        command.Parameters.AddWithValue(
"phone", newVersion.phone == null ?
                                        
DBNull.Value : (object) newVersion.phone );
        command.Parameters.AddWithValue(
"contactId", newVersion.contactId );
        connection.Open();
      }
     
finally
      {
        connection.Close();
      }
    }

   
public void delete( Contact contact )
    {
   
  OleDbConnection connection = new OleDbConnection( connectionString );
     
try
      {
       
String query = "DELETE FROM contact WHERE contactId=? AND firstName=? AND " +  
                       "lastName=? AND address=? AND city=? AND zip=? AND state=? " +
                       "AND phone=?"
;
       
OleDbCommand command = new OleDbCommand( query, connection );
        command.Parameters.AddWithValue(
"contactId", contact.contactId );
        command.Parameters.AddWithValue(
"firstName", contact.firstName );
        command.Parameters.AddWithValue(
"lastName", contact.lastName );
        command.Parameters.AddWithValue(
"address", contact.address == null ?
                                        
DBNull.Value : (object) contact.address );
        command.Parameters.AddWithValue(
"city", contact.city == null ?
                                        
DBNull.Value : (object) contact.city );
        command.Parameters.AddWithValue(
"zip", contact.zip == null ?
                                        
DBNull.Value : (object) contact.zip );
        command.Parameters.AddWithValue(
"state", contact.state == null ?
                                        
DBNull.Value : (object) contact.state );
        command.Parameters.AddWithValue(
"phone", contact.phone == null ?
                                        
DBNull.Value : (object) contact.phone );
        connection.Open();
      }
     
finally
      {
        connection.Close();
      }
    }

   
private void validate( Contact contact )
    {
   
StringBuilder errors = new StringBuilder();
   
if( contact.firstName == null || contact.firstName.Equals( "" ) )
       errors.Append(
"first name required," );
   
if( contact.lastName == null || contact.lastName.Equals( "" ) )
       errors.Append(
"last name required" );
   
if( errors.Length > 0 )
      
throw new Exception( errors.ToString() );
    }
  }
}

CONFIGURATION

The syncContacts method from the ContactAssembler class must be registered in flex-data-service.xml located in the WEB-INF\flex folder as shown below (lines 27-30):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

<destination id="contact">
  <
adapter ref="dotnet-dao" />

  <
properties>
    <
source>Examples.Flex.contact.ContactAssembler</source>
    <
scope>application</scope>

    <
metadata>
      <
identity property="contactId"/>
    </
metadata>

    <
network>
      <
session-timeout>20</session-timeout>
      <
paging enabled="false" pageSize="10" />
    </
network>

    <
server>
      <
fill-method>
        <
name>loadContacts</name>
      </
fill-method>

      <
fill-method>
        <
name>loadContacts</name>
        <
params>System.String</params>
      </
fill-method>

      <
sync-method>
        <
name>syncContacts</name>
      </
sync-method>
    </
server>

  </
properties>
</
destination>
 
Copyright © 2003-2006 Midnight Coders, LLC.  Privacy Policy | Contact Webmaster
Home | Products | Customers | Download | Licensing | Forum | Developers | About