Example #1
0
        public static List <TimesheetGroupResult> GetUserMissingTimesheets(User user, DateTime fromDate, DateTime toDate)
        {
            List <TimesheetGroupResult> items = new List <TimesheetGroupResult>();
            var groupBy      = new string[] { "ReportedDate" };
            var orderBy      = new orderBy[] { new orderBy("ReportedDate", "Ascending"), };
            var aggregations = new fieldAggregation[]
            {
                new fieldAggregation("Sum", "C_DurationHours", "TotalTime")
            };

            var condition = new cZQLCondition($@" ReportedBy = ""{user.id}"" AND ReportedDate >= ""{fromDate:yyyy-MM-dd}"" AND ReportedDate <= ""{toDate:yyyy-MM-dd}"" ");
            var paging    = new paging()
            {
                from    = 0,
                limit   = Int32.MaxValue,
                hasMore = true
            };

            var query  = new Ekin.Clarizen.Data.Queries.aggregateQuery("Timesheet", groupBy, orderBy, condition, aggregations, paging);
            var result = ClarizenAPI.AggregateQuery(query);

            if (result.IsCalledSuccessfully)
            {
                items = result.Data.entities.Select(i => Newtonsoft.Json.JsonConvert.DeserializeObject <TimesheetGroupResult>(i.ToString())).ToList();
            }

            return(items);
        }
        public List <T> LoadAll(string customCondition = "", bool readFromFile = false, bool useCache = false, int cacheDuration = 60)
        {
            string entityName = typeof(T).Name;
            var    items      = new List <T>();
            var    file       = GetFilePath(entityName);

            if (readFromFile && string.IsNullOrEmpty(customCondition))
            {
                var fileInfo = new FileInfo(file);
                if (fileInfo.Exists)
                {
                    items = FileHelper.ReadFromJsonFile <List <T> >(file);
                    return(items);
                }
            }

            var cacheKey = $"LoadAllByType:{entityName}:{GetEnvironmentSuffix()}:{customCondition}";

            if (useCache && _cacheManager != null)
            {
                items = _cacheManager.Get <List <T> >(cacheKey);

                if (items != null)
                {
                    return(items);
                }
            }

            cZQLCondition condition = null;

            if (!string.IsNullOrEmpty(customCondition))
            {
                condition = new cZQLCondition(customCondition);
            }

            int currentRetry = 1;

            do
            {
                try
                {
                    GetAllResult result = _api.GetAll(entityName, typeof(T), condition);

                    if (result.Data == null || result.Errors.Count > 0)
                    {
                        items = null;

                        if (result != null && result.Errors.Any())
                        {
                            var msg         = "Error occured on Clarizen API call. Retry count: " + currentRetry;
                            var detailedMsg = "Error: " + string.Join(System.Environment.NewLine, result.Errors.Select(i => i.message));
                            throw new Exception(msg, new Exception(detailedMsg));
                        }
                    }
                    else
                    {
                        items = (List <T>)result.Data;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    var msg = ExceptionHelper.GetExceptionErrorMessage(ex, "Exception in Clarizen Data LoadAllByType");
                    Debug.WriteLine(msg);

                    currentRetry++;

                    // Check if the exception thrown was a transient/timeout exception
                    // based on the logic in the error detection strategy.
                    // Determine whether to retry the operation, as well as how
                    // long to wait, based on the retry strategy.
                    if (currentRetry > API_RETRY_COUNT || !ex.Message.Contains("timed out"))
                    {
                        // If this isn't a transient error or we shouldn't retry,
                        // rethrow the exception.
                        items = new List <T>();
                    }
                }

                // Wait to retry the operation.
                // Consider calculating an exponential delay here and
                // using a strategy best suited for the operation and fault.
                Thread.Sleep(API_DELAY);
            } while (currentRetry <= API_RETRY_COUNT);

            if (useCache && _cacheManager != null)
            {
                _cacheManager.Set(cacheKey, items, cacheDuration);
            }

            if (readFromFile)
            {
                FileHelper.WriteToJsonFile(file, items);
            }

            return(items);
        }