|
package com.greenwave.load.simulator.web.resources; |
|
|
|
import java.net.SocketException; |
|
import java.util.Calendar; |
|
import java.util.Collections; |
|
import java.util.Comparator; |
|
import java.util.List; |
|
import java.util.Random; |
|
|
|
import javax.ws.rs.Consumes; |
|
import javax.ws.rs.GET; |
|
import javax.ws.rs.Path; |
|
import javax.ws.rs.Produces; |
|
import javax.ws.rs.QueryParam; |
|
import javax.ws.rs.core.MediaType; |
|
|
|
import mint.rest.common.util.MediaTypeEx; |
|
|
|
import org.fluttercode.datafactory.impl.DataFactory; |
|
import org.springframework.stereotype.Component; |
|
|
|
import com.google.common.collect.Lists; |
|
import com.greenwave.load.simulator.web.resources.dto.EventDTO; |
|
import com.greenwave.load.simulator.web.resources.dto.EventsList; |
|
|
|
@Component |
|
@Path("/api/events") |
|
@Consumes(MediaType.APPLICATION_JSON) |
|
@Produces(MediaTypeEx.APPLICATION_JSON_UTF_8) |
|
public class EventResource { |
|
|
|
private List<String> statuses = Lists.newArrayList(); |
|
private Random random = new Random();; |
|
|
|
public EventResource() { |
|
statuses.add("active"); |
|
statuses.add("finished"); |
|
} |
|
|
|
@GET |
|
/** |
|
* Regarding offset, pageSize, showActiveEventsOnly queryParams look at ui side, |
|
* see https://github.com/backbone-paginator/backbone-pageable for more details |
|
*/ |
|
public EventsList list(@QueryParam("offset") String offset, @QueryParam("pageSize") String pageSize, |
|
@QueryParam("showActiveEventsOnly") String showActiveEventsOnly) |
|
throws SocketException { |
|
|
|
Boolean showActiveEventsOnlyParsed = Boolean.valueOf(showActiveEventsOnly); |
|
List<EventDTO> result = Lists.newArrayList(); |
|
DataFactory df = new DataFactory(); |
|
|
|
for (int i = 0; i < (showActiveEventsOnlyParsed ? 25 : 50); i++) { |
|
EventDTO dto = new EventDTO(); |
|
|
|
dto.setName(df.getRandomWord() + " event"); |
|
String address = df.getAddress() + ", " + df.getCity() + ", " + df.getNumberText(5); |
|
String business = df.getBusinessName(); |
|
dto.setDescription(business + " located at " + address); |
|
dto.setId(random.nextInt(100)); |
|
dto.setStatus(showActiveEventsOnlyParsed ? "active" : statuses.get(random.nextInt(2))); |
|
|
|
Calendar calendar = Calendar.getInstance(); // this would default to now |
|
calendar.add(Calendar.DAY_OF_MONTH, -30); |
|
|
|
dto.setStart(df.getDate(calendar.getTime(), 1, 15)); |
|
dto.setStop(df.getDate(calendar.getTime(), 15, 30)); |
|
result.add(dto); |
|
} |
|
|
|
Collections.sort(result, new Comparator<EventDTO>() { |
|
@Override |
|
public int compare(EventDTO o1, EventDTO o2) { |
|
int result = o1.getStatus().compareTo(o2.getStatus()); |
|
if (result == 0) { |
|
return o2.getStart().compareTo(o1.getStart()); |
|
} |
|
return result; |
|
} |
|
}); |
|
|
|
final int totalCount = result.size(); |
|
List<EventDTO> paginatedResult; |
|
|
|
final Integer pageSizeParsed = Integer.valueOf(pageSize); |
|
final Integer offsetParsed = Integer.valueOf(offset); |
|
final int boundPageSize = offsetParsed + pageSizeParsed; |
|
|
|
if (result.size() >= boundPageSize) { |
|
paginatedResult = result.subList(offsetParsed, pageSizeParsed + offsetParsed); |
|
} else { |
|
paginatedResult = result.subList(offsetParsed, result.size()); |
|
} |
|
|
|
return new EventsList(paginatedResult, totalCount); |
|
} |
|
|
|
} |