public class InvoiceServlet extends ReflexServlet {
// MODELのPackage名を指定
private static final String MODEL_PACKAGE = "jp.reflexworks.invoice.model";
public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
boolean toXml = true;
if (req.getParameter("xml") != null) {
toXml = false; // toJSON
}
try {
// クライアントから受け取ったXML(JSON)をオブジェクトに変換
Invoice
invoice = (Invoice) getEntity(req, MODEL_PACKAGE);
// Update実行
JdoUtils jdoUtils = new JdoUtils();
jdoUtils.update(invoice);
Status
status = new Status();
status.code = HttpStatus.SC_OK;
status.message = "Updated.";
doResponse(resp, status, toXml, MODEL_PACKAGE);
}
// 楽観的ロック失敗時の処理
catch (ConcurrentModificationException e) {
Status
status = new Status();
status.code = HttpStatus.SC_CONFLICT;
status.message = "Optimistic locking failed.";
try {
doResponse(resp, status, toXml, MODEL_PACKAGE);
}
catch (Exception ee) {
ee.printStackTrace();
}
}
// 更新エラー時の処理
catch (JDOException e) {
Status
status = new Status();
status.code = HttpStatus.SC_CONFLICT;
status.message = "Updating failed.";
try {
doResponse(resp, status, toXml, MODEL_PACKAGE);
}
catch (Exception ee) {
ee.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
Status
status = new Status();
status.code = HttpStatus.SC_INTERNAL_SERVER_ERROR;
status.message = e.getMessage();
try {
doResponse(resp, status, toXml, MODEL_PACKAGE);
}
catch (Exception ee) {
ee.printStackTrace();
}
}
}
}