We want to make our users to be able to borrow books. How to do it ? Let's try to understand first the main idea.

If you'll take a look at the persistent object Book - you'll notice the field UserID. This field is not filled when we add new books, since it wasn't included in the ManageBookForm. Why do we need it then ? We will use it to assign a book to an user : when an user will borrow a book - we'll put the ID of this user into the field UserID of that book , so everyone will know that the book is already borrowed by someone.

But how an user will know what books he can borrow ? We can show him a list of books that weren't borrowed yet (it means that the field UserID doesn't contain a real ID of an user).
So, we need to generate a list of books that are not borrowed. How to do it ? Take a look here. Got it ? We just heed to add some stuff to getRenderableFields() method.
public class Book extends DBPersistentObject implements Listable {
    private final String null_id = "null_id";
    public static final int USER_BOOKS_PRESENTATION = 1;

    public void define() {
        addField("Title"new VarcharField(50));
        addField("Author"new VarcharField(50));
        addField("UserID"new IDField());
    }

    public Pair[] getRenderableFields(int presentation) {
        if (presentation == DEFAULT_PRESENTATION) {
            CompositeActionUrl actions = new CompositeActionUrl();
            StringBuffer sb = new StringBuffer();
            sb.append("main?");
            sb.append("panel=ManageBookForm");
            sb.append("&next_panel=LibraryAdminPanel");
            sb.append("&objectID=");
            actions.addActionUrl(new ActionUrl("Modify", sb.toString()));
            sb = new StringBuffer();
            sb.append("main?");
            sb.append("panel=DeleteBookPanel");
            sb.append("&objectID=");
            actions.addActionUrl(new ActionUrl("Delete", sb.toString()));
            return new Pair[]{
                new Pair("Title""Title"),
                new Pair("Author""Author"),
                new Pair(actions, "Action")
            };
        else {
            //not borrowed books
            BorrowField borrowField = new BorrowField("Borrow");
            return new Pair[]{
                new Pair("Title""Title"),
                new Pair("Author""Author"),
                new Pair(new ConditionField("UserID", null_id)""),
                new Pair(borrowField, "Action")
            };
        }
    }

    class BorrowField extends FormActionField {
        private String action = new String();

        public BorrowField(String action) {
            fieldName = "user_id";
            this.action = action;
        }

        public String getPresentation(ServletInOut inOut, ResultRow row) {
            StringBuffer sb = new StringBuffer("<a href=\"");
            sb.append("main?panel=" + action + "BookPanel&user_id=");
            String userID = inOut.getValue(fieldName);
            sb.append(userID);
            sb.append("&book_id=" + row.getString(DBPersistentObject.ID_FIELD_NAME));
            sb.append("\">" + action + "</a>");
            return sb.toString();
        }
    }
}

Here we made an inner class - BorrowField. We need it, because that's the way how we link an user and a book - when a book is borrowed by an user, we need to have both user ID and book ID, to be able to make the update in the database.
Also pay attention that when you click on the URL Borrow Books from UserPanel , the parameter presentation is not "0" anymore.
Now take a look at the list of books that can be borowed:

When you click on any URL from the Action column - the book is assigned to the current user (since UserPanel works with a particular user identified by his ID). The panel BorrowBookPanel does the needed update in the database and redirects you back to UserPanel. Here is is:
public class BorrowBookPanel extends AbstractPanel {
    public static final String tableName = "obj_Book";
    public static final String OBJECT_ID = "OBJECT_ID";
    public int delay = 3;

    public String show() {
        StringBuffer sb = new StringBuffer();
        SqlUtil.update(tableName, new Pair[]{new Pair("UserID", inOut.getValue("user_id"))},
                new Pair[]{new Pair(OBJECT_ID, inOut.getValue("book_id"))});
        sb.append("Book was borrowed.<BR>");
        return sb.toString();
    }

    public String getHeader() {
        StringBuffer sb = new StringBuffer();
        sb.append("<HTML><HEAD>");
        sb.append("<META HTTP-EQUIV=\"Refresh\"");
        sb.append("CONTENT=\"" + delay);
        sb.append(";URL=main?panel=UserPanel&objectID=");
        sb.append(inOut.getValue("user_id""\">");
        sb.append("</HEAD><BODY>");
        return sb.toString();
    }
}

From now on, this book won't appear in the book list till the user won't return it.
But ... users can't return books , can they ?! Not yet, but let's do it now.

Previous page Tree of contents Next page