Exemplo n.º 1
0
        bool InstantiateSchool(dynamic dataDict, Tuple<string, string> when_id)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            mLogger.Log(methodName, "", 3);

            bool success = true;

            School school = new School();
            SchoolLocation loc = new SchoolLocation();
            Principal principal = new Principal();

            // Get each dp (datapoint) found in dataDict
            foreach (KeyValuePair<string, dynamic> dpParent in dataDict)
            {
                if (dpParent.Value != null)
                {
                    Type valueType = dpParent.Value.GetType();
                    if (valueType.IsGenericType)
                    {
                        // This value item is not just another string value but (probably) another dictionary
                        Type baseType = valueType.GetGenericTypeDefinition();
                        if (baseType == typeof(Dictionary<,>))
                        {
                            mLogger.Log(methodName, string.Format("Getting data for Key: {0}", dpParent.Key), 3);
                            foreach (KeyValuePair<string, dynamic> dpChild in dpParent.Value)
                            {
                                mLogger.Log(methodName, string.Format("Key: {0}     Value: {1}", dpChild.Key, dpChild.Value), 3);

                                if (dpParent.Key == "location")
                                {
                                    PropertyInfo pi = loc.GetType().GetProperty(dpChild.Key);
                                    ValidateClassProperty(pi, dpChild.Key, "SchoolLocation");
                                    pi.SetValue(loc, Convert.ChangeType(dpChild.Value, pi.PropertyType), null);
                                }
                                else if (dpParent.Key == "principal")
                                {
                                    PropertyInfo pi = principal.GetType().GetProperty(dpChild.Key);
                                    ValidateClassProperty(pi, dpChild.Key, "Principal");
                                    pi.SetValue(principal, Convert.ChangeType(dpChild.Value, pi.PropertyType), null);
                                }
                            }
                        }
                    }
                    else if (valueType.Name == "String")
                    {
                        mLogger.Log(methodName, string.Format("Key: {0}     Value: {1}", dpParent.Key, dpParent.Value), 3);

                        PropertyInfo pi = school.GetType().GetProperty(dpParent.Key);
                        ValidateClassProperty(pi, dpParent.Key, "School");
                        pi.SetValue(school, Convert.ChangeType(dpParent.Value, pi.PropertyType), null);
                    }
                }
            }
            //  check whether we are instantiating for event data and if so, set event_type and id
            if (when_id.Item1 == "current_attributes")
            {
                school.event_type = when_id.Item1;
            }
            else if (when_id.Item1 == "previous_attributes")
            {
                school.event_type = when_id.Item1;
                school.id = when_id.Item2;
            }

            school.location = loc;
            school.principal = principal;
            mWrappedData.Schools.Add(school);

            return success;
        }
Exemplo n.º 2
0
        bool InstantiateSchool(dynamic dataDict, Tuple <string, string> when_id)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            mLogger.Log(methodName, "", 3);

            bool success = true;

            School         school    = new School();
            SchoolLocation loc       = new SchoolLocation();
            Principal      principal = new Principal();

            // Get each dp (datapoint) found in dataDict
            foreach (KeyValuePair <string, dynamic> dpParent in dataDict)
            {
                if (dpParent.Value != null)
                {
                    Type valueType = dpParent.Value.GetType();
                    if (valueType.IsGenericType)
                    {
                        // This value item is not just another string value but (probably) another dictionary
                        Type baseType = valueType.GetGenericTypeDefinition();
                        if (baseType == typeof(Dictionary <,>))
                        {
                            mLogger.Log(methodName, string.Format("Getting data for Key: {0}", dpParent.Key), 3);
                            foreach (KeyValuePair <string, dynamic> dpChild in dpParent.Value)
                            {
                                mLogger.Log(methodName, string.Format("Key: {0}     Value: {1}", dpChild.Key, dpChild.Value), 3);

                                if (dpParent.Key == "location")
                                {
                                    PropertyInfo pi = loc.GetType().GetProperty(dpChild.Key);
                                    ValidateClassProperty(pi, dpChild.Key, "SchoolLocation");
                                    pi.SetValue(loc, Convert.ChangeType(dpChild.Value, pi.PropertyType), null);
                                }
                                else if (dpParent.Key == "principal")
                                {
                                    PropertyInfo pi = principal.GetType().GetProperty(dpChild.Key);
                                    ValidateClassProperty(pi, dpChild.Key, "Principal");
                                    pi.SetValue(principal, Convert.ChangeType(dpChild.Value, pi.PropertyType), null);
                                }
                            }
                        }
                    }
                    else if (valueType.Name == "String")
                    {
                        mLogger.Log(methodName, string.Format("Key: {0}     Value: {1}", dpParent.Key, dpParent.Value), 3);

                        PropertyInfo pi = school.GetType().GetProperty(dpParent.Key);
                        ValidateClassProperty(pi, dpParent.Key, "School");
                        pi.SetValue(school, Convert.ChangeType(dpParent.Value, pi.PropertyType), null);
                    }
                }
            }
            //  check whether we are instantiating for event data and if so, set event_type and id
            if (when_id.Item1 == "current_attributes")
            {
                school.event_type = when_id.Item1;
            }
            else if (when_id.Item1 == "previous_attributes")
            {
                school.event_type = when_id.Item1;
                school.id         = when_id.Item2;
            }

            school.location  = loc;
            school.principal = principal;
            mWrappedData.Schools.Add(school);

            return(success);
        }