public static bool GetBestAddress(Int64 APartnerKey,
            out PLocationTable AAddress,
            out PPartnerLocationTable APartnerLocation,
            out string ACountryNameLocal,
            out string AEmailAddress)
        {
            bool NewTransaction;
            TDBTransaction Transaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.ReadCommitted,
                TEnforceIsolationLevel.eilMinimum, out NewTransaction);

            bool ResultValue = false;

            try
            {
                ResultValue = TAddressTools.GetBestAddress(APartnerKey,
                    out AAddress,
                    out APartnerLocation,
                    out ACountryNameLocal,
                    out AEmailAddress,
                    Transaction);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (NewTransaction)
                {
                    DBAccess.GDBAccessObj.RollbackTransaction();
                }
            }
            return ResultValue;
        }
Example #2
0
 /// <summary>
 /// Creates a new DataRow in the passed PLocation and PPartnerLocation tables.
 ///
 /// </summary>
 /// <param name="ALocationDT">Typed PLocation table. If nil is passed in it is created
 /// automatically</param>
 /// <param name="APartnerLocationDT">Typed PPartnerLocation table. If nil is passed in
 /// it is created automatically</param>
 /// <param name="APartnerKey">PartneKey of the Partner for which the Address should
 /// be created</param>
 /// <param name="APartnerClass">PartnerClass of the Partner for which the Address should
 /// be created</param>
 /// <param name="ACountryCode"></param>
 /// <param name="ANewLocationKey">A LocationKey that the new Location and
 /// PartnerLocation rows would be set to.</param>
 /// <returns>void</returns>
 /// <exception cref="ArgumentException"> if any of the Arguments (or their combination) is
 /// not valid
 /// </exception>
 public static void CreateNewAddress(PLocationTable ALocationDT,
     PPartnerLocationTable APartnerLocationDT,
     Int64 APartnerKey,
     TPartnerClass APartnerClass,
     String ACountryCode,
     Int32 ANewLocationKey)
 {
     CreateNewAddressInternal(ALocationDT, APartnerLocationDT, APartnerKey, APartnerClass, ACountryCode, ANewLocationKey);
 }
        /// <summary>
        /// Populates the grid for the dialog for selecting Addresses to be merged.
        /// </summary>
        public void InitializeAddressGrid(long APartnerKey)
        {
            // set text for label
            lblInfo.Text = Catalog.GetString("The following addresses exist for the Partner being merged. Select the addresses to be transferred.") +
                           "\n\n" + Catalog.GetString("Any addresses which are not selected will be deleted!");

            string CheckedMember = "CHECKED";
            string Address1      = PLocationTable.GetLocalityDBName();
            string Street2       = PLocationTable.GetStreetNameDBName();
            string Address3      = PLocationTable.GetAddress3DBName();
            string City          = PLocationTable.GetCityDBName();
            string LocationKey   = PLocationTable.GetLocationKeyDBName();
            string SiteKey       = PLocationTable.GetSiteKeyDBName();
            string LocationType  = PPartnerLocationTable.GetLocationTypeDBName();


            FMainDS = TRemote.MPartner.Partner.WebConnectors.GetPartnerDetails(APartnerKey, true, false, false);
            DataView MyDataView = FMainDS.PLocation.DefaultView;

            FDataTable = MyDataView.ToTable(true, new string[] { Address1, Street2, Address3, City, LocationKey, SiteKey });
            FDataTable.Columns.Add(new DataColumn(CheckedMember, typeof(bool)));
            FDataTable.Columns.Add(LocationType, typeof(string));

            for (int Counter = 0; Counter < FMainDS.PLocation.Rows.Count; ++Counter)
            {
                FDataTable.Rows[Counter][LocationType] = FMainDS.PPartnerLocation.Rows[Counter][LocationType];
            }

            clbRecords.Columns.Clear();
            clbRecords.AddCheckBoxColumn("", FDataTable.Columns[CheckedMember], 17, false);
            clbRecords.AddTextColumn("Address-1", FDataTable.Columns[Address1]);
            clbRecords.AddTextColumn("Street-2", FDataTable.Columns[Street2]);
            clbRecords.AddTextColumn("Address-3", FDataTable.Columns[Address3]);
            clbRecords.AddTextColumn("City", FDataTable.Columns[City]);
            clbRecords.AddTextColumn("Location Type", FDataTable.Columns[LocationType]);
            clbRecords.ValueChanged += new EventHandler(OnCheckboxChange);

            clbRecords.DataBindGrid(FDataTable, Address1, CheckedMember, Address1, false, true, false);
            clbRecords.SetCheckedStringList("");

            clbRecords.AutoResizeGrid();
        }
        /// <summary>
        /// The screen has been shown
        /// </summary>
        private void RunOnceOnActivationManual()
        {
            // enable autofind in list for first character (so the user can press character to find list entry)
            // from Sep 2015 this is handled automatically by the code generator

            clbAddress.SpecialKeys =
                ((SourceGrid.GridSpecialKeys)((((((SourceGrid.GridSpecialKeys.Arrows |
                                                   SourceGrid.GridSpecialKeys.PageDownUp) |
                                                  SourceGrid.GridSpecialKeys.Enter) |
                                                 SourceGrid.GridSpecialKeys.Escape) |
                                                SourceGrid.GridSpecialKeys.Control) | SourceGrid.GridSpecialKeys.Shift)));


            string CheckedColumn      = "CHECKED";
            string ShortNameColumn    = PPartnerTable.GetPartnerShortNameDBName();
            string PartnerKeyColumn   = PPartnerLocationTable.GetPartnerKeyDBName();
            string PartnerClassColumn = PPartnerTable.GetPartnerClassDBName();
            string LocationTypeColumn = PPartnerLocationTable.GetLocationTypeDBName();

            DataTable NewTable = FPartnerSharingLocationDV.ToTable(true,
                                                                   new string[] { ShortNameColumn, PartnerKeyColumn, PartnerClassColumn, LocationTypeColumn });

            NewTable.Columns.Add(new DataColumn(CheckedColumn, typeof(bool)));

            clbAddress.Columns.Clear();
            clbAddress.AddCheckBoxColumn("", NewTable.Columns[CheckedColumn], 17, false);
            clbAddress.AddTextColumn(Catalog.GetString("Name"), NewTable.Columns[ShortNameColumn], 240);
            clbAddress.AddPartnerKeyColumn(Catalog.GetString("Partner Key"), NewTable.Columns[PartnerKeyColumn], 90);
            clbAddress.AddTextColumn(Catalog.GetString("Partner Class"), NewTable.Columns[PartnerClassColumn], 90);
            clbAddress.AddTextColumn(Catalog.GetString("Location Type"), NewTable.Columns[LocationTypeColumn], 90);

            clbAddress.DataBindGrid(NewTable, ShortNameColumn, CheckedColumn, PartnerKeyColumn, false, true, false);

            // initialize list of checked items
            clbAddress.SetCheckedStringList("");

            // this is just to avoid compiler warning from FMainDS being initialized but not used
            if (FMainDS.IsInitialized)
            {
            }
        }
        /// <summary>
        /// todoComment
        /// </summary>
        /// <returns></returns>
        public TLocationPK DetermineCurrentLocationPK()
        {
            DataRow     CurrentDR = this.CurrentDataRow;
            TLocationPK LocationPK;

            if (CurrentDR != null)
            {
                // get LocationKey of current DataRow
                LocationPK =
                    new TLocationPK(Convert.ToInt64(CurrentDR[PPartnerLocationTable.GetSiteKeyDBName()]),
                                    Convert.ToInt32(CurrentDR[PPartnerLocationTable.GetLocationKeyDBName()]));
            }
            else
            {
                LocationPK = new TLocationPK(-1, -1);
            }

            // MessageBox.Show('SiteKey: ' + LocationPK.SiteKey.ToString + "\r\n" +
            // 'LocationKey: ' + LocationPK.LocationKey.ToString);
            return(LocationPK);
        }
Example #6
0
        /// <summary>
        /// todoComment
        /// </summary>
        /// <param name="e"></param>
        /// <param name="AVerificationResultCollection"></param>
        /// <param name="AVerificationResult"></param>
        /// <returns></returns>
        public static Boolean VerifyPartnerLocationData(DataColumnChangeEventArgs e,
                                                        TVerificationResultCollection AVerificationResultCollection,
                                                        out TVerificationResult AVerificationResult)
        {
            Boolean ReturnValue;

            AVerificationResult = null;

            // MessageBox.Show('Verifying DataRow...');
            if ((e.Column.ColumnName == PPartnerLocationTable.GetDateEffectiveDBName()) ||
                (e.Column.ColumnName == PPartnerLocationTable.GetDateGoodUntilDBName()))
            {
                VerifyDates(e, AVerificationResultCollection, out AVerificationResult);
            }

            if (e.Column.Ordinal == ((PPartnerLocationTable)e.Column.Table).ColumnEmailAddress.Ordinal)
            {
                VerifyEmailAddress(e, out AVerificationResult);
            }

            if (e.Column.Ordinal == ((PPartnerLocationTable)e.Column.Table).ColumnLocationType.Ordinal)
            {
                VerifyLocationType(e, out AVerificationResult);
            }

            // any verification errors?
            if (AVerificationResult == null)
            {
                ReturnValue = true;
            }
            else
            {
                ReturnValue = false;

                // MessageBox.Show('VerifyPartnerLocationData: There was an error!');
            }

            return(ReturnValue);
        }
Example #7
0
 /// <summary>
 /// Creates a new DataRow in the passed PLocation and PPartnerLocation tables.
 /// </summary>
 /// <param name="ALocationDT">Typed PLocation table. If null is passed in it is created
 /// automatically</param>
 /// <param name="APartnerLocationDT">Typed PPartnerLocation table. If null is passed in
 /// it is created automatically</param>
 /// <param name="APartnerKey">PartneKey of the Partner for which the Address should
 /// be created</param>
 /// <param name="APartnerClass">PartnerClass of the Partner for which the Address should
 /// be created</param>
 /// <param name="ACountryCode"></param>
 /// <param name="ANewLocationKey">A LocationKey that the new Location and
 /// PartnerLocation rows would be set to.</param>
 /// <param name="ACopyFromFamilyPartnerKey">Pass in the PartnerKey of a Family Partner
 /// to copy over all data from the Family's Address. Note: the DataRow
 /// specified with ACopyFromFamilyPartnerKey must be present in the
 /// APartnerLocationDT table!</param>
 /// <param name="ACopyFromFamilyLocationKey">Pass in the LocationKey of the Family
 /// Partner specified in ACopyFromFamilyPartnerKey to copy over all data from
 /// the Family's Address. Note: the DataRow specified with
 /// ACopyFromFamilyLocationKey must be present in both the ALocationDT and
 /// APartnerLocationDT table!</param>
 /// <param name="ACopyFromFamilySiteKey">A SiteKey to find the location that should be the source of the copy</param>
 /// <param name="ACopyFromFamilyOnlyLocation"></param>
 /// <param name="ADeleteDataRowCopiedFrom"></param>
 /// <exception cref="ArgumentException"> if any of the Arguments (or their combination) is
 /// not valid.</exception>
 public static void CreateNewAddress(PLocationTable ALocationDT,
                                     PPartnerLocationTable APartnerLocationDT,
                                     Int64 APartnerKey,
                                     TPartnerClass APartnerClass,
                                     String ACountryCode,
                                     Int32 ANewLocationKey,
                                     Int64 ACopyFromFamilyPartnerKey,
                                     Int32 ACopyFromFamilyLocationKey,
                                     Int64 ACopyFromFamilySiteKey,
                                     Boolean ACopyFromFamilyOnlyLocation,
                                     Boolean ADeleteDataRowCopiedFrom)
 {
     CreateNewAddressInternal(ALocationDT,
                              APartnerLocationDT,
                              APartnerKey,
                              APartnerClass,
                              ACountryCode,
                              ANewLocationKey,
                              ACopyFromFamilyPartnerKey,
                              ACopyFromFamilyLocationKey,
                              ACopyFromFamilySiteKey,
                              ACopyFromFamilyOnlyLocation,
                              ADeleteDataRowCopiedFrom);
 }
        /// <summary>
        /// Allows adding of a new Location of a FAMILY Partner to all PERSONS of that
        /// FAMILY.
        ///
        /// @comment Must only be called for Partners of Partner Class FAMILY - the
        /// function does no checks on that and will fail for other Partner Classes!
        /// </summary>
        /// <returns>void</returns>
        private static TSubmitChangesResult PerformLocationFamilyMemberPropagationChecks(PPartnerLocationRow APartnerLocationRow,
            ref PartnerAddressAggregateTDS AResponseDS,
            TDBTransaction ASubmitChangesTransaction,
            Int64 APartnerKey,
            String APartnerClass,
            ref PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable AAddressAddedPromotionDT,
            ref PPartnerLocationTable APartnerLocationTable,
            PartnerAddressAggregateTDSSimilarLocationParametersTable AExistingLocationParametersDT,
            TLocationPK[, ] ALocationReUseKeyMapping,
            out Boolean APerformPropagation,
            ref TVerificationResultCollection AVerificationResult)
        {
//          TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks for LocationKey: " + APartnerLocationRow.LocationKey.ToString() +
//                    "; AAddressAddedPromotionDT.Rows.Count: " + AAddressAddedPromotionDT.Rows.Count.ToString());

            APerformPropagation = false;
            TLocationPK SubmittedLocationPK = DetermineReplacedLocationPK(APartnerLocationRow, ALocationReUseKeyMapping);
            TLocationPK LocationPK = DetermineReplacedLocationPK(APartnerLocationRow, AExistingLocationParametersDT);

            if (CheckFamilyMemberPropagation(APartnerLocationRow, APartnerKey, APartnerClass, ref AAddressAddedPromotionDT, LocationPK,
                    ASubmitChangesTransaction))
            {
                // Check if there is a Parameter Row for the LocationKey we are looking at
                DataView PropagateLocationParametersDV = new DataView(AAddressAddedPromotionDT,
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetSiteKeyDBName() + " = " + LocationPK.SiteKey.ToString() +
                    " AND " +
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationKeyDBName() + " = " +
                    LocationPK.LocationKey.ToString() +
                    " AND " + PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationAddedDBName() + " = true AND " +
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetAnswerProcessedClientSideDBName() + " = false",
                    "",
                    DataViewRowState.CurrentRows);

                if (PropagateLocationParametersDV.Count > 0)
                {
//                  TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: Location " + APartnerLocationRow.LocationKey.ToString() + ": found Family Members, decision on propagation is needed.");

                    /*
                     * More information is needed (usually via user interaction)
                     * -> stop processing here and return parameters
                     * (usually used for UI interaction)
                     */
                    if (AResponseDS == null)
                    {
//                      TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: Creating AResponseDS.");
                        AResponseDS = new PartnerAddressAggregateTDS(MPartnerConstants.PARTNERADDRESSAGGREGATERESPONSE_DATASET);
                    }

//                  TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: AAddressAddedPromotionDT.Rows.Count: " + AAddressAddedPromotionDT.Rows.Count.ToString());
                    AResponseDS.Merge(AAddressAddedPromotionDT);
//                  TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: Merged AAddressAddedPromotionDT into AResponseDS.");
//                  TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: PerformLocationFamilyMemberPropagationChecks: AResponseDS.Tables[" +
//                            MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME + "].Rows.Count: " +
//                            AResponseDS.Tables[MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME].Rows.Count.ToString());
                    return TSubmitChangesResult.scrInfoNeeded;
                }
                else
                {
//                  TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: Location " + APartnerLocationRow.LocationKey.ToString() + ": found Family Members and new Location should be propagated to them!");

                    /*
                     * Family Members were found and the new Location should be added to all
                     * of them!
                     */
                    APerformPropagation = true;

                    // Load all Persons of the Family
                    PPersonTable FamilyPersonsDT = PPersonAccess.LoadViaPFamily(APartnerKey, ASubmitChangesTransaction);

                    // Find PPartnerLocation row of the Family that we should process
                    PPartnerLocationRow FamilyPartnerLocationRow = (PPartnerLocationRow)APartnerLocationTable.Rows.Find(
                        new System.Object[] { APartnerKey, APartnerLocationRow.SiteKey,
                                              APartnerLocationRow.LocationKey });

                    if (FamilyPartnerLocationRow != null)
                    {
                        for (int Counter = 0; Counter <= FamilyPersonsDT.Rows.Count - 1; Counter += 1)
                        {
                            PPersonRow ProcessedPersonRow = FamilyPersonsDT[Counter];
//                          TLogging.LogAtLevel(9,  "PerformLocationFamilyMemberPropagationChecks: Person  " + ProcessedPersonRow.PartnerKey.ToString() + ": checking...");

                            // Check if Person doesn't already have the Location
                            if (!PPartnerLocationAccess.Exists(ProcessedPersonRow.PartnerKey, SubmittedLocationPK.SiteKey,
                                    SubmittedLocationPK.LocationKey, ASubmitChangesTransaction))
                            {
                                /*
                                 * PartnerLocation records for family members are added to APartnerLocationTable for easier data handling and
                                 * will be removed again after SubmitChanges of whole dataset but before returning to client as otherwise
                                 * they would confusingly show up on client side.
                                 */

                                // Make sure record is not added more than once to APartnerLocationTable (in case it is not yet in database).
                                if (APartnerLocationTable.Rows.Find(new System.Object[] { ProcessedPersonRow.PartnerKey, SubmittedLocationPK.SiteKey,
                                                                                          SubmittedLocationPK.LocationKey }) == null)
                                {
                                    //                              TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: Person  " + ProcessedPersonRow.PartnerKey.ToString() +
                                    //                                  ": adding Location " + SubmittedLocationPK.LocationKey.ToString() + "...");

                                    // Add a copy of the PartnerLocation data to the Person
                                    PPartnerLocationRow AddPartnerLocationRow = APartnerLocationTable.NewRowTyped(false);
                                    AddPartnerLocationRow.ItemArray = DataUtilities.DestinationSaveItemArray(AddPartnerLocationRow,
                                        FamilyPartnerLocationRow);
                                    AddPartnerLocationRow.PartnerKey = ProcessedPersonRow.PartnerKey;
                                    AddPartnerLocationRow.SiteKey = SubmittedLocationPK.SiteKey;
                                    AddPartnerLocationRow.LocationKey = SubmittedLocationPK.LocationKey;
                                    APartnerLocationTable.Rows.Add(AddPartnerLocationRow);

                                    /*
                                     * If this Person has an PartnerLocation with LocationKey 0 (this
                                     * means that this was the only PartnerLocation so far), delete the
                                     * PartnerLocation with LocationKey 0.
                                     */
                                    if (PPartnerLocationAccess.Exists(ProcessedPersonRow.PartnerKey, SubmittedLocationPK.SiteKey, 0,
                                            ASubmitChangesTransaction))
                                    {
                                        //                                  TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: Person  " + ProcessedPersonRow.PartnerKey.ToString() + ": had Location 0 assigned, deleting it.");
                                        PPartnerLocationAccess.DeleteByPrimaryKey(ProcessedPersonRow.PartnerKey,
                                            APartnerLocationRow.SiteKey,
                                            0,
                                            ASubmitChangesTransaction);
                                    }
                                }
                            }
                            else
                            {
//                              TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: Person  " + ProcessedPersonRow.PartnerKey.ToString() +
//                                      ": already has Location " + SubmittedLocationPK.LocationKey.ToString() + " assigned.");
                            }
                        }
                    }
                    else
                    {
                        throw new EOPAppException(
                            "TPPartnerAddressAggregate.PerformLocationFamilyMemberPropagationChecks: PPartnerLocation record for Family is missing");
                    }
                }
            }
            else
            {
//              TLogging.LogAtLevel(9, "PerformLocationFamilyMemberPropagationChecks: Location " + SubmittedLocationPK.LocationKey.ToString() +
//                  ": Family either has no Family Members, or no propagation of the new Location is wanted. New Location will therefore only be added to the FAMILY.");

                /*
                 * Family either has no Family Members, or it has Members, but the decision
                 * was made that the new PartnerLocation should not be added to the Family
                 * Members.
                 * The new Location will therefore only be added to the FAMILY.
                 */
            }

            return TSubmitChangesResult.scrOK;
        }
Example #9
0
        /// <summary>
        /// Determine location keys for partners needed for extract, depending on if location information
        /// was retrieved by query or not.
        /// </summary>
        /// <param name="APartnerKeysTable"></param>
        /// <param name="APartnerKeyColumn"></param>
        /// <param name="ASiteKeyColumn"></param>
        /// <param name="ALocationKeyColumn"></param>
        /// <param name="APartnerLocationKeysTable"></param>
        /// <param name="ATransaction"></param>
        private static void DetermineBestLocationKeys(
            DataTable APartnerKeysTable,
            Int32 APartnerKeyColumn,
            Int32 ASiteKeyColumn,
            Int32 ALocationKeyColumn,
            out PPartnerLocationTable APartnerLocationKeysTable,
            TDBTransaction ATransaction)
        {
            Int64   PartnerKey;
            Int64   PreviousPartnerKey;
            Int32   NumberOfPartnerRows = APartnerKeysTable.Rows.Count;
            DataRow partnerRow;

            List <TLocationPK> LocationKeyList = new List <TLocationPK>();

            APartnerLocationKeysTable = new PPartnerLocationTable();

            // don't go further if table is empty
            if (APartnerKeysTable.Rows.Count == 0)
            {
                return;
            }

            // If location column exists then check if there is more than one location key for a partner.
            // If so then determine the best of the found addresses.
            if (ALocationKeyColumn >= 0)
            {
                PreviousPartnerKey = -1;

                // Rows are sorted by partner key. Create a list of location keys per partner and determine
                // the best of those addresses.
                for (int ii = NumberOfPartnerRows - 1; ii >= 0; ii--)
                {
                    partnerRow = APartnerKeysTable.Rows[ii];

                    PartnerKey = Convert.ToInt64(partnerRow[APartnerKeyColumn]);

                    if ((PartnerKey != PreviousPartnerKey) &&
                        (PreviousPartnerKey != -1))
                    {
                        if (!DetermineAndAddBestLocationKey(PreviousPartnerKey, LocationKeyList,
                                                            ref APartnerLocationKeysTable, ATransaction))
                        {
                            // if no address could be found then remove this partner
                            APartnerKeysTable.Rows[ii + 1].Delete();
                        }
                        else
                        {
                            // add first location for next partner
                            LocationKeyList.Clear();
                            LocationKeyList.Add(new TLocationPK(Convert.ToInt64(partnerRow[ASiteKeyColumn]),
                                                                Convert.ToInt32(partnerRow[ALocationKeyColumn])));
                        }
                    }
                    else
                    {
                        // add location for this partner
                        LocationKeyList.Add(new TLocationPK(Convert.ToInt64(partnerRow[ASiteKeyColumn]),
                                                            Convert.ToInt32(partnerRow[ALocationKeyColumn])));
                    }

                    // prepare for next round of loop
                    PreviousPartnerKey = PartnerKey;
                }

                // process last partner key after loop through all records
                if (!DetermineAndAddBestLocationKey(PreviousPartnerKey, LocationKeyList,
                                                    ref APartnerLocationKeysTable, ATransaction))
                {
                    // if no address could be found then remove this partner
                    APartnerKeysTable.Rows[0].Delete();
                }
            }
            else
            {
                // If no location information was retrieved with earlier query then find best address
                // for partner.
                for (int ii = NumberOfPartnerRows - 1; ii >= 0; ii--)
                {
                    partnerRow = APartnerKeysTable.Rows[ii];
                    PartnerKey = Convert.ToInt64(partnerRow[APartnerKeyColumn]);

                    if (!DetermineAndAddBestLocationKey(PartnerKey, LocationKeyList,
                                                        ref APartnerLocationKeysTable, ATransaction))
                    {
                        // if no address could be found then remove this partner
                        partnerRow.Delete();
                    }
                }
            }
        }
        public static DataTable GetEventUnits(bool AIncludeConferenceUnits, bool AIncludeOutreachUnits,
                                              string AEventName, bool AIncludeLocationData, bool ACurrentAndFutureEventsOnly)
        {
            TDBTransaction ReadTransaction;
            Boolean        NewTransaction = false;

            List <OdbcParameter> SqlParameterList = new List <OdbcParameter>();

            DataColumn[] Key    = new DataColumn[1];
            DataTable    Events = new DataTable();

            if (AEventName == "*")
            {
                AEventName = "";
            }
            else if (AEventName.EndsWith("*"))
            {
                AEventName = AEventName.Substring(0, AEventName.Length - 1);
            }

            if (TLogging.DL >= 9)
            {
                Console.WriteLine("GetEventUnits called!");
            }

            ReadTransaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.RepeatableRead,
                                                                                TEnforceIsolationLevel.eilMinimum,
                                                                                out NewTransaction);

            try
            {
                string SqlStmt =
                    "SELECT DISTINCT " +
                    PPartnerTable.GetPartnerShortNameDBName() +
                    ", " + PPartnerTable.GetPartnerClassDBName() +
                    ", " + PUnitTable.GetOutreachCodeDBName();

                if (AIncludeLocationData || ACurrentAndFutureEventsOnly)
                {
                    SqlStmt = SqlStmt +
                              ", " + PCountryTable.GetTableDBName() + "." + PCountryTable.GetCountryNameDBName() +
                              ", " + PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetDateEffectiveDBName() +
                              ", " + PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetDateGoodUntilDBName();
                }

                SqlStmt = SqlStmt +
                          ", " + PPartnerTable.GetTableDBName() + "." + PPartnerTable.GetPartnerKeyDBName() +
                          ", " + PUnitTable.GetUnitTypeCodeDBName() +

                          " FROM pub_" + PPartnerTable.GetTableDBName() +
                          ", pub_" + PUnitTable.GetTableDBName();

                if (AIncludeLocationData || ACurrentAndFutureEventsOnly)
                {
                    SqlStmt = SqlStmt +
                              ", pub_" + PLocationTable.GetTableDBName() +
                              ", pub_" + PPartnerLocationTable.GetTableDBName() +
                              ", pub_" + PCountryTable.GetTableDBName();
                }

                SqlStmt = SqlStmt +
                          " WHERE " +
                          PPartnerTable.GetTableDBName() + "." + PPartnerTable.GetPartnerKeyDBName() + " = " +
                          PUnitTable.GetTableDBName() + "." + PUnitTable.GetPartnerKeyDBName() + " AND ";

                if (AIncludeLocationData || ACurrentAndFutureEventsOnly)
                {
                    SqlStmt = SqlStmt +
                              PPartnerTable.GetTableDBName() + "." + PPartnerTable.GetPartnerKeyDBName() + " = " +
                              PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetPartnerKeyDBName() + " AND " +
                              PLocationTable.GetTableDBName() + "." + PLocationTable.GetSiteKeyDBName() + " = " +
                              PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetSiteKeyDBName() + " AND " +
                              PLocationTable.GetTableDBName() + "." + PLocationTable.GetLocationKeyDBName() + " = " +
                              PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetLocationKeyDBName() + " AND " +
                              PCountryTable.GetTableDBName() + "." + PCountryTable.GetCountryCodeDBName() + " = " +
                              PLocationTable.GetTableDBName() + "." + PLocationTable.GetCountryCodeDBName() + " AND ";
                }

                SqlStmt = SqlStmt +
                          PPartnerTable.GetStatusCodeDBName() + " = 'ACTIVE' " + " AND " +
                          PPartnerTable.GetPartnerClassDBName() + " = 'UNIT' ";

                // add criteria for conference and/or outreach
                String ConferenceWhereClause = "(" +
                                               PUnitTable.GetUnitTypeCodeDBName() + " LIKE '%CONF%' OR " +
                                               PUnitTable.GetUnitTypeCodeDBName() + " LIKE '%CONG%')";

                String OutreachWhereClause = PUnitTable.GetOutreachCodeDBName() + " IS NOT NULL AND " +
                                             PUnitTable.GetOutreachCodeDBName() + " <> '' AND (" +
                                             PUnitTable.GetUnitTypeCodeDBName() + " NOT LIKE '%CONF%' AND " +
                                             PUnitTable.GetUnitTypeCodeDBName() + " NOT LIKE '%CONG%')";

                if (AIncludeConferenceUnits &&
                    AIncludeOutreachUnits)
                {
                    SqlStmt = SqlStmt + " AND ((" + ConferenceWhereClause + ") OR (" + OutreachWhereClause + "))";
                }
                else if (AIncludeConferenceUnits)
                {
                    SqlStmt = SqlStmt + " AND (" + ConferenceWhereClause + ")";
                }
                else if (AIncludeOutreachUnits)
                {
                    SqlStmt = SqlStmt + " AND (" + OutreachWhereClause + ")";
                }

                // add criteria for event name filter
                if (AEventName.Length > 0)
                {
                    // in case there is a filter set for the event name
                    AEventName = AEventName.Replace('*', '%') + "%";
                    SqlStmt    = SqlStmt + " AND " + PUnitTable.GetUnitNameDBName() +
                                 " LIKE '" + AEventName + "'";
                }

                if (ACurrentAndFutureEventsOnly)
                {
                    SqlStmt = SqlStmt + " AND " + PPartnerLocationTable.GetDateGoodUntilDBName() + " >= ?";

                    SqlParameterList.Add(new OdbcParameter("param_date", OdbcType.Date)
                    {
                        Value = DateTime.Today.Date
                    });
                }

                // sort rows according to name
                SqlStmt = SqlStmt + " ORDER BY " + PPartnerTable.GetPartnerShortNameDBName();

                Events = DBAccess.GDBAccessObj.SelectDT(SqlStmt, "events",
                                                        ReadTransaction, SqlParameterList.ToArray());

                Key[0]            = Events.Columns[PPartnerTable.GetPartnerKeyDBName()];
                Events.PrimaryKey = Key;
            }
            catch (Exception e)
            {
                TLogging.Log(e.ToString());
            }
            finally
            {
                if (NewTransaction)
                {
                    DBAccess.GDBAccessObj.CommitTransaction();

                    if (TLogging.DL >= 7)
                    {
                        Console.WriteLine("GetEventUnits: committed own transaction.");
                    }
                }
            }

            return(Events);
        }
