public static ArrayList GetUnitHeirarchy() { const Int64 THE_ORGANISATION = 1000000; ArrayList Ret = new ArrayList(); try { TDBTransaction Transaction = DBAccess.GDBAccessObj.BeginTransaction(IsolationLevel.ReadCommitted); PUnitTable UnitTbl = PUnitAccess.LoadAll(Transaction); UUnitTypeTable UnitTypeTbl = UUnitTypeAccess.LoadAll(Transaction); UnitTypeTbl.DefaultView.Sort = UUnitTypeTable.GetUnitTypeCodeDBName(); UmUnitStructureTable HierarchyTbl = UmUnitStructureAccess.LoadAll(Transaction); HierarchyTbl.DefaultView.Sort = UmUnitStructureTable.GetChildUnitKeyDBName(); UnitTbl.DefaultView.Sort = PUnitTable.GetPartnerKeyDBName(); UnitHierarchyNode RootNode = new UnitHierarchyNode(); RootNode.MyUnitKey = THE_ORGANISATION; RootNode.ParentUnitKey = THE_ORGANISATION; RootNode.Description = "The Organisation"; RootNode.TypeCode = "Root"; Int32 RootUnitIdx = UnitTbl.DefaultView.Find(THE_ORGANISATION); if (RootUnitIdx >= 0) { RootNode.Description = ((PUnitRow)UnitTbl.DefaultView[RootUnitIdx].Row).UnitName; UnitTbl.DefaultView.Delete(RootUnitIdx); } Ret.Add(RootNode); foreach (DataRowView rv in UnitTbl.DefaultView) { PUnitRow UnitRow = (PUnitRow)rv.Row; UnitHierarchyNode Node = new UnitHierarchyNode(); Node.Description = UnitRow.UnitName + " " + UnitRow.Description; if (Node.Description == "") { Node.Description = "[" + UnitRow.PartnerKey.ToString("D10") + "]"; } Node.MyUnitKey = UnitRow.PartnerKey; // // Retrieve parent.. Int32 HierarchyTblIdx = HierarchyTbl.DefaultView.Find(Node.MyUnitKey); if (HierarchyTblIdx >= 0) { Node.ParentUnitKey = ((UmUnitStructureRow)HierarchyTbl.DefaultView[HierarchyTblIdx].Row).ParentUnitKey; } else { Node.ParentUnitKey = THE_ORGANISATION; } // // Retrieve TypeCode.. Int32 TypeTblIndex = UnitTypeTbl.DefaultView.Find(UnitRow.UnitTypeCode); if (TypeTblIndex >= 0) { Node.TypeCode = ((UUnitTypeRow)UnitTypeTbl.DefaultView[TypeTblIndex].Row).UnitTypeName; } else { Node.TypeCode = "Type: " + UnitRow.UnitTypeCode; } Ret.Add(Node); } } finally { DBAccess.GDBAccessObj.RollbackTransaction(); } return(Ret); }
/// <summary> /// Get recursively all the child units of a unit and puts them into the /// results list. /// </summary> /// <param name="AUnitKey">Parent unit to get the child unit from</param> /// <param name="AChildLevel">Indicates how deep we are in the recursion</param> /// <param name="AWithOutreaches">Indicates if outreaches and conferences should /// be included in the result</param> /// <param name="AChildRow">the number of the row</param> /// <returns>False if the parent unit is not active. /// Otherwise true</returns> private bool GetChildUnits(long AUnitKey, int AChildLevel, bool AWithOutreaches, ref int AChildRow) { UmUnitStructureTable UnitStructure; PUnitTable UnitTable; PPartnerTable PartnerTable; UUnitTypeTable UnitType; PartnerTable = PPartnerAccess.LoadByPrimaryKey(AUnitKey, situation.GetDatabaseConnection().Transaction); if ((PartnerTable.Rows.Count > 0) && (((PPartnerRow)PartnerTable.Rows[0]).StatusCode != "ACTIVE")) { return(false); } string PreceedingWhiteSpaces = new string(' ', AChildLevel * 2); UnitStructure = UmUnitStructureAccess.LoadViaPUnitParentUnitKey(AUnitKey, situation.GetDatabaseConnection().Transaction); // Add this unit to the results UnitTable = PUnitAccess.LoadByPrimaryKey(AUnitKey, situation.GetDatabaseConnection().Transaction); if (UnitTable.Rows.Count > 0) { PUnitRow UnitRow = (PUnitRow)UnitTable.Rows[0]; string UnitTypeName = UnitRow.UnitTypeCode; UnitType = UUnitTypeAccess.LoadByPrimaryKey(UnitRow.UnitTypeCode, situation.GetDatabaseConnection().Transaction); if (UnitType.Rows.Count > 0) { UnitTypeName = ((UUnitTypeRow)UnitType.Rows[0]).UnitTypeName; } string UnitKeyString = FormatAsUnitKey(UnitRow.PartnerKey); TVariant[] Header = { new TVariant(), new TVariant(), new TVariant(), new TVariant() }; TVariant[] Description = { new TVariant(), new TVariant() }; TVariant[] Columns = { new TVariant(PreceedingWhiteSpaces + UnitKeyString), new TVariant(PreceedingWhiteSpaces + UnitTypeName), new TVariant(PreceedingWhiteSpaces + UnitRow.UnitName), new TVariant(UnitRow.PartnerKey) }; situation.GetResults().AddRow(0, AChildRow++, true, 1, "", "", false, Header, Description, Columns); } // // Add the children to the results // SortedList <string, long> ChildList = new SortedList <string, long>(); AChildLevel++; foreach (DataRow Row in UnitStructure.Rows) { // Add the name and the key into a sorted list // so we can sort the result alphabetically long ChildUnitKey = (long)Row[UmUnitStructureTable.GetChildUnitKeyDBName()]; if (ChildUnitKey == AUnitKey) { continue; } UnitTable = PUnitAccess.LoadByPrimaryKey(ChildUnitKey, situation.GetDatabaseConnection().Transaction); if (UnitTable.Rows.Count < 1) { continue; } PUnitRow UnitRow = (PUnitRow)UnitTable.Rows[0]; string UnitName = UnitRow.UnitName; string UnitTypeName = UnitRow.UnitTypeCode; if (!AWithOutreaches && ((UnitTypeName.StartsWith("GA")) || (UnitTypeName.StartsWith("GC")) || (UnitTypeName.StartsWith("TN")) || (UnitTypeName.StartsWith("TS")))) { continue; } // use as key UnitName (for sorting) plus UnitKey so that it is // unique. We might have two units with the same name ChildList.Add(UnitName + ChildUnitKey.ToString(), ChildUnitKey); } foreach (KeyValuePair <string, long> kvp in ChildList) { GetChildUnits(kvp.Value, AChildLevel, AWithOutreaches, ref AChildRow); } return(true); }
public static ArrayList GetUnitHeirarchy() { const Int64 THE_ORGANISATION = 1000000; ArrayList Ret = new ArrayList(); TDBTransaction Transaction = null; DBAccess.GDBAccessObj.BeginAutoReadTransaction(IsolationLevel.ReadCommitted, ref Transaction, delegate { PPartnerTable PartnerTbl = PPartnerAccess.LoadViaPPartnerClasses("UNIT", Transaction); PartnerTbl.DefaultView.RowFilter = "p_status_code_c <> 'MERGED'"; PartnerTbl.DefaultView.Sort = PPartnerTable.GetPartnerKeyDBName(); PUnitTable UnitTbl = PUnitAccess.LoadAll(Transaction); UUnitTypeTable UnitTypeTbl = UUnitTypeAccess.LoadAll(Transaction); UnitTypeTbl.DefaultView.Sort = UUnitTypeTable.GetUnitTypeCodeDBName(); UmUnitStructureTable HierarchyTbl = UmUnitStructureAccess.LoadAll(Transaction); HierarchyTbl.DefaultView.Sort = UmUnitStructureTable.GetChildUnitKeyDBName(); UnitTbl.DefaultView.Sort = PUnitTable.GetPartnerKeyDBName(); UnitHierarchyNode RootNode = new UnitHierarchyNode(); UnitHierarchyNode UnassignedNode = new UnitHierarchyNode(); RootNode.MyUnitKey = THE_ORGANISATION; RootNode.ParentUnitKey = THE_ORGANISATION; RootNode.Description = "The Organisation"; RootNode.TypeCode = "Root"; Int32 RootUnitIdx = UnitTbl.DefaultView.Find(THE_ORGANISATION); if (RootUnitIdx >= 0) { RootNode.Description = ((PUnitRow)UnitTbl.DefaultView[RootUnitIdx].Row).UnitName; UnitTbl.DefaultView.Delete(RootUnitIdx); } Ret.Add(RootNode); UnassignedNode.MyUnitKey = 0; UnassignedNode.ParentUnitKey = 0; UnassignedNode.Description = Catalog.GetString("Unassigned Units"); Ret.Add(UnassignedNode); foreach (DataRowView rv in UnitTbl.DefaultView) { PUnitRow UnitRow = (PUnitRow)rv.Row; if (PartnerTbl.DefaultView.Find(UnitRow.PartnerKey) < 0) { // skip all merged units continue; } UnitHierarchyNode Node = new UnitHierarchyNode(); Node.Description = UnitRow.UnitName + " " + UnitRow.Description; if (Node.Description == "") { Node.Description = "[" + UnitRow.PartnerKey.ToString("D10") + "]"; } Node.MyUnitKey = UnitRow.PartnerKey; // // Retrieve parent.. Int32 HierarchyTblIdx = HierarchyTbl.DefaultView.Find(Node.MyUnitKey); if (HierarchyTblIdx >= 0) { Node.ParentUnitKey = ((UmUnitStructureRow)HierarchyTbl.DefaultView[HierarchyTblIdx].Row).ParentUnitKey; } else { Node.ParentUnitKey = UnassignedNode.MyUnitKey; } // // Retrieve TypeCode.. Int32 TypeTblIndex = UnitTypeTbl.DefaultView.Find(UnitRow.UnitTypeCode); if (TypeTblIndex >= 0) { Node.TypeCode = ((UUnitTypeRow)UnitTypeTbl.DefaultView[TypeTblIndex].Row).UnitTypeName; } else { Node.TypeCode = "Type: " + UnitRow.UnitTypeCode; } Ret.Add(Node); } }); return(Ret); }