de.heartbeat.backend.dashboard.ArchivedAlarmPage.java Source code

Java tutorial

Introduction

Here is the source code for de.heartbeat.backend.dashboard.ArchivedAlarmPage.java

Source

/*
 * Copyright 2015 momo.
 *
 * 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 de.heartbeat.backend.dashboard;

import de.heartbeat.backend.HomePage;
import de.heartbeat.datastorage.HibernateUtil;
import de.heartbeat.datastorage.entities.HeartBeat;
import de.heartbeat.datastorage.entities.Person;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 *
 * @author momo
 */
public class ArchivedAlarmPage extends HomePage {

    private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    private final List<HeartBeat> heartBeatListArchived = new ArrayList<>();

    public ArchivedAlarmPage(PageParameters parameters) {
        super(parameters);
        WebMarkupContainer archivedTable = new WebMarkupContainer("archivedTable");
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        heartBeatListArchived
                .addAll(session.createCriteria(HeartBeat.class).add(Restrictions.eq("archived", true)).list());
        session.getTransaction().commit();
        session.close();
        Collections.sort(heartBeatListArchived);

        ListView archivedView = new ListView("heartBeatCalledView", heartBeatListArchived) {
            @Override
            protected void populateItem(ListItem item) {
                final HeartBeat hb = (HeartBeat) item.getModelObject();
                final Person pers = hb.getPerson();
                DateTime date = new DateTime(hb.getTime());
                DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
                String str = fmt.print(date);
                item.add(new Label("name", pers.getFirstName() + " " + pers.getLastName()));
                item.add(new Label("time", str));
                item.add(new Label("pulse", hb.getPulse()));
            }
        };

        archivedTable.add(archivedView);
        add(archivedTable);
    }

}