Пример #1
0
        public void Associate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
        {
            if (!relatedEntities.Any())
            {
                throw new ArgumentException("Must contain at least one related entity!", "relatedEntities");
            }
            if (relatedEntities.Any(e => e.LogicalName != relatedEntities.First().LogicalName))
            {
                throw new NotImplementedException("Don't currently Support different Entity Types for related Entities!");
            }

            if (relationship.PrimaryEntityRole.GetValueOrDefault(EntityRole.Referenced) == EntityRole.Referencing)
            {
                throw new NotImplementedException("Referencing Not Currently Implemented");
            }

            var referencedIdName  = EntityHelper.GetIdAttributeName(entityName);
            var referencingIdName = EntityHelper.GetIdAttributeName(relatedEntities.First().LogicalName);

            if (referencedIdName == referencingIdName)
            {
                referencedIdName  += "one";
                referencingIdName += "two";
            }


            foreach (var relatedEntity in relatedEntities)
            {
                var relation = new Entity(relationship.SchemaName);
                relation[referencedIdName]  = entityId;
                relation[referencingIdName] = relatedEntity.Id;
                Service.Create(relation);
            }
        }
Пример #2
0
        public void Disassociate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
        {
            if (!relatedEntities.Any())
            {
                throw new ArgumentException("Must contain at least one related entity!", "relatedEntities");
            }
            if (relatedEntities.Any(e => e.LogicalName != relatedEntities.First().LogicalName))
            {
                throw new NotImplementedException("Don't currently Support different Entity Types for related Entities!");
            }

            if (relationship.PrimaryEntityRole.GetValueOrDefault(EntityRole.Referenced) == EntityRole.Referencing)
            {
                throw new NotImplementedException("Referencing Not Currently Implemented");
            }

            var referencedIdName  = EntityHelper.GetIdAttributeName(entityName);
            var referencingIdName = EntityHelper.GetIdAttributeName(relatedEntities.First().LogicalName);

            if (referencedIdName == referencingIdName)
            {
                referencedIdName  += "one";
                referencingIdName += "two";
            }


            foreach (var entity in relatedEntities.
                     Select(e => QueryExpressionFactory.Create(relationship.SchemaName, referencedIdName, entityId, referencingIdName, e.Id)).
                     Select(qe => Service.RetrieveMultiple(qe).ToEntityList <Entity>().FirstOrDefault()).
                     Where(entity => entity != null))
            {
                Service.Delete(entity);
            }
        }
        private AssociateResponse AssociateInternal(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
        {
            if (!relatedEntities.Any())
            {
                throw new ArgumentException("Must contain at least one related entity!", nameof(relatedEntities));
            }

            if (relatedEntities.Any(e => e.LogicalName != relatedEntities.First().LogicalName))
            {
                throw new NotImplementedException("Don't currently Support different Entity Types for related Entities!");
            }

            if (relationship.PrimaryEntityRole.GetValueOrDefault(EntityRole.Referenced) == EntityRole.Referencing)
            {
                throw new NotImplementedException("Referencing Not Currently Implemented");
            }

            var response = new AssociateResponse();

            if (Info.ManyToManyAssociationProvider.IsManyToManyRelationship(relationship.SchemaName))
            {
                var originalValue = EnforceValidForOperationCheck;
                EnforceValidForOperationCheck = false;
                try
                {
                    response["CreatedIds"] = Info.ManyToManyAssociationProvider.CreateAssociation(Service, entityName, entityId, relationship, relatedEntities);
                }
                finally
                {
                    EnforceValidForOperationCheck = originalValue;
                }
            }
            else if (EntityHelper.IsTypeDefined(Info.EarlyBoundEntityAssembly, Info.EarlyBoundNamespace, relationship.SchemaName))
            {
                var referencedIdName  = EntityHelper.GetIdAttributeName(entityName);
                var referencingIdName = EntityHelper.GetIdAttributeName(relatedEntities.First().LogicalName);
                if (referencedIdName == referencingIdName)
                {
                    referencedIdName  += "one";
                    referencingIdName += "two";
                }

                Associate1ToN(entityId, relationship, relatedEntities, referencedIdName, referencingIdName);
            }
            else
            {
                throw new NotImplementedException($"No entity found with logical name '{relationship.SchemaName}' for 1:N relationship!  {Info.ManyToManyAssociationProvider.GetNotFoundErrorMessage()}");
            }
            return(response);
        }
        public void Disassociate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
        {
            if (!relatedEntities.Any())
            {
                throw new ArgumentException("Must contain at least one related entity!", nameof(relatedEntities));
            }
            if (relatedEntities.Any(e => e.LogicalName != relatedEntities.First().LogicalName))
            {
                throw new NotImplementedException("Don't currently Support different Entity Types for related Entities!");
            }

            if (relationship.PrimaryEntityRole.GetValueOrDefault(EntityRole.Referenced) == EntityRole.Referencing)
            {
                throw new NotImplementedException("Referencing Not Currently Implemented");
            }

            var referencedIdName  = EntityHelper.GetIdAttributeName(entityName);
            var referencingIdName = EntityHelper.GetIdAttributeName(relatedEntities.First().LogicalName);

            if (referencedIdName == referencingIdName)
            {
                referencedIdName  += "one";
                referencingIdName += "two";
            }

            //if (EntityHelper.IsTypeDefined(Info.EarlyBoundEntityAssembly, Info.EarlyBoundNamespace,
            //    relationship.SchemaName))
            //{
            Disassociate1ToN(entityId, relationship, relatedEntities, referencedIdName, referencingIdName);
            //}
            //else
            //{
            //    foreach (var entity in relatedEntities)
            //    {
            //        DisassociateN2N(new EntityReference(entityName, entityId), entity, relationship);
            //    }
            //}
        }
Пример #5
0
        private void ActionForOkved(
            AccountKonturFocusModel accountData
            , string inn
            , Account proxy
            , bool isForeignOrganization
            , IOrganizationService service
            , ITracingService tracingService
            )
        {
            tracingService.Trace("enter konturfocussynchronizationplugin.actionforokved");

            AccountKonturFocusModel accountExtendData = null;
            OKVEDKonturFocusModel   okvedData         = null;

            string accountsKonturFocusDataJson = string.Empty;

            string okvedKonturFocusDataJson = GetAccountOkvedJsonDataInKonturFocus(service, inn);

            if (string.IsNullOrEmpty(okvedKonturFocusDataJson) || okvedKonturFocusDataJson == ItnNotFoundInKf)
            {
                tracingService.Trace("no data for okved");
                tracingService.Trace("exit konturfocussynchronizationplugin.actionforokved");
                return;
            }
            accountExtendData = JsonConvert.DeserializeObject <List <AccountKonturFocusModel> >(okvedKonturFocusDataJson).FirstOrDefault();

            List <AccountOkved> accountOkveds = GetAccountOkveds(proxy.Id, service);

            if (accountExtendData == null)
            {
                Okved m_okved = null;

                if (isForeignOrganization && accountData.activities != null && accountData.activities.principalActivity != null)
                {
                    m_okved = Find(accountData.activities.principalActivity.code.Trim(), service, tracingService);
                }


                if (m_okved == null && accountData.activities != null && accountData.activities.principalActivity != null)
                {
                    tracingService.Trace("new {0} okved", accountData.activities.principalActivity.code.Trim());
                    m_okved = new Okved
                    {
                        Code        = accountData.activities.principalActivity.code.Trim(),
                        Name        = accountData.activities.principalActivity.code.Trim(),
                        Description = accountData.activities.principalActivity.text.Trim(),
                    };
                    m_okved.Id = service.Create(m_okved);
                    tracingService.Trace("actionforokved - new okved '{0}'", m_okved.Name);
                }

                if (m_okved != null)
                {
                    tracingService.Trace("set {0} okved", m_okved.Name);
                    proxy.Okved   = m_okved.Name;
                    proxy.OkvedId = m_okved.ToEntityReference();
                }
            }
            else
            {
                if (accountExtendData.UL != null && accountExtendData.UL.activities != null)
                {
                    okvedData = new OKVEDKonturFocusModel
                    {
                        principalActivity = accountExtendData.UL.activities.principalActivity,
                        activities        = accountExtendData.UL.activities.complementaryActivities
                    };
                }
                else if (accountExtendData.IP != null && accountExtendData.IP.activities != null)
                {
                    okvedData = new OKVEDKonturFocusModel
                    {
                        principalActivity = accountExtendData.IP.activities.principalActivity,
                        activities        = accountExtendData.IP.activities.complementaryActivities
                    };
                }

                if (okvedData != null && okvedData.principalActivity != null)
                {
                    tracingService.Trace("work with okved {0}", okvedData.principalActivity.code.Trim());
                    Okved m_okved = Find(okvedData.principalActivity.code.Trim(), service, tracingService);
                    if (m_okved == null)
                    {
                        m_okved = new Okved
                        {
                            Code        = okvedData.principalActivity.code.Trim(),
                            Name        = okvedData.principalActivity.code.Trim(),
                            Description = okvedData.principalActivity.text.Trim(),
                        };
                        m_okved.Id = service.Create(m_okved);
                    }

                    if (m_okved != null)
                    {
                        tracingService.Trace("set {0} okved", m_okved.Name);
                        proxy.Okved   = m_okved.Name;
                        proxy.OkvedId = m_okved.ToEntityReference();
                    }

                    if (okvedData.activities != null && okvedData.activities.Any())
                    {
                        List <Okved> okveds = GetOkveds(service);
                        EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
                        Relationship relationship = new Relationship(@"lmr_account_lmr_okved");

                        foreach (activity activity in okvedData.activities)
                        {
                            var query = from item in okveds
                                        where item.Name == activity.code
                                        select item;

                            if (query.Any())
                            {
                                Okved okvedItem = query.First();
                                if (!accountOkveds.Any(s => s.OkvedId == okvedItem.Id))
                                {
                                    relatedEntities.Add(okvedItem.ToEntityReference());
                                }
                            }
                            else
                            {
                                Okved okvedItem = new Okved
                                {
                                    Code        = activity.code.Trim(),
                                    Name        = activity.code.Trim(),
                                    Description = activity.text.Trim(),
                                };
                                okvedItem.Id = service.Create(okvedItem);
                                okveds.Add(okvedItem);
                                relatedEntities.Add(okvedItem.ToEntityReference());
                            }
                        }
                        if (relatedEntities.Any())
                        {
                            tracingService.Trace("actionforokved preassociated");
                            service.Associate(Account.EntityLogicalName, proxy.Id, relationship, relatedEntities);
                            tracingService.Trace("actionforokved associated {0} records", relatedEntities.Count);
                        }
                    }
                }
            }
            tracingService.Trace("exit konturfocussynchronizationplugin.actionforokved");
        }