public static List <Chancery> GetAllChanceries() { List <Chancery> chanceries = new List <Chancery>(); string sqlExpression = "SELECT * FROM Chancery WHERE Deleted = 0"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(sqlExpression, connection); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { int id = (int)reader["Id"]; Company company = new Company { Id = SqlCompany_Chancery.GetCompanyByChancery(id), InitState = InitializationState.INITIALIZATION_NEEDED }; List <Document> archive = SqlArchive.GetArchivedDocuments(id); List <Document> pendingDocuments = SqlPendingDocuments.GetPendingDocuments(id); List <Secretary> secretaries = SqlSecretary.GetCompanySecretaries(company.Id); MainSecretary mainSecretary = null; if (SqlMainSecretary.GetCompanyMainSecretary(company.Id) != null) { mainSecretary = new MainSecretary { EmployeeId = SqlMainSecretary.GetCompanyMainSecretary(company.Id).EmployeeId, InitState = InitializationState.INITIALIZATION_NEEDED }; } Chancery chancery = new Chancery(company) { Id = id, Archive = archive, PendingDocuments = pendingDocuments, Secretaries = secretaries, MainSecretary = mainSecretary }; chanceries.Add(chancery); } } } return(chanceries); }
public static Chancery GetChancery(int id) { Chancery chancery = null; string sqlExpression = "SELECT * FROM Chancery WHERE Id = @id AND Deleted = 0"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(sqlExpression, connection); command.Parameters.Add("@id", SqlDbType.Int); command.Parameters["@id"].Value = id; SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { Company company = new Company { Id = SqlCompany_Chancery.GetCompanyByChancery(id), InitState = InitializationState.INITIALIZATION_NEEDED }; List <Document> archive = SqlArchive.GetArchivedDocuments(id); List <Document> pendingDocuments = SqlPendingDocuments.GetPendingDocuments(id); List <Secretary> secretaries = SqlSecretary.GetCompanySecretaries(company.Id); MainSecretary mainSecretary = SqlMainSecretary.GetCompanyMainSecretary(company.Id); chancery = new Chancery(company) { Id = id, Archive = archive, PendingDocuments = pendingDocuments, Secretaries = secretaries, MainSecretary = mainSecretary }; } } } return(chancery); }
public void UpdateCollections(DataLists oldDataLists, DataLists dataLists) { List <Person> oldPersons = oldDataLists.Persons; List <Person> persons = dataLists.Persons; foreach (Person person in persons) { //Если не было этой персоны то добавить if (oldPersons.Contains(person) == false) { SqlPerson.AddPerson(person); } } foreach (Person oldPerson in oldPersons) { //Если персона была удалена то удалить if (persons.Contains(oldPerson) == false) { SqlPerson.DeletePerson(oldPerson.Id); } } List <Company> oldCompanies = oldDataLists.Companies; List <Company> companies = dataLists.Companies; foreach (Company company in companies) { if (oldCompanies.Contains(company) == false) { SqlCompany.AddCompany(company); } } foreach (Company oldCompany in oldCompanies) { if (companies.Contains(oldCompany) == false) { SqlCompany_Director.DeleteFromCompany(oldCompany.Id); SqlCompany_Chancery.DeleteFromCompany(oldCompany.Id); SqlCompany.DeleteCompany(oldCompany.Id); } } List <Chancery> oldChanceries = oldDataLists.Chanceries; List <Chancery> chanceries = dataLists.Chanceries; foreach (Chancery chancery in chanceries) { if (oldChanceries.Contains(chancery) == false) { SqlChancery.AddChancery(chancery); AddCompanyChanceryRecord(chancery.Company, chancery); foreach (Document document in chancery.PendingDocuments) { SqlPendingDocuments.AddPendingDocument(chancery.Id, document.Id); } foreach (Document document in chancery.Archive) { SqlArchive.AddArchivedDocument(chancery.Id, document.Id); } } else { Chancery oldChancery = oldChanceries.Find((oc) => oc.Id == chancery.Id); foreach (Document document in chancery.PendingDocuments) { if (oldChancery.PendingDocuments.Contains(document) == false) { SqlPendingDocuments.AddPendingDocument(chancery.Id, document.Id); } } foreach (Document oldDocument in oldChancery.PendingDocuments) { if (chancery.PendingDocuments.Contains(oldDocument) == false) { SqlPendingDocuments.DeletePendingDocument(chancery.Id, oldDocument.Id); } } foreach (Document document in chancery.Archive) { if (oldChancery.Archive.Contains(document) == false) { SqlArchive.AddArchivedDocument(chancery.Id, document.Id); } } foreach (Document oldDocument in oldChancery.Archive) { if (chancery.Archive.Contains(oldDocument) == false) { SqlArchive.DeleteArchivedDocument(chancery.Id, oldDocument.Id); } } } } foreach (Chancery oldChancery in oldChanceries) { if (chanceries.Contains(oldChancery) == false) { SqlCompany_Chancery.GetCompanyByChancery(oldChancery.Id); SqlChancery.DeleteChancery(oldChancery.Id); } } List <Director> oldDirectors = oldDataLists.Directors; List <Director> directors = dataLists.Directors; foreach (Director director in directors) { if (oldDirectors.Contains(director) == false) { SqlDirector.AddDirector(director); AddCompanyDirectorRecord(director.Company, director); foreach (Document document in director.PendingDocuments) { SqlDirectorPendingDocuments.AddPendingDocument(director.EmployeeId, document.Id); } } else { Director oldDirector = oldDirectors.Find((od) => od.EmployeeId == director.EmployeeId); foreach (Document document in director.PendingDocuments) { if (oldDirector.PendingDocuments.Contains(document) == false) { SqlDirectorPendingDocuments.AddPendingDocument(director.EmployeeId, document.Id); } } foreach (Document oldDocument in oldDirector.PendingDocuments) { if (director.PendingDocuments.Contains(oldDocument) == false) { SqlDirectorPendingDocuments.DeletePendingDocument(director.EmployeeId, oldDocument.Id); } } } } foreach (Director oldDirector in oldDirectors) { if (directors.Contains(oldDirector) == false) { foreach (Document oldDocument in oldDirector.PendingDocuments) { SqlDirectorPendingDocuments.DeletePendingDocument(oldDirector.EmployeeId, oldDocument.Id); } SqlCompany_Director.GetCompanyByDirector(oldDirector.EmployeeId); SqlDirector.DeleteDirector(oldDirector.EmployeeId); } } List <Secretary> oldSecretaries = oldDataLists.Secretaries; List <Secretary> secretaries = dataLists.Secretaries; //документи не додаються foreach (Secretary secretary in secretaries) { if (oldSecretaries.Contains(secretary) == false) { SqlSecretary.AddSecretary(secretary); foreach (Document document in secretary.PendingDocuments) { SqlSecretaryPendingDocuments.AddPendingDocument(secretary.EmployeeId, document.Id); } foreach (Document document in secretary.CreatedDocuments) { SqlSecretaryCreatedDocuments.AddCreatedDocument(secretary.EmployeeId, document.Id); } } else { Secretary oldSecretary = oldSecretaries.Find((os) => os.EmployeeId == secretary.EmployeeId); foreach (Document document in secretary.PendingDocuments) { if (oldSecretary.PendingDocuments.Contains(document) == false) { SqlSecretaryPendingDocuments.AddPendingDocument(secretary.EmployeeId, document.Id); } } foreach (Document oldDocument in oldSecretary.PendingDocuments) { if (secretary.PendingDocuments.Contains(oldDocument) == false) { SqlSecretaryPendingDocuments.DeletePendingDocument(secretary.EmployeeId, oldDocument.Id); } } foreach (Document document in secretary.CreatedDocuments) { if (oldSecretary.CreatedDocuments.Contains(document) == false) { SqlSecretaryCreatedDocuments.AddCreatedDocument(secretary.EmployeeId, document.Id); } } foreach (Document oldDocument in oldSecretary.CreatedDocuments) { if (secretary.PendingDocuments.Contains(oldDocument) == false) { SqlSecretaryCreatedDocuments.DeleteCreatedDocument(secretary.EmployeeId, oldDocument.Id); } } } } foreach (Secretary oldSecretary in oldSecretaries) { if (secretaries.Contains(oldSecretary) == false) { foreach (Document oldDocument in oldSecretary.PendingDocuments) { SqlSecretaryPendingDocuments.DeletePendingDocument(oldSecretary.EmployeeId, oldDocument.Id); } foreach (Document oldDocument in oldSecretary.CreatedDocuments) { SqlSecretaryCreatedDocuments.DeleteCreatedDocument(oldSecretary.EmployeeId, oldDocument.Id); } SqlSecretary.DeleteSecretary(oldSecretary.EmployeeId); } } List <MainSecretary> oldMainSecretaries = oldDataLists.MainSecretaries; List <MainSecretary> mainSecretaries = dataLists.MainSecretaries; foreach (MainSecretary mainSecretary in mainSecretaries) { if (oldMainSecretaries.Contains(mainSecretary) == false) { SqlMainSecretary.AddMainSecretary(mainSecretary); } } foreach (MainSecretary oldMainSecretary in oldMainSecretaries) { if (mainSecretaries.Contains(oldMainSecretary) == false) { SqlMainSecretary.DeleteMainSecretary(oldMainSecretary.EmployeeId); } } List <Document> oldDocuments = oldDataLists.Documents; List <Document> documents = dataLists.Documents; foreach (Document document in documents) { if (oldDocuments.Contains(document) == false) { SqlDocument.AddDocument(document); } } foreach (Document oldDocument in oldDocuments) { if (documents.Contains(oldDocument) == false) { SqlDocument.DeleteDocument(oldDocument.Id); } } List <Marker> oldMarkers = oldDataLists.Markers; List <Marker> markers = dataLists.Markers; foreach (Marker marker in markers) { if (oldMarkers.Contains(marker) == false) { SqlMarker.AddMarker(marker); } } foreach (Marker oldMarker in oldMarkers) { if (markers.Contains(oldMarker) == false) { SqlMarker.DeleteMarker(oldMarker.Id); } } }