# ServerFeed [C#]

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Windows;
     using System.Windows.Controls;
     using System.Windows.Data;
     using System.Windows.Documents;
     using System.Windows.Input;
     using System.Windows.Media;
     using System.Windows.Media.Imaging;
     using System.Windows.Navigation;
     using System.Windows.Shapes;
     using WpfApplication1.ServiceReference1;
     using System.Collections.ObjectModel;

     namespace WpfApplication1
     {

         // Interaction logic for ServerFeed.xaml
         public partial class ServerFeed : UserControl
         {
             ServiceSample sample = new ServiceSample();

             public ServerFeed()
             {
                 InitializeComponent();
             }

             //Initialize the XAML object and retrieve group from server by login
             public ServerFeed(string login, string password)
             {
                 InitializeComponent();

                 //Login and retrive group
                 List<View_Group> listResult;
                 sample.GetGroup(out listResult, login, password);

                 //Bind group to TreeList
                 treeGroup.ItemsSource = TreeNode.GetTreeNode(listResult);
             }

             //Select group and retrive coresponding server feed
             private void treeGroup_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
             {
                 //Retrive server feed by group
                 List<XMLMedia> listResult;
                 sample.GetXMLMedia(out listResult, (treeGroup.SelectedItem as TreeNode).Id??0);

                 //Bind server feed to combobox
                 comboFeed.ItemsSource = (from l1 in listResult where l1.TypeId == 1 select l1).ToList();
             }

             //Select server feed and retrive coresponding data
             private void comboFeed_SelectionChanged(object sender, SelectionChangedEventArgs e)
             {
                 if (comboFeed.SelectedItem == null)
                 {
                     gridFeedData.ItemsSource = null;
                     return;
                 }

                 //Display only 30 row
                 ObservableCollection<FeedRow> listRow = new ObservableCollection<FeedRow>();
                 for (int i = 0; i < 30; i++)
                 {
                     listRow.Add(new FeedRow((comboFeed.SelectedItem as XMLMedia).Id, i));
                 }

                 //Retrive server feed data
                 List<XMLMediaData> listResult;
                 sample.GetXMLMediaData(out listResult, (comboFeed.SelectedItem as XMLMedia).Id);

                 //Update list from the retrived data
                 for (int i = 0; i < listResult.Count; i++)
                 {
                     listRow[listResult[i].RowIndex ?? 0].ListColData.Add(listResult[i]);
                 }

                 //Bind data to DataGrid
                 gridFeedData.ItemsSource = listRow;
             }

             //Save server feed changes
             private void btnValidate_Click(object sender, RoutedEventArgs e)
             {
                 //Retrive changed data
                 List<XMLMediaData> listResult = new List<XMLMediaData>();
                 (from l1 in (gridFeedData.ItemsSource as ObservableCollection<FeedRow>) select l1.ListColUpdated).ToList().ForEach(x => listResult.AddRange(x)); ;

                 //Update data
                 sample.SetXMLMediaData(listResult);
             }

             private void gridFeedData_LoadingRow(object sender, DataGridRowEventArgs e)
             {
                 e.Row.Header = (e.Row.GetIndex() + 1).ToString();
             }
         }

         public class FeedRow
         {
             public long XMLMediaId;
             public int RowIndex;
             public List<XMLMediaData> ListColData = new List<XMLMediaData>();

             public FeedRow(long xmlMediaId, int rowIndex)
             {
                 this.XMLMediaId = xmlMediaId;
                 this.RowIndex = rowIndex;
             }

             public List<XMLMediaData> ListColUpdated
             {
                 get
                 {
                     return (from l1 in ListColData where ListUpdatedIndex.Contains(l1.ColumnIndex ?? -1) select l1).Distinct().ToList();
                 }
             }

             public string ColA
             {
                 get { return GetColByIndex(0); }
                 set { SetColByIndex(0, value); }
             }

             public string ColB
             {
                 get { return GetColByIndex(1); }
                 set { SetColByIndex(1, value); }
             }

             public string ColC
             {
                 get { return GetColByIndex(2); }
                 set { SetColByIndex(2, value); }
             }

             public string ColD
             {
                 get { return GetColByIndex(3); }
                 set { SetColByIndex(3, value); }
             }

             public string ColE
             {
                 get { return GetColByIndex(4); }
                 set { SetColByIndex(4, value); }
             }

             string GetColByIndex(int index)
             {
                 var item = (from l1 in ListColData where l1.ColumnIndex == index select l1).FirstOrDefault();
                 return item == null ? "" : item.Text;
             }

             void SetColByIndex(int index, string value)
             {
                 var item = (from l1 in ListColData where l1.ColumnIndex == index select l1).FirstOrDefault();
                 if (item == null)
                 {
                     item = new XMLMediaData() { ColumnIndex = index, Text = value, XMLMediaId = XMLMediaId, RowIndex = RowIndex };
                 }
                 else
                 {
                     item.Text = value;
                 }
                 ListColData.Add(item);

                 if (!ListUpdatedIndex.Contains(index)) ListUpdatedIndex.Add(index);
             }

             List<int> ListUpdatedIndex = new List<int>();
         }
     }