/// <summary> /// this returns the default next available (highest) partner key of the given field /// </summary> /// <param name="AFieldPartnerKey">if this is -1, then the sitekey defined in System Parameters is used</param> /// <returns>void</returns> public static System.Int64 GetNewPartnerKey(System.Int64 AFieldPartnerKey) { PPartnerLedgerTable PartnerLedgerTable = null; Int64 ReturnValue = -1; if (AFieldPartnerKey == -1) { AFieldPartnerKey = DomainManager.GSiteKey; } TDBTransaction ReadTransaction = null; DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.RepeatableRead, TEnforceIsolationLevel.eilMinimum, ref ReadTransaction, delegate { PartnerLedgerTable = PPartnerLedgerAccess.LoadByPrimaryKey(AFieldPartnerKey, ReadTransaction); ReturnValue = PartnerLedgerTable[0].PartnerKey + PartnerLedgerTable[0].LastPartnerId + 1; // Now check that this does not exist, and increment until we // find one which does not while (PPartnerAccess.Exists(ReturnValue, ReadTransaction)) { ReturnValue = ReturnValue + 1; } }); return(ReturnValue); }
/// <summary> /// this returns the default next available (highest) partner key of the given field /// </summary> /// <param name="AFieldPartnerKey">if this is -1, then the sitekey defined in System Parameters is used</param> /// <param name="ADataBase"></param> /// <returns>void</returns> public static System.Int64 GetNewPartnerKey(System.Int64 AFieldPartnerKey, TDataBase ADataBase = null) { PPartnerLedgerTable PartnerLedgerTable = null; Int64 ReturnValue = -1; if (AFieldPartnerKey == -1) { AFieldPartnerKey = DomainManager.GSiteKey; } TDBTransaction ReadTransaction = new TDBTransaction(); TDataBase db = DBAccess.Connect("GetNewPartnerKey", ADataBase); db.ReadTransaction(ref ReadTransaction, delegate { PartnerLedgerTable = PPartnerLedgerAccess.LoadByPrimaryKey(AFieldPartnerKey, ReadTransaction); ReturnValue = PartnerLedgerTable[0].PartnerKey + PartnerLedgerTable[0].LastPartnerId + 1; // Now check that this does not exist, and increment until we // find one which does not while (PPartnerAccess.Exists(ReturnValue, ReadTransaction)) { ReturnValue = ReturnValue + 1; } }); if (ADataBase == null) { db.CloseDBConnection(); } return(ReturnValue); }
/// <summary> /// this checks if the new key is still available, /// and makes sure it will not be used as a default key anymore /// </summary> /// <param name="AFieldPartnerKey"></param> /// <param name="AOriginalDefaultKey">this has been previously retrieved from GetNewPartnerKey</param> /// <param name="ANewPartnerKey">the user proposes this key for a new partner; the function can change it and return a valid value, or -1</param> /// <returns>whether or not ANewPartnerKey has a valid new partner key; /// if it cannot be assigned, the function returns false, and ANewPartnerKey is -1 /// </returns> public static bool SubmitNewPartnerKey(System.Int64 AFieldPartnerKey, System.Int64 AOriginalDefaultKey, ref System.Int64 ANewPartnerKey) { bool ReturnValue = true; TDBTransaction ReadTransaction1; TDBTransaction ReadTransaction2; TDBTransaction WriteTransaction; Boolean NewTransaction1; Boolean NewTransaction2; Boolean NewTransaction3; PPartnerLedgerTable PartnerLedgerDT; System.Int64 CurrentDefaultPartnerKey; if (ANewPartnerKey == AOriginalDefaultKey) { // The user has selected the default ReadTransaction1 = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.RepeatableRead, TEnforceIsolationLevel.eilMinimum, out NewTransaction1); try { // Fetch the partner ledger record to update the last key PartnerLedgerDT = PPartnerLedgerAccess.LoadByPrimaryKey(AFieldPartnerKey, ReadTransaction1); CurrentDefaultPartnerKey = PartnerLedgerDT[0].PartnerKey + PartnerLedgerDT[0].LastPartnerId + 1; if (ANewPartnerKey != CurrentDefaultPartnerKey) { // Someone else has updated this since, so we will use the new default ANewPartnerKey = CurrentDefaultPartnerKey; } // Now check that this does not exist, and increment until we // find one which does not while (PPartnerAccess.Exists(ANewPartnerKey, ReadTransaction1)) { ANewPartnerKey = ANewPartnerKey + 1; } } finally { if (NewTransaction1) { DBAccess.GDBAccessObj.CommitTransaction(); if (TLogging.DebugLevel >= TLogging.DEBUGLEVEL_TRACE) { Console.WriteLine("TNewPartnerKey.SubmitNewPartnerKey: committed own transaction."); } } } PartnerLedgerDT[0].LastPartnerId = (int)(ANewPartnerKey - PartnerLedgerDT[0].PartnerKey); WriteTransaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.Serializable, TEnforceIsolationLevel.eilMinimum, out NewTransaction2); try { PPartnerLedgerAccess.SubmitChanges(PartnerLedgerDT, WriteTransaction); if (NewTransaction2) { DBAccess.GDBAccessObj.CommitTransaction(); } } catch (Exception Exc) { TLogging.Log("An Exception occured during the submission of a new PartnerKey:" + Environment.NewLine + Exc.ToString()); if (NewTransaction2) { DBAccess.GDBAccessObj.RollbackTransaction(); } throw; } } // end of: The user has selected the default else { ReadTransaction2 = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.RepeatableRead, TEnforceIsolationLevel.eilMinimum, out NewTransaction3); try { // check if the Partner Key is already being used if (PPartnerAccess.Exists(ANewPartnerKey, ReadTransaction2)) { ANewPartnerKey = -1; ReturnValue = false; } } finally { if (NewTransaction3) { DBAccess.GDBAccessObj.CommitTransaction(); if (TLogging.DebugLevel >= TLogging.DEBUGLEVEL_TRACE) { Console.WriteLine("TNewPartnerKey.SubmitNewPartnerKey: committed own transaction."); } } } } return(ReturnValue); }
private static void ParsePartners(ref PartnerImportExportTDS AMainDS, XmlNode ACurNode, TDBTransaction ATransaction, ref TVerificationResultCollection AVerificationResult) { XmlNode LocalNode = ACurNode; while (LocalNode != null) { if (LocalNode.Name.StartsWith("PartnerGroup")) { ParsePartners(ref AMainDS, LocalNode.FirstChild, ATransaction, ref AVerificationResult); } else if (LocalNode.Name.StartsWith("Partner")) { PPartnerRow PartnerRow = AMainDS.PPartner.NewRowTyped(); Boolean IsExistingPartner = false; if (!TYml2Xml.HasAttributeRecursive(LocalNode, "SiteKey")) { throw new Exception(Catalog.GetString("Missing SiteKey Attribute")); } if (!TYml2Xml.HasAttributeRecursive(LocalNode, "status")) { throw new Exception(Catalog.GetString("Missing status Attribute")); } // get a new partner key if (TYml2Xml.HasAttribute(LocalNode, "PartnerKey")) { PartnerRow.PartnerKey = Convert.ToInt64(TYml2Xml.GetAttribute(LocalNode, "PartnerKey")); if (PPartnerAccess.Exists(PartnerRow.PartnerKey, ATransaction)) { AMainDS.Merge(PPartnerAccess.LoadByPrimaryKey(PartnerRow.PartnerKey, ATransaction)); AMainDS.PPartner.DefaultView.RowFilter = String.Format("{0} = '{1}'", PPartnerTable.GetPartnerKeyDBName(), PartnerRow.PartnerKey); PartnerRow = (PPartnerRow)AMainDS.PPartner.DefaultView[0].Row; IsExistingPartner = true; } else { AMainDS.PPartner.Rows.Add(PartnerRow); } } else { PartnerRow.PartnerKey = TImportExportYml.NewPartnerKey; TImportExportYml.NewPartnerKey--; } String PartnerClass = TYml2Xml.GetAttributeRecursive(LocalNode, "class"); TLogging.LogAtLevel(TLogging.DEBUGLEVEL_TRACE, PartnerClass + " " + LocalNode.Name + " " + "PartnerKey=" + PartnerRow.PartnerKey ); if (IsExistingPartner && (PartnerClass != PartnerRow.PartnerClass)) { throw new Exception(String.Format("Error: Yml contains Existing Partner {0} with a different partner class {1}!", PartnerRow.PartnerKey, PartnerClass)); } PartnerRow.PartnerClass = PartnerClass; if (PartnerClass == MPartnerConstants.PARTNERCLASS_FAMILY) { PFamilyRow FamilyRow; if (IsExistingPartner) { AMainDS.Merge(PFamilyAccess.LoadByPrimaryKey(PartnerRow.PartnerKey, ATransaction)); AMainDS.PFamily.DefaultView.RowFilter = String.Format("{0} = '{1}'", PFamilyTable.GetPartnerKeyDBName(), PartnerRow.PartnerKey); FamilyRow = (PFamilyRow)AMainDS.PFamily.DefaultView[0].Row; } else { FamilyRow = AMainDS.PFamily.NewRowTyped(); FamilyRow.PartnerKey = PartnerRow.PartnerKey; PartnerRow.PartnerClass = MPartnerConstants.PARTNERCLASS_FAMILY; AMainDS.PFamily.Rows.Add(FamilyRow); } FamilyRow.FamilyName = TYml2Xml.GetAttributeRecursive(LocalNode, "LastName"); FamilyRow.FirstName = TYml2Xml.GetAttribute(LocalNode, "FirstName"); FamilyRow.Title = TYml2Xml.GetAttribute(LocalNode, "Title"); if (TYml2Xml.HasAttribute(LocalNode, "CreatedAt")) { FamilyRow.DateCreated = Convert.ToDateTime(TYml2Xml.GetAttribute(LocalNode, "CreatedAt")); } PartnerRow.AddresseeTypeCode = MPartnerConstants.PARTNERCLASS_FAMILY; PartnerRow.PartnerShortName = Calculations.DeterminePartnerShortName(FamilyRow.FamilyName, FamilyRow.Title, FamilyRow.FirstName); } if (PartnerClass == MPartnerConstants.PARTNERCLASS_PERSON) { PPersonRow PersonRow; if (IsExistingPartner) { AMainDS.Merge(PPersonAccess.LoadByPrimaryKey(PartnerRow.PartnerKey, ATransaction)); AMainDS.PPerson.DefaultView.RowFilter = String.Format("{0} = '{1}'", PPersonTable.GetPartnerKeyDBName(), PartnerRow.PartnerKey); PersonRow = (PPersonRow)AMainDS.PPerson.DefaultView[0].Row; } else { PersonRow = AMainDS.PPerson.NewRowTyped(); PersonRow.PartnerKey = PartnerRow.PartnerKey; AMainDS.PPerson.Rows.Add(PersonRow); } PersonRow.FamilyName = TYml2Xml.GetAttributeRecursive(LocalNode, "LastName"); PersonRow.FirstName = TYml2Xml.GetAttribute(LocalNode, "FirstName"); PersonRow.Title = TYml2Xml.GetAttribute(LocalNode, "Title"); if (TYml2Xml.HasAttribute(LocalNode, "CreatedAt")) { PersonRow.DateCreated = Convert.ToDateTime(TYml2Xml.GetAttribute(LocalNode, "CreatedAt")); } // PersonRow.Sp PartnerRow.PartnerShortName = Calculations.DeterminePartnerShortName(PersonRow.FamilyName, PersonRow.Title, PersonRow.FirstName); } else if (PartnerClass == MPartnerConstants.PARTNERCLASS_ORGANISATION) { POrganisationRow OrganisationRow; if (IsExistingPartner) { AMainDS.Merge(POrganisationAccess.LoadByPrimaryKey(PartnerRow.PartnerKey, ATransaction)); AMainDS.POrganisation.DefaultView.RowFilter = String.Format("{0} = '{1}'", POrganisationTable.GetPartnerKeyDBName(), PartnerRow.PartnerKey); OrganisationRow = (POrganisationRow)AMainDS.POrganisation.DefaultView[0].Row; } else { OrganisationRow = AMainDS.POrganisation.NewRowTyped(); OrganisationRow.PartnerKey = PartnerRow.PartnerKey; AMainDS.POrganisation.Rows.Add(OrganisationRow); } OrganisationRow.OrganisationName = TYml2Xml.GetAttributeRecursive(LocalNode, "Name"); PartnerRow.PartnerShortName = OrganisationRow.OrganisationName; } else if (PartnerClass == MPartnerConstants.PARTNERCLASS_UNIT) { PUnitRow UnitRow; if (IsExistingPartner) { AMainDS.Merge(PUnitAccess.LoadByPrimaryKey(PartnerRow.PartnerKey, ATransaction)); AMainDS.PUnit.DefaultView.RowFilter = String.Format("{0} = '{1}'", PUnitTable.GetPartnerKeyDBName(), PartnerRow.PartnerKey); UnitRow = (PUnitRow)AMainDS.PUnit.DefaultView[0].Row; } else { UnitRow = AMainDS.PUnit.NewRowTyped(); UnitRow.PartnerKey = PartnerRow.PartnerKey; AMainDS.PUnit.Rows.Add(UnitRow); } UnitRow.UnitTypeCode = TYml2Xml.GetAttributeRecursive(LocalNode, "UnitTypeCode"); UnitRow.UnitName = TYml2Xml.GetAttributeRecursive(LocalNode, "Name"); if (PartnerRow.PartnerKey < -1) { throw new Exception("Invalid Partner Key or No Partner Key - and no proper handling implemented"); // from here... /* * AVerificationResult.Add(new TVerificationResult( * String.Format(Catalog.GetString("Importing Unit {0}"), UnitRow.UnitName), * Catalog.GetString("You need to provide a partner key for the unit"), * TResultSeverity.Resv_Critical)); */ // ...to here: throws Exception in case of a illegal import file? // The above code must have a glitch } if (!TYml2Xml.HasAttribute(LocalNode, "ParentUnitKey")) { throw new Exception( "The currently being processed unit (PartnerKey " + PartnerRow.PartnerKey + ") requires a ParentUnitKey."); } Int64 ParentKey = Convert.ToInt64(TYml2Xml.GetAttributeRecursive(LocalNode, "ParentUnitKey")); UmUnitStructureRow UnitStructureRow = null; if (IsExistingPartner) { AMainDS.Merge(UmUnitStructureAccess.LoadViaPUnitChildUnitKey(PartnerRow.PartnerKey, ATransaction)); AMainDS.UmUnitStructure.DefaultView.RowFilter = String.Format("{0} = '{1}'", UmUnitStructureTable.GetChildUnitKeyDBName(), PartnerRow.PartnerKey); if (AMainDS.UmUnitStructure.DefaultView.Count > 0) { UnitStructureRow = (UmUnitStructureRow)AMainDS.UmUnitStructure.DefaultView[0].Row; } } if (UnitStructureRow == null) { UnitStructureRow = AMainDS.UmUnitStructure.NewRowTyped(); UnitStructureRow.ParentUnitKey = ParentKey; UnitStructureRow.ChildUnitKey = PartnerRow.PartnerKey; AMainDS.UmUnitStructure.Rows.Add(UnitStructureRow); } else { UnitStructureRow.ParentUnitKey = ParentKey; UnitStructureRow.ChildUnitKey = PartnerRow.PartnerKey; } PartnerRow.PartnerShortName = UnitRow.UnitName; } else if (PartnerClass == MPartnerConstants.PARTNERCLASS_BANK) { PBankRow BankRow; if (IsExistingPartner) { AMainDS.Merge(PBankAccess.LoadByPrimaryKey(PartnerRow.PartnerKey, ATransaction)); AMainDS.PBank.DefaultView.RowFilter = String.Format("{0} = '{1}'", PBankTable.GetPartnerKeyDBName(), PartnerRow.PartnerKey); BankRow = (PBankRow)AMainDS.PBank.DefaultView[0].Row; } else { BankRow = AMainDS.PBank.NewRowTyped(); BankRow.PartnerKey = PartnerRow.PartnerKey; AMainDS.PBank.Rows.Add(BankRow); } BankRow.BranchName = TYml2Xml.GetAttribute(LocalNode, "BranchName"); BankRow.BranchCode = TYml2Xml.GetAttribute(LocalNode, "BranchCode"); BankRow.Bic = TYml2Xml.GetAttribute(LocalNode, "BranchBic"); BankRow.EpFormatFile = TYml2Xml.GetAttribute(LocalNode, "EpFormatFile"); if (TYml2Xml.HasAttribute(LocalNode, "CreatedAt")) { BankRow.DateCreated = Convert.ToDateTime(TYml2Xml.GetAttribute(LocalNode, "CreatedAt")); } } else { /* * throw new Exception( * "Unknown Partner Class" + * TYml2Xml.GetAttributeRecursive(LocalNode, "class")); */ // TODO AVerificationResult add failing problem: unknown partner class } PartnerRow.StatusCode = TYml2Xml.GetAttributeRecursive(LocalNode, "status"); // import special types StringCollection SpecialTypes = StringHelper.StrSplit(TYml2Xml.GetAttributeRecursive(LocalNode, "SpecialTypes"), ","); if (IsExistingPartner) { PPartnerTypeAccess.LoadViaPPartner(AMainDS, PartnerRow.PartnerKey, ATransaction); } foreach (string SpecialType in SpecialTypes) { PPartnerTypeRow PartnerTypeRow = null; AMainDS.PPartnerType.DefaultView.RowFilter = String.Format("{0}={1} AND {2}='{3}'", PPartnerTypeTable.GetPartnerKeyDBName(), PartnerRow.PartnerKey, PPartnerTypeTable.GetTypeCodeDBName(), SpecialType ); if (AMainDS.PPartnerType.DefaultView.Count > 0) { PartnerTypeRow = (PPartnerTypeRow)AMainDS.PPartnerType.DefaultView[0].Row; } else { PartnerTypeRow = AMainDS.PPartnerType.NewRowTyped(); PartnerTypeRow.PartnerKey = PartnerRow.PartnerKey; PartnerTypeRow.TypeCode = SpecialType.Trim(); AMainDS.PPartnerType.Rows.Add(PartnerTypeRow); } // Check Partner type exists, or create it bool TypeIsKnown = PTypeAccess.Exists(PartnerTypeRow.TypeCode, ATransaction); if (!TypeIsKnown) { Int32 RowIdx = AMainDS.PType.DefaultView.Find(PartnerTypeRow.TypeCode); // I might have created it a second ago.. if (RowIdx < 0) { PTypeRow TypeRow = AMainDS.PType.NewRowTyped(); TypeRow.TypeCode = PartnerTypeRow.TypeCode; TypeRow.TypeDescription = "Created from YAML import"; AMainDS.PType.Rows.Add(TypeRow); } } } // import subscriptions StringCollection Subscriptions = StringHelper.StrSplit(TYml2Xml.GetAttributeRecursive(LocalNode, "Subscriptions"), ","); foreach (string publicationCode in Subscriptions) { PSubscriptionRow subscription = AMainDS.PSubscription.NewRowTyped(); subscription.PartnerKey = PartnerRow.PartnerKey; subscription.PublicationCode = publicationCode.Trim(); subscription.ReasonSubsGivenCode = "FREE"; AMainDS.PSubscription.Rows.Add(subscription); } // import address XmlNode addressNode = TYml2Xml.GetChild(LocalNode, "Address"); if ((addressNode == null) || (TYml2Xml.GetAttributeRecursive(addressNode, "Street").Length == 0)) { if (!IsExistingPartner) { // add the empty location PPartnerLocationRow partnerlocation = AMainDS.PPartnerLocation.NewRowTyped(true); partnerlocation.SiteKey = 0; partnerlocation.PartnerKey = PartnerRow.PartnerKey; partnerlocation.DateEffective = DateTime.Now; partnerlocation.LocationType = "HOME"; partnerlocation.SendMail = false; partnerlocation.EmailAddress = TYml2Xml.GetAttributeRecursive(addressNode, "Email"); partnerlocation.TelephoneNumber = TYml2Xml.GetAttributeRecursive(addressNode, "Phone"); partnerlocation.MobileNumber = TYml2Xml.GetAttributeRecursive(addressNode, "MobilePhone"); AMainDS.PPartnerLocation.Rows.Add(partnerlocation); } } else { PLocationRow location = AMainDS.PLocation.NewRowTyped(true); location.LocationKey = (AMainDS.PLocation.Rows.Count + 1) * -1; location.SiteKey = 0; if (!TYml2Xml.HasAttributeRecursive(LocalNode, "Country")) { throw new Exception(Catalog.GetString("Missing Country Attribute")); } location.CountryCode = TYml2Xml.GetAttributeRecursive(addressNode, "Country"); location.StreetName = TYml2Xml.GetAttributeRecursive(addressNode, "Street"); location.City = TYml2Xml.GetAttributeRecursive(addressNode, "City"); location.PostalCode = TYml2Xml.GetAttributeRecursive(addressNode, "PostCode"); AMainDS.PLocation.Rows.Add(location); PPartnerLocationRow partnerlocation = AMainDS.PPartnerLocation.NewRowTyped(true); partnerlocation.SiteKey = 0; partnerlocation.LocationKey = location.LocationKey; partnerlocation.PartnerKey = PartnerRow.PartnerKey; partnerlocation.SendMail = true; partnerlocation.DateEffective = DateTime.Now; partnerlocation.LocationType = "HOME"; partnerlocation.EmailAddress = TYml2Xml.GetAttributeRecursive(addressNode, "Email"); partnerlocation.TelephoneNumber = TYml2Xml.GetAttributeRecursive(addressNode, "Phone"); partnerlocation.MobileNumber = TYml2Xml.GetAttributeRecursive(addressNode, "MobilePhone"); AMainDS.PPartnerLocation.Rows.Add(partnerlocation); } // import finance details (bank account number) XmlNode financialDetailsNode = TYml2Xml.GetChild(LocalNode, "FinancialDetails"); ParseFinancialDetails(AMainDS, financialDetailsNode, PartnerRow.PartnerKey, ATransaction); } LocalNode = LocalNode.NextSibling; } }