ProjectStatusAction.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » WebDashboard » Plugins » ProjectReport » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » WebDashboard » Plugins » ProjectReport » ProjectStatusAction.cs
using System.Collections;
using Exortech.NetReflector;
using ThoughtWorks.CruiseControl.Core.Reporting.Dashboard.Navigation;
using ThoughtWorks.CruiseControl.Remote;
using ThoughtWorks.CruiseControl.WebDashboard.Dashboard;
using ThoughtWorks.CruiseControl.WebDashboard.IO;
using ThoughtWorks.CruiseControl.WebDashboard.MVC;
using ThoughtWorks.CruiseControl.WebDashboard.MVC.Cruise;
using ThoughtWorks.CruiseControl.WebDashboard.MVC.View;
using ThoughtWorks.CruiseControl.WebDashboard.ServerConnection;
using System.Xml;
using System.Text;
using System;

namespace ThoughtWorks.CruiseControl.WebDashboard.Plugins.ProjectReport{
    /// <summary>
    /// Returns an XML fragment containing the current status of the project.
    /// </summary>
    public class ProjectStatusAction : ICruiseAction
    {
        public const string ActionName = "ProjectStatus";
        private readonly IFarmService farmServer;

        /// <summary>
        /// Initialise a new <see cref="ProjectStatusAction"/>.
        /// </summary>
        /// <param name="farmServer">The farm service to use.</param>
        public ProjectStatusAction(IFarmService farmServer)
        {
            this.farmServer = farmServer;
        }

        /// <summary>
        /// Generates the XML response for the specified project.
        /// </summary>
        /// <param name="cruiseRequest"></param>
        /// <returns></returns>
        public IResponse Execute(ICruiseRequest cruiseRequest)
        {
            // Retrieve the actual snapshot
            IProjectSpecifier projectSpecifier = cruiseRequest.ProjectSpecifier;
            ProjectStatusSnapshot snapshot = farmServer.TakeStatusSnapshot(projectSpecifier, cruiseRequest.RetrieveSessionToken());

            // See what type of output is required
            IResponse output = null;
            string outputType = (cruiseRequest.Request.GetText("view") ?? string.Empty).ToLower();
            switch (outputType)
            {
                case "json":
                    // Generate the output as JSON
                    string json = ConvertStatusToJson(snapshot);
                    output = new JsonFragmentResponse(json);
                    break;
                default:
                    // Default output is XML
                    string xml = snapshot.ToString();
                    output = new XmlFragmentResponse(xml);
                    break;
            }
            return output;
        }

        /// <summary>
        /// Simple method to convert a project status to JSON (since we're not using .NET 3.5).
        /// </summary>
        /// <param name="status">The status to convert.</param>
        /// <returns></returns>
        private string ConvertStatusToJson(ProjectStatusSnapshot status)
        {
            StringBuilder jsonText = new StringBuilder();
            jsonText.Append("{");
            jsonText.AppendFormat("time:{0},", ToJsonDate(status.TimeOfSnapshot));
            AppendStatusDetails(status, jsonText);
            jsonText.Append("}");
            return jsonText.ToString();
        }

        /// <summary>
        /// Simple method to convert am item status to JSON (since we're not using .NET 3.5).
        /// </summary>
        /// <param name="status">The status to convert.</param>
        /// <returns></returns>
        private string ConvertStatusToJson(ItemStatus status)
        {
            StringBuilder jsonText = new StringBuilder();
            jsonText.Append("{");
            AppendStatusDetails(status, jsonText);
            jsonText.Append("}");
            return jsonText.ToString();
        }

        /// <summary>
        /// Simple method to append status information to a builder
        /// </summary>
        /// <param name="status">The status to append.</param>
        /// <param name="builder">The builder to append the details to.</param>
        private void AppendStatusDetails(ItemStatus status, StringBuilder builder)
        {
            builder.AppendFormat("id:'{0}'", status.Identifier);
            builder.AppendFormat(",name:'{0}'", ToJsonString(status.Name));
            builder.AppendFormat(",status:'{0}'", status.Status);
            if (!string.IsNullOrEmpty(status.Description)) builder.AppendFormat(",description:'{0}'", ToJsonString(status.Description));
            if (status.TimeStarted.HasValue) builder.AppendFormat(",started:{0}", ToJsonDate(status.TimeStarted.Value));
            if (status.TimeCompleted.HasValue) builder.AppendFormat(",completed:{0}", ToJsonDate(status.TimeCompleted.Value));
            if (status.TimeOfEstimatedCompletion.HasValue) builder.AppendFormat(",estimated:{0}", ToJsonDate(status.TimeOfEstimatedCompletion.Value));
            if (status.ChildItems.Count > 0)
            {
                builder.Append(",children:[");
                int count = 0;
                foreach (ItemStatus child in status.ChildItems)
                {
                    if (count++ > 0) builder.Append(",");
                    builder.Append(ConvertStatusToJson(child));
                }
                builder.Append("]");
            }
        }

        /// <summary>
        /// Convert a <see cref="String"/> to a JSON safe version.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private string ToJsonString(string value)
        {
            string json = value.Replace("\'", "\'\'");
            return json;
        }

        /// <summary>
        /// Convert a <see cref="DateTime"/> to a JSON representation.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private string ToJsonDate(DateTime value)
        {
            string json = string.Format("new Date({0}, {1}, {2}, {3}, {4}, {5})",
                value.Year,
                value.Month - 1,
                value.Day,
                value.Hour,
                value.Minute,
                value.Second);
            return json;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.