/// <summary> /// This method is used for creating new content and for updating an existing entity with new attributes that may have been /// created on the document type (attribution schema). /// </summary> /// <param name="contentViewModel"></param> /// <remarks> /// We need to paste the model back together now with the correct Ids since we generated them on the last Action and we've re-generated them again now. /// This is done by looking up the Alias Key/Value pair in the posted form element for each of the model's properties. When the key value pair is found /// we can extract the id that was assigned to it in the HTML markup and re-assign that id to the actual property id so it binds. /// </remarks> protected void ReconstructModelPropertyIds(BasicContentEditorModel contentViewModel) { foreach (var p in contentViewModel.Properties) { var alias = p.Alias; //find the alias form post key/value pair that matches this alias var aliasKey = (Request.Form.AllKeys .Where(x => x.EndsWith("__Alias__") && x.StartsWith(HiveIdExtensions.HtmlIdPrefix)) .Where(key => Request.Form[key] == alias)).SingleOrDefault(); if (aliasKey != null) { //now we can extract the ID that this property was related to var originalHiveId = HiveIdExtensions.TryParseFromHtmlId(aliasKey); //HiveId.TryParse(aliasKey.Split('.')[0].Split('_')[1]);) if (originalHiveId.Success) { //find the new editor model property with the attribute definition so we can update it's Ids to the original Ids so they bind var contentProperty = contentViewModel.Properties.Where(x => x.Alias == alias).Single(); //update the property's ID to the originally generated id contentProperty.Id = originalHiveId.Result; } } //if it was null it means we can't reconstruct or there's been no property data passed in } }
public void ExtensionMethods_GetHtmlId_ThenTryParseFromHtmlId_Success(string input) { var id = HiveId.TryParse(input).Result; var htmlId = id.GetHtmlId() + ".TemplateId"; var fromHtmlId = HiveIdExtensions.TryParseFromHtmlId(htmlId).Result; Assert.AreEqual(id, fromHtmlId, "HtmlId was: " + htmlId); }
public void AddOrUpdate(AbstractEntity persistedEntity) { Mandate.That <ArgumentException>(typeof(File).IsAssignableFrom(persistedEntity.GetType())); var file = (File)persistedEntity; Mandate.That <ArgumentNullException>(!file.Name.IsNullOrWhiteSpace() || !file.Location.IsNullOrWhiteSpace()); // Make sure that the incoming location has the root storage area prefixed if necessary file.Location = EnsureLocationRooted(string.IsNullOrEmpty(file.Location) ? file.Name : file.Location); //ensure the file name is set which should always be based on the location file.Name = Path.GetFileName(file.Location); // Ensure we have an id for the file if (HiveIdExtensions.IsNullValueOrEmpty(file.Id)) { file.Id = GenerateId(file.Location); } // Ensure that the folder exists, if this item is a folder) if (file.IsContainer) { var dir = new DirectoryInfo(file.Location); if (!dir.Exists) { dir.Create(); } } else { var containerPath = Path.GetDirectoryName(file.Location); if (containerPath != null) { var dir = new DirectoryInfo(containerPath); if (!dir.Exists) { dir.Create(); } } } // Write the file, provided it's not a directory if (!file.IsContainer) { var physicalForWriting = new FileInfo(file.Location); if (physicalForWriting.Exists) { physicalForWriting.Delete(); } using (var writer = physicalForWriting.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { writer.Write(file.ContentBytes, 0, file.ContentBytes.Length); } } //var physicalFile = new FileInfo(file.Location); //if (physicalFile.Name != file.Name) //{ // physicalFile.MoveTo(Path.Combine(physicalFile.Directory.FullName, file.Name)); //} // Write relations to disc foreach (var relation in file.Relations) { if (relation.Type.RelationName != FixedRelationTypes.FileRelationType.RelationName) { EnsureRelationsFolderExists(); var sourceMd5 = relation.SourceId.ToString().ToMd5(); var destMd5 = relation.DestinationId.ToString().ToMd5(); var relationPath = Path.Combine(_relationsFolder, sourceMd5 + "-" + destMd5 + ".xml"); var relationFileInfo = new FileInfo(relationPath); var relationContent = RelationSerializer.ToXml(relation); var relationContentBytes = Encoding.UTF8.GetBytes(relationContent.ToString()); if (relationFileInfo.Exists) { relationFileInfo.Delete(); } using ( var writer = relationFileInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { writer.Write(relationContentBytes, 0, relationContentBytes.Length); } } } }