//constructor
 public ControllerAudit(ControllerParent calling, FormAudit view, ArrayList list_customers, ArrayList list_licenseinventories, ArrayList list_systeminventories, ArrayList list_audits, ArrayList list_licenses) : base(calling, list_customers)
 {
     //connect controller to its view
     this.view = view;
     this.list_licenseInventories = list_licenseinventories;
     this.list_systemInventories = list_systeminventories;
     this.list_audits = list_audits;
     this.list_allAvailableLicenses = list_licenses;
     currentAudit = null;
     currentLicenseInventory = null;
     currentSystemInventory = null;
 }
        public override void SelectedCustomerChanged(Object customer)
        {
            base.SelectedCustomerChanged(customer);
            Log.WriteLog(string.Format("Customer changed successfully: New Customer: {0}", currentCustomer.Name));

            //Get Networkinventory of the customer
            currentNetworkInventory = null;
            foreach (NetworkInventory n in list_networkInventories)
            {
                if (n.Customernumber == currentCustomer.Cnumber)
                {
                    currentNetworkInventory = n;
                    Log.WriteLog(string.Format("NetworkInventory for customer {0} found", currentCustomer.Name));
                }
            }

            //Get LicenseInventory of the customer
            currentLicenseInventory = null;
            foreach (LicenseInventory li in list_licenseInventories)
            {
                if (li.Customernumber == currentCustomer.Cnumber)
                {
                    currentLicenseInventory = li;
                    Log.WriteLog(string.Format("LicenseInventory for customer {0} found", currentCustomer.Name));
                }
            }

            //Get latest SystemInventory of the customer
            currentSystemInventory = null;
            foreach (SystemInventory si in list_systemInventories)
            {
                if (si.Customernumber == currentCustomer.Cnumber)
                {
                    currentSystemInventory = si;
                    Log.WriteLog(string.Format("SystemInventory for customer {0} found", currentCustomer.Name));
                }
            }

            //Get latest Audit of the customer
            currentAudit = null;
            foreach (Audit a in list_Audits)
            {
                if (a.CustomerNumber == currentCustomer.Cnumber)
                {
                    currentAudit = a;
                    Log.WriteLog(string.Format("Audit for customer {0} found", currentCustomer.Name));
                }
            }
            UpdateView(false);


        }
        public ArrayList GetSystemInventories()
        {
            ArrayList list_systeminventories = new ArrayList();
            try
            {
                connection = new SQLiteConnection("Data Source=" + dbpath + ";Version=3; MultipleActiveResultSets=True; foreign keys=true;");
                connection.Open();
                command = new SQLiteCommand(connection);
                command.CommandText = "SELECT * FROM systeminventory;";
                SQLiteDataReader r = command.ExecuteReader();
                while (r.Read())
                {
                    SystemInventory currentSystemInventory = new SystemInventory(r.GetInt32(1), r.GetInt32(0));
                    currentSystemInventory.Date = (DateTime)r.GetDateTime(2);
                    //Get the beloning clientsystems of the systeminventory
                    SQLiteCommand command2 = new SQLiteCommand(connection);
                    command2.CommandText = "SELECT clientSystemNumber FROM containsSystem WHERE systemInventoryNumber=@siNumber;";
                    command2.Parameters.AddWithValue("@siNumber", currentSystemInventory.SystemInventoryNumber);
                    SQLiteDataReader r_inner = command2.ExecuteReader();
                    while (r_inner.Read())
                    {
                        int clientsystemkNumber = r_inner.GetInt32(0);
                        //Get the beloning Clientsystem
                        SQLiteCommand command3 = new SQLiteCommand(connection);
                        command3.CommandText = "SELECT * FROM clientsystem WHERE clientSystemNumber=@clientSystemNumber;";
                        command3.Parameters.AddWithValue("@clientSystemNumber", clientsystemkNumber);
                        SQLiteDataReader r_inner_inner = command3.ExecuteReader();
                        while (r_inner_inner.Read())
                        {
                            ClientSystem c;
                            object test = r_inner_inner["networknumber"];
                            if (test != DBNull.Value)
                            {
                                c = new ClientSystem(r_inner_inner.GetInt32(0), IPAddress.Parse((string)r_inner_inner["clientIP"]), r_inner_inner.GetInt32(1));
                            }
                            else
                            {
                                c = new ClientSystem(r_inner_inner.GetInt32(0), IPAddress.Parse((string)r_inner_inner["clientIP"]), -1);
                            }
                            test = r_inner_inner["type"];
                            if (test != DBNull.Value)
                            {
                                c.Type = (string)r_inner_inner["type"];
                                c.Computername = (string)r_inner_inner["computername"];
                                c.Serial = (string)r_inner_inner["serial"];
                            }
                            else
                            {
                                c.Type = null;
                                c.Computername = null;
                                c.Serial = null;
                            }
                            currentSystemInventory.AddSystemToInventory(c);
                        }//END ClientSystem
                    }//END containsSystem
                    list_systeminventories.Add(currentSystemInventory);
                }//END SI
            }
            catch (Exception e)
            {
                MessageBox.Show("Error SQL Reading SystemInventories", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.WriteLog(e.Message);
            }
            connection.Close();


            return list_systeminventories;
        }
 public void SaveSystemInventory(SystemInventory si)
 {
     try
     {
         connection = new SQLiteConnection("Data Source=" + dbpath + ";Version=3;MultipleActiveResultSets=True; foreign keys=true;");
         connection.Open();
         command = new SQLiteCommand(connection);
         command.CommandText = "INSERT INTO systeminventory (systemInventoryNumber, customerNumber, date) VALUES(@systemInventoryNumber, @customerNumber, @date);";
         command.Parameters.AddWithValue("@systemInventoryNumber", si.SystemInventoryNumber);
         command.Parameters.AddWithValue("@customerNumber", si.Customernumber);
         command.Parameters.AddWithValue("@date", si.Date);
         command.ExecuteNonQuery();
     }
     catch (Exception e)
     {
         MessageBox.Show("Error SQL Write SystemInventory", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Log.WriteLog(e.Message);
     }
     connection.Close();
 }
 public void SaveClientSystem(ClientSystem c, SystemInventory si)
 {//Mehtod is onlyused to initial save the system
     try
     {
         connection = new SQLiteConnection("Data Source=" + dbpath + ";Version=3; foreign keys=true;");
         connection.Open();
         command = new SQLiteCommand(connection);
         command.CommandText = "INSERT INTO clientsystem (clientSystemNumber, networknumber, computername, type, serial, clientIP) VALUES(@clientSystemNumber, @networknumber, @computername, @type, @serial, @clientIP);";
         command.Parameters.AddWithValue("@clientSystemNumber", c.ClientSystemNumber);
         command.Parameters.AddWithValue("@networknumber", c.Networknumber);
         command.Parameters.AddWithValue("@computername", c.Computername);
         command.Parameters.AddWithValue("@type", c.Type);
         command.Parameters.AddWithValue("@serial", c.Serial);
         command.Parameters.AddWithValue("@clientIP", c.ClientIP.ToString());
         command.ExecuteNonQuery();
         //Connect clientsystem to systeminventory in database
         command.CommandText = "INSERT INTO containsSystem (systemInventoryNumber, clientSystemNumber) VALUES(@systemInventoryNumber, @clientSystemNumber);";
         command.Parameters.AddWithValue("@clientSystemNumber", c.ClientSystemNumber);
         command.Parameters.AddWithValue("@systemInventoryNumber", si.SystemInventoryNumber);
         command.ExecuteNonQuery();
     }
     catch (Exception e)
     {
         MessageBox.Show("Error SQL Write Clientsystem", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Log.WriteLog(e.Message);
     }
     connection.Close();
 }
        public void cmbInventory_SelectedIndexChanged(Object inventory)
        {
            if (!cmbInventorySelectedIndex)
            {
                //extract Date from cmbInventory
                string input = (string)inventory;
                Log.WriteLog(string.Format("Inventory changed successfully: New Inventory: {0}", input));
                string[] splitted_input = input.Split();
                input = splitted_input[1] + " " + splitted_input[2];
                DateTime inventoryDate = DateTime.Parse(input);
                //get new System Inventory
                foreach (SystemInventory si in list_systemInventories)
                {
                    if (DateTime.Compare(si.Date, inventoryDate)==0)
                    {
                        currentSystemInventory = si;
                        Log.WriteLog(string.Format("Belonging SystemInventory found: {0}", currentSystemInventory.SystemInventoryNumber));
                    }
                }
                //get auidt belonging system inventory if it exists
                currentAudit = null;
                foreach (Audit a in list_audits)
                {
                    if (a.CustomerNumber == currentCustomer.Cnumber && a.SystemInventoryNumber == currentSystemInventory.SystemInventoryNumber)
                    {
                        currentAudit = a;
                        Log.WriteLog(string.Format("Belonging Audit found: {0}", currentAudit.AuditNumber));
                    }
                }
                UpdateView(false);
            }

        }
        public override void SelectedCustomerChanged(Object customer)
        {
            base.SelectedCustomerChanged(customer);
            currentLicenseInventory = null;
            currentSystemInventory = null;
            currentAudit = null;
            Log.WriteLog(string.Format("Customer changed successfully: New Customer: {0}", currentCustomer.Name));
            //Get Licenseinventory of the customer or Display Message
            foreach (LicenseInventory li in list_licenseInventories)
            {
                if (li.Customernumber == currentCustomer.Cnumber)
                {
                    currentLicenseInventory = li;
                    Log.WriteLog(string.Format("Licenseinventory for customer {0} found", currentCustomer.Name));
                    view.EnableAudit();
                }
            }
            if (currentLicenseInventory == null)
            {
                MessageBox.Show("Kein Lizenzinventar für diesen Kunden gefunden. Bitte erstellen Sie zuerst ein Lizenzinventar.", "Kein Lizenzinventar gefunden", MessageBoxButtons.OK, MessageBoxIcon.Error);
                view.DisableAudit();
            }
            //Get latest Audit
            foreach (Audit a in list_audits)
            {
                if (a.CustomerNumber == currentCustomer.Cnumber)
                {
                    if (currentAudit == null)
                    {
                        currentAudit = a;
                        Log.WriteLog(string.Format("Audit for customer {0} found", currentCustomer.Name));
                    }
                    else if (DateTime.Compare(a.Date, currentAudit.Date) > 0)
                    {
                        currentAudit = a;
                        Log.WriteLog(string.Format("Newer Audit for customer {0} found", currentCustomer.Name));
                    }

                }
            }
            if (currentAudit != null)
            {
                //Get belonging SystemInventory of the current Audit
                foreach (SystemInventory si in list_systemInventories)
                {
                    if (si.SystemInventoryNumber == currentAudit.SystemInventoryNumber)
                    {
                        currentSystemInventory = si;
                        Log.WriteLog("Belonging Systeminventory for current Audit found.");
                        view.EnableAudit();
                        MessageBox.Show(String.Format("Audit für diesen Kunden gefunden. Audit vom {0} für Systeminventar vom {1} wird dargestellt.\nBitte beachten Sie, dass sich das Lizenzinventar des Kunden seitdem geändert haben kann und das aktuell dargestellte Lizenzinventar nicht mit dem Ergebnis des Audits in Verbindung steht.",
                            currentAudit.Date, currentSystemInventory.Date), "Audit vorhanden", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    }
                }
            }
            //Get Systeminventory of the customer or Display Message in case no audit with belonging systeminventory was found
            if (currentSystemInventory == null)
            {
                foreach (SystemInventory si in list_systemInventories)
                {
                    if (si.Customernumber == currentCustomer.Cnumber)
                    {
                        currentSystemInventory = si;
                        Log.WriteLog(string.Format("Systeminventory for customer {0} found", currentCustomer.Name));
                        view.EnableAudit();
                    }
                }
                if (currentSystemInventory == null)
                {
                    MessageBox.Show("Kein Systeminventar für diesen Kunden gefunden. Bitte führen Sie zuerst ein Netzwerkinventarisierung durch.", "Kein Systeminventar gefunden", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    view.DisableAudit();
                }
            }
            UpdateView(false);
        }
 //scan Network for Clients
 private void scanNetwork(ProgressBar progress)
 {
     //Create a new system Inventory
     currentSystemInventory = CreateSystemInventroy(currentCustomer.Cnumber);
     Log.WriteLog("New SystemInventory created");
     //Get the latest systemnumber
     int latestsystemnumber;
     //only if there is no data, set it manually
     if (list_systems.Count <= 0)
     {
         latestsystemnumber = 0;
     }
     else
     {
         currentSystem = (ClientSystem)list_systems[list_systems.Count - 1];
         latestsystemnumber = currentSystem.ClientSystemNumber;
     }
     //Get number adresses
     int i = 0;
     foreach (Network n in selectedNetworks)
     {
         foreach (IPAddress ip in n.IpAddresses) i++;
     }
     if (progress != null)
     {
         progress.Value = 0;
         progress.Maximum = i;
     }
     //scan Networks
     Dictionary<String, IPAddress> unique = new Dictionary<string, IPAddress>();
     foreach (Network n in selectedNetworks)
     {
         // Ping each ip address of the network with timeout of 100ms
         foreach (IPAddress ip in n.IpAddresses)
         {
             try
             {
                 if (progress != null) progress.PerformStep();
                 unique.Add(ip.ToString(), ip);
                 Ping pingSender = new Ping();
                 PingReply reply = pingSender.Send(ip, 100);
                 Log.WriteLog(string.Format("Ping: {0}", ip.ToString()));
                 if (reply.Status == IPStatus.Success)
                 {
                     currentSystem = new ClientSystem(++latestsystemnumber, ip, n.NetworkNumber);
                     list_systems.Add(currentSystem);
                     db.SaveClientSystem(currentSystem, currentSystemInventory);
                     currentSystemInventory.AddSystemToInventory(currentSystem);
                     Log.WriteLog(string.Format("System {0} added to Systeminventory",
                         currentSystem.ClientIP.ToString()));
                 }
             }
             //If dictionary contains key already
             catch (ArgumentException e)
             {
                 Log.WriteLog(string.Format("IP-Addresse already in list: {0}", ip.ToString()));
                 Log.WriteLog(e.Message);
             }
         }
     }
 }
 public override void SelectedCustomerChanged(Object customer)
 {
     base.SelectedCustomerChanged(customer);
     currentNetworkInventory = null;
     currentSystemInventory = null;
     Log.WriteLog(string.Format("Customer changed successfully: New Customer: {0}", currentCustomer.Name));
     //Get Networkinventory of the customer
     foreach (NetworkInventory n in list_networkinventories)
     {
         if (n.Customernumber == currentCustomer.Cnumber)
         {
             currentNetworkInventory = n;
             Log.WriteLog(string.Format("Networkinventory for customer {0} found", currentCustomer.Name));
         }
     }
     if (currentNetworkInventory == null)
     {
         Log.WriteLog("No Networkinventory found");
         MessageBox.Show("Kein Netzwerkinventar gefunden, bitte ein Netzwerkinventar für den Kunden erstellen", "Kein Netzwerkinventar", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     //Get latest Systeminventory of the customer
     foreach (SystemInventory si in list_systemInventories)
     {
         if (si.Customernumber == currentCustomer.Cnumber)
         {
             if (currentSystemInventory == null)
             {
                 currentSystemInventory = si;
                 Log.WriteLog(string.Format("Systeminventory for customer {0} found", currentCustomer.Name));
             }
             //if a systeminventory is newer than the current take this instead
             else if (DateTime.Compare(si.Date, currentSystemInventory.Date) > 0)
             {
                 currentSystemInventory = si;
                 Log.WriteLog(string.Format("Newer Systeminventory for customer {0} found", currentCustomer.Name));
             }
         }
     }
     //Inform the customer and select all networks to show all Clientsystems of the latest Systeminventory
     if (currentSystemInventory != null)
     {
         // check if all belonging networks still exist
         bool deletednetworks = false;
         foreach (ClientSystem c in currentSystemInventory.List_Systems)
         {
             if (c.Networknumber == -1)
             {
                 deletednetworks = true;
                 break;
             }
         }
         if (!deletednetworks)
         {
             MessageBox.Show(String.Format("Es wird das aktuellste Systeminventar vom {0} dargestellt.", currentSystemInventory.Date), "Systeminventar gefunden", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else
         {
             MessageBox.Show(String.Format("Es wird das aktuellste Systeminventar vom {0} dargestellt. Dies enthält jedoch bereits gelöschte Netzwerke. Daher werden nicht mehr alle Clientsysteme dieses Inventars angezeigt. Es wird empfohlen eine erneute Inventarisierung für diesen Kunden durchzufürhen.",
                 currentSystemInventory.Date), "Systeminventar gefunden", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         UpdateView(false);
         view.SetChkAll(true);
         return;
     }
     UpdateView(false);
 }
 public SystemInventory CreateSystemInventroy(int customerNumber)
 {
     currentSystemInventory = new SystemInventory(customerNumber, list_systemInventories.Count);
     db.SaveSystemInventory(currentSystemInventory);
     list_systemInventories.Add(currentSystemInventory);
     return currentSystemInventory;
 }