10
10
import com .webobjects .eocontrol .EOQualifierEvaluation ;
11
11
import com .webobjects .foundation .NSArray ;
12
12
import com .webobjects .foundation .NSDictionary ;
13
- import com .webobjects .foundation .NSLog ;
14
13
import com .webobjects .foundation .NSNotification ;
15
14
import com .webobjects .foundation .NSNotificationCenter ;
16
15
import com .webobjects .foundation .NSSelector ;
17
16
import com .webobjects .foundation .NSTimestamp ;
18
17
import com .wowodc .model .BlogCategory ;
19
18
import com .wowodc .model .BlogEntry ;
19
+ import com .wowodc .model .DelegatePKHistory ;
20
20
import com .wowodc .model .Person ;
21
21
import com .wowodc .model .SyncInfo ;
22
22
import com .wowodc .model .enums .SyncInfoStatus ;
23
+ import com .wowodc .rest .controllers .BlogEntryController ;
23
24
import com .wowodc .rest .controllers .OtherRoutesController ;
24
25
import com .wowodc .rest .controllers .RssController ;
25
26
29
30
import er .extensions .eof .ERXEnterpriseObject ;
30
31
import er .extensions .foundation .ERXArrayUtilities ;
31
32
import er .extensions .foundation .ERXRandomGUID ;
33
+ import er .rest .ERXRestContext ;
32
34
import er .rest .ERXRestNameRegistry ;
35
+ import er .rest .IERXRestDelegate ;
33
36
import er .rest .routes .ERXRoute ;
34
37
import er .rest .routes .ERXRouteRequestHandler ;
35
38
@@ -51,7 +54,8 @@ public Application() {
51
54
52
55
ERXRouteRequestHandler restRequestHandler = new ERXRouteRequestHandler ();
53
56
restRequestHandler .addDefaultRoutes (BlogCategory .ENTITY_NAME );
54
- restRequestHandler .addDefaultRoutes (BlogEntry .ENTITY_NAME );
57
+ restRequestHandler .insertRoute (new ERXRoute (BlogEntry .ENTITY_NAME , "/posts/{uniqueTitle:String}" , ERXRoute .Method .Get , BlogEntryController .class , "show" ));
58
+ restRequestHandler .insertRoute (new ERXRoute (BlogEntry .ENTITY_NAME , "/posts" , ERXRoute .Method .Get , BlogEntryController .class , "index" ));
55
59
restRequestHandler .addDefaultRoutes (Person .ENTITY_NAME );
56
60
restRequestHandler .insertRoute (new ERXRoute ("Other" , "" , ERXRoute .Method .Get , OtherRoutesController .class , "mainPage" ));
57
61
restRequestHandler .insertRoute (new ERXRoute ("Other" , "/admin" , ERXRoute .Method .Get , OtherRoutesController .class , "adminPage" ));
@@ -91,18 +95,42 @@ public void coordinateChanges(NSNotification notification) {
91
95
EOEditingContext ec = ERXEC .newEditingContext (new EOObjectStoreCoordinator ());
92
96
ec .lock ();
93
97
94
- log .debug ("Change Notification " + userInfo );
95
98
try {
96
- NSLog .out .appendln ("deleted" + (NSArray )userInfo .objectForKey ("deleted" ));
97
- NSLog .out .appendln ("inserted: " + (NSArray )userInfo .objectForKey ("inserted" ));
98
- NSLog .out .appendln ("updated: " + (NSArray )userInfo .objectForKey ("updated" ));
99
-
100
- NSArray result = ERXArrayUtilities .filteredArrayWithQualifierEvaluation ((NSArray )userInfo .objectForKey ("inserted" ), new EOSyncEntityFilter () );
101
- for ( Object id : result ) {
102
- EOKeyGlobalID gid = (EOKeyGlobalID )id ;
103
- ERXEnterpriseObject eo = (ERXEnterpriseObject )ec .faultForGlobalID ( gid , ec );
104
-
105
- SyncInfo .createSyncInfo (ec , ERXRandomGUID .newGid (), new NSTimestamp (), SyncInfoStatus .INSERTED , eo .entityName () + ":" + eo .primaryKey ());
99
+ NSArray <Object > insertedObjects = ERXArrayUtilities .filteredArrayWithQualifierEvaluation ((NSArray <Object >)userInfo .objectForKey ("inserted" ), new EOSyncEntityFilter () );
100
+ for ( Object id : insertedObjects ) {
101
+ ERXEnterpriseObject eo = eo (id , ec );
102
+ SyncInfo syncDetail = SyncInfo .createSyncInfo (ec , ERXRandomGUID .newGid (), new NSTimestamp (), SyncInfoStatus .INSERTED , eo .entityName () + ":" + eo .primaryKey ());
103
+ syncDetail .setDelegatedPrimaryKeyValue ((String )entityId (eo ));
104
+ }
105
+
106
+ NSArray <Object > deletedObjects = ERXArrayUtilities .filteredArrayWithQualifierEvaluation ((NSArray <Object >)userInfo .objectForKey ("deleted" ), new EOSyncEntityFilter () );
107
+ for ( Object id : deletedObjects ) {
108
+ ERXEnterpriseObject eo = eo (id , ec );
109
+ if (eo != null ) {
110
+ SyncInfo syncDetail = SyncInfo .fetchSyncInfo (ec , SyncInfo .TOKEN .eq (eo .entityName () + ":" + eo .primaryKey ()));
111
+ if (syncDetail != null ) {
112
+ syncDetail .setEtag (ERXRandomGUID .newGid ());
113
+ syncDetail .setLastModified (new NSTimestamp ());
114
+ syncDetail .setStatus (SyncInfoStatus .DELETED );
115
+ }
116
+ }
117
+ }
118
+
119
+ NSArray <Object > updatedObjects = ERXArrayUtilities .filteredArrayWithQualifierEvaluation ((NSArray <Object >)userInfo .objectForKey ("updated" ), new EOSyncEntityFilter () );
120
+ for ( Object id : updatedObjects ) {
121
+ ERXEnterpriseObject eo = eo (id , ec );
122
+ if (eo != null ) {
123
+ SyncInfo syncDetail = SyncInfo .fetchSyncInfo (ec , SyncInfo .TOKEN .eq (eo .entityName () + ":" + eo .primaryKey ()));
124
+ if (syncDetail != null ) {
125
+ if (!(syncDetail .delegatedPrimaryKeyValue ().equals ((String )entityId (eo )))) {
126
+ DelegatePKHistory .createDelegatePKHistory (ec , syncDetail .delegatedPrimaryKeyValue (), syncDetail );
127
+ }
128
+ syncDetail .setEtag (ERXRandomGUID .newGid ());
129
+ syncDetail .setLastModified (new NSTimestamp ());
130
+ syncDetail .setStatus (SyncInfoStatus .UPDATED );
131
+ syncDetail .setDelegatedPrimaryKeyValue ((String )entityId (eo ));
132
+ }
133
+ }
106
134
}
107
135
108
136
ec .saveChanges ();
@@ -112,6 +140,16 @@ public void coordinateChanges(NSNotification notification) {
112
140
}
113
141
}
114
142
143
+ private ERXEnterpriseObject eo (Object id , EOEditingContext ec ) {
144
+ EOKeyGlobalID gid = (EOKeyGlobalID )id ;
145
+ ERXEnterpriseObject eo = (ERXEnterpriseObject )ec .faultForGlobalID ( gid , ec );
146
+ return eo ;
147
+ }
148
+
149
+ private Object entityId (ERXEnterpriseObject eo ) {
150
+ return IERXRestDelegate .Factory .delegateForEntityNamed (eo .entityName ()).primaryKeyForObject (eo , new ERXRestContext (eo .editingContext ()));
151
+ }
152
+
115
153
public class EOSyncEntityFilter implements EOQualifierEvaluation
116
154
{
117
155
public boolean evaluateWithObject (Object object )
0 commit comments