|
a |
|
b/personalization-manager/tools/apigenerator.pl |
|
|
1 |
#!/usr/bin/perl
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
|
|
|
5 |
my $SRC='/Users/robert.lill/workspace/alfred/personalization-manager/src/main/java/eu/alfred/personalization_manager/webservices/PersonalizationManagerService.java';
|
|
|
6 |
my $TRG='/Users/robert.lill/workspace/alfred/mobileassistantfoundation/personalizationmanagerapi/src/main/java/alfred/eu/personalizationmanagerapi';
|
|
|
7 |
my $PAS='/Users/robert.lill/workspace/alfred/mobileassistantfoundation/PersonalAssistantService/src/main/java/eu/alfred/personalassistant/service/personalization';
|
|
|
8 |
my $SHR='/Users/robert.lill/workspace/alfred/personalassistantcommons/PersonalAssistantShared/src/main/java/eu/alfred/api/personalization';
|
|
|
9 |
|
|
|
10 |
my @calls;
|
|
|
11 |
|
|
|
12 |
my ($path,$method,$description,$response,$name,$args);
|
|
|
13 |
|
|
|
14 |
my $locmap = {'RequestBody' => 'Field', 'PathVariable' => 'Path'};
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
sub camelCaseToCapitals($)
|
|
|
18 |
{
|
|
|
19 |
my $s = shift;
|
|
|
20 |
my $res = '';
|
|
|
21 |
for my $p (split /([A-Z])/, $s)
|
|
|
22 |
{
|
|
|
23 |
if ($p =~ /[A-Z]/)
|
|
|
24 |
{
|
|
|
25 |
$res .= "_$p";
|
|
|
26 |
}
|
|
|
27 |
else
|
|
|
28 |
{
|
|
|
29 |
$res .= uc($p);
|
|
|
30 |
}
|
|
|
31 |
}
|
|
|
32 |
return $res;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
open IN, "< $SRC";
|
|
|
36 |
my $line;
|
|
|
37 |
while ($line = <IN>)
|
|
|
38 |
{
|
|
|
39 |
chomp $line;
|
|
|
40 |
|
|
|
41 |
if ($line =~ /RequestMapping\s*\(\s*value\s*=\s*"(.*?)"/)
|
|
|
42 |
{
|
|
|
43 |
$path = $1;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
if ($line =~ /method\s*=\s*RequestMethod.(\w+)/)
|
|
|
47 |
{
|
|
|
48 |
$method = $1;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
if ($line =~ /ApiOperation\s*\(\s*value\s*=\s*"(.*?)"/)
|
|
|
52 |
{
|
|
|
53 |
$description = $1;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
if ($line =~ /public\s+ResponseEntity<(List<\w+>|\w+)> (\w+)\((.*)\)/)
|
|
|
57 |
{
|
|
|
58 |
$response = $1;
|
|
|
59 |
$name = $2;
|
|
|
60 |
my @arglist;
|
|
|
61 |
for my $arg (split /,/, $3)
|
|
|
62 |
{
|
|
|
63 |
$arg =~ /@(\w+)\s+(\w+)\s+(\w+)/;
|
|
|
64 |
my $loc = $locmap->{$1};
|
|
|
65 |
push @arglist, {'location' => $loc, 'type' => $2, 'name' => $3};
|
|
|
66 |
}
|
|
|
67 |
$args = \@arglist;
|
|
|
68 |
push @calls, {'path' => $path, 'method' => $method, 'description' => $description, 'name' => $name, 'response' => $response, 'args' => $args};
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
close IN;
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
sub PMservice()
|
|
|
76 |
{
|
|
|
77 |
|
|
|
78 |
open OUT, "> $TRG/PersonalizationManagerService.java";
|
|
|
79 |
|
|
|
80 |
print OUT << "EOT";
|
|
|
81 |
package alfred.eu.personalizationmanagerapi;
|
|
|
82 |
|
|
|
83 |
import alfred.eu.personalizationmanagerapi.client.dto.Contact;
|
|
|
84 |
import alfred.eu.personalizationmanagerapi.client.dto.Group;
|
|
|
85 |
import alfred.eu.personalizationmanagerapi.client.dto.HealthProfile;
|
|
|
86 |
import alfred.eu.personalizationmanagerapi.client.dto.Requesters;
|
|
|
87 |
import alfred.eu.personalizationmanagerapi.client.dto.UserProfile;
|
|
|
88 |
import retrofit.client.Response;
|
|
|
89 |
import retrofit.http.DELETE;
|
|
|
90 |
import retrofit.http.FormUrlEncoded;
|
|
|
91 |
import retrofit.http.GET;
|
|
|
92 |
import retrofit.http.POST;
|
|
|
93 |
import retrofit.http.PUT;
|
|
|
94 |
import retrofit.http.Path;
|
|
|
95 |
import retrofit.http.Field;
|
|
|
96 |
|
|
|
97 |
public interface PersonalizationManagerService {
|
|
|
98 |
|
|
|
99 |
EOT
|
|
|
100 |
|
|
|
101 |
for my $rref (@calls)
|
|
|
102 |
{
|
|
|
103 |
my $bodyarg = 0;
|
|
|
104 |
my @arglist;
|
|
|
105 |
for my $arg (@{$rref->{'args'}})
|
|
|
106 |
{
|
|
|
107 |
$bodyarg = 1 if ($arg->{'location'} eq 'Field');
|
|
|
108 |
push @arglist, "\@" . $arg->{'location'} . '("' . $arg->{'name'} . '") ' . $arg->{'type'} . ' ' . $arg->{'name'};
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
print OUT " // " . $rref->{'description'} . "\n";
|
|
|
112 |
print OUT " \@FormUrlEncoded\n" if ($bodyarg == 1);
|
|
|
113 |
print OUT " \@" . $rref->{'method'} . '("' . $rref->{'path'} . "\")\n";
|
|
|
114 |
$args = join ", ", @arglist;
|
|
|
115 |
print OUT " public Response " . $rref->{'name'} . "($args);\n";
|
|
|
116 |
print OUT "\n";
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
print OUT "}\n";
|
|
|
120 |
close OUT;
|
|
|
121 |
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
sub PMgr()
|
|
|
125 |
{
|
|
|
126 |
|
|
|
127 |
open OUT, "> $SHR/webservice/PersonalizationManager.java";
|
|
|
128 |
|
|
|
129 |
print OUT << "EOT";
|
|
|
130 |
package eu.alfred.api.personalization.webservice;
|
|
|
131 |
|
|
|
132 |
import android.os.Bundle;
|
|
|
133 |
import android.os.Handler;
|
|
|
134 |
import android.os.Message;
|
|
|
135 |
import android.os.Messenger;
|
|
|
136 |
import android.os.RemoteException;
|
|
|
137 |
|
|
|
138 |
import com.google.gson.Gson;
|
|
|
139 |
|
|
|
140 |
import org.json.JSONArray;
|
|
|
141 |
import org.json.JSONException;
|
|
|
142 |
import org.json.JSONObject;
|
|
|
143 |
|
|
|
144 |
import eu.alfred.api.personalization.model.*;
|
|
|
145 |
import eu.alfred.api.personalization.responses.PersonalizationResponse;
|
|
|
146 |
import eu.alfred.api.personalization.client.*;
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
public class PersonalizationManager {
|
|
|
150 |
|
|
|
151 |
private Messenger messenger;
|
|
|
152 |
|
|
|
153 |
private class PersonalizationSuccessResponse extends Handler {
|
|
|
154 |
private PersonalizationResponse personalizationSuccessResponse;
|
|
|
155 |
|
|
|
156 |
public PersonalizationSuccessResponse(PersonalizationResponse personalizationSuccessResponse) {
|
|
|
157 |
this.personalizationSuccessResponse = personalizationSuccessResponse;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
\@Override
|
|
|
161 |
public void handleMessage(Message msg) {
|
|
|
162 |
int respCode = msg.what;
|
|
|
163 |
|
|
|
164 |
switch (respCode) {
|
|
|
165 |
case PersonalizationConstants.CREATE_USER_PROFILE_RESPONSE:
|
|
|
166 |
case PersonalizationConstants.UPDATE_USER_PROFILE_RESPONSE:
|
|
|
167 |
case PersonalizationConstants.DELETE_USER_PROFILE_RESPONSE:
|
|
|
168 |
case PersonalizationConstants.CREATE_USER_CONTACT_RESPONSE:
|
|
|
169 |
case PersonalizationConstants.UPDATE_USER_CONTACT_RESPONSE:
|
|
|
170 |
case PersonalizationConstants.DELETE_USER_CONTACT_RESPONSE:
|
|
|
171 |
case PersonalizationConstants.CREATE_REQUESTER_RESPONSE:
|
|
|
172 |
case PersonalizationConstants.UPDATE_REQUESTER_RESPONSE:
|
|
|
173 |
case PersonalizationConstants.DELETE_REQUESTER_RESPONSE:
|
|
|
174 |
case PersonalizationConstants.ADD_MEMBER_TO_GROUP_RESPONSE:
|
|
|
175 |
case PersonalizationConstants.CREATE_GROUP_RESPONSE:
|
|
|
176 |
case PersonalizationConstants.DELETE_GROUP_RESPONSE:
|
|
|
177 |
case PersonalizationConstants.DELETE_USER_HEALTH_PROFILE_RESPONSE:
|
|
|
178 |
case PersonalizationConstants.UPDATE_GROUP_RESPONSE:
|
|
|
179 |
case PersonalizationConstants.UPDATE_USER_HEALTH_PROFILE_RESPONSE:
|
|
|
180 |
case PersonalizationConstants.CREATE_USER_HEALTH_PROFILE_RESPONSE:
|
|
|
181 |
case PersonalizationConstants.REMOVE_MEMBER_FROM_GROUP_RESPONSE:
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
JSONObject jsonResponse = null;
|
|
|
185 |
|
|
|
186 |
try {
|
|
|
187 |
jsonResponse = new JSONObject(msg.getData().getString("JsonData", "{}"));
|
|
|
188 |
personalizationSuccessResponse.OnSuccess(jsonResponse);
|
|
|
189 |
} catch (JSONException e) {
|
|
|
190 |
e.printStackTrace();
|
|
|
191 |
personalizationSuccessResponse.OnError(e);
|
|
|
192 |
}
|
|
|
193 |
break;
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
private class PersonalizationDataResponse extends Handler {
|
|
|
199 |
private PersonalizationResponse personalizationDataResponse;
|
|
|
200 |
|
|
|
201 |
public PersonalizationDataResponse(PersonalizationResponse personalizationDataResponse) {
|
|
|
202 |
this.personalizationDataResponse = personalizationDataResponse;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
\@Override
|
|
|
206 |
public void handleMessage(Message msg) {
|
|
|
207 |
int respCode = msg.what;
|
|
|
208 |
|
|
|
209 |
switch (respCode) {
|
|
|
210 |
case PersonalizationConstants.CREATE_USER_PROFILE_FILTER_RESPONSE:
|
|
|
211 |
case PersonalizationConstants.CREATE_USER_PROFILE_RESPONSE:
|
|
|
212 |
case PersonalizationConstants.RETRIEVE_USER_CONTACTS_BY_CRITERIA_RESPONSE:
|
|
|
213 |
case PersonalizationConstants.RETRIEVE_USER_PROFILES_LAST_SYNC_RESPONSE:
|
|
|
214 |
case PersonalizationConstants.RETRIEVE_USER_PROFILE_RESPONSE:
|
|
|
215 |
case PersonalizationConstants.UPDATE_REQUESTER_RESPONSE:
|
|
|
216 |
case PersonalizationConstants.UPDATE_USER_CONTACT_RESPONSE:
|
|
|
217 |
case PersonalizationConstants.RETRIEVE_USER_PROFILES_BY_CRITERIA_RESPONSE:
|
|
|
218 |
case PersonalizationConstants.RETRIEVE_REQUESTER_RESPONSE:
|
|
|
219 |
case PersonalizationConstants.RETRIEVE_USER_REQUESTER_RESPONSE:
|
|
|
220 |
case PersonalizationConstants.RETRIEVE_USER_CONTACTS_RESPONSE:
|
|
|
221 |
case PersonalizationConstants.RETRIEVE_USER_ID_RESPONSE:
|
|
|
222 |
case PersonalizationConstants.GET_UNSYNCHRONIZED_USER_PROFILES_RESPONSE:
|
|
|
223 |
case PersonalizationConstants.RETRIEVE_ALL_USER_CONTACTS_RESPONSE:
|
|
|
224 |
case PersonalizationConstants.RETRIEVE_GROUP_RESPONSE:
|
|
|
225 |
case PersonalizationConstants.RETRIEVE_GROUPS_WITH_MEMBER_RESPONSE:
|
|
|
226 |
case PersonalizationConstants.RETRIEVE_FILTERED_GROUPS_RESPONSE:
|
|
|
227 |
case PersonalizationConstants.RETRIEVE_USER_CONTACT_RESPONSE:
|
|
|
228 |
case PersonalizationConstants.RETRIEVE_USER_HEALTH_PROFILE_RESPONSE:
|
|
|
229 |
case PersonalizationConstants.RETRIEVE_USER_PROFILE_SENSORED_RESPONSE:
|
|
|
230 |
case PersonalizationConstants.RETRIEVE_USER_PROFILES_RESPONSE:
|
|
|
231 |
case PersonalizationConstants.RETRIEVE_USERS_GROUPS_RESPONSE:
|
|
|
232 |
case PersonalizationConstants.UPDATE_GROUP_RESPONSE:
|
|
|
233 |
|
|
|
234 |
JSONArray jsonResponse = null;
|
|
|
235 |
|
|
|
236 |
try {
|
|
|
237 |
jsonResponse = new JSONArray(msg.getData().getString("response"));
|
|
|
238 |
personalizationDataResponse.OnSuccess(jsonResponse);
|
|
|
239 |
} catch (JSONException e) {
|
|
|
240 |
e.printStackTrace();
|
|
|
241 |
personalizationDataResponse.OnError(e);
|
|
|
242 |
}
|
|
|
243 |
break;
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
public PersonalizationManager(Messenger messenger) {
|
|
|
249 |
this.messenger = messenger;
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
EOT
|
|
|
253 |
|
|
|
254 |
for my $rref (@calls)
|
|
|
255 |
{
|
|
|
256 |
my $bodyarg = 0;
|
|
|
257 |
my @arglist;
|
|
|
258 |
for my $arg (@{$rref->{'args'}})
|
|
|
259 |
{
|
|
|
260 |
$bodyarg = 1 if ($arg->{'location'} eq 'Field');
|
|
|
261 |
push @arglist, $arg->{'type'} . ' ' . $arg->{'name'};
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
print OUT "\n\t// " . $rref->{'description'} . "\n";
|
|
|
265 |
$args = join ", ", @arglist;
|
|
|
266 |
|
|
|
267 |
my $name = $rref->{'name'};
|
|
|
268 |
my $nameUC = &camelCaseToCapitals($name);
|
|
|
269 |
my $restype = ($name =~ /^(get|retrieve)/) ? "Data" : "Success";
|
|
|
270 |
|
|
|
271 |
print OUT << "EOT";
|
|
|
272 |
public void ${name}(${args}, PersonalizationResponse response) {
|
|
|
273 |
if (messenger != null) {
|
|
|
274 |
Message msg = Message.obtain(null, PersonalizationConstants.${nameUC});
|
|
|
275 |
|
|
|
276 |
if (response != null)
|
|
|
277 |
msg.replyTo = new Messenger(new Personalization${restype}Response(response));
|
|
|
278 |
|
|
|
279 |
Bundle data = new Bundle();
|
|
|
280 |
|
|
|
281 |
EOT
|
|
|
282 |
|
|
|
283 |
my $useGson = 0;
|
|
|
284 |
for my $arg (@{$rref->{'args'}})
|
|
|
285 |
{
|
|
|
286 |
my $at = $arg->{'type'};
|
|
|
287 |
$useGson = 1 unless ($at eq 'String');
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
print OUT "\t\t\tGson gson = new Gson();\n" if $useGson == 1;
|
|
|
291 |
|
|
|
292 |
for my $arg (@{$rref->{'args'}})
|
|
|
293 |
{
|
|
|
294 |
my $an = $arg->{'name'};
|
|
|
295 |
my $at = $arg->{'type'};
|
|
|
296 |
if ($at eq 'String')
|
|
|
297 |
{
|
|
|
298 |
print OUT "\t\t\tdata.putString(\"$an\", ${an});\n";
|
|
|
299 |
}
|
|
|
300 |
else
|
|
|
301 |
{
|
|
|
302 |
print OUT "\t\t\t${at}Dto ${an}Dto = ${at}Mapper.toDto($an);\n";
|
|
|
303 |
print OUT "\t\t\tString ${an}Json = gson.toJson(${an}Dto);\n";
|
|
|
304 |
print OUT "\t\t\tdata.putString(\"$an\", ${an}Json);\n";
|
|
|
305 |
}
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
print OUT << "EOT";
|
|
|
309 |
msg.setData(data);
|
|
|
310 |
try {
|
|
|
311 |
messenger.send(msg);
|
|
|
312 |
} catch (RemoteException e) {
|
|
|
313 |
e.printStackTrace();
|
|
|
314 |
}
|
|
|
315 |
}
|
|
|
316 |
}
|
|
|
317 |
EOT
|
|
|
318 |
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
print OUT "}\n";
|
|
|
322 |
close OUT;
|
|
|
323 |
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
|
|
|
327 |
sub tfac()
|
|
|
328 |
{
|
|
|
329 |
|
|
|
330 |
open OUT, "> $PAS/PersonalizationCRUDTaskFactory.java";
|
|
|
331 |
|
|
|
332 |
print OUT << "EOT";
|
|
|
333 |
package eu.alfred.personalassistant.service.personalization;
|
|
|
334 |
|
|
|
335 |
import android.content.Context;
|
|
|
336 |
import android.os.AsyncTask;
|
|
|
337 |
|
|
|
338 |
import eu.alfred.api.personalization.webservice.PersonalizationConstants;
|
|
|
339 |
import eu.alfred.personalassistant.service.personalization.tasks.CreateUserProfileTask;
|
|
|
340 |
import eu.alfred.personalassistant.service.personalization.tasks.RetrieveUserProfileTask;
|
|
|
341 |
|
|
|
342 |
public class PersonalizationCRUDTaskFactory {
|
|
|
343 |
public static AsyncTask<PersonalizationMessageClass, Void, String> GetInstance(Context context, int msgType){
|
|
|
344 |
switch(msgType){
|
|
|
345 |
EOT
|
|
|
346 |
|
|
|
347 |
for my $rref (@calls)
|
|
|
348 |
{
|
|
|
349 |
my $name = $rref->{'name'};
|
|
|
350 |
my $nameuc = ucfirst($name);
|
|
|
351 |
my $nameUC = &camelCaseToCapitals($name);
|
|
|
352 |
|
|
|
353 |
print OUT "\t\t\t// " . $rref->{'description'} . "\n";
|
|
|
354 |
print OUT "\t\t\tcase PersonalizationConstants.${nameUC}:\n";
|
|
|
355 |
print OUT "\t\t\t\treturn new ${nameuc}Task(context);\n";
|
|
|
356 |
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
print OUT << "EOT";
|
|
|
360 |
default:
|
|
|
361 |
throw new IllegalArgumentException("The parameter value is invalid. Value: " + String.valueOf(msgType));
|
|
|
362 |
}
|
|
|
363 |
}
|
|
|
364 |
}
|
|
|
365 |
EOT
|
|
|
366 |
close OUT;
|
|
|
367 |
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
|
|
|
372 |
sub client()
|
|
|
373 |
{
|
|
|
374 |
|
|
|
375 |
|
|
|
376 |
for my $rref (@calls)
|
|
|
377 |
{
|
|
|
378 |
my $name = $rref->{'name'};
|
|
|
379 |
my $nameuc = ucfirst($name);
|
|
|
380 |
my $response = $rref->{'response'};
|
|
|
381 |
|
|
|
382 |
# next if $name ne 'createUserProfile';
|
|
|
383 |
|
|
|
384 |
# -------------------- Client -----------------------------
|
|
|
385 |
|
|
|
386 |
my $requestparams = '';
|
|
|
387 |
my @arglist;
|
|
|
388 |
for my $par (@{$rref->{'args'}})
|
|
|
389 |
{
|
|
|
390 |
my $pn = $par->{'name'};
|
|
|
391 |
my $pt = $par->{'type'};
|
|
|
392 |
|
|
|
393 |
if ($pt eq 'String' && $pn =~ /id$/i)
|
|
|
394 |
{
|
|
|
395 |
$requestparams .= "\t\t$pt $pn = request.$pn;\n";
|
|
|
396 |
}
|
|
|
397 |
elsif ($pt eq 'String' && $par->{'location'} eq 'Field' && $pn eq 'valuesToUpdate')
|
|
|
398 |
{
|
|
|
399 |
$requestparams .= "\t\t$pt $pn = request.valuesToUpdate;\n";
|
|
|
400 |
}
|
|
|
401 |
else
|
|
|
402 |
{
|
|
|
403 |
$requestparams .= "\t\t$pt $pn = new ${pt}Mapper().toModel(request.${pn});\n";
|
|
|
404 |
}
|
|
|
405 |
push @arglist, $pn;
|
|
|
406 |
}
|
|
|
407 |
my $requestarglist = join ', ', @arglist;
|
|
|
408 |
|
|
|
409 |
my $responsetype = "${nameuc}Response";
|
|
|
410 |
if ($response eq 'String')
|
|
|
411 |
{
|
|
|
412 |
$responsetype = "StringResponse";
|
|
|
413 |
}
|
|
|
414 |
elsif ($response eq 'List<UserProfile>')
|
|
|
415 |
{
|
|
|
416 |
$responsetype = "UserProfileResponse";
|
|
|
417 |
}
|
|
|
418 |
elsif ($response eq 'List<HealthProfile>')
|
|
|
419 |
{
|
|
|
420 |
$responsetype = "HealthProfileResponse";
|
|
|
421 |
}
|
|
|
422 |
elsif ($response eq 'List<Group>')
|
|
|
423 |
{
|
|
|
424 |
$responsetype = "GroupResponse";
|
|
|
425 |
}
|
|
|
426 |
elsif ($response eq 'List<Contact>')
|
|
|
427 |
{
|
|
|
428 |
$responsetype = "ContactResponse";
|
|
|
429 |
}
|
|
|
430 |
elsif ($response eq 'List<Requester>')
|
|
|
431 |
{
|
|
|
432 |
$responsetype = "RequesterResponse";
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
|
|
|
436 |
|
|
|
437 |
open OUT, "> $TRG/client/client/${nameuc}Client.java";
|
|
|
438 |
|
|
|
439 |
print OUT << "EOT";
|
|
|
440 |
package alfred.eu.personalizationmanagerapi.client.client;
|
|
|
441 |
|
|
|
442 |
import android.content.Context;
|
|
|
443 |
|
|
|
444 |
import alfred.eu.personalizationmanagerapi.PersonalizationManagerAdapter;
|
|
|
445 |
import alfred.eu.personalizationmanagerapi.PersonalizationManagerService;
|
|
|
446 |
import alfred.eu.personalizationmanagerapi.client.ClientResponse;
|
|
|
447 |
import alfred.eu.personalizationmanagerapi.client.dto.*;
|
|
|
448 |
import alfred.eu.personalizationmanagerapi.client.mapper.StringResponseMapper;
|
|
|
449 |
import alfred.eu.personalizationmanagerapi.client.mapper.UserProfileMapper;
|
|
|
450 |
import alfred.eu.personalizationmanagerapi.client.request.${nameuc}Request;
|
|
|
451 |
import alfred.eu.personalizationmanagerapi.client.response.${responsetype};
|
|
|
452 |
import alfred.eu.personalizationmanagerapi.util.Strings;
|
|
|
453 |
|
|
|
454 |
import retrofit.client.Response;
|
|
|
455 |
|
|
|
456 |
|
|
|
457 |
public class ${nameuc}Client extends Client<${nameuc}Request, ClientResponse<${responsetype}>> {
|
|
|
458 |
|
|
|
459 |
public ${nameuc}Client(Context context) {
|
|
|
460 |
super(context);
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
\@Override
|
|
|
464 |
protected ClientResponse<${responsetype}> request(${nameuc}Request request) throws Exception {
|
|
|
465 |
PersonalizationManagerService service = PersonalizationManagerAdapter.getInstance(getContext()).getService();
|
|
|
466 |
|
|
|
467 |
${requestparams}
|
|
|
468 |
|
|
|
469 |
Response serviceResponse = service.${name}(${requestarglist});
|
|
|
470 |
|
|
|
471 |
printResponse(serviceResponse);
|
|
|
472 |
|
|
|
473 |
EOT
|
|
|
474 |
|
|
|
475 |
if ($response eq 'String')
|
|
|
476 |
{
|
|
|
477 |
print OUT << "EOT";
|
|
|
478 |
ClientResponse<StringResponse> response = new StringResponseMapper().mapResponse(
|
|
|
479 |
Strings.convertStreamToString(serviceResponse.getBody().in(), "UTF-8"));
|
|
|
480 |
EOT
|
|
|
481 |
}
|
|
|
482 |
else
|
|
|
483 |
{
|
|
|
484 |
print OUT << "EOT";
|
|
|
485 |
ClientResponse<${response}> response = new ${responsetype}Mapper().mapResponse(
|
|
|
486 |
Strings.convertStreamToString(serviceResponse.getBody().in(), "UTF-8"));
|
|
|
487 |
EOT
|
|
|
488 |
}
|
|
|
489 |
print OUT << "EOT";
|
|
|
490 |
|
|
|
491 |
if ("OK".equals(response.status)) {
|
|
|
492 |
return response;
|
|
|
493 |
} else {
|
|
|
494 |
throw new Exception("${nameuc} failed");
|
|
|
495 |
}
|
|
|
496 |
}
|
|
|
497 |
}
|
|
|
498 |
EOT
|
|
|
499 |
close OUT;
|
|
|
500 |
|
|
|
501 |
}
|
|
|
502 |
|
|
|
503 |
}
|
|
|
504 |
|
|
|
505 |
|
|
|
506 |
sub task()
|
|
|
507 |
{
|
|
|
508 |
|
|
|
509 |
for my $rref (@calls)
|
|
|
510 |
{
|
|
|
511 |
my $name = $rref->{'name'};
|
|
|
512 |
my $nameuc = ucfirst($name);
|
|
|
513 |
my $response = $rref->{'response'};
|
|
|
514 |
my $nameUC = &camelCaseToCapitals($name);
|
|
|
515 |
|
|
|
516 |
# next if $name ne 'createUserProfile';
|
|
|
517 |
# next if $name ne 'createUserContact';
|
|
|
518 |
|
|
|
519 |
# -------------------- Task -----------------------------
|
|
|
520 |
|
|
|
521 |
my $requestparams = '';
|
|
|
522 |
my @arglist;
|
|
|
523 |
for my $par (@{$rref->{'args'}})
|
|
|
524 |
{
|
|
|
525 |
if ($par->{'type'} eq 'String' && $par->{'name'} =~ /id$/i)
|
|
|
526 |
{
|
|
|
527 |
$requestparams .= "\t\t" . $par->{'type'} . " " . $par->{'name'} . " = request." . $par->{'name'} . ";\n";
|
|
|
528 |
}
|
|
|
529 |
elsif ($par->{'type'} eq 'String' && $par->{'location'} eq 'Field' && $par->{'name'} eq 'valuesToUpdate')
|
|
|
530 |
{
|
|
|
531 |
$requestparams .= "\t\t" . $par->{'type'} . " " . $par->{'name'} . " = request.valuesToUpdate;\n";
|
|
|
532 |
}
|
|
|
533 |
else
|
|
|
534 |
{
|
|
|
535 |
$requestparams .= "\t\t" . $par->{'type'} . " " . $par->{'name'} . " = new " . $par->{'type'} . "RequestMapper().toModel(request);\n";
|
|
|
536 |
}
|
|
|
537 |
push @arglist, $par->{'name'};
|
|
|
538 |
}
|
|
|
539 |
my $requestarglist = join ', ', @arglist;
|
|
|
540 |
|
|
|
541 |
my $responsetype = "${nameuc}Response";
|
|
|
542 |
if ($response eq 'String')
|
|
|
543 |
{
|
|
|
544 |
$responsetype = "StringResponse";
|
|
|
545 |
}
|
|
|
546 |
elsif ($response eq 'List<UserProfile>')
|
|
|
547 |
{
|
|
|
548 |
$responsetype = "UserProfilesResponse";
|
|
|
549 |
}
|
|
|
550 |
elsif ($response eq 'List<HealthProfile>')
|
|
|
551 |
{
|
|
|
552 |
$responsetype = "HealthProfilesResponse";
|
|
|
553 |
}
|
|
|
554 |
elsif ($response eq 'List<Group>')
|
|
|
555 |
{
|
|
|
556 |
$responsetype = "GroupsResponse";
|
|
|
557 |
}
|
|
|
558 |
elsif ($response eq 'List<Contact>')
|
|
|
559 |
{
|
|
|
560 |
$responsetype = "ContactsResponse";
|
|
|
561 |
}
|
|
|
562 |
elsif ($response eq 'List<Requester>')
|
|
|
563 |
{
|
|
|
564 |
$responsetype = "RequestersResponse";
|
|
|
565 |
}
|
|
|
566 |
|
|
|
567 |
|
|
|
568 |
|
|
|
569 |
open OUT, "> $PAS/tasks/${nameuc}Task.java";
|
|
|
570 |
|
|
|
571 |
print OUT << "EOT";
|
|
|
572 |
package eu.alfred.personalassistant.service.personalization.tasks;
|
|
|
573 |
|
|
|
574 |
import android.content.Context;
|
|
|
575 |
import android.os.AsyncTask;
|
|
|
576 |
import android.os.Bundle;
|
|
|
577 |
import android.os.Message;
|
|
|
578 |
import android.os.RemoteException;
|
|
|
579 |
import android.util.Log;
|
|
|
580 |
|
|
|
581 |
import com.google.gson.Gson;
|
|
|
582 |
|
|
|
583 |
import alfred.eu.personalizationmanagerapi.client.ClientResponse;
|
|
|
584 |
import alfred.eu.personalizationmanagerapi.client.client.${nameuc}Client;
|
|
|
585 |
import alfred.eu.personalizationmanagerapi.client.request.${nameuc}Request;
|
|
|
586 |
import alfred.eu.personalizationmanagerapi.client.response.${responsetype};
|
|
|
587 |
EOT
|
|
|
588 |
|
|
|
589 |
for my $par (@{$rref->{'args'}})
|
|
|
590 |
{
|
|
|
591 |
my $pt = $par->{'type'};
|
|
|
592 |
if ($pt ne 'String')
|
|
|
593 |
{
|
|
|
594 |
print OUT "import eu.alfred.api.personalization.client.${pt}Dto;\n";
|
|
|
595 |
print OUT "import alfred.eu.personalizationmanagerapi.client.mapper.${pt}ResponseMapper;\n";
|
|
|
596 |
}
|
|
|
597 |
}
|
|
|
598 |
|
|
|
599 |
|
|
|
600 |
print OUT << "EOT";
|
|
|
601 |
import eu.alfred.api.personalization.webservice.PersonalizationConstants;
|
|
|
602 |
import eu.alfred.personalassistant.service.personalization.PersonalizationMessageClass;
|
|
|
603 |
|
|
|
604 |
public class ${nameuc}Task extends AsyncTask<PersonalizationMessageClass, Void, String> {
|
|
|
605 |
|
|
|
606 |
private final static String TAG = "${nameuc}Task";
|
|
|
607 |
|
|
|
608 |
private Context context;
|
|
|
609 |
private PersonalizationMessageClass msg;
|
|
|
610 |
private Exception exception;
|
|
|
611 |
|
|
|
612 |
public ${nameuc}Task(Context context) {
|
|
|
613 |
this.context = context;
|
|
|
614 |
}
|
|
|
615 |
|
|
|
616 |
\@Override
|
|
|
617 |
protected String doInBackground(PersonalizationMessageClass... params) {
|
|
|
618 |
try {
|
|
|
619 |
msg = params[0];
|
|
|
620 |
${nameuc}Request request = new ${nameuc}Request();
|
|
|
621 |
EOT
|
|
|
622 |
for my $par (@{$rref->{'args'}})
|
|
|
623 |
{
|
|
|
624 |
my $pn = $par->{'name'};
|
|
|
625 |
my $pt = $par->{'type'};
|
|
|
626 |
if ($pt eq 'String')
|
|
|
627 |
{
|
|
|
628 |
print OUT "\t\t\trequest.$pn = msg.data.getString(\"$pn\");\n";
|
|
|
629 |
}
|
|
|
630 |
else
|
|
|
631 |
{
|
|
|
632 |
print OUT "\t\t\trequest.$pn = new Gson().fromJson(msg.data.getString(\"$pn\"), ${pt}Dto.class);\n";
|
|
|
633 |
}
|
|
|
634 |
}
|
|
|
635 |
print OUT << "EOT";
|
|
|
636 |
|
|
|
637 |
${nameuc}Client client = new ${nameuc}Client(context);
|
|
|
638 |
ClientResponse<${responsetype}> response = client.execute(request);
|
|
|
639 |
|
|
|
640 |
if (response.errorCode != 0)
|
|
|
641 |
exception = new Exception("(" + response.errorCode + ") " + response.status);
|
|
|
642 |
|
|
|
643 |
EOT
|
|
|
644 |
|
|
|
645 |
if ($response eq 'String')
|
|
|
646 |
{
|
|
|
647 |
print OUT "\t\t\tString result = response.data.string;\n";
|
|
|
648 |
}
|
|
|
649 |
else
|
|
|
650 |
{
|
|
|
651 |
print OUT "\t\t\tString result = new ${responsetype}Mapper().toModel(response.data);\n";
|
|
|
652 |
}
|
|
|
653 |
|
|
|
654 |
|
|
|
655 |
print OUT << "EOT";
|
|
|
656 |
return new Gson().toJson(result);
|
|
|
657 |
} catch (Exception e) {
|
|
|
658 |
exception = e;
|
|
|
659 |
Log.e(TAG, e.getClass().getSimpleName(), e);
|
|
|
660 |
}
|
|
|
661 |
return null;
|
|
|
662 |
}
|
|
|
663 |
|
|
|
664 |
\@Override
|
|
|
665 |
protected void onPostExecute(String result) {
|
|
|
666 |
super.onPostExecute(result);
|
|
|
667 |
Message resp = Message.obtain(null, PersonalizationConstants.${nameUC}_RESPONSE);
|
|
|
668 |
Bundle bundle = new Bundle();
|
|
|
669 |
bundle.putString(PersonalizationConstants.EXTRAS_JSON, result);
|
|
|
670 |
if (exception != null)
|
|
|
671 |
bundle.putString(PersonalizationConstants.EXTRAS_EXCEPTION, exception.getClass().getSimpleName() + ": " + exception.getMessage());
|
|
|
672 |
|
|
|
673 |
resp.setData(bundle);
|
|
|
674 |
try {
|
|
|
675 |
msg.replyTo.send(resp);
|
|
|
676 |
} catch (RemoteException e) {
|
|
|
677 |
Log.e(TAG, e.getClass().getSimpleName(), e);
|
|
|
678 |
}
|
|
|
679 |
}
|
|
|
680 |
}
|
|
|
681 |
EOT
|
|
|
682 |
close OUT;
|
|
|
683 |
|
|
|
684 |
}
|
|
|
685 |
|
|
|
686 |
}
|
|
|
687 |
|
|
|
688 |
|
|
|
689 |
sub request()
|
|
|
690 |
{
|
|
|
691 |
|
|
|
692 |
for my $rref (@calls)
|
|
|
693 |
{
|
|
|
694 |
my $name = $rref->{'name'};
|
|
|
695 |
my $nameuc = ucfirst($name);
|
|
|
696 |
my $response = $rref->{'response'};
|
|
|
697 |
my $nameUC = &camelCaseToCapitals($name);
|
|
|
698 |
|
|
|
699 |
|
|
|
700 |
# -------------------- Request -----------------------------
|
|
|
701 |
|
|
|
702 |
open OUT, "> $TRG/client/request/${nameuc}Request.java";
|
|
|
703 |
|
|
|
704 |
print OUT << "EOT";
|
|
|
705 |
package alfred.eu.personalizationmanagerapi.client.request;
|
|
|
706 |
|
|
|
707 |
import com.google.gson.annotations.Expose;
|
|
|
708 |
|
|
|
709 |
public class ${nameuc}Request {
|
|
|
710 |
EOT
|
|
|
711 |
|
|
|
712 |
for my $par (@{$rref->{'args'}})
|
|
|
713 |
{
|
|
|
714 |
print OUT "\t\@Expose\n";
|
|
|
715 |
print OUT "\tpublic " . $par->{'type'} . " " . $par->{'name'} . ";\n";
|
|
|
716 |
}
|
|
|
717 |
|
|
|
718 |
print OUT "}\n";
|
|
|
719 |
|
|
|
720 |
close OUT;
|
|
|
721 |
|
|
|
722 |
}
|
|
|
723 |
|
|
|
724 |
}
|
|
|
725 |
|
|
|
726 |
|
|
|
727 |
sub response()
|
|
|
728 |
{
|
|
|
729 |
|
|
|
730 |
for my $rref (@calls)
|
|
|
731 |
{
|
|
|
732 |
my $name = $rref->{'name'};
|
|
|
733 |
my $nameuc = ucfirst($name);
|
|
|
734 |
my $response = $rref->{'response'};
|
|
|
735 |
my $nameUC = &camelCaseToCapitals($name);
|
|
|
736 |
|
|
|
737 |
|
|
|
738 |
|
|
|
739 |
# -------------------- Response -----------------------------
|
|
|
740 |
|
|
|
741 |
my $resobj = $name;
|
|
|
742 |
$resobj =~ s/^[a-z]*([A-Z])//;
|
|
|
743 |
$resobj = lc($1) . $resobj;
|
|
|
744 |
|
|
|
745 |
open OUT, "> $TRG/client/response/${nameuc}Response.java";
|
|
|
746 |
|
|
|
747 |
print OUT << "EOT";
|
|
|
748 |
package alfred.eu.personalizationmanagerapi.client.response;
|
|
|
749 |
|
|
|
750 |
import com.google.gson.annotations.Expose;
|
|
|
751 |
|
|
|
752 |
public class ${nameuc}Response {
|
|
|
753 |
|
|
|
754 |
\@Expose
|
|
|
755 |
public ${response} ${resobj};
|
|
|
756 |
}
|
|
|
757 |
EOT
|
|
|
758 |
|
|
|
759 |
close OUT;
|
|
|
760 |
|
|
|
761 |
}
|
|
|
762 |
|
|
|
763 |
}
|
|
|
764 |
|
|
|
765 |
print "apigenerator [PMservice/PMgr/client/task/request/response]\n";
|
|
|
766 |
|
|
|
767 |
for my $t (@ARGV)
|
|
|
768 |
{
|
|
|
769 |
&PMservice() if $t eq 'PMservice';
|
|
|
770 |
&PMgr() if $t eq 'PMgr';
|
|
|
771 |
&tfac() if $t eq 'tfac';
|
|
|
772 |
&client() if $t eq 'client';
|
|
|
773 |
&task() if $t eq 'task';
|
|
|
774 |
&request() if $t eq 'request';
|
|
|
775 |
&response() if $t eq 'response';
|
|
|
776 |
}
|
|
|
777 |
|
|
|
778 |
|