This repository was archived by the owner on May 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathContactsSnippets.java
190 lines (171 loc) · 6.79 KB
/
ContactsSnippets.java
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
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See full license at the bottom of this file.
*/
package com.microsoft.office365.snippetapp.Snippets;
import com.microsoft.outlookservices.Contact;
import com.microsoft.outlookservices.EmailAddress;
import com.microsoft.outlookservices.odata.OutlookClient;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class ContactsSnippets {
private final OutlookClient mOutlookClient;
public ContactsSnippets(OutlookClient mailClient) {
mOutlookClient = mailClient;
}
/**
* Return a list of contacts and ordered by the
* contact's surname field.
*
* @return List. A list of the com.microsoft.outlookservices.Contact objects
*/
public List<Contact> getContacts(int pageSize) throws ExecutionException, InterruptedException {
return mOutlookClient
.getMe()
.getContacts()
.top(pageSize)
.skip(1)
.select("displayName")
.orderBy("surName")
.read().get();
}
/**
* Creates a new contact
*
* @param emailAddressString The email address of the contact to be added
* @param businessPhoneString The business telephone number of the new contact
* @param firstNameString The first name of the new contact
* @param homePhoneString The home telephone number of the new contact
* @param lastNameString The surname of the new contact
* @return String. The id of the new contact
*/
public String createContact(
String emailAddressString,
String businessPhoneString,
String homePhoneString,
String firstNameString,
String lastNameString) throws ExecutionException, InterruptedException {
Contact contact = new Contact();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress(emailAddressString);
List<EmailAddress> emailAddressList = new ArrayList<>();
emailAddressList.add(emailAddress);
contact.setEmailAddresses(emailAddressList);
List<String> businessPhones = new ArrayList<>();
businessPhones.add(businessPhoneString);
List<String> homePhones = new ArrayList<>();
homePhones.add(homePhoneString);
contact.setBusinessPhones(businessPhones);
contact.setHomePhones(homePhones);
contact.setGivenName(firstNameString);
contact.setSurname(lastNameString);
return mOutlookClient
.getMe()
.getContacts()
.select("ID")
.add(contact).get().getId();
}
/**
* Gets a contact by the contact Id
*
* @return Contact. The contact corresponding to the id
*/
public Contact getAContact(String id) throws ExecutionException, InterruptedException {
return mOutlookClient
.getMe()
.getContacts()
.getById(id)
.select("surname")
.read().get();
}
/**
* Updates the first and surname of a contact
*
* @param contactId The id of the contact to be updated
* @param firstNameString The updated first name of the new contact
* @param lastNameString The updated surname of the new contact
*/
public void updateContact(String contactId, String firstNameString, String lastNameString)
throws ExecutionException, InterruptedException {
//Get the contact to update
Contact updateContact = mOutlookClient
.getMe()
.getContacts()
.getById(contactId)
.select("ID")
.read()
.get();
//Update the contact first and last name
updateContact.setSurname(lastNameString);
updateContact.setGivenName(firstNameString);
//push the updated contact to the server
mOutlookClient
.getMe()
.getContacts()
.getById(contactId)
.update(updateContact).get();
}
/**
* Deletes a contact
*
* @param id The id of the contact to be deleted
* @throws ExecutionException
* @throws InterruptedException
*/
public void deleteContact(String id) throws ExecutionException, InterruptedException {
mOutlookClient
.getMe()
.getContacts()
.getById(id)
.addHeader("If-Match", "*")
.delete()
.get();
}
/**
* Runs a filtered query to find all contacts with the given surname. This snippet can be
* modified to run any filtered query. For a complete list of contact properties that
* can be filtered, see https://msdn.microsoft.com/office/office365/APi/complex-types-for-mail-contacts-calendar#RESTAPIResourcesEvent
*
* @param surname The surname to match
* @return A list of contacts matching the given surname
* @throws ExecutionException
* @throws InterruptedException
*/
public List<Contact> getContactsWithSurname(String surname) throws ExecutionException, InterruptedException {
return mOutlookClient
.getMe()
.getContacts()
.select("surname")
.filter("surname eq '" + surname + "'")
.read()
.get();
}
}
// *********************************************************
//
// O365-Android-Snippets, https://github.com/OfficeDev/O365-Android-Snippets
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// *********************************************************