View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.messaging.dao.impl;
17  
18  import java.util.List;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.PersistenceContext;
22  import javax.persistence.Query;
23  import javax.xml.namespace.QName;
24  
25  import org.kuali.rice.ksb.messaging.FlattenedServiceDefinition;
26  import org.kuali.rice.ksb.messaging.ServiceInfo;
27  import org.kuali.rice.ksb.messaging.dao.ServiceInfoDAO;
28  
29  
30  public class ServiceInfoDAOJpaImpl implements ServiceInfoDAO {
31  	
32  	@PersistenceContext
33  	EntityManager entityManager;
34  	
35      public void addEntry(ServiceInfo entry) {
36          if(entry.getMessageEntryId() == null) {
37          	entityManager.persist(entry.getSerializedServiceNamespace());
38          	entry.setFlattenedServiceDefinitionId(entry.getSerializedServiceNamespace().getFlattenedServiceDefinitionId());
39              entityManager.persist(entry);
40          }
41          else {
42          	entityManager.merge(entry.getSerializedServiceNamespace());
43              entityManager.merge(entry);
44          }
45      }
46  
47      @SuppressWarnings("unchecked")
48      public List<ServiceInfo> fetchAll() {
49      	Query query = entityManager.createNamedQuery("ServiceInfo.FetchAll");
50      	return (List<ServiceInfo>) query.getResultList();
51      }
52  
53      @SuppressWarnings("unchecked")
54      public List<ServiceInfo> fetchAllActive() {
55      	Query query = entityManager.createNamedQuery("ServiceInfo.FetchAllActive");
56      	return (List<ServiceInfo>) query.getResultList();
57      }
58  
59      @SuppressWarnings("unchecked")
60      public List<ServiceInfo> fetchActiveByNameLocalPart(String localPart) {
61      	Query query = entityManager.createNamedQuery("ServiceInfo.FetchActiveByName");
62      	query.setParameter("serviceName", "{%}" + localPart);
63      	return (List<ServiceInfo>) query.getResultList();    	
64      }
65      
66      @SuppressWarnings("unchecked")
67      public List<ServiceInfo> fetchActiveByName(QName serviceName) {
68      	Query query = entityManager.createNamedQuery("ServiceInfo.FetchActiveByName");
69      	query.setParameter("serviceName", serviceName.toString());
70      	return (List<ServiceInfo>) query.getResultList();    	
71      }
72      
73      @SuppressWarnings("unchecked")
74      public List<ServiceInfo> fetchActiveByNamespace(String serviceNamespace) {
75      	Query query = entityManager.createNamedQuery("ServiceInfo.FetchActiveByName");
76      	query.setParameter("serviceNamespace", "{"+serviceNamespace+"}%");
77      	return (List<ServiceInfo>) query.getResultList();    	
78      }
79      
80      @SuppressWarnings("unchecked")
81      public List<ServiceInfo> findLocallyPublishedServices(String ipNumber, String serviceNamespace) {
82      	Query query = entityManager.createNamedQuery("ServiceInfo.FindLocallyPublishedServices");
83      	query.setParameter("serverIp", ipNumber);
84      	query.setParameter("serviceNamespace", serviceNamespace);
85      	return (List<ServiceInfo>) query.getResultList();
86      }
87  
88      public void removeEntry(ServiceInfo entry) {
89      	//Query query = entityManager.createNamedQuery("ServiceInfo.DeleteByEntry");
90      	//query.setParameter("messageEntryId", entry.getMessageEntryId());
91  		//query.executeUpdate();
92      	//entityManager.remove(entityManager.getReference(ServiceInfo.class, entry.getMessageEntryId()));
93      	entityManager.remove(entityManager.find(FlattenedServiceDefinition.class, entry.getFlattenedServiceDefinitionId()));
94      	entityManager.remove(entityManager.find(ServiceInfo.class, entry.getMessageEntryId()));
95      }
96  
97      public ServiceInfo findServiceInfo(Long serviceInfoId) {
98      	return entityManager.find(ServiceInfo.class, serviceInfoId);
99      }
100 
101     public void removeLocallyPublishedServices(String ipNumber, String serviceNamespace) {
102     	//Query query = entityManager.createNamedQuery("ServiceInfo.DeleteLocallyPublishedServices");
103     	//query.setParameter("serverIp", ipNumber);
104     	//query.setParameter("serviceNamespace", serviceNamespace);
105 		//query.executeUpdate();
106     	List<ServiceInfo> localServices = findLocallyPublishedServices(ipNumber, serviceNamespace);
107     	for (ServiceInfo localService : localServices) {
108     		entityManager.remove(entityManager.find(FlattenedServiceDefinition.class, localService.getFlattenedServiceDefinitionId()));
109     		entityManager.remove(entityManager.find(ServiceInfo.class, localService.getMessageEntryId()));
110     	}
111     }
112 
113 	public EntityManager getEntityManager() {
114         return this.entityManager;
115     }
116 
117     public void setEntityManager(EntityManager entityManager) {
118         this.entityManager = entityManager;
119     }
120 
121 	public FlattenedServiceDefinition findFlattenedServiceDefinition(Long flattenedServiceDefinitionId) {
122 		return entityManager.find(FlattenedServiceDefinition.class, flattenedServiceDefinitionId);
123 	}
124 }