Java tutorial
/* * Copyright (C) 2012 bwgz.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.bwgz.swim.openlane.service; import java.io.Serializable; import java.util.Calendar; import java.util.Date; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.bwgz.swim.openlane.data.model.Event; import org.bwgz.swim.openlane.data.model.Gender; import org.bwgz.swim.openlane.data.model.Meet; import org.bwgz.swim.openlane.data.model.OpenLane; import org.bwgz.swim.openlane.data.model.Swimmer; import org.springframework.binding.message.MessageBuilder; import org.springframework.binding.message.MessageContext; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service("eventService") @Repository public class EventServiceImpl implements EventService, Serializable { private static final long serialVersionUID = 4321883246367966536L; private EntityManager em; @PersistenceContext public void setEntityManager(EntityManager em) { this.em = em; } private long calculateAge(Date meetAgeDate, Date dateOfBirth) { Calendar mad = Calendar.getInstance(); Calendar dob = Calendar.getInstance(); mad.setTime(meetAgeDate); dob.setTime(dateOfBirth); if (dob.after(mad)) { throw new IllegalArgumentException("Can't be born in the future"); } int age = mad.get(Calendar.YEAR) - dob.get(Calendar.YEAR); if (mad.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) { age--; } return age; } @SuppressWarnings("unchecked") private List<Event> findEvents(Meet meet, Gender gender, Long age) { return em.createQuery( "select e from Event e where (e.meet = :meet and (e.gender = :mixed or e.gender = :gender) and ((e.minAge = 0 and e.maxAge = 0) or (:age >= minAge and :age <= maxAge) or (:age >= minAge and e.maxAge = 0)))") .setParameter("meet", meet).setParameter("mixed", Gender.MIXED).setParameter("gender", gender) .setParameter("age", age).getResultList(); } public List<Event> getEvents(Meet meet, Gender gender, Date birthdate) { long age = calculateAge(meet.getAgeDate(), birthdate); return findEvents(meet, gender, age); } @Transactional private Boolean doApply(MessageContext context, Swimmer swimmer, Event event) { System.out.printf("EventServiceImpl.doApply(%s, %s)\n", swimmer, event); Boolean result; MessageBuilder builder = new MessageBuilder(); String source = "message"; String message; try { OpenLane openLane = new OpenLane(); openLane.setEvent(event); openLane.setSwimmer(swimmer); openLane.setApplied(new Date()); em.persist(openLane); event.getOpenLanes().add(openLane); em.merge(event); builder = builder.info(); message = String.format("Added - Event %s %d %s", event.getNumber(), event.getDistance(), event.getStroke()); result = Boolean.TRUE; } catch (Exception e) { builder = builder.error(); message = String.format("Could not add - Event %s %d %s", event.getNumber(), event.getDistance(), event.getStroke()); result = Boolean.FALSE; } context.addMessage(builder.source(source).defaultText(message).build()); return result; } @Transactional public Boolean doApply(MessageContext context, Swimmer swimmer, List<Event> events) { System.out.printf("EventServiceImpl.doApply(%s, %s)\n", swimmer, events); Boolean result = Boolean.FALSE; for (Event event : events) { result = doApply(context, swimmer, event); if (result == Boolean.FALSE) { break; } } return result; } @Transactional private Boolean doCancel(MessageContext context, Swimmer swimmer, Event event) { System.out.printf("EventServiceImpl.doCancel(%s, %s)\n", swimmer, event); Boolean result; MessageBuilder builder = new MessageBuilder(); String source = "message"; String message; try { OpenLane openLane = event.getOpenLane(swimmer); event.getOpenLanes().remove(openLane); em.merge(event); builder = builder.info(); message = String.format("Cancelled - Event %s %d %s", event.getNumber(), event.getDistance(), event.getStroke()); result = Boolean.TRUE; } catch (Exception e) { builder = builder.error(); message = String.format("Could not cancel - Event %s %d %s", event.getNumber(), event.getDistance(), event.getStroke()); result = Boolean.FALSE; } context.addMessage(builder.source(source).defaultText(message).build()); return result; } @Transactional public Boolean doCancel(MessageContext context, Swimmer swimmer, List<Event> events) { System.out.printf("EventServiceImpl.doCancel(%s, %s)\n", swimmer, events); Boolean result = Boolean.FALSE; for (Event event : events) { result = doCancel(context, swimmer, event); if (result == Boolean.FALSE) { break; } } return result; } }