This repository was archived by the owner on Jul 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactsWebservice.java
More file actions
180 lines (152 loc) · 6.91 KB
/
Copy pathContactsWebservice.java
File metadata and controls
180 lines (152 loc) · 6.91 KB
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
package com.test.webservice.service;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.appengine.repackaged.com.google.api.client.util.Strings;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.cmd.Query;
import com.test.webservice.model.Company;
import com.test.webservice.model.ContactItem;
import com.test.webservice.model.Person;
import com.test.webservice.model.Simple;
import com.test.webservice.model.search.BaseSearchCriteria;
import com.test.webservice.model.search.CompanySearchCriteria;
import com.test.webservice.model.search.PersonSearchCriteria;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.logging.Logger;
import static com.test.webservice.service.OfyService.ofy;
/**
* Contacts GoogleCloudEndpoint service for to add/list Persons and Companies
*
* @author Alexey Fedyunin (alexey@cranfan.ru)
*/
@Api(name = "contacts", version = "v1", description = "Contacts Web Service for add/list Persons and Companies")
public class ContactsWebservice {
/* Max amount of records for fetch from datastore per request. */
public static final int LIMIT = 1000;
protected final Logger log = Logger.getLogger(this.getClass().getName());
@ApiMethod(name = "addPerson", httpMethod = "POST")
public Person addPerson(Person person) {
log.info("Add person to datastore: " + person.toString());
ofy().save().entity(person).now();
return person;
}
@ApiMethod(name = "addCompany", httpMethod = "POST")
public Company addCompany(Company company) {
log.info("Add company to datastore: " + company.toString());
ofy().save().entity(company).now();
return company;
}
@ApiMethod(name = "listPerson", httpMethod = "POST")
public List<Person> listPerson(PersonSearchCriteria searchCriteria) {
log.info("Retrieve list of persons for search criteria: " + searchCriteria.toString());
// initial query build
Query query = ofy().load().type(Person.class).limit(LIMIT);
if (!Strings.isNullOrEmpty(searchCriteria.getFirstname())) {
query = startsWith(query, "firstName", searchCriteria.getFirstname());
}
if (!Strings.isNullOrEmpty(searchCriteria.getLastName())) {
query = startsWith(query, "lastName", searchCriteria.getLastName());
}
query = getTagsQuery(searchCriteria, query);
return query.list();
}
@ApiMethod(name = "listCompany", httpMethod = "POST")
public List<Company> listCompany(CompanySearchCriteria searchCriteria) {
log.info("Retrieve list of companies for search criteria: " + searchCriteria.toString());
// initial query build
Query query = ofy().load().type(Company.class).limit(LIMIT);
if (!Strings.isNullOrEmpty(searchCriteria.getCompanyName())) {
query = startsWith(query, "companyName", searchCriteria.getCompanyName());
}
query = getTagsQuery(searchCriteria, query);
return query.list();
}
/**
* Prepares initialQuery for filter by tags.
*
* @param searchCriteria exists criteria,
* @param initialQuery init query.
* @return initialQuery with filleting by tags.
*/
private Query getTagsQuery(BaseSearchCriteria searchCriteria, Query initialQuery) {
if (searchCriteria.getTags() != null && !searchCriteria.getTags().isEmpty()) {
initialQuery = initialQuery.filter("tags in", searchCriteria.getTags());
}
return initialQuery;
}
/**
* Prepares query for filtering string field (analog of sql LIKE % %)
*
* @param initialQuery init query,
* @param fieldName name of field for filtering,
* @param value filtering value.
* @return query with filtering by string field expression.
*/
private Query startsWith(Query initialQuery, String fieldName, String value) {
return initialQuery.filter(fieldName + " >=", value).filter(fieldName + " <", value + "\uFFFD");
}
@ApiMethod(name = "generateData", httpMethod = "GET")
public Simple generateData() {
log.info("Generate test data");
for (int i = 0; i < 100; i++) {
Company company = new Company();
company.setCompanyName(UUID.randomUUID().toString());
company.setAddress(UUID.randomUUID().toString());
company.setCode(UUID.randomUUID().toString());
company.setCountry(UUID.randomUUID().toString());
company.setLocal(UUID.randomUUID().toString());
company.setPostalCode(UUID.randomUUID().toString());
company.setEmailList(getRandomContactItems());
company.setLinkList(getRandomContactItems());
company.setPhoneNumberList(getRandomContactItems());
company.setTags(getRandomTagList());
ofy().save().entity(company).now();
Person Person = new Person();
Person.setFirstName(UUID.randomUUID().toString());
Person.setLastName(UUID.randomUUID().toString());
Person.setAddress(UUID.randomUUID().toString());
Person.setCode(UUID.randomUUID().toString());
Person.setCountry(UUID.randomUUID().toString());
Person.setLocal(UUID.randomUUID().toString());
Person.setPostalCode(UUID.randomUUID().toString());
Person.setEmailList(getRandomContactItems());
Person.setLinkList(getRandomContactItems());
Person.setPhoneNumberList(getRandomContactItems());
Person.setTags(getRandomTagList());
ofy().save().entity(Person).now();
}
return new Simple("Generated test data");
}
@ApiMethod(name = "clearData", httpMethod = "GET")
public Simple clearData() {
Iterable<Key<Company>> allKeys = ofy().load().type(Company.class).keys();
ofy().delete().keys(allKeys);
Iterable<Key<Person>> allKeys2 = ofy().load().type(Person.class).keys();
ofy().delete().keys(allKeys2);
return new Simple("Removed all data");
}
private List<String> getRandomTagList() {
Random random = new Random();
int size = random.nextInt(10);
List<String> randomTags = new ArrayList<>(size);
for (int j = 0; j < size; j++) {
randomTags.add(String.valueOf(random.nextInt(100)));
}
return randomTags;
}
private List<ContactItem> getRandomContactItems() {
Random random = new Random();
int size = random.nextInt(10);
List<ContactItem> randomContactItems = new ArrayList<>(size);
for (int j = 0; j < size; j++) {
ContactItem contactItem = new ContactItem();
contactItem.setType(UUID.randomUUID().toString());
contactItem.setValue(UUID.randomUUID().toString());
randomContactItems.add(contactItem);
}
return randomContactItems;
}
}