/// <summary>
        /// Sets the address value.
        /// </summary>
        /// <param name="serviceAddress">The service address.</param>
        /// <param name="controlAddress">The control address.</param>
        private void SetAddressValue(SrvAddress serviceAddress, Address controlAddress)
        {
            if (controlAddress != null)
            {
                serviceAddress.AddressTypeId = (int)(DataConstants.SystemAddressTypes.Main);
                serviceAddress.AddressStreetNumber = controlAddress.StreetNumber;
                serviceAddress.AddressPostCode = controlAddress.PostCode;
                serviceAddress.AddressHouseName = controlAddress.HouseName;
                serviceAddress.AddressLine1 = controlAddress.Line1;
                serviceAddress.AddressLine2 = controlAddress.Line2;
                serviceAddress.AddressLine3 = controlAddress.Line3;
                serviceAddress.AddressTown = controlAddress.Town;
                serviceAddress.AddressCounty = controlAddress.County;
                serviceAddress.AddressCountry = controlAddress.Country;
                serviceAddress.AddressDXTown = controlAddress.DXTown;
                serviceAddress.AddressDXNumber = controlAddress.DXNumber;
                serviceAddress.IsMailingAddress = controlAddress.IsMailingAddress;
                serviceAddress.IsBillingAddress = controlAddress.IsBillingAddress;

                serviceAddress.AddressOrgName = controlAddress.OrganisationName;
                serviceAddress.AddressComment = controlAddress.Comment;
                serviceAddress.AddressDepartment = controlAddress.Department;
                serviceAddress.AddressPostBox = controlAddress.PostBox;
                serviceAddress.AddressSubBuilding = controlAddress.SubBuilding;
                serviceAddress.AddressStreetNumber = controlAddress.StreetNumber;
                serviceAddress.AddressDependantLocality = controlAddress.DependantLocality;
                serviceAddress.AddressLastVerified = controlAddress.LastVerified;
            }
        }
        /// <summary>
        /// Get the address for the specified contact  
        /// </summary>
        /// <param name="logonId"></param>
        /// <param name="collectionRequest"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public AddressSearchReturnValue GetContactAddresses(Guid logonId, CollectionRequest collectionRequest,
                                        AddressSearchCriteria criteria)
        {
            AddressSearchReturnValue returnValue = new AddressSearchReturnValue();
            SrvAddress srvAddress;

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            if (!ApplicationSettings.Instance.IsUser(criteria.MemberId, criteria.OrganisationId))
                                throw new Exception("Access denied");
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    DataListCreator<Address> dataListCreator = new DataListCreator<Address>();

                    // Declare an inline event (annonymous delegate) to read the
                    // dataset if it is required
                    dataListCreator.ReadDataSet += delegate(object Sender, ReadDataSetEventArgs e)
                    {
                        if (criteria.MemberId != DataConstants.DummyGuid)
                        {
                            e.DataSet = SrvAddressLookup.GetMemberAddresses(criteria.MemberId);
                        }
                        else if (criteria.OrganisationId != DataConstants.DummyGuid)
                        {
                            e.DataSet = SrvAddressLookup.GetOrganisationAddresses(criteria.OrganisationId);
                        }
                    };

                    // Create the data list
                    returnValue.Addresses = dataListCreator.Create(logonId,
                        // Give the query a name so it can be cached
                        "ClientAddresses",
                        // Tell it the query criteria used so if the cache is accessed
                        // again it knows if it is the same query
                        criteria.ToString(),
                        collectionRequest,
                        // Import mappings to map the dataset row fields to the data
                        // list entity properties
                        new ImportMapping[] {
                            new ImportMapping("Id", "AddressId"),
                            new ImportMapping("TypeId", "AddressTypeID"),
                            new ImportMapping("Line1", "AddressLine1"),
                            new ImportMapping("Line2", "AddressLine2"),
                            new ImportMapping("Line3", "AddressLine3"),
                            new ImportMapping("Town", "AddressTown"),
                            new ImportMapping("County", "AddressCounty"),
                            new ImportMapping("PostCode", "AddressPostCode"),
                            new ImportMapping("DXTown", "AddressDXTown"),
                            new ImportMapping("DXNumber", "AddressDX"),
                            new ImportMapping("Country", "AddressCountry"),
                            new ImportMapping("IsMailingAddress", "AddressMailingAddress"),
                            new ImportMapping("IsBillingAddress", "AddressBillingAddress"),
                            new ImportMapping("Comment", "AddressComment"),
                            new ImportMapping("OrganisationName", "AddressOrgName"),
                            new ImportMapping("Department", "AddressDept"),
                            new ImportMapping("PostBox", "AddressPoBox"),
                            new ImportMapping("SubBuilding", "AddressSubBldg"),
                            new ImportMapping("StreetNumber", "AddressStreetNo"),
                            new ImportMapping("HouseName", "AddressHouseName"),
                            new ImportMapping("DependantLocality", "AddressDepLocality"),
                            new ImportMapping("LastVerified", "AddressLastVerified"),
                            }
                        );

                    int intCtr;
                    foreach (Address address in returnValue.Addresses.Rows)
                    {
                        srvAddress = new SrvAddress();
                        srvAddress.MemberId = criteria.MemberId;
                        srvAddress.OrganisationId = criteria.OrganisationId;
                        srvAddress.Load(address.Id);
                        address.AdditionalAddressElements = new List<AdditionalAddressElement>();
                        for (intCtr = 0; intCtr < srvAddress.AdditionalInfoElements.Count; intCtr++)
                        {
                            AdditionalAddressElement additionalAddressElement = new AdditionalAddressElement();
                            additionalAddressElement.ElementComment = srvAddress.AdditionalInfoElements[intCtr].AddressElComment;
                            additionalAddressElement.ElementText = srvAddress.AdditionalInfoElements[intCtr].AddressElementText;
                            additionalAddressElement.TypeId = srvAddress.AdditionalInfoElements[intCtr].AddressElTypeId;
                            address.AdditionalAddressElements.Add(additionalAddressElement);
                        }
                    }
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception Ex)
            {
                returnValue.Success = false;
                returnValue.Message = Ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Adds a new service.
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service.</param>
        /// <param name="serviceAddress">The service address.</param>
        /// <param name="serviceAdditionalElement">The service additional element.</param>
        /// <param name="serviceInfo">The service info.</param>
        /// <param name="serviceContactInfo">The service contact info.</param>
        /// <param name="serviceContactAddress">The service contact address.</param>
        /// <param name="ServiceContactAdditionalElement">The service contact additional element.</param>
        /// <param name="conflictNoteSummary">The conflict note summary.</param>
        /// <param name="conflictNoteContent">Content of the conflict note.</param>
        /// <returns></returns>
        public ReturnValue SaveService(Guid logonId, Address serviceAddress,
                                       AdditionalAddressElement[] serviceAdditionalElement,
                                       ServiceInfo serviceInfo,
                                       ServiceContact serviceContactInfo,
                                       Address serviceContactAddress,
                                       AdditionalAddressElement[] ServiceContactAdditionalElement,
                                       string conflictNoteSummary,
                                       string conflictNoteContent)
        {
            ReturnValue returnValue = new ReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            if (!ApplicationSettings.Instance.IsUser(serviceAdditionalElement[0].MemberId, serviceAdditionalElement[0].OrganisationId))
                                throw new Exception("Access denied");
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    SrvService service = new SrvService();
                    SrvAddress srvAddress = new SrvAddress();

                    this.SetAddressValue(srvAddress, serviceAddress);

                    // Save Additional Address Info to Address Object
                    if (serviceAdditionalElement != null)
                    {
                        for (int i = 0; i <= 9; i++)
                        {
                            srvAddress.AdditionalInfoElements[i].AddressElementText = serviceAdditionalElement[i].ElementText;
                        }
                    }
                    service.Addresses.Add(srvAddress);
                    service.Name = serviceInfo.ServiceName;
                    service.NetId = SrvServiceCommon.GenerateNumericPassword(6);
                    service.NetPassword = SrvServiceCommon.GenerateStringPassword(10);
                    service.Organisation.IndustryId = serviceInfo.IndustryId;

                    service.Addresses[0].AddressTypeId = Convert.ToInt32(DataConstants.SystemAddressTypes.Main);
                    service.Addresses[0].MemberId = DataConstants.DummyGuid;
                    service.Addresses[0].OrganisationId = DataConstants.DummyGuid;

                    service.ServiceContact[0].Description = serviceContactInfo.Description;
                    service.ServiceContact[0].Position = serviceContactInfo.Position;
                    service.ServiceContact[0].Person.ForeName = serviceContactInfo.ForeName;
                    service.ServiceContact[0].Person.Surname = serviceContactInfo.SurName;
                    service.ServiceContact[0].Person.Title = serviceContactInfo.Title;
                    service.ServiceContact[0].Person.Sex = serviceContactInfo.Sex;
                    service.ServiceContact[0].ConflictNoteContent = conflictNoteContent;
                    service.ServiceContact[0].ConflictNoteSummary = conflictNoteSummary;

                    SrvAddress contactAddress = new SrvAddress();
                    this.SetAddressValue(contactAddress, serviceContactAddress);

                    // Save Additional Address Info to Address Object
                    if (ServiceContactAdditionalElement != null)
                    {
                        for (int i = 0; i <= 9; i++)
                        {
                            contactAddress.AdditionalInfoElements[i].AddressElementText = ServiceContactAdditionalElement[i].ElementText;
                        }
                    }

                    service.ServiceContact[0].Addresses.Add(contactAddress);
                    service.ServiceContact[0].Addresses[0].MemberId = DataConstants.DummyGuid;
                    service.ServiceContact[0].Addresses[0].OrganisationId = DataConstants.DummyGuid;

                    string errorMessage;

                    returnValue.Success = service.Save(out errorMessage);
                    returnValue.Message = errorMessage;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Saves the service contact.
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service.</param>
        /// <param name="contactAddress">The contact address.</param>
        /// <param name="additionalElement">The additional element.</param>
        /// <param name="serviceContact">The service contact.</param>
        /// <param name="conflictNoteSummary">The conflict note summary.</param>
        /// <param name="conflictNoteContent">Content of the conflict note.</param>
        /// <returns></returns>
        public ReturnValue SaveServiceContact(Guid logonId, Address contactAddress,
                                       AdditionalAddressElement[] additionalElement,
                                       ServiceContact serviceContact,
                                       string conflictNoteSummary,
                                       string conflictNoteContent)
        {
            ReturnValue returnValue = new ReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            if (!ApplicationSettings.Instance.IsUser(additionalElement[0].MemberId, additionalElement[0].OrganisationId))
                                throw new Exception("Access denied");
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    SrvServiceContact serviceContactSrv = new SrvServiceContact();
                    SrvAddress srvAddress = new SrvAddress();

                    this.SetAddressValue(srvAddress, contactAddress);

                    // Save Additional Address Info to Address Object
                    if (additionalElement != null)
                    {
                        for (int i = 0; i <= 9; i++)
                        {
                            srvAddress.AdditionalInfoElements[i].AddressElementText = additionalElement[i].ElementText;
                        }
                    }
                    serviceContactSrv.Addresses.Add(srvAddress);

                    serviceContactSrv.ServiceId = serviceContact.ServiceId;
                    serviceContactSrv.Person.ForeName = serviceContact.ForeName;
                    serviceContactSrv.Person.Surname = serviceContact.SurName;
                    serviceContactSrv.Person.Title = serviceContact.Title;
                    serviceContactSrv.Position = serviceContact.Position;
                    serviceContactSrv.Person.Sex = serviceContact.Sex;

                    //These two notes fields are mandatory and have defined
                    //default values
                    serviceContactSrv.ConflictNoteSummary = conflictNoteSummary;
                    serviceContactSrv.ConflictNoteContent = conflictNoteContent;

                    string errorMessage;

                    returnValue.Success = serviceContactSrv.Save(out errorMessage);
                    returnValue.Message = errorMessage;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Saves the address for the contact.
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service.</param>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public AddressReturnValue SaveAddress(Guid logonId, Address address)
        {
            AddressReturnValue returnValue = new AddressReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            if (!ApplicationSettings.Instance.IsUser(address.MemberId, address.OrganisationId))
                                throw new Exception("Access denied");
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    SrvAddress srvAddress = new SrvAddress();
                    if (address.Id > 0)
                    {
                        srvAddress.Load(address.Id);
                    }

                    srvAddress.MemberId = address.MemberId;
                    srvAddress.OrganisationId = address.OrganisationId;
                    srvAddress.AddressTypeId = address.TypeId;
                    srvAddress.AddressId = address.Id;
                    srvAddress.AddressStreetNumber = address.StreetNumber;
                    srvAddress.AddressPostCode = address.PostCode;
                    srvAddress.AddressHouseName = address.HouseName;
                    srvAddress.AddressLine1 = address.Line1;
                    srvAddress.AddressLine2 = address.Line2;
                    srvAddress.AddressLine3 = address.Line3;
                    srvAddress.AddressTown = address.Town;
                    srvAddress.AddressCounty = address.County;
                    srvAddress.AddressCountry = address.Country;
                    srvAddress.AddressDXTown = address.DXTown;
                    srvAddress.AddressDXNumber = address.DXNumber;
                    srvAddress.IsMailingAddress = address.IsMailingAddress;
                    srvAddress.IsBillingAddress = address.IsBillingAddress;
                    srvAddress.AddressOrgName = address.OrganisationName;
                    srvAddress.AddressComment = address.Comment;
                    srvAddress.AddressDepartment = address.Department;
                    srvAddress.AddressPostBox = address.PostBox;
                    srvAddress.AddressSubBuilding = address.SubBuilding;
                    srvAddress.AddressDependantLocality = address.DependantLocality;
                    srvAddress.AddressLastVerified = address.LastVerified;

                    string errorMessage;

                    returnValue.Success = srvAddress.Save(out errorMessage);
                    returnValue.Message = errorMessage;

                    address.Id = srvAddress.AddressId;
                    returnValue.Address = address;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// This method adds the Person, Address and Additional Address information
        /// using contact service to create a general contact.
        /// </summary>
        /// <param name="logonId">User logon ID</param>
        /// <param name="contactAddress">Address information for contact</param>
        /// <param name="person">Person information</param>
        /// <param name="additionalElement">Additional Element</param>
        /// <param name="contactType">Individual / Organisation</param>
        /// <param name="organisation">Organisation Information</param>
        /// <param name="conflictNoteSummary">The conflict note summary.</param>
        /// <param name="conflictNoteContent">Content of the conflict note.</param>
        /// <returns>Return Value</returns>
        public ReturnValue SaveGeneralContact(Guid logonId,
                                              Address contactAddress,
                                              Person person,
                                              AdditionalAddressElement[] additionalElement,
                                              IRISLegal.IlbCommon.ContactType contactType,
                                              Organisation organisation,
                                              string conflictNoteSummary,
                                              string conflictNoteContent)
        {
            ReturnValue returnValue = new ReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            if (!ApplicationSettings.Instance.IsUser(additionalElement[0].MemberId, additionalElement[0].OrganisationId))
                                throw new Exception("Access denied");
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    SrvContact contactService = new SrvContact();
                    SrvAddress srvAddress = new SrvAddress();

                    srvAddress.AddressTypeId = Convert.ToInt32(DataConstants.SystemAddressTypes.Main);
                    srvAddress.AddressStreetNumber = contactAddress.StreetNumber;
                    srvAddress.AddressPostCode = contactAddress.PostCode;
                    srvAddress.AddressHouseName = contactAddress.HouseName;
                    srvAddress.AddressLine1 = contactAddress.Line1;
                    srvAddress.AddressLine2 = contactAddress.Line2;
                    srvAddress.AddressLine3 = contactAddress.Line3;
                    srvAddress.AddressTown = contactAddress.Town;
                    srvAddress.AddressCounty = contactAddress.County;
                    srvAddress.AddressCountry = contactAddress.Country;
                    srvAddress.AddressDXTown = contactAddress.DXTown;
                    srvAddress.AddressDXNumber = contactAddress.DXNumber;
                    srvAddress.IsMailingAddress = contactAddress.IsMailingAddress;
                    srvAddress.IsBillingAddress = contactAddress.IsBillingAddress;

                    srvAddress.AddressOrgName = contactAddress.OrganisationName;
                    srvAddress.AddressComment = contactAddress.Comment;
                    srvAddress.AddressDepartment = contactAddress.Department;
                    srvAddress.AddressPostBox = contactAddress.PostBox;
                    srvAddress.AddressSubBuilding = contactAddress.SubBuilding;
                    srvAddress.AddressStreetNumber = contactAddress.StreetNumber;
                    srvAddress.AddressDependantLocality = contactAddress.DependantLocality;
                    srvAddress.AddressLastVerified = contactAddress.LastVerified;

                    // Save Additional Address Info to Address Object
                    if (additionalElement != null)
                    {
                        for (int i = 0; i <= 9; i++)
                        {
                            srvAddress.AdditionalInfoElements[i].AddressElementText = additionalElement[i].ElementText;
                        }
                    }

                    contactService.Addresses.Add(srvAddress);
                    contactService.ContactType = contactType;

                    if (contactService.ContactType == IRISLegal.IlbCommon.ContactType.Individual)
                    {
                        //Person Information for Individual contact
                        contactService.Person.Surname = person.Surname;
                        contactService.Person.ForeName = person.ForeName;
                        contactService.Person.Title = person.Title;
                    }
                    else
                    {
                        contactService.Organisation.Name = organisation.Name;
                    }

                    contactService.ConflictNoteSummary = conflictNoteSummary;
                    contactService.ConflictNoteContent = conflictNoteContent;

                    string errorMessage;

                    returnValue.Success = contactService.Save(out errorMessage);
                    returnValue.Message = errorMessage;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Gets the associations for the matter.
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service.</param>
        /// <param name="collectionRequest">The collection request.</param>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        public MatterAssociationReturnValue MatterAssociationSearch(Guid logonId, CollectionRequest collectionRequest,
                                                                    MatterAssociationSearchCriteria criteria)
        {
            MatterAssociationReturnValue returnValue = new MatterAssociationReturnValue();

            try
            {
                Host.LoadLoggedOnUser(logonId);
                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            throw new Exception("Access denied");
                        default:
                            throw new Exception("Access denied");
                    }

                    // Create a data list creator for a list of associations
                    DataListCreator<MatterAssociationSearchItem> dataListCreator = new DataListCreator<MatterAssociationSearchItem>();

                    // Declare an inline event (annonymous delegate) to read the
                    // dataset if it is required
                    dataListCreator.ReadDataSet += delegate(object sender, ReadDataSetEventArgs e)
                    {
                        DataSet dataSet = SrvAssociationLookup.GetMatterAssociations(criteria.ProjectId, criteria.ApplicationId);
                        // Perform Sorting on Role
                        DataView dvContacts = new DataView(dataSet.Tables[0]);
                        dvContacts.Sort = "AssociationRoleDescription asc";

                        DataSet dsSorted = new DataSet();
                        dsSorted.Tables.Add(dvContacts.ToTable());
                        e.DataSet = dsSorted;

                        e.DataSet.Tables[0].Columns.Add("Email");
                        e.DataSet.Tables[0].Columns.Add("WorkTel");
                    };

                    // Create the data list
                    DataList<MatterAssociationSearchItem> matterAssociations = dataListCreator.Create(logonId,
                        // Give the query a name so it can be cached
                        "MatterAssociationSearch",
                        // Tell it the query criteria used so if the cache is accessed
                        // again it knows if it is the same query
                        criteria.ToString(),
                        collectionRequest,
                        // Import mappings to map the dataset row fields to the data
                        // list entity properties
                        new ImportMapping[] {
                            new ImportMapping("Role", "AssociationRoleDescription"),
                            new ImportMapping("Title", "PersonTitle"),
                            new ImportMapping("Name", "PersonName"),
                            new ImportMapping("Surname", "PersonSurname"),
                            new ImportMapping("Description", "ProjectMapDescription"),
                            new ImportMapping("MemberId", "MemberId"),
                            new ImportMapping("OrgId", "OrgId")
                            }
                        );

                    DataSet dsMatterAssociations;

                    matterAssociations.Rows.ForEach(delegate(MatterAssociationSearchItem item)
                    {
                        if (item.MemberId == DataConstants.DummyGuid)
                        {
                            dsMatterAssociations = SrvAddressLookup.GetOrganisationAddresses(item.OrgId);
                        }
                        else
                        {
                            dsMatterAssociations = SrvAddressLookup.GetMemberAddresses(item.MemberId);
                        }
                        SrvAddress srvAddress;
                        int intCtr;

                        foreach (DataRow dr in dsMatterAssociations.Tables[0].Rows)
                        {

                            srvAddress = new SrvAddress();
                            if (item.MemberId == DataConstants.DummyGuid)
                            {
                                srvAddress.OrganisationId = item.OrgId;
                            }
                            else
                            {
                                srvAddress.MemberId = item.MemberId;
                            }
                            srvAddress.Load(int.Parse(dr["addressId"].ToString()));

                            for (intCtr = 0; intCtr < srvAddress.AdditionalInfoElements.Count; intCtr++)
                            {
                                AdditionalAddressElement additionalAddressElement = new AdditionalAddressElement();

                                if (srvAddress.AdditionalInfoElements[intCtr].AddressElTypeId == 8)
                                {
                                    item.WorkEmail = srvAddress.AdditionalInfoElements[intCtr].AddressElementText;
                                }

                                if (srvAddress.AdditionalInfoElements[intCtr].AddressElTypeId == 2)
                                {
                                    item.WorkTelephone = srvAddress.AdditionalInfoElements[intCtr].AddressElementText;
                                }

                            }
                        }
                    });

                    returnValue.Associations = matterAssociations;
                }
                finally
                {
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }
            return returnValue;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logonId"></param>
        /// <param name="collectionRequest"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public PartnerSearchReturnValue PartnerSearch(Guid logonId, CollectionRequest collectionRequest,
            PartnerSearchCriteria criteria)
        {
            PartnerSearchReturnValue returnValue = new PartnerSearchReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            // Can do everything
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    // Create a data list creator for a list of matters
                    DataListCreator<PartnerSearchItem> dataListCreator = new DataListCreator<PartnerSearchItem>();

                    // Declare an inline event (annonymous delegate) to read the
                    // dataset if it is required
                    dataListCreator.ReadDataSet += delegate(object Sender, ReadDataSetEventArgs e)
                    {
                        // TODO: Does not use criteria: Name
                        e.DataSet = SrvEarnerLookup.GetPartnerLookup(criteria.IncludeArchived);

                        DataTable dt = Functions.SortDataTable(e.DataSet.Tables[0], "PersonSurname");
                        e.DataSet.Tables.Remove(e.DataSet.Tables[0]);
                        e.DataSet.Tables.Add(dt);
                    };

                    // Create the data list
                    DataList<PartnerSearchItem> partnerList = dataListCreator.Create(logonId,
                        // Give the query a name so it can be cached
                         "PartnerSearch",
                        // Tell it the query criteria used so if the cache is accessed
                        // again it knows if it is the same query
                         criteria.ToString(),
                         collectionRequest,
                        // Import mappings to map the dataset row fields to the data
                        // list entity properties
                         new ImportMapping[] {
                            new ImportMapping("PartnerId", "MemberId"),
                            new ImportMapping("Name", "PersonName"),
                            new ImportMapping("PersonTitle", "PersonTitle"),
                            new ImportMapping("Surname", "PersonSurname")
                            }
                         );

                    DataSet dsPartnerList;

                    partnerList.Rows.ForEach(delegate(PartnerSearchItem item)
                    {
                        dsPartnerList = SrvAddressLookup.GetMemberAddresses(item.PartnerId);
                        SrvAddress srvAddress;
                        int intCtr;

                        foreach (DataRow dr in dsPartnerList.Tables[0].Rows)
                        {

                            srvAddress = new SrvAddress();
                            srvAddress.MemberId = item.PartnerId;
                            srvAddress.Load(int.Parse(dr["addressId"].ToString()));

                            for (intCtr = 0; intCtr < srvAddress.AdditionalInfoElements.Count; intCtr++)
                            {
                                AdditionalAddressElement additionalAddressElement = new AdditionalAddressElement();

                                if (srvAddress.AdditionalInfoElements[intCtr].AddressElTypeId == 8)
                                {
                                    item.Email = srvAddress.AdditionalInfoElements[intCtr].AddressElementText;
                                }

                            }
                        }
                    });

                    returnValue.Partners = partnerList;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception Ex)
            {
                returnValue.Success = false;
                returnValue.Message = Ex.Message;
            }

            return returnValue;
        }
Пример #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="logonId"></param>
        /// <param name="collectionRequest"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public PartnerSearchReturnValue PartnerSearch(Guid logonId, CollectionRequest collectionRequest,
                                                      PartnerSearchCriteria criteria)
        {
            PartnerSearchReturnValue returnValue = new PartnerSearchReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    switch (UserInformation.Instance.UserType)
                    {
                    case DataConstants.UserType.Staff:
                    case DataConstants.UserType.Client:
                    case DataConstants.UserType.ThirdParty:
                        // Can do everything
                        break;

                    default:
                        throw new Exception("Access denied");
                    }

                    // Create a data list creator for a list of matters
                    DataListCreator <PartnerSearchItem> dataListCreator = new DataListCreator <PartnerSearchItem>();

                    // Declare an inline event (annonymous delegate) to read the
                    // dataset if it is required
                    dataListCreator.ReadDataSet += delegate(object Sender, ReadDataSetEventArgs e)
                    {
                        // TODO: Does not use criteria: Name
                        e.DataSet = SrvEarnerLookup.GetPartnerLookup(criteria.IncludeArchived);

                        DataTable dt = Functions.SortDataTable(e.DataSet.Tables[0], "PersonSurname");
                        e.DataSet.Tables.Remove(e.DataSet.Tables[0]);
                        e.DataSet.Tables.Add(dt);
                    };

                    // Create the data list
                    DataList <PartnerSearchItem> partnerList = dataListCreator.Create(logonId,
                                                                                      // Give the query a name so it can be cached
                                                                                      "PartnerSearch",
                                                                                      // Tell it the query criteria used so if the cache is accessed
                                                                                      // again it knows if it is the same query
                                                                                      criteria.ToString(),
                                                                                      collectionRequest,
                                                                                      // Import mappings to map the dataset row fields to the data
                                                                                      // list entity properties
                                                                                      new ImportMapping[] {
                        new ImportMapping("PartnerId", "MemberId"),
                        new ImportMapping("Name", "PersonName"),
                        new ImportMapping("PersonTitle", "PersonTitle"),
                        new ImportMapping("Surname", "PersonSurname")
                    }
                                                                                      );

                    DataSet dsPartnerList;

                    partnerList.Rows.ForEach(delegate(PartnerSearchItem item)
                    {
                        dsPartnerList = SrvAddressLookup.GetMemberAddresses(item.PartnerId);
                        SrvAddress srvAddress;
                        int intCtr;

                        foreach (DataRow dr in dsPartnerList.Tables[0].Rows)
                        {
                            srvAddress          = new SrvAddress();
                            srvAddress.MemberId = item.PartnerId;
                            srvAddress.Load(int.Parse(dr["addressId"].ToString()));

                            for (intCtr = 0; intCtr < srvAddress.AdditionalInfoElements.Count; intCtr++)
                            {
                                AdditionalAddressElement additionalAddressElement = new AdditionalAddressElement();

                                if (srvAddress.AdditionalInfoElements[intCtr].AddressElTypeId == 8)
                                {
                                    item.Email = srvAddress.AdditionalInfoElements[intCtr].AddressElementText;
                                }
                            }
                        }
                    });


                    returnValue.Partners = partnerList;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception Ex)
            {
                returnValue.Success = false;
                returnValue.Message = Ex.Message;
            }

            return(returnValue);
        }
        /// <summary>
        /// Add a new client subject to user type, permissions and licensing
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service</param>
        /// <param name="client">client details</param>
        /// <param name="person">Person details</param>
        /// <param name="organisation">Organisation details</param>
        /// <param name="addresses">Addresses list</param>
        /// <param name="addressInformation">Address information list</param>
        /// <returns>Returns one client entity</returns>
        public ClientReturnValue AddClient(
            Guid logonId,
            Client client,
            Person person,
            Organisation organisation,
            List<Address> addresses,
            List<AdditionalAddressElement> addressInformation)
        {
            ClientReturnValue returnValue = new ClientReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            throw new Exception("Access denied");
                        default:
                            throw new Exception("Access denied");
                    }

                    // Ensure we have permission
                    if (!UserSecuritySettings.GetUserSecuitySettings(2))
                    {
                        throw new Exception("You do not have sufficient permissions to carry out this request");
                    }

                    // Verify Annual Licence
                    if (!IRIS.Law.PmsBusiness.LicenseDetails.AnnualLicenseIsValid())
                    {
                        throw new Exception("Unable to add Client. Your Annual Licence has expired or is invalid.");
                    }

                    SrvClient srvClient = new SrvClient();

                    // Load client fields
                    srvClient.ClientType = client.Type;
                    srvClient.ClientPartnerId = client.PartnerId;
                    srvClient.ClientBranch = client.Branch;
                    srvClient.ClientNetPassword = client.NetPassword;
                    srvClient.ConflictNoteSummary = client.ConflictNoteSummary;
                    srvClient.ConflictNoteContent = client.ConflictNoteContent;

                    // Load person fields
                    switch (client.Type)
                    {
                        case IRISLegal.IlbCommon.ContactType.Individual:
                            srvClient.Person.Title = person.Title;
                            srvClient.Person.setDefaultSex();
                            srvClient.Person.Surname = person.Surname;
                            srvClient.Person.ForeName = person.ForeName;
                            break;
                        case IRISLegal.IlbCommon.ContactType.Organisation:
                            srvClient.Organisation = new SrvOrganisation();
                            srvClient.Organisation.Name = organisation.Name;
                            break;
                        default:
                            throw new Exception("Unknown ClientType");
                    }

                    // This will be used if Client Type selected is multiple
                    // so that second person will be associated with the first person
                    if (client.AssociationId != DataConstants.DummyGuid)
                    {
                        srvClient.AssociationRoleId = client.AssociationRoleId;
                        srvClient.AssociationId = client.AssociationId;
                    }

                    // Load address of person
                    if (addresses != null)
                    {
                        foreach (Address address in addresses)
                        {
                            SrvAddress srvAddress = new SrvAddress();

                            srvAddress.AddressTypeId = address.TypeId;
                            srvAddress.AddressStreetNumber = address.StreetNumber;
                            srvAddress.AddressPostCode = address.PostCode;
                            srvAddress.AddressHouseName = address.HouseName;
                            srvAddress.AddressLine1 = address.Line1;
                            srvAddress.AddressLine2 = address.Line2;
                            srvAddress.AddressLine3 = address.Line3;
                            srvAddress.AddressTown = address.Town;
                            srvAddress.AddressCounty = address.County;
                            srvAddress.AddressCountry = address.Country;
                            srvAddress.AddressDXTown = address.DXTown;
                            srvAddress.AddressDXNumber = address.DXNumber;
                            srvAddress.IsMailingAddress = address.IsMailingAddress;
                            srvAddress.IsBillingAddress = address.IsBillingAddress;

                            srvAddress.AddressOrgName = address.OrganisationName;
                            srvAddress.AddressComment = address.Comment;
                            srvAddress.AddressDepartment = address.Department;
                            srvAddress.AddressPostBox = address.PostBox;
                            srvAddress.AddressSubBuilding = address.SubBuilding;
                            srvAddress.AddressStreetNumber = address.StreetNumber;
                            srvAddress.AddressDependantLocality = address.DependantLocality;
                            srvAddress.AddressLastVerified = address.LastVerified;

                            // Save Additional Address Info to Address Object
                            if (addressInformation != null)
                            {
                                for (int i = 0; i <= addressInformation.Count - 1; i++)
                                {
                                    srvAddress.AdditionalInfoElements[i].AddressElementText =
                                        addressInformation[i].ElementText;
                                }
                            }

                            // TODO more address fields
                            srvClient.Addresses.Add(srvAddress);
                        }
                    }

                    string errorMessage;

                    returnValue.Success = srvClient.Save(out errorMessage);
                    returnValue.Message = errorMessage;

                    client.MemberId = srvClient.MemberId;
                    client.IsMember = srvClient.IsMember;

                    client.OrganisationId = srvClient.OrganisationId;

                    returnValue.Client = client;
                    returnValue.Person = person;
                    returnValue.Organisation = organisation;
                    returnValue.Addresses = addresses;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Update an existing client's address or add a new one
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service</param>
        /// <param name="memberId">Member id of the client</param>
        /// <param name="organisationId">Orgainisation id of the client</param>
        /// <param name="address">Address to update or new address to add.
        /// If the Address.Id = 0 then a new address is being added.</param>
        /// <returns></returns>
        public ReturnValue UpdateClientAddress(Guid logonId, Guid memberId,
            Guid organisationId, Address address)
        {
            ReturnValue returnValue = new ReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Can do everything
                            break;
                        case DataConstants.UserType.Client:
                            if (!ApplicationSettings.Instance.IsUser(memberId, organisationId))
                                throw new Exception("Access denied");
                            break;
                        case DataConstants.UserType.ThirdParty:
                            throw new Exception("Access denied");
                        default:
                            throw new Exception("Access denied");
                    }

                    SrvClient srvClient = new SrvClient();

                    srvClient.Load(memberId, organisationId);

                    SrvAddress srvAddress = null;

                    if (address.Id != 0)
                    {
                        // Updating an existing address so check it exists on this client
                        foreach (SrvAddress addr in srvClient.Addresses)
                        {
                            if (addr.AddressId == address.Id)
                            {
                                // Found it
                                srvAddress = addr;
                                break;
                            }
                        }

                        if (srvAddress == null)
                            throw new Exception("Address does not exist for client");
                    }
                    else
                    {
                        // A new address is being added
                        srvAddress = new SrvAddress();

                        srvAddress.MemberId = memberId;
                        srvAddress.OrganisationId = organisationId;
                    }

                    srvAddress.AddressTypeId = address.TypeId;
                    srvAddress.AddressLine1 = address.Line1;
                    // TODO more address fields

                    string errorMessage;

                    returnValue.Success = srvAddress.Save(out errorMessage);
                    returnValue.Message = errorMessage;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }