View Javadoc

1   /**
2    * Copyright 2005-2011 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.impl.registry;
17  
18  import java.sql.Connection;
19  import java.sql.PreparedStatement;
20  import java.sql.SQLException;
21  import java.util.List;
22  
23  import javax.xml.namespace.QName;
24  
25  import org.apache.ojb.broker.PersistenceBroker;
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.QueryByCriteria;
28  import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
29  import org.springmodules.orm.ojb.PersistenceBrokerCallback;
30  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
31  
32  /**
33   * An OJB dao implementation of the {@link ServiceRegistryDao}. 
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public class ServiceRegistryDaoOjb extends PersistenceBrokerDaoSupport implements ServiceRegistryDao {
39  
40  	@Override
41  	public ServiceInfoBo getServiceInfo(String serviceId) {
42  		Criteria crit = new Criteria();
43  		crit.addEqualTo("serviceId", serviceId);
44  		return (ServiceInfoBo)getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
45  	}
46  
47  	@Override
48  	public List<ServiceInfoBo> getOnlineServiceInfosByName(QName serviceName) {
49  		Criteria crit = new Criteria();
50  		crit.addEqualTo("serviceName", serviceName.toString());
51  		crit.addEqualTo("statusCode", ServiceEndpointStatus.ONLINE.getCode());
52  		return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
53  	}
54  
55  	@Override
56  	public List<ServiceInfoBo> getAllOnlineServiceInfos() {
57  		Criteria crit = new Criteria();
58  		crit.addEqualTo("statusCode", ServiceEndpointStatus.ONLINE.getCode());
59  		return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
60  	}
61  	
62  	@Override
63  	public List<ServiceInfoBo> getAllServiceInfos() {
64  		Criteria crit = new Criteria();
65  		return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
66  	}
67  
68  	@Override
69  	public List<ServiceInfoBo> getAllServiceInfosForInstance(String instanceId) {
70  		Criteria crit = new Criteria();
71  		crit.addEqualTo("instanceId", instanceId);
72  		return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
73  	}
74  	
75  	@Override
76  	public ServiceDescriptorBo getServiceDescriptor(String serviceDescriptorId) {
77  		Criteria crit = new Criteria();
78  		crit.addEqualTo("id", serviceDescriptorId);
79  		return (ServiceDescriptorBo)getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ServiceDescriptorBo.class, crit));
80  	}
81  
82  	@Override
83  	public ServiceDescriptorBo saveServiceDescriptor(ServiceDescriptorBo serviceDescriptor) {
84  		getPersistenceBrokerTemplate().store(serviceDescriptor);
85  		return serviceDescriptor;
86  	}
87  
88  	@Override
89  	public ServiceInfoBo saveServiceInfo(ServiceInfoBo serviceInfo) {
90  		getPersistenceBrokerTemplate().store(serviceInfo);
91  		return serviceInfo;
92  	}
93  
94  	@Override
95  	public void removeServiceInfo(String serviceId) {
96  		Criteria crit = new Criteria();
97  		crit.addEqualTo("serviceId", serviceId);
98  		getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
99  	}
100 
101 	@Override
102 	public void removeServiceDescriptor(String serviceDescriptorId) {
103 		Criteria crit = new Criteria();
104 		crit.addEqualTo("id", serviceDescriptorId);
105 		getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ServiceDescriptorBo.class, crit));
106 	}
107 
108 	@Override
109 	public boolean updateStatus(String serviceId, String statusCode) {
110 		ServiceInfoBo serviceInfoBo = getServiceInfo(serviceId);
111 		if (serviceInfoBo == null) {
112 			return false;
113 		}
114 		serviceInfoBo.setStatusCode(statusCode);
115 		saveServiceInfo(serviceInfoBo);
116 		return true;
117 	}
118 
119 	private static final String UPDATE_STATUS_FOR_INSTANCE_ID = "update KRSB_SVC_DEF_T set STAT_CD=? where INSTN_ID=?";
120 	@Override
121 	public void updateStatusForInstanceId(final String instanceId, final String statusCode) {
122 		getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
123 			@Override
124 			public Object doInPersistenceBroker(PersistenceBroker broker) {
125 				PreparedStatement preparedStatement = null;
126 				try {
127 					Connection connection = broker.serviceConnectionManager().getConnection();
128 					preparedStatement = connection.prepareStatement(UPDATE_STATUS_FOR_INSTANCE_ID);
129 					preparedStatement.setString(1, statusCode);
130 					preparedStatement.setString(2, instanceId);
131 					preparedStatement.executeUpdate();
132 				} catch (Exception e) {
133 					throw new RuntimeException("Failed to update status for instance id", e);
134 				} finally {
135 					if (preparedStatement != null) {
136 						try {
137 							preparedStatement.close();
138 						} catch (SQLException e) {}
139 					}
140 				}
141 				return null;
142 			}
143 		});
144 	}
145 
146 }