com.alfredmuponda.lostandfound.pages.Search.java Source code

Java tutorial

Introduction

Here is the source code for com.alfredmuponda.lostandfound.pages.Search.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alfredmuponda.lostandfound.pages;

import com.alfredmuponda.lostandfound.model.Item;
import com.alfredmuponda.lostandfound.model.SearchItem;
import java.util.Arrays;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
 *
 * @author Alfred
 */
public class Search extends WebPage {

    //private TextField searchByField;
    private DropDownChoice searchByField;
    private TextField searchInputField;
    SearchItem search;
    List<Item> check;
    List<String> itemTypes;

    private final LoadableDetachableModel resultsModel;
    private IModel searchModel;
    private IModel searchTypeModel;

    public Search() {
        itemTypes = Arrays.asList("Wallet", "National ID", "Driver's Licence", "Passport", "Report ID");

        searchModel = new Model();
        searchTypeModel = new Model();

        Form form = new Form("searchform");

        searchByField = new DropDownChoice("searchby", searchTypeModel, itemTypes);
        searchInputField = new TextField("searchinput", searchModel);

        resultsModel = new LoadableDetachableModel() {
            @Override
            protected Object load() {
                String searchBy = (String) searchTypeModel.getObject();
                String searchInput = (String) searchModel.getObject();
                search = new SearchItem();
                return search.search(searchBy, searchInput);
            }
        };

        Button button = new Button("submit") {
            @Override
            public void onSubmit() {
            }
        };
        Link link = new Link("view") {
            @Override
            public void onClick() {

            }
        };

        /*ListView shows search results*/
        ListView items = new ListView("items", resultsModel) {
            @Override
            protected void populateItem(ListItem item) {
                final Item theItem = (Item) item.getModelObject();
                item.add(new Label("itemtype", theItem.getItemType()));
                item.add(new Label("itemnumber", theItem.getItemID()));
                item.add(new Label("date", theItem.getDateLorF()));
                item.add(new Label("location", theItem.getLocation()));
                item.add(new Label("description", theItem.getDescription()));
                item.add(new Label("uniqueID", theItem.getUniqueIDAsString()));
                item.add(new Link("view") {
                    @Override
                    public void onClick() {
                        //add the UUID of the article, it is used in DetailedSearchResults page
                        PageParameters pageParameters = new PageParameters();
                        pageParameters.add("uuid", theItem.getUuid());
                        setResponsePage(DetailedSearchResults.class, pageParameters);
                    }
                });
            }
        };

        /*
        *experimentation with a PageableListView
        *Shows 20 results per page
        PageableListView items = new PageableListView ("items", resultsModel) {
        @Override
        protected void populateItem(ListItem item) {
            Item theItem = (Item) item.getModelObject();
            item.add(new Label("itemtype", theItem.getItemType()));
            item.add(new Label("itemnumber", theItem.getItemID()));
            item.add(new Label("date", theItem.getDate()));
            item.add(new Label("location", theItem.getLocation()));
            item.add(new Label("description", theItem.getDescription())); 
            item.add(new Label("uniqueID", theItem.getUniqueIDAsString()));
        }
        };
        add(items);
        add(new PagingNavigator("navigator", items));
        */

        add(new HeaderPanel("headerpanel"));
        add(form);
        form.add(searchByField);
        form.add(searchInputField);
        form.add(button);
        add(items);
    }
}