Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.vigglet.oei.standardinspection; import com.vigglet.oei.service.Service; import com.vigglet.oei.service.ServiceUtil; import com.vigglet.oei.servicerow.Servicerow; import com.vigglet.oei.servicerow.ServicerowUtil; import com.vigglet.oei.standardinspectionrow.Standardinspectionrow; import com.vigglet.oei.standardinspectionrow.StandardinspectionrowUtil; import com.vigglet.servlet.PostJsonServlet; import com.vigglet.oei.user.User; import com.vigglet.servlet.BaseServlet; import com.vigglet.util.JsonUtil; import java.io.IOException; import java.io.StringWriter; import java.util.Date; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; /** * * @author vikn */ @WebServlet("/api/standardinspection/copy-to-service") public class CopyStandardinspectionToServiceServlet extends BaseServlet { @Override protected void preProcessRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(req.getInputStream(), writer, "UTF-8"); String theString = writer.toString(); User user = getUser(req); Standardinspection model = JsonUtil.read(theString, Standardinspection.class); if (model != null) { model.setCompany(user.getCompany()); model = PostStandardinspectionServlet.update(model); Service service = new Service(); service.setCompany(model.getCompany()); service.setDate(new Date().getTime()); service.setDescription(model.getDescription()); service.setNote(model.getName()); service = ServiceUtil.getInstance().insertOrUpdate(service); int i = 0; for (Standardinspectionrow standardinspectionrow : model.getRows()) { Servicerow servicerow = new Servicerow(); servicerow.setMaterial(standardinspectionrow.getMaterial()); servicerow.setName(standardinspectionrow.getComment()); servicerow.setQuantity(standardinspectionrow.getQuantity()); servicerow.setType(standardinspectionrow.getType()); servicerow.setListorder(i++); servicerow.setService(service.getId()); ServicerowUtil.getInstance().insertOrUpdate(servicerow); } JsonUtil.write(resp.getOutputStream(), ServiceUtil.getInstance().findById(service.getId())); } else { Logger.getLogger(PostJsonServlet.class.getName()).log(Level.WARNING, "Could not read json!", theString); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } }