/// <summary> /// Update a patron entity and associated contacts /// </summary> /// <param name="patronDetailsForm">The form containing the updated patron and contacts details</param> /// <param name="existingPatron">The original patron entity</param> /// <param name="contacts">The original contact entities</param> /// <returns>success, message</returns> private async Task <Tuple <bool, string> > updatePatron(PatronDetailsForm patronDetailsForm, patron existingPatron, List <contact> contacts) { string patronUri = rootURI + "patrons/" + existingPatron.identifier; if (patronDetailsForm.PatronName.CompareTo(existingPatron.name) != 0) { existingPatron.name = patronDetailsForm.PatronName; Tuple <bool, object, string> ret = await lcfEntityRequestAsync(patronUri, typeof(patron), entityOperation.PUT, existingPatron); if (!ret.Item1) { return(new Tuple <bool, string>(false, String.Format("Problem updating patron '{0}' - {1}", existingPatron.identifier, ret.Item3))); } } string[] locator = new string[] { patronDetailsForm.PatronEmail }; Tuple <bool, string> updateContactRet = await updateContact(communicationType.Item05, locator, contacts, patronUri); if (updateContactRet.Item1) { locator = new string[] { patronDetailsForm.PatronHomePhone }; updateContactRet = await updateContact(communicationType.Item02, locator, contacts, patronUri); } if (updateContactRet.Item1) { locator = new string[] { patronDetailsForm.PatronBusinessPhone }; updateContactRet = await updateContact(communicationType.Item03, locator, contacts, patronUri); } if (updateContactRet.Item1) { locator = new string[] { patronDetailsForm.PatronMobilePhone }; updateContactRet = await updateContact(communicationType.Item04, locator, contacts, patronUri); } if (updateContactRet.Item1) { locator = new string[] { patronDetailsForm.PatronOtherPhone }; updateContactRet = await updateContact(communicationType.Item01, locator, contacts, patronUri); } if (updateContactRet.Item1) { updateContactRet = await updateContact(communicationType.Item06, patronDetailsForm.PatronAddress, contacts, patronUri); } if (!updateContactRet.Item1) { return(new Tuple <bool, string>(false, String.Format("Problem updating patron '{0}' - {1}", existingPatron.identifier, updateContactRet.Item2))); } else { return(new Tuple <bool, string>(true, String.Format("Patron '{0}' updated", existingPatron.identifier))); } }
/// <summary> /// Create a new patron entity and associated contacts /// </summary> /// <param name="patronDetailsForm">The form containing the patron and contacts details</param> /// <returns>success, message</returns> private async Task <Tuple <bool, string> > createPatron(PatronDetailsForm patronDetailsForm) { bool success = true; patron p = new patron(); p.identifier = patronDetailsForm.PatronIdentifier; p.name = patronDetailsForm.PatronName; p.language = languageCode.eng; p.associatedlocation = new associatedlocation[1]; p.associatedlocation[0] = new associatedlocation(); p.associatedlocation[0].associationtype = locationAssociationType.Item03; p.associatedlocation[0].locationref = new string[1]; p.associatedlocation[0].locationref[0] = "http://localhost:8080/TalisSOA-2.1.0-SNAPSHOT/protocols/lcf/1.0/locations/GA"; Tuple <bool, object, string> ret = await lcfEntityRequestAsync(rootURI + "patrons", typeof(patron), entityOperation.POST, p); if (!ret.Item1) { return(new Tuple <bool, string>(false, String.Format("Problem creating patron '{0}' - {1}", p.identifier, ret.Item3))); } var newPatron = ret.Item2 as patron; var newPatronLocationUri = ret.Item3; // create contacts if (newPatronLocationUri.Length != 0 && (patronDetailsForm.PatronAddress.Length != 0 || patronDetailsForm.PatronBusinessPhone.Length != 0 || patronDetailsForm.PatronEmail.Length != 0 || patronDetailsForm.PatronHomePhone.Length != 0 || patronDetailsForm.PatronMobilePhone.Length != 0)) { contact c; if (patronDetailsForm.PatronHomePhone.Length != 0) { c = new contact(); c.patronref = newPatronLocationUri; c.locator = new string[1]; c.locator[0] = patronDetailsForm.PatronHomePhone; c.communicationtype = communicationType.Item02; ret = await lcfEntityRequestAsync(rootURI + "contacts", typeof(contact), entityOperation.POST, c); success = ret.Item1; } if (success && patronDetailsForm.PatronBusinessPhone.Length != 0) { c = new contact(); c.patronref = newPatronLocationUri; c.locator = new string[1]; c.locator[0] = patronDetailsForm.PatronBusinessPhone; c.communicationtype = communicationType.Item03; ret = await lcfEntityRequestAsync(rootURI + "contacts", typeof(contact), entityOperation.POST, c); success = ret.Item1; } if (success && patronDetailsForm.PatronMobilePhone.Length != 0) { c = new contact(); c.patronref = newPatronLocationUri; c.locator = new string[1]; c.locator[0] = patronDetailsForm.PatronMobilePhone; c.communicationtype = communicationType.Item04; ret = await lcfEntityRequestAsync(rootURI + "contacts", typeof(contact), entityOperation.POST, c); success = ret.Item1; } if (success && patronDetailsForm.PatronOtherPhone.Length != 0) { c = new contact(); c.patronref = newPatronLocationUri; c.locator = new string[1]; c.locator[0] = patronDetailsForm.PatronOtherPhone; c.communicationtype = communicationType.Item01; ret = await lcfEntityRequestAsync(rootURI + "contacts", typeof(contact), entityOperation.POST, c); success = ret.Item1; } if (success && patronDetailsForm.PatronEmail.Length != 0) { c = new contact(); c.patronref = newPatronLocationUri; c.locator = new string[1]; c.locator[0] = patronDetailsForm.PatronEmail; c.communicationtype = communicationType.Item05; ret = await lcfEntityRequestAsync(rootURI + "contacts", typeof(contact), entityOperation.POST, c); success = ret.Item1; } if (success && patronDetailsForm.PatronAddress.Length != 0) { c = new contact(); c.patronref = newPatronLocationUri; c.communicationtype = communicationType.Item06; c.locator = new string[patronDetailsForm.PatronAddress.Length]; for (int i = 0; i < patronDetailsForm.PatronAddress.Length; i++) { c.locator[i] = patronDetailsForm.PatronAddress[i]; } ret = await lcfEntityRequestAsync(rootURI + "contacts", typeof(contact), entityOperation.POST, c); success = ret.Item1; } if (!success) { return(new Tuple <bool, string>(false, String.Format("Problem creating contact for patron '{0}' - {1}", p.identifier, ret.Item3))); } } return(new Tuple <bool, string>(true, String.Format("Patron '{0}' created", newPatron.identifier))); }
/// <summary> /// Display details of a patron and contacts for creation or updating purposes /// </summary> /// <param name="identifier">The identifier - may be blank when creating a new patron</param> /// <param name="create">Set to true when creating a patron</param> /// <returns>success, message</returns> private async Task <Tuple <bool, string> > displayPatronDetails(string identifier, bool create = false) { string uri = rootURI + "patrons/" + identifier; PatronDetailsForm patronDetailsForm = new PatronDetailsForm(); patron existingPatron = null; List <contact> contacts = new List <contact>(); Tuple <bool, object, string> ret; if (!create || !String.IsNullOrEmpty(identifier)) { // look for existing patron ret = await lcfEntityRequestAsync(uri, typeof(patron), entityOperation.GET); if (!ret.Item1 && !create) { return(new Tuple <bool, string>(false, String.Format("Problem finding patron '{0}' - {1}", identifier, ret.Item3))); } existingPatron = ret.Item2 as patron; if (existingPatron != null) { if (create) { return(new Tuple <bool, string>(false, String.Format("Patron '{0}' already exists", identifier))); } patronDetailsForm.PatronIdentifier = existingPatron.identifier; patronDetailsForm.PatronName = existingPatron.name; if (existingPatron.contactref != null) { foreach (string contactRef in existingPatron.contactref) { ret = await lcfEntityRequestAsync(contactRef, typeof(contact), entityOperation.GET); if (ret.Item1) { var contactRsp = ret.Item2 as contact; contacts.Add(contactRsp); } } contact contactMethod; if (null != (contactMethod = contacts.FirstOrDefault(s => s.communicationtype == communicationType.Item05))) { patronDetailsForm.PatronEmail = contactMethod.locator[0]; } if (null != (contactMethod = contacts.FirstOrDefault(s => s.communicationtype == communicationType.Item02))) { patronDetailsForm.PatronHomePhone = contactMethod.locator[0]; } if (null != (contactMethod = contacts.FirstOrDefault(s => s.communicationtype == communicationType.Item03))) { patronDetailsForm.PatronBusinessPhone = contactMethod.locator[0]; } if (null != (contactMethod = contacts.FirstOrDefault(s => s.communicationtype == communicationType.Item04))) { patronDetailsForm.PatronMobilePhone = contactMethod.locator[0]; } if (null != (contactMethod = contacts.FirstOrDefault(s => s.communicationtype == communicationType.Item01))) { patronDetailsForm.PatronOtherPhone = contactMethod.locator[0]; } if (null != (contactMethod = contacts.FirstOrDefault(s => s.communicationtype == communicationType.Item06))) { patronDetailsForm.PatronAddress = contactMethod.locator; } } } else { patronDetailsForm.PatronIdentifier = identifier; } } else { patronDetailsForm.PatronIdentifier = String.Empty; } if (existingPatron != null || create) { patronDetailsForm.ShowDialog(this); if (patronDetailsForm.DialogResult == DialogResult.OK) { // update details if (create) { // create patron Tuple <bool, string> createRet = await createPatron(patronDetailsForm); if (createRet.Item1) { return(new Tuple <bool, string>(true, createRet.Item2)); } else { return(new Tuple <bool, string>(false, createRet.Item2)); } } else { // update patron Tuple <bool, string> updateRet = await updatePatron(patronDetailsForm, existingPatron, contacts); if (updateRet.Item1) { return(new Tuple <bool, string>(true, updateRet.Item2)); } else { return(new Tuple <bool, string>(false, updateRet.Item2)); } } } } return(new Tuple <bool, string>(true, null)); }