001    /** =====================================================================       
002    *
003    *  File Name : $Id: FaultWorkAround.java,v 1.7 2008/01/15 11:08:16 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           20 Apr 2007 C.Bevil      First Release
023    *
024    *     Commissioning Notes
025    *     -------------------
026    *
027    *     None
028    *     
029    *  =====================================================================
030    *
031    *     @Version   : $Id: FaultWorkAround.java,v 1.7 2008/01/15 11:08:16 cb Exp $
032    *
033    *     @Author    : $Author: cb $
034    *
035    *     Header     : $Header: /opt/INGsrc/src/CVS/softproj/FaultDatabase/src/FaultDatabase/FaultDatabase/src/GWTApplication/client/FaultWorkAround.java,v 1.7 2008/01/15 11:08:16 cb Exp $
036    *
037    *     Log        : $Log: FaultWorkAround.java,v $
038    *     Log        : Revision 1.7  2008/01/15 11:08:16  cb
039    *     Log        : Ran through PMD and sorted out the javadoc so that we could export the
040    *     Log        : javadoc to the javadoc repository.
041    *     Log        :
042    *     Log        : Revision 1.6  2007/12/12 15:26:14  cb
043    *     Log        : Added new javascript library which allows fancy message boxes to be
044    *     Log        : displayed.
045    *     Log        :
046    *     Log        : Revision 1.5  2007/08/17 14:24:30  cb
047    *     Log        : Removed the submit & reset buttons as these functions will now be
048    *     Log        : controlled through the submit and reset buttons on the main update
049    *     Log        : panel
050    *     Log        :
051    *     Log        : Revision 1.4  2007/08/17 07:26:15  cb
052    *     Log        : Comment update, added error reports
053    *     Log        :
054    *     Log        : Revision 1.3  2007/08/01 13:00:07  cb
055    *     Log        : First prototype after import
056    *     Log        :
057    *     Log        : Revision 1.2  2007/07/13 10:54:07  cb
058    *     Log        : Complete interface prototype
059    *     Log        :
060    *     Log        : Revision 1.1.1.1  2007/06/01 08:33:25  cb
061    *     Log        : Imported using TkCVS
062    *     Log        :
063    *
064    * =====================================================================*/
065    
066    
067    package GWTApplication.client;
068    
069    import com.google.gwt.user.client.*;
070    import com.google.gwt.user.client.rpc.*;
071    import com.google.gwt.user.client.ui.*;
072    import com.google.gwt.user.client.ui.FlexTable.*;
073    
074    import com.gwtext.client.core.EventObject;
075    import com.gwtext.client.util.Format;
076    import com.gwtext.client.widgets.MessageBox;
077    import com.gwtext.client.widgets.MessageBoxConfig;
078    
079    import java.util.*;
080    
081    /**
082     * This class will be used to generate a tab at the bottom of the
083     * fault modification form which will allow the user to attach to
084     * the fault the details of a workaround which he has entered. The
085     * class makes extensive use of the google web toolkit.
086     * @author Craige Bevil
087     * @version $Id: FaultWorkAround.java,v 1.7 2008/01/15 11:08:16 cb Exp $
088     */
089    
090    class FaultWorkAround extends FaultAddition {
091    
092        /**
093         * This contains the details of the current workaround if there is
094         * one entered into the database against the current
095         * fault.
096         */
097        
098        private Workaround CurrentWorkaroundDetails;
099        
100        /**
101         * Constructor for the fault workaround tab in the modify fault form
102         * @param FaultNumber This is the number of the defect report
103         * which this workaround is associated with.
104         * @param svc This is an object which can be used to invoke remote
105         * calls on the servlet. 
106         * @param internationalizationConstants Used for
107         * internationalization. 
108         * @param userAuthentication This contains the details of the
109         * authentication which was performed by the user. 
110         * @param faultUpdateListener This is a reference to a listener
111         * object which will be called when the the workaround has been
112         * updated
113         */
114        
115        FaultWorkAround (final int FaultNumber,
116                         final FaultServiceAsync svc,
117                         final InternationalizationConstants internationalizationConstants,
118                         final AuthenticationDetails userAuthentication,
119                         final FaultUpdatedListener faultUpdateListener) {
120            
121            super(FaultNumber,svc,internationalizationConstants,userAuthentication,faultUpdateListener);
122            
123            setFormDefaultValues();
124        }
125    
126        /**
127         * Save the contents of the fault workaround to the database 
128         */
129        
130        public void saveFormContentsToDatabase () {
131    
132            final Workaround workaround = new Workaround();
133            
134            // If the user has not entered anything into the work around
135            // main text box then we do nothing here.
136            
137            if (!stringContainsText(FaultAdditionText.getHTML())) {
138                return;
139            }
140    
141            // Verify that the user has entered the amount of time
142            // spent in the correct format      
143                
144            if (!verifyTimeFormat(TimeSpent.textBox.getText())) {
145                MessageBox.alert(internationalizationConstants.information(),internationalizationConstants.timeFieldNotCompletedCorrectly(internationalizationConstants.timeSpent()));
146                return;                             
147            }
148        
149            // If we get this far then everything is order to we can
150            // now go ahead and try and update the database with the
151            // details of the workaround.
152    
153            workaround.description      = FaultAdditionText.getHTML();
154            workaround.id               = FaultNumber;
155            workaround.enteredByEmail   = EnteredBy.listBox.getValue(EnteredBy.listBox.getSelectedIndex());
156            workaround.timeSpent        = TimeSpent.textBox.getText();
157    
158            // Now send the details of the workaround down to the
159            // servlet so that it can be inserted into the database
160    
161            svc.updateWorkaroundDetails (workaround,new AsyncCallback() {
162                        
163                    /**
164                     * On success we clear the form and inform the user of
165                     * the number of the defect which was assigned
166                     */
167                    
168                    public void onSuccess (final Object result) {
169                                                    
170                        // Now update the default values for the
171                        // workaround to be those which the user has
172                        // just entered
173    
174                        CurrentWorkaroundDetails = workaround;
175    
176                        // Inform any listener that we have updated
177                        // the fault 
178                            
179                        if (faultUpdateListener != null) {
180                            faultUpdateListener.faultUpdatedEvent(FaultNumber);
181                        }                   
182                    }
183                    
184                    public void onFailure (Throwable ex) {
185                        MessageBox.alert(internationalizationConstants.information(),internationalizationConstants.unableToUpdateWorkAround());
186                    }
187                }); 
188        }
189        
190        /**
191         * This will be used to set the default values which are
192         * associated with the fault workaround. It will attempt to get
193         * the current workaround data for this fault from the database
194         * and populate the fields in the workaround form otherwise it
195         * will set the values of the form to be the default values.
196         */
197    
198        void setFormDefaultValues () {
199            
200            svc.getWorkaroundDetails (new Integer(FaultNumber),new AsyncCallback() {
201                                    
202                    public void onSuccess (final Object result) {
203                        CurrentWorkaroundDetails = (Workaround)result;
204                        resetFormToDefaultValues();
205                    }
206                    
207                    public void onFailure (Throwable ex)
208                    {
209                    }
210                });
211        }
212    
213        /**
214         * Reset the form to the values which are determined the default
215         * values which have been entered for the workaround by the
216         * user. The attribute <b>CurrentWorkaroundDetails</B> holds the
217         * details of the current workaround if there is one. 
218         */
219        
220        void resetFormToDefaultValues() {
221    
222            // If there is a workaround associated with the fault then we
223            // display the values associated with that workaround.
224    
225            if (CurrentWorkaroundDetails == null) {
226    
227                FaultAdditionText.setHTML("");
228                TimeSpent.textBox.setText("00:00");
229                setSelectedItemInListbox(EnteredBy.listBox,userAuthentication.UserEmailAddress);                
230    
231            } else {
232    
233                FaultAdditionText.setHTML(CurrentWorkaroundDetails.description);
234                TimeSpent.textBox.setText(CurrentWorkaroundDetails.timeSpent);
235                setSelectedItemInListbox(EnteredBy.listBox,CurrentWorkaroundDetails.enteredByEmail);
236            }
237        }
238    }