Skip to content

Commit

Permalink
Refs #3 - attempt to add functionaltiy for addresses
Browse files Browse the repository at this point in the history
It does not work, because now AddressesFind and ContactFind both return a
ResultSet, of which the member Result has a distinct type.

Note: If you want to add an address, you must provide a LocationTypeId.
  • Loading branch information
Johan Vervloet committed Sep 16, 2013
1 parent 0f59825 commit 4ed6656
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 2 deletions.
28 changes: 28 additions & 0 deletions Chiro.CiviCrm.Client/CiviCrmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ You may obtain a copy of the License at
limitations under the License.
*/

using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Chiro.CiviCrm.ClientInterfaces;
Expand Down Expand Up @@ -70,5 +71,32 @@ public void ContactSave(Contact contact)
contact.ContactType, contact.BirthDate, contact.DeceasedDate, contact.IsDeceased, contact.Gender,
contact.GenderId);
}

/// <summary>
/// Retrieves the addresses for the contact with given <paramref name="externalId"/>.
/// </summary>
/// <param name="externalId">EXTERNAL ID of the contact whose addresses are to be retrieved</param>
/// <returns>List of addresses</returns>
public List<Address> AddressesFind(int externalId)
{
var contact = Channel.ContactFind(_apiKey, _key, externalId).Contacts.FirstOrDefault();

if (contact == null)
{
return null;
}
return Channel.ContactAddressesFind(_apiKey, _key, contact.Id).Adresses.ToList();
}

/// <summary>
/// Creates a new address, or updates an existing address.
/// </summary>
/// <param name="address">Address to be updated (when Id != 0) or saved (when Id == 0).</param>
public void AddressSave(Address address)
{
Channel.AddressSave(_apiKey, _key, address.Id, address.ContactId, address.LocationTypeId, address.IsPrimary,
address.IsBilling, address.StreetAddress, address.City, address.StateProvinceId, address.PostalCode,
address.PostalCodeSuffix, address.CountryId);
}
}
}
14 changes: 14 additions & 0 deletions Chiro.CiviCrm.ClientInterfaces/ICiviCrmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

using System;
using System.Collections.Generic;
using Chiro.CiviCrm.ServiceContracts.DataContracts;

namespace Chiro.CiviCrm.ClientInterfaces
Expand Down Expand Up @@ -45,5 +46,18 @@ public interface ICiviCrmClient: IDisposable
/// <remarks>If the contact's ID is 0, it will be saved. If it differs from 0, the existing contact with the
/// given ID will be updated.</remarks>
void ContactSave(Contact contact);

/// <summary>
/// Retrieves the addresses for the contact with given <paramref name="externalId"/>.
/// </summary>
/// <param name="externalId">EXTERNAL ID of the contact whose addresses are to be retrieved</param>
/// <returns>List of addresses</returns>
List<Address> AddressesFind(int externalId);

