コード例 #1
0
        public void GetMyPersonalization_Get_ReturnsXml()
        {
            // Arrange
            ShimUtils.ValidateItemListWebAndSiteXElement = _ => { };

            // Act
            var result = MyPersonalization.GetMyPersonalization(GetXmlData());

            // Assert
            this.ShouldSatisfyAllConditions(
                () => result.ShouldNotBeNull(),
                () => result.ShouldContainWithoutWhitespace("<MyPersonalization>"),
                () => result.ShouldContainWithoutWhitespace($"<Personalizations Username=\"{DummyResult}\" ItemID=\"{One}\" ListID=\"{_listId}\" WebID=\"{_webId}\" SiteID=\"{_siteId}\" SiteURL=\"{ExampleUrl}\""),
                () => result.ShouldContainWithoutWhitespace($"<Personalization ID=\"{One}\" Key=\"{DummyString}\" Value=\"0\" />"),
                () => result.ShouldContainWithoutWhitespace("</Personalizations>"),
                () => result.ShouldContainWithoutWhitespace("</MyPersonalization>"));
        }
コード例 #2
0
        public void GetMyPersonalization_ValidateException_Throws()
        {
            // Arrange
            var data = GetXmlData();

            ShimUtils.ValidateItemListWebAndSiteXElement = _ => { throw new InvalidOperationException(DummyString); };

            // Act
            var action = new Action(() => MyPersonalization.GetMyPersonalization(data));

            // Assert
            var exception = action.ShouldThrow <APIException>();

            this.ShouldSatisfyAllConditions(
                () => exception.ExceptionNumber.ShouldBe(5005),
                () => exception.Message.ShouldBe(DummyString));
        }
