Пример #1
0
        /// <summary>
        /// Extract the iteration containment id from the supplied uri.
        /// </summary>
        /// <param name="uri">
        /// The uri route of the iteration contained resource.
        /// </param>
        /// <param name="iterationId">
        /// The unique identifier of the Iteration as out parameter.
        /// </param>
        /// <returns>
        /// true if iteration id was extracted from the uri, otherwise false
        /// </returns>
        public bool TryExtractIterationIdfromUri(Uri uri, out Guid iterationId)
        {
            try
            {
                var uriString        = uri.ToString();
                var iterationUriName = ContainerPropertyHelper.ContainerPropertyName(ClassKind.Iteration);
                var regexPatter      = iterationUriName + Constants.UriPathSeparator + Constants.UriGuidPattern;

                var match = Regex.Match(uriString, regexPatter);

                if (!match.Success)
                {
                    iterationId = Guid.Empty;
                    return(false);
                }

                var id = match.Value.Split(Constants.UriPathSeparatorChar).Last();
                iterationId = new Guid(id);
                return(true);
            }
            catch (Exception ex)
            {
                Logger.Warn(ex, "The Iteration identifier could not be exracted from {0}", uri);

                iterationId = Guid.Empty;
                return(false);
            }
        }
        /// <summary>
        /// Assert that the new added container is consistent with the container previously added
        /// </summary>
        /// <param name="lastRoute">container previously added</param>
        /// <param name="newRoute">new container</param>
        /// <returns>true if the new route is consistent with the existing one</returns>
        private bool IsAuthorizedRoute(ClassKind lastRoute, ClassKind newRoute)
        {
            var lastRouteContainerClass = ContainerPropertyHelper.ContainerClassName(lastRoute);

            if (newRoute.ToString() == lastRouteContainerClass)
            {
                return(true);
            }

            // the newRoute may not correspond to the container class because the container class is abstract.
            // Check if the parent of the added container is that abstract class
            var type = Type.GetType("CDP4Common.DTO." + newRoute);

            if (type != null)
            {
                var parent = type.QueryBaseType();
                while (parent != null)
                {
                    if (parent.Name == lastRouteContainerClass)
                    {
                        return(true);
                    }

                    parent = parent.QueryBaseType();
                }
            }

            return(false);
        }
        /// <summary>
        /// Adds a container to the current <see cref="Thing"/>. The containers should be added following the containment
        /// hierarchy starting at the current <see cref="Thing"/>. The last addition shall be either a <see cref="EngineeringModel"/>
        /// or a <see cref="SiteDirectory"/>
        /// </summary>
        /// <param name="classKind">
        /// A <see cref="ClassKind"/> specifying the type of the container that is being added
        /// </param>
        /// <param name="iid">
        /// the unique id of the container that is being added
        /// </param>
        public virtual void AddContainer(ClassKind classKind, Guid iid)
        {
            var lastRouteClassKind = this.partialClassKindRoute.Any()
                ? this.partialClassKindRoute.Last()
                : this.ClassKind;

            switch (lastRouteClassKind)
            {
            case ClassKind.SiteDirectory:
                throw new InvalidOperationException("Cannot add another container, SiteDirectory is a top container");

            case ClassKind.EngineeringModel:
                throw new InvalidOperationException("Cannot add another container, EngineeringModel is a top container");

            default:
            {
                if (this.IsAuthorizedRoute(lastRouteClassKind, classKind))
                {
                    var containerPropertyName = ContainerPropertyHelper.ContainerPropertyName(classKind);
                    var partialRoute          = string.Format("{0}/{1}", containerPropertyName, iid);
                    this.PartialRoutes.Add(partialRoute);
                    this.partialClassKindRoute.Add(classKind);
                    break;
                }

                throw new InvalidOperationException(string.Format("the added container of classKind: {0} is not consistent with the existing route", classKind));
            }
            }
        }
        /// <summary>
        /// Returns the computed route of the current <see cref="Thing"/>
        /// </summary>
        /// <returns>
        /// a string that represents the route
        /// </returns>
        protected string ComputedRoute()
        {
            var temporaryList = new List <string>(this.PartialRoutes);

            temporaryList.Reverse();
            var containerRoute = string.Join("/", temporaryList);

            var containerPropertyName = ContainerPropertyHelper.ContainerPropertyName(this.ClassKind);
            var partialRoute          = string.Format("{0}/{1}", containerPropertyName, this.Iid);

            if (string.IsNullOrEmpty(containerRoute))
            {
                return("/" + partialRoute);
            }

            return(string.Format("/{0}/{1}", containerRoute, partialRoute));
        }