private void _saveSelectedContactToAllTemplatesAndAddressBook() { if (SelectedAddrBookContact == null || CurAddressBookModel == null || CurAddressBookModel.AddressBookContactList == null || CurAddressBookModel.TemplateContacts == null) { return; } if (CurAddressBookModel.AddressBookContactList.Contains(SelectedAddrBookContact) == false) { bool foundContactWithMatchingNameInAddressBook = false; //using a for because you can't replace an object in a foreach for (int i = 0; i < CurAddressBookModel.AddressBookContactList.Count; i++) { AddressBookContact curContact = CurAddressBookModel.AddressBookContactList[i]; if (curContact.Name.ToUpper() == SelectedAddrBookContact.Name.ToUpper()) { foundContactWithMatchingNameInAddressBook = true; _cloneValuesAcrossAddressBookContacts(curContact, SelectedAddrBookContact, SharedEnums.ContactSourceType.AddressBook); } } if (foundContactWithMatchingNameInAddressBook == false) { AddressBookContact newContact = new AddressBookContact(); _cloneValuesAcrossAddressBookContacts(newContact, SelectedAddrBookContact, SharedEnums.ContactSourceType.AddressBook); CurAddressBookModel.AddressBookContactList.Add(newContact); } } foreach (TemplateContact curTemplate in CurAddressBookModel.TemplateContacts) { if (curTemplate.TemplateContacts.Contains(SelectedAddrBookContact) == false) { bool foundContactWithMatchingNameInTemplate = false; for (int i = 0; i < curTemplate.TemplateContacts.Count; i++) { AddressBookContact curContact = curTemplate.TemplateContacts[i]; if (curContact.Name.ToUpper() == SelectedAddrBookContact.Name.ToUpper()) { foundContactWithMatchingNameInTemplate = true; _cloneValuesAcrossAddressBookContacts(curContact, SelectedAddrBookContact, SharedEnums.ContactSourceType.TemplateFile); break; } } if (foundContactWithMatchingNameInTemplate == false) { AddressBookContact newContact = new AddressBookContact(); _cloneValuesAcrossAddressBookContacts(newContact, SelectedAddrBookContact, SharedEnums.ContactSourceType.TemplateFile); curTemplate.TemplateContacts.Add(newContact); } } } }
private void _addNewContact() { if (this.CurAddressBookModel != null && this.CurAddressBookModel.AddressBookContactList != null) { AddressBookContact newContact = new AddressBookContact(SharedEnums.ContactSourceType.AddressBook); this.CurAddressBookModel.AddressBookContactList.Add(newContact); this.SelectedAddrBookContact = newContact; } }
public void SaveChangesToFile(string addressBookPath = @"C:\Program Files\InspectIT\Address.mdb") { if (File.Exists(addressBookPath) == false) { throw new FileNotFoundException(); } using (OleDbConnection curOleConn = new OleDbConnection(string.Format("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = {0}", addressBookPath))) using (OleDbCommand curOleCommand = curOleConn.CreateCommand()) { curOleConn.Open(); //Need to clear out all contacts which have been removed from the contact list in the template: string pkValuesInContactList = getAllPkValuesFromContactsForUseWithSqlWhereInClause(this.AddressBookContactList); if (pkValuesInContactList != "()") { curOleCommand.CommandText = "DELETE FROM `Address` WHERE [Address_ID] NOT IN " + pkValuesInContactList; curOleCommand.ExecuteNonQuery(); } else { curOleCommand.CommandText = "DELETE FROM `Address`"; curOleCommand.ExecuteNonQuery(); } foreach (AddressBookContact curContact in this.AddressBookContactList) { if (curContact == null || string.IsNullOrWhiteSpace(curContact.Name) || curContact.ContactHasBeenModified == false) { continue; } AddressBookContact.CopyLogoToITpipesDirectory(curContact, "AddressBook"); if (curContact.SourceRecordIntPK > 0) { AddressBookContact.LoadCommandToUpdateExistingContactRecord(curOleCommand, curContact); } else { AddressBookContact.LoadCommandToInsertNewContactRecord(curOleCommand, curContact); } curOleCommand.ExecuteNonQuery(); if (curContact.SourceRecordIntPK > 0 == false) { curOleCommand.Parameters.Clear(); curOleCommand.CommandText = "SELECT TOP 1 [Address_ID] FROM Address ORDER BY [Address_ID] DESC"; curContact.SourceRecordIntPK = (int)curOleCommand.ExecuteScalar(); } } } }
private void _copySelectedTemplateContactToAddressBook() { if (SelectedTemplate == null || SelectedTemplate.TemplateContacts.Contains(this.SelectedAddrBookContact) == false || this.SelectedTemplate == null) { //In this case, the selected contact is not a template contact or there is no selected template--maybe put in some kind of notification later if I feel like it. return; } AddressBookContact clonedContact = this.SelectedAddrBookContact.Clone(); clonedContact.SourceFileType = SharedEnums.ContactSourceType.AddressBook; this.CurAddressBookModel.AddressBookContactList.Add(clonedContact); }
public static void CopyLogoToITpipesDirectory(AddressBookContact sourceContact, string logoPrefix) { if (File.Exists(sourceContact.PathToLogo) == false) { return; } if (Directory.Exists(@"C:\Program Files\InspectIT\Logos") == false) { Directory.CreateDirectory(@"C:\Program Files\InspectIT\Logos"); } System.Text.RegularExpressions.Regex filenameRegex = new System.Text.RegularExpressions.Regex("[^a-z^A-Z^0-9]"); string newFileName = string.Format(@"C:\Program Files\InspectIT\Logos\{0}.{1}.bmp", logoPrefix, filenameRegex.Replace(sourceContact.Name, string.Empty)); if (newFileName == sourceContact.PathToLogo) { return; } if (File.Exists(newFileName)) { File.Delete(newFileName); } try { if (Path.GetExtension(sourceContact.PathToLogo).ToUpper().Contains("BMP")) { File.Copy(sourceContact.PathToLogo, newFileName, true); } else { Image convertImage = Image.FromFile(sourceContact.PathToLogo); convertImage.Save(newFileName, System.Drawing.Imaging.ImageFormat.Bmp); convertImage.Dispose(); } sourceContact.PathToLogo = newFileName; } catch (System.IO.IOException ex) { System.Windows.MessageBox.Show(string.Format("Failed to copy logo file to ITpipes program directory. Please verify you have write access to C:\\Program Files\\InspectIT\n{0}", ex.Message)); } }
private void _cloneValuesAcrossAddressBookContacts(AddressBookContact targetContact, AddressBookContact sourceContact, SharedEnums.ContactSourceType newContactSourceType) { //Have to do some convoluted new string gibberish to prevent the object reference being set instead of the value. //There has to be a better way of doing this. I may end up just making addressbookcontacts a struct. *shrug* targetContact.City = new string(sourceContact.City.ToCharArray()); targetContact.ContactEmail = new string(sourceContact.ContactEmail.ToCharArray()); targetContact.ContactFax = new string(sourceContact.ContactFax.ToCharArray()); targetContact.ContactMobileNumber = new string(sourceContact.ContactMobileNumber.ToCharArray()); targetContact.ContactState = new string(sourceContact.ContactState.ToCharArray()); targetContact.ContactType = sourceContact.ContactType; targetContact.Department = new string(sourceContact.Department.ToCharArray()); targetContact.Name = new string(sourceContact.Name.ToCharArray()); targetContact.PathToLogo = new string(sourceContact.PathToLogo.ToCharArray()); targetContact.PhoneNumber = new string(sourceContact.PhoneNumber.ToCharArray()); targetContact.Responsible = new string(sourceContact.Responsible.ToCharArray()); targetContact.Street = new string(sourceContact.Street.ToCharArray()); targetContact.Zipcode = new string(sourceContact.Zipcode.ToCharArray()); targetContact.SourceFileType = newContactSourceType; }
public AddressBookContact Clone() { AddressBookContact returnContact = new AddressBookContact(); returnContact.City = this.City; returnContact.ContactEmail = this.ContactEmail; returnContact.ContactFax = this.ContactFax; returnContact.ContactMobileNumber = this.ContactMobileNumber; returnContact.ContactState = this.ContactState; returnContact.ContactType = this.ContactType; returnContact.Department = this.Department; returnContact.Name = this.Name; returnContact.PathToLogo = this.PathToLogo; returnContact.PhoneNumber = this.PhoneNumber; returnContact.Responsible = this.Responsible; returnContact.Street = this.Street; returnContact.Zipcode = this.Zipcode; //We deliberately DO NOT clone the primary key to the new object. return(returnContact); }
public void Dispose() { if (this.AddressBookContactList != null && AddressBookContactList.Count > 0) { //using a for loop because you can't remove items inside an enumeration of the collection for (int i = AddressBookContactList.Count - 1; i >= 0; i--) { AddressBookContact curContact = AddressBookContactList[i]; curContact.Dispose(); AddressBookContactList.RemoveAt(i); } } if (this.TemplateContacts != null && TemplateContacts.Count > 0) { for (int i = TemplateContacts.Count - 1; i >= 0; i--) { TemplateContact curContact = TemplateContacts[i]; curContact.Dispose(); TemplateContacts.RemoveAt(i); } } if (_templateBW != null) { _templateBW.Dispose(); } if (this.TemplateContacts != null) { foreach (TemplateContact curContact in this.TemplateContacts) { curContact.Dispose(); } this.TemplateContacts = null; } }
public void SaveTemplate(bool copyContactLogoToITpipesDirectory = true) { if (string.IsNullOrEmpty(this.PathToTemplateFile) || string.IsNullOrEmpty(this._pathToTemporarilyExtractedProjectMdb)) { throw new Exception("Cannot save a template whose PathToTemplateFile or _pathToTemporarilyExtractedProjectMdb is null or empty"); } if (File.Exists(this.PathToTemplateFile) == false || File.Exists(this._pathToTemporarilyExtractedProjectMdb) == false) { throw new FileNotFoundException(string.Format("Template file or temporary file not found:\nTemplate: {0}\nTemp File: {1}", this.PathToTemplateFile, this._pathToTemporarilyExtractedProjectMdb)); } using (OleDbConnection curOleConn = new OleDbConnection(this._privateOleConnString)) using (OleDbCommand curOleCommand = curOleConn.CreateCommand()) { curOleConn.Open(); //Need to clear out all contacts which have been removed from the contact list in the template: string pkValuesInContactList = getAllPkValuesFromContactsForUseWithSqlWhereInClause(this.TemplateContacts); if (pkValuesInContactList != "()") { curOleCommand.CommandText = "DELETE FROM `Info` WHERE [Info_ID] NOT IN " + pkValuesInContactList; curOleCommand.ExecuteNonQuery(); } else { curOleCommand.CommandText = "DELETE FROM `Info`"; curOleCommand.ExecuteNonQuery(); } foreach (AddressBookContact curContact in this.TemplateContacts) { if (curContact == null || string.IsNullOrWhiteSpace(curContact.Name) || curContact.ContactHasBeenModified == false) { continue; } AddressBookContact.CopyLogoToITpipesDirectory(curContact, this.TemplateName); if (curContact.SourceRecordIntPK > 0) { AddressBookContact.LoadCommandToUpdateExistingContactRecord(curOleCommand, curContact); } else { AddressBookContact.LoadCommandToInsertNewContactRecord(curOleCommand, curContact); } curOleCommand.ExecuteNonQuery(); if (curContact.SourceRecordIntPK < 1) { curOleCommand.Parameters.Clear(); curOleCommand.CommandText = "SELECT TOP 1 [Info_ID] FROM Info ORDER BY [Info_ID] DESC"; curContact.SourceRecordIntPK = (int)curOleCommand.ExecuteScalar(); } } } string pathToTempMdb = Path.Combine(Path.GetTempPath(), "project.mdb"); if (File.Exists(pathToTempMdb)) { File.Delete(pathToTempMdb); } File.Copy(this._pathToTemporarilyExtractedProjectMdb, pathToTempMdb); Ionic.Zip.ZipEntry projectMdbEntry = null; foreach (var curEntry in TemplateZipObject.Entries) { if (curEntry.FileName == "project.mdb") { projectMdbEntry = curEntry; } } if (projectMdbEntry != null) { TemplateZipObject.RemoveEntry(projectMdbEntry); } projectMdbEntry = null; TemplateZipObject.Encryption = Ionic.Zip.EncryptionAlgorithm.PkzipWeak; TemplateZipObject.AddFile(pathToTempMdb, "\\").Password = "******"; string tempFolderPath = Path.GetTempPath() + Path.GetFileNameWithoutExtension(TemplateZipObject.Name); if (Directory.Exists(tempFolderPath) == false) { Directory.CreateDirectory(tempFolderPath); } TemplateZipObject.Save(); TemplateZipObject.Reset(false); //If you don't reset the zip object, project.mdb becomes corrupt after the second consecutive save--not sure why. }
public static void LoadCommandToUpdateExistingContactRecord(OleDbCommand curOleCommand, AddressBookContact existingContact) { if (curOleCommand == null || string.IsNullOrEmpty(curOleCommand.CommandText) || existingContact == null) { throw new NullReferenceException("_populateUpdateTemplateCommandWithParameters was passed NULL or empty parameters"); } if (existingContact.SourceRecordIntPK < 1) { throw new IndexOutOfRangeException("AddressBookContact passed to _populateUpdateTemplateCommandWithParameters did not have a valid primary key parameter.\n" + "This method should only be called to populate OleDbCommand parameters for AddressBookContacts which already exist in the target template."); } string tableName = null; string pkColumnName = null; if (existingContact.SourceFileType == ContactSourceType.AddressBook) { tableName = "Address"; pkColumnName = "Address_ID"; } else if (existingContact.SourceFileType == ContactSourceType.TemplateFile) { tableName = "Info"; pkColumnName = "Info_ID"; } else { throw new Exception(string.Format("ContactSourceType Enum was not a recognized value: {0}", existingContact.SourceFileType.ToString())); } curOleCommand.CommandText = "UPDATE `" + tableName + "` SET " + "[Contact_Name] = ?, " + "[Contact_Street] = ?, " + "[Contact_City] = ?, " + "[Contact_State] = ?, " + "[Contact_Zip] = ?, " + "[Contact_Phone] = ?, " + "[Logo] = ?, " + "[Category] = ?, " + "[Department] = ?, " + "[Contact_Email] = ?, " + "[Contact_Fax] = ?, " + "[Contact_Mobile] = ?, " + "[Responsible] = ? " + "WHERE [" + pkColumnName + "] = ?;"; curOleCommand.Parameters.Clear(); curOleCommand.Parameters.AddWithValue("Contact_Name", existingContact.Name); curOleCommand.Parameters.AddWithValue("Contact_Street", existingContact.Street); curOleCommand.Parameters.AddWithValue("Contact_City", existingContact.City); curOleCommand.Parameters.AddWithValue("Contact_State", existingContact.ContactState); curOleCommand.Parameters.AddWithValue("Contact_Zip", existingContact.Zipcode); curOleCommand.Parameters.AddWithValue("Contact_Phone", existingContact.PhoneNumber); //The logo files are stored in the ITpipes program directory, and have to be referenced as a relative path--learned that when the logos weren't working. :-\ curOleCommand.Parameters.AddWithValue("Logo", existingContact.PathToLogo == null ? (object)DBNull.Value : string.Format(@"Logos\{0}", Path.GetFileName(existingContact.PathToLogo))); curOleCommand.Parameters.AddWithValue("Category", getContactTypeStringFromEnum(existingContact.ContactType)); curOleCommand.Parameters.AddWithValue("Department", existingContact.Department); curOleCommand.Parameters.AddWithValue("Contact_Email", existingContact.ContactEmail); curOleCommand.Parameters.AddWithValue("Contact_Fax", existingContact.ContactFax); curOleCommand.Parameters.AddWithValue("Contact_Mobile", existingContact.ContactMobileNumber); curOleCommand.Parameters.AddWithValue("Responsible", existingContact.Responsible); curOleCommand.Parameters.AddWithValue("pkColumnName", existingContact.SourceRecordIntPK); }
public static void LoadCommandToInsertNewContactRecord(OleDbCommand curOleCommand, AddressBookContact newContact) { if (curOleCommand == null || string.IsNullOrEmpty(curOleCommand.CommandText) || newContact == null) { throw new NullReferenceException("_loadCommandToInsertNewContactRecord was passed NULL or empty parameters"); } if (newContact.SourceRecordIntPK > 0) { throw new Exception("_loadCommandToInsertNewContactRecord was passed an address book record which already has a primary key assigned. " + "This method should only be called to populate OleDbCommand parameters for AddressBookContacts which do not yet exist in the target template."); } string tableName = null; if (newContact.SourceFileType == ContactSourceType.AddressBook) { tableName = "Address"; } else if (newContact.SourceFileType == ContactSourceType.TemplateFile) { tableName = "Info"; } else { throw new Exception(string.Format("ContactSourceType Enum was not a recognized value: {0}", newContact.SourceFileType.ToString())); } curOleCommand.CommandText = "INSERT INTO `" + tableName + "` (" + "[Contact_Name], " + "[Contact_Street], " + "[Contact_City], " + "[Contact_State], " + "[Contact_Zip], " + "[Contact_Phone], " + "[Logo], " + "[Category], " + "[Department], " + "[Contact_Email], " + "[Contact_Fax], " + "[Contact_Mobile], " + "[Responsible]) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; curOleCommand.Parameters.Clear(); curOleCommand.Parameters.AddWithValue("Contact_Name", newContact.Name); curOleCommand.Parameters.AddWithValue("Contact_Street", newContact.Street); curOleCommand.Parameters.AddWithValue("Contact_City", newContact.City); curOleCommand.Parameters.AddWithValue("Contact_State", newContact.ContactState); curOleCommand.Parameters.AddWithValue("Contact_Zip", newContact.Zipcode); curOleCommand.Parameters.AddWithValue("Contact_Phone", newContact.PhoneNumber); curOleCommand.Parameters.AddWithValue("Logo", newContact.PathToLogo == null ? (object)DBNull.Value : string.Format(@"Logos\{0}", Path.GetFileName(newContact.PathToLogo))); curOleCommand.Parameters.AddWithValue("Category", getContactTypeStringFromEnum(newContact.ContactType)); curOleCommand.Parameters.AddWithValue("Department", newContact.Department); curOleCommand.Parameters.AddWithValue("Contact_Email", newContact.ContactEmail); curOleCommand.Parameters.AddWithValue("Contact_Fax", newContact.ContactEmail); curOleCommand.Parameters.AddWithValue("Contact_Mobile", newContact.ContactMobileNumber); curOleCommand.Parameters.AddWithValue("Responsible", newContact.Responsible); }