public static string MeterStatusSummaryDrillDown(List<SpaceStatusModel> modelForView, CustomerConfig customerCfg) { StringBuilder sb = new StringBuilder(); // Build list of unique meters -- which will be our grouping List<int> uniqueMeterIDs = new List<int>(); foreach (SpaceStatusModel nextSpaceModel in modelForView) { if (uniqueMeterIDs.IndexOf(nextSpaceModel.MeterID) == -1) uniqueMeterIDs.Add(nextSpaceModel.MeterID); } // Loop for each meter foreach (int nextMeterID in uniqueMeterIDs) { // Build list of indexes inside modelForView that are for the current meter List<int> itemIndexesForCurrentMeter = new List<int>(); for (int loIdx = 0; loIdx < modelForView.Count; loIdx++) { if (modelForView[loIdx].MeterID == nextMeterID) itemIndexesForCurrentMeter.Add(loIdx); } SpaceStatusModel currentSpaceStatusModel = modelForView[itemIndexesForCurrentMeter[0]]; int totalSpaces = 0; int totalExpired = 0; int totalPaid = 0; int totalOther = 0; foreach (int itemIdx in itemIndexesForCurrentMeter) { SpaceStatusModel nextSpaceModel = modelForView[itemIdx]; totalSpaces++; /* if (nextSpaceModel.BayExpiryState == ExpiryState.Expired) totalExpired++; else if (nextSpaceModel.BayExpiryState == ExpiryState.Safe) totalPaid++; else if (nextSpaceModel.BayExpiryState == ExpiryState.Critical) totalPaid++; else totalOther++; */ if (nextSpaceModel.BayOccupancyState == OccupancyState.Empty) totalExpired++; else if (nextSpaceModel.BayOccupancyState == OccupancyState.Occupied) totalPaid++; else totalOther++; } // The TagBuilder class built-into the .NET framework is pretty nice, but it can get pretty tedious // trying to properly handle the InnerHtml if there are a lot of nested tags. To alleviate this problem, // I created a "BaseTagHelper" class that is very similar to the TagBuilder, but it hides the InnerHtml // property and replaces it with a "Children" property which is used to hold a a collection of nested // BaseTagHelper objects. When you call the ToString() method on BaseTagHelper, the InnerHtml will be // rendered automatically based on the children, and handles any level of nesting. This makes the whole // process simpler, because you can create a BaseTagHelper, add as many children (and grand-children) as // needed, then call one ToString() method that will render the main tag and all of its nested children! // Create tag for outer boundary of this entire block BaseTagHelper OuterPanel = new BaseTagHelper("div"); OuterPanel.AddCssClass("dtNoLinesOrGaps"); // Create a tag that will be used as a header row -- child of the outer boundary BaseTagHelper tbTitleRow = new BaseTagHelper("span"); OuterPanel.Children.Add(tbTitleRow); tbTitleRow.AddCssClass("dtr"); // display table-row tbTitleRow.MergeAttribute("style", "background-color:#CCFFFF;"); // Create tags that will be used as column headers -- child of the title row BaseTagHelper tbColHeader = new BaseTagHelper("span"); tbTitleRow.Children.Add(tbColHeader); tbColHeader.AddCssClass("dtc hcenter"); //display table-cell and horizontally centered tbColHeader.SetInnerText("Group"); tbColHeader = new BaseTagHelper("span"); tbTitleRow.Children.Add(tbColHeader); tbColHeader.AddCssClass("dtc hcenter"); tbColHeader.SetInnerText("Expired"); tbColHeader = new BaseTagHelper("span"); tbTitleRow.Children.Add(tbColHeader); tbColHeader.AddCssClass("dtc hcenter"); tbColHeader.SetInnerText("Vacant"); // Create a tag to be used as a group row -- child of the outer panel BaseTagHelper tbGroupRow = new BaseTagHelper("span"); OuterPanel.Children.Add(tbGroupRow); tbGroupRow.AddCssClass("dtr"); tbGroupRow.MergeAttribute("style", "background-color:#33FFFF;"); // Create a tag to be used as a column in the group row -- child of the group row BaseTagHelper tbGroupCol = new BaseTagHelper("span"); tbGroupRow.Children.Add(tbGroupCol); tbGroupCol.AddCssClass("dtc hcenter vcenter"); //display table-cell and both horizontally and vertically centered tbGroupCol.SetInnerText("Meter: " + currentSpaceStatusModel.MeterID.ToString()); // Column #2 of group row tbGroupCol = new BaseTagHelper("span"); tbGroupRow.Children.Add(tbGroupCol); tbGroupCol.AddCssClass("dtc hcenter vcenter"); //display table-cell and both horizontally and vertically centered tbGroupCol.SetInnerText(" "); // BaseTagHelper tbRoundPanel = new BaseTagHelper("span"); tbGroupCol.Children.Add(tbRoundPanel); tbRoundPanel.AddCssClass("rounded-corners NewMeterBlock"); tbRoundPanel.MergeAttribute("style", "width:120px; text-align:center; background-color:#99FFCC; border-collapse:separate; boder-spacing:1px;"); BaseTagHelper helloCell = new BaseTagHelper("span"); tbRoundPanel.Children.Add(helloCell); helloCell.SetInnerText(totalExpired.ToString() + "/" + totalSpaces.ToString()); BaseTagHelper tbTbl1 = new BaseTagHelper("div"); tbRoundPanel.Children.Add(tbTbl1); tbTbl1.MergeAttribute("style", "display:table; border:1px solid black; width:90%; margin:4px; border-collapse:separate; boder-spacing:1px;"); BaseTagHelper tbTbl1Row1 = new BaseTagHelper("span"); tbTbl1.Children.Add(tbTbl1Row1); tbTbl1Row1.AddCssClass("dtr"); BaseTagHelper tempCell = new BaseTagHelper("span"); tbTbl1Row1.Children.Add(tempCell); tempCell.AddCssClass("dtc"); tempCell.SetInnerText("Col1"); tempCell = new BaseTagHelper("span"); tbTbl1Row1.Children.Add(tempCell); tempCell.AddCssClass("dtc"); tempCell.MergeAttribute("style", "background-color:gray; width:100%;"); BaseTagHelper tbTbl2 = new BaseTagHelper("div"); tempCell.Children.Add(tbTbl2); tbTbl2.MergeAttribute("style", "display:table; width:100%;"); BaseTagHelper tbTbl2Row1 = new BaseTagHelper("span"); tbTbl2.Children.Add(tbTbl2Row1); tbTbl2Row1.AddCssClass("dtr"); int percent = 0; if (totalSpaces > 0) percent = Convert.ToInt32((Convert.ToDouble(totalExpired) / Convert.ToDouble(totalSpaces)) * 100.0f); if (percent > 0) { tempCell = new BaseTagHelper("span"); tbTbl2Row1.Children.Add(tempCell); tempCell.AddCssClass("dtc"); tempCell.MergeAttribute("style", "width:" + percent.ToString() + "%; padding:0px; background-color:Red; overflow:none;"); tempCell.SetInnerHtml(" "); // DEBUG: How do we use a not-breaking space? } percent = 0; if (totalSpaces > 0) percent = Convert.ToInt32((Convert.ToDouble(totalOther) / Convert.ToDouble(totalSpaces)) * 100.0f); if (percent > 0) { tempCell = new BaseTagHelper("span"); tbTbl2Row1.Children.Add(tempCell); tempCell.AddCssClass("dtc"); tempCell.MergeAttribute("style", "width:" + percent.ToString() + "%; padding:0px; background-color:Yellow; overflow:none;"); tempCell.SetInnerHtml(" "); } percent = 0; if (totalSpaces > 0) percent = Convert.ToInt32((Convert.ToDouble(totalPaid) / Convert.ToDouble(totalSpaces)) * 100.0f); if (percent > 0) { tempCell = new BaseTagHelper("span"); tbTbl2Row1.Children.Add(tempCell); tempCell.AddCssClass("dtc"); tempCell.MergeAttribute("style", "width:" + percent.ToString() + "%; padding:0px; background-color:Green; overflow:none;"); tempCell.SetInnerHtml(" "); //DEBUG: onclick="alert('clicked the green');" } // Column #3 of group row tbGroupCol = new BaseTagHelper("span"); tbGroupRow.Children.Add(tbGroupCol); tbGroupCol.AddCssClass("dtc hcenter vcenter"); //display table-cell and both horizontally and vertically centered tbGroupCol.SetInnerText("col3"); // tbRoundPanel = new BaseTagHelper("span"); tbGroupCol.Children.Add(tbRoundPanel); tbRoundPanel.AddCssClass("rounded-corners NewMeterBlock"); tbRoundPanel.MergeAttribute("style", "width:120px; text-align:center; background-color:#99FFCC; border-collapse:separate; boder-spacing:1px;"); tbRoundPanel.SetInnerText("Hello2"); // // Now render the outer panel tag, and we will obtain the Html for all of its children too sb.AppendLine(OuterPanel.ToString(TagRenderMode.Normal)); } return sb.ToString(); }
// Strongly type input instead of dataset public static string ClusterStatusBlock(List<SpaceStatusModel> modelForView, CustomerConfig customerCfg) { StringBuilder sb = new StringBuilder(); // Build list of unique meters -- which will be our grouping List<int> uniqueMeterIDs = new List<int>(); foreach (SpaceStatusModel nextSpaceModel in modelForView) { if (uniqueMeterIDs.IndexOf(nextSpaceModel.MeterID) == -1) uniqueMeterIDs.Add(nextSpaceModel.MeterID); } // Loop for each meter foreach (int nextMeterID in uniqueMeterIDs) { // Build list of indexes inside modelForView that are for the current meter List<int> itemIndexesForCurrentMeter = new List<int>(); for (int loIdx = 0; loIdx < modelForView.Count; loIdx++) { if (modelForView[loIdx].MeterID == nextMeterID) itemIndexesForCurrentMeter.Add(loIdx); } SpaceStatusModel currentSpaceStatusModel = modelForView[itemIndexesForCurrentMeter[0]]; sb.AppendLine("<div class=\" rounded-corners NewMeterBlock\"> "); sb.AppendLine("<div class=\"NewMeterTitle\">Meter " + currentSpaceStatusModel.MeterID.ToString()); sb.AppendLine("<span class=\"NewMeterSubTitle\"> [Idle For: " + currentSpaceStatusModel.Meter_imin.ToString() + " minutes] [Last Updated: " + currentSpaceStatusModel.Meter_upTS.ToString() + "]"); // DEBUG: We need formatted string like: row["upTSString"].ToString() sb.AppendLine("</span> "); sb.AppendLine("</div> "); // Loop through each bay of current meter for (int loIdx = 0; loIdx < itemIndexesForCurrentMeter.Count; loIdx++) { currentSpaceStatusModel = modelForView[itemIndexesForCurrentMeter[loIdx]]; sb.AppendLine("<div class=\"NewBayBlockGavin\"> "); sb.AppendLine("<div class=\"NewBayTitle\">" + currentSpaceStatusModel.BayID.ToString() + "</div> "); // Determine the payment expiration state Duncan.PEMS.SpaceStatus.Models.ExpiryState es = currentSpaceStatusModel.BayExpiryState; string ExpiryTimeString = currentSpaceStatusModel.GetExpiryTimeString(customerCfg); // Output appropriate HTML elements for the expiration state if (es == Duncan.PEMS.SpaceStatus.Models.ExpiryState.Safe) { sb.AppendLine("<div class=\"CenteredBlock BayExpiryTimeSafe\">" + ExpiryTimeString + "</div> "); } else if (es == Duncan.PEMS.SpaceStatus.Models.ExpiryState.Expired) { sb.AppendLine("<div class=\"CenteredBlock BayExpiryTimeExpired\">" + ExpiryTimeString + "</div> "); } else if (es == Duncan.PEMS.SpaceStatus.Models.ExpiryState.Critical) { sb.AppendLine("<div class=\"CenteredBlock BayExpiryTimeCritical\">" + ExpiryTimeString + "</div> "); } else if (es == Duncan.PEMS.SpaceStatus.Models.ExpiryState.Grace) { sb.AppendLine("<div class=\"CenteredBlock BayExpiryTimeGracePeriod\">" + ExpiryTimeString + "</div> "); } else { sb.AppendLine("<div class=\"CenteredBlock NewBayExpiryTimeInoperational\">" + ExpiryTimeString + "</div> "); } ///////////////////////////// // Get the vehicle occupancy state Duncan.PEMS.SpaceStatus.Models.OccupancyState os = currentSpaceStatusModel.BayOccupancyState; // Output appropriate HTML elements for the occupancy state string TimeSinceLastInOut = BindShortTimeSpan(currentSpaceStatusModel.TimeSinceLastInOut, currentSpaceStatusModel.BayOccupancyState, customerCfg); string LastInOutTime = BindTimeOfDay(currentSpaceStatusModel.BayVehicleSensingTimestamp, currentSpaceStatusModel.BayOccupancyState, customerCfg); switch (os) { case Duncan.PEMS.SpaceStatus.Models.OccupancyState.Empty: sb.AppendLine("<div class=\"CenteredBlock BayOccupancyEmpty\"></div>"); sb.AppendLine("<div class=\"LastInOutTimeOut\">" + TimeSinceLastInOut + "</div>"); sb.AppendLine("<div class=\"LastInOutTimeOfDay\"> <span>" + LastInOutTime + "</span> </div>"); break; case Duncan.PEMS.SpaceStatus.Models.OccupancyState.NotAvailable: sb.AppendLine("<div class=\"CenteredBlock BayOccupancyNotAvailable\"></div>"); sb.AppendLine("<div class=\"LastInOutTimeNA\">" + TimeSinceLastInOut + "</div>"); sb.AppendLine("<div class=\"LastInOutTimeOfDayNA\"> <span>" + LastInOutTime + "</span> </div>"); break; case Duncan.PEMS.SpaceStatus.Models.OccupancyState.Occupied: sb.AppendLine("<div class=\"CenteredBlock BayOccupancyOccupied\"></div>"); sb.AppendLine("<div class=\"LastInOutTimeIn\">" + TimeSinceLastInOut + "</div>"); sb.AppendLine("<div class=\"LastInOutTimeOfDay\">" + LastInOutTime + "</div>"); break; case Duncan.PEMS.SpaceStatus.Models.OccupancyState.OutOfDate: sb.AppendLine("<div class=\"CenteredBlock BayOccupancyOutOfDate\"></div>"); sb.AppendLine("<div class=\"LastInOutTimeNA\">" + TimeSinceLastInOut + "</div>"); sb.AppendLine("<div class=\"LastInOutTimeOfDayNA\"> <span>" + LastInOutTime + "</span> </div>"); break; case Duncan.PEMS.SpaceStatus.Models.OccupancyState.Violation: sb.AppendLine("<div class=\"CenteredBlock Violation\"></div>"); sb.AppendLine("<div class=\"LastInOutTimeIn\">" + TimeSinceLastInOut + "</div>"); sb.AppendLine("<div class=\"LastInOutTimeOfDay\"> <span>" + LastInOutTime + "</span> </div>"); break; case Duncan.PEMS.SpaceStatus.Models.OccupancyState.MeterFeeding: sb.AppendLine("<div class=\"CenteredBlock MeterFeeding\"></div>"); sb.AppendLine("<div class=\"LastInOutTimeIn\">" + TimeSinceLastInOut + "</div>"); sb.AppendLine("<div class=\"LastInOutTimeOfDay\"> <span>" + LastInOutTime + "</span> </div>"); break; case Duncan.PEMS.SpaceStatus.Models.OccupancyState.Unknown: sb.AppendLine("<div class=\"CenteredBlock BayOccupancyNotAvailable\"></div>"); sb.AppendLine("<div class=\"LastInOutTimeNA\">" + TimeSinceLastInOut + "</div>"); sb.AppendLine("<div class=\"LastInOutTimeOfDayNA\"> <span>" + LastInOutTime + "</span> </div>"); break; } sb.AppendLine("</div> "); // End of Bay block } sb.AppendLine("</div> "); // End of Meter block } // Finalize the result from the string builder contents return sb.ToString(); }