Example #11
0
        /// <summary>
        /// Creates a list of locations for a given Partner.
        /// </summary>
        /// <remarks>Corresponds in parts with with Progress 4GL Method
        /// 'GetPartnerLocations' in partner/maillib.p.</remarks>
        /// <param name="APartnerKey">PartnerKey of the Partner being processed.</param>
        /// <param name="AMailingAddressesOnly">If true: only include addresses with mailing flag set.</param>
        /// <param name="AIncludeCurrentAddresses">If true: include current addresses.</param>
        /// <param name="AIncludeFutureAddresses">If true: include future addresses.</param>
        /// <param name="AIncludeExpiredAddresses">If true: include expired addresses.</param>
        /// <param name="APartnerLocations">The Locations of the Partner being processed.</param>
        /// <returns>False if an invalid PartnerKey was passed in or if Petra Security
        /// denied access to the Partner, otherwise true.</returns>
        public static bool GetPartnerLocations(Int64 APartnerKey,
            bool AMailingAddressesOnly,
            bool AIncludeCurrentAddresses,
            bool AIncludeFutureAddresses,
            bool AIncludeExpiredAddresses,
            out PPartnerLocationTable APartnerLocations)
        {
            TDBTransaction ReadTransaction;
            Boolean NewTransaction;

            String SelectSQL;
            DataSet FillDataSet;

            OdbcParameter param;

            // Initialise out Argument
            APartnerLocations = null;

            if (APartnerKey > 0)
            {
                TLogging.LogAtLevel(8, "TMailing.GetPartnerLocations: Checking access to Partner.");

                if (TSecurity.CanAccessPartnerByKey(APartnerKey, false) ==
                    TPartnerAccessLevelEnum.palGranted)
                {
                    ReadTransaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(
                        IsolationLevel.ReadCommitted,
                        TEnforceIsolationLevel.eilMinimum,
                        out NewTransaction);

                    // Load Partner Locations, taking passed in restrictions into account.
                    try
                    {
                        SelectSQL =
                            "SELECT *" +
                            "  FROM PUB_" + PPartnerLocationTable.GetTableDBName() +
                            " WHERE " + PPartnerLocationTable.GetPartnerKeyDBName() + " = ?" +
                            "   AND (NOT ? = true OR (? = true AND " + PPartnerLocationTable.GetSendMailDBName() + " = true))" +
                            "   AND ((? = true AND ((" + PPartnerLocationTable.GetDateEffectiveDBName() + " <= ?" +
                            "          OR " + PPartnerLocationTable.GetDateEffectiveDBName() + " IS NULL)" +
                            "     AND (" + PPartnerLocationTable.GetDateGoodUntilDBName() + " >= ?" +
                            "          OR " + PPartnerLocationTable.GetDateGoodUntilDBName() + " IS NULL)))" +
                            "     OR (? = true AND " + PPartnerLocationTable.GetDateEffectiveDBName() + " > ?)" +
                            "     OR (? = true AND " + PPartnerLocationTable.GetDateGoodUntilDBName() + " < ?))";

                        List <OdbcParameter>parameters = new List <OdbcParameter>();
                        param = new OdbcParameter("PartnerKey", OdbcType.Decimal, 10);
                        param.Value = APartnerKey;
                        parameters.Add(param);
                        param = new OdbcParameter("MailingAddressOnly1", OdbcType.Bit);
                        param.Value = AMailingAddressesOnly;
                        parameters.Add(param);
                        param = new OdbcParameter("MailingAddressOnly2", OdbcType.Bit);
                        param.Value = AMailingAddressesOnly;
                        parameters.Add(param);
                        param = new OdbcParameter("IncludeCurrentAddresses", OdbcType.Bit);
                        param.Value = AIncludeCurrentAddresses;
                        parameters.Add(param);
                        param = new OdbcParameter("TodaysDate1", OdbcType.Date);
                        param.Value = DateTime.Now;
                        parameters.Add(param);
                        param = new OdbcParameter("TodaysDate2", OdbcType.Date);
                        param.Value = DateTime.Now;
                        parameters.Add(param);
                        param = new OdbcParameter("IncludeFutureAddresses", OdbcType.Bit);
                        param.Value = AIncludeFutureAddresses;
                        parameters.Add(param);
                        param = new OdbcParameter("TodaysDate3", OdbcType.Date);
                        param.Value = DateTime.Now;
                        parameters.Add(param);
                        param = new OdbcParameter("IncludeExpiredAddresses", OdbcType.Bit);
                        param.Value = AIncludeExpiredAddresses;
                        parameters.Add(param);
                        param = new OdbcParameter("TodaysDate4", OdbcType.Date);
                        param.Value = DateTime.Now;
                        parameters.Add(param);

                        /*
                         * Our out Argument 'APartnerLocations' is a Typed DataTable, but SelectDT
                         * returns an untyped DataTable, therefore we need to create a Typed DataTable
                         * that contains the data of the returned untyped DataTable!
                         */
                        FillDataSet = new DataSet();
                        APartnerLocations = new PPartnerLocationTable(PPartnerLocationTable.GetTableDBName());
                        FillDataSet.Tables.Add(APartnerLocations);

                        DBAccess.GDBAccessObj.Select(FillDataSet, SelectSQL,
                            PPartnerLocationTable.GetTableDBName(),
                            ReadTransaction,
                            parameters.ToArray());
//                      TLogging.LogAtLevel(7, "TMailing.GetPartnerLocations:  FillDataSet.Tables.Count: " + FillDataSet.Tables.Count.ToString());
                        FillDataSet.Tables.Remove(APartnerLocations);

                        if (APartnerLocations.Rows.Count > 0)
                        {
//                          TLogging.LogAtLevel(7, "TMailing.GetPartnerLocations: Found " + APartnerLocations.Rows.Count.ToString() + " PartnerLocations found for Partner " + APartnerKey.ToString() + ".");
                        }
                        else
                        {
                            /*
                             * /* No Rows returned = no PartnerLocations for Partner.
                             * That shouldn't happen with existing Partners, but if it does (eg. non-existing
                             * PartnerKey passed in) we return an empty Typed DataTable.
                             */
//                          TLogging.LogAtLevel(7, "TMailing.GetPartnerLocations: No PartnerLocations found for Partner " + APartnerKey.ToString() + "!");
                            APartnerLocations = new PPartnerLocationTable();
                        }
                    }
                    finally
                    {
                        if (NewTransaction)
                        {
                            DBAccess.GDBAccessObj.CommitTransaction();
                            TLogging.LogAtLevel(7, "TMailing.GetPartnerLocations: committed own transaction.");
                        }
                    }

                    return true;
                }
                else
                {
                    TLogging.LogAtLevel(8, "TMailing.GetPartnerLocations: Access to Partner DENIED!");

                    // Petra Security prevents us from accessing this Partner -> return false;
                    return false;
                }
            }
            else
            {
                // Invalid PartnerKey -> return false;
                return false;
            }
        }
Example #12
0
        /// <summary>
        /// Find the best address of a partner
        /// </summary>
        /// <param name="APartnerKey">Partner key</param>
        /// <param name="ASituation">describing the current state of the report generator</param>
        /// <param name="AAddressRow">best address</param>
        /// <returns>true if a best address was found, otherwise false</returns>
        public static bool GetPartnerBestAddressRow(long APartnerKey, TRptSituation ASituation, out PPartnerLocationRow AAddressRow)
        {
            bool FoundBestAddress = false;

            AAddressRow = null;
            PPartnerLocationTable PartnerLocationTable;

            PartnerLocationTable = new PPartnerLocationTable();

            // add special column BestAddress and Icon
            PartnerLocationTable.Columns.Add(new System.Data.DataColumn("BestAddress", typeof(Boolean)));
            PartnerLocationTable.Columns.Add(new System.Data.DataColumn("Icon", typeof(Int32)));

            // find all locations of the partner, put it into a dataset
            PartnerLocationTable = PPartnerLocationAccess.LoadViaPPartner(APartnerKey, ASituation.GetDatabaseConnection().Transaction);

            // uses Ict.Petra.Shared.MPartner.Calculations.pas, DetermineBestAddress
            Calculations.DeterminePartnerLocationsDateStatus(PartnerLocationTable, DateTime.Today);
            Calculations.DetermineBestAddress(PartnerLocationTable);

            foreach (PPartnerLocationRow row in PartnerLocationTable.Rows)
            {
                // find the row with BestAddress = 1
                if (Convert.ToInt32(row["BestAddress"]) == 1)
                {
                    AAddressRow = row;
                    FoundBestAddress = true;
                }
            }

            return FoundBestAddress;
        }
Example #13
0
        /// <summary>
        /// Gets the 'Primary Email Address' and some location details of the 'Best Address'.
        /// </summary>
        /// <returns>The 'Primary Email Address', or <see cref="String.Empty" /> in case the Partner hasn't got one.</returns>
        public static string GetBestEmailAddressWithDetails(Int64 APartnerKey, out PLocationTable AAddress, out string ACountryNameLocal)
        {
            string         EmailAddress     = String.Empty;
            PLocationTable Address          = null;
            string         CountryNameLocal = "";
            TDBTransaction Transaction      = null;
            bool           FoundBestAddress = false;

            DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.ReadCommitted,
                                                                      TEnforceIsolationLevel.eilMinimum,
                                                                      ref Transaction,
                                                                      delegate
            {
                DataSet PartnerLocationsDS = new DataSet();

                PartnerLocationsDS.Tables.Add(new PPartnerLocationTable());
                PartnerLocationsDS.Tables.Add(new PCountryTable());
                DataTable PartnerLocationTable = PartnerLocationsDS.Tables[PPartnerLocationTable.GetTableName()];
                PCountryTable CountryTable     = (PCountryTable)PartnerLocationsDS.Tables[PCountryTable.GetTableName()];
                CountryTable.DefaultView.Sort  = PCountryTable.GetCountryCodeDBName();

                // add special column BestAddress and Icon
                PartnerLocationTable.Columns.Add(new System.Data.DataColumn("BestAddress", typeof(Boolean)));
                PartnerLocationTable.Columns.Add(new System.Data.DataColumn("Icon", typeof(Int32)));

                // find all locations of the partner, put it into a dataset
                PPartnerLocationAccess.LoadViaPPartner(PartnerLocationsDS, APartnerKey, Transaction);

                Ict.Petra.Shared.MPartner.Calculations.DeterminePartnerLocationsDateStatus(PartnerLocationsDS);
                Ict.Petra.Shared.MPartner.Calculations.DetermineBestAddress(PartnerLocationsDS);

                foreach (PPartnerLocationRow row in PartnerLocationTable.Rows)
                {
                    // find the row with BestAddress = 1
                    if (Convert.ToInt32(row["BestAddress"]) == 1)
                    {
                        FoundBestAddress = true;

                        // we also want the post address, need to load the p_location table:
                        Address = PLocationAccess.LoadByPrimaryKey(row.SiteKey, row.LocationKey, Transaction);

                        if (CountryTable.DefaultView.Find(Address[0].CountryCode) == -1)
                        {
                            CountryTable.Merge(PCountryAccess.LoadByPrimaryKey(Address[0].CountryCode, Transaction));
                        }

                        CountryNameLocal = CountryTable[CountryTable.DefaultView.Find(Address[0].CountryCode)].CountryNameLocal;
                    }
                }

                if (FoundBestAddress)
                {
                    if (!TContactDetailsAggregate.GetPrimaryEmailAddress(APartnerKey, out EmailAddress))
                    {
                        EmailAddress = String.Empty;
                    }
                }
            });

            AAddress          = Address;
            ACountryNameLocal = CountryNameLocal;

            return(EmailAddress);
        }
        /// <summary>
        /// Determines which address is the 'Best Address' of a Partner, and marks it in the DataColumn 'BestAddress'.
        /// </summary>
        /// <remarks>There are convenient overloaded server-side Methods, Ict.Petra.Server.MPartner.ServerCalculations.DetermineBestAddress,
        /// which work by specifying the PartnerKey of a Partner in an Argument.</remarks>
        /// <param name="APartnerLocationsDT">DataTable containing the addresses of a Partner.</param>
        /// <returns>A <see cref="TLocationPK" /> which points to the 'Best Address'. If no 'Best Address' was found,
        /// SiteKey and LocationKey of this instance will be both -1.</returns>
        public static TLocationPK DetermineBestAddress(DataTable APartnerLocationsDT)
        {
            TLocationPK ReturnValue;

            DataRow[]    OrderedRows;
            System.Int32 CurrentRow;
            System.Int32 BestRow;
            System.Int16 FirstRowAddrOrder;
            bool         FirstRowMailingAddress;

            System.DateTime BestRowDate;
            System.DateTime TempDate;
            CurrentRow = 0;
            BestRow    = 0;
            bool Unchanged = false;

            TLogging.LogAtLevel(8, "Calculations.DetermineBestAddress: processing " + APartnerLocationsDT.Rows.Count.ToString() + " rows...");

            if (APartnerLocationsDT == null)
            {
                throw new ArgumentException("Argument APartnerLocationsDT must not be null");
            }

            if (!APartnerLocationsDT.Columns.Contains(PARTNERLOCATION_BESTADDR_COLUMN))
            {
                DeterminePartnerLocationsDateStatus(APartnerLocationsDT, DateTime.Today);
            }

            /*
             *  Add custom DataColumn if its not part of the DataTable yet
             */
            if (!APartnerLocationsDT.Columns.Contains(PARTNERLOCATION_BESTADDR_COLUMN))
            {
                APartnerLocationsDT.Columns.Add(new System.Data.DataColumn(PARTNERLOCATION_BESTADDR_COLUMN, typeof(Boolean)));
            }

            /*
             * Order tables' rows: first all records with p_send_mail_l = true, these are ordered
             * ascending by Icon, then all records with p_send_mail_l = false, these are ordered
             * ascending by Icon.
             */
            OrderedRows = APartnerLocationsDT.Select(APartnerLocationsDT.DefaultView.RowFilter,
                                                     PPartnerLocationTable.GetSendMailDBName() + " DESC, " + PartnerEditTDSPPartnerLocationTable.GetIconDBName() + " ASC",
                                                     DataViewRowState.CurrentRows);

            if (OrderedRows.Length > 1)
            {
                FirstRowAddrOrder      = Convert.ToInt16(OrderedRows[0][PartnerEditTDSPPartnerLocationTable.GetIconDBName()]);
                FirstRowMailingAddress = Convert.ToBoolean(OrderedRows[0][PPartnerLocationTable.GetSendMailDBName()]);

                // determine pBestRowDate
                if (FirstRowAddrOrder != 3)
                {
                    BestRowDate = TSaveConvert.ObjectToDate(OrderedRows[CurrentRow][PPartnerLocationTable.GetDateEffectiveDBName()]);
                }
                else
                {
                    BestRowDate = TSaveConvert.ObjectToDate(OrderedRows[CurrentRow][PPartnerLocationTable.GetDateGoodUntilDBName()]);
                }

                // iterate through the sorted rows
                for (CurrentRow = 0; CurrentRow <= OrderedRows.Length - 1; CurrentRow += 1)
                {
                    Unchanged = OrderedRows[CurrentRow].RowState == DataRowState.Unchanged;

                    // reset any row that might have been marked as 'best' before
                    OrderedRows[CurrentRow][PartnerEditTDSPPartnerLocationTable.GetBestAddressDBName()] = ((object)0);

                    // We do not want changing the BestAddress column to enable save. So revert row status to original.
                    if (Unchanged)
                    {
                        OrderedRows[CurrentRow].AcceptChanges();
                    }

                    // determine pTempDate
                    if (FirstRowAddrOrder != 3)
                    {
                        TempDate = TSaveConvert.ObjectToDate(OrderedRows[CurrentRow][PPartnerLocationTable.GetDateEffectiveDBName()]);
                    }
                    else
                    {
                        TempDate = TSaveConvert.ObjectToDate(OrderedRows[CurrentRow][PPartnerLocationTable.GetDateGoodUntilDBName()]);
                    }

                    // still the same ADDR_ORDER than the ADDR_ORDER of the first row and
                    // still the same Mailing Address than the Mailing Address flag of the first row > proceed
                    if ((Convert.ToInt16(OrderedRows[CurrentRow][PartnerEditTDSPPartnerLocationTable.GetIconDBName()]) == FirstRowAddrOrder) &&
                        (Convert.ToBoolean(OrderedRows[CurrentRow][PPartnerLocationTable.GetSendMailDBName()]) == FirstRowMailingAddress))
                    {
                        switch (FirstRowAddrOrder)
                        {
                        case 1:
                        case 3:

                            // find the Row with the highest p_date_effective_d (or p_date_good_until_d) date
                            if (TempDate > BestRowDate)
                            {
                                BestRowDate = TempDate;
                                BestRow     = CurrentRow;
                            }

                            break;

                        case 2:

                            // find the Row with the lowest p_date_effective_d date
                            if (TempDate < BestRowDate)
                            {
                                BestRowDate = TempDate;
                                BestRow     = CurrentRow;
                            }

                            break;
                        }
                    }
                }

                Unchanged = OrderedRows[0].RowState == DataRowState.Unchanged;

                // mark the location that was determined to be the 'best'
                OrderedRows[BestRow][PartnerEditTDSPPartnerLocationTable.GetBestAddressDBName()] = ((object)1);

                // We do not want changing the BestAddress column to enable save. So revert row status to original.
                if (Unchanged)
                {
                    OrderedRows[0].AcceptChanges();
                }

                ReturnValue =
                    new TLocationPK(Convert.ToInt64(OrderedRows[BestRow][PLocationTable.GetSiteKeyDBName()]),
                                    Convert.ToInt32(OrderedRows[BestRow][PLocationTable.GetLocationKeyDBName()]));
            }
            else
            {
                if (OrderedRows.Length == 1)
                {
                    Unchanged = OrderedRows[0].RowState == DataRowState.Unchanged;

                    // mark the only location to be the 'best'
                    OrderedRows[0][PartnerEditTDSPPartnerLocationTable.GetBestAddressDBName()] = ((object)1);

                    // We do not want changing the BestAddress column to enable save. So revert row status to original.
                    if (Unchanged)
                    {
                        OrderedRows[0].AcceptChanges();
                    }

                    ReturnValue = new TLocationPK(Convert.ToInt64(OrderedRows[0][PLocationTable.GetSiteKeyDBName()]),
                                                  Convert.ToInt32(OrderedRows[0][PLocationTable.GetLocationKeyDBName()]));
                }
                else
                {
                    ReturnValue = new TLocationPK();
                }
            }

            return(ReturnValue);
        }
Example #15
0
 /// <summary>
 /// Creates a new DataRow in the passed PLocation and PPartnerLocation tables.
 /// </summary>
 /// <param name="ALocationDT">Typed PLocation table. If null is passed in it is created
 /// automatically</param>
 /// <param name="APartnerLocationDT">Typed PPartnerLocation table. If null is passed in
 /// it is created automatically</param>
 /// <param name="APartnerKey">PartneKey of the Partner for which the Address should
 /// be created</param>
 /// <param name="APartnerClass">PartnerClass of the Partner for which the Address should
 /// be created</param>
 /// <param name="ACountryCode"></param>
 /// <param name="ANewLocationKey">A LocationKey that the new Location and
 /// PartnerLocation rows would be set to.</param>
 /// <param name="ACopyFromFamilyPartnerKey">Pass in the PartnerKey of a Family Partner
 /// to copy over all data from the Family's Address. Note: the DataRow
 /// specified with ACopyFromFamilyPartnerKey must be present in the
 /// APartnerLocationDT table!</param>
 /// <param name="ACopyFromFamilyLocationKey">Pass in the LocationKey of the Family
 /// Partner specified in ACopyFromFamilyPartnerKey to copy over all data from
 /// the Family's Address. Note: the DataRow specified with
 /// ACopyFromFamilyLocationKey must be present in both the ALocationDT and
 /// APartnerLocationDT table!</param>
 /// <param name="ACopyFromFamilySiteKey">A SiteKey to find the location that should be the source of the copy</param>
 /// <param name="ACopyFromFamilyOnlyLocation"></param>
 /// <param name="ADeleteDataRowCopiedFrom"></param>
 /// <exception cref="ArgumentException"> if any of the Arguments (or their combination) is
 /// not valid.</exception>
 public static void CreateNewAddress(PLocationTable ALocationDT,
     PPartnerLocationTable APartnerLocationDT,
     Int64 APartnerKey,
     TPartnerClass APartnerClass,
     String ACountryCode,
     Int32 ANewLocationKey,
     Int64 ACopyFromFamilyPartnerKey,
     Int32 ACopyFromFamilyLocationKey,
     Int64 ACopyFromFamilySiteKey,
     Boolean ACopyFromFamilyOnlyLocation,
     Boolean ADeleteDataRowCopiedFrom)
 {
     CreateNewAddressInternal(ALocationDT,
         APartnerLocationDT,
         APartnerKey,
         APartnerClass,
         ACountryCode,
         ANewLocationKey,
         ACopyFromFamilyPartnerKey,
         ACopyFromFamilyLocationKey,
         ACopyFromFamilySiteKey,
         ACopyFromFamilyOnlyLocation,
         ADeleteDataRowCopiedFrom);
 }
Example #16
0
        /// <summary>
        /// Creates a new DataRow in the passed PLocation and PPartnerLocation tables.
        /// </summary>
        /// <param name="ALocationDT">Typed PLocation table. If null is passed in it is created
        /// automatically</param>
        /// <param name="APartnerLocationDT">Typed PPartnerLocation table. If null is passed in
        /// it is created automatically</param>
        /// <param name="APartnerKey">PartneKey of the Partner for which the Address should
        /// be created</param>
        /// <param name="APartnerClass">PartnerClass of the Partner for which the Address should
        /// be created</param>
        /// <param name="ACountryCode"></param>
        /// <param name="ANewLocationKey">A LocationKey that the new Location and
        /// PartnerLocation rows would be set to.</param>
        /// <param name="ACopyFromFamilyPartnerKey">Pass in the PartnerKey of a Family Partner
        /// to copy over all data from the Family's Address
        /// (ACopyFromPartnerLocationKey must be -1 to be legal). Default: -1
        /// (=no copy). Note: the DataRow specified with ACopyFromFamilyPartnerKey must
        /// be present in the APartnerLocationDT table!</param>
        /// <param name="ACopyFromFamilyLocationKey">Pass in the LocationKey of the Family
        /// Partner specified in ACopyFromFamilyPartnerKey to copy over all data from
        /// the Family's Address (ACopyFromPartnerLocationKey must be -1 to be legal)
        /// Default: -1 (=no copy). Note: the DataRow specified with
        /// ACopyFromFamilyLocationKey must be present in both the ALocationDT and
        /// APartnerLocationDT table!</param>
        /// <param name="ACopyFromFamilySiteKey">A SiteKey to find the location that should be the
        /// source of the copy</param>
        /// <param name="ACopyFromFamilyOnlyLocation"></param>
        /// <param name="ADeleteDataRowCopiedFrom"></param>
        /// <exception cref="ArgumentException">If any of the Arguments (or their combination) is
        /// not valid.</exception>
        public static void CreateNewAddressInternal(PLocationTable ALocationDT,
            PPartnerLocationTable APartnerLocationDT,
            Int64 APartnerKey,
            TPartnerClass APartnerClass,
            String ACountryCode,
            Int32 ANewLocationKey,
            Int64 ACopyFromFamilyPartnerKey,
            Int32 ACopyFromFamilyLocationKey,
            Int64 ACopyFromFamilySiteKey,
            Boolean ACopyFromFamilyOnlyLocation,
            Boolean ADeleteDataRowCopiedFrom)
        {
            PLocationRow NewLocationsRow;
            PLocationRow CopyLocationsRow;
            PPartnerLocationRow NewPartnerLocationRow;
            PPartnerLocationRow CopyPartnerLocationRow;

            #region Check parameters

            if (ALocationDT == null)
            {
                ALocationDT = new PLocationTable();
            }

            if (APartnerLocationDT == null)
            {
                APartnerLocationDT = new PPartnerLocationTable();
            }

            if (TStaticDataTables.TMPartner.GetStaticTable(TStaticPartnerTablesEnum.PartnerClassList).Rows.Find(APartnerClass) == null)
            {
                throw new ArgumentException("APartnerClass needs to be a valid Partner Class");
            }

            if ((ACopyFromFamilyPartnerKey != -1) || (ACopyFromFamilyLocationKey != -99))
            {
                if (ACopyFromFamilyPartnerKey != -1)
                {
                    if (ACopyFromFamilyLocationKey == -99)
                    {
                        throw new ArgumentException("ACopyFromFamilyLocationKey needs to be specified if ACopyFromFamilyPartnerKey is specified");
                    }
                }

                if (ACopyFromFamilyLocationKey != -99)
                {
                    if (ACopyFromFamilyPartnerKey == -1)
                    {
                        throw new ArgumentException("ACopyFromFamilyPartnerKey needs to be specified if ACopyFromFamilyLocationKey is specified");
                    }
                }
            }

            #endregion

            /*
             * Add new Locations row
             */
            NewLocationsRow = ALocationDT.NewRowTyped(true);

            // Assign Primary Key columns
            NewLocationsRow.SiteKey = SharedConstants.FIXED_SITE_KEY; // TODO: use s_system_parameter.s_site_key_n once p_partner_location actually uses the Petra System SiteKey in the PrimaryKey (instead of 0, which is used currently)
            NewLocationsRow.LocationKey = ANewLocationKey;

            // Copy over Columns of the Row specified with ACopyFromFamilyLocationKey?
            if (ACopyFromFamilyLocationKey != -99)
            {
                CopyLocationsRow = (PLocationRow)ALocationDT.Rows.Find(new Object[] { ACopyFromFamilySiteKey, ACopyFromFamilyLocationKey });

                if (CopyLocationsRow != null)
                {
                    if (ACountryCode != "")
                    {
                        throw new ArgumentException(
                            "ACountryCode must not be specified because it would be overwritten by the value from the row specified with ACopyFromFamilyLocationKey");
                    }

                    CopyLocationData(CopyLocationsRow, NewLocationsRow);

                    if (ADeleteDataRowCopiedFrom)
                    {
                        CopyLocationsRow.Delete();
                        CopyLocationsRow.AcceptChanges();
                    }
                }
                else
                {
                    throw new ArgumentException("Row with specified ACopyFromFamilyLocationKey cannot be found in PLocations DataTable!");
                }
            }
            else
            {
                // No copying > assign values of columns manually
                NewLocationsRow.CountryCode = ACountryCode;
            }

            NewLocationsRow.CreatedBy = UserInfo.GUserInfo.UserID;
            ALocationDT.Rows.Add(NewLocationsRow);

            // If an existing Location shouldn't be copied to a temp LocationKey (<0) then make this DataRow unchanged
            // (prevents this row from getting commited to the PetraServer).
            if (ANewLocationKey >= 0)
            {
                NewLocationsRow.AcceptChanges();
            }

            /*
             * Add new PartnerLocations row
             */
            NewPartnerLocationRow = APartnerLocationDT.NewRowTyped(true);

            // Assign Primary Key columns
            NewPartnerLocationRow.PartnerKey = APartnerKey;
            NewPartnerLocationRow.SiteKey = SharedConstants.FIXED_SITE_KEY; // TODO: use s_system_parameter.s_site_key_n once p_partner_location actually uses the Petra System SiteKey in the PrimaryKey (instead of 0, which is used currently)
            NewPartnerLocationRow.LocationKey = ANewLocationKey;

            // Copy over Columns of the Row specified with ACopyFromFamilyPartnerKey
            // and ACopyFromFamilyLocationKey?
            if ((!ACopyFromFamilyOnlyLocation) && (ACopyFromFamilyLocationKey != -99))
            {
                CopyPartnerLocationRow =
                    (PPartnerLocationRow)APartnerLocationDT.Rows.Find(new Object[] { ACopyFromFamilyPartnerKey, ACopyFromFamilySiteKey,
                                                                                     ACopyFromFamilyLocationKey });

                if (CopyPartnerLocationRow != null)
                {
                    // Security check for LocationType SECURITY_CAN_LOCATIONTYPE
                    if (((CopyPartnerLocationRow.LocationType.EndsWith(SharedConstants.SECURITY_CAN_LOCATIONTYPE)
                          && (UserInfo.GUserInfo.IsInGroup(SharedConstants.PETRAGROUP_ADDRESSCAN))))
                        || (!((CopyPartnerLocationRow.LocationType.EndsWith(SharedConstants.SECURITY_CAN_LOCATIONTYPE)))))
                    {
                        // Copy over all columns, except the Primary Key columns and the last four
                        // columns (containing creation and change information)
                        NewPartnerLocationRow.LocationType = CopyPartnerLocationRow.LocationType;

                        if (!CopyPartnerLocationRow.IsDateGoodUntilNull())
                        {
                            // Copy over DateGoodUntil only if it doesn't lie in the past
                            if (CopyPartnerLocationRow.DateGoodUntil >= DateTime.Today)
                            {
                                NewPartnerLocationRow.DateGoodUntil = CopyPartnerLocationRow.DateGoodUntil;
                            }
                        }

                        if (ADeleteDataRowCopiedFrom)
                        {
                            CopyPartnerLocationRow.Delete();
                            CopyPartnerLocationRow.AcceptChanges();
                        }
                    }
                    else
                    {
                        throw new ESecurityGroupAccessDeniedException(
                            "Address copying denied: " + "The Location Type of the Address that should be copied is '" +
                            CopyPartnerLocationRow.LocationType + "' but you are not in Security Group '" + SharedConstants.PETRAGROUP_ADDRESSCAN +
                            "'!");
                    }
                }
                else
                {
                    throw new ArgumentException("Row with specified ACopyFromFamilyPartnerKey cannot be found in PPartnerLocations DataTable!");
                }
            }
            else
            {
                NewPartnerLocationRow.LocationType = TSharedAddressHandling.GetDefaultLocationType(APartnerClass);
            }

            NewPartnerLocationRow.DateEffective = DateTime.Today;
            NewPartnerLocationRow.CreatedBy = UserInfo.GUserInfo.UserID;

            if (APartnerClass == TPartnerClass.PERSON)
            {
                NewPartnerLocationRow.SendMail = false;
            }
            else
            {
                NewPartnerLocationRow.SendMail = true;
            }

            APartnerLocationDT.Rows.Add(NewPartnerLocationRow);
        }
        /// <summary>
        /// todoComment
        ///
        /// @comment Must only be called for Partners of Partner Class FAMILY - the
        /// function does no checks on that and will fail for other Partner Classes!
        /// </summary>
        /// <param name="APartnerLocationRow"></param>
        /// <param name="APartnerKey"></param>
        /// <param name="AResponseDS"></param>
        /// <param name="ASubmitChangesTransaction"></param>
        /// <param name="AAddressChangedPromotionDT"></param>
        /// <param name="AChangeLocationParametersDT"></param>
        /// <param name="APartnerLocationTable"></param>
        /// <param name="AVerificationResult"></param>
        /// <returns></returns>
        private static TSubmitChangesResult PerformPartnerLocationChangeChecks(PPartnerLocationRow APartnerLocationRow,
            Int64 APartnerKey,
            ref PartnerAddressAggregateTDS AResponseDS,
            TDBTransaction ASubmitChangesTransaction,
            ref PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable AAddressChangedPromotionDT,
            ref PartnerAddressAggregateTDSChangePromotionParametersTable AChangeLocationParametersDT,
            ref PPartnerLocationTable APartnerLocationTable,
            ref TVerificationResultCollection AVerificationResult)
        {
            TSubmitChangesResult ReturnValue;
            DataView PropagateLocationParametersDV;
            DataView PropagateLocationParametersProcessedDV;
            Boolean UpdatePartnerLocations;

            Int64[, ] UpdatePartnerLocationOtherPersons;
            OdbcParameter[] ParametersArray;
            DataSet PartnerLocationModifyDS;
            StringCollection ChangedFieldsColl;
            String[] ChangedFieldsArr;
            Int32 Counter;
            Int32 Counter2;
            Int32 Counter3;
            Int32 Counter4;
            PPartnerLocationTable PartnerLocationModificationDT;
//          TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: AAddressChangedPromotionDT.Rows.Count: " + AAddressChangedPromotionDT.Rows.Count.ToString());

            UpdatePartnerLocations = false;

            if (CheckPartnerLocationChange(APartnerLocationRow, APartnerKey, ref AAddressChangedPromotionDT, ASubmitChangesTransaction,
                    out UpdatePartnerLocations, out UpdatePartnerLocationOtherPersons, ref AChangeLocationParametersDT))
            {
                // Check if there is a Parameter Row for the LocationKey we are looking at
                PropagateLocationParametersDV = new DataView(AAddressChangedPromotionDT,
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetSiteKeyDBName() + " = " +
                    APartnerLocationRow[PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetSiteKeyDBName(),
                                        DataRowVersion.Original].ToString() + " AND " +
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationKeyDBName() +
                    " = " +
                    APartnerLocationRow[PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationKeyDBName(),
                                        DataRowVersion.Original].ToString() + " AND " +
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetPartnerLocationChangeDBName() + " = true AND " +
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetAnswerProcessedClientSideDBName() + " = false",
                    "",
                    DataViewRowState.CurrentRows);

                // APartnerLocationRow.SiteKey.ToString
                // APartnerLocationRow.LocationKey.ToString
                if (PropagateLocationParametersDV.Count > 0)
                {
//                  TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: PartnerLocation " + APartnerLocationRow.LocationKey.ToString() +
//                          ": PartnerLocation has been changed, decision on propagation is needed.");

                    /*
                     * More information is needed (usually via user interaction)
                     * -> stop processing here and return parameters
                     * (usually used for UI interaction)
                     */
                    if (AResponseDS == null)
                    {
//                      TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: Creating AResponseDS.");
                        AResponseDS = new PartnerAddressAggregateTDS(MPartnerConstants.PARTNERADDRESSAGGREGATERESPONSE_DATASET);
                    }

//                  TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: AAddressAddedPromotionDT.Rows.Count: " + AAddressChangedPromotionDT.Rows.Count.ToString());
                    AResponseDS.Merge(AAddressChangedPromotionDT);

//                  TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: Merged AAddressAddedPromotionDT into AResponseDS.");
                    AResponseDS.Merge(AChangeLocationParametersDT);
//                  TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: Merged ChangePromotionParametersDT into AResponseDS.");
//                  TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: AResponseDS.Tables[" + MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME +
//                        "].Rows.Count: " + AResponseDS.Tables[MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME].Rows.Count.ToString());
                    ReturnValue = TSubmitChangesResult.scrInfoNeeded;
                    return ReturnValue;
                }
                else
                {
                    /*
                     * NOTE: If there are no Family Members, CheckPartnerLocationChange will
                     * return true, but the PropagateLocationParametersDV.Count will be 0, so
                     * the processing will also go here and find UpdatePartnerLocations false
                     * (default assigned at beginning), so nothing will happen.
                     */
                    if (UpdatePartnerLocations)
                    {
//                      TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: User made his/her choice regarding PartnerLocation Change promotion; now processing...");

                        /*
                         * User made his/her choice regarding PartnerLocation Change promotion;
                         * now process it
                         */
//                      TLogging.LogAtLevel(9,  "PerformPartnerLocationChangeChecks: Updated FAMILY PartnerLocation " + APartnerLocationRow.LocationKey.ToString() +
//                            ": changes should be assigned to " + Convert.ToInt32(UpdatePartnerLocationOtherPersons.GetLength(0)).ToString() + " PartnerLocations of PERSONs...");

                        PartnerLocationModifyDS = null;

                        // Find associated Parameter Row
                        PropagateLocationParametersProcessedDV = new DataView(
                            AAddressChangedPromotionDT, PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetSiteKeyDBName() + " = " +
                            APartnerLocationRow[PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetSiteKeyDBName(),
                                                DataRowVersion.Original].ToString() + " AND " +
                            PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationKeyDBName() + " = " +
                            APartnerLocationRow[PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationKeyDBName(),
                                                DataRowVersion.Original].ToString() + " AND " +
                            PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetPartnerLocationChangeDBName() +
                            " = true AND " +
                            PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetAnswerProcessedClientSideDBName() +
                            " = true", "",
                            DataViewRowState.CurrentRows);

                        // APartnerLocationRow.SiteKey.ToString
                        // APartnerLocationRow.LocationKey.ToString

                        /*
                         * Create a Collection by splitting String 'ChangedFields' from the
                         * Parameter Row.
                         * The String contains: DBName, Label, OriginalValue, CurrentValue for
                         * each changed DataColumn.
                         */
                        ChangedFieldsColl =
                            StringHelper.StrSplit(((PartnerAddressAggregateTDSAddressAddedOrChangedPromotionRow)
                                                   PropagateLocationParametersProcessedDV[0].
                                                   Row).ChangedFields, "|");
                        ChangedFieldsArr = new String[(Convert.ToInt16(ChangedFieldsColl.Count / 4.0))];
//                      TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: Length(ChangedFieldsArr): " + Convert.ToInt16(ChangedFieldsArr.Length).ToString());

                        Counter = 0;
                        Counter2 = 0;

                        // Build array that contains just the DB names of the changed fields
                        while (Counter2 <= ChangedFieldsArr.Length - 1)
                        {
                            ChangedFieldsArr[Counter2] = ChangedFieldsColl[Counter];

                            // position Counter to next DB Field name
                            Counter = Counter + 4;
                            Counter2 = Counter2 + 1;
                        }

                        /*
                         * Load data for each Person's PartnerLocation to which the changes to
                         * the Family's PartnerLocation should be taken over and apply the change.
                         */
                        PartnerLocationModifyDS = new DataSet();
                        PartnerLocationModifyDS.Tables.Add(new PPartnerLocationTable(PPartnerLocationTable.GetTableName()));

                        for (Counter3 = 0; Counter3 <= UpdatePartnerLocationOtherPersons.GetLength(0) - 1; Counter3 += 1)
                        {
                            ParametersArray = new OdbcParameter[3];
                            ParametersArray[0] = new OdbcParameter("", OdbcType.Decimal, 10);
                            ParametersArray[0].Value = (System.Object)(UpdatePartnerLocationOtherPersons[Counter3, 0]);
                            ParametersArray[1] = new OdbcParameter("", OdbcType.Decimal, 10);
                            ParametersArray[1].Value = (System.Object)(UpdatePartnerLocationOtherPersons[Counter3, 1]);
                            ParametersArray[2] = new OdbcParameter("", OdbcType.Int);
                            ParametersArray[2].Value = (System.Object)(UpdatePartnerLocationOtherPersons[Counter3, 2]);
                            PartnerLocationModifyDS = DBAccess.GDBAccessObj.Select(PartnerLocationModifyDS,
                                "SELECT * " + "FROM PUB_" + PPartnerLocationTable.GetTableDBName() + ' ' + "WHERE " +
                                PPartnerLocationTable.GetPartnerKeyDBName() + " = ? " + "AND " + PPartnerLocationTable.GetSiteKeyDBName() + " = ? " +
                                "AND " + PPartnerLocationTable.GetLocationKeyDBName() + " = ?",
                                PPartnerLocationTable.GetTableName(),
                                ASubmitChangesTransaction,
                                ParametersArray);
                            PartnerLocationModificationDT = ((PPartnerLocationTable)PartnerLocationModifyDS.Tables[0]);

                            // Take over defined Columns' contents (only those Columns containing
                            // changes) of the FAMILY's PartnerLocation to the loaded
                            // PartnerLocation
                            for (Counter4 = 0; Counter4 <= ChangedFieldsArr.Length - 1; Counter4 += 1)
                            {
                                PartnerLocationModificationDT[0][ChangedFieldsArr[Counter4]] = APartnerLocationRow[ChangedFieldsArr[Counter4]];
//                              TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: Changes to Column " + ChangedFieldsArr[Counter4].ToString() +
//                                      " taken over for LocationKey " + PartnerLocationModificationDT[0][PPartnerLocationTable.GetLocationKeyDBName()].ToString());
                            }

                            // Submit the changes of the processed Person's PartnerLocation record to the DB
                            PPartnerLocationAccess.SubmitChanges(PartnerLocationModificationDT, ASubmitChangesTransaction);

                            // Don't keep the Person's PPartnerLocation in memory that we just processed!
                            PartnerLocationModifyDS.Tables[0].Rows.Clear();
                        }
                    }
                }

                ReturnValue = TSubmitChangesResult.scrOK;
            }
            else
            {
//              TLogging.LogAtLevel(9, "PerformPartnerLocationChangeChecks: Location " + APartnerLocationRow.LocationKey.ToString() + ": User cancelled the selection - stopping the whole saving process!");

                /*
                 * User cancelled the selection - stop the whole saving process!
                 */
                AVerificationResult.Add(new TVerificationResult("Partner-specific Address Data Change Promotion: Information",
                        "No changes were saved because the Partner-specific Address Data Promotion dialog was cancelled by the user.",
                        "Saving cancelled by user", "", TResultSeverity.Resv_Noncritical));
                ReturnValue = TSubmitChangesResult.scrError;
            }

            return ReturnValue;
        }
