Java tutorial
/* * 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.restservice; import de.heartbeat.backend.ErrorPage; import de.heartbeat.datastorage.HibernateUtil; import de.heartbeat.datastorage.entities.HeartBeat; import de.heartbeat.datastorage.entities.Person; import de.heartbeat.datastorage.entities.PulseLimit; import static java.lang.String.valueOf; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.util.string.StringValue; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; /** * * @author momo */ public class HeartBeatRest extends WebPage { private static final long serialVersionUID = 1L; private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); public HeartBeatRest(final PageParameters parameters) { super(parameters); boolean isAlert = false; int lowerBound = 0; int higherBound = 0; StringValue alert = parameters.get("alert"); if (!alert.isEmpty()) { if (valueOf(alert).equals("true")) { isAlert = Boolean.parseBoolean(alert.toString()); } } Timestamp timestamp = new Timestamp(new Date().getTime()); StringValue lower = parameters.get("lowerBound"); StringValue higher = parameters.get("higherBound"); StringValue pulse = parameters.get("pulse"); StringValue deviceId = parameters.get("deviceId"); StringValue time = parameters.get("time"); if (!isAlert) { lowerBound = Integer.parseInt(lower.toString()); } if (!isAlert) { higherBound = Integer.parseInt(higher.toString()); } int pulseInt = Integer.parseInt(pulse.toString()); // restService?pulse=60&lowerBound=45&higherBound=120&deviceId=123456&time=2015-12-21 18:53:11.115 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS"); Date parsedDate; try { parsedDate = dateFormat.parse(time.toString()); timestamp = new Timestamp(parsedDate.getTime()); } catch (ParseException ex) { Logger.getLogger(HeartBeatRest.class.getName()).log(Level.SEVERE, null, ex); } HeartBeat heartBeat = new HeartBeat(pulse.toString(), timestamp); if (!isAlert) { if (pulseInt < lowerBound || pulseInt > higherBound) { heartBeat.setAlert(true); } else { heartBeat.setAlert(false); } } else { heartBeat.setAlert(isAlert); } Person person; PulseLimit limit; Session session = sessionFactory.openSession(); session.beginTransaction(); person = (Person) session.createCriteria(Person.class) .add(Restrictions.like("deviceID", deviceId.toString())).uniqueResult(); if (person != null) { heartBeat.setPerson(person); person.getHeartbeats().add(heartBeat); session.saveOrUpdate(heartBeat); session.saveOrUpdate(person); session.getTransaction().commit(); session.close(); } else { session.getTransaction().commit(); session.close(); setResponsePage(ErrorPage.class); } add(new Label("pulse", pulse)); add(new Label("deviceId", deviceId)); add(new Label("time", timestamp)); } }