1
1
package org .codebrewery ;
2
2
3
- import com .ning .http .client .AsyncHttpClient ;
4
- import com .ning .http .client .Response ;
5
-
6
3
import java .io .IOException ;
7
- import java .util .ArrayList ;
8
4
import java .util .List ;
9
5
import java .util .concurrent .ExecutionException ;
10
6
7
+ import com .ning .http .client .AsyncHttpClient ;
8
+ import com .ning .http .client .Response ;
9
+
11
10
/**
12
11
* Created by ejeserl on 9/19/15.
13
12
*
14
- *19520622-1482
13
+ * 19520622-1482
15
14
*/
16
15
public class DefaultApiImplementation implements ApiInterface {
17
16
@@ -28,25 +27,26 @@ public class DefaultApiImplementation implements ApiInterface {
28
27
29
28
String generateCompleteBaseUrl () {
30
29
31
- return "http://" + config .getHost () + ":" + config .getPort () + "/" + config .getApiLocation () + "/" ;
30
+ return "http://" + config .getHost () + ":" + config .getPort () + "/" + config .getApiLocation () + "/" ;
32
31
}
33
32
34
33
String generateInstanceUrl (Model model ) {
35
34
return generateCollectionUrl (model .getClass ()) + "/" + model .getIdentifierValue ();
36
35
}
36
+
37
37
@ Override
38
- public Model save (Model model ) throws JavaOrmenException {
38
+ public < T extends Model > T save (T model ) throws JavaOrmenException {
39
39
try {
40
40
41
- String url = generateCollectionUrl (model .getClass ());
41
+ String url = generateCollectionUrl (model .getClass ());
42
42
// execute the query
43
43
Response response = execute (new AsyncHttpClient ().prepareGet (url ));
44
44
// convert the response to an Model
45
- return JSONConverter .unMarshall (response .getResponseBody (), model .getClass ());
45
+ return ( T ) JSONConverter .unMarshall (response .getResponseBody (), model .getClass ());
46
46
47
- } catch (InterruptedException | ExecutionException | IOException e ) {
47
+ } catch (InterruptedException | ExecutionException | IOException e ) {
48
48
// wrap the exception in a javaOrmenException
49
- throw new JavaOrmenException ("failed to save model with identifier" + model .getIdentifierValue (),e );
49
+ throw new JavaOrmenException ("failed to save model with identifier" + model .getIdentifierValue (), e );
50
50
}
51
51
}
52
52
@@ -68,71 +68,71 @@ private String generateCollectionUrl(Class<? extends Model> aClass) {
68
68
}
69
69
70
70
@ Override
71
- public Model update (Model model ) throws JavaOrmenException {
71
+ public < T extends Model > T update (T model ) throws JavaOrmenException {
72
72
try {
73
73
74
74
String url = generateInstanceUrl (model );
75
75
// execute the query
76
76
Response response = execute (new AsyncHttpClient ().preparePut (url ).setBody (JSONConverter .marshall (model )));
77
77
// convert the response to an Model
78
- return JSONConverter .unMarshall (response .getResponseBody (), model .getClass ());
78
+ return ( T ) JSONConverter .unMarshall (response .getResponseBody (), model .getClass ());
79
79
80
- } catch (InterruptedException | ExecutionException | IOException e ) {
80
+ } catch (InterruptedException | ExecutionException | IOException e ) {
81
81
// wrap the exception in a javaOrmenException
82
- throw new JavaOrmenException ("failed to update model with identifier " + model .getIdentifierValue (),e );
82
+ throw new JavaOrmenException ("failed to update model with identifier " + model .getIdentifierValue (), e );
83
83
}
84
84
}
85
85
86
86
@ Override
87
- public Model insert (Model model ) throws JavaOrmenException {
87
+ public < T extends Model > T insert (T model ) throws JavaOrmenException {
88
88
89
- try {
89
+ try {
90
90
91
- String url = generateInstanceUrl (model );
92
- // execute the query
93
- Response response = execute (new AsyncHttpClient ().preparePost (url ).setBody (JSONConverter .marshall (model )));
94
- // convert the response to an Model
95
- return JSONConverter .unMarshall (response .getResponseBody (), model .getClass ());
91
+ String url = generateInstanceUrl (model );
92
+ // execute the query
93
+ Response response = execute (new AsyncHttpClient ().preparePost (url ).setBody (JSONConverter .marshall (model )));
94
+ // convert the response to an Model
95
+ return ( T ) JSONConverter .unMarshall (response .getResponseBody (), model .getClass ());
96
96
97
- } catch (InterruptedException | ExecutionException | IOException e ) {
98
- // wrap the exception in a javaOrmenException
99
- throw new JavaOrmenException ("failed to insert model with identifier " + model .getIdentifierValue (),e );
100
- }
97
+ } catch (InterruptedException | ExecutionException | IOException e ) {
98
+ // wrap the exception in a javaOrmenException
99
+ throw new JavaOrmenException ("failed to insert model with identifier " + model .getIdentifierValue (), e );
100
+ }
101
101
}
102
102
103
103
@ Override
104
- public void delete (Model model ) throws JavaOrmenException {
104
+ public < T extends Model > void delete (T model ) throws JavaOrmenException {
105
105
try {
106
106
107
107
String url = generateInstanceUrl (model );
108
108
// execute the query
109
109
Response response = execute (new AsyncHttpClient ().prepareDelete (url ));
110
110
// convert the response to an Model
111
111
112
- } catch (InterruptedException | ExecutionException e ) {
112
+ } catch (InterruptedException | ExecutionException e ) {
113
113
// wrap the exception in a javaOrmenException
114
- throw new JavaOrmenException ("failed to delete model with identifier " + model .getIdentifierValue (),e );
114
+ throw new JavaOrmenException ("failed to delete model with identifier " + model .getIdentifierValue (), e );
115
115
}
116
116
}
117
117
118
118
@ Override
119
- public Model fetch (Model model ) throws JavaOrmenException {
119
+ public < T extends Model > T fetch (T model ) throws JavaOrmenException {
120
120
try {
121
121
122
122
String url = generateInstanceUrl (model );
123
123
// execute the query
124
124
Response response = execute (new AsyncHttpClient ().prepareGet (url ));
125
125
// convert the response to an Model
126
- return JSONConverter .unMarshall (response .getResponseBody (), model .getClass ());
126
+ return ( T ) JSONConverter .unMarshall (response .getResponseBody (), model .getClass ());
127
127
128
- } catch (InterruptedException | ExecutionException | IOException e ) {
128
+ } catch (InterruptedException | ExecutionException | IOException e ) {
129
129
// wrap the exception in a javaOrmenException
130
- throw new JavaOrmenException ("failed to fetch model with identifier " + model .getIdentifierValue (),e );
130
+ throw new JavaOrmenException ("failed to fetch model with identifier " + model .getIdentifierValue (), e );
131
131
}
132
132
}
133
133
134
134
@ Override
135
- public List < Model > getAll (Class aClass ) throws JavaOrmenException {
135
+ public < M extends Model > List < M > getAll (Class < M > aClass ) throws JavaOrmenException {
136
136
try {
137
137
138
138
String url = generateCollectionUrl (aClass );
@@ -141,11 +141,10 @@ public List<Model> getAll(Class aClass) throws JavaOrmenException {
141
141
// convert the response to an Model
142
142
return JSONConverter .unMarshallList (response .getResponseBody (), aClass );
143
143
144
- } catch (InterruptedException | ExecutionException | IOException e ) {
144
+ } catch (InterruptedException | ExecutionException | IOException e ) {
145
145
// wrap the exception in a javaOrmenException
146
- throw new JavaOrmenException ("failed to fech list of models of class " + aClass .getSimpleName (),e );
146
+ throw new JavaOrmenException ("failed to fech list of models of class " + aClass .getSimpleName (), e );
147
147
}
148
148
}
149
149
150
-
151
150
}
0 commit comments