Example #18
0
        /// <summary>
        /// extend an extract from a list of best addresses
        /// </summary>
        /// <param name="AExtractId">Extract Id of the Extract to extend</param>
        /// <param name="APartnerKeysTable"></param>
        /// <param name="APartnerKeyColumn">number of the column that contains the partner keys</param>
        /// <param name="ASiteKeyColumn">number of the column that contains the site keys</param>
        /// <param name="ALocationKeyColumn">number of the column that contains the location keys</param>
        /// <param name="AIgnoreDuplicates">true if duplicates should be looked out for. Can be set to false if called only once and not several times per extract.</param>
        public static void ExtendExtractFromListOfPartnerKeys(
            Int32 AExtractId,
            DataTable APartnerKeysTable,
            Int32 APartnerKeyColumn,
            Int32 ASiteKeyColumn,
            Int32 ALocationKeyColumn,
            bool AIgnoreDuplicates)
        {
            int RecordCounter = 0;
            PPartnerLocationTable PartnerLocationKeysTable;
            Int64 PartnerKey;

            TDBTransaction Transaction  = null;
            bool           SubmissionOK = true;

            DBAccess.GDBAccessObj.GetNewOrExistingAutoTransaction(IsolationLevel.Serializable, ref Transaction, ref SubmissionOK,
                                                                  delegate
            {
                MExtractTable ExtractTable = new MExtractTable();
                ExtractTable = MExtractAccess.LoadViaMExtractMaster(AExtractId, Transaction);

                // Location Keys need to be determined as extracts do not only need partner keys but
                // also Location Keys.
                DetermineBestLocationKeys(APartnerKeysTable, APartnerKeyColumn, ASiteKeyColumn,
                                          ALocationKeyColumn, out PartnerLocationKeysTable,
                                          Transaction);

                // use the returned table which contains partner and location keys to build the extract
                foreach (PPartnerLocationRow PartnerLocationRow in PartnerLocationKeysTable.Rows)
                {
                    PartnerKey = Convert.ToInt64(PartnerLocationRow[PPartnerLocationTable.GetPartnerKeyDBName()]);

                    if (PartnerKey > 0)
                    {
                        RecordCounter += 1;
                        TLogging.LogAtLevel(1, "Preparing Partner " + PartnerKey + " (Record Number " + RecordCounter + ")");

                        // add row for partner to extract and fill with contents
                        MExtractRow NewRow = ExtractTable.NewRowTyped();
                        NewRow.ExtractId   = AExtractId;
                        NewRow.PartnerKey  = PartnerKey;
                        NewRow.SiteKey     = Convert.ToInt64(PartnerLocationRow[PPartnerLocationTable.GetSiteKeyDBName()]);
                        NewRow.LocationKey = Convert.ToInt32(PartnerLocationRow[PPartnerLocationTable.GetLocationKeyDBName()]);

                        // only add row if it does not already exist for this partner
                        if (AIgnoreDuplicates || !ExtractTable.Rows.Contains(new object[] { NewRow.ExtractId, NewRow.PartnerKey, NewRow.SiteKey }))
                        {
                            ExtractTable.Rows.Add(NewRow);
                        }
                    }
                }

                if (ExtractTable.Rows.Count > 0)
                {
                    // update field in extract master for quick access to number of partners in extract
                    MExtractMasterTable ExtractMaster = MExtractMasterAccess.LoadByPrimaryKey(AExtractId, Transaction);
                    ExtractMaster[0].KeyCount         = ExtractTable.Rows.Count;

                    ExtractTable.ThrowAwayAfterSubmitChanges = true;     // no need to keep data as this increases speed significantly

                    MExtractAccess.SubmitChanges(ExtractTable, Transaction);

                    MExtractMasterAccess.SubmitChanges(ExtractMaster, Transaction);
                }
            });
        }
        /// <summary>
        /// Determines the changes in the screen's dataset and submits them to the
        /// Server.
        /// </summary>
        /// <param name="AInspectDS">The screen's dataset
        /// </param>
        /// <returns>True if saving of data succeeded, otherwise false.</returns>
        private Boolean SaveChanges(ref PartnerEditTDS AInspectDS)
        {
            Boolean ReturnValue = false;
            PartnerEditTDS SubmitDS = null;
            TSubmitChangesResult SubmissionResult;

            TVerificationResultCollection VerificationResult;
            TVerificationResult VerificationResultItem;

            System.Windows.Forms.DialogResult UnitParentAssignment;
            int RowIndex;
            int NumRows;
            Int32 MaxColumn;
            Boolean SavedPartnerIsNewPartner = false;
            bool PartnerAttributesOrRelationsChanged = false;
            System.Int32 ChangedColumns;
#if SHOWCHANGES
            String DebugMessage;
#endif
#if DATASETDEBUGGING
            Int16 TmpRowCounter;
            string TmpDebugString = String.Empty;
#endif
            // Be sure to fire the OnLeave event on the active control of any user control
            FPetraUtilsObject.ForceOnLeaveForActiveControl();

            FPetraUtilsObject.OnDataSavingStart(this, new System.EventArgs());

            // Don't allow saving if user is still editing a Detail of a List
            if (FPetraUtilsObject.InDetailEditMode())
            {
                ReturnValue = false;
                return ReturnValue;
            }

            // Make sure that DataBinding writes the value of the active Control to the underlying DataSource!
            TDataBinding.EnsureDataChangesAreStored(this);

            GetDataFromControls();

            // Clear any validation errors so that the following call to ValidateAllData starts with a 'clean slate'.
            FPetraUtilsObject.VerificationResultCollection.Clear();

            if (ValidateAllData())
            {
                // Ask the user about non-critical warnings, if they are the only 'errors' in the collection
                if (FPetraUtilsObject.VerificationResultCollection.HasOnlyNonCriticalErrors
                    && (TDataValidation.ProcessAnyDataValidationWarnings(FPetraUtilsObject.VerificationResultCollection,
                            MCommonResourcestrings.StrFormSaveDataAnywayQuestion, this.GetType()) == false))
                {
                    return false;
                }

                // Fire the DataSavingValidated event, which is the last chance to cancel the save
                System.ComponentModel.CancelEventArgs eCancel = new System.ComponentModel.CancelEventArgs(false);
                FPetraUtilsObject.OnDataSavingValidated(this, eCancel);

                if (eCancel.Cancel == true)
                {
                    return false;
                }

                foreach (DataTable InspectDT in AInspectDS.Tables)
                {
                    foreach (DataRow InspectDR in InspectDT.Rows)
                    {
                        InspectDR.EndEdit();
                    }
                }

                if (FPetraUtilsObject.HasChanges)
                {
                    FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataInProgress);
                    this.Cursor = Cursors.WaitCursor;

                    /* $IFDEF SHOWCHANGES MessageBox.Show('SaveChanges: AInspectDS.PLocation.Rows[0].HasVersion(DataRowVersion.Original): ' + AInspectDS.PLocation.Rows[0].HasVersion(DataRowVersion.Original).ToString + '; LocationKey: ' +
                     *AInspectDS.PLocation.Row[0].LocationKey.ToString); $ENDIF */

                    /* $IFDEF SHOWCHANGES MessageBox.Show('SaveChanges: AInspectDS.PLocation.Rows[1].HasVersion(DataRowVersion.Original): ' + AInspectDS.PLocation.Rows[1].HasVersion(DataRowVersion.Original).ToString + '; LocationKey: ' +
                     *AInspectDS.PLocation.Row[1].LocationKey.ToString); $ENDIF */
                    if (!FPetraUtilsObject.SubmitChangesContinue)
                    {
                        foreach (DataTable InspectDT in AInspectDS.Tables)
                        {
                            // MessageBox.Show('inspectDataTable: ' + InspectDT.ToString);
                            if ((InspectDT.TableName != PLocationTable.GetTableName()) && (InspectDT.TableName != PPartnerLocationTable.GetTableName()))
                            {
                                MaxColumn = InspectDT.Columns.Count;
                                ChangedColumns = DataUtilities.AcceptChangesForUnmodifiedRows(InspectDT, MaxColumn);

                                if (ChangedColumns != 0)
                                {
#if SHOWCHANGES
                                    MessageBox.Show(InspectDT.TableName + " - changed colums: " + ChangedColumns.ToString());
#endif
                                }
                            }
                            else if (InspectDT.TableName == PLocationTable.GetTableName())
                            {
                                MaxColumn = new PLocationTable().Columns.Count;

                                // MessageBox.Show('PLocation MaxColumn: ' + MaxColumn.ToString);
                                ChangedColumns = DataUtilities.AcceptChangesForUnmodifiedRows(AInspectDS.PLocation, MaxColumn, true);

                                if (ChangedColumns != 0)
                                {
#if SHOWCHANGES
                                    MessageBox.Show(PLocationTable.GetTableName() + " - changed colums: " + ChangedColumns.ToString());
#endif
                                }
                            }
                            else
                            {
                                MaxColumn = new PPartnerLocationTable().Columns.Count;

                                // MessageBox.Show('PPartnerLocation MaxColumn: ' + MaxColumn.ToString);
                                ChangedColumns = DataUtilities.AcceptChangesForUnmodifiedRows(AInspectDS.PPartnerLocation,
                                    MaxColumn,
                                    true);

                                if (ChangedColumns != 0)
                                {
#if SHOWCHANGES
                                    MessageBox.Show(PPartnerLocationTable.GetTableName() + " - changed colums: " + ChangedColumns.ToString());
#endif
                                }
                            }

#if SHOWCHANGES
                            foreach (DataRow InspectDR in InspectDT.Rows)
                            {
                                DebugMessage = InspectDT.ToString();

                                if ((InspectDR.RowState == DataRowState.Modified) || (InspectDR.RowState == DataRowState.Added))
                                {
                                    ChangedColumns = 0;

                                    if (InspectDR.RowState == DataRowState.Modified)
                                    {
                                        DebugMessage = DebugMessage + " --- changed columns:" + Environment.NewLine;
                                    }
                                    else
                                    {
                                        DebugMessage = DebugMessage + " --- inserted Row. Column contents:" + Environment.NewLine;
                                    }

                                    for (int Counter1 = 0; Counter1 <= MaxColumn - 1; Counter1 += 1)
                                    {
                                        if ((InspectDR.RowState == DataRowState.Added) || (InspectDR != InspectDR[Counter1, DataRowVersion.Current]))
                                        {
                                            ChangedColumns = ChangedColumns + 1;
                                            DebugMessage = DebugMessage + "  " + (InspectDT.Columns[Counter1].ColumnName).ToString() + ": " +
                                                           InspectDR[Counter1, DataRowVersion.Current].ToString() + Environment.NewLine;
                                        }
                                    }

                                    if (ChangedColumns != 0)
                                    {
                                        MessageBox.Show(DebugMessage);
                                    }
                                    else
                                    {
                                        DebugMessage = DebugMessage + "  NO changed columns.";
                                        MessageBox.Show(DebugMessage);
                                    }
                                }
                                // (inspectDataRow.RowState = DataRowState.Modified) or (inspectDataRow.RowState = DataRowState.Added)
                                else
                                {
                                    // MessageBox.Show('inspectDataRow.RowState: ' + inspectDataRow.RowState.ToString("G"));
                                    if (InspectDR.RowState == DataRowState.Deleted)
                                    {
                                        DebugMessage = DebugMessage + " --- deleted Row. Original Column[0] contents: " +
                                                       InspectDR[0, DataRowVersion.Original].ToString();
                                        MessageBox.Show(DebugMessage);
                                    }
                                }
                            }
                            // for inspectDataRow in inspectDataTable.Rows do
#endif
                        }

                        // for inspectDataTable in inspectDataSet.Tables do
                    }

                    // If changes have been made to pm_staff_data (commitments) then update the partner's family's Gift Destination records
                    TGiftDestination GiftDestination = new TGiftDestination();

                    if (GiftDestination.UpdateGiftDestination(ref AInspectDS))
                    {
                        // set the gift destination on this screen and all other open partner edit screens for the same family
                        SetGiftDestinationAllScreens();
                    }

                    SubmitDS = AInspectDS.GetChangesTyped(true);

                    if (SubmitDS == null)
                    {
                        // There is nothing to be saved.
                        // Update UI
                        FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataNothingToSave);
                        this.Cursor = Cursors.Default;

                        // We don't have unsaved changes anymore
                        FPetraUtilsObject.DisableSaveButton();

                        return true;
                    }

                    if ((SubmitDS.Tables.Contains(PPartnerAttributeTable.GetTableName()))
                        || (SubmitDS.Tables.Contains(PPartnerRelationshipTable.GetTableName())))
                    {
                        PartnerAttributesOrRelationsChanged = true;
                    }

#if DATASETDEBUGGING
                    TLogging.Log("Before submitting data to the Server. Client DataSet: " + SubmitDS.GetXml());
#endif
                    // Submit changes to the PETRAServer
                    try
                    {
                        if (!FPetraUtilsObject.SubmitChangesContinue)
                        {
                            FResponseDS = null;
                            SubmissionResult = FPartnerEditUIConnector.SubmitChanges(ref SubmitDS, ref FResponseDS, out VerificationResult);
                        }
                        else
                        {
                            SubmissionResult = FPartnerEditUIConnector.SubmitChangesContinue(out SubmitDS, ref FResponseDS, out VerificationResult);
                        }
                    }
                    catch (ESecurityDBTableAccessDeniedException Exp)
                    {
                        FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataException);
                        this.Cursor = Cursors.Default;

                        TMessages.MsgSecurityException(Exp, this.GetType());

                        ReturnValue = false;
                        FPetraUtilsObject.OnDataSaved(this, new TDataSavedEventArgs(ReturnValue));
                        return ReturnValue;
                    }
                    catch (EDBConcurrencyException Exp)
                    {
                        FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataException);
                        this.Cursor = Cursors.Default;

                        TMessages.MsgDBConcurrencyException(Exp, this.GetType());

                        ReturnValue = false;
                        FPetraUtilsObject.OnDataSaved(this, new TDataSavedEventArgs(ReturnValue));
                        return ReturnValue;
                    }
                    catch (Exception)
                    {
                        FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataException);
                        this.Cursor = Cursors.Default;

                        FPetraUtilsObject.OnDataSaved(this, new TDataSavedEventArgs(ReturnValue));
                        throw;
                    }

                    switch (SubmissionResult)
                    {
                        case TSubmitChangesResult.scrOK:

                            SavedPartnerIsNewPartner = IsNewPartner(AInspectDS);

                            // MessageBox.Show('DUMMY: ' + (SubmitDS.Tables['Locations'].Rows[0]['DUMMY']).ToString() );
                            if ((SharedTypes.PartnerClassStringToEnum(AInspectDS.PPartner[0].PartnerClass) == TPartnerClass.UNIT)
                                && SavedPartnerIsNewPartner)
                            {
                                /*
                                 * A new Partner of PartnerClass UNIT has been created
                                 * -- give option to assign 'Parent' in Unit Hierarchy
                                 */
                                UnitParentAssignment = MessageBox.Show(StrQueryUnitParent,
                                    StrQueryUnitParentTitle,
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button1);

                                if (UnitParentAssignment == System.Windows.Forms.DialogResult.Yes)
                                {
                                    TFrmUnitHierarchy HierarchyForm = new TFrmUnitHierarchy(this.ParentForm);

                                    HierarchyForm.Show();
                                    HierarchyForm.ShowThisUnit(AInspectDS.PPartner[0].PartnerKey);
                                }
                            }

#if DATASETDEBUGGING
                            TLogging.Log("After submtting returned Server DataSet: " + SubmitDS.GetXml());
                            MessageBox.Show("Before CleanupAddressesBeforeMerge");
#endif
                            // Get rid of any new Addresses; they are returned back with different LocationKeys (based on a Sequence)
                            ucoLowerPart.CleanupAddressesBeforeMerge();
#if DATASETDEBUGGING
                            MessageBox.Show("After CleanupAddressesBeforeMerge");

                            if ((SubmitDS.PLocation != null)
                                && (SubmitDS.PLocation.Rows.Count > 0))
                            {
                                MessageBox.Show(
                                    "Location[0] LocationKey: " + SubmitDS.PLocation[0].LocationKey.ToString() +
                                    "; PartnerLocation[0] LocationKey: " +
                                    SubmitDS.PPartnerLocation[0].LocationKey.ToString());
                            }
#endif

                            // Delete all added Rows in the original dataset. They will automatically
                            // be put back in with the Merge. If added Rows are deleted they will
                            // be removed from the Row collection on the server. In that case the
                            // client will not be aware of that. For modified Rows: if they are
                            // deleted on the server they will not be taken out of the collection
                            // but come back to the client as being marked as 'Deleted'. Therefore
                            // with deleting the added Rows beforehand we are making sure that
                            // the dataset on the client is synchronized with the one on the server.
                            foreach (DataTable t in AInspectDS.Tables)
                            {
                                if ((t == AInspectDS.PBankingDetails)
                                    || (t == AInspectDS.PPartnerBankingDetails)
                                    || (t == AInspectDS.PDataLabelValuePartner))
                                {
                                    NumRows = t.Rows.Count;

                                    for (RowIndex = NumRows - 1; RowIndex >= 0; RowIndex -= 1)
                                    {
                                        DataRow InspectDR = t.Rows[RowIndex];

                                        // delete all added Rows.
                                        if (InspectDR.RowState == DataRowState.Added)
                                        {
                                            InspectDR.Delete();
                                        }
                                    }
                                }
                            }

                            // Update UserDefaults, if necessary
                            MaintainUserDefaults();

                            if (SubmitDS.Tables.Contains(PPartnerTaxDeductiblePctTable.GetTableName()))
                            {
                                UpdateTaxDeductiblePct();
                            }

                            // Call AcceptChanges to get rid now of any deleted columns before we Merge with the result from the Server
                            AInspectDS.AcceptChanges();
#if DATASETDEBUGGING
                            TLogging.Log("After getting rid now of any deleted columns   Client DataSet: " + AInspectDS.GetXml());

                            if (AInspectDS.Tables.Contains(PLocationTable.GetTableName()))
                            {
                                for (TmpRowCounter = 0; TmpRowCounter < AInspectDS.Tables[PLocationTable.GetTableName()].Rows.Count; TmpRowCounter++)
                                {
                                    TmpDebugString = TmpDebugString + PLocationTable.GetTableName() + ".Row[" + TmpRowCounter.ToString() +
                                                     "]: PLocationKey: " +
                                                     AInspectDS.Tables[PLocationTable.GetTableName()].Rows[TmpRowCounter][PLocationTable.
                                                                                                                          GetLocationKeyDBName()].
                                                     ToString() + "(); PSiteKey: " +
                                                     AInspectDS.Tables[PLocationTable.GetTableName()].Rows[TmpRowCounter][PLocationTable.
                                                                                                                          GetSiteKeyDBName()].
                                                     ToString() + "(); Modification TimeStamp: " +
                                                     AInspectDS.Tables[PLocationTable.GetTableName()].Rows[TmpRowCounter][PLocationTable.
                                                                                                                          GetModificationIdDBName()].
                                                     ToString() + "\r\n";
                                }
                            }

                            if (AInspectDS.Tables.Contains(PPartnerLocationTable.GetTableName()))
                            {
                                TmpDebugString = TmpDebugString + "\r\n";

                                for (TmpRowCounter = 0;
                                     TmpRowCounter < AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows.Count;
                                     TmpRowCounter++)
                                {
                                    TmpDebugString = TmpDebugString + PPartnerLocationTable.GetTableName() + ".Row[" + TmpRowCounter.ToString() +
                                                     "]: PLocationKey: " +
                                                     AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows[TmpRowCounter][
                                        PPartnerLocationTable.GetLocationKeyDBName()].ToString() + "(); PSiteKey: " +
                                                     AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows[TmpRowCounter][
                                        PPartnerLocationTable.GetSiteKeyDBName()].ToString() + "(); PPartnerKey: " +
                                                     AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows[TmpRowCounter][
                                        PPartnerLocationTable.GetPartnerKeyDBName()].ToString() + "(); Modification TimeStamp: " +
                                                     AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows[TmpRowCounter][
                                        PPartnerLocationTable.GetModificationIdDBName()].ToString() + "\r\n";
                                }

                                MessageBox.Show(TmpDebugString, "DEBUG: PLocation / PPartnerLocation local contents  [#1]");
                            }
#endif
                            // Merge back with data from the Server (eg. for getting Sequence values)
                            AInspectDS.Merge(SubmitDS, false);
#if DATASETDEBUGGING
                            TLogging.Log("After Merge back with data from the Server  Server DataSet: " + SubmitDS.GetXml());
                            TLogging.Log("After Merge back with data from the Server  Client DataSet: " + AInspectDS.GetXml());

                            if (AInspectDS.Tables.Contains(PLocationTable.GetTableName()))
                            {
                                TmpDebugString = "";

                                for (TmpRowCounter = 0; TmpRowCounter < AInspectDS.Tables[PLocationTable.GetTableName()].Rows.Count; TmpRowCounter++)
                                {
                                    TmpDebugString = TmpDebugString + PLocationTable.GetTableName() + ".Row[" + TmpRowCounter.ToString() +
                                                     "]: PLocationKey: " +
                                                     AInspectDS.Tables[PLocationTable.GetTableName()].Rows[TmpRowCounter][PLocationTable.
                                                                                                                          GetLocationKeyDBName()].
                                                     ToString() + "(); PSiteKey: " +
                                                     AInspectDS.Tables[PLocationTable.GetTableName()].Rows[TmpRowCounter][PLocationTable.
                                                                                                                          GetSiteKeyDBName()].
                                                     ToString() + "(); Modification TimeStamp: " +
                                                     AInspectDS.Tables[PLocationTable.GetTableName()].Rows[TmpRowCounter][PLocationTable.
                                                                                                                          GetModificationIdDBName()].
                                                     ToString() + "\r\n";
                                }
                            }

                            if (AInspectDS.Tables.Contains(PPartnerLocationTable.GetTableName()))
                            {
                                TmpDebugString = TmpDebugString + "\r\n";

                                for (TmpRowCounter = 0;
                                     TmpRowCounter < AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows.Count;
                                     TmpRowCounter++)
                                {
                                    TmpDebugString = TmpDebugString + PPartnerLocationTable.GetTableName() + ".Row[" + TmpRowCounter.ToString() +
                                                     "]: PLocationKey: " +
                                                     AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows[TmpRowCounter][
                                        PPartnerLocationTable.GetLocationKeyDBName()].ToString() + "(); PSiteKey: " +
                                                     AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows[TmpRowCounter][
                                        PPartnerLocationTable.GetSiteKeyDBName()].ToString() + "(); PPartnerKey: " +
                                                     AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows[TmpRowCounter][
                                        PPartnerLocationTable.GetPartnerKeyDBName()].ToString() + "(); Modification TimeStamp: " +
                                                     AInspectDS.Tables[PPartnerLocationTable.GetTableName()].Rows[TmpRowCounter][
                                        PPartnerLocationTable.GetModificationIdDBName()].ToString() + "\r\n";
                                }

                                MessageBox.Show(TmpDebugString, "DEBUG: PLocation / PPartnerLocation local contents  [#2]");
                            }
#endif

                            ucoUpperPart.UpdateStatusUpdatedDate();  // this is to refresh 'Status Updated' if it has been changed in the ComboBox and then saved...

                            ucoLowerPart.RefreshRecordsAfterMerge();
                            ucoLowerPart.RefreshPersonnelDataAfterMerge(PartnerAttributesOrRelationsChanged);

                            // Call AcceptChanges so that we don't have any changed data anymore!
                            AInspectDS.AcceptChanges();
#if DATASETDEBUGGING
                            TLogging.Log("After calling AcceptChanges on the Client DataSet: " + AInspectDS.GetXml());
#endif
                            // Update UI
                            FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataSuccessful);
                            this.Cursor = Cursors.Default;
                            EnableSave(false);

                            // If the screen was opened for a NEW Partner, remove the 'NEW:' indicator from the Window Title Bar and
                            // set this Partner to be the "Last Used Partner".
                            if (SavedPartnerIsNewPartner)
                            {
                                FPetraUtilsObject.HasNewData = false;
                                SetScreenCaption();

                                // Set Partner to be the "Last Used Partner"
                                TUserDefaults.NamedDefaults.SetLastPartnerWorkedWith(AInspectDS.PPartner[0].PartnerKey,
                                    TLastPartnerUse.lpuMailroomPartner, SharedTypes.PartnerClassStringToEnum(FPartnerClass));
                            }

                            // We don't have unsaved changes anymore
                            FPetraUtilsObject.DisableSaveButton();
                            FPetraUtilsObject.SubmitChangesContinue = false;

                            // Assign PartnerKey. This is needed in case this was a new Partner before saving!
                            FPartnerKey = AInspectDS.PPartner[0].PartnerKey;

                            ReturnValue = true;
                            FPetraUtilsObject.OnDataSaved(this, new TDataSavedEventArgs(ReturnValue));

                            if ((VerificationResult != null)
                                && (VerificationResult.HasCriticalOrNonCriticalErrors))
                            {
                                TDataValidation.ProcessAnyDataValidationErrors(false, VerificationResult,
                                    this.GetType(), null);
                            }

                            break;

                        case TSubmitChangesResult.scrError:
                            this.Cursor = Cursors.Default;

                            if (!(VerificationResult.Contains((System.Object) "Location Change Promotion: Information")))
                            {
                                FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataErrorOccured);

                                TDataValidation.ProcessAnyDataValidationErrors(false, VerificationResult,
                                    this.GetType(), null);
                            }
                            else
                            {
                                FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataCancelled);

                                VerificationResultItem = (TVerificationResult)VerificationResult.FindBy(
                                    (object)"Location Change Promotion: Information");

                                MessageBox.Show(VerificationResultItem.ResultText,
                                    VerificationResultItem.ResultTextCaption, MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                            }

                            FPetraUtilsObject.SubmitChangesContinue = false;

                            ReturnValue = false;
                            FPetraUtilsObject.OnDataSaved(this, new TDataSavedEventArgs(ReturnValue));
                            break;

                        case TSubmitChangesResult.scrNothingToBeSaved:

                            // If there were no changes discovered then still need to call AcceptChanges to get rid now of
                            // any deleted columns
                            AInspectDS.AcceptChanges();

                            // Update UI
                            this.Cursor = Cursors.Default;
                            FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataNothingToSave);

                            // We don't have unsaved changes anymore
                            EnableSave(false);
                            FPetraUtilsObject.HasChanges = false;

                            ReturnValue = true;
                            FPetraUtilsObject.OnDataSaved(this, new TDataSavedEventArgs(ReturnValue));
                            break;

                        case TSubmitChangesResult.scrInfoNeeded:
                            this.Cursor = Cursors.Default;
