Modify Java Source

Modify the Java source code in the JSPBookDemo project to access the SQL Server database.

Modify BookBean.java

  1. In the Project Explorer expland JSPBookDemo > Java Resources > src.
  2. Double-click BookBean.java to open it in the file editor.
  3. Replace the entire contents of the file with the following:
    package com.microfocus.book;
    
    import com.microfocus.cobol.program.ScaledInteger;
    
    public class BookBean
    {
      private final String _stockno;
      private final String _isbn;
      private final String _title;
      private final String _author;
      private final String _type;
      private final String _price;
      private final String _onhand;
      private final String _sold;
      private final String _stockval;
      
      BookBean(String stockno, String isbn, String title, String author,
          String type, String price, String onhand, String sold, String stockval)
      {
        this._stockno = stockno;
        this._isbn = isbn;      
        this._title = title;     
        this._author = author;    
        this._type = type;      
        this._price = price;     
        this._onhand = onhand;    
        this._sold = sold;      
        this._stockval = stockval;
      }
      
      public String getStockno()
      {
        return _stockno;
      }
      
      public String getIsbn()
      {
        return _isbn;
      }
      
      public String getTitle()
      {
        return _title;
      }
      
      public String getAuthor()
      {
        return _author;
      }
      
      public String getType()
      {
        return _type;
      }
      
      public String getPrice()
      {
        return _price;
      }
      
      public String getOnhand()
      {
        return _onhand;
      }
      
      public String getSold()
      {
        return _sold;
      }
      
      public String getStockval()
      {
        return _stockval;
      }
      
      public void toDetails(Details details)
      {
        details.setStockno(_stockno);
        details.setIsbn(Long.parseLong(_isbn));
        details.setTitle(_title);
        details.setAuthor(_author);
        details.setType(_type);
        details.setRetail(ScaledInteger.parseScaledInteger(_price));
        
        int onHandInt = Integer.parseInt(_onhand);
        if(onHandInt < 0)
          throw new RuntimeException("The number of books on hand must be 0 or positive");
        details.setOnhand(onHandInt);
        
        int soldInt = Integer.parseInt(_sold);
        if(soldInt < 0)
          throw new RuntimeException("The number of books sold must be 0 or positive");
        details.setSold(soldInt);
      }
      
      public static BookBean fromDetails(Details details)
      {
        String stockno = details.getStockno().trim();
        String isbn = "" + details.getIsbn();
        String title = details.getTitle().trim();
        String author = details.getAuthor().trim();
        String type = details.getType().trim();
        String price = details.getRetail().toString();
        String onhand = "" + details.getOnhand();
        String sold = "" + details.getSold();
        ScaledInteger stockvalInt = details.getRetail().multiply(new ScaledInteger(details.getOnhand(), 0));
        String stockval = stockvalInt.toString();
        
        return new BookBean(stockno, isbn, title, author, type, price, onhand, sold, stockval);
      }
      
      public static BookBean blankBook()
      {
        return msgBook("*************************************");
      }
      
      public static BookBean msgBook(String msg)
      {
        String stockno    = "****";                               
        String isbn       = "*************";                      
        String title      = msg;
        String author     = "*************************************";
        String type       = "****";                               
        String price      = "****";                               
        String onhand     = "****";                               
        String sold       = "****";                               
        String stockval   = "****";                               
        
        return new BookBean(stockno, isbn, title, author, type, price, onhand, sold, stockval);
      }
    }
  4. Click File > Save to save your changes.

