Esempio n. 1
0
        private void SaveChangeObjects(LicenseeChangeLog node, string applicationId, string parentLegalEntityId = null, string parentChangeLogId = null)
        {
            if (node.ChangeType != LicenseeChangeType.unchanged)
            {
                MicrosoftDynamicsCRMadoxioLicenseechangelog patchEntity = new MicrosoftDynamicsCRMadoxioLicenseechangelog();
                patchEntity.CopyValues(node);
                node.ApplicationId = applicationId;
                if (parentLegalEntityId != null)
                {
                    node.ParentLegalEntityId = parentLegalEntityId;
                }
                if (parentChangeLogId != null)
                {
                    node.ParentLinceseeChangeLogId = parentChangeLogId;
                }

                if (string.IsNullOrEmpty(node.Id)) // create
                {
                    // bind to Parent legal entity
                    if (!string.IsNullOrEmpty(node.ParentLegalEntityId))
                    {
                        patchEntity.ParentLegalEntityOdataBind = _dynamicsClient.GetEntityURI("adoxio_legalentities", node.ParentLegalEntityId);
                    }
                    // bind to legal entity
                    if (!string.IsNullOrEmpty(node.LegalEntityId))
                    {
                        patchEntity.LegalEntityIdOdataBind = _dynamicsClient.GetEntityURI("adoxio_legalentities", node.LegalEntityId);
                    }

                    // bind to parent licensee change log
                    if (!string.IsNullOrEmpty(node.ParentLinceseeChangeLogId))
                    {
                        patchEntity.ParentLinceseeChangeLogOdataBind = _dynamicsClient.GetEntityURI("adoxio_licenseechangelogs", node.ParentLinceseeChangeLogId);
                    }


                    // bind to application
                    if (!string.IsNullOrEmpty(node.ApplicationId))
                    {
                        patchEntity.ApplicationOdataBind = _dynamicsClient.GetEntityURI("adoxio_applications", node.ApplicationId);
                        parentLegalEntityId = node.LegalEntityId;
                    }

                    try
                    {
                        var result = _dynamicsClient.Licenseechangelogs.Create(patchEntity);
                        parentChangeLogId = result.AdoxioLicenseechangelogid;
                    }
                    catch (HttpOperationException httpOperationException)
                    {
                        _logger.LogError(httpOperationException, $"Error saving LicenseeChangeLog: {httpOperationException.Request.Content} Response: {httpOperationException.Response.Content}");
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, $"Unexpected Exception while adding LegalEntityOwned reference to legal entity");
                    }
                }
                else // update
                {
                    // bind to application
                    if (!string.IsNullOrEmpty(node.ApplicationId))
                    {
                        patchEntity.ApplicationOdataBind = _dynamicsClient.GetEntityURI("adoxio_applications", node.ApplicationId);
                    }


                    try
                    {
                        _dynamicsClient.Licenseechangelogs.Update(node.Id, patchEntity);
                        var result = _dynamicsClient.Licenseechangelogs.GetByKey(node.Id);
                        parentChangeLogId   = result.AdoxioLicenseechangelogid;
                        parentLegalEntityId = node.LegalEntityId;
                    }
                    catch (HttpOperationException httpOperationException)
                    {
                        _logger.LogError(httpOperationException, $"Error saving LicenseeChangeLog: {httpOperationException.Request.Content} Response: {httpOperationException.Response.Content}");
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, $"Unexpected Exception while saving LicenseeChangeLog");
                    }
                }
            }
            else
            {
                parentLegalEntityId = node.LegalEntityId;
                parentChangeLogId   = node.Id;
            }

            if (node.Children != null)
            {
                foreach (var item in node.Children)
                {
                    SaveChangeObjects(item, applicationId, parentLegalEntityId, parentChangeLogId);
                }
            }
        }