#if DATASETDEBUGGING
                            for (int Counter2 = 0; Counter2 < FResponseDS.Tables.Count; Counter2++)
                            {
                                MessageBox.Show(
                                    "Table #" + Counter2.ToString() + "'s name is " + FResponseDS.Tables[Counter2].TableName + ". It has " +
                                    FResponseDS.Tables[Counter2].Rows.Count.ToString() + " Rows. Type: " +
                                    FResponseDS.Tables[Counter2].GetType().ToString());
                            }

                            if (FResponseDS.Tables.Contains(MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME))
                            {
                                MessageBox.Show("FResponseDS Type: " + FResponseDS.GetType().ToString());
                                MessageBox.Show(MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME + " Type: " +
                                    FResponseDS.Tables[MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME].GetType().ToString());
                                MessageBox.Show(
                                    "FResponseDS.Tables[" + MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME + "].Rows.Count: " +
                                    FResponseDS.Tables[MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME].Rows.Count.ToString());
                                // Check if there is a Parameter Row that is not yet processed
                                DataView ExistingLocationParametersDV = new DataView(
                                    FResponseDS.Tables[MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME],
                                    PartnerAddressAggregateTDSSimilarLocationParametersTable.GetAnswerProcessedClientSideDBName() +
                                    " = false", "", DataViewRowState.CurrentRows);

                                if (ExistingLocationParametersDV.Count > 0)
                                {
                                    MessageBox.Show("ExistingLocationParametersDV.Count: " + ExistingLocationParametersDV.Count.ToString());
                                    MessageBox.Show(
                                        "Row[0].LocationKey: " +
                                        (ExistingLocationParametersDV[0].Row as PartnerAddressAggregateTDSSimilarLocationParametersRow).LocationKey.
                                        ToString());
                                }
                            }
#endif
                            ucoLowerPart.SimilarLocationsProcessing(
                            (PartnerAddressAggregateTDSSimilarLocationParametersTable)FResponseDS.Tables[MPartnerConstants.
                                                                                                         EXISTINGLOCATIONPARAMETERS_TABLENAME]);
#if DATASETDEBUGGING
                            if (FResponseDS.Tables[MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME].Rows.Count > 0)
                            {
                                MessageBox.Show("Reuse?: " + (FResponseDS.Tables[MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME]
                                                              as PartnerAddressAggregateTDSSimilarLocationParametersTable)[0].AnswerReuse.ToString());
                            }

                            if (FResponseDS.Tables.Contains(MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME))
                            {
                                MessageBox.Show(MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME + " Type: " +
                                    FResponseDS.Tables[MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME].GetType().ToString());
                                MessageBox.Show(
                                    "FResponseDS.Tables[" + MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME + "].Rows.Count: " +
                                    FResponseDS.Tables[MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME].Rows.Count.ToString());
                                //Check if there is a Parameter Row that is not yet processed
                                DataView AddressAddedOrChangedParametersDV = new DataView(
                                    FResponseDS.Tables[MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME],
                                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetAnswerProcessedClientSideDBName() +
                                    " = false", "", DataViewRowState.CurrentRows);

                                if (AddressAddedOrChangedParametersDV.Count > 0)
                                {
                                    MessageBox.Show("AddressAddedOrChangedParametersDV.Count: " + AddressAddedOrChangedParametersDV.Count.ToString());
                                    MessageBox.Show(
                                        "Row[0].LocationKey: " +
                                        (AddressAddedOrChangedParametersDV[0].Row as PartnerAddressAggregateTDSAddressAddedOrChangedPromotionRow).
                                        LocationKey.
                                        ToString());
                                }
                            }
#endif
                            ucoLowerPart.AddressAddedOrChangedProcessing(
                            (PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable)
                            FResponseDS.Tables[MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME],
                            (PartnerAddressAggregateTDSChangePromotionParametersTable)
                            FResponseDS.Tables[MPartnerConstants.ADDRESSCHANGEPROMOTIONPARAMETERS_TABLENAME]);
#if DATASETDEBUGGING
                            MessageBox.Show("After AddressAddedOrChangedProcessing");
#endif
                            // Recursive call!
                            FPetraUtilsObject.SubmitChangesContinue = true;
                            ReturnValue = SaveChanges(ref AInspectDS);

                            return ReturnValue;
                    }
                }
                else
                {
                    // Update UI
                    FPetraUtilsObject.WriteToStatusBar(MCommonResourcestrings.StrSavingDataNothingToSave);
                    this.Cursor = Cursors.Default;
                    EnableSave(false);

                    // We don't have unsaved changes anymore
                    FPetraUtilsObject.HasChanges = false;

                    ReturnValue = true;
                    FPetraUtilsObject.OnDataSaved(this, new TDataSavedEventArgs(ReturnValue));
                }
            }
            else
            {
                FPetraUtilsObject.OnDataSaved(this, new TDataSavedEventArgs(false));
            }

            // if the partner has been saved then broadcast a message to any listening forms to inform them
            if (ReturnValue)
            {
                TFormsMessage BroadcastMessage;
                TFormsMessage BroadcastMessageGiftDestination;
                String PartnerShortNameForBroadcast;
                long FamilyPartnerKey;
                String FamilyShortName;

                if (SavedPartnerIsNewPartner)
                {
                    BroadcastMessage = new TFormsMessage(TFormsMessageClassEnum.mcNewPartnerSaved,
                        FCallerContext);
                }
                else
                {
                    BroadcastMessage = new TFormsMessage(TFormsMessageClassEnum.mcExistingPartnerSaved,
                        FCallerContext);
                }

                if (!FMainDS.PPartner[0].IsPartnerShortNameNull())
                {
                    PartnerShortNameForBroadcast = FMainDS.PPartner[0].PartnerShortName;
                }
                else
                {
                    PartnerShortNameForBroadcast = String.Empty;
                }

                BroadcastMessage.SetMessageDataPartner(
                    FPartnerKey,
                    SharedTypes.PartnerClassStringToEnum(FPartnerClass),
                    PartnerShortNameForBroadcast,
                    FMainDS.PPartner[0].StatusCode);

                TFormsList.GFormsList.BroadcastFormMessage(BroadcastMessage);

                if ((SubmitDS.PPartnerGiftDestination != null) && (SubmitDS.PPartnerGiftDestination.Rows.Count > 0))
                {
                    BroadcastMessageGiftDestination = new TFormsMessage(TFormsMessageClassEnum.mcPersonnelCommitmentChanged,
                        FCallerContext);

                    if (FPartnerClass == TPartnerClass.FAMILY.ToString())
                    {
                        FamilyPartnerKey = FPartnerKey;
                        FamilyShortName = PartnerShortNameForBroadcast;
                    }
                    else
                    {
                        FamilyPartnerKey = ((PPersonRow)FMainDS.PPerson.Rows[0]).FamilyKey;
                        FamilyShortName = String.Empty;
                    }

                    BroadcastMessageGiftDestination.SetMessageDataPartner(
                        FamilyPartnerKey,
                        TPartnerClass.FAMILY,
                        FamilyShortName,
                        FMainDS.PPartner[0].StatusCode);

                    TFormsList.GFormsList.BroadcastFormMessage(BroadcastMessageGiftDestination);
                }
            }

            return ReturnValue;
        }
Example #20
0
        /// <summary>
        /// generate the partners from a text file that was generated with Benerator
        /// </summary>
        /// <param name="AInputBeneratorFile"></param>
        public static void GenerateFamilyPartners(string AInputBeneratorFile)
        {
            TDataBase      db          = DBAccess.Connect("GenerateFamilyPartners");
            TDBTransaction Transaction = db.BeginTransaction(IsolationLevel.ReadCommitted);

            // get a list of banks (all class BANK)
            string    sqlGetBankPartnerKeys = "SELECT p_partner_key_n FROM PUB_p_bank";
            DataTable BankKeys = db.SelectDT(sqlGetBankPartnerKeys, "keys", Transaction);

            Transaction.Rollback();

            PartnerEditTDS MainDS = new PartnerEditTDS();

            // AlanP: May 2016 - We may no longer need the UTF8 because the method now automatically discovers the encoding even with no BOM
            XmlDocument doc = TCsv2Xml.ParseCSVFile2Xml(AInputBeneratorFile, ",", Encoding.UTF8);

            XmlNode RecordNode = doc.FirstChild.NextSibling.FirstChild;

            while (RecordNode != null)
            {
                string familySituation = TXMLParser.GetAttribute(RecordNode, "familySituation");

                PFamilyRow  familyRecord     = null;
                PPartnerRow FamilyPartnerRow = null;

                if (familySituation == "singleMan")
                {
                    familyRecord = SampleDataWorkers.GenerateFamilyRecord(RecordNode, "Male", MainDS);
                    SampleDataWorkers.GeneratePersonRecord(RecordNode, familyRecord, "Male", MainDS);
                }
                else if (familySituation == "singleWoman")
                {
                    familyRecord = SampleDataWorkers.GenerateFamilyRecord(RecordNode, "Female", MainDS);
                    SampleDataWorkers.GeneratePersonRecord(RecordNode, familyRecord, "Female", MainDS);
                }
                else if (familySituation == "family")
                {
                    familyRecord = SampleDataWorkers.GenerateFamilyRecord(RecordNode, "Male", MainDS);
                    SampleDataWorkers.GeneratePersonRecord(RecordNode, familyRecord, "Male", MainDS);
                    SampleDataWorkers.GeneratePersonRecord(RecordNode, familyRecord, "Female", MainDS);

                    int NumberOfChildren = Convert.ToInt32(TXMLParser.GetAttribute(RecordNode, "numberOfChildren"));

                    if (NumberOfChildren > 0)
                    {
                        FamilyPartnerRow = (PPartnerRow)MainDS.PPartner.Rows.Find(familyRecord.PartnerKey);
                        FamilyPartnerRow.AddresseeTypeCode = MPartnerConstants.ADDRESSEETYPE_FAMILY;
                    }
                }

                FamilyPartnerRow = (PPartnerRow)MainDS.PPartner.Rows.Find(familyRecord.PartnerKey);
                FamilyPartnerRow.ReceiptEachGift        = false;
                FamilyPartnerRow.ReceiptLetterFrequency = "Annual";

                SampleDataWorkers.GenerateAddressForFamily(RecordNode, familyRecord, MainDS);

                SampleDataWorkers.GenerateBankDetails(RecordNode, familyRecord, MainDS, BankKeys);

                if (MainDS.PFamily.Rows.Count % 100 == 0)
                {
                    TLogging.Log("created donor " + MainDS.PFamily.Rows.Count.ToString() + " " + familyRecord.FamilyName);
                }

                RecordNode = RecordNode.NextSibling;
            }

            // we do not save person records for normal family partners
            MainDS.PPerson.Clear();

            // need to clear all partner records of the PERSON partners as well
            DataView PersonPartners = new DataView(MainDS.PPartner);

            PersonPartners.RowFilter =
                string.Format("{0} = '{1}'",
                              PPartnerTable.GetPartnerClassDBName(),
                              MPartnerConstants.PARTNERCLASS_PERSON);

            DataView PartnerLocations = new DataView(MainDS.PPartnerLocation);

            foreach (DataRowView rv in PersonPartners)
            {
                PPartnerRow partnerRow = (PPartnerRow)rv.Row;

                PartnerLocations.RowFilter =
                    string.Format("{0} = {1}",
                                  PPartnerLocationTable.GetPartnerKeyDBName(),
                                  partnerRow.PartnerKey);

                PartnerLocations[0].Row.Delete();

                partnerRow.Delete();
            }

            MainDS.ThrowAwayAfterSubmitChanges = true;

            PartnerEditTDSAccess.SubmitChanges(MainDS);

            TLogging.Log("after saving donors");
        }
Example #21
0
        /// Upgrade to version 2015-01
        public static bool UpgradeDatabase201412_201501()
        {
            // There are no new tables and fields

            TDBTransaction       SubmitChangesTransaction = null;
            TSubmitChangesResult SubmissionResult         = TSubmitChangesResult.scrError;

            DBAccess.GDBAccessObj.BeginAutoTransaction(IsolationLevel.Serializable, ref SubmitChangesTransaction,
                                                       ref SubmissionResult,
                                                       delegate
            {
                PPartnerAttributeTable partnerattributes = new PPartnerAttributeTable();
                PPartnerLocationTable partnerlocations   = PPartnerLocationAccess.LoadAll(SubmitChangesTransaction);

                // this update only works for very simple databases, only one partner location record per partner...
                if (partnerlocations.Count > 1000)
                {
                    throw new Exception("the upgrade has not been optimized for huge databases");
                }

                List <Int64> PartnerKeys = new List <Int64>();

                foreach (PPartnerLocationRow partnerlocation in partnerlocations.Rows)
                {
                    if (!PartnerKeys.Contains(partnerlocation.PartnerKey))
                    {
                        PartnerKeys.Add(partnerlocation.PartnerKey);
                    }
                    else
                    {
                        TLogging.Log("several locations for partner " + partnerlocation.PartnerKey.ToString());
                    }
                }

                // Number for the p_sequence_i Column. Gets increased with every p_partner_attribute record that gets produced!
                int SequenceNumber = 0;

                foreach (Int64 partnerkey in PartnerKeys)
                {
                    // Get that Partner's p_partner_location records from PPartnerLocationRecords
                    DataRow[] CurrentRows = partnerlocations.Select(PPartnerLocationTable.GetPartnerKeyDBName() + " = " + partnerkey.ToString());

                    if (CurrentRows.Length == 0)
                    {
                        continue;
                    }

                    DataTable PPartnersLocationsDT = GetNewPPartnerLocationTableInstance();

                    foreach (DataRow r in CurrentRows)
                    {
                        PPartnersLocationsDT.Rows.Add(r.ItemArray);
                    }

                    TLocationPK bestAddress = Calculations.DetermineBestAddress(PPartnersLocationsDT);

                    int IndexPhone  = 0;
                    int IndexEmail  = 0;
                    int IndexFax    = 0;
                    int IndexUrl    = 0;
                    int IndexMobile = 0;

                    List <string> AvoidDuplicates = new List <string>();
                    string AttributeConcatenated;

                    foreach (PPartnerLocationRow partnerlocation in PPartnersLocationsDT.Rows)
                    {
                        bool primaryAddress =
                            (bestAddress.LocationKey == partnerlocation.LocationKey && bestAddress.SiteKey == partnerlocation.SiteKey);
                        bool currentAddress  = (((int)partnerlocation[PARTNERLOCATION_ICON_COLUMN]) == 1);
                        bool businessAddress = (partnerlocation.LocationType == "BUSINESS" || partnerlocation.LocationType == "FIELD");
                        // TODO: avoid duplicate entries with the same type

                        if (!partnerlocation.IsEmailAddressNull())
                        {
                            PPartnerAttributeRow partnerattribute = partnerattributes.NewRowTyped();
                            partnerattribute.Sequence             = SequenceNumber++;
                            partnerattribute.PartnerKey           = partnerlocation.PartnerKey;
                            partnerattribute.Value               = partnerlocation.EmailAddress;
                            partnerattribute.AttributeType       = MPartnerConstants.ATTR_TYPE_EMAIL;
                            partnerattribute.Current             = currentAddress;
                            partnerattribute.Primary             = primaryAddress;
                            partnerattribute.Index               = IndexEmail++;
                            partnerattribute.Specialised         = businessAddress;
                            partnerattribute.NoLongerCurrentFrom = partnerlocation.DateGoodUntil;

                            AttributeConcatenated = ConcatPartnerAttributes(partnerattribute);

                            if (!AvoidDuplicates.Contains(AttributeConcatenated))
                            {
                                partnerattributes.Rows.Add(partnerattribute);
                                AvoidDuplicates.Add(AttributeConcatenated);
                            }
                            else
                            {
                                TLogging.Log("dropping duplicate " + AttributeConcatenated);
                            }

                            partnerlocation.SetEmailAddressNull();
                        }

                        if (!partnerlocation.IsTelephoneNumberNull())
                        {
                            if (!partnerlocation.IsExtensionNull())
                            {
                                partnerlocation.TelephoneNumber += "-" + partnerlocation.Extension;
                            }

                            PPartnerAttributeRow partnerattribute = partnerattributes.NewRowTyped();
                            partnerattribute.Sequence             = SequenceNumber++;
                            partnerattribute.PartnerKey           = partnerlocation.PartnerKey;
                            partnerattribute.Value               = partnerlocation.TelephoneNumber;
                            partnerattribute.AttributeType       = MPartnerConstants.ATTR_TYPE_PHONE;
                            partnerattribute.Current             = currentAddress;
                            partnerattribute.Primary             = primaryAddress;
                            partnerattribute.Index               = IndexPhone++;
                            partnerattribute.Specialised         = businessAddress;
                            partnerattribute.NoLongerCurrentFrom = partnerlocation.DateGoodUntil;

                            AttributeConcatenated = ConcatPartnerAttributes(partnerattribute);

                            if (!AvoidDuplicates.Contains(AttributeConcatenated))
                            {
                                partnerattributes.Rows.Add(partnerattribute);
                                AvoidDuplicates.Add(AttributeConcatenated);
                            }
                            else
                            {
                                TLogging.Log("dropping duplicate " + AttributeConcatenated);
                            }

                            partnerlocation.SetTelephoneNumberNull();
                        }

                        if (!partnerlocation.IsFaxNumberNull())
                        {
                            if (!partnerlocation.IsFaxExtensionNull())
                            {
                                partnerlocation.FaxNumber += "-" + partnerlocation.FaxExtension;
                            }

                            PPartnerAttributeRow partnerattribute = partnerattributes.NewRowTyped();
                            partnerattribute.Sequence             = SequenceNumber++;
                            partnerattribute.PartnerKey           = partnerlocation.PartnerKey;
                            partnerattribute.Value               = partnerlocation.FaxNumber;
                            partnerattribute.AttributeType       = MPartnerConstants.ATTR_TYPE_FAX;
                            partnerattribute.Current             = currentAddress;
                            partnerattribute.Primary             = primaryAddress;
                            partnerattribute.Index               = IndexFax++;
                            partnerattribute.Specialised         = businessAddress;
                            partnerattribute.NoLongerCurrentFrom = partnerlocation.DateGoodUntil;

                            AttributeConcatenated = ConcatPartnerAttributes(partnerattribute);

                            if (!AvoidDuplicates.Contains(AttributeConcatenated))
                            {
                                partnerattributes.Rows.Add(partnerattribute);
                                AvoidDuplicates.Add(AttributeConcatenated);
                            }
                            else
                            {
                                TLogging.Log("dropping duplicate " + AttributeConcatenated);
                            }

                            partnerlocation.SetFaxNumberNull();
                        }

                        if (!partnerlocation.IsAlternateTelephoneNull())
                        {
                            PPartnerAttributeRow partnerattribute = partnerattributes.NewRowTyped();
                            partnerattribute.Sequence             = SequenceNumber++;
                            partnerattribute.PartnerKey           = partnerlocation.PartnerKey;
                            partnerattribute.Value               = partnerlocation.AlternateTelephone;
                            partnerattribute.AttributeType       = MPartnerConstants.ATTR_TYPE_PHONE;
                            partnerattribute.Current             = currentAddress;
                            partnerattribute.Primary             = primaryAddress;
                            partnerattribute.Index               = IndexPhone++;
                            partnerattribute.Specialised         = businessAddress;
                            partnerattribute.NoLongerCurrentFrom = partnerlocation.DateGoodUntil;

                            AttributeConcatenated = ConcatPartnerAttributes(partnerattribute);

                            if (!AvoidDuplicates.Contains(AttributeConcatenated))
                            {
                                partnerattributes.Rows.Add(partnerattribute);
                                AvoidDuplicates.Add(AttributeConcatenated);
                            }
                            else
                            {
                                TLogging.Log("dropping duplicate " + AttributeConcatenated);
                            }

                            partnerlocation.SetAlternateTelephoneNull();
                        }

                        if (!partnerlocation.IsMobileNumberNull())
                        {
                            PPartnerAttributeRow partnerattribute = partnerattributes.NewRowTyped();
                            partnerattribute.Sequence             = SequenceNumber++;
                            partnerattribute.PartnerKey           = partnerlocation.PartnerKey;
                            partnerattribute.Value               = partnerlocation.MobileNumber;
                            partnerattribute.AttributeType       = MPartnerConstants.ATTR_TYPE_MOBILE_PHONE;
                            partnerattribute.Current             = currentAddress;
                            partnerattribute.Primary             = primaryAddress;
                            partnerattribute.Index               = IndexMobile++;
                            partnerattribute.Specialised         = businessAddress;
                            partnerattribute.NoLongerCurrentFrom = partnerlocation.DateGoodUntil;

                            AttributeConcatenated = ConcatPartnerAttributes(partnerattribute);

                            if (!AvoidDuplicates.Contains(AttributeConcatenated))
                            {
                                partnerattributes.Rows.Add(partnerattribute);
                                AvoidDuplicates.Add(AttributeConcatenated);
                            }
                            else
                            {
                                TLogging.Log("dropping duplicate " + AttributeConcatenated);
                            }

                            partnerlocation.SetMobileNumberNull();
                        }

                        if (!partnerlocation.IsUrlNull())
                        {
                            PPartnerAttributeRow partnerattribute = partnerattributes.NewRowTyped();
                            partnerattribute.Sequence             = SequenceNumber++;
                            partnerattribute.PartnerKey           = partnerlocation.PartnerKey;
                            partnerattribute.Value               = partnerlocation.Url;
                            partnerattribute.AttributeType       = MPartnerConstants.ATTR_TYPE_WEBSITE;
                            partnerattribute.Current             = currentAddress;
                            partnerattribute.Primary             = primaryAddress;
                            partnerattribute.Index               = IndexUrl++;
                            partnerattribute.Specialised         = businessAddress;
                            partnerattribute.NoLongerCurrentFrom = partnerlocation.DateGoodUntil;

                            AttributeConcatenated = ConcatPartnerAttributes(partnerattribute);

                            if (!AvoidDuplicates.Contains(AttributeConcatenated))
                            {
                                partnerattributes.Rows.Add(partnerattribute);
                                AvoidDuplicates.Add(AttributeConcatenated);
                            }
                            else
                            {
                                TLogging.Log("dropping duplicate " + AttributeConcatenated);
                            }

                            partnerlocation.SetUrlNull();
                        }
                    }
                }

                PPartnerLocationAccess.SubmitChanges(partnerlocations, SubmitChangesTransaction);
                PPartnerAttributeAccess.SubmitChanges(partnerattributes, SubmitChangesTransaction);
                SubmissionResult = TSubmitChangesResult.scrOK;
            });
            return(true);
        }
Example #22
0
        /// <summary>
        /// Determines the 'Best Address' of the PERSON and its Phone Number and Email Address.
        /// </summary>
        /// <param name="APhoneNumberOfPerson">Phone Number of the PERSON in international format.</param>
        /// <param name="AEmailAddressOfPerson">Email Address of the PERSON.</param>
        /// <returns><see cref="Ict.Petra.Shared.MPartner.TLocationPK" /> pointing to the 'Best Address' of the PERSON.</returns>
        private TLocationPK DetermineAddressComponents(out string APhoneNumberOfPerson, out string AEmailAddressOfPerson)
        {
            TLocationPK ReturnValue = Ict.Petra.Shared.MPartner.Calculations.DetermineBestAddress(
                FMainDS.Tables[PartnerEditTDSPPartnerLocationTable.GetTableName()]);
            DataRow BestPartnerLocationDR;
            DataRow BestLocationDR;
            string  TelephoneNumber;
            string  Extension;
            string  CountryCode;

            // Initialise out Arguments
            APhoneNumberOfPerson  = null;
            AEmailAddressOfPerson = null;

            BestPartnerLocationDR = FMainDS.Tables[PartnerEditTDSPPartnerLocationTable.GetTableName()].Rows.Find(new object[]
                                                                                                                 { FMainDS.PPerson[0].PartnerKey, ReturnValue.SiteKey, ReturnValue.LocationKey });

            if (BestPartnerLocationDR != null)
            {
                BestLocationDR = FMainDS.Tables[PLocationTable.GetTableName()].Rows.Find(new object[]
                                                                                         { ReturnValue.SiteKey, ReturnValue.LocationKey });

                if (!BestPartnerLocationDR.IsNull(PPartnerLocationTable.GetTelephoneNumberDBName()))
                {
                    TelephoneNumber = (string)BestPartnerLocationDR[PPartnerLocationTable.GetTelephoneNumberDBName()];
                }
                else
                {
                    TelephoneNumber = String.Empty;
                }

                if (!BestPartnerLocationDR.IsNull(PPartnerLocationTable.GetExtensionDBName()))
                {
                    Extension = ((int)BestPartnerLocationDR[PPartnerLocationTable.GetExtensionDBName()]).ToString();
                }
                else
                {
                    Extension = String.Empty;
                }

                if (!BestLocationDR.IsNull(PLocationTable.GetCountryCodeDBName()))
                {
                    CountryCode = (string)BestLocationDR[PLocationTable.GetCountryCodeDBName()];
                }
                else
                {
                    CountryCode = String.Empty;
                }

                APhoneNumberOfPerson = Ict.Petra.Shared.MPartner.Calculations.FormatIntlPhoneNumber(
                    TelephoneNumber,
                    Extension,
                    CountryCode,
                    @TDataCache.GetCacheableDataTableFromCache);

                if (!BestPartnerLocationDR.IsNull(PPartnerLocationTable.GetEmailAddressDBName()))
                {
                    AEmailAddressOfPerson = (string)BestPartnerLocationDR[PPartnerLocationTable.GetEmailAddressDBName()];
                }
                else
                {
                    AEmailAddressOfPerson = String.Empty;
                }
            }
            else
            {
                MessageBox.Show("Unexpected condition: 'Best Address of PERSON is null'", DEV_FIX);
            }

            return(ReturnValue);
        }