Modify BookInterface.java

  • Using the same technique, replace the contents of BookInterface.java with the following:
    package com.microfocus.book;
    
    import javax.servlet.http.HttpSession;
    
    import com.microfocus.cobol.program.IObjectControl;
    import com.microfocus.cobol.runtimeservices.IRunUnit;
    import com.microfocus.cobol.runtimeservices.servlet.ServletRunUnitManager;
    
    public class BookInterface
    {
        public static final String READ_RECORD   = "1";
        public static final String ADD_RECORD    = "2";
        public static final String DELETE_RECORD = "3";
        public static final String NEXT_RECORD   = "4";
      
        public final IRunUnit runUnit;
        public final BookLegacy bookLegacy;
      
      public BookInterface(HttpSession session)
      {
        this(ServletRunUnitManager.getManager().GetSessionRunUnit(session));
      }
      
      public BookInterface(IRunUnit runUnit)
      {
        this.runUnit = runUnit;
        BookLegacy bookLegacy = (BookLegacy) runUnit.GetInstance(BookLegacy.class);
        
        if(bookLegacy == null)
        {
          bookLegacy = new BookLegacy();
          runUnit.Add(bookLegacy);
        }
        
        this.bookLegacy = bookLegacy;
      }
      
      public BookBean readBook(String stockNo) throws JavaBookException
      {
        Details details = getObject(Details.class);
        Sqlstate status = getObject(Sqlstate.class);
        Sqlmessage message = getObject(Sqlmessage.class);
        
    
        details.setStockno(stockNo);
        bookLegacy.BookLegacy(READ_RECORD, details, status, message);
        
        throwExceptionIfError(status, message);
        
        return BookBean.fromDetails(details);
      }
      
      public BookBean addBook(BookBean book) throws JavaBookException
      {
        Details details = getObject(Details.class);
        Sqlstate status = getObject(Sqlstate.class);
        Sqlmessage message = getObject(Sqlmessage.class);
        
        book.toDetails(details);
        bookLegacy.BookLegacy(ADD_RECORD, details, status, message);
        
        throwExceptionIfError(status, message);
        
        return BookBean.fromDetails(details);
      }
      
      public BookBean deleteBook(String stockNo) throws JavaBookException
      {
        Details details = getObject(Details.class);
        Sqlstate status = getObject(Sqlstate.class);
        Sqlmessage message = getObject(Sqlmessage.class);
        
        details.setStockno(stockNo);
        bookLegacy.BookLegacy(DELETE_RECORD, details, status, message);
        
        throwExceptionIfError(status, message);
        
        return BookBean.fromDetails(details);
      }
      
      public BookBean nextBook(String stockNo) throws JavaBookException
      {
        Details details = getObject(Details.class);
        Sqlstate status = getObject(Sqlstate.class);
        Sqlmessage message = getObject(Sqlmessage.class);
        
        details.setStockno(stockNo);
        bookLegacy.BookLegacy(NEXT_RECORD, details, status, message);
        
        throwExceptionIfError(status, message);
        
        return BookBean.fromDetails(details);
      }
      
      private static void throwExceptionIfError(Sqlstate status, Sqlmessage message) throws JavaBookException
      {
        String sqlstate = status.getSqlstate();
        if(!"00000".equals(sqlstate) && !"02000".equals(sqlstate) && !"21000".equals(sqlstate))
        {
            throw new JavaBookException(status.getSqlstate() + message.getSqlmessage());
        }
      }
      
      private <T extends IObjectControl> T getObject(Class<T> cls)
      {
        try
        {
          T output = cls.newInstance();
          runUnit.Add(output);
          
          return output;
        }
        catch (Throwable t)
        {
          throw new RuntimeException(t);
        }
      }
    }

