17
17
18
18
package io .skygear .skygear ;
19
19
20
+ import android .util .Log ;
21
+
20
22
import org .json .JSONArray ;
23
+ import org .json .JSONException ;
24
+ import org .json .JSONObject ;
21
25
22
26
import java .security .InvalidParameterException ;
23
27
import java .util .ArrayList ;
24
- import java .util .Arrays ;
25
28
import java .util .HashMap ;
26
- import java .util .HashSet ;
27
29
import java .util .List ;
28
- import java .util .Set ;
30
+
31
+ import static io .skygear .skygear .RecordSerializer .RecordIdentifier ;
29
32
30
33
/**
31
34
* The Skygear Record Delete Request.
32
35
*/
33
36
public class RecordDeleteRequest extends Request {
37
+ private static final String TAG = "Skygear SDK" ;
38
+
34
39
private String databaseId ;
35
- private List <Record > records ;
40
+ private List <RecordIdentifier > recordIdentifiers ;
36
41
37
42
/**
38
43
* Instantiates a new record delete request with default properties.
39
44
*/
40
45
RecordDeleteRequest () {
41
46
super ("record:delete" );
42
47
this .data = new HashMap <>();
43
- this .records = new ArrayList <>();
48
+ this .recordIdentifiers = new ArrayList <>();
44
49
}
45
50
46
51
/**
@@ -52,30 +57,44 @@ public class RecordDeleteRequest extends Request {
52
57
public RecordDeleteRequest (Record [] records , Database database ) {
53
58
this ();
54
59
this .databaseId = database .getName ();
55
- this .records = Arrays .asList (records );
60
+
61
+ for (Record perRecord : records ) {
62
+ this .recordIdentifiers .add (
63
+ new RecordIdentifier (perRecord .type , perRecord .id )
64
+ );
65
+ }
66
+
56
67
this .updateData ();
57
68
}
58
69
59
70
private void updateData () {
60
- String recordType = null ;
61
- JSONArray recordIDs = new JSONArray ();
62
71
JSONArray deprecatedIDs = new JSONArray ();
63
- for (Record perRecord : this .records ) {
64
- if (recordType == null ) {
65
- recordType = perRecord .getType ();
66
- }
72
+ JSONArray recordIdentifiers = new JSONArray ();
73
+
74
+ try {
75
+ for (RecordIdentifier perRecordIdentifier : this .recordIdentifiers ) {
76
+ String perRecordDeprecatedID = String .format (
77
+ "%s/%s" ,
78
+ perRecordIdentifier .type ,
79
+ perRecordIdentifier .id
80
+ );
81
+
82
+ deprecatedIDs .put (perRecordDeprecatedID );
67
83
68
- recordIDs .put (perRecord .getId ());
69
- deprecatedIDs .put (String .format ("%s/%s" , perRecord .getType (), perRecord .getId ()));
84
+ JSONObject perRecordIdentifierData = new JSONObject ();
85
+ perRecordIdentifierData .put ("_id" , perRecordDeprecatedID );
86
+ perRecordIdentifierData .put ("_recordType" , perRecordIdentifier .type );
87
+ perRecordIdentifierData .put ("_recordID" , perRecordIdentifier .id );
88
+
89
+ recordIdentifiers .put (perRecordIdentifierData );
90
+ }
91
+ } catch (JSONException e ) {
92
+ Log .w (TAG , "Fail to serialize record identifiers" );
70
93
}
71
94
72
95
this .data .put ("ids" , deprecatedIDs );
73
- this .data .put ("recordIDs " , recordIDs );
96
+ this .data .put ("records " , recordIdentifiers );
74
97
this .data .put ("database_id" , this .databaseId );
75
-
76
- if (recordType != null ) {
77
- this .data .put ("recordType" , recordType );
78
- }
79
98
}
80
99
81
100
@ Override
@@ -86,14 +105,5 @@ protected void validate() throws Exception {
86
105
if (ids .length () == 0 ) {
87
106
throw new InvalidParameterException ("No records to be processed" );
88
107
}
89
-
90
- Set <String > typeSet = new HashSet <>();
91
- for (Record perRecord : this .records ) {
92
- typeSet .add (perRecord .getType ());
93
- }
94
-
95
- if (typeSet .size () > 1 ) {
96
- throw new InvalidParameterException ("Only records in the same type are allowed" );
97
- }
98
108
}
99
109
}
0 commit comments