/// <summary>
        /// Modifies a given QueryCriteria to exclude inactive items
        /// </summary>
        /// <typeparam name="T">Type of object to return</typeparam>
        /// <param name="criteria">QueryCriteria to adjust</param>
        /// <returns></returns>
        private static QueryCriteria ExcludeInactiveItems <T>(QueryCriteria criteria) where T : TypeProjection
        {
            PropertyPathHelper pathHelper = new PropertyPathHelper();

            pathHelper.PropertyName = "ObjectStatus";
            pathHelper.ObjectClass  = ClassConstants.GetClassIdByType <T>();

            QueryCriteriaExpression activeItemsOnly = new QueryCriteriaExpression
            {
                PropertyName = pathHelper.ToString(),
                PropertyType = QueryCriteriaPropertyType.Property,
                Operator     = QueryCriteriaExpressionOperator.Equal,
                Value        = EnumerationConstants.ConfigItem.BuiltinValues.ObjectStatus.Active.ToString("D")
            };

            QueryCriteria newCriteria = criteria;

            if (newCriteria.Expressions.Count > 0)
            {
                newCriteria.GroupingOperator = QueryCriteriaGroupingOperator.And;
            }
            else
            {
                newCriteria.GroupingOperator = QueryCriteriaGroupingOperator.SimpleExpression;
            }

            newCriteria.Expressions.Add(activeItemsOnly);

            return(newCriteria);
        }
        /// <summary>
        /// Convenience method to retrieve an object derived from the TypeProjection class, using its FullName
        /// </summary>
        /// <typeparam name="T">TypeProjection derived type</typeparam>
        /// <param name="authToken">AuthorizationToken to use</param>
        /// <param name="name">FullName to find</param>
        /// <returns></returns>
        internal static async Task <T> GetByFullName <T>(AuthorizationToken authToken, string name) where T : TypeProjection
        {
            QueryCriteriaExpression expr = new QueryCriteriaExpression
            {
                PropertyName = "FullName",
                PropertyType = QueryCriteriaPropertyType.GenericProperty,
                Operator     = QueryCriteriaExpressionOperator.Equal,
                Value        = ClassConstants.GetClassNameByType <T>() + ":" + name
            };

            QueryCriteria criteria = new QueryCriteria(TypeProjectionConstants.GetProjectionIdByType <T>())
            {
                GroupingOperator = QueryCriteriaGroupingOperator.SimpleExpression
            };

            criteria.Expressions.Add(expr);

            List <T> retList = await GetByCriteria <T>(authToken, criteria);

            if (retList.Count == 0)
            {
                return(null);
            }
            else if (retList.Count == 1)
            {
                return(retList[0]);
            }
            else
            {
                throw new CiresonApiException("More than one item found with identical full name");
            }
        }
        /// <summary>
        /// Creates a new TypeProjection of derived type T and returns it
        /// </summary>
        /// <typeparam name="T">TypeProjection derived type</typeparam>
        /// <param name="authToken">AuthorizationToken to use</param>
        /// <returns></returns>
        internal static async Task <T> CreateBlankObject <T>(AuthorizationToken authToken, string name, string displayName, dynamic objProps = null) where T : TypeProjection
        {
            // See if we have a CI matching this name first
            T item = await GetByFullName <T>(authToken, name);

            if (item != null)
            {
                string fullName = ClassConstants.GetClassNameByType <T>() + ":" + name;
                throw new CiresonDuplicateItemException("An object by the name " + fullName + " already exists.");
            }

            // Setup the CI
            dynamic ci = new ExpandoObject();

            ci.formJson          = new ExpandoObject();
            ci.formJson.isDirty  = true;
            ci.formJson.original = null;

            ci.formJson.current             = new ExpandoObject();
            ci.formJson.current.BaseId      = null;
            ci.formJson.current.ClassTypeId = ClassConstants.GetClassIdByType <T>();
            ci.formJson.current.ClassName   = ClassConstants.GetClassNameByType <T>();
            ci.formJson.current.Name        = name;
            ci.formJson.current.DisplayName = displayName;
            ci.formJson.current.TimeAdded   = "0001-01-01T00:00:00";

            //ci.formJson.current.ObjectStatus = new ExpandoObject();
            //ci.formJson.current.ObjectStatus.Id = EnumerationConstants.TypeProjection.BuiltinValues.ObjectStatus.Active;

            // Merge another property object in
            if (objProps != null)
            {
                IDictionary <string, object> ciDict = (IDictionary <string, object>)ci.formJson.current;

                foreach (var property in objProps.GetType().GetProperties())
                {
                    if (property.CanRead)
                    {
                        ciDict[property.Name] = property.GetValue(objProps);
                    }
                }
            }

            // Create the new TypeProjection, then return the full-property object
            T newCI = await CreateObjectFromData <T>(authToken, ci);

            return(await GetByBaseId <T>(authToken, newCI.BaseId));
        }
Exemple #4
0
        /// <summary>
        /// Converts raw related object data into a TypeProjection-derived object
        /// </summary>
        /// <param name="objData">Object data</param>
        /// <returns></returns>
        private T ExpandoToProjection(ExpandoObject objData)
        {
            string className      = (objData as dynamic).ClassName;
            Type   projectionType = ClassConstants.GetTypeByClassName(className);

            // Instantiate
            BindingFlags flags      = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
            CultureInfo  culture    = null;
            T            projection = (T)Activator.CreateInstance(projectionType, flags, null, null, culture);

            projection.CurrentObject  = objData.DeepCopy();
            projection.OriginalObject = objData;
            projection.ReadOnly       = true;
            projection.IsDirty        = false;

            return(projection);
        }