Esempio n. 1
0
        /// <summary>
        /// Fetches Partner Info data from the Petra Server.
        /// </summary>
        /// <param name="ADummy">A dummy object (needed because called from the Timer). Pass in null.</param>
        private void FetchDataFromServer(object ADummy)
        {
            TimeSpan Duration;
            PartnerInfoTDS PartlyPopulatedPartnerInfoDS = null;
            bool ServerCallSuccessful = false;

            if (FTimer == null)  // prevent execution of this Method if Timer is already disposed!
            {
                return;
            }

            if (FLastDataFeched != DateTime.MinValue)
            {
                Duration = DateTime.Now.Subtract(FLastDataFeched);
            }
            else
            {
                Duration = new TimeSpan(0, 0, 0, 0, MINIMUM_SERVERCALL_GAP);
            }

            // Decide whether to make the call to the PetraServer, or whether to delay it.
            if (Duration.TotalMilliseconds < MINIMUM_SERVERCALL_GAP)
            {
//                TLogging.Log("MINIMUM_SERVERCALL_GAP NOT reached, sleeping " +
//                             (MINIMUM_SERVERCALL_GAP - Duration.TotalMilliseconds).ToString() + "ms ...");

                FTimer.Change(Convert.ToInt32(MINIMUM_SERVERCALL_GAP - Duration.TotalMilliseconds),
                    System.Threading.Timeout.Infinite);
            }
            else
            {
                // we are now in the process of fetching data from the PetraServer!
                FFetchingData = true;
                FLastDataFeched = DateTime.Now;

                // We need to work with an exact copy of FServerCallParams because FServerCallParams
                // can be overwritten anytime!
                FCurrentServerCallParams = FServerCallParams.GetDeepCopy();

//                TLogging.Log("MINIMUM_SERVERCALL_GAP reached, making Server call for Partner " +
//                             FCurrentServerCallParams.PartnerKey.ToString() + ". FCurrentServerCallParams.PartnerInfoScope: " +
//                             FCurrentServerCallParams.PartnerInfoScope.ToString("G") + "...");


                FServerCallError = String.Empty;
                FCurrentServerCallParams.ServerCallOK = false;

                // Reset Dictionary
                HyperlinkWithValueArguments =
                    new Dictionary <string, Tuple <TPartnerAttributeTypeValueKind, string>>(1);

                try
                {
                    Ict.Common.DB.TServerBusyHelper.CoordinatedAutoRetryCall("Partner Info", ref ServerCallSuccessful,
                        delegate
                        {
                            /*
                             * Actual PetraServer calls!
                             */
                            if ((FCurrentServerCallParams.PartnerInfoScope == TPartnerInfoScopeEnum.pisPartnerLocationAndRestOnly)
                                || (FCurrentServerCallParams.PartnerInfoScope == TPartnerInfoScopeEnum.pisLocationPartnerLocationAndRestOnly))
                            {
                                FCurrentServerCallParams.ServerCallOK = TServerLookup.TMPartner.PartnerInfo(FCurrentServerCallParams.PartnerKey,
                                    FCurrentServerCallParams.LocationKey,
                                    FCurrentServerCallParams.PartnerInfoScope,
                                    out FPartnerInfoDS);

                                ServerCallSuccessful = true;

                                FPartnerInfoDS.Merge(FPartnerAttributeCategoryDT);
                                FPartnerInfoDS.Merge(FPartnerAttributeTypeDT);
                            }
                            else if (FCurrentServerCallParams.PartnerInfoScope == TPartnerInfoScopeEnum.pisFull)
                            {
                                FCurrentServerCallParams.ServerCallOK = TServerLookup.TMPartner.PartnerInfo(FCurrentServerCallParams.PartnerKey,
                                    FCurrentServerCallParams.PartnerInfoScope,
                                    out FPartnerInfoDS);

                                ServerCallSuccessful = true;

                                FPartnerInfoDS.Merge(FPartnerAttributeCategoryDT);
                                FPartnerInfoDS.Merge(FPartnerAttributeTypeDT);

                                FPartnerClass = SharedTypes.PartnerClassStringToEnum(FPartnerInfoDS.PartnerHeadInfo[0].PartnerClass);
                                FPartnerShortName = FPartnerInfoDS.PartnerHeadInfo[0].PartnerShortName;
                            }
                            else
                            {
                                FCurrentServerCallParams.ServerCallOK = TServerLookup.TMPartner.PartnerInfo(FCurrentServerCallParams.PartnerKey,
                                    FCurrentServerCallParams.LocationKey,
                                    FCurrentServerCallParams.PartnerInfoScope,
                                    out PartlyPopulatedPartnerInfoDS);

                                ServerCallSuccessful = true;
                            }
                        });
                }
                catch (ESecurityPartnerAccessDeniedException Exp)
                {
                    FServerCallError = TMessages.MsgSecurityExceptionString(Exp);
                }
                catch (Exception Exc)
                {
                    FServerCallError = "An error occured while trying to retrieve data for the Partner.";
                    TLogging.Log("Partner Info: An error occured while trying to retrieve data for the Partner!  Details:\r\n" +
                        Exc.ToString());
                }

                if (!ServerCallSuccessful)
                {
                    // ServerCallRetries must be equal to MAX_RETRIES when we get here!
                    FServerCallError = String.Format(StrPartnerInfoCannotBeShownDueToTooBusyServerText,
                        Catalog.GetString(" in the Find Result"), Catalog.GetString(" in the Find Result"),
                        Catalog.GetString("re-run the search operation by pressing 'Search'"));
                }

                //MessageBox.Show("FCurrentServerCallParams.ServerCallOK: " + FCurrentServerCallParams.ServerCallOK.ToString());
                if (FServerCallError == String.Empty)
                {
                    /*
                     * Find out if the User requested a different Partner/LocationKey combination
                     * in the meantime. If so, don't update the UI with the currently retrieved data,
                     * but retrive the data for the new Partner/LocationKey combination and update the
                     * UI with that!
                     */
                    if (FCurrentServerCallParams.IsIdentical(FServerCallParams))
                    {
                        if ((FCurrentServerCallParams.PartnerInfoScope == TPartnerInfoScopeEnum.pisPartnerLocationOnly)
                            || (FCurrentServerCallParams.PartnerInfoScope == TPartnerInfoScopeEnum.pisLocationPartnerLocationOnly))
                        {
                            FPartnerInfoDS.PLocation.Rows.Clear();
                            FPartnerInfoDS.PPartnerLocation.Rows.Clear();
                            FPartnerInfoDS.Merge(PartlyPopulatedPartnerInfoDS);

                            FPartnerInfoDS.Merge(FPartnerAttributeCategoryDT);
                            FPartnerInfoDS.Merge(FPartnerAttributeTypeDT);
                        }

                        /*
                         * Update the User Interface with the data that was retrieved from the PetraServer!
                         */
                        UpdateUI();

                        // we don't fetch data from the PetraServer anymore.
                        FFetchingData = false;
                    }
                    else
                    {
                        //                    TLogging.Log("Newer FServerCallParams available; making new Server call!");
                        FetchDataFromServer(null);
                    }
                }
                else
                {
                    /*
                     * An Exception occured while making the call to the PetraServer ->
                     * inform user about that!
                     */
                    UpdateUI();

                    // we don't fetch data from the PetraServer anymore.
                    FFetchingData = false;
                }
            }
        }