| /******************************************************************************* |
| * 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.ejb.Stateless; |
| import javax.persistence.EntityManager; |
| import javax.persistence.PersistenceContext; |
| 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 |
| */ |
| @Stateless |
| public class HostedLeagueRepositoryBean implements HostedLeagueRepository { |
| |
| @PersistenceContext(unitName = "mysports-admin") |
| private EntityManager entityManager; |
| |
| public EntityManager getEntityManager() { |
| return this.entityManager; |
| } |
| |
| public void setEntityManager(EntityManager em) { |
| this.entityManager = em; |
| } |
| |
| public List<HostedLeague> allLeagues() { |
| return getEntityManager().createNamedQuery("HostedLeague.findAll", HostedLeague.class).getResultList(); |
| } |
| |
| public List<HostedLeague> allSharedLeagues() { |
| return getEntityManager().createNamedQuery("HostedLeague.findAllShared", HostedLeague.class).getResultList(); |
| } |
| |
| public HostedLeague getLeague(String leagueId) { |
| return getEntityManager().find(HostedLeague.class, leagueId); |
| } |
| |
| public HostedLeague delete(String leagueId) { |
| EntityManager em = getEntityManager(); |
| HostedLeague league = em.find(HostedLeague.class, leagueId); |
| |
| if (league != null) { |
| em.remove(league); |
| } |
| return league; |
| } |
| |
| public HostedLeague create(String id, String name, String scheme) { |
| EntityManager em = getEntityManager(); |
| |
| HostedLeague league = new HostedLeague(id, name, scheme); |
| em.persist(league); |
| |
| return league; |
| } |
| |
| public HostedLeague update(HostedLeague league) { |
| EntityManager em = getEntityManager(); |
| |
| return em.merge(league); |
| } |
| |
| public String getORM(String leagueId) { |
| return MappingsLoader.getORMapping(getEntityManager(), leagueId); |
| } |
| |
| public XmlBindings getOXM(String leagueId) { |
| return MappingsLoader.getXMLBindings(getEntityManager(), leagueId); |
| } |
| |
| 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 = getEntityManager(); |
| |
| 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(); |
| } |
| |
| public InputStream getLogo(String leagueId) { |
| return Thread.currentThread().getContextClassLoader().getResourceAsStream(leagueId.toLowerCase() + ".png"); |
| } |
| |
| } |