Example #23
0
        /// <summary>
        /// return an XmlDocument with all partner info;
        /// the partners are grouped by class, country, status, and sitekey
        /// </summary>
        /// <returns></returns>
        public static string ExportPartners()
        {
            PartnerEditTDS MainDS = new PartnerEditTDS();

            LoadDataFromDB(ref MainDS);

            // Group partners into categories.
            //
            // A partner's category is defined by his: class, country, status, and sitekey
            // It is stored as a string e.g. "FAMILY,DE,ACTIVE,0".
            //
            SortedList <string, List <long> > PartnerCategories = GroupPartnersIntoCategories(MainDS);

            // create XML structure for each category
            XmlDocument PartnerData = TYml2Xml.CreateXmlDocument();
            XmlNode     rootNode    = PartnerData.FirstChild.NextSibling;

            Int32 groupCounter = 0;

            foreach (string category in PartnerCategories.Keys)
            {
                // get category data
                groupCounter++;
                XmlElement groupNode = PartnerData.CreateElement("PartnerGroup" + groupCounter.ToString());
                rootNode.AppendChild(groupNode);

                Int32    partnerCounter  = 0;
                string[] categoryDetails = category.Split(new char[] { ',' });
                // may want to skip the categories with sitekey = -1
                // right now, we still export them and ignore the partners 0 and 1000000 later

                groupNode.SetAttribute("class", categoryDetails[0]);
                groupNode.SetAttribute("Country", categoryDetails[1]);
                groupNode.SetAttribute("status", categoryDetails[2]);
                groupNode.SetAttribute("SiteKey", categoryDetails[3]);

                List <long> partnerKeys = PartnerCategories[category];

                foreach (long partnerKey in partnerKeys)
                {
                    if ((partnerKey != 0) && (partnerKey != 1000000)) // skip organization root and the 0 when exporting
                    {
                        MainDS.PPartner.DefaultView.RowFilter = PPartnerTable.GetPartnerKeyDBName() + "=" + partnerKey.ToString();
                        PPartnerRow partnerRow = (PPartnerRow)MainDS.PPartner.DefaultView[0].Row;

                        PFamilyRow familyRow = null;

                        if (partnerRow.PartnerClass == MPartnerConstants.PARTNERCLASS_FAMILY)
                        {
                            MainDS.PFamily.DefaultView.RowFilter = PFamilyTable.GetPartnerKeyDBName() + "=" + partnerKey.ToString();
                            familyRow = (PFamilyRow)MainDS.PFamily.DefaultView[0].Row;
                        }

                        PPersonRow personRow = null;

                        if (partnerRow.PartnerClass == MPartnerConstants.PARTNERCLASS_PERSON)
                        {
                            MainDS.PPerson.DefaultView.RowFilter = PPersonTable.GetPartnerKeyDBName() + "=" + partnerKey.ToString();
                            personRow = (PPersonRow)MainDS.PPerson.DefaultView[0].Row;
                        }

                        POrganisationRow organisationRow = null;

                        if (partnerRow.PartnerClass == MPartnerConstants.PARTNERCLASS_ORGANISATION)
                        {
                            MainDS.POrganisation.DefaultView.RowFilter = POrganisationTable.GetPartnerKeyDBName() + "=" + partnerKey.ToString();
                            organisationRow = (POrganisationRow)MainDS.POrganisation.DefaultView[0].Row;
                        }

                        PUnitRow           unitRow          = null;
                        UmUnitStructureRow unitStructureRow = null;

                        if (partnerRow.PartnerClass == MPartnerConstants.PARTNERCLASS_UNIT)
                        {
                            MainDS.PUnit.DefaultView.RowFilter = PUnitTable.GetPartnerKeyDBName() + "=" + partnerKey.ToString();
                            unitRow = (PUnitRow)MainDS.PUnit.DefaultView[0].Row;
                            MainDS.UmUnitStructure.DefaultView.RowFilter = UmUnitStructureTable.GetChildUnitKeyDBName() + " = " + partnerKey.ToString();

                            long numParents = MainDS.UmUnitStructure.DefaultView.Count;

                            if (numParents == 1)
                            {
                                unitStructureRow = (UmUnitStructureRow)MainDS.UmUnitStructure.DefaultView[0].Row;
                            }
                            else
                            {
                                throw new Exception(
                                          "Units must have exactly one ParentUnit. " +
                                          "The unit with partnerKey " + partnerKey.ToString() + " has " +
                                          numParents.ToString() + ".");
                            }
                        }

                        PBankRow BankRow = null;

                        if (partnerRow.PartnerClass == MPartnerConstants.PARTNERCLASS_BANK)
                        {
                            MainDS.PBank.DefaultView.RowFilter = PBankTable.GetPartnerKeyDBName() + "=" + partnerKey.ToString();
                            BankRow = (PBankRow)MainDS.PBank.DefaultView[0].Row;
                        }

                        partnerCounter++;
                        XmlElement partnerNode = PartnerData.CreateElement("Partner" + partnerCounter.ToString());
                        groupNode.AppendChild(partnerNode);

                        partnerNode.SetAttribute("PartnerKey", partnerRow.PartnerKey.ToString());

                        //groupNode.SetAttribute("ShortName", partnerRow.PartnerShortName.ToString());

                        if (personRow != null)
                        {
                            partnerNode.SetAttribute("FirstName", personRow.FirstName.ToString());
                            partnerNode.SetAttribute("LastName", personRow.FamilyName.ToString());
                            partnerNode.SetAttribute("Title", personRow.Title.ToString());
                        }
                        else if (familyRow != null)
                        {
                            partnerNode.SetAttribute("FirstName", familyRow.FirstName.ToString());
                            partnerNode.SetAttribute("LastName", familyRow.FamilyName.ToString());
                            partnerNode.SetAttribute("Title", familyRow.Title.ToString());
                        }
                        else if (organisationRow != null)
                        {
                            partnerNode.SetAttribute("Name", organisationRow.OrganisationName.ToString());
                        }
                        else if (unitRow != null)
                        {
                            partnerNode.SetAttribute("Name", unitRow.UnitName.ToString());
                            partnerNode.SetAttribute("UnitTypeCode", unitRow.UnitTypeCode.ToString());

                            if (unitStructureRow != null)
                            {
                                partnerNode.SetAttribute("ParentUnitKey", unitStructureRow.ParentUnitKey.ToString());
                            }
                        }

                        if (BankRow != null)
                        {
                            partnerNode.SetAttribute("BranchName", BankRow.BranchName);
                            partnerNode.SetAttribute("BranchCode", BankRow.BranchCode);
                            partnerNode.SetAttribute("BranchBic", BankRow.Bic);
                            partnerNode.SetAttribute("EpFormatFile", BankRow.EpFormatFile);
                        }

                        partnerNode.SetAttribute("CreatedAt", partnerRow.DateCreated.Value.ToString("yyyy-MM-dd HH:mm:ss"));

                        // special types
                        string specialTypes = "";
                        MainDS.PPartnerType.DefaultView.RowFilter = PPartnerTypeTable.GetPartnerKeyDBName() + "=" + partnerKey.ToString();

                        foreach (DataRowView rv in MainDS.PPartnerType.DefaultView)
                        {
                            if (specialTypes.Length > 0)
                            {
                                specialTypes += ", ";
                            }

                            specialTypes += ((PPartnerTypeRow)rv.Row).TypeCode;
                        }

                        if (specialTypes.Length > 0)
                        {
                            partnerNode.SetAttribute("SpecialTypes", specialTypes);
                        }

                        // addresses
                        DataView partnerLocationView = MainDS.PPartnerLocation.DefaultView;
                        partnerLocationView.RowFilter =
                            PPartnerLocationTable.GetPartnerKeyDBName() + " = " + partnerRow.PartnerKey.ToString() +
                            "AND " + PPartnerLocationTable.GetLocationKeyDBName() + " <> 0 "; // ignore invalid addresses
                        Int32 addressCounter = 0;

                        foreach (DataRowView rv in partnerLocationView)
                        {
                            XmlElement addressNode = PartnerData.CreateElement("Address" + (addressCounter > 0 ? addressCounter.ToString() : ""));
                            addressCounter++;
                            partnerNode.AppendChild(addressNode);

                            PPartnerLocationRow partnerLocationRow = (PPartnerLocationRow)rv.Row;

                            DataView locationView = MainDS.PLocation.DefaultView;
                            locationView.RowFilter =
                                PLocationTable.GetSiteKeyDBName() + "=" + partnerLocationRow.SiteKey.ToString() + " AND " +
                                PLocationTable.GetLocationKeyDBName() + "=" + partnerLocationRow.LocationKey.ToString();

                            if (locationView.Count > 0)
                            {
                                PLocationRow locationRow = (PLocationRow)locationView[0].Row;

                                addressNode.SetAttribute("Street", locationRow.StreetName);
                                addressNode.SetAttribute("City", locationRow.City);
                                addressNode.SetAttribute("PostCode", locationRow.PostalCode);
                            }

                            addressNode.SetAttribute("Email", partnerLocationRow.EmailAddress);
                            addressNode.SetAttribute("Phone", partnerLocationRow.TelephoneNumber);
                            addressNode.SetAttribute("MobilePhone", partnerLocationRow.MobileNumber);
                        }

                        // TODO: notes
                        // TODO: This doesn't export as much data as it should?
                    }
                }
            }

            return(TXMLParser.XmlToString(PartnerData));
        }
Example #24
0
 /// <summary>
 /// </summary>
 /// <param name="ALocationDT"></param>
 /// <param name="APartnerLocationDT"></param>
 /// <param name="APartnerKey"></param>
 /// <param name="APartnerClass"></param>
 /// <param name="ACountryCode"></param>
 /// <param name="ANewLocationKey"></param>
 /// <param name="ACopyFromPartnerLocationKey"></param>
 public static void CreateNewAddressInternal(PLocationTable ALocationDT,
     PPartnerLocationTable APartnerLocationDT,
     Int64 APartnerKey,
     TPartnerClass APartnerClass,
     String ACountryCode,
     Int32 ANewLocationKey,
     Int32 ACopyFromPartnerLocationKey)
 {
     CreateNewAddressInternal(ALocationDT,
         APartnerLocationDT,
         APartnerKey,
         APartnerClass,
         ACountryCode,
         ANewLocationKey,
         ACopyFromPartnerLocationKey,
         -99,
         -1,
         -99,
         -99,
         false,
         true);
 }
        /// <summary>
        /// Creates an instance of a 'cut-down' PPartnerLocation Table that will be used for storing partial
        /// to-be-imported p_partner_location record information. It will also be used for the 'Best Address'
        /// determination, which is important for the Contact Details migration.
        /// </summary>
        /// <returns>Instance of a 'cut-down' PPartnerLocation Table that will be used for 'Best Address' determination.</returns>
        public static DataTable GetNewPPartnerLocationTableInstance()
        {
            DataTable ReturnValue = new PPartnerLocationTable();
            DataColumn IconForSortingCol;

            // Add special DataColumns that are needed for the 'Best Address' calculation
            ReturnValue.Columns.Add(new System.Data.DataColumn(PARTNERLOCATION_ICON_COLUMN, typeof(Int32)));

            // Add a 'Calculated Column' for sorting
            IconForSortingCol = new DataColumn();
            IconForSortingCol.DataType = System.Type.GetType("System.Int32");
            IconForSortingCol.ColumnName = "Icon_For_Sorting";
            IconForSortingCol.Expression = "IIF(Icon = 1, 2, IIF(Icon = 2, 1, 3))"; // exchanges instead of Icon=1 we get Icon=2
            ReturnValue.Columns.Add(IconForSortingCol);

            return ReturnValue;
        }
Example #26
0
        /// <summary>
        /// Creates a list of locations for a given Partner.
        /// </summary>
        /// <remarks>Corresponds in parts with with Progress 4GL Method
        /// 'GetPartnerLocations' in partner/maillib.p.</remarks>
        /// <param name="APartnerKey">PartnerKey of the Partner being processed.</param>
        /// <param name="AMailingAddressesOnly">If true: only include addresses with mailing flag set.</param>
        /// <param name="AIncludeCurrentAddresses">If true: include current addresses.</param>
        /// <param name="AIncludeFutureAddresses">If true: include future addresses.</param>
        /// <param name="AIncludeExpiredAddresses">If true: include expired addresses.</param>
        /// <param name="APartnerLocations">The Locations of the Partner being processed.</param>
        /// <param name="ADataBase">An instantiated <see cref="TDataBase" /> object, or null (default = null). If null
        /// gets passed then the Method executes DB commands with the 'globally available'
        /// <see cref="DBAccess.GDBAccessObj" /> instance, otherwise with the instance that gets passed in with this
        /// Argument!</param>
        /// <returns>False if an invalid PartnerKey was passed in or if Petra Security
        /// denied access to the Partner, otherwise true.</returns>
        public static bool GetPartnerLocations(Int64 APartnerKey,
                                               bool AMailingAddressesOnly,
                                               bool AIncludeCurrentAddresses,
                                               bool AIncludeFutureAddresses,
                                               bool AIncludeExpiredAddresses,
                                               out PPartnerLocationTable APartnerLocations,
                                               TDataBase ADataBase = null)
        {
            TDBTransaction ReadTransaction;
            Boolean        NewTransaction;

            String  SelectSQL;
            DataSet FillDataSet;

            OdbcParameter param;

            // Initialise out Argument
            APartnerLocations = null;

            if (APartnerKey > 0)
            {
                TLogging.LogAtLevel(8, "TMailing.GetPartnerLocations: Checking access to Partner.");

                if (TSecurity.CanAccessPartnerByKey(APartnerKey, false, ADataBase) ==
                    TPartnerAccessLevelEnum.palGranted)
                {
                    ReadTransaction = DBAccess.GetDBAccessObj(ADataBase).GetNewOrExistingTransaction(
                        IsolationLevel.ReadCommitted,
                        TEnforceIsolationLevel.eilMinimum,
                        out NewTransaction);

                    // Load Partner Locations, taking passed in restrictions into account.
                    try
                    {
                        SelectSQL =
                            "SELECT *" +
                            "  FROM PUB_" + PPartnerLocationTable.GetTableDBName() +
                            " WHERE " + PPartnerLocationTable.GetPartnerKeyDBName() + " = ?" +
                            "   AND (NOT ? = true OR (? = true AND " + PPartnerLocationTable.GetSendMailDBName() + " = true))" +
                            "   AND ((? = true AND ((" + PPartnerLocationTable.GetDateEffectiveDBName() + " <= ?" +
                            "          OR " + PPartnerLocationTable.GetDateEffectiveDBName() + " IS NULL)" +
                            "     AND (" + PPartnerLocationTable.GetDateGoodUntilDBName() + " >= ?" +
                            "          OR " + PPartnerLocationTable.GetDateGoodUntilDBName() + " IS NULL)))" +
                            "     OR (? = true AND " + PPartnerLocationTable.GetDateEffectiveDBName() + " > ?)" +
                            "     OR (? = true AND " + PPartnerLocationTable.GetDateGoodUntilDBName() + " < ?))";

                        List <OdbcParameter> parameters = new List <OdbcParameter>();
                        param       = new OdbcParameter("PartnerKey", OdbcType.Decimal, 10);
                        param.Value = APartnerKey;
                        parameters.Add(param);
                        param       = new OdbcParameter("MailingAddressOnly1", OdbcType.Bit);
                        param.Value = AMailingAddressesOnly;
                        parameters.Add(param);
                        param       = new OdbcParameter("MailingAddressOnly2", OdbcType.Bit);
                        param.Value = AMailingAddressesOnly;
                        parameters.Add(param);
                        param       = new OdbcParameter("IncludeCurrentAddresses", OdbcType.Bit);
                        param.Value = AIncludeCurrentAddresses;
                        parameters.Add(param);
                        param       = new OdbcParameter("TodaysDate1", OdbcType.Date);
                        param.Value = DateTime.Now;
                        parameters.Add(param);
                        param       = new OdbcParameter("TodaysDate2", OdbcType.Date);
                        param.Value = DateTime.Now;
                        parameters.Add(param);
                        param       = new OdbcParameter("IncludeFutureAddresses", OdbcType.Bit);
                        param.Value = AIncludeFutureAddresses;
                        parameters.Add(param);
                        param       = new OdbcParameter("TodaysDate3", OdbcType.Date);
                        param.Value = DateTime.Now;
                        parameters.Add(param);
                        param       = new OdbcParameter("IncludeExpiredAddresses", OdbcType.Bit);
                        param.Value = AIncludeExpiredAddresses;
                        parameters.Add(param);
                        param       = new OdbcParameter("TodaysDate4", OdbcType.Date);
                        param.Value = DateTime.Now;
                        parameters.Add(param);

                        /*
                         * Our out Argument 'APartnerLocations' is a Typed DataTable, but SelectDT
                         * returns an untyped DataTable, therefore we need to create a Typed DataTable
                         * that contains the data of the returned untyped DataTable!
                         */
                        FillDataSet       = new DataSet();
                        APartnerLocations = new PPartnerLocationTable(PPartnerLocationTable.GetTableDBName());
                        FillDataSet.Tables.Add(APartnerLocations);

                        DBAccess.GetDBAccessObj(ADataBase).Select(FillDataSet, SelectSQL,
                                                                  PPartnerLocationTable.GetTableDBName(),
                                                                  ReadTransaction,
                                                                  parameters.ToArray());
//                      TLogging.LogAtLevel(7, "TMailing.GetPartnerLocations:  FillDataSet.Tables.Count: " + FillDataSet.Tables.Count.ToString());
                        FillDataSet.Tables.Remove(APartnerLocations);

                        if (APartnerLocations.Rows.Count > 0)
                        {
//                          TLogging.LogAtLevel(7, "TMailing.GetPartnerLocations: Found " + APartnerLocations.Rows.Count.ToString() + " PartnerLocations found for Partner " + APartnerKey.ToString() + ".");
                        }
                        else
                        {
                            /*
                             * /* No Rows returned = no PartnerLocations for Partner.
                             * That shouldn't happen with existing Partners, but if it does (eg. non-existing
                             * PartnerKey passed in) we return an empty Typed DataTable.
                             */
//                          TLogging.LogAtLevel(7, "TMailing.GetPartnerLocations: No PartnerLocations found for Partner " + APartnerKey.ToString() + "!");
                            APartnerLocations = new PPartnerLocationTable();
                        }
                    }
                    finally
                    {
                        if (NewTransaction)
                        {
                            DBAccess.GetDBAccessObj(ADataBase).CommitTransaction();
                            TLogging.LogAtLevel(7, "TMailing.GetPartnerLocations: committed own transaction.");
                        }
                    }

                    return(true);
                }
                else
                {
                    TLogging.LogAtLevel(8, "TMailing.GetPartnerLocations: Access to Partner DENIED!");

                    // Petra Security prevents us from accessing this Partner -> return false;
                    return(false);
                }
            }
            else
            {
                // Invalid PartnerKey -> return false;
                return(false);
            }
        }
Example #27
0
        /// find the current best address for the partner
        public static bool GetBestAddress(Int64 APartnerKey,
                                          out PLocationTable AAddress,
                                          out PPartnerLocationTable APartnerLocation,
                                          out string ACountryNameLocal,
                                          out string AEmailAddress,
                                          TDBTransaction ATransaction)
        {
            AEmailAddress     = "";
            AAddress          = null;
            APartnerLocation  = null;
            ACountryNameLocal = "";

            DataSet PartnerLocationsDS = new DataSet();

            PartnerLocationsDS.Tables.Add(new PPartnerLocationTable());
            PartnerLocationsDS.Tables.Add(new PCountryTable());
            DataTable     PartnerLocationTable = PartnerLocationsDS.Tables[PPartnerLocationTable.GetTableName()];
            PCountryTable CountryTable         = (PCountryTable)PartnerLocationsDS.Tables[PCountryTable.GetTableName()];

            CountryTable.DefaultView.Sort = PCountryTable.GetCountryCodeDBName();

            // add special column BestAddress and Icon
            PartnerLocationTable.Columns.Add(new System.Data.DataColumn("BestAddress", typeof(Boolean)));
            PartnerLocationTable.Columns.Add(new System.Data.DataColumn("Icon", typeof(Int32)));

            // find all locations of the partner, put it into a dataset
            PPartnerLocationAccess.LoadViaPPartner(PartnerLocationsDS, APartnerKey, ATransaction);

            Ict.Petra.Shared.MPartner.Calculations.DeterminePartnerLocationsDateStatus(PartnerLocationsDS);
            Ict.Petra.Shared.MPartner.Calculations.DetermineBestAddress(PartnerLocationsDS);

            foreach (PPartnerLocationRow row in PartnerLocationTable.Rows)
            {
                // find the row with BestAddress = 1
                if (Convert.ToInt32(row["BestAddress"]) == 1)
                {
                    if (!row.IsEmailAddressNull())
                    {
                        AEmailAddress = row.EmailAddress;
                    }

                    // we also want the post address, need to load the p_location table:
                    AAddress = PLocationAccess.LoadByPrimaryKey(row.SiteKey, row.LocationKey, ATransaction);

                    APartnerLocation = new PPartnerLocationTable();
                    APartnerLocation.ImportRow(row);

                    // watch out for empty country codes
                    if (AAddress[0].CountryCode.Trim().Length > 0)
                    {
                        if (CountryTable.DefaultView.Find(AAddress[0].CountryCode) == -1)
                        {
                            CountryTable.Merge(PCountryAccess.LoadByPrimaryKey(AAddress[0].CountryCode, ATransaction));
                        }

                        ACountryNameLocal = CountryTable[CountryTable.DefaultView.Find(AAddress[0].CountryCode)].CountryNameLocal;
                    }

                    break;
                }
            }

            return(AAddress != null);
        }
Example #28
0
        /// <summary>
        /// Retrieves all of the PartnerInfo data.
        /// </summary>
        /// <param name="APartnerKey">PartnerKey of Partner to find the PartnerInfo data for</param>
        /// <param name="APartnerInfoDS">Typed DataSet that contains the requested data.</param>
        /// <param name="AReadTransaction" >Open DB Transaction.</param>
        /// <returns>True if Partner exists, otherwise false.</returns>
        public static bool AllPartnerInfoData(Int64 APartnerKey, ref PartnerInfoTDS APartnerInfoDS,
                                              TDBTransaction AReadTransaction)
        {
            bool        ReturnValue = false;
            PPartnerRow PartnerDR;

            TLocationPK           BestLocationPK;
            PLocationRow          LocationDR;
            PLocationRow          LocationDR2;
            PPartnerLocationRow   PartnerLocationDR;
            PPartnerLocationRow   PartnerLocationDR2;
            PLocationTable        LocationDT;
            PPartnerLocationTable PartnerLocationDT;

            try
            {
                /*
                 * Check for existance of Partner
                 */
                PartnerDR = MCommonMain.CheckPartnerExists2(APartnerKey, true, AReadTransaction.DataBaseObj);

                if (PartnerDR != null)
                {
                    /*
                     * Perform security checks; these throw ESecurityPartnerAccessDeniedException
                     * in case access isn't granted.
                     */
                    TSecurity.CanAccessPartnerExc(PartnerDR, AReadTransaction.DataBaseObj);

                    /*
                     * Get the Partner's Address data of its 'Best' Address
                     */
                    if (TMailing.GetPartnersBestLocationData(APartnerKey, out BestLocationPK,
                                                             out LocationDR, out PartnerLocationDR, AReadTransaction.DataBaseObj))
                    {
                        #region Process Address

                        /*
                         * Check for existance of PLocation and PPartnerLocation Tables in the passed in
                         * DataSet APartnerInfoDS.
                         */
                        if (!APartnerInfoDS.Tables.Contains(PLocationTable.GetTableName()))
                        {
                            // Need to create Table here
                            APartnerInfoDS.Tables.Add(new PLocationTable());
                        }

                        if (!APartnerInfoDS.Tables.Contains(PPartnerLocationTable.GetTableName()))
                        {
                            // Need to create Table here
                            APartnerInfoDS.Tables.Add(new PPartnerLocationTable());
                        }

                        // Add copies of the Location and PartnerLocation DataRows of the 'Best Address'

                        /*
                         * Remove DataColumns that might have been added by the call to
                         * TMailing.GetPartnersBestLocationData - otherwise PartnerLocationDR2.ItemArray
                         * assignment will fail.
                         */
                        if (PartnerLocationDR.Table.Columns.Contains(Calculations.PARTNERLOCATION_BESTADDR_COLUMN))
                        {
                            PartnerLocationDR.Table.Columns.Remove(Calculations.PARTNERLOCATION_BESTADDR_COLUMN);
                        }

                        if (PartnerLocationDR.Table.Columns.Contains(Calculations.PARTNERLOCATION_ICON_COLUMN))
                        {
                            PartnerLocationDR.Table.Columns.Remove(Calculations.PARTNERLOCATION_ICON_COLUMN);
                        }

                        LocationDR2                  = APartnerInfoDS.PLocation.NewRowTyped(false);
                        LocationDR2.ItemArray        = LocationDR.ItemArray;
                        PartnerLocationDR2           = APartnerInfoDS.PPartnerLocation.NewRowTyped(false);
                        PartnerLocationDR2.ItemArray = PartnerLocationDR.ItemArray;

                        APartnerInfoDS.PLocation.Rows.Add(LocationDR2);
                        APartnerInfoDS.PPartnerLocation.Rows.Add(PartnerLocationDR2);

                        #endregion

                        // Apply Address Security
                        LocationDT        = APartnerInfoDS.PLocation;
                        PartnerLocationDT = APartnerInfoDS.PPartnerLocation;

                        TPPartnerAddressAggregate.ApplySecurity(ref PartnerLocationDT,
                                                                ref LocationDT);

                        // Process 'Head' data and rest of data for the Partner
                        HeadInternal(PartnerDR, ref APartnerInfoDS);
                        RestInternal(PartnerDR, ref APartnerInfoDS, AReadTransaction);

                        ReturnValue = true;
                    }
                }
            }
            catch (ESecurityPartnerAccessDeniedException)
            {
                // don't log this exception - this is thrown on purpose here and the Client needs to deal with it.
                throw;
            }
            catch (Exception Exp)
            {
                TLogging.Log("TServerLookups_PartnerInfo.AllPartnerInfoData exception: " + Exp.ToString(), TLoggingType.ToLogfile);
                TLogging.Log(Exp.StackTrace, TLoggingType.ToLogfile);

                throw;
            }

            return(ReturnValue);
        }
Example #29
0
        public void TestNewPartnerWithLocation0()
        {
            TPartnerEditUIConnector connector = new TPartnerEditUIConnector();

            PartnerEditTDS MainDS = new PartnerEditTDS();

            PPartnerRow PartnerRow = TCreateTestPartnerData.CreateNewFamilyPartner(MainDS);

            PPartnerLocationRow PartnerLocationRow = MainDS.PPartnerLocation.NewRowTyped();

            PartnerLocationRow.SiteKey     = DomainManager.GSiteKey;
            PartnerLocationRow.PartnerKey  = PartnerRow.PartnerKey;
            PartnerLocationRow.LocationKey = 0;
            MainDS.PPartnerLocation.Rows.Add(PartnerLocationRow);

            DataSet ResponseDS = new PartnerEditTDS();
            TVerificationResultCollection VerificationResult;

            TSubmitChangesResult result = connector.SubmitChanges(ref MainDS, ref ResponseDS, out VerificationResult);

            CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
                                                                                "There was a critical error when saving:");

            Assert.AreEqual(TSubmitChangesResult.scrOK, result, "Create a partner with location 0");

            TCreateTestPartnerData.CreateNewLocation(PartnerRow.PartnerKey, MainDS);

            // remove location 0, same is done in csharp\ICT\Petra\Client\MCommon\logic\UC_PartnerAddresses.cs TUCPartnerAddressesLogic::AddRecord
            // Check if record with PartnerLocation.LocationKey = 0 is around > delete it
            DataRow PartnerLocationRecordZero =
                MainDS.PPartnerLocation.Rows.Find(new object[] { PartnerRow.PartnerKey, DomainManager.GSiteKey, 0 });

            if (PartnerLocationRecordZero != null)
            {
                DataRow LocationRecordZero = MainDS.PLocation.Rows.Find(new object[] { DomainManager.GSiteKey, 0 });

                if (LocationRecordZero != null)
                {
                    LocationRecordZero.Delete();
                }

                PartnerLocationRecordZero.Delete();
            }

            ResponseDS = new PartnerEditTDS();
            result     = connector.SubmitChanges(ref MainDS, ref ResponseDS, out VerificationResult);

            CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
                                                                                "There was a critical error when saving:");

            Assert.AreEqual(TSubmitChangesResult.scrOK, result, "Replace location 0 of partner");

            Assert.AreEqual(1, MainDS.PPartnerLocation.Rows.Count, "the partner should only have one location in the dataset");

            // get all addresses of the partner
            TDBTransaction ReadTransaction = null;

            DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.ReadCommitted, TEnforceIsolationLevel.eilMinimum,
                                                                      ref ReadTransaction,
                                                                      delegate
            {
                PPartnerLocationTable testPartnerLocations = PPartnerLocationAccess.LoadViaPPartner(PartnerRow.PartnerKey, ReadTransaction);
                Assert.AreEqual(1, testPartnerLocations.Rows.Count, "the partner should only have one location");
                Assert.Greater(testPartnerLocations[0].LocationKey, 0, "TPartnerEditUIConnector SubmitChanges returns valid location key");
            });
        }
