/*
* (C) Copyright 2014 Agile Booster.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Maxime ESCOURBIAC
*/
package com.agilebooster.data.jpa;
import com.agilebooster.data.entity.Event;
import com.agilebooster.data.entity.Project;
import com.agilebooster.data.entity.User;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
* Controller class from Events table.
*
* @author Maxime ESCOURBIAC
*/
public class EventController {
private final EntityManagerFactory emf;
/**
* Default constructor.
*/
public EventController() {
this.emf = Persistence.createEntityManagerFactory("org.agilebooster_db");
}
/**
* Entity Manager. Manage the object-relational-mapping persistance.
*
* @return Entity Manager.
*/
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
/**
* Create the event in database.
*
* @param event Event to create.
* @return Event created with id value returned.
*/
public Event create(Event event) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
//Insert event.
em.persist(event);
em.flush();
//Update Project.
Project project = event.getProject();
project.addEvent(event);
em.merge(project);
//Update Organizer
User organizer = event.getOrganizer();
organizer.addEventOrganized(event);
em.merge(organizer);
//Update guests
List<User> guests = event.getGuests();
for (User guest : guests) {
guest.addInvitedEvent(event);
em.merge(guest);
}
//Commit.
em.getTransaction().commit();
} catch (Exception ex) {
event = null;
Logger.getLogger(EventController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (em != null) {
em.close();
}
}
return event;
}
/**
* Destroy the event.
*
* @param event Event to destroy.
* @return True if the deletion was successfuly executed.
*
*/
public boolean destroy(Event event) {
EntityManager em = null;
boolean success = true;
try {
em = getEntityManager();
em.getTransaction().begin();
event = em.getReference(Event.class, event.getId());
//Updating project.
Project project = event.getProject();
if (project != null) {
project.getEvents().remove(event);
em.merge(project);
}
//Updating organizer.
User organizer = event.getOrganizer();
if (organizer != null) {
organizer.getEventsOrganized().remove(event);
em.merge(organizer);
}
//Updating guests.
List<User> guests = event.getGuests();
for (User guest : guests) {
guest.getInvitedEvents().remove(event);
em.merge(guest);
}
//Deleting event.
em.remove(em.merge(event));
em.getTransaction().commit();
} catch (Exception ex) {
Logger.getLogger(ProjectController.class.getName()).log(Level.SEVERE, null, ex);
success = false;
} finally {
if (em != null) {
em.close();
}
}
return success;
}
/**
* Refresh an event.
*
* @param event Event to refresh.
* @return The event refreshed.
*/
public Event
refresh(Event event) {
return getEntityManager().getReference(Event.class, event.getId());
}
}