001    /** =====================================================================       
002    *
003    *  File Name : $Id: FaultUpdateMonitor.java,v 1.6 2007/09/06 07:58:12 cb Exp $
004    *
005    *  Description
006    *  -----------
007    *
008    *  See javadoc comment 
009    * 
010    *  =====================================================================
011    *
012    *   @Author : Craige Bevil 
013    *             Control Software Group
014    *             Isaac Newton Group of Telescopes
015    *
016    *  =====================================================================
017    *
018    *     Modification Log
019    *
020    *     Vers         Date        Author       Reason
021    *     ----         ----        ------       ------
022    *      1                       C.Bevil      First Release
023    *
024    *     Commissioning Notes
025    *     -------------------
026    *
027    *     None
028    *     
029    *  =====================================================================
030    *
031    *     @Version   : $Id: FaultUpdateMonitor.java,v 1.6 2007/09/06 07:58:12 cb Exp $
032    *
033    *     @Author    : $Author: cb $
034    *
035    *     Header     : $Header: /opt/INGsrc/src/CVS/softproj/FaultDatabase/src/FaultDatabase/FaultDatabase/src/GWTApplication/client/FaultUpdateMonitor.java,v 1.6 2007/09/06 07:58:12 cb Exp $
036    *
037    *     Log        : $Log: FaultUpdateMonitor.java,v $
038    *     Log        : Revision 1.6  2007/09/06 07:58:12  cb
039    *     Log        : Correct comments, added trap when updating listeners just in case
040    *     Log        : there is an exception.
041    *     Log        :
042    *     Log        : Revision 1.5  2007/09/06 06:53:43  cb
043    *     Log        : Before overhaul to fix update problems
044    *     Log        :
045    *     Log        : Revision 1.4  2007/08/22 15:57:26  cb
046    *     Log        : We now monitor all faults in the database which may have changed.
047    *     Log        :
048    *     Log        : Revision 1.3  2007/08/17 14:26:43  cb
049    *     Log        : Updated for lastest prototype incorporating a lot of Nikos comments
050    *     Log        :
051    *     Log        : Revision 1.2  2007/08/14 13:32:24  cb
052    *     Log        : Before we rip out the fault hash so that we check against the whole
053    *     Log        : database for updated faults
054    *     Log        :
055    *     Log        : Revision 1.1  2007/07/24 08:27:09  cb
056    *     Log        : First version
057    *     Log        :
058    *
059    * =====================================================================*/
060    
061    
062    package GWTApplication.client;
063    
064    import com.google.gwt.core.client.GWT;
065    import com.google.gwt.user.client.*;
066    import com.google.gwt.user.client.rpc.*;
067    import com.google.gwt.user.client.ui.*;
068    import com.google.gwt.user.client.ui.FlexTable.*;
069    import com.google.gwt.user.client.ui.HTMLTable.*;
070    import com.google.gwt.user.client.ui.Hyperlink.*;
071    
072    
073    import java.util.*;
074    
075    /**
076     * This class will be used to check the database intermittingly to see
077     * if any of the faults which listeners have registered an interest in
078     * have been updated. If any faults have been updated then the
079     * listeners are informed of the faults which have changed since the
080     * last time that we checked.
081     * @author Craige Bevil 
082     * @version $Id: FaultUpdateMonitor.java,v 1.6 2007/09/06 07:58:12 cb Exp $
083     * @see SearchResultFormatter
084     */
085    
086    class FaultUpdateMonitor extends com.google.gwt.user.client.Timer {
087    
088        /** 
089         * This is the service which will be used to access the servlet
090         * which is running in the tomcat container
091         */
092    
093        FaultServiceAsync svc;
094    
095        /**
096         * This is the time in milliseconds since 1970 since the last time
097         * that we checked if the faults had been updated
098         */
099        
100        Date timeOfLastUpdateCheck = null;
101        
102        /**
103         * This is a list of the fault listeners
104         */
105        
106        HashMap Listeners = new HashMap();
107        
108        /**
109         * This class will be used for internationalization so that we can
110         * flick between the locales of English and Spanish.
111         */
112    
113        private InternationalizationConstants internationalizationConstants = (InternationalizationConstants) GWT.create(InternationalizationConstants.class);
114    
115        /**
116         * Constructor
117         */
118    
119        FaultUpdateMonitor (FaultServiceAsync svc) {
120            this.svc = svc;
121        }
122    
123        /**
124         * Obligation of the TimerTask interface and is called
125         * intermittingly. This calls servlet operation which will check
126         * to see if any of the faults have been updated and if so it will
127         * inform the listener.
128         */
129        
130        public void run () {
131    
132            // If there are no listeners then we do nothing
133    
134            if (Listeners.size() == 0) {
135                return;
136            }
137    
138            // Now we need to call the servlet to get the list of faults
139            // which have been updated since the time of the last check 
140            
141            svc.getFaultsUpdated (timeOfLastUpdateCheck,internationalizationConstants.locale(),new AsyncCallback() {
142                    
143                    public void onSuccess (final Object result) {
144                        
145                        // On success we will get list of all of the
146                        // faults which have been updated since the last
147                        // time that we checked.
148                        
149                        FaultUpdateSnapshot faultsChanged =(FaultUpdateSnapshot)result;
150                        
151                        // Update the time that we checked the database
152                        // last (note that we get the time from the web
153                        // server and NOT the local time from the web
154                        // client as that can be completely different to
155                        // the web client). 
156                        
157                        timeOfLastUpdateCheck = faultsChanged.timeStampOfLastCheck; 
158                        
159                        if (faultsChanged.faultsUpdated.size() > 0) {
160                            
161                            // Now inform all of the listeners of the
162                            // faults that have been updated
163                            
164                            Iterator listenerList = Listeners.keySet().iterator();
165                            
166                            while (listenerList.hasNext()) {
167                                
168                                // Now get the list of faults which this listener is
169                                // interested in and prepare the list which we will
170                                // send down to the servlet for verification.
171                                
172                                final FaultListener listener = (FaultListener)listenerList.next();
173                                
174                                try {
175                                    listener.faultsChanged(faultsChanged.faultsUpdated);                                                    
176                                } catch (Exception e) {
177                                    // If we cannnot inform a listener then do nothing 
178                                }
179                            }
180                        }                                                       
181                    }
182                    
183                    public void onFailure (Throwable ex)
184                    {
185                        // It's not that important to the system if we
186                        // cannot reach the server so don't complain.
187                    }
188                });
189        }
190    
191        /**
192         * Will be used by a client to register itself as a listener so
193         * that it will be informed when faults which it is interested in
194         * are changed in the database. 
195         * @param faultListener This is the object which will be called
196         * when any one of the faults listed in the faultsOfInterest array
197         * are updated.
198         */
199        
200        public void addListener(final FaultListener faultListener) {
201            Listeners.put(faultListener,faultListener);
202        }    
203    }
204    
205