Example #30
0
        /// <summary>
        /// this will find the best p_partner_location, and export the values to parameters (p_street_name_c, etc.)
        /// </summary>
        /// <returns>void</returns>
        private bool GetPartnerBestAddress(Int64 APartnerKey)
        {
            bool ReturnValue = false;

            // reset the variables
            PLocationTable LocationTable = new PLocationTable();

            foreach (DataColumn col in LocationTable.Columns)
            {
                situation.GetParameters().RemoveVariable(StringHelper.UpperCamelCase(col.ColumnName, true, true));
            }

            DataTable PartnerLocationTable = new PPartnerLocationTable();

            foreach (DataColumn col in PartnerLocationTable.Columns)
            {
                // do not clear the partner key, in case we do not have an address for the partner
                if (col.ColumnName != PPartnerLocationTable.GetPartnerKeyDBName())
                {
                    situation.GetParameters().RemoveVariable(StringHelper.UpperCamelCase(col.ColumnName, true, true));
                }
            }

            situation.GetParameters().RemoveVariable("FirstName");
            situation.GetParameters().RemoveVariable("FamilyName");

            StringCollection NameColumnNames = new StringCollection();
            NameColumnNames.Add(PPartnerTable.GetPartnerShortNameDBName());
            PPartnerTable PartnerTable = PPartnerAccess.LoadByPrimaryKey(APartnerKey, NameColumnNames,
                situation.GetDatabaseConnection().Transaction);
            string PartnerShortName = PartnerTable.Rows[0][PPartnerTable.GetPartnerShortNameDBName()].ToString();

            situation.GetParameters().AddCalculationParameter("NameWithTitle",
                new TVariant(Ict.Petra.Shared.MPartner.Calculations.FormatShortName(PartnerShortName, eShortNameFormat.eReverseShortname)));

            DataSet PartnerLocationsDS = new DataSet();
            PartnerLocationsDS.Tables.Add(new PPartnerLocationTable());
            PartnerLocationTable = PartnerLocationsDS.Tables[PPartnerLocationTable.GetTableName()];

            // add special column BestAddress and Icon
            PartnerLocationTable.Columns.Add(new System.Data.DataColumn("BestAddress", typeof(Boolean)));
            PartnerLocationTable.Columns.Add(new System.Data.DataColumn("Icon", typeof(Int32)));

            // find all locations of the partner, put it into a dataset
            PPartnerLocationAccess.LoadViaPPartner(PartnerLocationsDS, APartnerKey, situation.GetDatabaseConnection().Transaction);

            // uses Ict.Petra.Shared.MPartner.Calculations.pas, DetermineBestAddress
            Calculations.DeterminePartnerLocationsDateStatus(PartnerLocationsDS);
            Calculations.DetermineBestAddress(PartnerLocationsDS);

            foreach (PPartnerLocationRow row in PartnerLocationTable.Rows)
            {
                // find the row with BestAddress = 1
                if (Convert.ToInt32(row["BestAddress"]) == 1)
                {
                    // find the location record with that address
                    LocationTable = PLocationAccess.LoadByPrimaryKey(row.SiteKey, row.LocationKey, situation.GetDatabaseConnection().Transaction);

                    // put the found values in the parameters
                    if (LocationTable.Rows.Count > 0)
                    {
                        // get the location details into the parameters
                        foreach (DataColumn col in LocationTable.Columns)
                        {
                            situation.GetParameters().AddCalculationParameter(StringHelper.UpperCamelCase(col.ColumnName, true,
                                    true), new TVariant(LocationTable.Rows[0][col.ColumnName]));
                        }

                        // also put the phone number and email etc into the parameters
                        foreach (DataColumn col in PartnerLocationTable.Columns)
                        {
                            situation.GetParameters().AddCalculationParameter(StringHelper.UpperCamelCase(col.ColumnName, true,
                                    true), new TVariant(row[col.ColumnName]));
                        }

                        // get the Partner Firstname and Surname as well; depends on the partner class
                        // first try person
                        NameColumnNames = new StringCollection();
                        NameColumnNames.Add(PPersonTable.GetFirstNameDBName());
                        NameColumnNames.Add(PPersonTable.GetFamilyNameDBName());
                        PPersonTable PersonTable = PPersonAccess.LoadByPrimaryKey(APartnerKey, NameColumnNames,
                            situation.GetDatabaseConnection().Transaction);

                        if (PersonTable.Rows.Count > 0)
                        {
                            situation.GetParameters().AddCalculationParameter("FirstName",
                                new TVariant(PersonTable.Rows[0][PPersonTable.GetFirstNameDBName()]));
                            situation.GetParameters().AddCalculationParameter("FamilyName",
                                new TVariant(PersonTable.Rows[0][PPersonTable.GetFamilyNameDBName()]));
                        }
                        else
                        {
                            // then it was a family?
                            NameColumnNames = new StringCollection();
                            NameColumnNames.Add(PFamilyTable.GetFirstNameDBName());
                            NameColumnNames.Add(PFamilyTable.GetFamilyNameDBName());
                            PFamilyTable FamilyTable = PFamilyAccess.LoadByPrimaryKey(APartnerKey, NameColumnNames,
                                situation.GetDatabaseConnection().Transaction);

                            if (FamilyTable.Rows.Count > 0)
                            {
                                situation.GetParameters().AddCalculationParameter("FirstName",
                                    new TVariant(FamilyTable.Rows[0][PFamilyTable.GetFirstNameDBName()]));
                                situation.GetParameters().AddCalculationParameter("FamilyName",
                                    new TVariant(FamilyTable.Rows[0][PFamilyTable.GetFamilyNameDBName()]));
                            }
                            else
                            {
                                // it was an organisation or church, just use the shortname
                                situation.GetParameters().RemoveVariable("FirstName");
                                situation.GetParameters().RemoveVariable("FamilyName");

                                if (PartnerTable.Rows.Count > 0)
                                {
                                    situation.GetParameters().AddCalculationParameter("FamilyName",
                                        new TVariant(PartnerShortName));
                                }
                            }
                        }

                        ReturnValue = true;
                    }
                }
            }

            return ReturnValue;
        }
        /// <summary>
        /// check the location change; validate and take other required action
        /// eg. change the location of family members, promote address changes
        /// </summary>
        /// <param name="ALocationRow"></param>
        /// <param name="APartnerKey"></param>
        /// <param name="AResponseDS"></param>
        /// <param name="ASubmitChangesTransaction"></param>
        /// <param name="AAddressAddedPromotionDT"></param>
        /// <param name="AChangeLocationParametersDT"></param>
        /// <param name="APartnerLocationTable"></param>
        /// <param name="AVerificationResult"></param>
        /// <param name="ACreateLocation"></param>
        /// <param name="AOriginalLocationKey"></param>
        /// <returns></returns>
        private static TSubmitChangesResult PerformLocationChangeChecks(PLocationRow ALocationRow,
            Int64 APartnerKey,
            ref PartnerAddressAggregateTDS AResponseDS,
            TDBTransaction ASubmitChangesTransaction,
            ref PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable AAddressAddedPromotionDT,
            ref PartnerAddressAggregateTDSChangePromotionParametersTable AChangeLocationParametersDT,
            ref PPartnerLocationTable APartnerLocationTable,
            ref TVerificationResultCollection AVerificationResult,
            out Boolean ACreateLocation,
            out TLocationPK AOriginalLocationKey)
        {
            TSubmitChangesResult ReturnValue;
            DataView PropagateLocationParametersDV;
            DataView PropagateLocationParametersDV2;
            Boolean UpdateLocation;

            Int64[] CreateLocationOtherPartnerKeys;
            PartnerAddressAggregateTDSChangePromotionParametersTable ChangePromotionParametersDT;
            PLocationTable NewLocationTable;
            PLocationRow NewLocationRowSaved;
            Int32 NewLocationLocationKey;
            PPartnerLocationRow PartnerLocationRowForChangedLocation;

            DataSet PartnerLocationModifyDS;
            int Counter;
            Int64 OldLocationKey;
            OdbcParameter[] ParametersArray;
            String OtherPartnerKeys = "";

            AOriginalLocationKey = null;
//          TLogging.LogAtLevel(9, "PerformLocationChangeChecks: AAddressAddedPromotionDT.Rows.Count: " + AAddressAddedPromotionDT.Rows.Count.ToString());

            if (CheckLocationChange(ALocationRow, APartnerKey, ref AAddressAddedPromotionDT, ASubmitChangesTransaction, out UpdateLocation,
                    out ACreateLocation, out CreateLocationOtherPartnerKeys, out ChangePromotionParametersDT))
            {
                // Check if there is a Parameter Row for the LocationKey we are looking at
                PropagateLocationParametersDV = new DataView(AAddressAddedPromotionDT,
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetSiteKeyDBName() + " = " + ALocationRow.SiteKey.ToString() +
                    " AND " +
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationKeyDBName() + " = " +
                    ALocationRow.LocationKey.ToString() +
                    " AND " + PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationChangeDBName() + " = true AND " +
                    PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetAnswerProcessedClientSideDBName() + " = false",
                    "",
                    DataViewRowState.CurrentRows);

                if (PropagateLocationParametersDV.Count > 0)
                {
//                  TLogging.LogAtLevel(9, "PerformLocationChangeChecks: Location " + ALocationRow.LocationKey.ToString() +
//                          ": Location has been changed, decision on propagation is needed.");

                    /*
                     * More information is needed (usually via user interaction)
                     * -> stop processing here and return parameters
                     * (usually used for UI interaction)
                     */
                    if (AResponseDS == null)
                    {
//                      TLogging.LogAtLevel(9, TLogging.Log("PerformLocationChangeChecks: Creating AResponseDS.");
                        AResponseDS = new PartnerAddressAggregateTDS(MPartnerConstants.PARTNERADDRESSAGGREGATERESPONSE_DATASET);
                    }

//                  TLogging.LogAtLevel(9, "PerformLocationChangeChecks: AAddressAddedPromotionDT.Rows.Count: " + AAddressAddedPromotionDT.Rows.Count.ToString());
                    AResponseDS.Merge(AAddressAddedPromotionDT);
//                  TLogging.LogAtLevel(9, "PerformLocationChangeChecks: Merged AAddressAddedPromotionDT into AResponseDS.");
                    AResponseDS.Merge(ChangePromotionParametersDT);
//                  TLogging.LogAtLevel(9, "PerformLocationChangeChecks: Merged ChangePromotionParametersDT into AResponseDS.");
//                  TLogging.LogAtLevel(9, "PerformLocationChangeChecks: AResponseDS.Tables[" + MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME +
//                      "].Rows.Count: " + AResponseDS.Tables[MPartnerConstants.ADDRESSADDEDORCHANGEDPROMOTION_TABLENAME].Rows.Count.ToString());
                    return TSubmitChangesResult.scrInfoNeeded;
                }
                else
                {
//                  TLogging.LogAtLevel(9, "PerformLocationChangeChecks: User made his/her choice regarding Location Change promotion; now processing...");

                    /*
                     * User made his/her choice regarding Location Change promotion; now process it
                     */
                    if (ACreateLocation)
                    {
                        OldLocationKey = ALocationRow.LocationKey;
                        AOriginalLocationKey = new TLocationPK(
                            Convert.ToInt64(ALocationRow[PLocationTable.GetSiteKeyDBName(),
                                                         DataRowVersion.Original]),
                            Convert.ToInt32(ALocationRow[PLocationTable.GetLocationKeyDBName(),
                                                         DataRowVersion.Original]));

                        // ALocationRow.LocationKey;
//                      TLogging.LogAtLevel(9, "PerformLocationChangeChecks: Location " + AOriginalLocationKey.LocationKey.ToString() + ": should be created.");

                        /*
                         * Create and save NEW Location that holds the same data than the changed
                         * Location.
                         */
                        NewLocationTable = new PLocationTable();
                        NewLocationRowSaved = NewLocationTable.NewRowTyped(false);
                        NewLocationRowSaved.ItemArray = DataUtilities.DestinationSaveItemArray(NewLocationRowSaved, ALocationRow);
                        NewLocationRowSaved.LocationKey = -1;
                        NewLocationTable.Rows.Add(NewLocationRowSaved);

                        // Submit the NEW Location to the DB
                        PLocationAccess.SubmitChanges(NewLocationTable, ASubmitChangesTransaction);

                        // The DB gives us a LocationKey from a Sequence. Remember this one.
                        NewLocationLocationKey = (Int32)NewLocationRowSaved.LocationKey;
//                      TLogging.LogAtLevel(9, "PerformLocationChangeChecks: New Location created! Its Location Key is: " + NewLocationLocationKey.ToString());

                        // Add the new row to the LocationTable that is beeing processed as well
                        // NewLocationCurrentTableRow := (ALocationRow.Table as PLocationTable).NewRowTyped(false);
                        // NewLocationCurrentTableRow.ItemArray := NewLocationRowSaved.ItemArray;
                        // ALocationRow.Table.Rows.Add(NewLocationCurrentTableRow);
                        // Make the row unchanged so that it isn't picked up as a 'new Address'
                        // and that it doesn't get saved later. Will be sent back to the Partner
                        // Edit screen lateron.
                        // NewLocationCurrentTableRow.AcceptChanges;

                        /*
                         * Update the reference from the changed Location to the new Location in
                         * the Partner's PartnerLocation DataTable. This will be saved later in
                         * the call to SubmitChanges in the main loop of the SubmitData function.
                         */
                        PartnerLocationRowForChangedLocation =
                            (PPartnerLocationRow)APartnerLocationTable.Rows.Find(new object[] { APartnerKey, ALocationRow.SiteKey,
                                                                                                ALocationRow.LocationKey });
                        PartnerLocationRowForChangedLocation.LocationKey = NewLocationLocationKey;

                        // Now delete the changed Location so that it doesn't get saved!
                        // ALocationRow.Delete;
                        // ALocationRow.AcceptChanges;
                        // Overwrite the Location that should be replaced with the data of the new Location
                        ALocationRow.ItemArray = NewLocationRowSaved.ItemArray;
                        PropagateLocationParametersDV2 = new DataView(AAddressAddedPromotionDT,
                            PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetSiteKeyDBName() + " = " +
                            NewLocationRowSaved.SiteKey.ToString() + " AND " +
                            PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationKeyDBName() + " = " +
                            OldLocationKey.ToString() +
                            " AND " + PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetLocationChangeDBName() + " = true AND " +
                            PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable.GetAnswerProcessedClientSideDBName() + " = true",
                            "",
                            DataViewRowState.CurrentRows);
                        ((PartnerAddressAggregateTDSAddressAddedOrChangedPromotionRow)(PropagateLocationParametersDV2[0].Row)).LocationKey =
                            ALocationRow.LocationKey;

                        if (CreateLocationOtherPartnerKeys.Length > 0)
                        {
//                          TLogging.LogAtLevel(9, "PerformLocationChangeChecks: Created Location " + NewLocationLocationKey.ToString() +
//                              ": should be assigned to " + Convert.ToInt32(CreateLocationOtherPartnerKeys.Length).ToString() + " Partners...");

                            // Build list of PartnerKeys for IN (x,y) clause in the SQL statement
                            for (Counter = 0; Counter <= CreateLocationOtherPartnerKeys.Length - 1; Counter += 1)
                            {
                                OtherPartnerKeys = OtherPartnerKeys + CreateLocationOtherPartnerKeys[Counter].ToString() + ',';
                            }

                            // remove last ','
                            OtherPartnerKeys = OtherPartnerKeys.Substring(0, OtherPartnerKeys.Length - 1);

                            // Load data for all the other selected Partners that reference
                            // the PartnerLocation
                            PartnerLocationModifyDS = new DataSet();
                            PartnerLocationModifyDS.Tables.Add(new PPartnerLocationTable());
                            ParametersArray = new OdbcParameter[2];
                            ParametersArray[0] = new OdbcParameter("", OdbcType.Decimal, 10);
                            ParametersArray[0].Value = (System.Object)(NewLocationRowSaved.SiteKey);
                            ParametersArray[1] = new OdbcParameter("", OdbcType.Int);
                            ParametersArray[1].Value = (System.Object)(AOriginalLocationKey.LocationKey);
                            PartnerLocationModifyDS = DBAccess.GDBAccessObj.Select(PartnerLocationModifyDS,
                                "SELECT * " + "FROM PUB_" + PPartnerLocationTable.GetTableDBName() + ' ' + "WHERE " +
                                PPartnerLocationTable.GetPartnerKeyDBName() + " IN (" + OtherPartnerKeys + ") " + "AND " +
                                PPartnerLocationTable.GetSiteKeyDBName() + " = ? " + "AND " + PPartnerLocationTable.GetLocationKeyDBName() + " = ?",
                                PPartnerLocationTable.GetTableName(),
                                ASubmitChangesTransaction,
                                ParametersArray);

                            // Change the LocationKey for every one of those PartnerLocation
                            // DataRows to point to the NEW Location
                            for (Counter = 0; Counter <= CreateLocationOtherPartnerKeys.Length - 1; Counter += 1)
                            {
                                ((PPartnerLocationTable)PartnerLocationModifyDS.Tables[0])[Counter].LocationKey = NewLocationLocationKey;
                            }

                            // Submit the changes to those PartnerLocations to the DB
                            PPartnerLocationAccess.SubmitChanges((PPartnerLocationTable)PartnerLocationModifyDS.Tables[0],
                                ASubmitChangesTransaction);
                        }
                        else
                        {
//                          TLogging.LogAtLevel(9, "PerformLocationChangeChecks: Created Location " + NewLocationLocationKey.ToString() + ": should not be assigned to any other Partners...");

                            /*
                             * Don't need to do anything here - the just created Location got already
                             * assigned to the Partner we are currently working with.
                             */
                        }
                    }
                    else if (UpdateLocation)
                    {
//                      TLogging.LogAtLevel(9, "PerformLocationChangeChecks: Location " + ALocationRow.LocationKey.ToString() +
//                              ": should simply get updated; therefore the Locations of ALL Partners will be changed...");

                        /*
                         * Don't need to do anything here - the changed Location will be saved
                         * in the call to SubmitChanges in the main loop of the SubmitData function.
                         */
                    }
                }

                ReturnValue = TSubmitChangesResult.scrOK;
            }
            else
            {
                TLogging.LogAtLevel(9, "PerformLocationChangeChecks: Location " + ALocationRow.LocationKey.ToString() +
                    ": User cancelled the selection - stopping the whole saving process!");

                /*
                 * User cancelled the selection - stop the whole saving process!
                 */
                AVerificationResult.Add(new TVerificationResult("Location Change Promotion: Information",
                        "No changes were saved because the Location Change Promotion dialog was cancelled by the user.", "Saving cancelled by user",
                        "",
                        TResultSeverity.Resv_Noncritical));
                ReturnValue = TSubmitChangesResult.scrError;
            }

            return ReturnValue;
        }
Example #32
0
        /// <summary>
        /// extend an extract from a list of best addresses
        /// </summary>
        /// <param name="AExtractId">Extract Id of the Extract to extend</param>
        /// <param name="APartnerKeysTable"></param>
        /// <param name="APartnerKeyColumn">number of the column that contains the partner keys</param>
        /// <param name="ASiteKeyColumn">number of the column that contains the site keys</param>
        /// <param name="ALocationKeyColumn">number of the column that contains the location keys</param>
        /// <param name="AKeyCount">The number of keys that were actually added to the extract (any duplicates are excluded)</param>
        /// <param name="AIgnoredKeyList">A reference to a List of long.  If not null the server will fill it with the partner keys that were ignored.  Does not include duplicates.</param>
        /// <param name="AIgnoreDuplicates">true if duplicates should be looked out for. Can be set to false if called only once and not several times per extract.</param>
        /// <param name="AIgnoreInactive">true if inactive partners should be ignored</param>
        /// <param name="AIgnoreNonMailingLocations">true to ignore if the partner's best address is a non-mailing location</param>
        /// <param name="AIgnoreNoSolicitations">true to ignore partners where the No Solicitations flag is set</param>
        public static void ExtendExtractFromListOfPartnerKeys(
            Int32 AExtractId,
            DataTable APartnerKeysTable,
            Int32 APartnerKeyColumn,
            Int32 ASiteKeyColumn,
            Int32 ALocationKeyColumn,
            out Int32 AKeyCount,
            out List <long> AIgnoredKeyList,
            bool AIgnoreDuplicates,
            bool AIgnoreInactive,
            bool AIgnoreNonMailingLocations,
            bool AIgnoreNoSolicitations)
        {
            int RecordCounter = 0;
            PPartnerLocationTable PartnerLocationKeysTable;
            Int64 PartnerKey;

            List <long> ignoredKeyList = new List <long>();

            TDBTransaction Transaction  = null;
            bool           SubmissionOK = true;

            DBAccess.GDBAccessObj.GetNewOrExistingAutoTransaction(IsolationLevel.Serializable, ref Transaction, ref SubmissionOK,
                                                                  delegate
            {
                // Pre-process the table to remove partner rows that do not match the filter requirements
                for (int i = APartnerKeysTable.Rows.Count - 1; i >= 0; i--)
                {
                    DataRow dr       = APartnerKeysTable.Rows[i];
                    Int64 partnerKey = Convert.ToInt64(dr[APartnerKeyColumn]);

                    // Get a partner record containing our fields of interest
                    StringCollection fields = new StringCollection();
                    fields.Add(PPartnerTable.GetStatusCodeDBName());
                    fields.Add(PPartnerTable.GetNoSolicitationsDBName());
                    DataTable dt = PPartnerAccess.LoadByPrimaryKey(partnerKey, fields, Transaction);

                    if (dt.Rows.Count > 0)
                    {
                        if (AIgnoreInactive || AIgnoreNoSolicitations)
                        {
                            bool isActive         = false;
                            bool isNoSolicitation = false;
                            object o = dt.Rows[0][PPartnerTable.GetStatusCodeDBName()];

                            if (o != null)
                            {
                                TStdPartnerStatusCode statusCode = SharedTypes.StdPartnerStatusCodeStringToEnum(o.ToString());
                                isActive = (statusCode == TStdPartnerStatusCode.spscACTIVE);
                            }

                            o = dt.Rows[0][PPartnerTable.GetNoSolicitationsDBName()];

                            if (o != null)
                            {
                                isNoSolicitation = Convert.ToBoolean(o);
                            }

                            if ((AIgnoreInactive && !isActive) || (AIgnoreNoSolicitations && isNoSolicitation))
                            {
                                ignoredKeyList.Add(partnerKey);
                                APartnerKeysTable.Rows.Remove(dr);
                            }
                        }
                    }
                    else
                    {
                        ignoredKeyList.Add(partnerKey);
                    }
                }

                MExtractTable ExtractTable = new MExtractTable();
                ExtractTable = MExtractAccess.LoadViaMExtractMaster(AExtractId, Transaction);

                // Location Keys need to be determined as extracts do not only need partner keys but
                // also Location Keys.
                DetermineBestLocationKeys(APartnerKeysTable, APartnerKeyColumn, ASiteKeyColumn,
                                          ALocationKeyColumn, out PartnerLocationKeysTable,
                                          Transaction);

                // use the returned table which contains partner and location keys to build the extract
                foreach (PPartnerLocationRow PartnerLocationRow in PartnerLocationKeysTable.Rows)
                {
                    PartnerKey = PartnerLocationRow.PartnerKey;

                    if (PartnerKey > 0)
                    {
                        if (AIgnoreNonMailingLocations)
                        {
                            // The PartnerLocationRow only contains the PK fields so now we need to get the SendMail column
                            StringCollection fields = new StringCollection();
                            fields.Add(PPartnerLocationTable.GetSendMailDBName());

                            PPartnerLocationTable t =
                                PPartnerLocationAccess.LoadByPrimaryKey(PartnerKey, PartnerLocationRow.SiteKey,
                                                                        PartnerLocationRow.LocationKey, fields, Transaction);

                            if ((t != null) && (t.Rows.Count > 0) && (((PPartnerLocationRow)t.Rows[0]).SendMail == false))
                            {
                                ignoredKeyList.Add(PartnerKey);
                                continue;
                            }
                        }

                        RecordCounter += 1;
                        TLogging.LogAtLevel(1, "Preparing Partner " + PartnerKey + " (Record Number " + RecordCounter + ")");

                        // add row for partner to extract and fill with contents
                        MExtractRow NewRow = ExtractTable.NewRowTyped();
                        NewRow.ExtractId   = AExtractId;
                        NewRow.PartnerKey  = PartnerKey;
                        NewRow.SiteKey     = Convert.ToInt64(PartnerLocationRow[PPartnerLocationTable.GetSiteKeyDBName()]);
                        NewRow.LocationKey = Convert.ToInt32(PartnerLocationRow[PPartnerLocationTable.GetLocationKeyDBName()]);

                        // only add row if it does not already exist for this partner
                        if (AIgnoreDuplicates || !ExtractTable.Rows.Contains(new object[] { NewRow.ExtractId, NewRow.PartnerKey, NewRow.SiteKey }))
                        {
                            ExtractTable.Rows.Add(NewRow);
                        }
                    }
                }

                if (ExtractTable.Rows.Count > 0)
                {
                    // update field in extract master for quick access to number of partners in extract
                    MExtractMasterTable ExtractMaster = MExtractMasterAccess.LoadByPrimaryKey(AExtractId, Transaction);
                    ExtractMaster[0].KeyCount         = ExtractTable.Rows.Count;

                    ExtractTable.ThrowAwayAfterSubmitChanges = true;     // no need to keep data as this increases speed significantly

                    MExtractAccess.SubmitChanges(ExtractTable, Transaction);

                    MExtractMasterAccess.SubmitChanges(ExtractMaster, Transaction);
                }
            });

            AKeyCount       = RecordCounter;
            AIgnoredKeyList = ignoredKeyList;
        }
        /// <summary>
        /// Check each Location DataRow before calling SubmitChanges
        /// to enforce Business Rules:
        /// - Added or changed Location: check for a similar Location record
        /// - if no similar Location record exists, save this Location record
        /// - if a similar Location record exists: allow choosing whether the
        ///    existing one should be used, or this Location record should be saved
        /// - Changed Location: don't save Location record if the data is actually
        ///     the same than before
        /// - Deleted Location: delete Location only if no other PartnerLocation
        ///      is referencing it
        /// - Deleted Location: remove references from any Extracts
        /// </summary>
        private static TSubmitChangesResult ProcessLocationChanges(
            PLocationTable ALocationTable,
            PPartnerLocationTable APartnerLocationTable,
            ref PartnerAddressAggregateTDS AResponseDS,
            TDBTransaction ASubmitChangesTransaction,
            Int64 APartnerKey,
            ref PartnerAddressAggregateTDSSimilarLocationParametersTable AExistingLocationParametersDT,
            ref TLocationPK[, ] ASimilarLocationReUseKeyMapping,
            ref PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable AAddressAddedOrChangedPromotionParametersDT,
            ref PartnerAddressAggregateTDSChangePromotionParametersTable AChangeLocationParametersDT,
            ref TVerificationResultCollection AVerificationResult)
        {
            TSubmitChangesResult Result = TSubmitChangesResult.scrOK;
            TSubmitChangesResult TmpResult;

            for (Int16 LocationCounter = 0; LocationCounter < ALocationTable.Rows.Count; LocationCounter++)
            {
                if ((ALocationTable.Rows[LocationCounter].RowState == DataRowState.Added)
                    || (ALocationTable.Rows[LocationCounter].RowState == DataRowState.Modified))
                {
                    if (ALocationTable[LocationCounter].LocationKey == 0)
                    {
                        throw new Exception("TPPartnerAddress.ProcessLocationChanges: must not add or modify the empty location");
                    }
                }

                if (ALocationTable.Rows[LocationCounter].RowState == DataRowState.Deleted)
                {
                    if (Convert.ToInt32(ALocationTable[LocationCounter][PLocationTable.GetLocationKeyDBName(),
                                                                        DataRowVersion.Original]) == 0)
                    {
                        throw new Exception("TPPartnerAddress.ProcessLocationChanges: must not delete the empty location");
                    }
                }

                if (ALocationTable.Rows[LocationCounter].RowState == DataRowState.Added)
                {
                    bool ReUseSimilarLocation = false;

                    // Check for reuse of a similar location in the DB
                    PLocationRow TmpRow = ALocationTable[LocationCounter];
                    TmpResult = PerformSimilarLocationReUseChecks(
                        ref TmpRow,
                        ref AResponseDS,
                        ASubmitChangesTransaction,
                        APartnerKey,
                        ref AExistingLocationParametersDT,
                        ref APartnerLocationTable,
                        ref ASimilarLocationReUseKeyMapping,
                        out ReUseSimilarLocation,
                        ref AVerificationResult);
//                  TLogging.LogAtLevel(8, "SubmitChanges: TmpRow.LocationKey after PerformSimilarLocationReUseChecks (1): " + TmpRow.LocationKey.ToString());

                    if (TmpResult != TSubmitChangesResult.scrOK)
                    {
                        // Stop processing here - we need a decision whether to re-use
                        // an existing Location or not (or the user tried to re-use a
                        // Location that is already used by this Partner, which is a
                        // user error)
                        return TmpResult;
                    }
                } // DataRowState.Added
                else if (ALocationTable.Rows[LocationCounter].RowState == DataRowState.Modified)
                {
                    if (CheckHasLocationChanged(ALocationTable[LocationCounter]))
                    {
                        bool ReUseSimilarLocation = false;

                        // Check for reuse of a similar location in the DB
                        PLocationRow TmpRow = ALocationTable[LocationCounter];
                        TmpResult = PerformSimilarLocationReUseChecks(ref TmpRow,
                            ref AResponseDS,
                            ASubmitChangesTransaction,
                            APartnerKey,
                            ref AExistingLocationParametersDT,
                            ref APartnerLocationTable,
                            ref ASimilarLocationReUseKeyMapping,
                            out ReUseSimilarLocation,
                            ref AVerificationResult);

//                      TLogging.LogAtLevel(9, "SubmitChanges: TmpRow.LocationKey after PerformSimilarLocationReUseChecks (2): " + TmpRow.LocationKey.ToString());

                        if (TmpResult != TSubmitChangesResult.scrOK)
                        {
                            // Stop processing here - we need a decision whether to re-use
                            // an existing Location or not (or the user tried to re-use a
                            // Location that is already used by this Partner, which is a
                            // user error)
                            return TmpResult;
                        }

                        if (!ReUseSimilarLocation)
                        {
                            // No similar Location exists, or an existing similar Location
                            // should not be reused
                            if (CheckHasPartnerLocationOtherPartnerReferences(ALocationTable[LocationCounter], APartnerKey,
                                    ASubmitChangesTransaction))
                            {
//                              TLogging.LogAtLevel(9, "SubmitChanges: Location " + ALocationTable[LocationCounter].LocationKey.ToString() + ": is used by other Partners as well.");

                                bool CreateLocationFlag;
                                TLocationPK OriginalLocationKey;

                                TmpResult =
                                    PerformLocationChangeChecks(ALocationTable[LocationCounter],
                                        APartnerKey,
                                        ref AResponseDS,
                                        ASubmitChangesTransaction,
                                        ref AAddressAddedOrChangedPromotionParametersDT,
                                        ref AChangeLocationParametersDT,
                                        ref APartnerLocationTable,
                                        ref AVerificationResult,
                                        out CreateLocationFlag,
                                        out OriginalLocationKey);

                                if (TmpResult != TSubmitChangesResult.scrOK)
                                {
                                    Result = TmpResult;

                                    if (Result == TSubmitChangesResult.scrError)
                                    {
                                        return Result;
                                    }
                                }

                                if (CreateLocationFlag)
                                {
                                    ModifyExistingLocationParameters(ALocationTable[LocationCounter],
                                        OriginalLocationKey,
                                        ref AExistingLocationParametersDT);

                                    // Make this location's DataRow undmodified because it should not be submitted to the database
                                    ALocationTable.Rows[LocationCounter].AcceptChanges();
                                    LocationCounter--;
                                }
                            } // if CheckHasPartnerLocationOtherPartnerReferences ... then
                        }
                    } // if CheckHasLocationChanged ... then
                    else
                    {
//                      TLogging.LogAtLevel(9, "Location " + ALocationTable[LocationCounter].LocationKey.ToString() + ": data has NOT changed -> will not be saved.");

                        // remove this location because it should not be submitted to the database
                        ALocationTable.Rows.RemoveAt(LocationCounter);
                        LocationCounter--;
                    }
                } // DataRowState.Modified
                else if (ALocationTable.Rows[LocationCounter].RowState == DataRowState.Deleted)
                {
//                  TLogging.LogAtLevel(9, "SubmitChanges: Location " + ALocationTable[LocationCounter]
//                      [PLocationTable.GetLocationKeyDBName(), DataRowVersion.Original].ToString() + ": has been marked for deletion.");

                    // Handle deletion of Location row: delete it only if no other PartnerLocation is referencing it
                    if (CheckHasPartnerLocationOtherPartnerReferences(ALocationTable[LocationCounter], APartnerKey, ASubmitChangesTransaction))
                    {
//                      TLogging.LogAtLevel(9,  TLogging.Log("SubmitChanges: Location " +
//                          ALocationTable[LocationCounter][PLocationTable.GetLocationKeyDBName(), DataRowVersion.Original].ToString() +
//                          ": has been marked for deletion and is used by others, so it won''t get deleted.");

                        // remove this location because it should not be submitted to the database
                        ALocationTable.Rows.RemoveAt(LocationCounter);
                        LocationCounter--;
                    }
                    else
                    {
//                      TLogging.LogAtLevel(9, "SubmitChanges: Location " + ALocationTable[LocationCounter][PLocationTable.GetLocationKeyDBName(),
//                          DataRowVersion.Original].ToString() + ": has been marked for deletion and will get deleted.");

                        // Any Extract in Petra that references this Location must no longer
                        // reference this Location since it will get deleted
                        RemoveLocationFromExtracts(ALocationTable[LocationCounter], ASubmitChangesTransaction);
                    }
                } // if LocationTable.Rows[LocationCounter].RowState = DataRowState.Deleted
                else if (ALocationTable.Rows[LocationCounter].RowState != DataRowState.Unchanged)
                {
                    throw new ArgumentException(
                        "SubmitChanges can only deal with Locations of DataRowState Added, Modified or Deleted, but not with " +
                        (Enum.GetName(typeof(DataRowState), ALocationTable.Rows[LocationCounter].RowState)));
                }
            }

            return Result;
        }
