View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.jdiagnose.library.db;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.Date;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import net.sf.hibernate.Hibernate;
26  
27  import org.jdiagnose.RemoteResult;
28  import org.jdiagnose.remote.RemoteDataService;
29  import org.jdiagnose.remote.RemoteResultException;
30  import org.jdiagnose.remote.RemoteResultStore;
31  import org.jdiagnose.remote.system.DefaultQuery;
32  import org.jdiagnose.remote.system.Query;
33  import org.springframework.orm.hibernate.support.HibernateDaoSupport;
34  
35  /***
36   * @author jmccrindle
37   */
38  public class HibernateRemoteDataService extends HibernateDaoSupport implements RemoteDataService, RemoteResultStore {
39  
40      private static final List agentNames = Arrays.asList(new String[] {"agent"});
41      private static final List hostNames = Arrays.asList(new String[] {"host"});
42      private static final List hostAgentNames = Arrays.asList(new String[] {"host", "agent"});
43      
44      private Query query = new DefaultQuery();
45      
46      private static class QueryWhere {
47          private List names = new ArrayList();
48          private List values = new ArrayList();
49          private String where = null;
50          String[] getNamesArray() {
51              return (String[]) names.toArray(new String[names.size()]);
52          }
53          Object[] getValuesArray() {
54              return values.toArray(new Object[values.size()]);
55          }
56      }
57      
58      public HibernateRemoteDataService() {
59          
60      }
61      
62      public HibernateRemoteDataService(Query query) {
63          this.query = query;
64      }
65      
66      /* (non-Javadoc)
67       * @see org.jdiagnose.remote.RemoteResultStore#addRemoteResult(org.jdiagnose.remote.RemoteResult)
68       */
69      public void addRemoteResult(RemoteResult result) throws RemoteResultException {
70          try {
71              DbRemoteResult dbRemoteResult = new DbRemoteResult(result);
72              getHibernateTemplate().save(dbRemoteResult);
73          } catch (Throwable e) {
74              throw new RemoteResultException(e);
75          }
76      }
77      
78      /* (non-Javadoc)
79       * @see org.jdiagnose.remote.RemoteResultStore#addRemoteResults(java.util.Iterator)
80       */
81      public void addRemoteResults(Iterator results) throws RemoteResultException {
82          while (results.hasNext()) {
83              RemoteResult result = (RemoteResult) results.next();
84              addRemoteResult(result);
85          }
86      }
87  
88      protected boolean isBlank(String value) {
89          return value == null || value.trim().length() == 0;
90      }
91  
92      protected QueryWhere createWhere(String name, Query query, List names, List values) {
93          QueryWhere where = new QueryWhere();
94          
95          where.names.addAll(names);
96          where.values.addAll(values);
97          
98          StringBuffer result = new StringBuffer();
99          if(query != null) {
100             if(names.size() == 0) {
101                 result.append(" where ");
102             } else {
103                 result.append(" and ");
104             }
105             result.append(name + ".startDate >= :startDate ");
106             where.names.add("startDate");
107             where.values.add(new Date(query.getStartDate()));
108             result.append(" and " + name + ".startDate <= :endDate ");
109             where.names.add("endDate");
110             where.values.add(new Date(query.getEndDate()));
111             if(!isBlank(query.getAgent()) && !names.contains("agent")) {
112                 result.append(" and " + name + ".agent = :agent ");
113                 where.names.add("agent");
114                 where.values.add(query.getAgent());
115             }
116             if(!isBlank(query.getHost()) && !names.contains("host")) {
117                 result.append(" and " + name + ".host = :host ");
118                 where.names.add("host");
119                 where.values.add(query.getHost());
120             }
121             if(!isBlank(query.getDiagnostic()) && !names.contains("diagnostic")) {
122                 result.append(" and " + name + ".name = :diagnostic ");
123                 where.names.add("diagnostic");
124                 where.values.add(query.getDiagnostic());
125             }
126         }
127         where.where = result.toString();
128         return where;
129     }
130     
131     /* (non-Javadoc)
132      * @see org.jdiagnose.remote.RemoteDataService#getAllResults()
133      */
134     public Iterator getAllResults() {
135         QueryWhere where = createWhere("d", query, Collections.EMPTY_LIST, Collections.EMPTY_LIST);
136         return getHibernateTemplate().findByNamedParam("select d from org.jdiagnose.library.db.DbRemoteResult as d " + where.where, 
137                 where.getNamesArray(), where.getValuesArray()).iterator();
138     }
139 
140     /* (non-Javadoc)
141      * @see org.jdiagnose.remote.RemoteDataService#getResults(java.lang.String, java.lang.String, java.lang.String)
142      */
143     public Iterator getResults(String diagnostic, String agent, String host) {
144         String sql = "select distinct d from org.jdiagnose.library.db.DbRemoteResult as d " +
145                 "where d.name = :diagnostic and d.agent = :agent and d.host = :host and d.startDate >= :startDate and d.startDate <= :endDate";
146         return getHibernateTemplate().findByNamedParam(sql, 
147                 new String[] {"diagnostic", "agent", "host", "startDate", "endDate"},
148                 new Object[] {diagnostic, agent, host, new Date(query.getStartDate()), new Date(query.getEndDate())}).iterator();
149     }
150 
151     /* (non-Javadoc)
152      * @see org.jdiagnose.remote.RemoteDataService#getLatestHostResults(java.lang.String)
153      */
154     public Iterator getLatestResultsByHost() {
155         QueryWhere where = createWhere("r", query, Collections.EMPTY_LIST, Collections.EMPTY_LIST);
156         return getHibernateTemplate().findByNamedParam(
157                 "select distinct d from org.jdiagnose.library.db.DbRemoteResult as d " +
158                 "where (d.sequenceNumber) in " +
159                 "   (select max(r.sequenceNumber) from org.jdiagnose.library.db.DbRemoteResult as r " + where.where +
160                 "   group by r.host, r.agent, r.name) order by d.host, d.agent", where.getNamesArray(), where.getValuesArray()).iterator();
161     }
162 
163     /* (non-Javadoc)
164      * @see org.jdiagnose.remote.RemoteDataService#getLatestHostResults(java.lang.String)
165      */
166     public Iterator getLatestResultsByAgent() {
167         QueryWhere where = createWhere("r", query, Collections.EMPTY_LIST, Collections.EMPTY_LIST);
168         return getHibernateTemplate().findByNamedParam(
169                 "select distinct d from org.jdiagnose.library.db.DbRemoteResult as d " +
170                 "where (d.sequenceNumber) in " +
171                 "   (select max(r.sequenceNumber) from org.jdiagnose.library.db.DbRemoteResult as r" + where.where + 
172                 "   group by r.host, r.agent, r.name) order by d.agent, d.host", where.getNamesArray(), where.getValuesArray()).iterator();
173     }
174 
175     /* (non-Javadoc)
176      * @see org.jdiagnose.remote.RemoteDataService#getLatestHostResults(java.lang.String)
177      */
178     public Iterator getLatestHostResults(String host) {
179         QueryWhere where = createWhere("r", query, hostNames, Arrays.asList(new String[] {host}));
180         return getHibernateTemplate().findByNamedParam(
181                 "select distinct d from org.jdiagnose.library.db.DbRemoteResult as d " +
182                 "where (d.sequenceNumber) in " +
183                 "   (select max(r.sequenceNumber) from org.jdiagnose.library.db.DbRemoteResult as r" +
184                 "   where r.host = :host " + where.where + " group by r.agent, r.name)", where.getNamesArray(), where.getValuesArray()).iterator();
185     }
186 
187     /* (non-Javadoc)
188      * @see org.jdiagnose.remote.RemoteDataService#getLatestHostResults(java.lang.String)
189      */
190     public Iterator getLatestAgentHostResults(String agent, String host) {
191         QueryWhere where = createWhere("r", query, hostAgentNames, Arrays.asList(new String[] {host, agent}));
192         String sql = "select distinct d from org.jdiagnose.library.db.DbRemoteResult as d " +
193                 "where (d.sequenceNumber) in " +
194                 "   (select max(r.sequenceNumber) from org.jdiagnose.library.db.DbRemoteResult as r" +
195                 "   where r.host = :host and r.agent = :agent " + where.where + " group by r.name)";
196         return getHibernateTemplate().findByNamedParam(
197                 sql, where.getNamesArray(), where.getValuesArray()).iterator();
198     }
199 
200     /* (non-Javadoc)
201      * @see org.jdiagnose.remote.RemoteDataService#getLatestAgentResults(java.lang.String)
202      */
203     public Iterator getLatestAgentResults(String agent) {
204         QueryWhere where = createWhere("r", query, agentNames, Arrays.asList(new String[] {agent}));
205         return getHibernateTemplate().findByNamedParam(
206                 "select distinct d from org.jdiagnose.library.db.DbRemoteResult as d " +
207                 "where (d.sequenceNumber) in " +
208                 "   (select max(r.sequenceNumber) from org.jdiagnose.library.db.DbRemoteResult as r" +
209                 "   where r.agent = :agent " + where.where + " group by r.host, r.name)", where.getNamesArray(), where.getValuesArray()).iterator();
210     }
211 
212     /* (non-Javadoc)
213      * @see org.jdiagnose.remote.RemoteDataService#getResult(java.lang.String)
214      */
215     public RemoteResult getResult(String guid) {
216         List list = getHibernateTemplate().find("select d from org.jdiagnose.library.db.DbRemoteResult as d where d.guid = ?", (Object) guid, Hibernate.STRING);
217         if(list.size() > 0) {
218             return (RemoteResult) list.get(0);
219         } else {
220             return null;
221         }
222     }
223 
224     public void setQuery(Query query) {
225         this.query = query;
226     }
227 }