Ejemplo n.º 1
0
        private void LockNames(Context context)
        {
            try
            {
                Status.Update("[Generator] Locking friendly names ... ", false);

                foreach (var filter in settings.EntityProfilesHeaderSelector.EntityProfilesHeaders.Select(filterList => filterList)
                         .SelectMany(filter => filter.EntityProfiles))
                {
                    // if filter's entity exists in selected entities
                    var entity = context.Entities.FirstOrDefault(entityQ => entityQ.LogicalName == filter.LogicalName);

                    if (entity == null)
                    {
                        continue;
                    }

                    // get all non-renamed attributes to lock
                    foreach (var attribute in entity.Fields.Where(field => field.LogicalName != null)
                             .Select(field => field.LogicalName).Except(filter.AttributeRenames.Keys))
                    {
                        // if attribute exists in the entity
                        var field = entity.Fields.FirstOrDefault(fieldQ => fieldQ.LogicalName == attribute);

                        if (field == null)
                        {
                            continue;
                        }

                        // lock
                        filter.AttributeRenames[attribute] = field.FriendlyName;
                    }

                    // get all non-renamed relations to lock
                    foreach (var relation in entity.RelationshipsOneToMany.Where(relation => relation.SchemaName != null)
                             .Select(relation => relation.SchemaName).Except(filter.OneToNRenames.Keys))
                    {
                        // if relation exists in the entity
                        var relationInEntity = entity.RelationshipsOneToMany.FirstOrDefault(relationQ => relationQ.SchemaName == relation);

                        if (relationInEntity == null)
                        {
                            continue;
                        }

                        filter.OneToNRenames[relation] = relationInEntity.FriendlyName;
                    }

                    foreach (var relation in entity.RelationshipsManyToOne.Where(relation => relation.SchemaName != null)
                             .Select(relation => relation.SchemaName).Except(filter.NToOneRenames.Keys))
                    {
                        var relationInEntity = entity.RelationshipsManyToOne.FirstOrDefault(relationQ => relationQ.SchemaName == relation);

                        if (relationInEntity == null)
                        {
                            continue;
                        }

                        filter.NToOneRenames[relation] = relationInEntity.FriendlyName;
                    }

                    foreach (var relation in entity.RelationshipsManyToMany.Where(relation => relation.SchemaName != null)
                             .Select(relation => relation.SchemaName).Except(filter.NToNRenames.Keys))
                    {
                        var relationInEntity =
                            entity.RelationshipsManyToMany.FirstOrDefault(relationQ => relationQ.SchemaName == relation);

                        if (relationInEntity == null)
                        {
                            continue;
                        }

                        filter.NToNRenames[relation] = relationInEntity.FriendlyName;
                    }
                }
            }
            catch (Exception ex)
            {
                Status.PopException(Dispatcher, ex);
            }
            finally
            {
                Status.Update("done!");
            }
        }
Ejemplo n.º 2
0
        private void MapperOnStatusUpdate(object o, MapperEventArgs args)
        {
            try
            {
                switch (args.Status)
                {
                case MapperStatus.Cancelled:
                    UnregisterMapperEvents();
                    Status.Update("[Generator] [DONE] Cancelled generator!");
                    Status.HideBusy(Dispatcher, BusyIndicator);
                    StillOpen = false;
                    Dispatcher.Invoke(Close, DispatcherPriority.Send);
                    break;

                case MapperStatus.Error:
                    UnregisterMapperEvents();
                    Status.Update("[Generator] [ERROR] Generator failed!");

                    var message = args.Exception.Message;
                    var inner   = args.Exception?.InnerException;

                    if (inner?.Message.IsFilled() == true)
                    {
                        message += $" | {inner.Message}";
                    }

                    if (args.Exception is NullReferenceException)
                    {
                        message = $"Generator failed. Clear the cache and try again.";
                    }

                    Status.PopException(Dispatcher, message);
                    Status.HideBusy(Dispatcher, BusyIndicator);
                    break;

                case MapperStatus.Finished:
                    UnregisterMapperEvents();
                    Context                               = Mapper.Context;
                    Context.SplitFiles                    = settings.SplitFiles;
                    Context.SplitContractFiles            = settings.SplitContractFiles;
                    Context.UseDisplayNames               = settings.UseDisplayNames == true;
                    Context.IsUseCustomDictionary         = settings.IsUseCustomDictionary;
                    Context.IsUseCustomEntityReference    = settings.IsUseCustomEntityReference;
                    Context.IsAddEntityAnnotations        = settings.IsAddEntityAnnotations;
                    Context.IsAddContractAnnotations      = settings.IsAddContractAnnotations;
                    Context.IsGenerateLoadPerRelation     = settings.IsGenerateLoadPerRelation;
                    Context.IsGenerateEnumNames           = settings.IsGenerateEnumNames;
                    Context.IsGenerateEnumLabels          = settings.IsGenerateEnumLabels;
                    Context.IsGenerateFieldSchemaNames    = settings.IsGenerateFieldSchemaNames;
                    Context.IsGenerateFieldLabels         = settings.IsGenerateFieldLabels;
                    Context.IsGenerateRelationNames       = settings.IsGenerateRelationNames;
                    Context.IsImplementINotifyProperty    = settings.IsImplementINotifyProperty;
                    Context.GenerateGlobalActions         = settings.GenerateGlobalActions;
                    Context.PluginMetadataEntities        = settings.PluginMetadataEntitiesSelected.ToList();
                    Context.OptionsetLabelsEntities       = settings.OptionsetLabelsEntitiesSelected.ToList();
                    Context.LookupLabelsEntities          = settings.LookupLabelsEntitiesSelected.ToList();
                    Context.JsEarlyBoundEntities          = settings.JsEarlyBoundEntitiesSelected.ToList();
                    Context.EarlyBoundFilteredSelected    = settings.EarlyBoundFilteredSelected.ToList();
                    Context.SelectedActions               = settings.SelectedActions;
                    Context.ClearMode                     = settings.ClearMode;
                    Context.SelectedEntities              = settings.EntitiesSelected.ToArray();
                    Context.IsGenerateAlternateKeys       = settings.IsGenerateAlternateKeys;
                    Context.IsUseCustomTypeForAltKeys     = settings.IsUseCustomTypeForAltKeys;
                    Context.IsMakeCrmEntitiesJsonFriendly = settings.IsMakeCrmEntitiesJsonFriendly;
                    Context.CrmEntityProfiles             = settings.CrmEntityProfiles;
                    Context.EntityProfilesHeaderSelector  = settings.EntityProfilesHeaderSelector;

                    if (settings.LockNamesOnGenerate)
                    {
                        LockNames(Context);
                    }

                    MetadataCache.Context = Context;

                    Configuration.SaveSettings(settings);

                    Status.HideBusy(Dispatcher, BusyIndicator);
                    StillOpen = false;
                    Dispatcher.Invoke(Close, DispatcherPriority.Send);
                    break;
                }
            }
            catch (Exception ex)
            {
                Status.PopException(Dispatcher, ex);
                Status.HideBusy(Dispatcher, BusyIndicator);
            }
        }