View Javadoc

1   /**
2    * Copyright 2005-2015 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 List<ServiceInfoBo> getAllServiceInfosForApplication(String applicationId) {
77          Criteria crit = new Criteria();
78          crit.addEqualTo("applicationId", applicationId);
79          return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
80      }
81  	
82  	@Override
83  	public ServiceDescriptorBo getServiceDescriptor(String serviceDescriptorId) {
84  		Criteria crit = new Criteria();
85  		crit.addEqualTo("id", serviceDescriptorId);
86  		return (ServiceDescriptorBo)getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ServiceDescriptorBo.class, crit));
87  	}
88  
89  	@Override
90  	public ServiceDescriptorBo saveServiceDescriptor(ServiceDescriptorBo serviceDescriptor) {
91  		getPersistenceBrokerTemplate().store(serviceDescriptor);
92  		return serviceDescriptor;
93  	}
94  
95  	@Override
96  	public ServiceInfoBo saveServiceInfo(ServiceInfoBo serviceInfo) {
97  		getPersistenceBrokerTemplate().store(serviceInfo);
98  		return serviceInfo;
99  	}
100 
101 	@Override
102 	public void removeServiceInfo(String serviceId) {
103 		Criteria crit = new Criteria();
104 		crit.addEqualTo("serviceId", serviceId);
105 		getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
106 	}
107 
108 	@Override
109 	public void removeServiceDescriptor(String serviceDescriptorId) {
110 		Criteria crit = new Criteria();
111 		crit.addEqualTo("id", serviceDescriptorId);
112 		getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ServiceDescriptorBo.class, crit));
113 	}
114 
115 	@Override
116 	public boolean updateStatus(String serviceId, String statusCode) {
117 		ServiceInfoBo serviceInfoBo = getServiceInfo(serviceId);
118 		if (serviceInfoBo == null) {
119 			return false;
120 		}
121 		serviceInfoBo.setStatusCode(statusCode);
122 		saveServiceInfo(serviceInfoBo);
123 		return true;
124 	}
125 
126 	private static final String UPDATE_STATUS_FOR_INSTANCE_ID = "update KRSB_SVC_DEF_T set STAT_CD=? where INSTN_ID=?";
127 	@Override
128 	public void updateStatusForInstanceId(final String instanceId, final String statusCode) {
129 		getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
130 			@Override
131 			public Object doInPersistenceBroker(PersistenceBroker broker) {
132 				PreparedStatement preparedStatement = null;
133 				try {
134 					Connection connection = broker.serviceConnectionManager().getConnection();
135 					preparedStatement = connection.prepareStatement(UPDATE_STATUS_FOR_INSTANCE_ID);
136 					preparedStatement.setString(1, statusCode);
137 					preparedStatement.setString(2, instanceId);
138 					preparedStatement.executeUpdate();
139 				} catch (Exception e) {
140 					throw new RuntimeException("Failed to update status for instance id", e);
141 				} finally {
142 					if (preparedStatement != null) {
143 						try {
144 							preparedStatement.close();
145 						} catch (SQLException e) {}
146 					}
147 				}
148 				return null;
149 			}
150 		});
151 	}
152 
153 }