Subversion Repository Public Repository

litesoft

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
 * Copyright 2011 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.gwtphonegap.client.contacts.browser;

import java.util.Date;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONNull;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.googlecode.gwtphonegap.client.contacts.Contact;
import com.googlecode.gwtphonegap.client.contacts.ContactAddress;
import com.googlecode.gwtphonegap.client.contacts.ContactField;
import com.googlecode.gwtphonegap.client.contacts.ContactName;
import com.googlecode.gwtphonegap.client.contacts.ContactOrganisation;
import com.googlecode.gwtphonegap.collection.shared.CollectionFactory;
import com.googlecode.gwtphonegap.collection.shared.LightArray;

public class ContactBrowserImpl implements Contact {

	private static final String CONTACT_FIELD_PREF = "pref";

	private static final String CONTACT_FIELD_VALUE = "value";

	private static final String CONTACT_FIELD_TYPE = "type";

	private static final String NAME_SUFFIX = "suffix";

	private static final String NAME_PREFIX = "prefix";

	private static final String NAME_MIDDLE_NAME = "middleName";

	private static final String NAME_GIVEN_NAME = "givenName";

	private static final String NAME_FAMILY_NAME = "familyName";

	private static final String NAME_FORMATTED = "formatted";

	private static final String ADDRESS_COUNTRY = "country";

	private static final String ADDRESS_POSTAL_CODE = "postalCode";

	private static final String ADDRESS_REGION = "region";

	private static final String ADDRESS_LOCALITY = "locality";

	private static final String ADDRESS_STREET_ADDRESS = "streetAddress";

	private static final String ADDRESS_FORMATTED = "formatted";

	private static final String ORGA_TITLE = "title";

	private static final String ORGA_DEPARTMENT = "department";

	private static final String CONTACT_ORGANISATION = "organisation";

	private static final String CONTACT_ADDRESSES = "addresses";

	private static final String CONTACT_NAME = "name";

	private static final String CONTACT_URLS = "urls";

	private static final String CONTACT_CATEGORIES = "categories";

	private static final String CONTACT_PHOTOS = "photos";

	private static final String CONTACT_IMS = "ims";

	private static final String CONTACT_EMAILS = "emails";

	private static final String CONTACT_PHONE_NUMBERS = "phoneNumbers";

	private static final String CONTACT_BIRTHDAY = "birthday";

	private static final String CONTACT_TIME_ZONE = "timeZone";

	private static final String CONTACT_NOTE = "note";

	private static final String CONTACT_GENDER = "gender";

	private static final String CONTACT_REVISION = "revision";

	private static final String CONTACT_NICK_NAME = "nickName";

	private static final String CONTACT_DISPLAY_NAME = "displayName";

	private static final String FIELD_ID = "id";

	private static final String ORGA_NAME = "name";

	private static final String ADDRESS_TYPE = "type";

	private static final String ADDRESS_PREF = "pref";

	private final ContactsBrowserImpl contactBrowserImpl;

	private String id;
	private String displayName;
	private String nickName;
	private String revision;
	private Date birthday;
	private String gender;
	private String note;
	private String timeZone;

	private ContactName contactName;

	private LightArray<ContactField> phoneNumbers;

	private LightArray<ContactField> emails;

	private LightArray<ContactAddress> contactAddresses;

	private LightArray<ContactField> ims;

	private LightArray<ContactOrganisation> organisations;

	private LightArray<ContactField> photos;

	private LightArray<ContactField> categories;

	private LightArray<ContactField> urls;

