name.marcelomorales.siqisiqi.examples.crud.webapp.HomePage.java Source code

Java tutorial

Introduction

Here is the source code for name.marcelomorales.siqisiqi.examples.crud.webapp.HomePage.java

Source

/*
 * Copyright 2013 Marcelo Morales me@marcelomorales.name
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package name.marcelomorales.siqisiqi.examples.crud.webapp;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.jolbox.bonecp.Statistics;
import name.marcelomorales.siqisiqi.bonecp.BoneCPAdm;
import name.marcelomorales.siqisiqi.examples.crud.empdept.api.EmpDeptDao;
import name.marcelomorales.siqisiqi.examples.crud.empdept.entities.Dept;
import name.marcelomorales.siqisiqi.examples.crud.empdept.entities.Emp;
import name.marcelomorales.siqisiqi.examples.crud.empdept.model.Sex;
import name.marcelomorales.siqisiqi.examples.crud.empdept.repositories.DeptRepository;
import name.marcelomorales.siqisiqi.examples.crud.empdept.repositories.EmpRepository;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
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.model.CompoundPropertyModel;
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;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;

import javax.inject.Inject;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class HomePage extends WebPage {

    private static final long serialVersionUID = 1L;

    private final IModel<String> fullTextModel;

    private final IModel<Sex> sexModel;

    @Inject
    private EmpDeptDao empDeptDao;

    @Inject
    private DeptRepository deptRepository;

    @Inject
    private EmpRepository empRepository;

    @Inject
    private BoneCPAdm boneCPAdm;

    public HomePage(final PageParameters parameters) {
        super(parameters);

        add(new Form<Void>("newdept") {

            final Model<String> deptNameModel;
            final Model<String> buildingModel;

            {
                add(new TextField<>("deptname", deptNameModel = Model.of("")));
                add(new TextField<>("building", buildingModel = Model.of("")));
            }

            @Override
            protected void onSubmit() {
                empDeptDao.newDept(deptNameModel.getObject(), buildingModel.getObject());
                setResponsePage(HomePage.class);
            }
        });

        add(new DefaultDataTable<>("depts",
                ImmutableList.<IColumn<Dept, String>>of(new PropertyColumn<Dept, String>(Model.of("id"), "deptno"),
                        new PropertyColumn<Dept, String>(Model.of("totalSalaries"), "totalSalaries"),
                        new PropertyColumn<Dept, String>(Model.of("building"), "building"),
                        new PropertyColumn<Dept, String>(Model.of("deptname"), "deptname")),
                new SortableDataProvider<Dept, String>() {

                    @Override
                    public Iterator<? extends Dept> iterator(long first, long count) {
                        return deptRepository.findByExample(null, null, null, first, count).iterator();
                    }

                    @Override
                    public long size() {
                        return deptRepository.count();
                    }

                    @Override
                    public IModel<Dept> model(Dept object) {
                        Dept dept = new Dept();
                        BeanUtils.copyProperties(object, dept);
                        return Model.of(dept);
                    }
                }, 5));

        add(new Form<Emp>("newemp", new CompoundPropertyModel<>(Model.of(new Emp()))) {
            {
                add(new TextField<String>("fname"));
                add(new TextField<String>("lname"));
                add(new DropDownChoice<>("sex", ImmutableList.copyOf(Sex.values())));
                add(new TextField<String>("ssn"));
                add(new TextField<BigDecimal>("salary"));
                add(new DropDownChoice<>("dept", new LoadableDetachableModel<List<Dept>>() {

                    @Override
                    protected List<Dept> load() {
                        return Lists.newLinkedList(deptRepository.findAll());
                    }
                }, new ChoiceRenderer<Dept>("deptname")));
            }

            @Override
            protected void onSubmit() {
                empDeptDao.saveEmp(getModelObject());
                setResponsePage(HomePage.class);
            }
        });

        add(new DefaultDataTable<>("emps",
                ImmutableList.<IColumn<Emp, String>>of(new PropertyColumn<Emp, String>(Model.of("id"), "empid"),
                        new PropertyColumn<Emp, String>(Model.of("fname"), "fname"),
                        new PropertyColumn<Emp, String>(Model.of("lname"), "lname"),
                        new PropertyColumn<Emp, String>(Model.of("sex"), "sex"),
                        new PropertyColumn<Emp, String>(Model.of("ssn"), "ssn"),
                        new PropertyColumn<Emp, String>(Model.of("salary"), "salary"),
                        new PropertyColumn<Emp, String>(Model.of("dept"), "dept.deptname")),
                new SortableDataProvider<Emp, String>() {

                    @Override
                    public Iterator<? extends Emp> iterator(long first, long count) {
                        return empRepository.findByExample(null, null, null, first, count).iterator();
                    }

                    @Override
                    public long size() {
                        return empRepository.count();
                    }

                    @Override
                    public IModel<Emp> model(Emp object) {
                        Emp emp = new Emp();
                        BeanUtils.copyProperties(object, emp);
                        return Model.of(emp);
                    }
                }, 5));

        fullTextModel = Model.of("");
        add(new Form<String>("fulltexts", fullTextModel) {
            {
                add(new TextField<>("search", getModel()));
            }
        });

        add(new DefaultDataTable<>("fulltextdept",
                ImmutableList.<IColumn<Dept, String>>of(new PropertyColumn<Dept, String>(Model.of("id"), "deptno"),
                        new PropertyColumn<Dept, String>(Model.of("totalSalaries"), "totalSalaries"),
                        new PropertyColumn<Dept, String>(Model.of("building"), "building"),
                        new PropertyColumn<Dept, String>(Model.of("deptname"), "deptname")),
                new SortableDataProvider<Dept, String>() {

                    @Override
                    public Iterator<? extends Dept> iterator(long first, long count) {
                        return deptRepository.findByFullTexts(fullTextModel.getObject(), null, first, count)
                                .iterator();
                    }

                    @Override
                    public long size() {
                        return deptRepository.countByFullTexts(fullTextModel.getObject());
                    }

                    @Override
                    public IModel<Dept> model(Dept object) {
                        Dept dept = new Dept();
                        BeanUtils.copyProperties(object, dept);
                        return Model.of(dept);
                    }
                }, 5));

        sexModel = Model.of();
        add(new Form<Sex>("findBySex", sexModel) {
            {
                add(new DropDownChoice<>("sex", getModel(), ImmutableList.copyOf(Sex.values())));
            }
        });

        add(new DefaultDataTable<>("empbysex",
                ImmutableList.<IColumn<Emp, String>>of(new PropertyColumn<Emp, String>(Model.of("id"), "empid"),
                        new PropertyColumn<Emp, String>(Model.of("fname"), "fname"),
                        new PropertyColumn<Emp, String>(Model.of("lname"), "lname"),
                        new PropertyColumn<Emp, String>(Model.of("sex"), "sex"),
                        new PropertyColumn<Emp, String>(Model.of("ssn"), "ssn"),
                        new PropertyColumn<Emp, String>(Model.of("salary"), "salary"),
                        new PropertyColumn<Emp, String>(Model.of("dept"), "dept.deptname")),
                new SortableDataProvider<Emp, String>() {

                    @Override
                    public Iterator<? extends Emp> iterator(long first, long count) {
                        if (sexModel.getObject() == null) {
                            return Collections.emptyIterator();
                        }
                        return empRepository.findBySex(sexModel.getObject(), new PageRequest((int) (first / 5), 5))
                                .iterator();
                    }

                    @Override
                    public long size() {
                        if (sexModel.getObject() == null) {
                            return 0;
                        }
                        return empRepository.countBySex(sexModel.getObject());
                    }

                    @Override
                    public IModel<Emp> model(Emp object) {
                        Emp emp = new Emp();
                        BeanUtils.copyProperties(object, emp);
                        return Model.of(emp);
                    }
                }, 5));

        add(new WebMarkupContainer("stats", new CompoundPropertyModel<>(new LoadableDetachableModel<Statistics>() {

            @Override
            protected Statistics load() {
                return boneCPAdm.getStatistics();
            }
        })) {
            {
                add(new Label("connectionsRequested"));
                add(new Label("statementExecuteTimeAvg"));
                add(new Label("statementsExecuted"));
                add(new Label("totalCreatedConnections"));
                add(new Label("totalFree"));
            }
        });

    }
}