blob: f53519809c5a108743422d3d8400881a66b352ec [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010-2012 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* dclarke - EclipseLink 2.4 - MySports Demo Bug 344608
******************************************************************************/
package example.mysports.admin.services;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.annotation.sql.DataSourceDefinition;
import javax.enterprise.context.ApplicationScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.ws.rs.PathParam;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings;
import example.mysports.admin.jaxrs.MappingsLoader;
import example.mysports.admin.model.HostedLeague;
import example.mysports.admin.model.Style;
/**
*
* @author dclarke
* @since EclipseLink 2.4
*/
@ManagedBean
@ApplicationScoped
public class HostedLeagueRepository {
@PersistenceUnit(unitName = "mysports-admin")
private EntityManagerFactory emf;
public EntityManagerFactory getEmf() {
return this.emf;
}
public void setEmf(EntityManagerFactory emf) {
this.emf = emf;
}
public List<HostedLeague> allLeagues() {
EntityManager em = getEmf().createEntityManager();
try {
return em.createNamedQuery("HostedLeague.findAll", HostedLeague.class).getResultList();
} finally {
em.close();
}
}
public List<HostedLeague> allSharedLeagues() {
EntityManager em = getEmf().createEntityManager();
try {
return em.createNamedQuery("HostedLeague.findAllShared", HostedLeague.class).getResultList();
} finally {
em.close();
}
}
public HostedLeague getLeague(String leagueId) {
EntityManager em = getEmf().createEntityManager();
try {
return em.find(HostedLeague.class, leagueId);
} finally {
em.close();
}
}
public HostedLeague delete(String leagueId) {
EntityManager em = getEmf().createEntityManager();
try {
HostedLeague league = em.find(HostedLeague.class, leagueId);
if (league != null) {
em.getTransaction().begin();
em.remove(league);
em.getTransaction().commit();
}
return league;
} finally {
em.close();
}
}
public HostedLeague create(String id, String name, String scheme) {
EntityManager em = getEmf().createEntityManager();
try {
em.getTransaction().begin();
HostedLeague league = new HostedLeague(id, name, scheme);
em.persist(league);
em.getTransaction().commit();
return league;
} finally {
em.close();
}
}
public HostedLeague update(HostedLeague league) {
EntityManager em = getEmf().createEntityManager();
try {
em.getTransaction().begin();
league = em.merge(league);
em.getTransaction().commit();
return league;
} finally {
em.close();
}
}
public String getORM(String leagueId) {
EntityManager em = getEmf().createEntityManager();
try {
return MappingsLoader.getORMapping(em, leagueId);
} finally {
em.close();
}
}
public XmlBindings getOXM(String leagueId) {
EntityManager em = getEmf().createEntityManager();
try {
return MappingsLoader.getXMLBindings(em, leagueId);
} finally {
em.close();
}
}
public String getOXMString(String leagueId) throws JAXBException {
XmlBindings xmlBindings = getOXM(leagueId);
JAXBContext jc = JAXBContext.newInstance(xmlBindings.getClass().getPackage().getName());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshaller.marshal(xmlBindings, sw);
return sw.toString();
}
public String getCSS(String leagueId) {
EntityManager em = getEmf().createEntityManager();
try {
HostedLeague league = em.find(HostedLeague.class, leagueId);
Style style = null;
if (league != null && league.getColourScheme() != null) {
style = em.find(Style.class, league.getColourScheme());
}
if (style == null) {
em.find(Style.class, "default");
}
return style.getCss();
} finally {
em.close();
}
}
public InputStream getLogo(@PathParam("league") String leagueId) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(leagueId.toLowerCase() + ".png");
}
}