/// <summary>
/// Creates a new address, or updates an existing address.
/// </summary>
/// <param name="address">Address to be updated (when Id != 0) or saved (when Id == 0).</param>
void AddressSave(Address address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataContracts\Address.cs" />
<Compile Include="DataContracts\Contact.cs" />
<Compile Include="DataContracts\ContactSet.cs" />
<Compile Include="DataContracts\AddressSet.cs" />
<Compile Include="DataContracts\Enums.cs" />
<Compile Include="ICiviCrmApi.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
56 changes: 56 additions & 0 deletions Chiro.CiviCrm.ServiceContracts/DataContracts/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2013 Chirojeugd-Vlaanderen vzw
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System.Xml.Serialization;

namespace Chiro.CiviCrm.ServiceContracts.DataContracts
{
public class Address
{
[XmlElement("id")]
public int Id { get; set; }

[XmlElement("contact_id")]
public int ContactId { get; set; }

[XmlElement("location_type_id")]
public int LocationTypeId { get; set; }

[XmlElement("is_primary")]
public bool IsPrimary { get; set; }

[XmlElement("is_billing")]
public bool IsBilling { get; set; }

[XmlElement("street_address")]
public string StreetAddress { get; set; }

[XmlElement("city")]
public string City { get; set; }

[XmlElement("state_province_id")]
public int StateProvinceId { get; set; }

[XmlElement("postal_code")]
public int PostalCode { get; set; }

[XmlElement("postal_code_suffix")]
public string PostalCodeSuffix { get; set; }

[XmlElement("country_id")]
public int CountryId { get; set; }
}
}
27 changes: 27 additions & 0 deletions Chiro.CiviCrm.ServiceContracts/DataContracts/AddressSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2013 Chirojeugd-Vlaanderen vzw
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System.Xml.Serialization;

namespace Chiro.CiviCrm.ServiceContracts.DataContracts
{
[XmlRoot(ElementName = "ResultSet")]
public class AddressSet
{
[XmlElement("Result")]
public Address[] Adresses { get; set; }
}
}
10 changes: 9 additions & 1 deletion Chiro.CiviCrm.ServiceContracts/DataContracts/Contact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ limitations under the License.
*/

using System;
using System.Security;
using System.Xml;
using System.Xml.Serialization;

namespace Chiro.CiviCrm.ServiceContracts.DataContracts
{
public class Contact
{
#region Ugly hack

// I need this to handle null DateTimes returned by the CiviCRM API.
// Maybe this can be avoided using a client side custom MessageFormatter.
// If not, these 'DateStrings' should not be visible to the user of
// Chiro.CiviCrm.Client.

[XmlElement("birth_date")]
public string BirthDateString
{
Expand Down Expand Up @@ -57,6 +63,8 @@ public string DeceasedDateString
}
}

#endregion

[XmlElement("contact_id")]
public int Id { get; set; }

Expand Down
22 changes: 22 additions & 0 deletions Chiro.CiviCrm.ServiceContracts/ICiviCrmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,27 @@ public interface ICiviCrmApi: IDisposable
[WebInvoke(RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate =
"?api_key={apiKey}&key={key}&debug=1&version=3&entity=Contact&action=create&contact_type={contactType}&contact_id={id}&first_name={firstName}&last_name={lastName}&external_identifier={externalID}&birth_date={birthDate}&deceased_date={deceasedDate}&is_deceased={isDeceased}&gender={gender}&gender_id={genderId}")]
void ContactSave(string apiKey, string key, int id, string firstName, string lastName, int externalId, ContactType contactType, DateTime? birthDate, DateTime? deceasedDate, bool isDeceased, Gender gender, int genderId);

/// <summary>
/// Find the adresses of a contact with given <paramref name="contactId"/>.
/// </summary>
/// <param name="apiKey">API-key of the API user</param>
/// <param name="key">Key of the CiviCRM installation</param>
/// <param name="contactId">ID of the contact whose addresses are requested</param>
/// <returns>List of addresses</returns>
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml,
UriTemplate =
"?api_key={apiKey}&key={key}&debug=1&version=3&entity=Address&action=get&contact_id={contactId}")]
AddressSet ContactAddressesFind(string apiKey, string key, int contactId);


[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate =
"?api_key={apiKey}&key={key}&debug=1&version=3&entity=Address&action=create&id={Id}&contact_id={contactId}&location_type_id={locationTypeId}&is_primary={isPrimary}&is_billing={isBilling}&street_address={streetAddress}&city={city}&state_province_id={stateProvinceId}&postal_code={postalCode}&postal_code_suffix={postalCodeSuffix}&country_id={CountryId}"
)]
void AddressSave(string apiKey, string key, int id, int contactId, int locationTypeId, bool isPrimary,
bool isBilling, string streetAddress, string city, int stateProvinceId, int postalCode,
string postalCodeSuffix, int countryId);
}
}
24 changes: 23 additions & 1 deletion Chiro.CiviCrm.Wcf.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
using System;
using Chiro.CiviCrm.Client;
using Chiro.CiviCrm.ClientInterfaces;
using Chiro.CiviCrm.ServiceContracts.DataContracts;

namespace Chiro.CiviCrm.Wcf.Example
{
Expand All @@ -41,14 +42,35 @@ static void Main(string[] args)
}
else
{
Console.WriteLine("Found: {0} {1}", contact.FirstName, contact.LastName);
Console.WriteLine("Found: {0} {1}; id: {2}", contact.FirstName, contact.LastName, contact.Id);

// retrieve addresses

foreach (var address in client.AddressesFind(contact.ExternalId))
{
Console.WriteLine("{0}, {1} {2} {3}", address.StreetAddress, address.PostalCode, address.PostalCodeSuffix, address.City);
}

// change the name of the contact.

contact.FirstName = "Jos";
contact.BirthDate = new DateTime(1990,4,3);

client.ContactSave(contact);

// add address

client.AddressSave(new Address
{
Id = 0,
ContactId = contact.Id,
StreetAddress = "Kipdorp 130",
PostalCode = 2000,
City = "Antwerpen",
StateProvinceId = 1785,
CountryId = 1020,
LocationTypeId = 1
});
}

Console.WriteLine("Press enter.");
Expand Down

0 comments on commit 4ed6656

Please sign in to comment.