Exemple #1
0
        /// <summary>
        /// Create a new KrakenException
        /// </summary>
        /// <remarks>
        /// Only use this exception if no suitable alternative exists - eg: use InvalidArgumentException if it is a better fit
        /// </remarks>
        public static KrakenException Create(Exception innerException, string format, params object[] args)
        {
            string          message   = string.Format(format, args);
            KrakenException exception = new KrakenException(message, innerException);

            return(exception);
        }
        public string Dump(IEnumerable <T> target)
        {
            if (_dumpDataMethod == null)
            {
                throw KrakenException.Create("You used the wrong constructor for ObjectDumper");
            }
            var objectDumpList = GetDumpStructures(target);

            return(Dump(objectDumpList));
        }
Exemple #3
0
        private static Stream GetStream(Assembly assembly, string resource)
        {
            Stream stream = assembly.GetManifestResourceStream(resource);

            if (stream == null)
            {
                //Whale.Domain.Messaging.Resources.AlarmNotificationTemplate.cshtml
                //Whale.Domain.Messaging.Resources.AlarmNotificationEmail.cshtml
                throw KrakenException.Create("Embedded Resource was not found: {0}::{1}", assembly.GetName().Name, resource);
            }
            return(stream);
        }
        public static DateTime GetSettingAsDateTime(KrakenConfigLocation settingLocation, string settingKey)
        {
            switch (settingLocation)
            {
            case KrakenConfigLocation.AppSettings:
                return(GetAppSettingAsDateTime(settingKey));

            //case KrakenConfigLocation.SystemSetting:
            //    return GetDatabaseSettingAsDateTime(settingKey);
            default:
                throw KrakenException.Create("SettingLocation {0} not supported", settingLocation);
            }
        }
        /// <summary>
        /// Useful for transformation after
        /// </summary>
        public List <ObjectDump> GetDumpStructures(IEnumerable <T> target)
        {
            if (_dumpDataMethod == null)
            {
                throw KrakenException.Create("You used the wrong constructor for ObjectDumper");
            }
            var objectDumpList = new List <ObjectDump>();

            foreach (T obj in target)
            {
                var dump = _dumpDataMethod(obj);
                dump.OriginalEntity = obj;
                objectDumpList.Add(dump);
            }
            return(objectDumpList);
        }
Exemple #6
0
        /// <summary>
        /// Extracts a resource from the given assembly and dumps it's contents into a byte array
        /// </summary>
        public static byte[] ExportToBinary(Assembly assembly, string resource)
        {
            byte[] binaryFile;

            using (Stream resourceStream = assembly.GetManifestResourceStream(resource))
            {
                if (resourceStream == null)
                {
                    throw KrakenException.Create("Resource '{0}' was expected in assembly '{1}' but was not found.", resource, assembly.Location);
                }

                int streamLength = Convert.ToInt32(resourceStream.Length);
                binaryFile = new byte[streamLength];
                resourceStream.Read(binaryFile, 0, streamLength);
            }

            return(binaryFile);
        }
Exemple #7
0
        /// <summary>
        /// Extracts a resource from the given assembly and dumps it's contents into the specified file
        /// </summary>
        /// <remarks>
        /// Binary safe method so jpegs etc will work
        /// </remarks>
        public static void ExportToFile(Assembly assembly, string resource, string fileName)
        {
            Stream resourceStream = assembly.GetManifestResourceStream(resource);

            if (resourceStream == null)
            {
                throw KrakenException.Create("Resource '{0}' was expected in assembly '{1}' but was not found.", resource, assembly.Location);
            }

            var fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
            var bw = new BinaryWriter(fs);
            var br = new BinaryReader(resourceStream);

            bw.Write(br.ReadBytes((int)resourceStream.Length));

            bw.Close();
            br.Close();
            fs.Close();
        }
