Java tutorial
/******************************************************************************* * Copyright (C) 2013, University of Patras, Greece* * * 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 *******************************************************************************/ package eu.xipi.bro4xipi.brokermodel; import gr.upatras.ece.nam.broker.Broker; import gr.upatras.ece.nam.broker.model.availabilitycontract.ResourceServiceContract; import gr.upatras.ece.nam.broker.model.users.Account; import gr.upatras.ece.nam.broker.model.users.Admin; import java.util.Iterator; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * This class maintains the entity manager and get a broker element from DB * @author ctranoris * */ public class BrokerJpaController { @PersistenceContext(unitName = "camel") private EntityManager entityManager; public EntityManager getEntityManager() { return entityManager; } public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } private static final transient Log logger = LogFactory.getLog(BrokerJpaController.class.getName()); public long count() { Query q = entityManager.createQuery("SELECT COUNT(m) FROM Broker m"); return (Long) q.getSingleResult(); } public void create(Broker messageEntity) { entityManager.persist(messageEntity); } public Broker read(final String name) { Query q = entityManager.createQuery("SELECT m FROM Broker m WHERE m.name='" + name + "'"); //q.setFirstResult(1); //comment return (Broker) q.getSingleResult(); } @SuppressWarnings("unchecked") public List<Broker> read(int firstResult, int maxResults) { Query q = entityManager.createQuery("SELECT m FROM Broker m"); q.setFirstResult(firstResult); q.setMaxResults(maxResults); return q.getResultList(); } public long countServiceContracts() { Query q = entityManager.createQuery("SELECT COUNT(m) FROM ResourceServiceContract m"); return (Long) q.getSingleResult(); } @SuppressWarnings("unchecked") /** * return contracts by offeredServiceName * @param firstResult * @param maxResults * @param offeredServiceName * @return */ public List<ResourceServiceContract> readContracts(int firstResult, int maxResults, String offeredServiceName) { Query q = entityManager.createQuery("SELECT c " + "FROM ResourceServiceContract c " + "JOIN c.forOfferedService s " + "WHERE s.name= :offeredServiceName"); q.setParameter("offeredServiceName", offeredServiceName); q.setFirstResult(firstResult); q.setMaxResults(maxResults); return q.getResultList(); } public Broker update(Broker messageEntity) { return entityManager.merge(messageEntity); } public void delete(final Broker message) { entityManager.remove(message); } public void InitializeDB() { // EntityManagerProvider entityManagerProvider = new EntityManagerProvider(); // entityManagerProvider.setCurrentEntityManager(entityManager); // EntityManagerProvider.setInstance(entityManagerProvider ); // // BrokermodelModelPackage.initialize(); // BrokermodelModelFactory fact = BrokermodelModelPackage.MODELFACTORY; // Broker bro = fact.createBroker();// new Broker(); Broker bro = new Broker(); bro.setName("myBroker"); bro.setAPIGateway("ip:port"); bro.setResourceURI("-"); bro.setDescription("This is the top exposed domain"); Admin registeredUsersValue = new Admin(); registeredUsersValue.setAddress("-"); registeredUsersValue.setName("username"); registeredUsersValue.setOrganization("organization"); Account newHasAccount = new Account(); newHasAccount.setUsername("admin"); newHasAccount.setPassword("admin"); registeredUsersValue.setHasAccount(newHasAccount); bro.addToRegisteredUsers(registeredUsersValue); addBroker(bro); } public void getAll() { List<Broker> lb = entityManager.createQuery("select p from Broker p").getResultList(); for (Iterator iterator = lb.iterator(); iterator.hasNext();) { Broker broker = (Broker) iterator.next(); logger.info(" broker found: " + broker.getName() + " id: " + broker.getId() + " uniqueid: " + broker.getUniqueID()); } } public Broker getFirstBroker() { List<Broker> lb = entityManager.createQuery("select p from Broker p").getResultList(); if (lb.size() == 0) { logger.info(" getFirstBroker() lb.size() == 0 "); //InitializeDB(); //lb = entityManager.createQuery("select p from gr_upatras_ece_nam_broker_Broker p").getResultList(); return null; } // if ( lb.size() == 0 ){ // logger.error(" Problem getFirstBroker() lb.size() == 0, will init DB "); // } for (Iterator iterator = lb.iterator(); iterator.hasNext();) { Broker broker = (Broker) iterator.next(); logger.info(" FIRST broker found: " + broker.getName() + " id: " + broker.getId() + " uniqueid: " + broker.getUniqueID()); return broker; } return null; } public BrokerJpaController() { logger.info("BrokerServiceImpl constructor"); } public String echo(String message) { return "Echo processed: " + message; } public Broker updateBroker(Broker bro) { return entityManager.merge(bro); } public void addBroker(Broker bro) { if (entityManager != null) { create(bro); logger.info(" broker created: " + bro.getName()); } else { logger.info("entityManager is null"); } } }