Example #1
0
        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);
        }