Java tutorial
/* * Copyright (C) 2015 Ron Leisti * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.ronleisti.workdays.webservice; import java.sql.Date; import java.util.List; import java.util.Map; import javax.ejb.EJB; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Function; import com.google.common.collect.Lists; import com.ronleisti.workdays.model.UnitOfWork; import com.ronleisti.workdays.service.UnitsOfWorkService; @Path("/resources/unitsofwork/{day}") public class UnitsOfWorkWebService { private static final Logger log = LoggerFactory.getLogger(UnitsOfWorkWebService.class); @EJB private UnitsOfWorkService service; @GET @Produces(MediaType.APPLICATION_JSON) public Response getUnitsOfWork(@PathParam("day") String day) { final List<Map<String, Object>> results = service.queryByDay(Date.valueOf(day)); return Response.ok(results, MediaType.APPLICATION_JSON).build(); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response postUnitsOfWork(@PathParam("day") String day, Object data) { final Date dayValue = Date.valueOf(day); if (data instanceof List<?>) { @SuppressWarnings("unchecked") final List<Object> items = (List<Object>) data; final List<UnitOfWork> parsedItems = Lists.transform(items, new Function<Object, UnitOfWork>() { @SuppressWarnings("unchecked") @Override public UnitOfWork apply(final Object rawItem) { return UnitOfWork.fromMap(dayValue, (Map<String, Object>) rawItem); } }); service.updateDay(dayValue, parsedItems); } return getUnitsOfWork(day); } }