public ActionResult EditRelatedPlace(EntityRelationshipModel model) { try { if (this.ModelState.IsValid) { var place = this.ImsiClient.Get <Place>(model.SourceId, null) as Place; if (place == null) { this.TempData["error"] = Locale.UnableToUpdatePlace; return(RedirectToAction("Edit", new { id = model.SourceId })); } place.Relationships.RemoveAll(r => r.Key == model.Id); place.Relationships.Add(new EntityRelationship(Guid.Parse(model.RelationshipType), Guid.Parse(model.TargetId))); var updatedPlace = this.ImsiClient.Update <Place>(place); return(RedirectToAction("Edit", new { id = updatedPlace.Key.Value })); } } catch (Exception e) { Trace.TraceError($"Unable to edit related place: { e }"); } return(View(model)); }
public ActionResult Edit(Guid id, Guid sourceId, string type) { Guid?versionKey = null; try { var modelType = this.GetModelType(type); var entity = this.GetEntity(sourceId, modelType); versionKey = entity.VersionKey; var model = new EntityRelationshipModel(Guid.NewGuid(), id) { //ExistingRelationships = place.Relationships.Select(r => new EntityRelationshipViewModel(r)).ToList() }; model.RelationshipTypes.AddRange(this.GetConceptSet(ConceptSetKeys.EntityRelationshipType).Concepts.ToSelectList(this.HttpContext.GetCurrentLanguage(), c => c.Key == EntityRelationshipTypeKeys.OwnedEntity).ToList()); //entity.Relationships.RemoveAll(r => r.Key == id); //var updatedEntity = this.UpdateEntity(entity, modelType); //this.TempData["success"] = Locale.Relationship + " " + Locale.Deleted + " " + Locale.Successfully; //return RedirectToAction("Edit", type, new { id = updatedEntity.Key.Value, versionId = updatedEntity.VersionKey.Value }); return(View(new EntityRelationshipModel(id, sourceId, (Guid)versionKey))); } catch (Exception e) { Trace.TraceError($"Unable to delete entity relationship: {e}"); } this.TempData["error"] = Locale.UnableToEditRelationship; return(RedirectToAction("Edit" + type, type, new { id = sourceId, versionId = versionKey })); }
public ActionResult EditRelatedPlace(Guid id, Guid entityRelationshipId) { try { var place = this.ImsiClient.Get <Place>(id, null) as Place; if (place == null) { this.TempData["error"] = Locale.PlaceNotFound; return(RedirectToAction("Edit", new { id = id })); } var entityRelationship = place.Relationships.Find(r => r.Key == entityRelationshipId); var model = new EntityRelationshipModel(entityRelationship, place.Type, place.ClassConceptKey?.ToString()); return(View(model)); } catch (Exception e) { Trace.TraceError($"Unable to edit related place: { e }"); } this.TempData["error"] = Locale.PlaceNotFound; return(RedirectToAction("Edit", new { id = id })); }
public ActionResult EditRelatedManufacturedMaterial(EntityRelationshipModel model) { try { if (this.ModelState.IsValid) { var material = this.entityService.Get <Material>(model.SourceId); if (material == null) { this.TempData["error"] = Locale.UnableToCreateRelatedManufacturedMaterial; return(RedirectToAction("Edit", new { id = model.SourceId })); } material.Relationships.RemoveAll(r => r.Key == model.Id); material.Relationships.Add(new EntityRelationship(Guid.Parse(model.RelationshipType), Guid.Parse(model.TargetId))); this.entityService.Update(material); this.TempData["success"] = Locale.RelatedManufacturedMaterialCreatedSuccessfully; return(RedirectToAction("Edit", new { id = material.Key.Value })); } } catch (Exception e) { Trace.TraceError($"Unable to update related manufactured material: { e }"); } this.TempData["error"] = Locale.UnableToUpdateRelatedManufacturedMaterial; return(View(model)); }
public ActionResult AssociateMaterial(EntityRelationshipModel model) { var concepts = new List <Concept>(); Material material = null; try { if (this.ModelState.IsValid) { // HACK: manually validating the quantity field, since for this particular page the quantity is required // but it feels like overkill to literally create the same model for the purpose of making only 1 property // required. if (!model.Quantity.HasValue) { this.ModelState.AddModelError(nameof(model.Quantity), Locale.QuantityRequired); this.TempData["error"] = Locale.QuantityRequired; concepts.Add(this.conceptService.GetConcept(EntityRelationshipTypeKeys.UsedEntity)); model.RelationshipTypes.AddRange(concepts.ToSelectList(this.HttpContext.GetCurrentLanguage(), c => c.Key == EntityRelationshipTypeKeys.UsedEntity)); return(View(model)); } material = this.entityService.Get <Material>(model.SourceId); if (material == null) { this.TempData["error"] = Locale.MaterialNotFound; return(RedirectToAction("Edit", new { id = model.SourceId })); } material.Relationships.RemoveAll(r => r.TargetEntityKey == Guid.Parse(model.TargetId) && r.RelationshipTypeKey == Guid.Parse(model.RelationshipType)); material.Relationships.Add(new EntityRelationship(Guid.Parse(model.RelationshipType), Guid.Parse(model.TargetId)) { EffectiveVersionSequenceId = material.VersionSequence, Key = Guid.NewGuid(), Quantity = model.Quantity.Value, SourceEntityKey = model.SourceId }); this.entityService.Update(material); this.TempData["success"] = Locale.MaterialRelatedSuccessfully; return(RedirectToAction("Edit", new { id = material.Key.Value })); } } catch (Exception e) { Trace.TraceError($"Unable to create related manufactured material: { e }"); } this.TempData["error"] = Locale.UnableToRelateMaterial; model.TargetList = this.BuildMaterialSelectList(material); concepts.Add(this.conceptService.GetConcept(EntityRelationshipTypeKeys.UsedEntity)); model.RelationshipTypes.AddRange(concepts.ToSelectList(this.HttpContext.GetCurrentLanguage(), c => c.Key == EntityRelationshipTypeKeys.UsedEntity)); return(View(model)); }
public ActionResult CreateRelatedPlace(Guid id) { try { var place = this.entityService.Get <Place>(id); if (place == null) { this.TempData["error"] = Locale.PlaceNotFound; return(RedirectToAction("Edit", new { id = id })); } var relationships = new List <EntityRelationship>(); relationships.AddRange(this.GetEntityRelationships <Place>(place.Key.Value, r => r.RelationshipTypeKey == EntityRelationshipTypeKeys.Child || r.RelationshipTypeKey == EntityRelationshipTypeKeys.Parent || r.RelationshipTypeKey == EntityRelationshipTypeKeys.DedicatedServiceDeliveryLocation)); place.Relationships = relationships.Intersect(place.Relationships, new EntityRelationshipComparer()).ToList(); var model = new EntityRelationshipModel(Guid.NewGuid(), id) { ExistingRelationships = place.Relationships.Select(r => new EntityRelationshipViewModel(r)).ToList(), SourceClass = place.ClassConceptKey?.ToString() }; // JF - THIS NEEDS TO BE CHANGED TO USE A CONCEPT SET var concepts = new List <Concept> { this.GetConcept(EntityRelationshipTypeKeys.Child), this.GetConcept(EntityRelationshipTypeKeys.Parent), this.GetConcept(EntityRelationshipTypeKeys.DedicatedServiceDeliveryLocation) }; model.RelationshipTypes.AddRange(concepts.ToSelectList(this.HttpContext.GetCurrentLanguage())); return(View(model)); } catch (Exception e) { Trace.TraceError($"Unable to create related place: { e }"); } this.TempData["error"] = Locale.PlaceNotFound; return(RedirectToAction("Edit", new { id = id })); }
public ActionResult CreateRelatedManufacturedMaterial(EntityRelationshipModel model) { try { if (this.ModelState.IsValid) { var organization = this.GetEntity <Organization>(model.SourceId); if (organization == null) { this.TempData["error"] = Locale.UnableToCreateRelatedManufacturedMaterial; return(RedirectToAction("Edit", new { id = model.SourceId })); } organization.Relationships.RemoveAll(r => r.TargetEntityKey == Guid.Parse(model.TargetId) && r.RelationshipTypeKey == Guid.Parse(model.RelationshipType)); organization.Relationships.Add(new EntityRelationship(Guid.Parse(model.RelationshipType), Guid.Parse(model.TargetId)) { EffectiveVersionSequenceId = organization.VersionSequence, Key = Guid.NewGuid(), Quantity = model.Quantity ?? 0, SourceEntityKey = model.SourceId }); this.UpdateEntity <Organization>(organization); this.TempData["success"] = Locale.RelatedManufacturedMaterialCreatedSuccessfully; return(RedirectToAction("Edit", new { id = organization.Key.Value })); } } catch (Exception e) { Trace.TraceError($"Unable to create related manufactured material: { e }"); } var concepts = new List <Concept> { this.GetConcept(EntityRelationshipTypeKeys.Instance), this.GetConcept(EntityRelationshipTypeKeys.ManufacturedProduct) }; model.RelationshipTypes.AddRange(concepts.ToSelectList(this.HttpContext.GetCurrentLanguage())); this.TempData["error"] = Locale.UnableToCreateRelatedManufacturedMaterial; return(View(model)); }
/// <summary> /// Associates a material to another material. /// </summary> /// <param name="id">The identifier.</param> /// <returns>Returns the associate material page.</returns> public ActionResult AssociateMaterial(Guid id) { try { var material = this.entityService.Get <Material>(id); if (material == null) { this.TempData["error"] = Locale.MaterialNotFound; return(RedirectToAction("Edit", new { id = id })); } var relationships = new List <EntityRelationship>(); relationships.AddRange(this.entityService.GetEntityRelationships <Material>(material.Key.Value, r => r.RelationshipTypeKey == EntityRelationshipTypeKeys.UsedEntity && r.ObsoleteVersionSequenceId == null).ToList()); material.Relationships = relationships.Intersect(material.Relationships, new EntityRelationshipComparer()).ToList(); var model = new EntityRelationshipModel(Guid.NewGuid(), id) { RelationshipType = EntityRelationshipTypeKeys.UsedEntity.ToString(), ExistingRelationships = material.Relationships.Select(r => new EntityRelationshipViewModel(r)).ToList(), TargetList = this.BuildMaterialSelectList(material) }; var concepts = new List <Concept> { this.conceptService.GetConcept(EntityRelationshipTypeKeys.UsedEntity, true) }; model.RelationshipTypes.AddRange(concepts.ToSelectList(this.HttpContext.GetCurrentLanguage(), c => c.Key == EntityRelationshipTypeKeys.UsedEntity)); return(View(model)); } catch (Exception e) { this.TempData["error"] = Locale.UnexpectedErrorMessage; Trace.TraceError($"Unable to load associate material page: { e }"); } return(RedirectToAction("Edit", new { id = id })); }
public ActionResult CreateRelatedManufacturedMaterial(Guid id) { try { var organization = this.GetEntity <Organization>(id); if (organization == null) { this.TempData["error"] = Locale.OrganizationNotFound; return(RedirectToAction("Edit", new { id = id })); } var relationships = new List <EntityRelationship>(); relationships.AddRange(this.GetEntityRelationships <ManufacturedMaterial>(organization.Key.Value, r => (r.RelationshipTypeKey == EntityRelationshipTypeKeys.Instance || r.RelationshipTypeKey == EntityRelationshipTypeKeys.ManufacturedProduct) && r.ObsoleteVersionSequenceId == null).ToList()); organization.Relationships = relationships.Intersect(organization.Relationships, new EntityRelationshipComparer()).ToList(); var model = new EntityRelationshipModel(Guid.NewGuid(), id) { ExistingRelationships = organization.Relationships.Select(r => new EntityRelationshipViewModel(r)).ToList() }; var concepts = new List <Concept> { this.GetConcept(EntityRelationshipTypeKeys.Instance), this.GetConcept(EntityRelationshipTypeKeys.ManufacturedProduct) }; model.RelationshipTypes.AddRange(concepts.ToSelectList(this.HttpContext.GetCurrentLanguage())); return(View(model)); } catch (Exception e) { Trace.TraceError($"Unable to create related place: { e }"); } this.TempData["error"] = Locale.PlaceNotFound; return(RedirectToAction("Edit", new { id = id })); }
override public bool VisitCollection(QueryBuilderMappingParser.CollectionContext context) { if (Pass == 3) { MongoDBCollection collection = new MongoDBCollection(context.name.Text); List <MapRule> mapRules = new List <MapRule>(); if (context.erRefs() != null) { foreach (var er in context.erRefs().erRef()) { try { var erElement = EntityRelationshipModel.FindByName(er.refName.Text); var isMain = er.main == null ? false : true; MapRule mapRule = new MapRule(erElement, collection, isMain); mapRules.Add(mapRule); } catch (Exception) { Errors.Add($"Error 005: (line {er.refName.Line}:{er.refName.Column}): referenced ER element '{er.refName.Text}' not found!"); } } } foreach (var f in context.field()) { VisitField("", f, collection, mapRules); } MongoDBSchema.Collections.Add(collection); foreach (var mr in mapRules) { ERMongoMapping.Rules.Add(mr); } } return(true); }
override public bool VisitRelationship(QueryBuilderMappingParser.RelationshipContext context) { if (Pass == 2) { // RelationshipCardinality deveria fazer parte de RelationshipConnection Relationship relationship = new Relationship(context.name.Text); foreach (QueryBuilderMappingParser.AttributeContext ac in context.attribute()) { relationship.AddAttribute(ac.name.Text, ac.type.Text, ac.multivalued != null, ac.primarykey != null); } foreach (var end in context.relationshipEnd()) { RelationshipEnd rend = new RelationshipEnd(); try { var target = EntityRelationshipModel.FindByName(end.name.Text); if (target.GetType() != typeof(Entity)) { Errors.Add($"Error 003: (line {end.name.Line}:{end.name.Column}): relationship end '{end.name.Text}' is not an Entity!"); } else { rend.TargetEntity = (Entity)target; relationship.AddRelationshipEnd(rend); } } catch (Exception) { Errors.Add($"Error 004: (line {end.name.Line}:{end.name.Column}): relationship end '{end.name.Text}' not found!"); } } EntityRelationshipModel.Elements.Add(relationship); } return(true); }
public ActionResult EditRelatedManufacturedMaterial(Guid id) { try { var material = this.entityService.Get <Material>(id); if (material == null) { this.TempData["error"] = Locale.PlaceNotFound; return(RedirectToAction("Edit", new { id = id })); } for (var i = 0; i < material.Relationships.Count(r => r.RelationshipTypeKey == EntityRelationshipTypeKeys.Instance && r.TargetEntity == null && r.TargetEntityKey.HasValue); i++) { material.Relationships[i].TargetEntity = this.entityService.Get <ManufacturedMaterial>(material.Relationships[i].TargetEntityKey.Value, null) as ManufacturedMaterial; } var model = new EntityRelationshipModel(Guid.NewGuid(), id) { ExistingRelationships = material.Relationships.Select(r => new EntityRelationshipViewModel(r)).ToList() }; model.RelationshipTypes.AddRange(this.conceptService.GetConceptSet(ConceptSetKeys.EntityRelationshipType).Concepts.ToSelectList(this.HttpContext.GetCurrentLanguage(), c => c.Key == EntityRelationshipTypeKeys.OwnedEntity).ToList()); return(View(model)); } catch (Exception e) { Trace.TraceError($"Unable to retrieve related manufactured material: { e }"); } this.TempData["error"] = Locale.PlaceNotFound; return(RedirectToAction("Edit", new { id = id })); }
public ActionResult CreateRelatedPlace(EntityRelationshipModel model) { try { if (this.ModelState.IsValid) { Place place = null; if (model.Inverse) { place = this.GetEntity <Place>(Guid.Parse(model.TargetId)); } else { place = this.GetEntity <Place>(model.SourceId); } if (place == null) { Trace.TraceWarning("Could not locate entity {0}", model.SourceId); this.TempData["error"] = Locale.UnableToCreateRelatedPlace; return(RedirectToAction("Edit", new { id = model.SourceId })); } if (Guid.Parse(model.RelationshipType) == EntityRelationshipTypeKeys.Parent) { place.Relationships.RemoveAll(r => r.TargetEntityKey == Guid.Parse(model.TargetId) && r.RelationshipTypeKey == Guid.Parse(model.RelationshipType)); } place.Relationships.Add(new EntityRelationship(Guid.Parse(model.RelationshipType), model.Inverse ? model.SourceId : Guid.Parse(model.TargetId)) { EffectiveVersionSequenceId = place.VersionSequence, Key = Guid.NewGuid(), Quantity = model.Quantity ?? 0, SourceEntityKey = model.SourceId }); this.ImsiClient.Update(place); this.TempData["success"] = Locale.RelatedPlaceCreatedSuccessfully; return(RedirectToAction("Edit", new { id = place.Key.Value })); } } catch (Exception e) { Trace.TraceError($"Unable to create related place: { e }"); } var concepts = new List <Concept> { this.GetConcept(EntityRelationshipTypeKeys.Child), this.GetConcept(EntityRelationshipTypeKeys.Parent) }; Guid relationshipType; if (Guid.TryParse(model.RelationshipType, out relationshipType)) { model.RelationshipTypes.AddRange(concepts.ToSelectList(this.HttpContext.GetCurrentLanguage(), c => c.Key == relationshipType)); } this.TempData["error"] = Locale.UnableToCreateRelatedPlace; return(View(model)); }
private List <EntityModel> CreateDefaultEntities() { List <EntityModel> defaultEntList = new List <EntityModel>(); //Continue in attribute24 and relationship7 #region BlobFile_mb EntityModel blobFileEntity = new EntityModel(this); blobFileEntity.Name = "BlobFile_mb"; blobFileEntity.Attributes.Clear(); EntityAttributeModel attribute1 = new EntityAttributeModel(blobFileEntity); attribute1.Name = "blob_mb"; attribute1.AttributeType = AttributeType.BLOB; attribute1.AttributeInfo = new AttributeInfo(); blobFileEntity.Attributes.Add(attribute1); EntityRelationshipModel relationship1 = new EntityRelationshipModel(); relationship1.Name = "blobInfo_mb"; relationship1.InverseRelationshipName = "blobFile_mb"; relationship1.TargetTableName = "BlobInfo_mb"; relationship1.SupportMultipleRelationships = false; blobFileEntity.Relationships.Add(relationship1); EntityRelationshipModel relationship6 = new EntityRelationshipModel(); relationship6.Name = "blobInfoSmall_mb"; relationship6.InverseRelationshipName = "smallBlobFile_mb"; relationship6.TargetTableName = "BlobInfo_mb"; relationship6.SupportMultipleRelationships = false; blobFileEntity.Relationships.Add(relationship6); defaultEntList.Add(blobFileEntity); #endregion #region BlobInfo_mb EntityModel blobInfoEntity = new EntityModel(this); blobInfoEntity.Name = "BlobInfo_mb"; blobInfoEntity.Attributes.Clear(); EntityAttributeModel attribute2 = new EntityAttributeModel(blobInfoEntity); attribute2.Name = "blobStatus_mb"; attribute2.AttributeType = AttributeType.Integer16; attribute2.AttributeInfo = new AttributeInfoInteger(); blobInfoEntity.Attributes.Add(attribute2); EntityAttributeModel attribute3 = new EntityAttributeModel(blobInfoEntity); attribute3.Name = "errorMsg_mb"; attribute3.AttributeType = AttributeType.String; attribute3.AttributeInfo = new AttributeInfoString(); blobInfoEntity.Attributes.Add(attribute3); EntityAttributeModel attribute4 = new EntityAttributeModel(blobInfoEntity); attribute4.Name = "guid_mb"; attribute4.AttributeType = AttributeType.String; attribute4.AttributeInfo = new AttributeInfoString(); attribute4.AttributeInfo.IsIndexed = true; blobInfoEntity.Attributes.Add(attribute4); EntityAttributeModel attribute20 = new EntityAttributeModel(blobInfoEntity); attribute20.Name = "location_mb"; attribute20.AttributeType = AttributeType.String; attribute20.AttributeInfo = new AttributeInfoString(); blobInfoEntity.Attributes.Add(attribute20); EntityRelationshipModel relationship2 = new EntityRelationshipModel(); relationship2.Name = "blobFile_mb"; relationship2.InverseRelationshipName = "blobInfo_mb"; relationship2.TargetTableName = "BlobFile_mb"; relationship2.SupportMultipleRelationships = false; relationship2.DeletionRule = "Cascade"; blobInfoEntity.Relationships.Add(relationship2); EntityRelationshipModel relationship5 = new EntityRelationshipModel(); relationship5.Name = "smallBlobFile_mb"; relationship5.InverseRelationshipName = "blobInfoSmall_mb"; relationship5.TargetTableName = "BlobFile_mb"; relationship5.SupportMultipleRelationships = false; relationship5.DeletionRule = "Cascade"; blobInfoEntity.Relationships.Add(relationship5); defaultEntList.Add(blobInfoEntity); #endregion #region ConflictError_mb EntityModel conflictErrorEntity = new EntityModel(this); conflictErrorEntity.Name = "ConflictError_mb"; conflictErrorEntity.Attributes.Clear(); EntityAttributeModel attribute5 = new EntityAttributeModel(conflictErrorEntity); attribute5.Name = "entityGuid_mb"; attribute5.AttributeType = AttributeType.String; attribute5.AttributeInfo = new AttributeInfoString(); attribute5.AttributeInfo.IsIndexed = true; conflictErrorEntity.Attributes.Add(attribute5); EntityAttributeModel attribute6 = new EntityAttributeModel(conflictErrorEntity); attribute6.Name = "entityKind_mb"; attribute6.AttributeType = AttributeType.String; attribute6.AttributeInfo = new AttributeInfoString(); attribute6.AttributeInfo.IsIndexed = true; conflictErrorEntity.Attributes.Add(attribute6); EntityAttributeModel attribute7 = new EntityAttributeModel(conflictErrorEntity); attribute7.Name = "modifiedFields_mb"; attribute7.AttributeType = AttributeType.String; attribute7.AttributeInfo = new AttributeInfoString(); conflictErrorEntity.Attributes.Add(attribute7); defaultEntList.Add(conflictErrorEntity); #endregion #region DeviceNotification_mb EntityModel deviceNotificationEntity = new EntityModel(this); deviceNotificationEntity.Name = "DeviceNotification_mb"; deviceNotificationEntity.Attributes.Clear(); EntityAttributeModel attribute8 = new EntityAttributeModel(deviceNotificationEntity); attribute8.Name = "deviceId_mb"; attribute8.AttributeType = AttributeType.String; attribute8.AttributeInfo = new AttributeInfoString(); deviceNotificationEntity.Attributes.Add(attribute8); EntityAttributeModel attribute9 = new EntityAttributeModel(deviceNotificationEntity); attribute9.Name = "isNewToken_mb"; attribute9.AttributeType = AttributeType.Boolean; attribute9.AttributeInfo = new AttributeInfo(); deviceNotificationEntity.Attributes.Add(attribute9); EntityAttributeModel attribute10 = new EntityAttributeModel(deviceNotificationEntity); attribute10.Name = "token_mb"; attribute10.AttributeType = AttributeType.String; attribute10.AttributeInfo = new AttributeInfoString(); deviceNotificationEntity.Attributes.Add(attribute10); defaultEntList.Add(deviceNotificationEntity); #endregion #region DownloadSyncChunk_mb EntityModel downloadSyncChunkEntity = new EntityModel(this); downloadSyncChunkEntity.Name = "DownloadSyncChunk_mb"; downloadSyncChunkEntity.Attributes.Clear(); EntityAttributeModel attribute11 = new EntityAttributeModel(downloadSyncChunkEntity); attribute11.Name = "downloadId_mb"; attribute11.AttributeType = AttributeType.Integer64; attribute11.AttributeInfo = new AttributeInfoInteger(); attribute11.AttributeInfo.IsIndexed = true; downloadSyncChunkEntity.Attributes.Add(attribute11); EntityAttributeModel attribute12 = new EntityAttributeModel(downloadSyncChunkEntity); attribute12.Name = "guid_mb"; attribute12.AttributeType = AttributeType.String; attribute12.AttributeInfo = new AttributeInfoString(); attribute12.AttributeInfo.IsIndexed = true; downloadSyncChunkEntity.Attributes.Add(attribute12); EntityAttributeModel attribute13 = new EntityAttributeModel(downloadSyncChunkEntity); attribute13.Name = "jsonMessage_mb"; attribute13.AttributeType = AttributeType.BLOB; attribute13.AttributeInfo = new AttributeInfoString(); downloadSyncChunkEntity.Attributes.Add(attribute13); EntityAttributeModel attribute23 = new EntityAttributeModel(downloadSyncChunkEntity); attribute23.Name = "chunkOrder"; attribute23.AttributeType = AttributeType.Integer32; attribute23.AttributeInfo = new AttributeInfoInteger(); downloadSyncChunkEntity.Attributes.Add(attribute23); EntityRelationshipModel relationship3 = new EntityRelationshipModel(); relationship3.Name = "downloadSyncHeader_mb"; relationship3.InverseRelationshipName = "downloadSyncChunk_mb"; relationship3.TargetTableName = "DownloadSyncHeader_mb"; relationship3.SupportMultipleRelationships = false; downloadSyncChunkEntity.Relationships.Add(relationship3); defaultEntList.Add(downloadSyncChunkEntity); #endregion #region DownloadSyncHeader_mb EntityModel downloadSyncHeaderEntity = new EntityModel(this); downloadSyncHeaderEntity.Name = "DownloadSyncHeader_mb"; downloadSyncHeaderEntity.Attributes.Clear(); EntityAttributeModel attribute14 = new EntityAttributeModel(downloadSyncHeaderEntity); attribute14.Name = "downloadedEntities_mb"; attribute14.AttributeType = AttributeType.Integer32; attribute14.AttributeInfo = new AttributeInfoInteger(); downloadSyncHeaderEntity.Attributes.Add(attribute14); EntityAttributeModel attribute15 = new EntityAttributeModel(downloadSyncHeaderEntity); attribute15.Name = "downloadId_mb"; attribute15.AttributeType = AttributeType.Integer64; attribute15.AttributeInfo = new AttributeInfoInteger(); downloadSyncHeaderEntity.Attributes.Add(attribute15); EntityAttributeModel attribute16 = new EntityAttributeModel(downloadSyncHeaderEntity); attribute16.Name = "numRecs_mb"; attribute16.AttributeType = AttributeType.Integer32; attribute16.AttributeInfo = new AttributeInfoInteger(); downloadSyncHeaderEntity.Attributes.Add(attribute16); EntityAttributeModel attribute17 = new EntityAttributeModel(downloadSyncHeaderEntity); attribute17.Name = "sourceTable_mb"; attribute17.AttributeType = AttributeType.String; attribute17.AttributeInfo = new AttributeInfoString(); downloadSyncHeaderEntity.Attributes.Add(attribute17); EntityAttributeModel attribute18 = new EntityAttributeModel(downloadSyncHeaderEntity); attribute18.Name = "syncDate_mb"; attribute18.AttributeType = AttributeType.Double; attribute18.AttributeInfo = new AttributeInfoDouble(); downloadSyncHeaderEntity.Attributes.Add(attribute18); EntityAttributeModel attribute19 = new EntityAttributeModel(downloadSyncHeaderEntity); attribute19.Name = "syncOrder_mb"; attribute19.AttributeType = AttributeType.Integer32; attribute19.AttributeInfo = new AttributeInfoInteger(); downloadSyncHeaderEntity.Attributes.Add(attribute19); EntityAttributeModel attribute22 = new EntityAttributeModel(downloadSyncHeaderEntity); attribute22.Name = "guid_mb"; attribute22.AttributeType = AttributeType.String; attribute22.AttributeInfo = new AttributeInfoString(); attribute22.AttributeInfo.IsIndexed = true; downloadSyncHeaderEntity.Attributes.Add(attribute22); attribute22 = new EntityAttributeModel(downloadSyncHeaderEntity); attribute22.Name = "syncType_mb"; attribute22.AttributeType = AttributeType.Integer32; attribute22.AttributeInfo = new AttributeInfoInteger(); attribute22.AttributeInfo.IsIndexed = true; downloadSyncHeaderEntity.Attributes.Add(attribute22); EntityRelationshipModel relationship4 = new EntityRelationshipModel(); relationship4.Name = "downloadSyncChunk_mb"; relationship4.InverseRelationshipName = "downloadSyncHeader_mb"; relationship4.TargetTableName = "DownloadSyncChunk_mb"; relationship4.SupportMultipleRelationships = true; downloadSyncHeaderEntity.Relationships.Add(relationship4); defaultEntList.Add(downloadSyncHeaderEntity); #endregion #region LastSync_mb EntityModel lastSyncEntity = new EntityModel(this); lastSyncEntity.Name = "LastSync_mb"; lastSyncEntity.Attributes.Clear(); EntityAttributeModel attribute21 = new EntityAttributeModel(lastSyncEntity); attribute21.Name = "serverDate_mb"; attribute21.AttributeType = AttributeType.Double; attribute21.AttributeInfo = new AttributeInfoDouble(); lastSyncEntity.Attributes.Add(attribute21); defaultEntList.Add(lastSyncEntity); #endregion return(defaultEntList); }
public ActionResult CreateRelatedManufacturedMaterial(EntityRelationshipModel model) { var concepts = new List <Concept>(); try { if (this.ModelState.IsValid) { // HACK: manually validating the quantity field, since for this particular page the quantity is required // but it feels like overkill to literally create the same model for the purpose of making only 1 property // required. if (!model.Quantity.HasValue) { this.ModelState.AddModelError(nameof(model.Quantity), Locale.QuantityRequired); this.TempData["error"] = Locale.QuantityRequired; concepts.Add(this.conceptService.GetConcept(EntityRelationshipTypeKeys.Instance, true)); // re-populate the model var existingMaterial = this.entityService.Get <Material>(model.SourceId); var relationships = new List <EntityRelationship>(); relationships.AddRange(this.entityService.GetEntityRelationships <ManufacturedMaterial>(existingMaterial.Key.Value, r => r.RelationshipTypeKey == EntityRelationshipTypeKeys.Instance && r.ObsoleteVersionSequenceId == null).ToList()); existingMaterial.Relationships = relationships.Intersect(existingMaterial.Relationships, new EntityRelationshipComparer()).ToList(); model.ExistingRelationships = existingMaterial.Relationships.Select(r => new EntityRelationshipViewModel(r)).ToList(); model.RelationshipTypes.AddRange(concepts.ToSelectList(this.HttpContext.GetCurrentLanguage(), c => c.Key == EntityRelationshipTypeKeys.Instance)); return(View(model)); } var material = this.entityService.Get <Material>(model.SourceId); if (material == null) { this.TempData["error"] = Locale.UnableToCreateRelatedManufacturedMaterial; return(RedirectToAction("Edit", new { id = model.SourceId })); } material.Relationships.RemoveAll(r => r.TargetEntityKey == Guid.Parse(model.TargetId) && r.RelationshipTypeKey == Guid.Parse(model.RelationshipType)); material.Relationships.Add(new EntityRelationship(Guid.Parse(model.RelationshipType), Guid.Parse(model.TargetId)) { Key = Guid.NewGuid(), Quantity = model.Quantity ?? 0, SourceEntityKey = model.SourceId }); var updatedMaterial = this.entityService.Update(material); this.TempData["success"] = Locale.RelatedManufacturedMaterialCreatedSuccessfully; return(RedirectToAction("Edit", new { id = updatedMaterial.Key.Value, versionId = updatedMaterial.VersionKey })); } } catch (Exception e) { Trace.TraceError($"Unable to create related manufactured material: { e }"); } this.TempData["error"] = Locale.UnableToCreateRelatedManufacturedMaterial; concepts.Add(this.conceptService.GetConcept(EntityRelationshipTypeKeys.Instance, true)); model.RelationshipTypes.AddRange(concepts.ToSelectList(this.HttpContext.GetCurrentLanguage(), c => c.Key == EntityRelationshipTypeKeys.Instance)); return(View(model)); }
/// <summary> /// Create a new dynamic entity strongly typed class base on a xsd document /// </summary> /// <param name="entityDefinition">Entity Definition to be parsed</param> /// <param name="timestamp">timestamp of the last version of the XSD definition</param> /// <returns>return the dynamic entity generate type base on the xsd.</returns> public static Type CreateTypeFromEntityDefinition(DatabaseModel ParentModel, EntityModel entityDefinition, DateTime timestamp) { lock (currentTypes) { string entityName = entityDefinition.Name; //System.Xml.Schema.XmlSchemaSet x; //x = new System.Xml.Schema.XmlSchemaSet(); entityName = entityName.Replace(".", "_"); Type currentType = DynamicEntityTypeManager.GetType(entityName); if (currentType == null) { if (moduleBuilder == null) { InitData(ParentModel); } TypeBuilder typeBuilder = GetTypeBuilder(entityName); Type[] constructorArgTypes = new Type[] { }; ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, constructorArgTypes); Type[] baseConstructorArgTypes = new Type[] { typeof(string) }; ILGenerator ilGenerator = constructorBuilder.GetILGenerator(); ConstructorInfo baseConstructor = typeof(DynamicEntityBase).GetConstructor(baseConstructorArgTypes); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldstr, entityDefinition.Name); ilGenerator.Emit(OpCodes.Call, baseConstructor); ilGenerator.Emit(OpCodes.Ret); // add copy constructor /* constructorArgTypes = new Type[] { typeof(DynamicEntityBase) }; * ConstructorBuilder constructorBuilder2 = typeBuilder.DefineConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, constructorArgTypes); * Type[] baseConstructorArgTypes2 = new Type[] { typeof(DynamicEntityBase) }; * ConstructorInfo baseConstructor2 = typeof(DynamicEntityBase).GetConstructor(baseConstructorArgTypes2); * ilGenerator = constructorBuilder2.GetILGenerator(); * ilGenerator.Emit(OpCodes.Ldarg_0); * ilGenerator.Emit(OpCodes.Ldarg_1); * ilGenerator.Emit(OpCodes.Call, baseConstructor2); * ilGenerator.Emit(OpCodes.Nop); * ilGenerator.Emit(OpCodes.Nop); * ilGenerator.Emit(OpCodes.Nop); * ilGenerator.Emit(OpCodes.Ret);*/ foreach (EntityAttributeModel element in entityDefinition.Attributes) { string propName = element.Name; if (propertNameRegex.IsMatch(propName, 0)) { // gets the type code of the property, and ensure that they don't have the namespace to basic schema definition Type propType = DynamicEntityTypeManager.GetClrTypeFromPropertyType(element.AttributeType); CreateProperty(typeBuilder, propName, propType, element.AttributeInfo.IsPrimaryKey, element.AttributeInfo.IsPrimaryKey, null); } else { throw new ArgumentException( @"Each property name must be alphanumeric and start with character."); } } // temporary type info for forward reference currentTypes[entityName] = new DynamicTypeInfo(entityName, timestamp, typeBuilder); foreach (EntityRelationshipModel relationship in entityDefinition.Relationships) { string propName = relationship.Name; if (propertNameRegex.IsMatch(propName, 0)) { // gets the type code of the property, and ensure that they don't have the namespace to basic schema definition Type relationshipType = DynamicEntityTypeManager.GetType(relationship.TargetTableName); EntityModel targetEntityDefinition = ParentModel.Entities.FirstOrDefault(entity => entity.Name == relationship.TargetTableName); bool isBuildChild = false; if (relationshipType == null) { if (relationship.TargetTableName == entityDefinition.Name) { // reference this same type being build relationshipType = typeBuilder; isBuildChild = false; } else { // create the type look for the model entity definition relationshipType = CreateTypeFromEntityDefinition(ParentModel, targetEntityDefinition, DateTime.UtcNow); isBuildChild = true; } } Type propType = null; List <CustomAttributeBuilder> additionalAttributes = new List <CustomAttributeBuilder>(); if (relationship.SupportMultipleRelationships) { propType = typeof(ICollection <>).MakeGenericType(relationshipType); if (!string.IsNullOrEmpty(relationship.InverseRelationshipName)) { EntityRelationshipModel inverseRelation = targetEntityDefinition.Relationships.FirstOrDefault(rel => rel.Name == relationship.InverseRelationshipName); ConstructorInfo attributeConstructor = null; CustomAttributeBuilder attributeBuilder = null; if (inverseRelation != null) { attributeConstructor = typeof(System.ComponentModel.DataAnnotations.Schema.InversePropertyAttribute).GetConstructor(new Type[] { typeof(string) }); attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { relationship.InverseRelationshipName }, new FieldInfo[] { }, new object[] { }); additionalAttributes.Add(attributeBuilder); } } } else { propType = relationshipType; if (!string.IsNullOrEmpty(relationship.InverseRelationshipName)) { EntityRelationshipModel inverseRelation = targetEntityDefinition.Relationships.FirstOrDefault(rel => rel.Name == relationship.InverseRelationshipName); ConstructorInfo attributeConstructor = null; CustomAttributeBuilder attributeBuilder = null; if (inverseRelation != null) { attributeConstructor = typeof(System.ComponentModel.DataAnnotations.Schema.InversePropertyAttribute).GetConstructor(new Type[] { typeof(string) }); attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { relationship.InverseRelationshipName }, new FieldInfo[] { }, new object[] { }); additionalAttributes.Add(attributeBuilder); } //create foreign key field EntityAttributeModel targetKey = targetEntityDefinition.Attributes.FirstOrDefault(a => a.AttributeInfo.IsPrimaryKey); if (targetKey != null) { string fkFieldName = relationship.Name + "_FK"; attributeConstructor = typeof(System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute).GetConstructor(new Type[] { typeof(string) }); if (inverseRelation.SupportMultipleRelationships) { CreateProperty(typeBuilder, fkFieldName, DynamicEntityTypeManager.GetClrTypeFromPropertyType(targetKey.AttributeType), false, true, null); attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { fkFieldName }, new FieldInfo[] { }, new object[] { }); } else { attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { targetKey.Name }, new FieldInfo[] { }, new object[] { }); } additionalAttributes.Add(attributeBuilder); } } } if (propType == null) { throw new NullReferenceException(string.Format("The relationship {0} type can't be determinate", relationship.Name)); } CreateProperty(typeBuilder, propName, propType, false, !isBuildChild, additionalAttributes); } else { throw new ArgumentException( @"Each property name must be alphanumeric and start with character."); } } Type newType = typeBuilder.CreateType(); //currentTypes[resourceFriendlyName] = newType; currentTypes[entityName] = new DynamicTypeInfo(entityName, timestamp, newType); //assemblyBuilder.Save("BLTypeManagerTest.dll"); return(newType); } return(currentType); } }