001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.ksb.impl.registry;
017    
018    import java.sql.Connection;
019    import java.sql.PreparedStatement;
020    import java.sql.SQLException;
021    import java.util.List;
022    
023    import javax.xml.namespace.QName;
024    
025    import org.apache.ojb.broker.PersistenceBroker;
026    import org.apache.ojb.broker.query.Criteria;
027    import org.apache.ojb.broker.query.QueryByCriteria;
028    import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
029    import org.springmodules.orm.ojb.PersistenceBrokerCallback;
030    import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
031    
032    /**
033     * An OJB dao implementation of the {@link ServiceRegistryDao}. 
034     * 
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     *
037     */
038    public class ServiceRegistryDaoOjb extends PersistenceBrokerDaoSupport implements ServiceRegistryDao {
039    
040            @Override
041            public ServiceInfoBo getServiceInfo(String serviceId) {
042                    Criteria crit = new Criteria();
043                    crit.addEqualTo("serviceId", serviceId);
044                    return (ServiceInfoBo)getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
045            }
046    
047            @Override
048            public List<ServiceInfoBo> getOnlineServiceInfosByName(QName serviceName) {
049                    Criteria crit = new Criteria();
050                    crit.addEqualTo("serviceName", serviceName.toString());
051                    crit.addEqualTo("statusCode", ServiceEndpointStatus.ONLINE.getCode());
052                    return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
053            }
054    
055            @Override
056            public List<ServiceInfoBo> getAllOnlineServiceInfos() {
057                    Criteria crit = new Criteria();
058                    crit.addEqualTo("statusCode", ServiceEndpointStatus.ONLINE.getCode());
059                    return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
060            }
061            
062            @Override
063            public List<ServiceInfoBo> getAllServiceInfos() {
064                    Criteria crit = new Criteria();
065                    return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
066            }
067    
068            @Override
069            public List<ServiceInfoBo> getAllServiceInfosForInstance(String instanceId) {
070                    Criteria crit = new Criteria();
071                    crit.addEqualTo("instanceId", instanceId);
072                    return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
073            }
074    
075        @Override
076        public List<ServiceInfoBo> getAllServiceInfosForApplication(String applicationId) {
077            Criteria crit = new Criteria();
078            crit.addEqualTo("applicationId", applicationId);
079            return (List<ServiceInfoBo>)getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ServiceInfoBo.class, crit));
080        }
081            
082            @Override
083            public ServiceDescriptorBo getServiceDescriptor(String serviceDescriptorId) {
084                    Criteria crit = new Criteria();
085                    crit.addEqualTo("id", serviceDescriptorId);
086                    return (ServiceDescriptorBo)getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ServiceDescriptorBo.class, crit));
087            }
088    
089            @Override
090            public ServiceDescriptorBo saveServiceDescriptor(ServiceDescriptorBo serviceDescriptor) {
091                    getPersistenceBrokerTemplate().store(serviceDescriptor);
092                    return serviceDescriptor;
093            }
094    
095            @Override
096            public ServiceInfoBo saveServiceInfo(ServiceInfoBo serviceInfo) {
097                    getPersistenceBrokerTemplate().store(serviceInfo);
098                    return serviceInfo;
099            }
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    }