Example #34
0
        /// <summary>
        /// Determine best location for partner out of a list of possible locations. Or simply find best one
        /// if no suggestion is made.
        /// </summary>
        /// <param name="APartnerKey"></param>
        /// <param name="ALocationKeyList"></param>
        /// <param name="APartnerLocationKeysTable"></param>
        /// <param name="ATransaction"></param>
        /// <returns>True if the address was found and added, otherwise false.</returns>
        private static Boolean DetermineAndAddBestLocationKey(
            Int64 APartnerKey,
            List <TLocationPK> ALocationKeyList,
            ref PPartnerLocationTable APartnerLocationKeysTable,
            TDBTransaction ATransaction)
        {
            PPartnerLocationTable AllPartnerLocationTable;
            PPartnerLocationTable FilteredPartnerLocationTable = new PPartnerLocationTable();
            TLocationPK           LocationPK = new TLocationPK();
            PPartnerLocationRow   PartnerLocationKeyRow;
            PPartnerLocationRow   PartnerLocationRowCopy;
            TLocationPK           BestLocationPK;

            if (ALocationKeyList.Count == 0)
            {
                // no list suggested: find best address in db for this partner
                BestLocationPK = TMailing.GetPartnersBestLocation(APartnerKey);
            }
            else if (ALocationKeyList.Count == 1)
            {
                // only one location suggested: take this one
                BestLocationPK = ALocationKeyList[0];
            }
            else
            {
                // Process location key list related to partner.
                // In order to use Calculations.DetermineBestAddress we need to first retrieve full data
                // for all suggested records from the db. Therefore load all locations for this partner
                // and then create a table of the ones that are suggested.
                AllPartnerLocationTable = PPartnerLocationAccess.LoadViaPPartner(APartnerKey, ATransaction);

                foreach (PPartnerLocationRow PartnerLocationRow in AllPartnerLocationTable.Rows)
                {
                    LocationPK.SiteKey     = PartnerLocationRow.SiteKey;
                    LocationPK.LocationKey = PartnerLocationRow.LocationKey;

                    if (ALocationKeyList.Contains(LocationPK))
                    {
                        PartnerLocationRowCopy = (PPartnerLocationRow)FilteredPartnerLocationTable.NewRow();
                        DataUtilities.CopyAllColumnValues(PartnerLocationRow, PartnerLocationRowCopy);
                        FilteredPartnerLocationTable.Rows.Add(PartnerLocationRowCopy);
                    }
                }

                BestLocationPK = Calculations.DetermineBestAddress(FilteredPartnerLocationTable);
            }

            // create new row, initialize it and add it to the table
            if (BestLocationPK.LocationKey != -1)
            {
                PartnerLocationKeyRow = (PPartnerLocationRow)APartnerLocationKeysTable.NewRow();
                PartnerLocationKeyRow[PPartnerLocationTable.GetPartnerKeyDBName()]  = APartnerKey;
                PartnerLocationKeyRow[PPartnerLocationTable.GetSiteKeyDBName()]     = BestLocationPK.SiteKey;
                PartnerLocationKeyRow[PPartnerLocationTable.GetLocationKeyDBName()] = BestLocationPK.LocationKey;

                // only add row if it does not already exist
                if (!APartnerLocationKeysTable.Rows.Contains(
                        new object[] { PartnerLocationKeyRow.PartnerKey, PartnerLocationKeyRow.SiteKey, PartnerLocationKeyRow.LocationKey }))
                {
                    APartnerLocationKeysTable.Rows.Add(PartnerLocationKeyRow);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
 /// <summary>
 /// get the current address from a location table
 /// </summary>
 /// <param name="ATable">table with locations</param>
 /// <returns>data view containing the current address</returns>
 public static DataView DetermineCurrentAddresses(PPartnerLocationTable ATable)
 {
     // dd/MM/yyyy did not work on Mono on Mac
     // see also http://www.csharp-examples.net/dataview-rowfilter/
     return new DataView(ATable, "((" + PPartnerLocationTable.GetDateEffectiveDBName() + " <= #" + DateTime.Now.Date.ToString(
             "yyyy-MM-dd") + "# OR " + PPartnerLocationTable.GetDateEffectiveDBName() + " IS NULL) AND (" +
         PPartnerLocationTable.GetDateGoodUntilDBName() + " >= #" + DateTime.Now.Date.ToString(
             "yyyy-MM-dd") + "# OR " + PPartnerLocationTable.GetDateGoodUntilDBName() + " IS NULL))", "", DataViewRowState.CurrentRows);
 }
Example #36
0
        /// <summary>
        /// Creates a new DataRow in the passed PLocation and PPartnerLocation tables.
        /// </summary>
        /// <param name="ALocationDT">Typed PLocation table. If null is passed in it is created
        /// automatically</param>
        /// <param name="APartnerLocationDT">Typed PPartnerLocation table. If null is passed in
        /// it is created automatically</param>
        /// <param name="APartnerKey">PartneKey of the Partner for which the Address should
        /// be created</param>
        /// <param name="APartnerClass">PartnerClass of the Partner for which the Address should
        /// be created</param>
        /// <param name="ACountryCode"></param>
        /// <param name="ANewLocationKey">A LocationKey that the new Location and
        /// PartnerLocation rows would be set to.</param>
        /// <param name="ACopyFromFamilyPartnerKey">Pass in the PartnerKey of a Family Partner
        /// to copy over all data from the Family's Address
        /// (ACopyFromPartnerLocationKey must be -1 to be legal). Default: -1
        /// (=no copy). Note: the DataRow specified with ACopyFromFamilyPartnerKey must
        /// be present in the APartnerLocationDT table!</param>
        /// <param name="ACopyFromFamilyLocationKey">Pass in the LocationKey of the Family
        /// Partner specified in ACopyFromFamilyPartnerKey to copy over all data from
        /// the Family's Address (ACopyFromPartnerLocationKey must be -1 to be legal)
        /// Default: -1 (=no copy). Note: the DataRow specified with
        /// ACopyFromFamilyLocationKey must be present in both the ALocationDT and
        /// APartnerLocationDT table!</param>
        /// <param name="ACopyFromFamilySiteKey">A SiteKey to find the location that should be the
        /// source of the copy</param>
        /// <param name="ACopyFromFamilyOnlyLocation"></param>
        /// <param name="ADeleteDataRowCopiedFrom"></param>
        /// <exception cref="ArgumentException">If any of the Arguments (or their combination) is
        /// not valid.</exception>
        public static void CreateNewAddressInternal(PLocationTable ALocationDT,
                                                    PPartnerLocationTable APartnerLocationDT,
                                                    Int64 APartnerKey,
                                                    TPartnerClass APartnerClass,
                                                    String ACountryCode,
                                                    Int32 ANewLocationKey,
                                                    Int64 ACopyFromFamilyPartnerKey,
                                                    Int32 ACopyFromFamilyLocationKey,
                                                    Int64 ACopyFromFamilySiteKey,
                                                    Boolean ACopyFromFamilyOnlyLocation,
                                                    Boolean ADeleteDataRowCopiedFrom)
        {
            PLocationRow        NewLocationsRow;
            PLocationRow        CopyLocationsRow;
            PPartnerLocationRow NewPartnerLocationRow;
            PPartnerLocationRow CopyPartnerLocationRow;

            #region Check parameters

            if (ALocationDT == null)
            {
                ALocationDT = new PLocationTable();
            }

            if (APartnerLocationDT == null)
            {
                APartnerLocationDT = new PPartnerLocationTable();
            }

            if (TStaticDataTables.TMPartner.GetStaticTable(TStaticPartnerTablesEnum.PartnerClassList).Rows.Find(APartnerClass) == null)
            {
                throw new ArgumentException("APartnerClass needs to be a valid Partner Class");
            }

            if ((ACopyFromFamilyPartnerKey != -1) || (ACopyFromFamilyLocationKey != -99))
            {
                if (ACopyFromFamilyPartnerKey != -1)
                {
                    if (ACopyFromFamilyLocationKey == -99)
                    {
                        throw new ArgumentException("ACopyFromFamilyLocationKey needs to be specified if ACopyFromFamilyPartnerKey is specified");
                    }
                }

                if (ACopyFromFamilyLocationKey != -99)
                {
                    if (ACopyFromFamilyPartnerKey == -1)
                    {
                        throw new ArgumentException("ACopyFromFamilyPartnerKey needs to be specified if ACopyFromFamilyLocationKey is specified");
                    }
                }
            }

            #endregion

            /*
             * Add new Locations row
             */
            NewLocationsRow = ALocationDT.NewRowTyped(true);

            // Assign Primary Key columns
            NewLocationsRow.SiteKey     = SharedConstants.FIXED_SITE_KEY; // TODO: use s_system_parameter.s_site_key_n once p_partner_location actually uses the Petra System SiteKey in the PrimaryKey (instead of 0, which is used currently)
            NewLocationsRow.LocationKey = ANewLocationKey;

            // Copy over Columns of the Row specified with ACopyFromFamilyLocationKey?
            if (ACopyFromFamilyLocationKey != -99)
            {
                CopyLocationsRow = (PLocationRow)ALocationDT.Rows.Find(new Object[] { ACopyFromFamilySiteKey, ACopyFromFamilyLocationKey });

                if (CopyLocationsRow != null)
                {
                    if (ACountryCode != "")
                    {
                        throw new ArgumentException(
                                  "ACountryCode must not be specified because it would be overwritten by the value from the row specified with ACopyFromFamilyLocationKey");
                    }

                    CopyLocationData(CopyLocationsRow, NewLocationsRow);

                    if (ADeleteDataRowCopiedFrom)
                    {
                        CopyLocationsRow.Delete();
                        CopyLocationsRow.AcceptChanges();
                    }
                }
                else
                {
                    throw new ArgumentException("Row with specified ACopyFromFamilyLocationKey cannot be found in PLocations DataTable!");
                }
            }
            else
            {
                // No copying > assign values of columns manually
                NewLocationsRow.CountryCode = ACountryCode;
            }

            NewLocationsRow.CreatedBy = UserInfo.GUserInfo.UserID;
            ALocationDT.Rows.Add(NewLocationsRow);

            // If an existing Location shouldn't be copied to a temp LocationKey (<0) then make this DataRow unchanged
            // (prevents this row from getting commited to the PetraServer).
            if (ANewLocationKey >= 0)
            {
                NewLocationsRow.AcceptChanges();
            }

            /*
             * Add new PartnerLocations row
             */
            NewPartnerLocationRow = APartnerLocationDT.NewRowTyped(true);

            // Assign Primary Key columns
            NewPartnerLocationRow.PartnerKey  = APartnerKey;
            NewPartnerLocationRow.SiteKey     = SharedConstants.FIXED_SITE_KEY; // TODO: use s_system_parameter.s_site_key_n once p_partner_location actually uses the Petra System SiteKey in the PrimaryKey (instead of 0, which is used currently)
            NewPartnerLocationRow.LocationKey = ANewLocationKey;

            // Copy over Columns of the Row specified with ACopyFromFamilyPartnerKey
            // and ACopyFromFamilyLocationKey?
            if ((!ACopyFromFamilyOnlyLocation) && (ACopyFromFamilyLocationKey != -99))
            {
                CopyPartnerLocationRow =
                    (PPartnerLocationRow)APartnerLocationDT.Rows.Find(new Object[] { ACopyFromFamilyPartnerKey, ACopyFromFamilySiteKey,
                                                                                     ACopyFromFamilyLocationKey });

                if (CopyPartnerLocationRow != null)
                {
                    // Security check for LocationType SECURITY_CAN_LOCATIONTYPE
                    if (((CopyPartnerLocationRow.LocationType.EndsWith(SharedConstants.SECURITY_CAN_LOCATIONTYPE) &&
                          (UserInfo.GUserInfo.IsInGroup(SharedConstants.PETRAGROUP_ADDRESSCAN)))) ||
                        (!((CopyPartnerLocationRow.LocationType.EndsWith(SharedConstants.SECURITY_CAN_LOCATIONTYPE)))))
                    {
                        // Copy over all columns, except the Primary Key columns and the last four
                        // columns (containing creation and change information)
                        NewPartnerLocationRow.LocationType = CopyPartnerLocationRow.LocationType;

                        if (!CopyPartnerLocationRow.IsDateGoodUntilNull())
                        {
                            // Copy over DateGoodUntil only if it doesn't lie in the past
                            if (CopyPartnerLocationRow.DateGoodUntil >= DateTime.Today)
                            {
                                NewPartnerLocationRow.DateGoodUntil = CopyPartnerLocationRow.DateGoodUntil;
                            }
                        }

                        if (ADeleteDataRowCopiedFrom)
                        {
                            CopyPartnerLocationRow.Delete();
                            CopyPartnerLocationRow.AcceptChanges();
                        }
                    }
                    else
                    {
                        throw new ESecurityGroupAccessDeniedException(
                                  "Address copying denied: " + "The Location Type of the Address that should be copied is '" +
                                  CopyPartnerLocationRow.LocationType + "' but you are not in Security Group '" + SharedConstants.PETRAGROUP_ADDRESSCAN +
                                  "'!");
                    }
                }
                else
                {
                    throw new ArgumentException("Row with specified ACopyFromFamilyPartnerKey cannot be found in PPartnerLocations DataTable!");
                }
            }
            else
            {
                NewPartnerLocationRow.LocationType = TSharedAddressHandling.GetDefaultLocationType(APartnerClass);
            }

            NewPartnerLocationRow.DateEffective = DateTime.Today;
            NewPartnerLocationRow.CreatedBy     = UserInfo.GUserInfo.UserID;

            if (APartnerClass == TPartnerClass.PERSON)
            {
                NewPartnerLocationRow.SendMail = false;
            }
            else
            {
                NewPartnerLocationRow.SendMail = true;
            }

            APartnerLocationDT.Rows.Add(NewPartnerLocationRow);
        }
        /// <summary>
        /// For all members of the Family: if they have a PartnerLocation mapped to the
        /// same Location as the Family then update the DateGoodUntil column.
        ///
        /// @comment Must only be called for Partners of Partner Class FAMILY - the
        /// function does no checks on that and will fail for other Partner Classes!
        ///
        /// </summary>
        /// <param name="AFamilyPartnerKey">PartnerKey of the Family</param>
        /// <param name="APartnerLocationDR">PartnerLocation DataRow of the Family</param>
        /// <param name="ASubmitChangesTransaction">Running transaction in which the DB commands
        /// will be enlisted</param>
        private static void PromoteToFamilyMembersDateGoodUntilChange(Int64 AFamilyPartnerKey,
            PPartnerLocationRow APartnerLocationDR,
            TDBTransaction ASubmitChangesTransaction)
        {
            StringCollection RequiredColumns;
            PPersonTable FamilyPersonsDT;
            PPartnerLocationTable PartnerLocationDT;
            PPartnerLocationTable PartnerLocationSubmitDT;
            PPartnerLocationRow PartnerLocationSubmitDR;
            Int16 Counter;

            FamilyPersonsDT = GetFamilyMemberPartnerKeys(AFamilyPartnerKey, ASubmitChangesTransaction);
            PartnerLocationSubmitDT = new PPartnerLocationTable();
            RequiredColumns = new StringCollection();
            RequiredColumns.Add(PPartnerLocationTable.GetModificationIdDBName());

            /*
             * For all members of the Family: if they have a PartnerLocation mapped to the
             * same Location as the Family then update the DateGoodUntil column
             */
//          TLogging.LogAtLevel(9, "PromoteToFamilyMembersDateGoodUntilChange for Location " + APartnerLocationDR.LocationKey.ToString() + ": Family has " + FamilyPersonsDT.Rows.Count.ToString() + " members.");

            for (Counter = 0; Counter <= FamilyPersonsDT.Rows.Count - 1; Counter += 1)
            {
                // Load ModificationId for the PartnerLocation of each Person (otherwise we
                // will run into optimistic locking problems!)
                PartnerLocationDT = PPartnerLocationAccess.LoadByPrimaryKey(
                    FamilyPersonsDT[Counter].PartnerKey,
                    APartnerLocationDR.SiteKey,
                    APartnerLocationDR.LocationKey,
                    RequiredColumns,
                    ASubmitChangesTransaction);

                /*
                 * Check that found Person has PartnerLocation that is mapped to the same
                 * Location than the one that the PartnerLocation of the Family that we are
                 * processing is mapped to
                 */
                if (PartnerLocationDT.Rows.Count != 0)
                {
                    // Build row to be able to update that PartnerLocation
                    PartnerLocationSubmitDR = PartnerLocationSubmitDT.NewRowTyped(false);
                    PartnerLocationSubmitDR.PartnerKey = FamilyPersonsDT[Counter].PartnerKey;
                    PartnerLocationSubmitDR.SiteKey = APartnerLocationDR.SiteKey;
                    PartnerLocationSubmitDR.LocationKey = APartnerLocationDR.LocationKey;
                    PartnerLocationSubmitDR.ModificationId = PartnerLocationDT[0].ModificationId;
                    PartnerLocationSubmitDT.Rows.Add(PartnerLocationSubmitDR);

                    // Change its DateGoodUntil column to match the Family's PartnerLocation DateGoodUntil column
                    if (!APartnerLocationDR.IsDateGoodUntilNull())
                    {
                        // Make this row unchanged so that SubmitChanges later picks up a changed
                        // DataRow, and not a new DataRow.
                        PartnerLocationSubmitDR.AcceptChanges();

                        // Now change DateGoodUntil
                        PartnerLocationSubmitDR.DateGoodUntil = APartnerLocationDR.DateGoodUntil;
                    }
                    else
                    {
                        // Change DataRow to a dummy value to make Null different to what was
                        // there before
                        PartnerLocationSubmitDR.DateGoodUntil = DateTime.MinValue;
                        PartnerLocationSubmitDR.AcceptChanges();

                        // Now change DateGoodUntil
                        PartnerLocationSubmitDR.SetDateGoodUntilNull();
                    }

                    // Get PartnerLocation data of just processed Person out of memory!
                    PartnerLocationDT.Rows.Clear();
                }
            }

            /*
             * Save changes to DateGoodUntil columns if PartnerLocation of any FamilyMember got changed.
             */
            if (PartnerLocationSubmitDT.Rows.Count > 0)
            {
                // Submit the changes to the DB
                PPartnerLocationAccess.SubmitChanges(PartnerLocationSubmitDT, ASubmitChangesTransaction);
            }
        }
        /// <summary>
        /// todoComment
        /// </summary>
        /// <param name="APartnerKey"></param>
        /// <param name="ASiteKey"></param>
        /// <param name="APartnerLocationTable"></param>
        /// <param name="ATransaction"></param>
        private static void MakeSureLocation0IsNotPresent(Int64 APartnerKey,
            Int64 ASiteKey,
            PPartnerLocationTable APartnerLocationTable,
            TDBTransaction ATransaction)
        {
            PPartnerLocationTable TemplateDT;
            PPartnerLocationRow TemplateRow;

            if (APartnerLocationTable.Select(PPartnerLocationTable.GetPartnerKeyDBName() + " = " + APartnerKey.ToString() + " AND " +
                    PPartnerLocationTable.GetSiteKeyDBName() + " = " + ASiteKey.ToString() + " AND " + PPartnerLocationTable.GetSiteKeyDBName() +
                    " = 0", "",
                    DataViewRowState.Deleted).Length == 0)
            {
                TemplateDT = new PPartnerLocationTable();
                TemplateRow = TemplateDT.NewRowTyped(false);
                TemplateRow.PartnerKey = APartnerKey;
                TemplateRow.SiteKey = ASiteKey;
                TemplateRow.LocationKey = 0;

                /*
                 * Currently we need to do a count before issuing the delete command.
                 * If we don't do this, the DataStore raises a System.Data.Odbc.OdbcException
                 * if such a row doesn't exist. The DataStore should really be fixed, then
                 * counting before execution should not be necessary, which would be faster.
                 */
                if (Convert.ToInt32(PPartnerLocationAccess.CountUsingTemplate(TemplateRow, null, ATransaction)) > 0)
                {
                    PPartnerLocationAccess.DeleteUsingTemplate(TemplateRow, null, ATransaction);
//                  TLogging.LogAtLevel(9, "MakeSureLocation0IsNotPresent: Deleted PPartnerLoction that referenced Location 0.");
                }
            }
            else
            {
//              TLogging.LogAtLevel(9, "MakeSureLocation0IsNotPresent: Submitted DataSet contains a Deleted PPartnerLoction Location 0; will get deleted later.");
            }
        }
        /// <summary>
        /// todoComment
        /// </summary>
        /// <param name="APartnerKey"></param>
        /// <param name="ASiteKey"></param>
        /// <param name="ALocationKey"></param>
        /// <param name="AReadTransaction"></param>
        /// <returns></returns>
        public static Boolean CheckHasPartnerLocationOtherPartnerReferences(Int64 APartnerKey,
            Int64 ASiteKey,
            Int32 ALocationKey,
            TDBTransaction AReadTransaction)
        {
            Boolean ReturnValue;
            PPartnerLocationTable TemplateTable;
            PPartnerLocationRow TemplateRow;
            StringCollection TemplateOperators;
            int OtherPartnerLocationReferences;

//          TLogging.LogAtLevel(9, "CheckHasPartnerLocationOtherPartnerReferences for Location " + ALocationKey.ToString());

            if (ALocationKey != 0)
            {
                // Look for other Partners that reference the specified PartnerLocation
                TemplateTable = new PPartnerLocationTable();
                TemplateRow = TemplateTable.NewRowTyped(false);
                TemplateRow.PartnerKey = APartnerKey;
                TemplateRow.SiteKey = ASiteKey;
                TemplateRow.LocationKey = ALocationKey;
                TemplateOperators = new StringCollection();
                TemplateOperators.Add("<>");
                OtherPartnerLocationReferences = PPartnerLocationAccess.CountUsingTemplate(TemplateRow, TemplateOperators, AReadTransaction);
//              TLogging.LogAtLevel(9, "CheckHasPartnerLocationOtherPartnerReferences: Location " + ALocationKey.ToString() + ": is used by " +
//                  OtherPartnerLocationReferences.ToString() + " other Partners.");
                ReturnValue = (OtherPartnerLocationReferences > 0);
            }
            else
            {
                /*
                 * Special case: for Location 0 we always return 'is referenced' - it is a
                 * dummy record (signalising 'Partner has no address') that is seen as
                 * beeing referenced all the time (even if no Partner is referencing it).
                 */
//              TLogging.LogAtLevel(9, "CheckHasPartnerLocationOtherPartnerReferences: Location " + ALocationKey.ToString() +
//                  ": is Location 0, therefore it is seen as beeing used by other Partners.");
                ReturnValue = true;
            }

            return ReturnValue;
        }
        /// <summary>
        ///  Check each PartnerLocation DataRow before calling SubmitChanges
        ///  to enforce Business Rules:
        ///  - Added PartnerLocation:
        ///  - if working with a PartnerLocation of a FAMILY:
        ///  - Added PartnerLocation: if working with a Location of a FAMILY: allow
        ///  choosing whether this PartnerLocation should be added to all PERSONs
        ///  in the FAMILY
        ///  - make sure that Location 0 is no longer mapped to this Partner.
        ///  - Modified Location:
        ///  - if working with a PartnerLocation of a FAMILY:
        ///  - check whether other Partners are referencing it, and if so,
        ///  allow choosing which of the Partners (or none or all) should be
        ///  affected by the change
        ///  - if the value in the DateGoodUntil column has changed, silently
        ///  update it for all PERSONs of a FAMILY that have the same LocationKey.
        ///  - Deleted PartnerLocation: check whether this is the last
        ///  PartnerLocation that is left for this Partner. If this is the case,
        ///  don't delete the PartnerLocation, but set it's LocationKey to 0.
        /// </summary>
        private static TSubmitChangesResult ProcessPartnerLocationChanges(
            PPartnerLocationTable PartnerLocationTable,
            ref PartnerAddressAggregateTDS AResponseDS,
            TDBTransaction ASubmitChangesTransaction,
            Int64 APartnerKey,
            String APartnerClass,
            ref TLocationPK[, ] ASimilarLocationReUseKeyMapping,
            ref PartnerAddressAggregateTDSSimilarLocationParametersTable AExistingLocationParametersDT,
            ref PartnerAddressAggregateTDSAddressAddedOrChangedPromotionTable AAddressAddedOrChangedPromotionParametersDT,
            ref PartnerAddressAggregateTDSChangePromotionParametersTable AChangeLocationParametersDT,
            ref TVerificationResultCollection AVerificationResult)
        {
            TSubmitChangesResult Result = TSubmitChangesResult.scrOK;
            TSubmitChangesResult TmpResult;

            for (Int16 PartnerLocationCounter = 0; PartnerLocationCounter <= PartnerLocationTable.Rows.Count - 1; PartnerLocationCounter += 1)
            {
                switch (PartnerLocationTable.Rows[PartnerLocationCounter].RowState)
                {
                    case DataRowState.Added:

                        if (PartnerLocationTable[PartnerLocationCounter].LocationKey != 0)
                        {
                            /*
                             * PartnerLocation of a FAMILY: Family Members promotion
                             */
                            if (APartnerClass == SharedTypes.PartnerClassEnumToString(TPartnerClass.FAMILY))
                            {
                                bool PerformPropagation = false;

                                TmpResult = PerformLocationFamilyMemberPropagationChecks(
                                    PartnerLocationTable[PartnerLocationCounter],
                                    ref AResponseDS,
                                    ASubmitChangesTransaction,
                                    APartnerKey,
                                    APartnerClass,
                                    ref AAddressAddedOrChangedPromotionParametersDT,
                                    ref PartnerLocationTable,
                                    AExistingLocationParametersDT,
                                    ASimilarLocationReUseKeyMapping,
                                    out PerformPropagation,
                                    ref AVerificationResult);

                                if (TmpResult != TSubmitChangesResult.scrOK)
                                {
                                    Result = TmpResult;

                                    if (TmpResult == TSubmitChangesResult.scrError)
                                    {
                                        return TmpResult;
                                    }
                                }

                                if (PerformPropagation)
                                {
                                    ModifyAddressAddedOrChangedParameters(PartnerLocationTable[PartnerLocationCounter],
                                        ref AAddressAddedOrChangedPromotionParametersDT);
                                }
                            }

                            /*
                             * Since a new Location has been added, we need to make sure that
                             * Location 0 is no longer mapped to this Partner!
                             */
                            MakeSureLocation0IsNotPresent(APartnerKey,
                                PartnerLocationTable[PartnerLocationCounter].SiteKey,
                                PartnerLocationTable,
                                ASubmitChangesTransaction);
                        }
                        else
                        {
                            MakeSureLocation0SavingIsAllowed(PartnerLocationTable[PartnerLocationCounter],
                                APartnerKey,
                                ASubmitChangesTransaction);
                        }

                        break;

                    case DataRowState.Modified:

                        /*
                         * PartnerLocation of a FAMILY: Family Members promotion
                         */
                        if (APartnerClass == SharedTypes.PartnerClassEnumToString(TPartnerClass.FAMILY))
                        {
                            /*
                             * If the value in the DateGoodUntil column has changed, silently
                             * update it for all PERSONs of this FAMILY that have the same
                             * LocationKey.
                             */
                            if (TSaveConvert.ObjectToDate(PartnerLocationTable[PartnerLocationCounter][PPartnerLocationTable.
                                                                                                       GetDateGoodUntilDBName(),
                                                                                                       DataRowVersion.Original]) !=
                                TSaveConvert.ObjectToDate(PartnerLocationTable[PartnerLocationCounter][PPartnerLocationTable.
                                                                                                       GetDateGoodUntilDBName(),
                                                                                                       DataRowVersion.Current]))
                            {
//                              TLogging.LogAtLevel(8, "SubmitChanges: PartnerLocation of a FAMILY: DateGoodUntil has changed -> promoting change to FAMILY members...");

                                PromoteToFamilyMembersDateGoodUntilChange(APartnerKey, PartnerLocationTable[PartnerLocationCounter],
                                    ASubmitChangesTransaction);
                            }
                        }

                        break;

                    case DataRowState.Deleted:

                        /*
                         * PPartnerLocation must not get deleted if it is the last one of the
                         * Partner, but must get mapped to Location 0 instead!
                         */

                        // Make sure that Location 0 can never get deleted!
                        if (Convert.ToInt32(PartnerLocationTable[PartnerLocationCounter][PPartnerLocationTable.GetLocationKeyDBName(),
                                                                                         DataRowVersion.Original]) != 0)
                        {
                            // Some other Location than Location 0 is about to be deleted!
                            // Check in the in-memory PartnerLocation Table first...
                            DataRow[] ChangePartnerLocationKeyRows = PartnerLocationTable.Select(
                                PPartnerLocationTable.GetPartnerKeyDBName() + " = " + APartnerKey.ToString() + " AND " +
                                PPartnerLocationTable.GetLocationKeyDBName() + " <> " +
                                PartnerLocationTable[PartnerLocationCounter][PPartnerLocationTable.GetLocationKeyDBName(),
                                                                             DataRowVersion.Original].ToString(), "",
                                DataViewRowState.CurrentRows);

                            if (ChangePartnerLocationKeyRows.Length == 0)
                            {
                                // No PPartnerLocation that is not deleted is left in
                                // PartnerLocationTable > now check for deleted ones
                                DataView DeletedPartnerLocationsDV = new DataView(PartnerLocationTable, "", "", DataViewRowState.Deleted);
                                int[] DeletedPartnerLocationKeys = new int[DeletedPartnerLocationsDV.Count];

                                for (Int16 DeletedPartnerLocationsCounter = 0;
                                     DeletedPartnerLocationsCounter <= DeletedPartnerLocationsDV.Count - 1;
                                     DeletedPartnerLocationsCounter += 1)
                                {
                                    DeletedPartnerLocationKeys[DeletedPartnerLocationsCounter] =
                                        Convert.ToInt32(DeletedPartnerLocationsDV[DeletedPartnerLocationsCounter].Row[PPartnerLocationTable.
                                                                                                                      GetLocationKeyDBName(),
                                                                                                                      DataRowVersion.Original
                                            ]);
                                }

                                // now check in the DB as well
                                if (!CheckHasPartnerOtherPartnerLocations(DeletedPartnerLocationKeys, APartnerKey, ASubmitChangesTransaction))
                                {
                                    // 'Undelete' DataRow and make it point to Location 0
                                    // (dummy Location) > will get submitted lateron!
                                    PartnerLocationTable[PartnerLocationCounter].RejectChanges();
                                    PartnerLocationTable[PartnerLocationCounter].LocationKey = 0;
//                                  TLogging.LogAtLevel(8, "SubmitChanges: PPartnerLocation " +
//                                      PartnerLocationTable[PartnerLocationCounter][PPartnerLocationTable.GetLocationKeyDBName(),
//                                      DataRowVersion.Original].ToString() + ": was last PartnerLocation, so its LocationKey got set to 0 (will be submitted lateron)!");
                                }
                            }
                            else
                            {
                                // There is at least one PPartnerLocation that is not deleted
                                // left in PartnerLocationTable, so the current PPartnerLocation
                                // can't be the last one > nothing to do.
                            }
                        }
                        else
                        {
                            DataRow[] ChangePartnerLocationKeyRows = PartnerLocationTable.Select(
                                PPartnerLocationTable.GetPartnerKeyDBName() + " = " + APartnerKey.ToString() + " AND " +
                                PPartnerLocationTable.GetLocationKeyDBName() + " = 0 ", "", DataViewRowState.CurrentRows);
//                          TLogging.LogAtLevel(8, "SubmitChanges: ChangePartnerLocationKeyRows Length: " + Convert.ToInt16(ChangePartnerLocationKeyRows.Length).ToString());

                            if (ChangePartnerLocationKeyRows.Length != 0)
                            {
                                // remove this location because it should not be submitted to the database
                                PartnerLocationTable.Rows.RemoveAt(PartnerLocationCounter);
                                PartnerLocationCounter--;

//                              TLogging.LogAtLevel(8, "SubmitChanges: Extra Location 0 row won''t be submitted lateron");
                            }
                        }

                        break;

                    case DataRowState.Unchanged:
                        break;

                    default:
                        throw new ArgumentException(
                        "SubmitChanges can only deal with PartnerLocations of DataRowState Added, Modified or Deleted, but not with " +
                        (Enum.GetName(typeof(DataRowState), PartnerLocationTable.Rows[PartnerLocationCounter].RowState)));
                }
            }

            return Result;
        }
Example #41
0
        /// <summary>
        /// Determine best location for partner out of a list of possible locations. Or simply find best one
        /// if no suggestion is made.
        /// </summary>
        /// <param name="APartnerKey"></param>
        /// <param name="ALocationKeyList"></param>
        /// <param name="APartnerLocationKeysTable"></param>
        /// <param name="ATransaction"></param>
        /// <returns>True if the address was found and added, otherwise false.</returns>
        private static Boolean DetermineAndAddBestLocationKey(
            Int64 APartnerKey,
            List <TLocationPK>ALocationKeyList,
            ref PPartnerLocationTable APartnerLocationKeysTable,
            TDBTransaction ATransaction)
        {
            PPartnerLocationTable AllPartnerLocationTable;
            PPartnerLocationTable FilteredPartnerLocationTable = new PPartnerLocationTable();
            TLocationPK LocationPK = new TLocationPK();
            PPartnerLocationRow PartnerLocationKeyRow;
            PPartnerLocationRow PartnerLocationRowCopy;
            TLocationPK BestLocationPK;

            if (ALocationKeyList.Count == 0)
            {
                // no list suggested: find best address in db for this partner
                BestLocationPK = TMailing.GetPartnersBestLocation(APartnerKey);
            }
            else if (ALocationKeyList.Count == 1)
            {
                // only one location suggested: take this one
                BestLocationPK = ALocationKeyList[0];
            }
            else
            {
                // Process location key list related to partner.
                // In order to use Calculations.DetermineBestAddress we need to first retrieve full data
                // for all suggested records from the db. Therefore load all locations for this partner
                // and then create a table of the ones that are suggested.
                AllPartnerLocationTable = PPartnerLocationAccess.LoadViaPPartner(APartnerKey, ATransaction);

                foreach (PPartnerLocationRow PartnerLocationRow in AllPartnerLocationTable.Rows)
                {
                    LocationPK.SiteKey = PartnerLocationRow.SiteKey;
                    LocationPK.LocationKey = PartnerLocationRow.LocationKey;

                    if (ALocationKeyList.Contains(LocationPK))
                    {
                        PartnerLocationRowCopy = (PPartnerLocationRow)FilteredPartnerLocationTable.NewRow();
                        DataUtilities.CopyAllColumnValues(PartnerLocationRow, PartnerLocationRowCopy);
                        FilteredPartnerLocationTable.Rows.Add(PartnerLocationRowCopy);
                    }
                }

                BestLocationPK = Calculations.DetermineBestAddress(FilteredPartnerLocationTable);
            }

            // create new row, initialize it and add it to the table
            if (BestLocationPK.LocationKey != -1)
            {
                PartnerLocationKeyRow = (PPartnerLocationRow)APartnerLocationKeysTable.NewRow();
                PartnerLocationKeyRow[PPartnerLocationTable.GetPartnerKeyDBName()] = APartnerKey;
                PartnerLocationKeyRow[PPartnerLocationTable.GetSiteKeyDBName()] = BestLocationPK.SiteKey;
                PartnerLocationKeyRow[PPartnerLocationTable.GetLocationKeyDBName()] = BestLocationPK.LocationKey;

                // only add row if it does not already exist
                if (!APartnerLocationKeysTable.Rows.Contains(
                        new object[] { PartnerLocationKeyRow.PartnerKey, PartnerLocationKeyRow.SiteKey, PartnerLocationKeyRow.LocationKey }))
                {
                    APartnerLocationKeysTable.Rows.Add(PartnerLocationKeyRow);
                }

                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// todoComment
        /// </summary>
        private static TSubmitChangesResult PerformSimilarLocationReUseChecks(ref PLocationRow ALocationRow,
            ref PartnerAddressAggregateTDS AResponseDS,
            TDBTransaction ASubmitChangesTransaction,
            Int64 APartnerKey,
            ref PartnerAddressAggregateTDSSimilarLocationParametersTable AExistingLocationParametersDT,
            ref PPartnerLocationTable APartnerLocationTable,
            ref TLocationPK[, ] ALocationReUseKeyMapping,
            out Boolean AReUseSimilarLocation,
            ref TVerificationResultCollection AVerificationResult)
        {
            TSubmitChangesResult ReturnValue;
            PPartnerLocationRow PartnerLocationCheckRow;
            DataView ExistingLocationParametersDV;

            PLocationRow ExistingLocationRow;
            Int64 CurrentSiteKey;
            Int64 ExistingSiteKey;
            Int32 CurrentLocationKey;
            Int32 ExistingLocationKey;

            PLocationTable SimilarLocationDT;

//          TLogging.LogAtLevel(9, "PerformSimilarLocationReUseChecks: AExistingLocationParametersDT.Rows.Count: " +
//              AExistingLocationParametersDT.Rows.Count.ToString());
            AReUseSimilarLocation = false;

            if (CheckReUseExistingLocation(ALocationRow, APartnerKey, ref AExistingLocationParametersDT, ASubmitChangesTransaction,
                    out ExistingSiteKey, out ExistingLocationKey))
            {
                // Check if there is a Parameter Row for the LocationKey we are looking at
                ExistingLocationParametersDV = new DataView(AExistingLocationParametersDT,
                    PartnerAddressAggregateTDSSimilarLocationParametersTable.GetSiteKeyDBName() + " = " + ALocationRow.SiteKey.ToString() + " AND " +
                    PartnerAddressAggregateTDSSimilarLocationParametersTable.GetLocationKeyDBName() + " = " + ALocationRow.LocationKey.ToString() +
                    " AND " + PartnerAddressAggregateTDSSimilarLocationParametersTable.GetAnswerProcessedClientSideDBName() + " = false",
                    "",
                    DataViewRowState.CurrentRows);

                if (ExistingLocationParametersDV.Count > 0)
                {
//                  TLogging.LogAtLevel(9, "PerformSimilarLocationReUseChecks: Location " + ALocationRow.LocationKey.ToString() + ": found similar Location, decision is needed.");

                    /*
                     * More information is needed (usually via user interaction)
                     * -> stop processing here and return parameters
                     * (usually used for UI interaction)
                     */
                    if (AResponseDS == null)
                    {
//                      TLogging.LogAtLevel(9, "PerformSimilarLocationReUseChecks: Creating AResponseDS.");
                        AResponseDS = new PartnerAddressAggregateTDS(MPartnerConstants.PARTNERADDRESSAGGREGATERESPONSE_DATASET);
                    }

//                  TLogging.LogAtLevel(9, "PerformSimilarLocationReUseChecks: AExistingLocationParametersDT.Rows.Count: " + AExistingLocationParametersDT.Rows.Count.ToString());
                    AResponseDS.Merge(AExistingLocationParametersDT);
//                  TLogging.LogAtLevel(9, "PerformSimilarLocationReUseChecks: Merged ExistingLocationParametersDT into AResponseDS.");
//                  TLogging.LogAtLevel(9, "PerformSimilarLocationReUseChecks: AResponseDS.Tables[" + MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME +
//                            "].Rows.Count: " + AResponseDS.Tables[MPartnerConstants.EXISTINGLOCATIONPARAMETERS_TABLENAME].Rows.Count.ToString());
                    return TSubmitChangesResult.scrInfoNeeded;
                }
                else
                {
//                  TLogging.LogAtLevel(9, "PerformSimilarLocationReUseChecks: Location " + ALocationRow.LocationKey.ToString() +
//                      ": found similar Location and this one (" + ExistingLocationKey.ToString() + ") should be used instead of creating a new one!");

                    /*
                     * Location with the same data already exists and it should be
                     * re-used instead of creating a new one!
                     */

                    // Keep a mapping of the initially submitted LocationKey to the newly assigned one
                    ALocationReUseKeyMapping = new TLocationPK[ALocationReUseKeyMapping.GetLength(0) + 1, 2];
                    ALocationReUseKeyMapping[(ALocationReUseKeyMapping.GetLength(0)) - 1, 0] = new TLocationPK(ALocationRow.SiteKey,
                        (int)ALocationRow.LocationKey);
                    ALocationReUseKeyMapping[(ALocationReUseKeyMapping.GetLength(0)) - 1, 1] = new TLocationPK(ExistingSiteKey, ExistingLocationKey);
                    AReUseSimilarLocation = true;

                    if (!AExistingLocationParametersDT[0].AnswerProcessedServerSide)
                    {
                        AExistingLocationParametersDT[0].AnswerProcessedServerSide = true;

                        // Preserve Key of current Location
                        CurrentSiteKey = ALocationRow.SiteKey;
                        CurrentLocationKey = (int)ALocationRow.LocationKey;

                        /*
                         * Make sure that the Partner hasn't already got a PartnerLocation with
                         * the same Key (neither in memory nor in the DB)
                         */
//                      TLogging.LogAtLevel(9, "PerformSimilarLocationReUseChecks: Finding PartnerLocation Row in APartnerLocationTable with LocationKey " + ALocationRow.LocationKey.ToString());
                        PartnerLocationCheckRow =
                            (PPartnerLocationRow)APartnerLocationTable.Rows.Find(new object[] { APartnerKey, ALocationRow.SiteKey,
                                                                                                ALocationRow.LocationKey });

                        if (PartnerLocationCheckRow != null)
                        {
                            /*
                             * Checks in Memory: look for Current (ie. unchanged, new or edited)
                             * rows first whether they are the Location that is about to being
                             * reused. Secondly, check if there is a deleted Location with the same
                             * LocationKey that is about to being reused; only if this is not the
                             * case:
                             * Check in the DB whether the Partner hasn't got the Location
                             * with the same LocationKey that is about to being reuse.
                             */
                            if ((APartnerLocationTable.Select(PPartnerLocationTable.GetPartnerKeyDBName() + " = " + APartnerKey.ToString() +
                                     " AND " + PPartnerLocationTable.GetSiteKeyDBName() + " = " + ExistingSiteKey.ToString() + " AND " +
                                     PPartnerLocationTable.GetLocationKeyDBName() + " = " + ExistingLocationKey.ToString(), "",
                                     DataViewRowState.CurrentRows).Length != 0)
                                || ((APartnerLocationTable.Select(PPartnerLocationTable.GetPartnerKeyDBName() + " = " + APartnerKey.ToString() +
                                         " AND " +
                                         PPartnerLocationTable.GetSiteKeyDBName() + " = " + ExistingSiteKey.ToString() + " AND " +
                                         PPartnerLocationTable.GetLocationKeyDBName() + " = " + ExistingLocationKey.ToString(), "",
                                         DataViewRowState.Deleted).Length == 0)
                                    && (PPartnerLocationAccess.Exists(APartnerKey, ExistingSiteKey, ExistingLocationKey,
                                            ASubmitChangesTransaction))))
                            {
                                AVerificationResult.Add(new TVerificationResult("[Partner Address Save]", "Partner " + APartnerKey.ToString() +
                                        " already has a " + "record linked with Location " + ExistingLocationKey.ToString() + Environment.NewLine +
                                        "Unable to save.", "Duplicate Address Entered", "", TResultSeverity.Resv_Critical));
                                ReturnValue = TSubmitChangesResult.scrError;
                                return ReturnValue;
                            }
                            else
                            {
//                              TLogging.LogAtLevel(9, "PerformSimilarLocationReUseChecks: LocationKey: " + ExistingLocationKey.ToString() + " will later get assigned to PPartnerLocation.");
                            }
                        }
                        else
                        {
                            throw new EOPAppException(
                                "PerformSimilarLocationReUseChecks: PartnerLocationCheckRow with SiteKey " + ALocationRow.SiteKey.ToString() +
                                " and LocationKey " +
                                ALocationRow.LocationKey.ToString() + " not found!");
                        }

                        /*
                         * Copy all fields from the existing Location to the current Location
                         */
                        SimilarLocationDT = PLocationAccess.LoadByPrimaryKey(ExistingSiteKey, ExistingLocationKey, null, ASubmitChangesTransaction);

                        if (SimilarLocationDT.Rows.Count != 0)
                        {
                            ExistingLocationRow = (PLocationRow)SimilarLocationDT.Rows[0];

                            ALocationRow.ItemArray = ExistingLocationRow.ItemArray;

                            /*
                             * NOTE: The SiteKey and LocationKey are re-assigned to the ones of the
                             * current Location. This is done to have the current SiteKey and
                             * LocationKey preserved throughout the whole process of working with the
                             * Locations. The SiteKey and LocationKey are exchanged with the ones of
                             * the existing Location before the DataRow gets sent back to the Client!
                             */
                            ALocationRow.SiteKey = CurrentSiteKey;
                            ALocationRow.LocationKey = CurrentLocationKey;
//                          TLogging.LogAtLevel(9, "CheckReUseExistingLocation: Location " + ALocationRow.LocationKey.ToString() +
//                                ": data got replaced with data from the existing Location (" + ExistingLocationKey.ToString() + ")!");
                        }
                        else
                        {
                            throw new EOPAppException(
                                "Couldn''t find existing Similar Location with SiteKey " + ALocationRow.SiteKey.ToString() + " and LocationKey " +
                                ALocationRow.LocationKey.ToString() + '!');
                        }
                    }
                }
            }
            else
            {
//              TLogging.LogAtLevel(9, "CheckReUseExistingLocation: Location " + ALocationRow.LocationKey.ToString() +
//                  ": Location does not exist yet (or an existing Location should not be re-used) -> will get saved later.");

                /*
                 * No similar Location exists, or an existing similar Location should
                 * not be re-used: Save this Location
                 * -> will get saved later in call to SubmitChanges
                 */
            }

            return TSubmitChangesResult.scrOK;
        }
Example #43
0
        /// <summary>
        /// count the available current addresses and the total number of addresses
        /// </summary>
        /// <param name="ATable">table with locations</param>
        /// <param name="ATotalAddresses">returns the total number of address</param>
        /// <param name="ACurrentAddresses">returns the number of current addresses</param>
        public static void CalculateTabCountsAddresses(PPartnerLocationTable ATable, out Int32 ATotalAddresses, out Int32 ACurrentAddresses)
        {
            DataView TmpDV;

            // Inspect only CurrentRows (this excludes Deleted DataRows)
            TmpDV = new DataView(ATable, "", "", DataViewRowState.CurrentRows);
            ATotalAddresses = TmpDV.Count;

            if ((ATotalAddresses == 1) && (((PPartnerLocationRow)TmpDV[0].Row).LocationKey == 0))
            {
                // In case the only Address is linked to Location 0: we don't have a
                // Current Address, because this signalises that there is no valid address.
                // MessageBox.Show('The last Address is the ''No Address on file'' Address!');
                ACurrentAddresses = 0;
            }
            else
            {
                // MessageBox.Show('Query: ' + '((' + PPartnerLocationTable.GetDateEffectiveDBName + ' <= #'
                // + DateTime.Now.Date.ToString('MM/dd/yyyy') + '# OR ' +
                // PPartnerLocationTable.GetDateEffectiveDBName + ' IS NULL) AND (' +
                // PPartnerLocationTable.GetDateGoodUntilDBName + ' >= #'
                // + DateTime.Now.Date.ToString('MM/dd/yyyy') + '# OR ' +
                // PPartnerLocationTable.GetDateGoodUntilDBName + ' IS NULL))');
                ACurrentAddresses = DetermineCurrentAddresses(ATable).Count;
            }

            // MessageBox.Show('ACurrentAddresses: ' + ACurrentAddresses.ToString);
        }
        /// <summary>
        /// Applies Address Security by inspecting all PartnerLocation records'
        /// LocationTypes and the user's presence in security Group ADDRESSCAN.
        /// </summary>
        /// <remarks>
        /// <para>
        /// If the user isn't in the mentioned Security Group, the LocationTypes of all
        /// records are checked.
        /// For any records where the security check fails, the information in all
        /// DataColumns of the Location and PartnerLocation Tables that are present in
        /// the submitted DataTables gets stripped out, Strings get replaced with
        /// StrRestrictedInformation. For the LocationTable this is only true if it is
        /// submitted and holds records that are restricted in the PPartnerLocation
        /// DataTable.
        /// </para>
        /// <para>
        /// <em>Another overload <see cref="M:ApplySecurity(ref DataTable)" /> is available which
        /// can be used if the Partner Location (and/or Location) data isn't available
        /// in the form of the Typed DataTables PPartnerLocation and PLocation!</em>
        /// </para>
        /// </remarks>
        /// <param name="ALocationDT">Location Typed DataTable (optional, set to nil if there is
        /// no Location Typed DataTable that goes with the PartnerLocation Typed
        /// DataTable that is submitted in APartnerLocationDT.</param>
        /// <param name="APartnerLocationDT">PartnerLocation Typed DataTable
        /// </param>
        /// <returns>void</returns>
        public static void ApplySecurity(ref PPartnerLocationTable APartnerLocationDT, ref PLocationTable ALocationDT)
        {
            PPartnerLocationRow PartnerLocationDR;
            PLocationRow LocationDR;

            if (!UserInfo.GUserInfo.IsInGroup(SharedConstants.PETRAGROUP_ADDRESSCAN))
            {
//              TLogging.LogAtLevel(9, "ApplySecurity (2): User isn't in Security Group ADDRESSCAN.");

                foreach (DataRow TmpDR in APartnerLocationDT.Rows)
                {
                    PartnerLocationDR = (PPartnerLocationRow)TmpDR;

                    if (PartnerLocationDR.LocationType.EndsWith(SharedConstants.SECURITY_CAN_LOCATIONTYPE))
                    {
                        /*
                         * Need to strip out information from PPartnerLocation DataTable;
                         * replace Strings with StrRestrictedInformation.
                         */
                        PartnerLocationDR.SetDateEffectiveNull();
                        PartnerLocationDR.SetDateGoodUntilNull();
                        PartnerLocationDR.SendMail = false;
                        PartnerLocationDR.Telex = 0;
                        PartnerLocationDR.LocationDetailComment = Catalog.GetString("** restricted **");
                        PartnerLocationDR.SetDateCreatedNull();
                        PartnerLocationDR.CreatedBy = Catalog.GetString("** restricted **");
                        PartnerLocationDR.SetDateModifiedNull();
                        PartnerLocationDR.ModifiedBy = Catalog.GetString("** restricted **");
                        PartnerLocationDR.AcceptChanges();

                        // Check if we can find this PartnerLocation Row also in the Location DataTable
                        if (ALocationDT != null)
                        {
                            LocationDR = (PLocationRow)ALocationDT.Rows.Find(new System.Object[] { PartnerLocationDR.SiteKey,
                                                                                                   PartnerLocationDR.LocationKey });

                            if (LocationDR != null)
                            {
                                /*
                                 * Location DataTable holds the Row that matches the PartnerLocation Row
                                 * --> Need to strip out information from PLocation DataTable;
                                 * replace Strings with StrRestrictedInformation.
                                 */
                                LocationDR.StreetName = Catalog.GetString("** restricted **");
                                LocationDR.Locality = Catalog.GetString("** restricted **");
                                LocationDR.City = Catalog.GetString("** restricted **");
                                LocationDR.County = Catalog.GetString("** restricted **");
                                LocationDR.PostalCode = Catalog.GetString("** restricted **");

                                // TRANSLATORS: this is used for the restricted country codes
                                LocationDR.CountryCode = Catalog.GetString("99");
                                LocationDR.Address3 = Catalog.GetString("** restricted **");
                                LocationDR.SetDateCreatedNull();
                                LocationDR.CreatedBy = Catalog.GetString("** restricted **");
                                LocationDR.SetDateModifiedNull();
                                LocationDR.ModifiedBy = Catalog.GetString("** restricted **");
                                LocationDR.AcceptChanges();
                            }
                        }
                    }
                }
            }
        }
Example #45
0
        /// <summary>
        /// extend an extract from a list of best addresses
        /// </summary>
        /// <param name="AExtractId">Extract Id of the Extract to extend</param>
        /// <param name="APartnerKeysTable"></param>
        /// <param name="APartnerKeyColumn">number of the column that contains the partner keys</param>
        /// <param name="ASiteKeyColumn">number of the column that contains the site keys</param>
        /// <param name="ALocationKeyColumn">number of the column that contains the location keys</param>
        /// <param name="AIgnoreDuplicates">true if duplicates should be looked out for. Can be set to false if called only once and not several times per extract.</param>
        /// <param name="ACommitTransaction">true if transaction should be committed at end of method</param>
        public static void ExtendExtractFromListOfPartnerKeys(
            Int32 AExtractId,
            DataTable APartnerKeysTable,
            Int32 APartnerKeyColumn,
            Int32 ASiteKeyColumn,
            Int32 ALocationKeyColumn,
            bool AIgnoreDuplicates,
            bool ACommitTransaction)
        {
            Boolean NewTransaction;
            int     RecordCounter = 0;
            PPartnerLocationTable PartnerLocationKeysTable;
            Int64 PartnerKey;

            TDBTransaction WriteTransaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.Serializable,
                                                                                                TEnforceIsolationLevel.eilMinimum, out NewTransaction);

            try
            {
                MExtractTable ExtractTable = new MExtractTable();
                ExtractTable = MExtractAccess.LoadViaMExtractMaster(AExtractId, WriteTransaction);

                // Location Keys need to be determined as extracts do not only need partner keys but
                // also Location Keys.
                DetermineBestLocationKeys(APartnerKeysTable, APartnerKeyColumn, ASiteKeyColumn,
                                          ALocationKeyColumn, out PartnerLocationKeysTable,
                                          WriteTransaction);

                // use the returned table which contains partner and location keys to build the extract
                foreach (PPartnerLocationRow PartnerLocationRow in PartnerLocationKeysTable.Rows)
                {
                    PartnerKey = Convert.ToInt64(PartnerLocationRow[PPartnerLocationTable.GetPartnerKeyDBName()]);

                    if (PartnerKey > 0)
                    {
                        RecordCounter += 1;
                        TLogging.Log("Preparing Partner " + PartnerKey.ToString() + " (Record Number " + RecordCounter.ToString() + ")");

                        // add row for partner to extract and fill with contents
                        MExtractRow NewRow = ExtractTable.NewRowTyped();
                        NewRow.ExtractId   = AExtractId;
                        NewRow.PartnerKey  = PartnerKey;
                        NewRow.SiteKey     = Convert.ToInt64(PartnerLocationRow[PPartnerLocationTable.GetSiteKeyDBName()]);
                        NewRow.LocationKey = Convert.ToInt32(PartnerLocationRow[PPartnerLocationTable.GetLocationKeyDBName()]);

                        // only add row if it does not already exist for this partner
                        if (AIgnoreDuplicates || !ExtractTable.Rows.Contains(new object[] { NewRow.ExtractId, NewRow.PartnerKey, NewRow.SiteKey }))
                        {
                            ExtractTable.Rows.Add(NewRow);
                        }
                    }
                }

                if (ExtractTable.Rows.Count > 0)
                {
                    // update field in extract master for quick access to number of partners in extract
                    MExtractMasterTable ExtractMaster = MExtractMasterAccess.LoadByPrimaryKey(AExtractId, WriteTransaction);
                    ExtractMaster[0].KeyCount = ExtractTable.Rows.Count;

                    ExtractTable.ThrowAwayAfterSubmitChanges = true; // no need to keep data as this increases speed significantly

                    MExtractAccess.SubmitChanges(ExtractTable, WriteTransaction);

                    MExtractMasterAccess.SubmitChanges(ExtractMaster, WriteTransaction);
                }

                if (ACommitTransaction)
                {
                    DBAccess.GDBAccessObj.CommitTransaction();
                }
            }
            catch (Exception Exc)
            {
                TLogging.Log("An Exception occured while extending an Extract from a list of Partner Keys:" + Environment.NewLine + Exc.ToString());

                if (NewTransaction)
                {
                    DBAccess.GDBAccessObj.RollbackTransaction();
                }

                throw;
            }
        }
Example #46
0
        /// <summary>
        /// Determine location keys for partners needed for extract, depending on if location information
        /// was retrieved by query or not.
        /// </summary>
        /// <param name="APartnerKeysTable"></param>
        /// <param name="APartnerKeyColumn"></param>
        /// <param name="ASiteKeyColumn"></param>
        /// <param name="ALocationKeyColumn"></param>
        /// <param name="APartnerLocationKeysTable"></param>
        /// <param name="ATransaction"></param>
        private static void DetermineBestLocationKeys(
            DataTable APartnerKeysTable,
            Int32 APartnerKeyColumn,
            Int32 ASiteKeyColumn,
            Int32 ALocationKeyColumn,
            out PPartnerLocationTable APartnerLocationKeysTable,
            TDBTransaction ATransaction)
        {
            Int64 PartnerKey;
            Int64 PreviousPartnerKey;
            Int32 NumberOfPartnerRows = APartnerKeysTable.Rows.Count;
            DataRow partnerRow;

            List <TLocationPK>LocationKeyList = new List <TLocationPK>();

            APartnerLocationKeysTable = new PPartnerLocationTable();

            // don't go further if table is empty
            if (APartnerKeysTable.Rows.Count == 0)
            {
                return;
            }

            // If location column exists then check if there is more than one location key for a partner.
            // If so then determine the best of the found addresses.
            if (ALocationKeyColumn >= 0)
            {
                PreviousPartnerKey = -1;

                // Rows are sorted by partner key. Create a list of location keys per partner and determine
                // the best of those addresses.
                for (int ii = NumberOfPartnerRows - 1; ii >= 0; ii--)
                {
                    partnerRow = APartnerKeysTable.Rows[ii];

                    PartnerKey = Convert.ToInt64(partnerRow[APartnerKeyColumn]);

                    if ((PartnerKey != PreviousPartnerKey)
                        && (PreviousPartnerKey != -1))
                    {
                        if (!DetermineAndAddBestLocationKey(PreviousPartnerKey, LocationKeyList,
                                ref APartnerLocationKeysTable, ATransaction))
                        {
                            // if no address could be found then remove this partner
                            APartnerKeysTable.Rows[ii + 1].Delete();
                        }
                        else
                        {
                            // add first location for next partner
                            LocationKeyList.Clear();
                            LocationKeyList.Add(new TLocationPK(Convert.ToInt64(partnerRow[ASiteKeyColumn]),
                                    Convert.ToInt32(partnerRow[ALocationKeyColumn])));
                        }
                    }
                    else
                    {
                        // add location for this partner
                        LocationKeyList.Add(new TLocationPK(Convert.ToInt64(partnerRow[ASiteKeyColumn]),
                                Convert.ToInt32(partnerRow[ALocationKeyColumn])));
                    }

                    // prepare for next round of loop
                    PreviousPartnerKey = PartnerKey;
                }

                // process last partner key after loop through all records
                if (!DetermineAndAddBestLocationKey(PreviousPartnerKey, LocationKeyList,
                        ref APartnerLocationKeysTable, ATransaction))
                {
                    // if no address could be found then remove this partner
                    APartnerKeysTable.Rows[0].Delete();
                }
            }
            else
            {
                // If no location information was retrieved with earlier query then find best address
                // for partner.
                for (int ii = NumberOfPartnerRows - 1; ii >= 0; ii--)
                {
                    partnerRow = APartnerKeysTable.Rows[ii];
                    PartnerKey = Convert.ToInt64(partnerRow[APartnerKeyColumn]);

                    if (!DetermineAndAddBestLocationKey(PartnerKey, LocationKeyList,
                            ref APartnerLocationKeysTable, ATransaction))
                    {
                        // if no address could be found then remove this partner
                        partnerRow.Delete();
                    }
                }
            }
        }
Example #47
0
        /// find the current best address for the partner
        public static bool GetBestAddress(Int64 APartnerKey,
            out PLocationTable AAddress,
            out PPartnerLocationTable APartnerLocation,
            out string ACountryNameLocal,
            out string AEmailAddress,
            TDBTransaction ATransaction)
        {
            AEmailAddress = "";
            AAddress = null;
            APartnerLocation = null;
            ACountryNameLocal = "";

            DataSet PartnerLocationsDS = new DataSet();

            PartnerLocationsDS.Tables.Add(new PPartnerLocationTable());
            PartnerLocationsDS.Tables.Add(new PCountryTable());
            DataTable PartnerLocationTable = PartnerLocationsDS.Tables[PPartnerLocationTable.GetTableName()];
            PCountryTable CountryTable = (PCountryTable)PartnerLocationsDS.Tables[PCountryTable.GetTableName()];
            CountryTable.DefaultView.Sort = PCountryTable.GetCountryCodeDBName();

            // add special column BestAddress and Icon
            PartnerLocationTable.Columns.Add(new System.Data.DataColumn("BestAddress", typeof(Boolean)));
            PartnerLocationTable.Columns.Add(new System.Data.DataColumn("Icon", typeof(Int32)));

            // find all locations of the partner, put it into a dataset
            PPartnerLocationAccess.LoadViaPPartner(PartnerLocationsDS, APartnerKey, ATransaction);

            Ict.Petra.Shared.MPartner.Calculations.DeterminePartnerLocationsDateStatus(PartnerLocationsDS);
            Ict.Petra.Shared.MPartner.Calculations.DetermineBestAddress(PartnerLocationsDS);

            foreach (PPartnerLocationRow row in PartnerLocationTable.Rows)
            {
                // find the row with BestAddress = 1
                if (Convert.ToInt32(row["BestAddress"]) == 1)
                {
                    if (!row.IsEmailAddressNull())
                    {
                        AEmailAddress = row.EmailAddress;
                    }

                    // we also want the post address, need to load the p_location table:
                    AAddress = PLocationAccess.LoadByPrimaryKey(row.SiteKey, row.LocationKey, ATransaction);

                    APartnerLocation = new PPartnerLocationTable();
                    APartnerLocation.ImportRow(row);

                    // watch out for empty country codes
                    if (AAddress[0].CountryCode.Trim().Length > 0)
                    {
                        if (CountryTable.DefaultView.Find(AAddress[0].CountryCode) == -1)
                        {
                            CountryTable.Merge(PCountryAccess.LoadByPrimaryKey(AAddress[0].CountryCode, ATransaction));
                        }

                        ACountryNameLocal = CountryTable[CountryTable.DefaultView.Find(AAddress[0].CountryCode)].CountryNameLocal;
                    }

                    break;
                }
            }

            return AAddress != null;
        }
        public static DataTable GetEventUnits()
        {
            List <OdbcParameter> SqlParameterList = new List <OdbcParameter>();

            DataColumn[] Key    = new DataColumn[3];
            DataTable    Events = new DataTable();

            if (TLogging.DL >= 9)
            {
                Console.WriteLine("GetEventUnits called!");
            }

            TDBTransaction Transaction = null;

            DBAccess.GDBAccessObj.BeginAutoReadTransaction(IsolationLevel.ReadCommitted,
                                                           ref Transaction,
                                                           delegate
            {
                string SqlStmt =
                    "SELECT DISTINCT " +
                    PPartnerTable.GetPartnerShortNameDBName() +
                    ", " + PPartnerTable.GetPartnerClassDBName() +
                    ", " + PUnitTable.GetOutreachCodeDBName() +
                    ", " + PCountryTable.GetTableDBName() + "." + PCountryTable.GetCountryNameDBName() +
                    ", " + PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetSiteKeyDBName() +
                    ", " + PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetLocationKeyDBName() +
                    ", " + PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetDateEffectiveDBName() +
                    ", " + PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetDateGoodUntilDBName() +

                    ", " + PPartnerTable.GetTableDBName() + "." + PPartnerTable.GetPartnerKeyDBName() +
                    ", " + PUnitTable.GetUnitTypeCodeDBName() +
                    ", " + PUnitTable.GetUnitNameDBName() +

                    " FROM pub_" + PPartnerTable.GetTableDBName() +
                    ", pub_" + PUnitTable.GetTableDBName() +
                    ", pub_" + PLocationTable.GetTableDBName() +
                    ", pub_" + PPartnerLocationTable.GetTableDBName() +
                    ", pub_" + PCountryTable.GetTableDBName() +

                    " WHERE " +
                    PPartnerTable.GetTableDBName() + "." + PPartnerTable.GetPartnerKeyDBName() + " = " +
                    PUnitTable.GetTableDBName() + "." + PUnitTable.GetPartnerKeyDBName() + " AND " +

                    PPartnerTable.GetTableDBName() + "." + PPartnerTable.GetPartnerKeyDBName() + " = " +
                    PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetPartnerKeyDBName() + " AND " +
                    PLocationTable.GetTableDBName() + "." + PLocationTable.GetSiteKeyDBName() + " = " +
                    PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetSiteKeyDBName() + " AND " +
                    PLocationTable.GetTableDBName() + "." + PLocationTable.GetLocationKeyDBName() + " = " +
                    PPartnerLocationTable.GetTableDBName() + "." + PPartnerLocationTable.GetLocationKeyDBName() + " AND " +
                    PCountryTable.GetTableDBName() + "." + PCountryTable.GetCountryCodeDBName() + " = " +
                    PLocationTable.GetTableDBName() + "." + PLocationTable.GetCountryCodeDBName() + " AND " +

                    PPartnerTable.GetStatusCodeDBName() + " = 'ACTIVE' " + " AND " +
                    PPartnerTable.GetPartnerClassDBName() + " = 'UNIT' ";

                // sort rows according to name
                SqlStmt = SqlStmt + " ORDER BY " + PPartnerTable.GetPartnerShortNameDBName();

                Events = DBAccess.GDBAccessObj.SelectDT(SqlStmt, "events",
                                                        Transaction, SqlParameterList.ToArray());

                Key[0]            = Events.Columns[PPartnerTable.GetPartnerKeyDBName()];
                Key[1]            = Events.Columns[PPartnerLocationTable.GetSiteKeyDBName()];
                Key[2]            = Events.Columns[PPartnerLocationTable.GetLocationKeyDBName()];
                Events.PrimaryKey = Key;
            });

            return(Events);
        }
Example #49
0
 /// <summary>
 /// </summary>
 /// <param name="ALocationDT"></param>
 /// <param name="APartnerLocationDT"></param>
 /// <param name="APartnerKey"></param>
 /// <param name="APartnerClass"></param>
 /// <param name="ACountryCode"></param>
 /// <param name="ANewLocationKey"></param>
 /// <param name="ACopyFromPartnerLocationKey"></param>
 /// <param name="ACopyFromPartnerSiteKey"></param>
 /// <param name="ACopyFromFamilyPartnerKey"></param>
 /// <param name="ACopyFromFamilyLocationKey"></param>
 /// <param name="ACopyFromFamilySiteKey"></param>
 public static void CreateNewAddress(PLocationTable ALocationDT,
     PPartnerLocationTable APartnerLocationDT,
     Int64 APartnerKey,
     TPartnerClass APartnerClass,
     String ACountryCode,
     Int32 ANewLocationKey,
     Int32 ACopyFromPartnerLocationKey,
     Int64 ACopyFromPartnerSiteKey,
     Int64 ACopyFromFamilyPartnerKey,
     Int32 ACopyFromFamilyLocationKey,
     Int64 ACopyFromFamilySiteKey)
 {
     CreateNewAddress(ALocationDT,
         APartnerLocationDT,
         APartnerKey,
         APartnerClass,
         ACountryCode,
         ANewLocationKey,
         ACopyFromPartnerLocationKey,
         ACopyFromPartnerSiteKey,
         ACopyFromFamilyPartnerKey,
         ACopyFromFamilyLocationKey,
         ACopyFromFamilySiteKey,
         false,
         true);
 }