private void _setFireClassMAQData(HMISData.HMISMaterial Hazard, CswNbtObjClassFireClassExemptAmount FireClass, Double MAQOffset)
 {
     Hazard.Storage.Solid.MAQ  = _calculateMAQOffsetPercentage(FireClass.StorageSolidExemptAmount.Text, MAQOffset);
     Hazard.Storage.Liquid.MAQ = _calculateMAQOffsetPercentage(FireClass.StorageLiquidExemptAmount.Text, MAQOffset);
     Hazard.Storage.Gas.MAQ    = _calculateMAQOffsetPercentage(FireClass.StorageGasExemptAmount.Text, MAQOffset);
     Hazard.Closed.Solid.MAQ   = _calculateMAQOffsetPercentage(FireClass.ClosedSolidExemptAmount.Text, MAQOffset);
     Hazard.Closed.Liquid.MAQ  = _calculateMAQOffsetPercentage(FireClass.ClosedLiquidExemptAmount.Text, MAQOffset);
     Hazard.Closed.Gas.MAQ     = _calculateMAQOffsetPercentage(FireClass.ClosedGasExemptAmount.Text, MAQOffset);
     Hazard.Open.Solid.MAQ     = _calculateMAQOffsetPercentage(FireClass.OpenSolidExemptAmount.Text, MAQOffset);
     Hazard.Open.Liquid.MAQ    = _calculateMAQOffsetPercentage(FireClass.OpenLiquidExemptAmount.Text, MAQOffset);
 }
        private void _setFireClasses(CswPrimaryKey ControlZoneId, HMISData Data)
        {
            CswNbtNode ControlZone = _CswNbtResources.Nodes.GetNode(ControlZoneId);
            Double     MAQOffset   = Double.NaN;
            CswNbtMetaDataNodeTypeProp MAQOffsetNTP = _CswNbtResources.MetaData.getNodeTypeProp(ControlZone.NodeTypeId, "MAQ Offset %");

            if (null != MAQOffsetNTP)
            {
                MAQOffset = ControlZone.Properties[MAQOffsetNTP].AsNumber.Value;
            }
            MAQOffset = Double.IsNaN(MAQOffset) ? 100.0 : MAQOffset;
            CswNbtMetaDataNodeTypeProp FireClassSetNameNTP = _CswNbtResources.MetaData.getNodeTypeProp(ControlZone.NodeTypeId, "Fire Class Set Name");
            CswPrimaryKey FCEASId = ControlZone.Properties[FireClassSetNameNTP].AsRelationship.RelatedNodeId;

            Data.FireClassExemptAmountSet = ControlZone.Properties[FireClassSetNameNTP].AsRelationship.CachedNodeName;
            if (null != FCEASId)
            {
                List <HMISData.HMISMaterial> HazardClassList = new List <HMISData.HMISMaterial>();
                CswNbtMetaDataObjectClass    FCEAOC          = _CswNbtResources.MetaData.getObjectClass(CswEnumNbtObjectClass.FireClassExemptAmountClass);
                foreach (CswNbtObjClassFireClassExemptAmount FCEANode in FCEAOC.getNodes(false, false))
                {
                    if (FCEANode.SetName.RelatedNodeId == FCEASId)
                    {
                        HMISData.HMISMaterial EmptyHazardClass = new HMISData.HMISMaterial
                        {
                            HazardClass    = FCEANode.HazardClass.Value,
                            HazardCategory = FCEANode.HazardCategory.Text,
                            Class          = FCEANode.Class.Text,
                            SortOrder      = FCEANode.SortOrder.Value
                        };
                        _setFireClassMAQData(EmptyHazardClass, FCEANode, MAQOffset);
                        HazardClassList.Add(EmptyHazardClass);
                    }
                }
                HazardClassList.Sort((s1, s2) => s1.SortOrder.CompareTo(s2.SortOrder));
                Data.Materials = new Collection <HMISData.HMISMaterial>(HazardClassList);
            }
        }
        private void _addQuantityDataToHMISMaterial(HMISData.HMISMaterial Material, String UseType, Double Quantity_Kgs, Double Quantity_Lts, string PhysState, CswPrimaryKey MaterialId)
        {
            CswNbtUnitConversion Conversion1 = new CswNbtUnitConversion(_CswNbtResources, _getUnitIdByName("kg"), _getBaseUnitId(PhysState), MaterialId);
            Double ConvertedQty1             = Conversion1.convertUnit(Quantity_Kgs);
            CswNbtUnitConversion Conversion2 = new CswNbtUnitConversion(_CswNbtResources, _getUnitIdByName("Liters"), _getBaseUnitId(PhysState), MaterialId);
            Double ConvertedQty2             = Conversion2.convertUnit(Quantity_Lts);
            Double ConvertedQty = ConvertedQty1 + ConvertedQty2;

            switch (UseType)
            {
            case CswEnumNbtContainerUseTypes.Storage:
                switch (PhysState.ToLower())
                {
                case CswNbtPropertySetMaterial.CswEnumPhysicalState.Solid:
                    Material.Storage.Solid.Qty += ConvertedQty;
                    break;

                case CswNbtPropertySetMaterial.CswEnumPhysicalState.Liquid:
                    Material.Storage.Liquid.Qty += ConvertedQty;
                    break;

                case CswNbtPropertySetMaterial.CswEnumPhysicalState.Gas:
                    Material.Storage.Gas.Qty += ConvertedQty;
                    break;
                }
                break;

            case CswEnumNbtContainerUseTypes.Closed:
                switch (PhysState.ToLower())
                {
                case CswNbtPropertySetMaterial.CswEnumPhysicalState.Solid:
                    Material.Storage.Solid.Qty += ConvertedQty;
                    Material.Closed.Solid.Qty  += ConvertedQty;
                    break;

                case CswNbtPropertySetMaterial.CswEnumPhysicalState.Liquid:
                    Material.Storage.Liquid.Qty += ConvertedQty;
                    Material.Closed.Liquid.Qty  += ConvertedQty;
                    break;

                case CswNbtPropertySetMaterial.CswEnumPhysicalState.Gas:
                    Material.Storage.Gas.Qty += ConvertedQty;
                    Material.Closed.Gas.Qty  += ConvertedQty;
                    break;
                }
                break;

            case CswEnumNbtContainerUseTypes.Open:
                switch (PhysState.ToLower())
                {
                case CswNbtPropertySetMaterial.CswEnumPhysicalState.Solid:
                    Material.Open.Solid.Qty += ConvertedQty;
                    break;

                case CswNbtPropertySetMaterial.CswEnumPhysicalState.Liquid:
                    Material.Open.Liquid.Qty += ConvertedQty;
                    break;
                }
                break;
            }
        } // _addQuantityDataToHMISMaterial()
        public HMISData getHMISData(HMISData.HMISDataRequest Request)
        {
            HMISData      Data          = new HMISData();
            CswPrimaryKey ControlZoneId = null;

            if (false == string.IsNullOrEmpty(Request.ControlZoneId))
            {
                ControlZoneId = CswConvert.ToPrimaryKey(Request.ControlZoneId);
            }
            else if (false == string.IsNullOrEmpty(Request.ControlZone))
            {
                if (CswTools.IsInteger(Request.ControlZone))
                {
                    ControlZoneId = new CswPrimaryKey("nodes", CswConvert.ToInt32(Request.ControlZone));
                }
                else
                {
                    CswNbtView  ControlZoneView = getControlZonesView(Request.ControlZone);
                    ICswNbtTree ControlZoneTree = _CswNbtResources.Trees.getTreeFromView(ControlZoneView, RequireViewPermissions: false, IncludeSystemNodes: true, IncludeHiddenNodes: true);
                    if (ControlZoneTree.getChildNodeCount() > 0)
                    {
                        ControlZoneTree.goToNthChild(0);
                        ControlZoneId = ControlZoneTree.getNodeIdForCurrentPosition();
                    }
                }
            }

            if (CswTools.IsPrimaryKey(ControlZoneId))
            {
                Data.ControlZone = _CswNbtResources.Nodes.getNodeName(ControlZoneId);

                string HMISSql = @"with loc as (select n.nodeid
                                                  from nodes n
                                                  join nodetypes t on t.nodetypeid = n.nodetypeid
                                                  join object_class oc on t.objectclassid = oc.objectclassid
                                                  join (select j.nodeid, j.field1_fk 
                                                          from object_class_props ocp  
                                                          join nodetype_props ntp on ocp.objectclasspropid = ntp.objectclasspropid 
                                                          join jct_nodes_props j on ntp.nodetypepropid = j.nodetypepropid
                                                         where ocp.propname = 'Control Zone'
                                                       ) cz on (cz.nodeid = n.nodeid)
                                                 where oc.objectclass = 'LocationClass'
                                                   and cz.field1_fk = " + ControlZoneId.PrimaryKey + @"
                                               ),
                                        mat as (select n.nodeid, n.nodename materialname, hc.clobdata hazardclasses, sf.gestaltsearch specialflags, ps.field1 physstate
                                                  from nodes n
                                                  join nodetypes t on t.nodetypeid = n.nodetypeid
                                                  join object_class oc on t.objectclassid = oc.objectclassid
                                                  join (select j.nodeid, j.clobdata
                                                          from object_class_props ocp  
                                                          join nodetype_props ntp on ocp.objectclasspropid = ntp.objectclasspropid 
                                                          join jct_nodes_props j on ntp.nodetypepropid = j.nodetypepropid
                                                         where ocp.propname = 'Hazard Classes'
                                                       ) hc on (hc.nodeid = n.nodeid)
                                                  join (select j.nodeid, j.gestaltsearch 
                                                          from object_class_props ocp  
                                                          join nodetype_props ntp on ocp.objectclasspropid = ntp.objectclasspropid 
                                                          join jct_nodes_props j on ntp.nodetypepropid = j.nodetypepropid
                                                         where ocp.propname = 'Special Flags'
                                                       ) sf on (sf.nodeid = n.nodeid)
                                                  join (select j.nodeid, j.field1
                                                          from object_class_props ocp  
                                                          join nodetype_props ntp on ocp.objectclasspropid = ntp.objectclasspropid 
                                                          join jct_nodes_props j on ntp.nodetypepropid = j.nodetypepropid
                                                         where ocp.propname = 'Physical State'
                                                       ) ps on (ps.nodeid = n.nodeid)
                                                 where oc.objectclass = 'ChemicalClass'
                                                   and (sf.gestaltsearch is null or sf.gestaltsearch not like '%not reportable%')";
                if (string.IsNullOrEmpty(Request.Class))
                {
                    HMISSql += "   and hc.clobdata is not null";
                }
                else
                {
                    HMISSql += "   and hc.clobdata like '%" + Request.Class + @"%'";
                }
                HMISSql += @"                ),
                                       cont as (select SUM(q.field2_numeric) total_qty_kg, 
                                                       SUM(q.field3_numeric) total_qty_lt, 
                                                       ut.field1 usetype, 
                                                       m.field1_fk materialid
                                                  from nodes n
                                                  join nodetypes t on t.nodetypeid = n.nodetypeid
                                                  join object_class oc on t.objectclassid = oc.objectclassid
                                                  join (select j.nodeid, j.field1_fk 
                                                          from object_class_props ocp  
                                                          join nodetype_props ntp on ocp.objectclasspropid = ntp.objectclasspropid 
                                                          join jct_nodes_props j on ntp.nodetypepropid = j.nodetypepropid
                                                         where ocp.propname = 'Location'
                                                       ) l on (l.nodeid = n.nodeid)
                                                  join (select j.nodeid, j.field2_numeric, j.field3_numeric 
                                                          from object_class_props ocp  
                                                          join nodetype_props ntp on ocp.objectclasspropid = ntp.objectclasspropid 
                                                          join jct_nodes_props j on ntp.nodetypepropid = j.nodetypepropid
                                                         where ocp.propname = 'Quantity'
                                                       ) q on (q.nodeid = n.nodeid)
                                                  join (select j.nodeid, j.field1 
                                                          from object_class_props ocp  
                                                          join nodetype_props ntp on ocp.objectclasspropid = ntp.objectclasspropid 
                                                          join jct_nodes_props j on ntp.nodetypepropid = j.nodetypepropid
                                                         where ocp.propname = 'Use Type'
                                                       ) ut on (ut.nodeid = n.nodeid)
                                                  join (select j.nodeid, j.field1_fk 
                                                          from object_class_props ocp  
                                                          join nodetype_props ntp on ocp.objectclasspropid = ntp.objectclasspropid 
                                                          join jct_nodes_props j on ntp.nodetypepropid = j.nodetypepropid
                                                         where ocp.propname = 'Material'
                                                       ) m on (m.nodeid = n.nodeid)
                                                 where oc.objectclass = 'ContainerClass'
                                                   and ut.field1 is not null
                                                   and l.field1_fk in (select nodeid from loc) 
                                                   and (q.field2_numeric > 0 
                                                     or q.field3_numeric > 0)
                                                 group by ut.field1, m.field1_fk
                                               )
                                   select c.*, mat.hazardclasses, mat.specialflags, mat.materialname, mat.physstate
                                     from cont c
                                     join mat on (c.materialid = mat.nodeid)";
                CswArbitrarySelect HMISSelect = _CswNbtResources.makeCswArbitrarySelect("HMIS_Select", HMISSql);
                DataTable          HMISTable  = HMISSelect.getTable();

                if (string.IsNullOrEmpty(Request.Class))
                {
                    // Get totals for all classes
                    _setFireClasses(ControlZoneId, Data);

                    foreach (DataRow row in HMISTable.Rows)
                    {
                        CswCommaDelimitedString HazardClasses = new CswCommaDelimitedString();
                        HazardClasses.FromString(CswConvert.ToString(row["hazardclasses"]));
                        if (HazardClasses.Contains("FL-1A") || HazardClasses.Contains("FL-1B") || HazardClasses.Contains("FL-1C"))
                        {
                            HazardClasses.Add("FL-Comb");
                        }
                        foreach (String HazardClass in HazardClasses)
                        {
                            HMISData.HMISMaterial HMISMaterial = Data.Materials.FirstOrDefault(EmptyHazardClass => EmptyHazardClass.HazardClass == HazardClass);
                            if (null != HMISMaterial)  //This would only be null if the Material's HazardClass options don't match the Default FireClass nodes
                            {
                                _addQuantityDataToHMISMaterial(HMISMaterial,
                                                               CswConvert.ToString(row["usetype"]),
                                                               CswConvert.ToDouble(row["total_qty_kg"]),
                                                               CswConvert.ToDouble(row["total_qty_lt"]),
                                                               CswConvert.ToString(row["physstate"]),
                                                               new CswPrimaryKey("nodes", CswConvert.ToInt32(row["materialid"])));
                            }
                        }
                    } // foreach( DataRow row in HMISTable )
                }     // if( string.IsNullOrEmpty( Request.Class ) )
                else
                {
                    // Get material information for one class
                    foreach (DataRow row in HMISTable.Rows)
                    {
                        HMISData.HMISMaterial NewMaterial = new HMISData.HMISMaterial
                        {
                            Material    = CswConvert.ToString(row["materialname"]),
                            NodeId      = CswConvert.ToInt32(row["materialid"]),
                            HazardClass = Request.Class
                        };
                        _addQuantityDataToHMISMaterial(NewMaterial,
                                                       CswConvert.ToString(row["usetype"]),
                                                       CswConvert.ToDouble(row["total_qty_kg"]),
                                                       CswConvert.ToDouble(row["total_qty_lt"]),
                                                       CswConvert.ToString(row["physstate"]),
                                                       new CswPrimaryKey("nodes", CswConvert.ToInt32(row["materialid"])));
                        Data.Materials.Add(NewMaterial);
                    }
                } // if-else( string.IsNullOrEmpty( Request.Class ) )
            }     // if( CswTools.IsPrimaryKey( ControlZoneId ) )
            return(Data);
        }