コード例 #3
0
        /// <summary>
        /// Gets the data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public string GetData(string data)
        {
            try
            {
                string           response;
                NumberFormatInfo providerEn = new System.Globalization.NumberFormatInfo();
                providerEn.NumberDecimalSeparator = ".";
                providerEn.NumberGroupSeparator   = ",";
                providerEn.NumberGroupSizes       = new int[] { 3 };

                XElement dataRootElement = XDocument.Parse(data).Root;

                if (dataRootElement == null)
                {
                    throw new APIException((int)Errors.GetDataRootElementNotFound,
                                           "Cannot find the Root AssignmentPlanner element.");
                }

                using (var workEngineAPI = new WorkEngineAPI())
                {
                    response = workEngineAPI.Execute("Reporting_GetMyWorkData",
                                                     new XElement("GetMyWorkReportingData",
                                                                  dataRootElement.Element("Params")).ToString());
                }

                XDocument myWorkDataXml = XDocument.Parse(response);

                ValidateMyWorkDataResponse(myWorkDataXml);

                var gridSafeFields = new Dictionary <string, string>();

                var bElement         = new XElement("B");
                var resourcesElement = new XElement("Resources");
                var footElement      = new XElement("Foot");

                var resources = new Dictionary <int, string>();

                SPRegionalSettings spRegionalSettings = _spWeb.CurrentUser.RegionalSettings ??
                                                        _spWeb.Site.RootWeb.RegionalSettings;

                var workDayStartHour = (short)(spRegionalSettings.WorkDayStartHour / 60);
                var workDayEndHour   = (short)(spRegionalSettings.WorkDayEndHour / 60);

                string siteUrl = _spWeb.Site.Url;
                string safeServerRelativeUrl = _spWeb.SafeServerRelativeUrl();

                foreach (
                    XElement resourceElement in
                    myWorkDataXml.Element("Result").Element("GetMyWorkData").Element("Data").Elements("Resource"))
                {
                    XElement resultElement = resourceElement.Element("Result");

                    if (!resultElement.Attribute("Status").Value.Equals("0"))
                    {
                        continue;
                    }

                    int    resourceId   = 0;
                    string resourceName = string.Empty;

                    foreach (XElement itemElement in resourceElement.Element("Data").Elements("Item"))
                    {
                        var iElement = new XElement("I");

                        int    itemId = 0;
                        Guid   listId = Guid.Empty;
                        Guid   webId  = Guid.Empty;
                        Guid   siteId = Guid.Empty;
                        string value  = string.Empty;
                        float  floatFieldValue;

                        foreach (XElement fieldElement in itemElement.Elements("Field"))
                        {
                            string field = fieldElement.Attribute("Name").Value;

                            if (!gridSafeFields.ContainsKey(field))
                            {
                                gridSafeFields.Add(field, Utils.ToGridSafeFieldName(field));
                            }

                            if (float.TryParse(fieldElement.Value.ToString(), out floatFieldValue))
                            {
                                value = floatFieldValue.ToString(providerEn);
                            }
                            else
                            {
                                value = fieldElement.Value;
                            }

                            string fieldName = field.ToLower();

                            if (fieldName.Equals("id"))
                            {
                                itemId = Convert.ToInt32(value);
                            }
                            else if (fieldName.Equals("listid"))
                            {
                                listId = new Guid(value);
                            }
                            else if (fieldName.Equals("webid"))
                            {
                                webId = new Guid(value);
                            }
                            else if (fieldName.Equals("siteid"))
                            {
                                siteId = new Guid(value);
                            }
                            else if (fieldName.Equals("assignedtoid"))
                            {
                                resourceId = Convert.ToInt32(value);
                            }
                            else if (fieldName.Equals("assignedtotext"))
                            {
                                resourceName = value;
                            }
                            else if (fieldElement.Attribute("Type").Value.Equals("System.DateTime") &&
                                     !string.IsNullOrEmpty(value))
                            {
                                DateTime localTime = SPUtility.CreateDateTimeFromISO8601DateTimeString(value);

                                if (fieldName.Equals("startdate") &&
                                    (localTime.Hour < workDayStartHour || localTime.Hour > workDayEndHour))
                                {
                                    localTime = new DateTime(localTime.Year, localTime.Month, localTime.Day,
                                                             workDayStartHour, 0, 0);
                                }
                                else if (fieldName.Equals("duedate") &&
                                         (localTime.Hour < workDayStartHour || localTime.Hour > workDayEndHour))
                                {
                                    localTime = new DateTime(localTime.Year, localTime.Month, localTime.Day,
                                                             workDayEndHour, 0, 0);
                                }

                                value = SPUtility.CreateISO8601DateTimeFromSystemDateTime(localTime);
                            }

                            iElement.Add(new XAttribute(gridSafeFields[field], value));
                        }

                        string flagQuery =
                            string.Format(
                                @"<MyPersonalization>
                                        <Keys>AssignmentPlannerFlag</Keys>
                                        <Item ID=""{0}""/>
                                        <List ID=""{1}""/>
                                        <Web ID=""{2}""/>
                                        <Site ID=""{3}"" URL=""{4}""/>
                                </MyPersonalization>",
                                itemId, listId, webId, siteId, siteUrl);

                        XDocument flagResponse = XDocument.Parse(MyPersonalization.GetMyPersonalization(flagQuery));

                        string flag = null;

                        XElement personalizationRootElement = flagResponse.Element("MyPersonalization");

                        if (personalizationRootElement != null)
                        {
                            flag = (from e in personalizationRootElement.Descendants("Personalization")
                                    let keyAttribute = e.Attribute("Key")
                                                       where keyAttribute != null && keyAttribute.Value.Equals("AssignmentPlannerFlag")
                                                       let valueAttribute = e.Attribute("Value")
                                                                            where valueAttribute != null
                                                                            select valueAttribute.Value).FirstOrDefault();
                        }

                        var flagValue = flag ?? "0";
                        var flagUrl   = safeServerRelativeUrl + "/_layouts/epmlive/images/mywork/flagged.png";
                        if (flagValue.Equals("0"))
                        {
                            flagUrl = safeServerRelativeUrl + "/_layouts/epmlive/images/mywork/unflagged.png";
                        }

                        iElement.Add(new XAttribute("Duration", string.Empty),
                                     new XAttribute("Flag", string.Format(@"<img src=""{0}"" class=""AP_Flag""/>", flagUrl)),
                                     new XAttribute("FlagValue", flagValue), new XAttribute("Height", 23));

                        if (resourceId == 0 || string.IsNullOrEmpty(resourceName))
                        {
                            continue;
                        }

                        bElement.Add(iElement);
                        if (!resources.ContainsKey(resourceId))
                        {
                            resources.Add(resourceId, resourceName);
                        }
                    }
                }

                foreach (var keyValuePair in resources)
                {
                    resourcesElement.Add(new XElement("R", new XAttribute("Name", keyValuePair.Value),
                                                      new XAttribute("Availability", 8), new XAttribute("Type", 1)));

                    footElement.Add(new XElement("I", new XAttribute("id", string.Format("-{0}", keyValuePair.Key)),
                                                 new XAttribute("Def", "Resource"),
                                                 new XAttribute("Title", keyValuePair.Value)));
                }

                return(new XElement("Grid", new XElement("Body", bElement), resourcesElement, footElement).ToString());
            }
            catch (APIException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new APIException((int)Errors.GetData, e.Message);
            }
        }