For making users able to return books, we should make for each user the list of books that he has already borrowed. And how we can do that ? We just need to add some more stuff to getRenderableFields() method of the persistent object Book.
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 if (presentation == USER_BOOKS_PRESENTATION) {
            //books borrowed by this user
            BorrowField borrowField = new BorrowField("Return");
            return new Pair[]{
                new Pair("Title""Title"),
                new Pair("Author""Author"),
                new Pair(new FormConditionField("UserID""user_id")""),
                new Pair(borrowField, "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();
        }
    }
}

For every user we will display only those books that have his ID in their UserID field :
Pay attention again to the parameter presentation.

When you click on any URL from the Action column - the book is returned back to the library. The panel ReturnBookPanel does the needed update in the database and redirects you back to UserPanel. Here it is:
public class ReturnBookPanel extends AbstractPanel {
    private final String null_id = "null_id";
    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", null_id)}new Pair[]{
            new Pair(OBJECT_ID, inOut.getValue("book_id"))});
        sb.append("Book was returned.<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 may be borrowed again.
There is only one feature to implement - administrator should be able to delete users. Let's do it now.

Previous page Tree of contents Next page