private void RegenerateDataStructureOfSelectedEntity(AbstractUserContext userContext, string entityId) { try { ORM <CRM2011Entity> crmEntityORM = new ORM <CRM2011Entity>(); CRM2011Entity crmEntity = crmEntityORM.Fetch(entityId); if (crmEntity != null) { log.Info(string.Format("started regenerating data structure of {0} entity", crmEntity.CRMEntityName)); ORM <SimpleFlowStructure> simpleFlowStructureORM = new ORM <SimpleFlowStructure>(); SimpleFlowStructure simpleFlowStructure = simpleFlowStructureORM.Fetch(new WhereCondition[] { new FieldWhereCondition("data_type_name", QueryMatchType.Equals, crmEntity.CRMEntityName), new FieldWhereCondition("data_type_name_space", QueryMatchType.Equals, crmEntity.GetCrmEntityNamespace()) }).FirstOrDefault(); if (simpleFlowStructure != null) { IOrganizationService serviceProxy = GetCRMClientServiceProxy(); if (serviceProxy == null) { throw new BusinessRuleException("Unable to found CRM service client proxy"); } simpleFlowStructure.Children = null; AddOrUpdateCRMEntityWithDataStructure(serviceProxy, crmEntity.CRMEntityName, simpleFlowStructure); // Make sure the updated CRMEntityFields are stored: crmEntityORM.Store(this, true, false); } log.Info(string.Format("completed regenerating data structure of {0} entity", crmEntity.CRMEntityName)); } } catch (Exception ex) { log.Error(ex); throw ex; } }
public override void BeforeSave() { CRM2011Connection specifiedConnection = connection; if (specifiedConnection == null) { // If 'connection' is null, this might be an import. log.Debug("sConnection is null, fetching by ID"); specifiedConnection = CRM2011Connection.GetCRMConnectionById(connectionId); if (specifiedConnection != null) { Connection = specifiedConnection; } } // If a connection exists at this point, make sure this entity name doesn't already exist for this connection: if (specifiedConnection != null) { SetNamesFromSelectedName(); log.Debug($"Checking whether entity already exists with connection_id '{specifiedConnection.connectionId}' and crm_entity_name '{CRMEntityName}'."); ORM <CRM2011Entity> orm = new ORM <CRM2011Entity>(); var conditions = new List <WhereCondition> { new FieldWhereCondition("connection_id", QueryMatchType.Equals, specifiedConnection.connectionId), new FieldWhereCondition("crm_entity_name", QueryMatchType.Equals, this.CRMEntityName) }; if (!string.IsNullOrWhiteSpace(this.entityId)) { // (if ID is the same, this is an edit) conditions.Add(new FieldWhereCondition("entity_id", QueryMatchType.DoesNotEqual, this.entityId)); } CRM2011Entity otherEntity = orm.Fetch(conditions.ToArray()).FirstOrDefault(); log.Debug($"entity: {otherEntity?.CRMEntityDisplayName ?? "(null)"}"); if (otherEntity != null) { throw new InvalidOperationException("This entity already exists for this connection."); } } if (specifiedConnection == null) { // If the ID is missing, this might be an import. Check for a matching name: log.Debug("sConnection is null, fetching by name"); specifiedConnection = CRM2011Connection.GetCRMConnectionForName(connectionName); } if (specifiedConnection == null) { // If no connection was found by ID or by name, create one: log.Debug("sConnection is null, creating"); specifiedConnection = new CRM2011Connection() { ConnectionName = connectionName, OrganisationUrl = organisationUrl, Domain = domain, UserName = userName, Password = password }; // Add new connection to settings: CRM2011Connection[] oldConnections = ModuleSettingsAccessor <CRM2011Settings> .Instance.Connections; ModuleSettingsAccessor <CRM2011Settings> .Instance.Connections = oldConnections.Concat(new[] { specifiedConnection }).ToArray(); log.Debug($"about to save new connections..."); ModuleSettingsAccessor <CRM2011Settings> .SaveSettings(); specifiedConnection = CRM2011Connection.GetCRMConnectionForName(connectionName); if (specifiedConnection == null) { throw new EntityNotFoundException("CRMConnection was not created successfully."); } log.Debug("new connections saved."); } if (specifiedConnection != null) { // Update our data to match the connection's data: log.Debug("sConnection exists, updating data to match it..."); Connection = specifiedConnection; connectionId = specifiedConnection.connectionId; connectionName = specifiedConnection.ConnectionName; organisationUrl = specifiedConnection.OrganisationUrl; domain = specifiedConnection.Domain; userName = specifiedConnection.UserName; password = specifiedConnection.Password; } SetNamesFromSelectedName(); base.BeforeSave(); AddOrUpdateCRMEntity(); }
private void AddCRMEntity(AbstractUserContext usercontext, object obj) { CRM2011Entity field = (CRM2011Entity)obj; new DynamicORM().Store(field); }