Modify BookServlet.java

  • Using the same technique, replace the contents of BookServlet.java with the following:
    package com.microfocus.book;
    
    import java.io.PrintWriter;
    import java.io.StringWriter;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import com.microfocus.cobol.runtimeservices.IRunUnit;
    import com.microfocus.cobol.runtimeservices.servlet.ServletRunUnitManager;
    
    public class BookServlet extends HttpServlet
    {
      private static final long serialVersionUID = -3563065100184185678L;
      
      public static final String STOCK_NO_ATTRIBUTE   = "stockno";
      public static final String TITLE_ATTRIBUTE      = "title";
      public static final String AUTHOR_ATTRIBUTE     = "author";
      public static final String TYPE_ATTRIBUTE     = "type";
      public static final String ISBN_ATTRIBUTE     = "isbn";
      public static final String PRICE_ATTRIBUTE      = "price";
      public static final String ONHAND_ATTRIBUTE     = "onhand";
      public static final String SOLD_ATTRIBUTE     = "sold";
      public static final String STATUS_ATTRIBUTE     = "status";
      public static final String RUN_UNIT_ID_ATTRIBUTE  = "rununitid";
      
      public static final String SUBMIT_PARAMETER     = "submit";
      public static final String READ_PARAMETER     = "Read";
      public static final String ADD_PARAMETER      = "Add";
      public static final String DELETE_PARAMETER     = "Delete";
      public static final String NEXT_PARAMETER     = "Next";
      public static final String END_PARAMETER      = "End Session";
      public static final String ERROR_VALUE        = "ERROR";
      public static final String DEFAULT_VALUE      = "DEFAULT";
      public static final String VIEW_URL         = "/BookJsp.jsp";
      
      protected void doProcessing(HttpServletRequest req, HttpServletResponse res, boolean isGet)
      {
        String subValue = req.getParameter(SUBMIT_PARAMETER);
        
        if(subValue == null)
        {
          subValue = DEFAULT_VALUE;
        }
        
        setRunUnitId(req);
        
      if(subValue.equals(READ_PARAMETER))
      {
        performRead(req, res);
      }
      else if(subValue.equals(ADD_PARAMETER))
      {
        performAdd(req, res);
      }
      else if(subValue.equals(DELETE_PARAMETER))
      {
        performDelete(req, res);
      }
      else if(subValue.equals(NEXT_PARAMETER))
      {
        performNext(req, res);
      }
      else if(subValue.equals(END_PARAMETER))
      {
        performEndSession(req, res);
      }
      else
      {
        outputBlankBook(req, res);
      }
      
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(VIEW_URL);
        try
      {
        dispatcher.forward(req, res);
      }
      catch(Exception e)
      {
        throw new RuntimeException(e);
      }
      }
    
      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse res)
      {
        doProcessing(req, res, true);
      }
      
      @Override
      protected void doPost(HttpServletRequest req, HttpServletResponse res)
      {
        doProcessing(req, res, false);
      }
    
      private void performRead(HttpServletRequest req, HttpServletResponse res)
      {
        BookInterface bookInterface = getBookInterface(req.getSession());
        String bookId = getStockNo(req, res);
        
        if(bookId == null)
        {
          bookId = ERROR_VALUE;
        }
        
        try
        {
          BookBean book = bookInterface.readBook(bookId);
          
          outputBook(req, res, book);
        }
        catch(JavaBookException e)
        {
          outputBookException(req, res, e);
        }
        catch (Exception e)
        {
          outputException(req, res, e);
        }
      }
    
      private void performAdd(HttpServletRequest req, HttpServletResponse res)
      {
        BookInterface bookInterface = getBookInterface(req.getSession());
        
        try
        {
          BookBean book = getBook(req, res);
    
          book = bookInterface.addBook(book);
    
          outputBook(req, res, book);
        }
        catch(JavaBookException e)
        {
          outputBookException(req, res, e);
        }
        catch (Exception e)
        {
          outputException(req, res, e);
        }
      }
    
      private void performDelete(HttpServletRequest req, HttpServletResponse res)
      {
        BookInterface bookInterface = getBookInterface(req.getSession());
        String bookId = getStockNo(req, res);
        
        if(bookId == null)
        {
          bookId = ERROR_VALUE;
        }
        
        try
        {
          BookBean book = bookInterface.deleteBook(bookId);
          
          outputBook(req, res, book);
        }
        catch(JavaBookException e)
        {
          outputBookException(req, res, e);
        }
        catch (Exception e)
        {
          outputException(req, res, e);
        }
      }
    
      private void performNext(HttpServletRequest req, HttpServletResponse res)
      {
        BookInterface bookInterface = getBookInterface(req.getSession());
        String bookId = getStockNo(req, res);
        
        if(bookId == null)
        {
          bookId = ERROR_VALUE;
        }
        
        try
        {
          BookBean book = bookInterface.nextBook(bookId);
          
          outputBook(req, res, book);
        }
        catch(JavaBookException e)
        {
          outputBookException(req, res, e);
        }
        catch (Exception e)
        {
          outputException(req, res, e);
        }
      }
        
      private void performEndSession(HttpServletRequest req, HttpServletResponse res)
      {
          HttpSession session = req.getSession();
          session.invalidate();
          
          outputError(req, res, "Session invalidated");
      }
    
    
      private void outputBlankBook(HttpServletRequest req, HttpServletResponse res)
      {   
            outputBook(req, res, BookBean.blankBook());
      }
    
      private void outputBookException(HttpServletRequest req, HttpServletResponse res, JavaBookException jbe)
      {
            outputError(req, res, jbe.getMessage());
      }
    
      private void outputException(HttpServletRequest req, HttpServletResponse res, Exception e)
      {
        String msg = e.getClass().getName() + " [" + e.getMessage() + "]";
        StringWriter strWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(strWriter);
        e.printStackTrace(printWriter);
        req.setAttribute(STATUS_ATTRIBUTE, strWriter.toString());
        outputError(req, res, msg);
      }
    
      private void outputBook(HttpServletRequest req, HttpServletResponse res, BookBean book)
      {
        if(book != null)
        {
          req.setAttribute("book", book);
        }
        else
        {
          req.setAttribute("book", BookBean.msgBook("ERROR! book is null in output book"));
        }
      }
    
      private void outputError(HttpServletRequest req, HttpServletResponse res, String msg)
      {
        outputBook(req, res, BookBean.msgBook(msg));
      }
      
      private BookBean getBook(HttpServletRequest req, HttpServletResponse res)
      {
        return new BookBean
        (
          getStockNo(req, res),
          getIsbn(req, res),
          getTitle(req, res),
          getAuthor(req, res),
          getType(req, res),
          getPrice(req, res),
          getOnhand(req, res),
          getSold(req, res),
          ""
        );
      }
      
      private String getAttribute(HttpServletRequest req, HttpServletResponse res, String attribute)
      {
        String stockNoStr;
        stockNoStr = req.getParameter(attribute);
        if(stockNoStr == null)
        {
            stockNoStr = ERROR_VALUE;
        }
        
        return stockNoStr;
      }
      
      private String getStockNo(HttpServletRequest req, HttpServletResponse res)
      {
        return getAttribute(req, res, STOCK_NO_ATTRIBUTE);
      }
      
      private String getTitle(HttpServletRequest req, HttpServletResponse res)
      {
        return getAttribute(req, res, TITLE_ATTRIBUTE);
      }
      
      private String getAuthor(HttpServletRequest req, HttpServletResponse res)
      {
        return getAttribute(req, res, AUTHOR_ATTRIBUTE);
      }
      
      private String getType(HttpServletRequest req, HttpServletResponse res)
      {
        return getAttribute(req, res, TYPE_ATTRIBUTE);
      }
      
      private String getIsbn(HttpServletRequest req, HttpServletResponse res)
      {
        return getAttribute(req, res, ISBN_ATTRIBUTE);
      }
      
      private String getPrice(HttpServletRequest req, HttpServletResponse res)
      {
        return getAttribute(req, res, PRICE_ATTRIBUTE);
      }
      
      private String getOnhand(HttpServletRequest req, HttpServletResponse res)
      {
        return getAttribute(req, res, ONHAND_ATTRIBUTE);
      }
      
      private String getSold(HttpServletRequest req, HttpServletResponse res)
      {
        return getAttribute(req, res, SOLD_ATTRIBUTE);
      }
      
      private IRunUnit getRunUnit(HttpSession session)
      {
        return ServletRunUnitManager.getManager().GetSessionRunUnit(session);
      }
    
      private void setRunUnitId(HttpServletRequest req)
      {
        HttpSession session = req.getSession();
        IRunUnit runUnit = getRunUnit(session);
        String ruid = "" +  runUnit.getRunUnitID();
        req.setAttribute(RUN_UNIT_ID_ATTRIBUTE, ruid);
      }
      
      private BookInterface getBookInterface(HttpSession session)
      {
        BookInterface output = new BookInterface(session);
        
        return output;
      }
    }
    

Modify JavaBookException.java

  • Using the same technique, replace the contents of JavaBookException.java with the following:
    package com.microfocus.book;
    
    import java.util.*;
    
    public class JavaBookException extends Exception
    {
    
      /**
    	 * 
    	 */
      private static final long serialVersionUID = -3882735817601888938L;
    
      private static final Map<String, String> messages;
      private static final String unknownErrorMessage = "Unknown Error: ";
    
      public final String statusCode;
    
      public JavaBookException(String statusCode)
      {
        super(messages.containsKey(statusCode) ? messages.get(statusCode)
            : unknownErrorMessage + statusCode);
        this.statusCode = statusCode;
      }
    
      static
      {
        messages = new HashMap<String, String>();
        messages.put("35", "Error: Data file not found");
        messages.put("23", "Error: Stock item not found");
        messages.put("46", "No more items left");
        messages.put("99", "Error: Item already exists");
        messages.put("01", "Error: File error");
        messages.put("B1", "Error: No key entered");
      }
    }