forked from smart-it/smart-user
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OrganizationsResource.java
438 lines (359 loc) · 15.3 KB
/
OrganizationsResource.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
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.smartitengineering.user.ws.resources;
/**
*
* @author russel
*/
import com.smartitengineering.user.service.Services;
import com.smartitengineering.user.domain.Address;
import com.smartitengineering.user.domain.Organization;
import com.smartitengineering.util.rest.atom.server.AbstractResource;
import com.sun.jersey.api.view.Viewable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriBuilder;
import org.apache.abdera.Abdera;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
import org.apache.abdera.model.Link;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
@Path("/orgs")
public class OrganizationsResource extends AbstractResource {
static final Method ORGANIZATION_AFTER_SHORTNAME_METHOD;
static final Method ORGANIZATION_BEFORE_SHORTNAME_METHOD;
@Context
private HttpServletRequest servletRequest;
static {
try {
ORGANIZATION_BEFORE_SHORTNAME_METHOD = OrganizationsResource.class.getMethod("getBefore", String.class);
}
catch (Exception ex) {
throw new InstantiationError();
}
try {
ORGANIZATION_AFTER_SHORTNAME_METHOD = OrganizationsResource.class.getMethod("getAfter", String.class);
}
catch (Exception ex) {
throw new InstantiationError();
}
}
// @QueryParam("name")
// private String nameLike;
@QueryParam("shortname")
private String uniqueShortName;
@DefaultValue("10")
@QueryParam("count")
private Integer count;
@GET
@Produces(MediaType.APPLICATION_ATOM_XML)
@Path("/before/{beforeShortName}")
public Response getBefore(@PathParam("beforeShortName") String beforeShortName) {
return get(beforeShortName, true);
}
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/before/{beforeShortName}")
public Response getBeforeHtml(@PathParam("beforeShortName") String beforeShortName) {
ResponseBuilder responseBuilder = Response.ok();
if (count == null) {
count = 10;
}
Collection<Organization> organizations = Services.getInstance().getOrganizationService().getOrganizations(
null, beforeShortName, true, count);
servletRequest.setAttribute("templateContent",
"/com/smartitengineering/user/ws/resources/OrganizationsResource/organizationList.jsp");
Viewable view = new Viewable("/template/template.jsp", organizations);
responseBuilder.entity(view);
return responseBuilder.build();
}
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/before/{beforeShortName}/frags")
public Response getBeforeHtmlFrags(@PathParam("beforeShortName") String beforeShortName) {
ResponseBuilder responseBuilder = Response.ok();
if (count == null) {
count = 10;
}
Collection<Organization> organizations = Services.getInstance().getOrganizationService().getOrganizations(
null, beforeShortName, true, count);
Viewable view = new Viewable("organizationFrags.jsp", organizations);
responseBuilder.entity(view);
return responseBuilder.build();
}
@GET
@Produces(MediaType.APPLICATION_ATOM_XML)
@Path("/after/{afterShortName}")
public Response getAfter(@PathParam("afterShortName") String afterShortName) {
return get(afterShortName, false);
}
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/after/{afterShortName}")
public Response getAfterHtml(@PathParam("afterShortName") String afterShortName) {
ResponseBuilder responseBuilder = Response.ok();
if (count == null) {
count = 10;
}
Collection<Organization> organizations = Services.getInstance().getOrganizationService().getOrganizations(
null, afterShortName, false, count);
servletRequest.setAttribute("templateContent",
"/com/smartitengineering/user/ws/resources/OrganizationsResource/organizationList.jsp");
Viewable view = new Viewable("/template/template.jsp", organizations);
responseBuilder.entity(view);
return responseBuilder.build();
}
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/after/{afterShortName}/frags")
public Response getAfterHtmlFrags(@PathParam("afterShortName") String afterShortName) {
ResponseBuilder responseBuilder = Response.ok();
if (count == null) {
count = 10;
}
Collection<Organization> organizations = Services.getInstance().getOrganizationService().getOrganizations(
null, afterShortName, false, count);
Viewable view = new Viewable("organizationFrags.jsp", organizations);
responseBuilder.entity(view);
return responseBuilder.build();
}
@GET
@Produces(MediaType.APPLICATION_ATOM_XML)
public Response get() {
return get(null, true);
}
@GET
@Produces(MediaType.TEXT_HTML)
public Response getHtml() {
ResponseBuilder responseBuilder = Response.ok();
if (count == null) {
count = 10;
}
Collection<Organization> organizations = Services.getInstance().getOrganizationService().getOrganizations(
uniqueShortName, uniqueShortName, false, count);
servletRequest.setAttribute("templateHeadContent",
"/com/smartitengineering/user/ws/resources/OrganizationsResource/OrganizationHeader.jsp");
servletRequest.setAttribute("templateContent",
"/com/smartitengineering/user/ws/resources/OrganizationsResource/organizationList.jsp");
Viewable view = new Viewable("/template/template.jsp", organizations);
responseBuilder.entity(view);
return responseBuilder.build();
}
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/frags")
public Response getHtmlFrags() {
ResponseBuilder responseBuilder = Response.ok();
if (count == null) {
count = 10;
}
Collection<Organization> organizations = Services.getInstance().getOrganizationService().getOrganizations(
uniqueShortName, uniqueShortName, false, count);
Viewable view = new Viewable("organizationFrags.jsp", organizations);
responseBuilder.entity(view);
return responseBuilder.build();
}
// @GET
// @Produces(MediaType.APPLICATION_ATOM_XML)
public Response get(String organizationName, boolean isBefore) {
if (count == null) {
count = 10;
}
ResponseBuilder responseBuilder = Response.ok();
// create a new atom feed
Feed atomFeed = Abdera.getNewFactory().newFeed();
// create a link to parent resource, in this case now it is linked to root resource
Link parentResourceLink = Abdera.getNewFactory().newLink();
parentResourceLink.setHref(UriBuilder.fromResource(RootResource.class).build().toString());
parentResourceLink.setRel("root");
atomFeed.addLink(parentResourceLink);
// get the organizations accoring to the query
//Collection<Organization> organizations = Services.getInstance().getOrganizationService().getAllOrganization();
// Collection<Organization> organizations = Services.getInstance().getOrganizationService().getOrganizations(
// organizationName, uniqueShortName, isBefore, count);
Collection<Organization> organizations = Services.getInstance().getOrganizationService().getOrganizations(
"", organizationName, isBefore, count);
// for testing purpose we manually add organization to the list.
// List<Organization> serviceOrganization = new ArrayList<Organization>();
// serviceOrganization.add(new Organization("Sitel", "1"));
// serviceOrganization.add(new Organization("mehmood equity", "2"));
// Collection<Organization> organizations = serviceOrganization;
if (organizations != null && !organizations.isEmpty()) {
MultivaluedMap<String, String> queryParam = getUriInfo().getQueryParameters();
List<Organization> organizationList = new ArrayList<Organization>(organizations);
// uri builder for next and previous organizations according to count
final UriBuilder nextUri = getRelativeURIBuilder().path(OrganizationsResource.class).path(
ORGANIZATION_AFTER_SHORTNAME_METHOD);
final UriBuilder previousUri = getRelativeURIBuilder().path(OrganizationsResource.class).path(
ORGANIZATION_BEFORE_SHORTNAME_METHOD);
// link to the next organizations based on count
Link nextLink = getAbderaFactory().newLink();
nextLink.setRel(Link.REL_NEXT);
Organization lastOrganization = organizationList.get(organizationList.size() - 1);
for (String key : queryParam.keySet()) {
final Object[] values = queryParam.get(key).toArray();
nextUri.queryParam(key, values);
previousUri.queryParam(key, values);
}
nextLink.setHref(nextUri.build(lastOrganization.getUniqueShortName()).toString());
//nextLink.setHref(UriBuilder.fromResource(OrganizationsResource.class).build(lastOrganization.getUniqueShortName()).toString());
atomFeed.addLink(nextLink);
/* link to the previous organizations based on count */
Link prevLink = getAbderaFactory().newLink();
prevLink.setRel(Link.REL_PREVIOUS);
Organization firstOrganization = organizationList.get(0);
prevLink.setHref(previousUri.build(firstOrganization.getUniqueShortName()).toString());
//prevLink.setHref(nameLike)
atomFeed.addLink(prevLink);
// add entry of individual organization
for (Organization organization : organizations) {
Entry organizationEntry = getAbderaFactory().newEntry();
organizationEntry.setId(organization.getUniqueShortName().toString());
organizationEntry.setTitle(organization.getName());
organizationEntry.setSummary(organization.getName());
organizationEntry.setUpdated(organization.getLastModifiedDate());
/* setting link to the individual organization resource*/
Link organizationLink = getAbderaFactory().newLink();
organizationLink.setHref(getRelativeURIBuilder().path(OrganizationResource.class).build(organization.
getUniqueShortName()).toString());
organizationLink.setRel(Link.REL_ALTERNATE);
organizationLink.setMimeType(MediaType.APPLICATION_ATOM_XML);
organizationEntry.addLink(organizationLink);
atomFeed.addEntry(organizationEntry);
}
}
responseBuilder.entity(atomFeed);
return responseBuilder.build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response post(Organization organization) {
ResponseBuilder responseBuilder;
try {
//Services.getInstance().getOrganizationService().populateAuthor(organization);
Services.getInstance().getOrganizationService().save(organization);
responseBuilder = Response.status(Response.Status.CREATED);
responseBuilder.location(getAbsoluteURIBuilder().path(OrganizationResource.class).build(organization.
getUniqueShortName()));
}
catch (Exception ex) {
responseBuilder = Response.status(Response.Status.INTERNAL_SERVER_ERROR);
}
return responseBuilder.build();
}
private Organization getObjectFromContent(String message) {
Map<String, String> keyValueMap = new HashMap<String, String>();
String[] keyValuePairs = message.split("&");
for (int i = 0; i < keyValuePairs.length; i++) {
String[] keyValuePair = keyValuePairs[i].split("=");
keyValueMap.put(keyValuePair[0], keyValuePair[1]);
}
Organization newOrganization = new Organization();
if (keyValueMap.get("id") != null) {
newOrganization.setId(keyValueMap.get("id"));
}
if (keyValueMap.get("name") != null) {
newOrganization.setName(keyValueMap.get("name"));
}
if (keyValueMap.get("uniqueShortName") != null) {
newOrganization.setUniqueShortName(keyValueMap.get("uniqueShortName"));
}
Address address = new Address();
if (keyValueMap.get("city") != null) {
address.setCity(keyValueMap.get("city"));
}
if (keyValueMap.get("country") != null) {
address.setCountry(keyValueMap.get("country"));
}
if (keyValueMap.get("state") != null) {
address.setState(keyValueMap.get("state"));
}
if (keyValueMap.get("zip") != null) {
address.setZip(keyValueMap.get("zip"));
}
newOrganization.setAddress(address);
return newOrganization;
}
@POST
public Response post(@HeaderParam("Content-type") String contentType, String message) {
ResponseBuilder responseBuilder = Response.status(Status.SERVICE_UNAVAILABLE);
if (StringUtils.isBlank(message)) {
responseBuilder = Response.status(Status.BAD_REQUEST);
responseBuilder.build();
}
final boolean isHtmlPost;
if (StringUtils.isBlank(contentType)) {
contentType = MediaType.APPLICATION_OCTET_STREAM;
isHtmlPost = false;
}
else if (contentType.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
contentType = MediaType.APPLICATION_OCTET_STREAM;
isHtmlPost = true;
try {
//Will search for the first '=' if not found will take the whole string
final int startIndex = 0;//message.indexOf("=") + 1;
//Consider the first '=' as the start of a value point and take rest as value
final String realMsg = message.substring(startIndex);
//Decode the message to ignore the form encodings and make them human readable
message = URLDecoder.decode(realMsg, "UTF-8");
}
catch (UnsupportedEncodingException ex) {
}
}
else {
isHtmlPost = false;
}
if (isHtmlPost) {
Organization newOrganization = new Organization();
try {
newOrganization = getObjectFromContent(message);
Services.getInstance().getOrganizationService().save(newOrganization);
responseBuilder.status(Status.SEE_OTHER);
responseBuilder.location(getAbsoluteURIBuilder().path(OrganizationResource.class).build(newOrganization.
getUniqueShortName()));
}
catch (Exception ex) {
servletRequest.setAttribute("error", ex.getMessage());
servletRequest.setAttribute("errorOrganization", newOrganization);
Collection<Organization> organizations = Services.getInstance().getOrganizationService().getOrganizations(
uniqueShortName, uniqueShortName, false, count);
servletRequest.setAttribute("templateHeadContent",
"/com/smartitengineering/user/ws/resources/OrganizationsResource/OrganizationHeader.jsp");
servletRequest.setAttribute("templateContent",
"/com/smartitengineering/user/ws/resources/OrganizationsResource/organizationList.jsp");
Viewable view = new Viewable("/template/template.jsp", organizations);
responseBuilder.entity(view);
}
}
return responseBuilder.build();
}
@Override
protected String getAuthor() {
return "Smart User";
}
}