/// <summary> /// Returns a FramedImageDTO that matches the given id parameter. /// </summary> /// <param name="id"></param> /// <returns>FramedImageDTO</returns> /// <exception cref="Exception">Will throw if errors occured while reading from persistent storage.</exception> public FramedImageDTO GetById(int id) { try { FramedImageModel model = framedImagedPersistence.GetById(id); return(model == null ? null : new FramedImageDTO { Id = model.Id, ImageFilepath = ImageDirectoryPath + "\\" + model.Filename, Witdh = model.Witdh, Height = model.Height, LocationX = model.LocationX, LocationY = model.LocationY, FrameThickness = model.FrameThickness, RotateAngle = model.RotateAngle, Caption = model.Caption, EnableCaption = model.EnableCaption }); } catch (Exception ex) { logger.LogException(ex); throw READ_ERROR; } }
/// <summary> /// Saves a FramedImageDTO to a persistent storage /// </summary> /// <param name="framedImage"></param> /// <exception cref="Exception">Will throw if errors occured while saving the FramedImageDTO to a persistent storage.</exception> /// <exception cref="Exception">Will throw if received file is not an image.</exception> public void Save(FramedImageDTO framedImage) { if (framedImage != null) { try { FileStream fileStream = new FileStream(framedImage.ImageFilepath, FileMode.Open, FileAccess.Read); if (fileStream.IsImage()) { string ext = framedImage.ImageFilepath.Substring(framedImage.ImageFilepath.LastIndexOf(".") + 1); int imageNumber = framedImagedPersistence.GetCount(); string datetime = DateTime.Now.Ticks.ToString(); string newFilename = String.Format("Image-{0}-{1}.{2}", imageNumber++, datetime, ext); FramedImageModel model = new FramedImageModel { Id = framedImage.Id, Filename = newFilename, Witdh = framedImage.Witdh, Height = framedImage.Height, LocationX = framedImage.LocationX, LocationY = framedImage.LocationY, FrameThickness = framedImage.FrameThickness, RotateAngle = framedImage.RotateAngle, Caption = framedImage.Caption, EnableCaption = framedImage.EnableCaption, EnableShadow = framedImage.EnableShadow, ShadowOpacity = framedImage.ShadowOpacity, ShadowDepth = framedImage.ShadowDepth, ShadowDirection = framedImage.ShadowDirection, ShadowBlurRadius = framedImage.ShadowBlurRadius }; framedImagedPersistence.Save(model); framedImage.Id = model.Id; CopyImage(framedImage.ImageFilepath, newFilename); framedImage.ImageFilepath = ImageDirectoryPath + "\\" + model.Filename; fileStream.Close(); fileStream.Dispose(); } else { throw FILE_NOT_IMAGE_ERROR; } } catch (Exception ex) { logger.LogException(ex); throw SAVE_ERROR; } } }
public override int Save(FramedImageModel model) { if (model == null) { throw new ArgumentNullException("model"); } int id = 0; try { XDocument document = XDocument.Load(XML_DOCUMENT_LOCATION); XElement UserImages = document.Element(MODEL_ELEMENT_COLLECTION_NAME); id = GenerateNextId(); XElement element = new XElement ( MODEL_ELEMENT_NAME, new XAttribute("Id", model.Id = id), new XElement("Filename", model.Filename), new XAttribute("Width", model.Witdh), new XAttribute("Height", model.Height), new XAttribute("LocationX", model.LocationX), new XAttribute("LocationY", model.LocationY), new XAttribute("FrameThickness", model.FrameThickness), new XAttribute("RotateAngle", model.RotateAngle), new XElement("Caption", new XAttribute("Enabled", model.EnableCaption), new XAttribute("Text", model.Caption)), new XElement("Shadow", new XAttribute("Enabled", model.EnableShadow), new XAttribute("Opacity", model.ShadowOpacity), new XAttribute("Depth", model.ShadowDepth), new XAttribute("Direction", model.ShadowDirection), new XAttribute("BlurRadius", model.ShadowBlurRadius)) ); UserImages.AddFirst(element); using (Stream fs = new FileStream(XML_DOCUMENT_LOCATION, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 1024, FileOptions.WriteThrough)) { document.Save(fs); } } catch { id = 0; throw; } return(id); }
public override FramedImageModel GetById(int id) { FramedImageModel returnValue = null; XDocument document = XDocument.Load(XML_DOCUMENT_LOCATION); XElement UserImages = document.Element(MODEL_ELEMENT_COLLECTION_NAME); XElement userImage = (from image in UserImages.Elements() where image.Attribute("Id").Value == id.ToString() select image).FirstOrDefault(); if (userImage != null) { returnValue = new FramedImageModel(); returnValue.Id = int.Parse(userImage.Attribute("Id").Value); returnValue.Filename = userImage.Element("Filename").Value; returnValue.Witdh = (double)userImage.Attribute("Width"); returnValue.Height = (double)userImage.Attribute("Height"); returnValue.LocationX = (double)userImage.Attribute("LocationX"); returnValue.LocationY = (double)userImage.Attribute("LocationY"); returnValue.FrameThickness = (int)userImage.Attribute("FrameThickness"); returnValue.RotateAngle = int.Parse(userImage.Attribute("RotateAngle").Value); XElement captionElement = userImage.Element("Caption"); returnValue.Caption = captionElement.Attribute("Text")?.Value ?? ""; returnValue.EnableCaption = (bool)captionElement.Attribute("Enabled"); XElement shadowElement = userImage.Element("Shadow"); returnValue.EnableShadow = (bool)shadowElement.Attribute("Enabled"); returnValue.ShadowOpacity = (double)shadowElement.Attribute("Opacity"); returnValue.ShadowDepth = (double)shadowElement.Attribute("Depth"); returnValue.ShadowDirection = (double)shadowElement.Attribute("Direction"); returnValue.ShadowBlurRadius = (double)shadowElement.Attribute("BlurRadius"); } return(returnValue); }
public override void Edit(int id, FramedImageModel updatedModel) { if (updatedModel == null) { throw new ArgumentNullException("updatedModel"); } XDocument document = XDocument.Load(XML_DOCUMENT_LOCATION); XElement UserImages = document.Element(MODEL_ELEMENT_COLLECTION_NAME); XElement userImage = (from image in UserImages.Elements() where image.Attribute("Id").Value == id.ToString() select image).FirstOrDefault(); userImage.SetElementValue("Filename", updatedModel.Filename); userImage.SetAttributeValue("Width", updatedModel.Witdh); userImage.SetAttributeValue("Height", updatedModel.Height); userImage.SetAttributeValue("LocationX", updatedModel.LocationX); userImage.SetAttributeValue("LocationY", updatedModel.LocationY); userImage.SetAttributeValue("FrameThickness", updatedModel.FrameThickness); userImage.SetAttributeValue("RotateAngle", updatedModel.RotateAngle); userImage.Element("Caption").SetAttributeValue("Enabled", updatedModel.EnableCaption); userImage.Element("Caption").SetAttributeValue("Text", updatedModel.Caption); userImage.Element("Shadow").SetAttributeValue("Enabled", updatedModel.EnableShadow); userImage.Element("Shadow").SetAttributeValue("Opacity", updatedModel.ShadowOpacity); userImage.Element("Shadow").SetAttributeValue("Depth", updatedModel.ShadowDepth); userImage.Element("Shadow").SetAttributeValue("Direction", updatedModel.ShadowDirection); userImage.Element("Shadow").SetAttributeValue("BlurRadius", updatedModel.ShadowBlurRadius); using (Stream fs = new FileStream(XML_DOCUMENT_LOCATION, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 1024, FileOptions.WriteThrough)) { document.Save(fs); } }