/****************************************************************************** // Canary Islands Computer Museum Website // ---------------------------------------------------------------------------- // // Filename : Company.cs // Author(s) : Natalia Portillo // // --[ Description ] ---------------------------------------------------------- // // Contains operations to manage companies. // // --[ License ] -------------------------------------------------------------- // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // ---------------------------------------------------------------------------- // Copyright © 2003-2018 Natalia Portillo *******************************************************************************/ using System; using System.Collections.Generic; using System.Data; using Cicm.Database.Schemas; using Console = System.Console; namespace Cicm.Database { public partial class Operations { /// /// Gets all companies /// /// All companies /// true if is correct, false otherwise public bool GetCompanies(out List entries) { #if DEBUG Console.WriteLine("Getting all companies..."); #endif try { const string SQL = "SELECT * from companies"; IDbCommand dbCmd = dbCon.CreateCommand(); IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); dbCmd.CommandText = SQL; DataSet dataSet = new DataSet(); dataAdapter.SelectCommand = dbCmd; dataAdapter.Fill(dataSet); entries = CompaniesFromDataTable(dataSet.Tables[0]); return true; } catch(Exception ex) { Console.WriteLine("Error getting companies."); Console.WriteLine(ex); entries = null; return false; } } /// /// Gets the specified number of companies since the specified start /// /// List of companies /// Start of query /// How many entries to retrieve /// true if is correct, false otherwise public bool GetCompanies(out List entries, ulong start, ulong count) { #if DEBUG Console.WriteLine("Getting {0} companies from {1}...", count, start); #endif try { string sql = $"SELECT * FROM companies LIMIT {start}, {count}"; IDbCommand dbCmd = dbCon.CreateCommand(); IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); dbCmd.CommandText = sql; DataSet dataSet = new DataSet(); dataAdapter.SelectCommand = dbCmd; dataAdapter.Fill(dataSet); entries = CompaniesFromDataTable(dataSet.Tables[0]); return true; } catch(Exception ex) { Console.WriteLine("Error getting companies."); Console.WriteLine(ex); entries = null; return false; } } /// /// Gets company by specified id /// /// Id /// Company with specified id, null if not found or error public Company GetCompany(int id) { #if DEBUG Console.WriteLine("Getting company with id {0}...", id); #endif try { string sql = $"SELECT * from companies WHERE id = '{id}'"; IDbCommand dbCmd = dbCon.CreateCommand(); IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); dbCmd.CommandText = sql; DataSet dataSet = new DataSet(); dataAdapter.SelectCommand = dbCmd; dataAdapter.Fill(dataSet); List entries = CompaniesFromDataTable(dataSet.Tables[0]); return entries == null || entries.Count == 0 ? null : entries[0]; } catch(Exception ex) { Console.WriteLine("Error getting company."); Console.WriteLine(ex); return null; } } /// /// Counts the number of companies in the database /// /// Entries in database public long CountCompanies() { #if DEBUG Console.WriteLine("Counting companies..."); #endif IDbCommand dbcmd = dbCon.CreateCommand(); dbcmd.CommandText = "SELECT COUNT(*) FROM companies"; object count = dbcmd.ExecuteScalar(); dbcmd.Dispose(); try { return Convert.ToInt64(count); } catch { return 0; } } /// /// Adds a new company to the database /// /// Entry to add /// ID of added entry /// true if added correctly, false otherwise public bool AddCompany(Company entry, out long id) { #if DEBUG Console.Write("Adding company `{0}`...", entry.Name); #endif IDbCommand dbcmd = GetCommandCompany(entry); IDbTransaction trans = dbCon.BeginTransaction(); dbcmd.Transaction = trans; const string SQL = "INSERT INTO companies (name, founded, website, twitter, facebook, sold, sold_to, " + "address, city, province, postal_code, country, status) VALUES (@name, @founded, @website, " + "@twitter, @facebook, @sold, @sold_to, @address, @city, @province, @postal_code, " + "@country, status)"; dbcmd.CommandText = SQL; dbcmd.ExecuteNonQuery(); trans.Commit(); dbcmd.Dispose(); id = dbCore.LastInsertRowId; #if DEBUG Console.WriteLine(" id {0}", id); #endif return true; } /// /// Updated a company in the database /// /// Updated entry /// true if updated correctly, false otherwise public bool UpdateCompany(Company entry) { #if DEBUG Console.WriteLine("Updating company `{0}`...", entry.Name); #endif IDbCommand dbcmd = GetCommandCompany(entry); IDbTransaction trans = dbCon.BeginTransaction(); dbcmd.Transaction = trans; string sql = "UPDATE companies SET name = @name, founded = @founded, website = @website, twitter = @twitter, " + "facebook = @facebook, sold = @sold, sold_to = @sold_to, address = @address, city = @city, " + "province = @province, postal_code = @postal_code, country = @country, status = @status " + $"WHERE id = {entry.Id}"; dbcmd.CommandText = sql; dbcmd.ExecuteNonQuery(); trans.Commit(); dbcmd.Dispose(); return true; } /// /// Removes a company from the database /// /// ID of entry to remove /// true if removed correctly, false otherwise public bool RemoveCompany(long id) { #if DEBUG Console.WriteLine("Removing company widh id `{0}`...", id); #endif IDbCommand dbcmd = dbCon.CreateCommand(); IDbTransaction trans = dbCon.BeginTransaction(); dbcmd.Transaction = trans; string sql = $"DELETE FROM companies WHERE id = '{id}';"; dbcmd.CommandText = sql; dbcmd.ExecuteNonQuery(); trans.Commit(); dbcmd.Dispose(); return true; } IDbCommand GetCommandCompany(Company entry) { IDbCommand dbcmd = dbCon.CreateCommand(); IDbDataParameter param1 = dbcmd.CreateParameter(); IDbDataParameter param2 = dbcmd.CreateParameter(); IDbDataParameter param3 = dbcmd.CreateParameter(); IDbDataParameter param4 = dbcmd.CreateParameter(); IDbDataParameter param5 = dbcmd.CreateParameter(); IDbDataParameter param6 = dbcmd.CreateParameter(); IDbDataParameter param7 = dbcmd.CreateParameter(); IDbDataParameter param8 = dbcmd.CreateParameter(); IDbDataParameter param9 = dbcmd.CreateParameter(); IDbDataParameter param10 = dbcmd.CreateParameter(); IDbDataParameter param11 = dbcmd.CreateParameter(); IDbDataParameter param12 = dbcmd.CreateParameter(); IDbDataParameter param13 = dbcmd.CreateParameter(); param1.ParameterName = "@name"; param2.ParameterName = "@founded"; param3.ParameterName = "@website"; param4.ParameterName = "@twitter"; param5.ParameterName = "@facebook"; param6.ParameterName = "@sold"; param7.ParameterName = "@sold_to"; param8.ParameterName = "@address"; param9.ParameterName = "@city"; param10.ParameterName = "@province"; param11.ParameterName = "@postal_code"; param12.ParameterName = "@country"; param13.ParameterName = "@status"; param1.DbType = DbType.String; param2.DbType = DbType.DateTime; param3.DbType = DbType.String; param4.DbType = DbType.String; param5.DbType = DbType.String; param6.DbType = DbType.DateTime; param7.DbType = DbType.UInt32; param8.DbType = DbType.String; param9.DbType = DbType.String; param10.DbType = DbType.String; param11.DbType = DbType.String; param12.DbType = DbType.UInt16; param13.DbType = DbType.UInt32; param1.Value = entry.Name; param2.Value = entry.Founded; param3.Value = entry.Website; param4.Value = entry.Twitter; param5.Value = entry.Facebook; if(entry.SoldTo != null) { param6.Value = entry.Sold; param7.Value = entry.SoldTo.Id; } else { param6.Value = null; param7.Value = null; } param8.Value = entry.Address; param9.Value = entry.City; param10.Value = entry.Province; param11.Value = entry.PostalCode; if(entry.Country != null) param12.Value = entry.Country.Id; else param12.Value = null; param13.Value = entry.Status; dbcmd.Parameters.Add(param1); dbcmd.Parameters.Add(param2); dbcmd.Parameters.Add(param3); dbcmd.Parameters.Add(param4); dbcmd.Parameters.Add(param5); dbcmd.Parameters.Add(param6); dbcmd.Parameters.Add(param7); dbcmd.Parameters.Add(param8); dbcmd.Parameters.Add(param9); dbcmd.Parameters.Add(param10); dbcmd.Parameters.Add(param11); dbcmd.Parameters.Add(param12); dbcmd.Parameters.Add(param13); return dbcmd; } List CompaniesFromDataTable(DataTable dataTable) { List entries = new List(); foreach(DataRow dataRow in dataTable.Rows) { Company entry = new Company { Id = int.Parse(dataRow["id"].ToString()), Name = dataRow["name"].ToString(), Website = dataRow["website"].ToString(), Twitter = dataRow["twitter"].ToString(), Facebook = dataRow["facebook"].ToString(), Address = dataRow["address"].ToString(), City = dataRow["city"].ToString(), Province = dataRow["province"].ToString(), PostalCode = dataRow["postal_code"].ToString(), Status = (CompanyStatus)int.Parse(dataRow["status"].ToString()) }; if(!string.IsNullOrWhiteSpace(dataRow["founded"].ToString())) entry.Founded = Convert.ToDateTime(dataRow["founded"].ToString()); if(!string.IsNullOrWhiteSpace(dataRow["sold"].ToString()) && !string.IsNullOrWhiteSpace(dataRow["sold_to"].ToString())) { entry.Sold = Convert.ToDateTime(dataRow["sold"].ToString()); entry.SoldTo = GetCompany(int.Parse(dataRow["sold_to"].ToString())); } if(!string.IsNullOrWhiteSpace(dataRow["country"].ToString())) entry.Country = GetIso3166(int.Parse(dataRow["country"].ToString())); if(GetCompanyLogosByCompany(out List logos, entry.Id)) { entry.Logos = logos.ToArray(); if(entry.Logos != null && entry.Logos.Length > 0) if(entry.Logos.Length > 1) { int currentYear = 0; foreach(CompanyLogo logo in entry.Logos) { if(logo.Year <= currentYear) continue; entry.LastLogo = logo; currentYear = logo.Year; } } else entry.LastLogo = entry.Logos[0]; } if(GetCompanyDescriptionsByCompany(out List descriptions, entry.Id)) if(descriptions != null && descriptions.Count > 0) entry.Description = descriptions[0].Text; entries.Add(entry); } return entries; } } }