コード例 #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            List <DirectoryItem> directoryItems = new List <DirectoryItem>();
            DirectoryItem        directoryItem  = null;

            // Get the list of properties and methods that have been marked with the ADIdentifier attribute.
            Dictionary <string, string> objProps   = DirectoryItemServices.GetDirectoryItemAS400Properties();
            Dictionary <string, string> objMethods = DirectoryItemServices.GetDirectoryItemAS400Methods();
            string propertyName;
            string methodName;

            /* Conversions aren't done like "rows" and "fields" in a data table.  What we actually get are markers
             * (TokenTypes) telling us what this particular piece of information is.  So we need to loop through it
             * all and watch the TokenTypes to know if this is the equivalent of a new "row" or a "field" in the
             * current row.*/
            while (reader.Read())
            {
                // Loop through until we're at new "row" (StartObject) or an AD field name (PropertyName) or
                // we know we've gotten all the properties of the current "row" (EndObject).
                switch (reader.TokenType)
                {
                case JsonToken.StartObject:     // This is a new "row".
                    directoryItem = new DirectoryItem();
                    break;

                case JsonToken.PropertyName:     // This is a "field".
                    // Read in the field name.
                    string fieldIdentifier = reader.Value.ToString().ToLower();

                    // Read in the property value.  It will be next in the reader.
                    if (reader.Read())
                    {
                        // Double check this is a field we actually care about and get the name of the field it maps to.
                        if (objProps.TryGetValue(fieldIdentifier, out propertyName))      // Look for propery.
                        {
                            // Get the field's type and convert and store the value.
                            PropertyInfo pi             = directoryItem.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                            var          convertedValue = Convert.ChangeType(reader.Value, pi.PropertyType);
                            pi.SetValue(directoryItem, convertedValue, null);
                        }
                        else if (objMethods.TryGetValue(fieldIdentifier, out methodName))      // Look for method.
                        {
                            // Get the method and execute it.
                            MethodInfo mi             = directoryItem.GetType().GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                            var        convertedValue = Convert.ChangeType(reader.Value, mi.GetParameters()[0].ParameterType);
                            mi.Invoke(directoryItem, new object[] { convertedValue });
                        }
                    }
                    break;

                case JsonToken.EndObject:     // We've reached the end of the "row".
                    directoryItems.Add(directoryItem);
                    break;

                default:
                    break;
                }
            }
            return(directoryItems);
        }
コード例 #2
0
        /// <summary>
        /// Pulls the information from AS400 to be included in the directory. We grab all the attritubte names
        /// to map and build the list of fields for the PcTable function.
        /// </summary>
        /// <returns></returns>
        private static List <DirectoryItem> LoadPcInformation()
        {
            List <DirectoryItem> directoryItems = new List <DirectoryItem>();

            // Build a comman delimeted list of field names from both the properties and methosd marked in the class
            // with the AS400 attribute.
            string fieldsFromProperties = string.Join(",", DirectoryItemServices.GetDirectoryItemAS400Properties().Select(p => p.Key).ToArray());
            string fieldsFromMethods    = string.Join(",", DirectoryItemServices.GetDirectoryItemAS400Methods().Select(p => p.Key).ToArray());

            //  Get the data table from AS400.
            HajProfitCenter hajProfitCenter = new HajProfitCenter();
            DataTable       dt = hajProfitCenter.PcTable(
                fieldsFromProperties + ((fieldsFromProperties.Length > 0) & (fieldsFromMethods.Length > 0) ? "," : "") + fieldsFromMethods,
                HajProfitCenter.TableOrder.OrderByPc, HajProfitCenter.PcType.TypeDirectory, false, true, true
                );

            // Serialize the object to a string and then send it into our custom deserialization/mapping process.
            string output = JsonConvert.SerializeObject(dt, new DataSetConverter());

            directoryItems = JsonConvert.DeserializeObject <List <DirectoryItem> >(output, new AS400DirectoryItemConverter());

            // Add some specific property info for our newly created list.
            // todo: this needs to be redone to set Service Center on certain PCs and Region Staff on other PCs.  We are still waiting on the magic number that determines which is which.
            directoryItems.ForEach(di =>
            {
                di.IsProfitCenter  = true;
                di.IsRegionManager = true;
                di.IsServiceCenter = false;
                di.IsRegionStaff   = false;
            });

            return(directoryItems);
        }
コード例 #3
0
        /// <summary>
        /// Pulls the information from AS400 to be included in the directory. We grab all the attritubte names
        /// to map and build the list of fields for the PcTable function.
        /// </summary>
        /// <returns></returns>
        private static List <DirectoryItem> LoadPcInformation()
        {
            List <DirectoryItem> directoryItems = new List <DirectoryItem>();

            // Build a comman delimeted list of field names from both the properties and methosd marked in the class
            // with the AS400 attribute.
            string fieldsFromProperties = string.Join(",", DirectoryItemServices.GetDirectoryItemAS400Properties().Select(p => p.Key).ToArray());
            string fieldsFromMethods    = string.Join(",", DirectoryItemServices.GetDirectoryItemAS400Methods().Select(p => p.Key).ToArray());

            //  Get the data table from AS400.
            //HajProfitCenter hajProfitCenter = new HajProfitCenter();

            /*DataTable dt = hajProfitCenter.PcTable(
             *  fieldsFromProperties + ((fieldsFromProperties.Length > 0) & (fieldsFromMethods.Length > 0) ? "," : "") + fieldsFromMethods,
             *  HajProfitCenter.TableOrder.OrderByPc, HajProfitCenter.PcType.TypeDirectory, false, true, true
             * );*/
            DataTable dt = GetPcInfoWithCoords();


            // Serialize the object to a string and then send it into our custom deserialization/mapping process.
            string output = JsonConvert.SerializeObject(dt, new DataSetConverter());

            directoryItems = JsonConvert.DeserializeObject <List <DirectoryItem> >(output, new AS400DirectoryItemConverter());

            // Add some specific property info for our newly created list.
            directoryItems.ForEach(di =>
            {
                if ((di.ManagerID ?? "").Length > 0)
                {
                    foreach (SearchResult searchResult in
                             HajClassLib.ADInfo.SearchADInformation(new TupleList <string, string> {
                        { "ou", "people" }
                    }, new List <string>()
                    {
                        "sAMAccountName=" + di.ManagerID
                    }, new List <string>()
                    {
                        "mobile"
                    }))
                    {
                        if (searchResult.Properties.Contains("mobile"))
                        {
                            di.Mobile = (string)searchResult.Properties["mobile"][0];
                        }
                    }
                }
                di.IsProfitCenter  = true;
                di.IsPCManager     = false;
                di.IsRegionManager = false;
                di.IsServiceCenter = false;
                di.IsRegionSupport = false;
            });

            return(directoryItems);
        }