Exemple #8
0
        /// <summary>
        /// Converts a timespan to a human readable format,
        /// eg. d days, h hours, m mins, s secs.
        ///
        /// Based off some potty mouth amateur web monkey
        /// http://www.codekeep.net/snippets/dc060497-9e0c-4a60-b1ed-aff6127fb80b.aspx
        /// </summary>
        /// <returns>Human readable time duration.</returns>
        public static string ToHumanReadable(this TimeSpan timeSpan, HumanReadableTimeSpanOptions options)
        {
            if (timeSpan.Equals(TimeSpan.MaxValue))
            {
                return("TimeSpan.Max");
            }
            if (timeSpan.Equals(TimeSpan.MinValue))
            {
                return("TimeSpan.Min");
            }
            decimal seconds = Convert.ToDecimal(timeSpan.TotalSeconds);
            var     labels  = new HumanReadableTimeLabels();

            switch (options.LabelMode)
            {
            case HumanReadableLabelMode.Abbreviated:
                labels.Day         = "day";
                labels.Hour        = "hr";
                labels.Minute      = "min";
                labels.Second      = "sec";
                labels.Millisecond = "ms";
                break;

            case HumanReadableLabelMode.Verbose:
                labels.Day         = "day";
                labels.Hour        = "hour";
                labels.Minute      = "minute";
                labels.Second      = "second";
                labels.Millisecond = "millisecond";
                break;

            case HumanReadableLabelMode.Custom:
                Guard.Null(options.CustomLabels, "CustomLabels property required");
                labels = options.CustomLabels;
                break;

            default:
                throw KrakenException.Create("A new label mode?");
            }

            if (seconds == 0)
            {
                return($"0 {labels.Second}s");
            }

            StringBuilder sb = new StringBuilder();

            if (seconds >= 86400)
            {
                sb.AppendFormat("{0} {2}{1}"
                                , (int)seconds / 86400
                                , seconds >= 86400 * 2 ? "s" : string.Empty
                                , labels.Day);

                seconds -= (int)(seconds / 86400) * 86400;
            }
            if (seconds >= 3600)
            {
                if (sb.Length > 0)
                {
                    sb.Append(", ");
                }
                sb.AppendFormat(
                    "{0} {1}{2}"
                    , (int)seconds / 3600
                    , labels.Hour
                    , seconds >= 3600 * 2 ? "s" : string.Empty
                    );
                seconds -= (int)(seconds / 3600) * 3600;
            }
            if (seconds >= 60)
            {
                if (sb.Length > 0)
                {
                    sb.Append(", ");
                }
                sb.AppendFormat(
                    "{0} {1}{2}"
                    , (int)seconds / 60
                    , labels.Minute
                    , seconds >= 60 * 2 ? "s" : string.Empty
                    );
                seconds -= (int)(seconds / 60) * 60;
            }
            if (seconds > 0)
            {
                if (sb.Length > 0)
                {
                    sb.AppendFormat(
                        ", {0} {1}{2}"
                        , (int)seconds
                        , labels.Second
                        , seconds == 1 ? string.Empty : "s");
                }
                else
                {
                    if (seconds == (int)seconds)
                    {
                        sb.AppendFormat("{0} {1}s", (int)seconds, labels.Second);
                    }
                    else if (seconds > decimal.One)
                    {
                        sb.AppendFormat("{0} {1}s", seconds.ToString("N2"), labels.Second);
                    }
                    else if (timeSpan.TotalMilliseconds >= 1)
                    {
                        var plural = labels.Millisecond == "ms" ? string.Empty : "s";
                        sb.AppendFormat("{0} {1}{2}", timeSpan.TotalMilliseconds.ToString("N0"), labels.Millisecond, plural);
                    }
                    else
                    {
                        var plural = Convert.ToDecimal(timeSpan.TotalMilliseconds) == Decimal.One ? "" : "s";
                        sb.AppendFormat("{0} {1}{2}", timeSpan.TotalMilliseconds.ToString("N2"), labels.Millisecond, plural);
                    }
                }
            }

            return(sb.ToString());
        }