	public ContactBrowserImpl(ContactsBrowserImpl contactBrowserImpl) {
		this.contactBrowserImpl = contactBrowserImpl;

	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public void setDisplayName(String displayName) {
		this.displayName = displayName;

	}

	@Override
	public String getDisplayName() {
		if (displayName == null || "".equals(displayName)) {
			String first = getName().getGivenName();
			String lastName = getName().getGivenName();

			String disp = "";
			if (first != null) {
				disp += first + " ";

			}
			if (lastName != null) {
				disp += lastName;
			}
		}

		return displayName;
	}

	@Override
	public void setName(ContactName contactName) {
		this.contactName = contactName;

	}

	@Override
	public ContactName getName() {
		if (contactName == null) {
			contactName = new ContactNameBrowserImpl();
		}
		return contactName;
	}

	@Override
	public void setNickName(String name) {
		this.nickName = name;

	}

	@Override
	public String getNickName() {
		return nickName;
	}

	@Override
	public LightArray<ContactField> getPhoneNumbers() {
		if (phoneNumbers == null) {
			phoneNumbers = CollectionFactory.constructArray();
		}
		return this.phoneNumbers;
	}

	@Override
	public void setPhoneNumbers(LightArray<ContactField> array) {
		this.phoneNumbers = array;

	}

	@Override
	public void setEmails(LightArray<ContactField> emails) {
		this.emails = emails;

	}

	@Override
	public LightArray<ContactField> getEmails() {
		if (emails == null) {
			emails = CollectionFactory.constructArray();
		}
		return emails;
	}

	@Override
	public void setContactAddresses(LightArray<ContactAddress> contactAddresses) {
		this.contactAddresses = contactAddresses;

	}

	@Override
	public LightArray<ContactAddress> getContactAddresses() {
		if (contactAddresses == null) {
			contactAddresses = CollectionFactory.constructArray();
		}
		return contactAddresses;
	}

	@Override
	public void setIms(LightArray<ContactField> ims) {
		this.ims = ims;

	}

	@Override
	public LightArray<ContactField> getIms() {
		if (ims == null) {
			ims = CollectionFactory.constructArray();
		}
		return ims;
	}

	@Override
	public void setOrganisations(LightArray<ContactOrganisation> organisations) {
		this.organisations = organisations;

	}

	@Override
	public LightArray<ContactOrganisation> getOrganisations() {
		if (organisations == null) {
			organisations = CollectionFactory.constructArray();
		}
		return this.organisations;
	}

	@Override
	public String getRevision() {
		return revision;
	}

	public void setRevision(String revision) {
		this.revision = revision;
	}

	@Override
	public Date getBirthDay() {
		return birthday;
	}

	@Override
	public void setBirthDay(Date birthday) {
		this.birthday = birthday;

	}

	@Override
	public void setGender(String gender) {
		this.gender = gender;

	}

	@Override
	public String getGender() {
		return gender;
	}

	@Override
	public void setNote(String note) {
		this.note = note;

	}

	@Override
	public String getNote() {
		return note;
	}

	@Override
	public void setPhotos(LightArray<ContactField> photos) {
		this.photos = photos;

	}

	@Override
	public LightArray<ContactField> getPhotos() {
		if (photos == null) {
			photos = CollectionFactory.constructArray();
		}
		return photos;
	}

	@Override
	public void setCategories(LightArray<ContactField> categories) {
		this.categories = categories;

	}

	@Override
	public LightArray<ContactField> getCategories() {
		if (categories == null) {
			categories = CollectionFactory.constructArray();
		}
		return categories;
	}

	@Override
	public void setUrls(LightArray<ContactField> urls) {
		this.urls = urls;

	}

	@Override
	public LightArray<ContactField> getUrls() {
		if (urls == null) {
			urls = CollectionFactory.constructArray();
		}
		return this.urls;
	}

	@Override
	public void setTimeZone(String zone) {
		timeZone = zone;

	}

	@Override
	public String getTimeZone() {
		return timeZone;
	}

	@Override
	public void remove() {
		contactBrowserImpl.remove(this);

	}

	@Override
	public void save() {
		contactBrowserImpl.save(this);

	}

	@Override
	public Contact cloneContact() {
		return contactBrowserImpl.cloneContact(this);
	}

	public static Contact fromJSON(ContactsBrowserImpl controller, JSONObject jsonContact) {
		ContactBrowserImpl contact = new ContactBrowserImpl(controller);

		// simple fields
		contact.setId(getFieldAsString(jsonContact.get(FIELD_ID)));
		contact.setDisplayName(getFieldAsString(jsonContact.get(CONTACT_DISPLAY_NAME)));
		contact.setNickName(getFieldAsString(jsonContact.get(CONTACT_NICK_NAME)));
		contact.setRevision(getFieldAsString(jsonContact.get(CONTACT_REVISION)));
		contact.setGender(getFieldAsString(jsonContact.get(CONTACT_GENDER)));
		contact.setNote(getFieldAsString(jsonContact.get(CONTACT_NOTE)));
		contact.setTimeZone(getFieldAsString(jsonContact.get(CONTACT_TIME_ZONE)));

		// birthday
		JSONValue dateValue = jsonContact.get(CONTACT_BIRTHDAY);
		if (dateValue != null && dateValue.isNumber() != null) {
			contact.setBirthDay(new Date((long) dateValue.isNumber().doubleValue()));
		}

		// contact fields
		JSONArray phoneNumberArray = jsonContact.get(CONTACT_PHONE_NUMBERS).isArray();
		LightArray<ContactField> phoneNumbers = getContactFieldsForArray(phoneNumberArray);
		contact.setPhoneNumbers(phoneNumbers);

		JSONArray emailsArray = jsonContact.get(CONTACT_EMAILS).isArray();
		LightArray<ContactField> emails = getContactFieldsForArray(emailsArray);
		contact.setEmails(emails);

		JSONArray imsArray = jsonContact.get(CONTACT_IMS).isArray();
		LightArray<ContactField> ims = getContactFieldsForArray(imsArray);
		contact.setIms(ims);

		JSONArray photosArray = jsonContact.get(CONTACT_PHOTOS).isArray();
		LightArray<ContactField> photos = getContactFieldsForArray(photosArray);
		contact.setPhotos(photos);

		JSONArray categoriesArray = jsonContact.get(CONTACT_CATEGORIES).isArray();
		LightArray<ContactField> categories = getContactFieldsForArray(categoriesArray);
		contact.setCategories(categories);

		JSONArray urlsArray = jsonContact.get(CONTACT_URLS).isArray();
		LightArray<ContactField> urls = getContactFieldsForArray(urlsArray);
		contact.setUrls(urls);

		ContactName name = getName(jsonContact.get(CONTACT_NAME).isObject());
		contact.setName(name);

		LightArray<ContactAddress> addresses = getAddressArray(jsonContact.get(CONTACT_ADDRESSES).isArray());
		contact.setContactAddresses(addresses);

		LightArray<ContactOrganisation> organisations = getContactOrganisationArray(jsonContact.get(CONTACT_ORGANISATION).isArray());
		contact.setOrganisations(organisations);

		return contact;
	}

	private static LightArray<ContactOrganisation> getContactOrganisationArray(JSONArray jsonArray) {
		LightArray<ContactOrganisation> array = CollectionFactory.constructArray();

		if (jsonArray == null) {
			return array;
		}

		for (int i = 0; i < jsonArray.size(); i++) {
			array.push(getOrganisation(jsonArray.get(i).isObject()));
		}

		return array;
	}

	private static ContactOrganisation getOrganisation(JSONObject object) {
		ContactOrganisationBrowserImpl co = new ContactOrganisationBrowserImpl();
		if (object == null) {
			return co;
		}

		String name = getFieldAsString(object.get(CONTACT_NAME));
		String department = getFieldAsString(object.get(ORGA_DEPARTMENT));
		String title = getFieldAsString(object.get(ORGA_TITLE));

		co.setDepartment(department);
		co.setName(name);
		co.setTitle(title);

		return co;
	}

	private static LightArray<ContactAddress> getAddressArray(JSONArray jsonArray) {
		LightArray<ContactAddress> array = CollectionFactory.constructArray();

		if (jsonArray == null) {
			return array;
		}

		for (int i = 0; i < jsonArray.size(); i++) {
			array.push(getAddress(jsonArray.get(i).isObject()));
		}

		return array;
	}

	private static ContactAddress getAddress(JSONObject object) {
		ContactAddressBrowserImpl ca = new ContactAddressBrowserImpl();
		if (object == null) {
			return ca;
		}

		String formatted = getFieldAsString(object.get(ADDRESS_FORMATTED));
		String streedAddress = getFieldAsString(object.get(ADDRESS_STREET_ADDRESS));
		String locality = getFieldAsString(object.get(ADDRESS_LOCALITY));
		String region = getFieldAsString(object.get(ADDRESS_REGION));
		String postalCode = getFieldAsString(object.get(ADDRESS_POSTAL_CODE));
		String country = getFieldAsString(object.get(ADDRESS_COUNTRY));
		String type = getFieldAsString(object.get(ADDRESS_TYPE));
		boolean pref = getFieldAsBoolean(object.get(ADDRESS_PREF));

		ca.setFormatted(formatted);
		ca.setStreetAddress(streedAddress);
		ca.setLocality(locality);
		ca.setRegion(region);
		ca.setPostalCode(postalCode);
		ca.setCountry(country);
		ca.setType(type);
		ca.setPref(pref);

		return ca;
	}

	private static ContactName getName(JSONObject object) {
		ContactNameBrowserImpl name = new ContactNameBrowserImpl();
		if (object == null) {
			return name;
		}

		String formatted = getFieldAsString(object.get(NAME_FORMATTED));
		String familyName = getFieldAsString(object.get(NAME_FAMILY_NAME));
		String givenName = getFieldAsString(object.get(NAME_GIVEN_NAME));
		String middleName = getFieldAsString(object.get(NAME_MIDDLE_NAME));
		String prefix = getFieldAsString(object.get(NAME_PREFIX));
		String suffix = getFieldAsString(object.get(NAME_SUFFIX));

		name.setFormatted(formatted);
		name.setFamilyName(familyName);
		name.setGivenName(givenName);
		name.setMiddleName(middleName);
		name.setHonoricfPrefix(prefix);
		name.setHonoricfSuffix(suffix);

		return name;
	}

	private static LightArray<ContactField> getContactFieldsForArray(JSONArray jsonArray) {
		LightArray<ContactField> array = CollectionFactory.constructArray();
		if (jsonArray == null) {
			return array;
		}

		for (int i = 0; i < jsonArray.size(); i++) {
			ContactFieldBrowserImpl cf = new ContactFieldBrowserImpl();
			JSONObject object = jsonArray.get(i).isObject();

			String type = getFieldAsString(object.get(CONTACT_FIELD_TYPE));
			String value = getFieldAsString(object.get(CONTACT_FIELD_VALUE));
			boolean pref = getFieldAsBoolean(object.get(CONTACT_FIELD_PREF));
			cf.setPref(pref);
			cf.setType(type);
			cf.setValue(value);
			array.push(cf);
		}

		return array;
	}

	private static boolean getFieldAsBoolean(JSONValue field) {
		return field.isBoolean() != null ? field.isBoolean().booleanValue() : false;
	}

	private static String getFieldAsString(JSONValue field) {
		return (field != null && field.isString() != null) ? field.isString().stringValue() : "";
	}

	public JSONObject serializeContact() {
		JSONObject root = new JSONObject();

		// simple values
		root.put(FIELD_ID, getAsJSONString(this.getId()));
		root.put(CONTACT_DISPLAY_NAME, getAsJSONString(this.getDisplayName()));
		root.put(CONTACT_NICK_NAME, getAsJSONString(this.getNickName()));
		root.put(CONTACT_REVISION, getAsJSONString(this.getRevision()));
		root.put(CONTACT_GENDER, getAsJSONString(this.getGender()));
		root.put(CONTACT_TIME_ZONE, getAsJSONString(this.getTimeZone()));

		if (this.getBirthDay() != null) {
			double value = this.getBirthDay().getTime();
			root.put(CONTACT_BIRTHDAY, new JSONNumber(value));
		}

		root.put(CONTACT_PHONE_NUMBERS, toJSONArray(this.getPhoneNumbers()));

		root.put(CONTACT_EMAILS, toJSONArray(this.getEmails()));
		root.put(CONTACT_IMS, toJSONArray(this.getIms()));
		root.put(CONTACT_PHOTOS, toJSONArray(this.getPhotos()));
		root.put(CONTACT_CATEGORIES, toJSONArray(this.getCategories()));
		root.put(CONTACT_URLS, toJSONArray(this.getUrls()));

		root.put(CONTACT_NAME, createContact(this.getName()));

		root.put(CONTACT_ADDRESSES, createAddresses(this.getContactAddresses()));

		root.put(CONTACT_ORGANISATION, createOrganisation(this.getOrganisations()));

		return root;
	}

	private JSONArray createOrganisation(LightArray<ContactOrganisation> organisations) {
		JSONArray jsonArray = new JSONArray();

		for (int i = 0; i < organisations.length(); i++) {
			ContactOrganisation co = organisations.get(i);
			JSONObject orga = new JSONObject();

			orga.put(ORGA_NAME, getAsJSONString(co.getName()));
			orga.put(ORGA_DEPARTMENT, getAsJSONString(co.getDepartment()));
			orga.put(ORGA_TITLE, getAsJSONString(co.getTitle()));

			jsonArray.set(i, orga);
		}

		return jsonArray;
	}

	private JSONArray createAddresses(LightArray<ContactAddress> addresses) {
		JSONArray jsonArray = new JSONArray();

		for (int i = 0; i < addresses.length(); i++) {
			ContactAddress contactAddress = addresses.get(i);
			JSONObject address = new JSONObject();
			address.put(ADDRESS_FORMATTED, getAsJSONString(contactAddress.getFormatted()));
			address.put(ADDRESS_STREET_ADDRESS, getAsJSONString(contactAddress.getStreetAddress()));
			address.put(ADDRESS_LOCALITY, getAsJSONString(contactAddress.getLocality()));
			address.put(ADDRESS_REGION, getAsJSONString(contactAddress.getRegion()));
			address.put(ADDRESS_POSTAL_CODE, getAsJSONString(contactAddress.getPostalCode()));
			address.put(ADDRESS_COUNTRY, getAsJSONString(contactAddress.getCountry()));
			address.put(ADDRESS_TYPE, getAsJSONString(contactAddress.getType()));
			address.put(ADDRESS_PREF, JSONBoolean.getInstance(contactAddress.isPref()));
			jsonArray.set(i, address);
		}

		return jsonArray;
	}

	private JSONObject createContact(ContactName contactName) {
		JSONObject jsonName = new JSONObject();

		jsonName.put(NAME_FORMATTED, getAsJSONString(contactName.getFormatted()));
		jsonName.put(NAME_FAMILY_NAME, getAsJSONString(contactName.getFamilyName()));
		jsonName.put(NAME_GIVEN_NAME, getAsJSONString(contactName.getGivenName()));
		jsonName.put(NAME_MIDDLE_NAME, getAsJSONString(contactName.getMiddleName()));
		jsonName.put(NAME_PREFIX, getAsJSONString(contactName.getHonoricPrefix()));
		jsonName.put(NAME_SUFFIX, getAsJSONString(contactName.getHonoricSuffix()));

		return jsonName;
	}

	private JSONValue toJSONArray(LightArray<ContactField> contactField) {
		JSONArray jsonArray = new JSONArray();
		for (int i = 0; i < contactField.length(); i++) {
			ContactField field = contactField.get(i);
			JSONObject jsonField = new JSONObject();
			jsonField.put(CONTACT_FIELD_TYPE, getAsJSONString(field.getType()));
			jsonField.put(CONTACT_FIELD_VALUE, getAsJSONString(field.getValue()));

			jsonField.put(CONTACT_FIELD_PREF, JSONBoolean.getInstance(field.isPref()));
			jsonArray.set(i, jsonField);
		}

		return jsonArray;
	}

	private static JSONValue getAsJSONString(String id) {
		if (id == null) {
			return JSONNull.getInstance();
		}

		return new JSONString(id);
	}

}

Commits for litesoft/trunk/GWT_Sandbox/SampleHello/src/com/googlecode/gwtphonegap/client/contacts/browser/ContactBrowserImpl.java

Diff revisions: vs.
Revision Author Commited Message
666 GeorgeS picture GeorgeS Sun 13 May, 2012 18:11:48 +0000