最後に、サーブレット・クラスで適切なメソッドを実装して、そのメソッドに渡された状態を正しく読み取り、BookWrapper 上で適切なメソッドを実行してから、返された値をユーザに渡す必要があります。
$set ilusing"java.io" $set ilusing"java.util" $set ilusing"com.microfocus.cobol.program"
78 submitParameter value "submit".
78 readParameter value "Read".
78 addParameter value "Add".
78 deleteParameter value "Delete".
78 nextParameter value "Next".
78 errorVal value "ERROR".
78 defaultVal value "DEFAULT".
method-id outputBook private.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse
book as type Book.
if book <> null
invoke req::setAttribute(stockNoAttribute book::StockNumber::trim())
invoke req::setAttribute(titleAttribute book::Title::trim())
invoke req::setAttribute(authorAttribute book::Author::trim())
invoke req::setAttribute(typeAttribute book::Type::trim())
invoke req::setAttribute(priceAttribute "" & book::RetailPrice)
invoke req::setAttribute(onHandAttribute "" & book::NumberOnHand)
invoke req::setAttribute(soldAttribute "" & book::NumberSold)
invoke req::setAttribute(stockValAttribute "" & book::StockValue)
else
invoke req::setAttribute(stockNoAttribute "****")
invoke req::setAttribute(titleAttribute "*************************************")
invoke req::setAttribute(authorAttribute "*************************************")
invoke req::setAttribute(typeAttribute "****")
invoke req::setAttribute(priceAttribute "****")
invoke req::setAttribute(onHandAttribute "****")
invoke req::setAttribute(soldAttribute "****")
invoke req::setAttribute(stockValAttribute "****")
end-if
goback.
end method.
method-id outputBlankBook private.
01 book type Book value new Book.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse.
set book::StockNumber to ""
set book::Title to ""
set book::Author to ""
set book::Type to ""
set book::RetailPrice to 0
set book::NumberOnHand to 0
set book::NumberSold to 0
invoke self::outputBook(req res book)
goback.
end method.
method-id outputException private.
01 msg string.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse
e as type Exception.
set msg to e::getClass()::getName() & " [" & e::getMessage() & "]"
invoke self::outputError(req res msg)
end method.
method-id outputError private.
01 book type Book value new Book.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse
msg as string.
invoke req::setAttribute(stockNoAttribute "****")
invoke req::setAttribute(titleAttribute msg)
invoke req::setAttribute(authorAttribute "*************************************")
invoke req::setAttribute(typeAttribute "****")
invoke req::setAttribute(priceAttribute "****")
invoke req::setAttribute(onHandAttribute "****")
invoke req::setAttribute(soldAttribute "****")
invoke req::setAttribute(stockValAttribute "****")
goback.
end method.
method-id getParamsAsBook private.
01 stockNoStr string.
01 titleStr string.
01 authorStr string.
01 typeStr string.
01 priceStr string.
01 onHandStr string.
01 soldStr string.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse
returning book as type Book.
set book to new Book
set stockNoStr to req::getParameter(stockNoAttribute)
if stockNoStr = null
set stockNoStr to errorVal
end-if
set book::StockNumber to stockNoStr
set titleStr to req::getParameter(titleAttribute)
if titleStr = null
set titleStr to errorVal
end-if
set book::Title to titleStr
set authorStr to req::getParameter(authorAttribute)
if authorStr = null
set authorStr to errorVal
end-if
set book::Author to authorStr
set typeStr to req::getParameter(typeAttribute)
if typeStr = null
set typeStr to errorVal
end-if
set book::Type to typeStr
set priceStr to req::getParameter(priceAttribute)
if priceStr = null
set priceStr to errorVal
end-if
set book::RetailPrice to type ScaledInteger::parseScaledInteger(priceStr)
set onHandStr to req::getParameter(onHandAttribute)
if onHandStr = null or onHandStr::length() = 0
set onHandStr to "0"
end-if
set book::NumberOnHand to type Integer::parseInt(onHandStr)
set soldStr to req::getParameter(soldAttribute)
if soldStr = null or onHandStr::length() = 0
set soldStr to "0"
end-if
set book::NumberSold to type Integer::parseInt(soldStr)
end method.
method-id performRead private.
01 bookId string.
01 book type Book.
01 e type Exception.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse.
set bookId to req::getParameter(stockNoAttribute)
if bookId = null
set bookId to errorVal
end-if
try
set book to type Book::Read(bookId)
invoke self::outputBook(req res book)
catch e
invoke self::outputException(req res e)
end-try
goback.
end method.
method-id doProcessing protected.
01 viewUrl string value "/BookJsp.jsp".
01 dispatcher type RequestDispatcher.
01 subValue string.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse
isGet as condition-value.
set subValue to req::getParameter(submitParameter)
if subValue = null
set subValue to defaultVal
end-if
evaluate true
when subValue::equals(readParameter)
invoke self::performRead(req res)
when subValue::equals(addParameter)
invoke self::performAdd(req res)
when subValue::equals(deleteParameter)
invoke self::performDelete(req res)
when subValue::equals(nextParameter)
invoke self::performNext(req res)
when other
invoke self::outputBlankBook(req res)
end-evaluate
set dispatcher to self::getServletContext()::getRequestDispatcher(viewUrl)
invoke dispatcher::forward(req res)
goback.
end method.
method-id performAdd private.
01 book type Book.
01 e type Exception.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse.
try
set book to self::getParamsAsBook(req res)
set book to type Book::Add(book)
invoke self::outputBook(req res book)
catch e
invoke self::outputException(req res e)
end-try
goback.
end method.
method-id performDelete private.
01 book type Book.
01 e type Exception.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse.
try
set book to self::getParamsAsBook(req res)
set book to type Book::Delete(book::StockNumber)
invoke self::outputBook(req res null)
catch e
invoke self::outputException(req res e)
end-try
goback.
end method.
method-id performNext private.
01 book type Book.
01 e type Exception.
procedure division using by value req as type HttpServletRequest
res as type HttpServletResponse.
try
set book to self::getParamsAsBook(req res)
set book to type Book::Next(book::StockNumber)
invoke self::outputBook(req res book)
catch e
invoke self::outputException(req res e)
end-try
goback.
end method.