/// <summary>
 ///     The filters have to be applied in an specific component, property
 ///     or param of a property. This method go deep in the calCOmponents following the
 ///     treeStructure of the filter till it get the component where to apply the filter.
 /// </summary>
 /// <param name="container">The container of the components.</param>
 /// <param name="treeStructure">The IXMLTree where is the filter.</param>
 /// <param name="filter">Return The XmlTreeStructure node that contains the filter.</param>
 /// <param name="component">The final component where to apply the filter.</param>
 /// <returns>True if found the component, false otherwise.</returns>
 public static bool ComponentSeeker(this ICalendarComponentsContainer container,
                                    IXMLTreeStructure treeStructure, out IXMLTreeStructure filter, out ICalendarComponent component)
 {
     filter    = null;
     component = null;
     while (true)
     {
         ICalendarComponent comp     = null;
         IXMLTreeStructure  compNode = null;
         treeStructure.GetChildAtAnyLevel("comp-filter", out compNode);
         if (compNode == null)
         {
             //if the filter doesn't has a child with comp-filter name
             //and the name of the current container is the same
             if (((ICalendarObject)container).Name == treeStructure.Attributes["name"])
             {
                 filter    = treeStructure;
                 component = container as ICalendarComponent;
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
         var compName = compNode.Attributes["name"];
         //if the container doesn't has a calComponent with the desired compName
         //then return false
         if (!container.CalendarComponents.ContainsKey(compName))
         {
             return(false);
         }
         //take the comp with the desired name
         comp = container.CalendarComponents[compName].First();
         var componentsContainer = comp as ICalendarComponentsContainer;
         //if the the filter has more components and the container has more calendarComp
         //then go deeper
         if (componentsContainer != null && compNode.Children.Any(x => x.NodeName == "comp-filter"))
         {
             container     = componentsContainer;
             treeStructure = compNode;
             continue;
         }
         //if not then apply the filter in the comp
         component = comp;
         filter    = compNode;
         return(true);
     }
 }
Beispiel #2
0
        /// <summary>
        ///     Method in charge of fill a CalendarResource and Return it.
        /// </summary>
        /// <param name="propertiesAndHeaders"></param>
        /// <param name="iCal"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        private async Task <CalendarResource> FillResource(Dictionary <string, string> propertiesAndHeaders,
                                                           ICalendarComponentsContainer iCal,
                                                           HttpResponse response)
        {
            #region Extracting Properties

            string calendarResourceId;
            propertiesAndHeaders.TryGetValue("calendarResourceId", out calendarResourceId);

            string url;
            propertiesAndHeaders.TryGetValue("url", out url);

            string principalId;
            propertiesAndHeaders.TryGetValue("principalId", out principalId);


            //var headers = response.GetTypedHeaders();

            #endregion

            // calculate etag that will notice a change in the resource
            var etag = $"\"{Guid.NewGuid()}\"";
            response.Headers["etag"] = etag;

            var resource = new CalendarResource(url, calendarResourceId);

            //add the owner property
            var principal    = _principalRepository.GetByIdentifier(principalId);
            var principalUrl = principal == null ? "" : principal.PrincipalURL;

            resource.Properties.Add(PropertyCreation.CreateOwner(principalUrl));
            resource.Properties.Add(PropertyCreation.CreateAclPropertyForUserCollections(principalUrl));
            resource.Properties.Add(PropertyCreation.CreateSupportedPrivilegeSetForResources());

            // await _resourceRespository.Add(resource);

            //adding the calculated etag in the getetag property of the resource
            var errorStack = new Stack <string>();
            await
            _resourceRespository.CreatePropertyForResource(resource, "getetag", "DAV:",
                                                           $"<D:getetag {_namespaces["D"]}>{etag}</D:getetag>",
                                                           errorStack, true);

            //updating the ctag of the collection noticing this way that the collection has changed.
            var stack = new Stack <string>();
            await
            _collectionRespository.CreateOrModifyProperty(
                url?.Remove(url.LastIndexOf("/", StringComparison.Ordinal) + 1), "getctag", _namespacesSimple["S"],
                $@"<S:getctag {_namespaces["S"]} >{Guid.NewGuid()}</S:getctag>", stack, true);

            //getting the uid
            var calendarComponents =
                iCal.CalendarComponents.FirstOrDefault(comp => comp.Key != "VTIMEZONE").Value;
            var calendarComponent = calendarComponents.FirstOrDefault();
            if (calendarComponent != null)
            {
                resource.Uid = calendarComponent.Properties["UID"].StringValue;
            }

            return(resource);
        }