Imports DevExpress.XtraPrinting.Native Imports EnvelopeGenerator.CommonServices Imports GdPicture14.Annotations Imports EnvelopeGenerator.Domain Imports EnvelopeGenerator.Domain.Entities Public Class FieldEditorController Inherits BaseController Private ReadOnly Document As Document Public Property Elements As New List(Of Signature) Public Class ElementInfo Public ReceiverId As Integer Public Page As Integer Public Guid As Integer End Class Public Sub New(pState As State, pDocument As Document) MyBase.New(pState) Document = pDocument ElementModel = New ElementModel(pState) End Sub Public Function GetElementInfo(pAnnotationTag As String) As ElementInfo Dim oTag As String() = pAnnotationTag.Split("|"c) Dim oReceiverId = Integer.Parse(oTag(0)) Dim oPage = Integer.Parse(oTag(1)) Dim oGuid = Integer.Parse(oTag(2)) Return New ElementInfo With { .Guid = oGuid, .Page = oPage, .ReceiverId = oReceiverId } End Function Private Function GetElementByPosition(pAnnotation As AnnotationStickyNote, pPage As Integer, pReceiverId As Integer) As Signature Dim oElement = Elements. Where(Function(e) Return e.Left = CSng(Math.Round(pAnnotation.Left, 5)) And e.Top = CSng(Math.Round(pAnnotation.Top, 5)) And e.Page = pPage And e.ReceiverId = pReceiverId End Function).SingleOrDefault() Return oElement End Function Private Function GetElementByGuid(pGuid As Integer) As Signature Dim oElement = Elements.Where(Function(e) pGuid = e.Id).SingleOrDefault() Return oElement End Function Public Function GetElement(pAnnotation As AnnotationStickyNote) As Signature Dim oInfo = GetElementInfo(pAnnotation.Tag) If oInfo.Guid = -1 Then Return GetElementByPosition(pAnnotation, oInfo.Page, oInfo.ReceiverId) Else Return GetElementByGuid(oInfo.Guid) End If End Function Public Sub AddOrUpdateElement(pAnnotation As AnnotationStickyNote, pPageOrientation As PageOrientation) Dim oElement = GetElement(pAnnotation) Dim oAnnotationHeight As Single = pAnnotation.Height Dim oAnnotationWidth As Single = pAnnotation.Width 'If pPageOrientation = PageOrientation.Portrait Then ' oAnnotationHeight = pAnnotation.Height ' oAnnotationWidth = pAnnotation.Width 'Else ' oAnnotationHeight = pAnnotation.Width ' oAnnotationWidth = pAnnotation.Height 'End If If oElement IsNot Nothing Then oElement.Height = oAnnotationHeight oElement.Width = oAnnotationWidth oElement.X = pAnnotation.Left oElement.Y = pAnnotation.Top Else Dim oInfo = GetElementInfo(pAnnotation.Tag) Elements.Add(New Signature() With { .ElementType = Constants.ElementType.Signature, .Height = oAnnotationHeight, .Width = oAnnotationWidth, .X = pAnnotation.Left, .Y = pAnnotation.Top, .DocumentId = Document.Id, .ReceiverId = oInfo.ReceiverId, .Page = oInfo.Page }) End If End Sub Public Function ClearElements(pPage As Integer, pReceiverId As Integer) As IEnumerable(Of Signature) Return Elements. Where(Function(e) e.Page <> pPage And e.ReceiverId <> pReceiverId). ToList() End Function Public Function LoadElements() As Boolean Elements = ElementModel.List(Document.Id) If Elements Is Nothing Then Return False Else Return True End If End Function Public Function SaveElements(pReceiverId As Integer) As Boolean Return Elements. Where(Function(e) e.ReceiverId = pReceiverId). Select(AddressOf SaveElement). All(Function(pResult) pResult = True) End Function Public Function SaveElement(pElement As Signature) As Boolean Try If pElement.Id > 0 Then Return ElementModel.Update(pElement) Else Return ElementModel.Insert(pElement) End If Catch ex As Exception Logger.Error(ex) Return False End Try End Function Public Function DeleteElement(pElement As Signature) As Boolean Try ' Element aus Datenbank löschen If ElementModel.DeleteElement(pElement) Then Dim oElement = New List(Of Signature)() From {pElement} Elements = Elements.Except(oElement).ToList() Return True Else Logger.Error("Element [{0}] could not be deleted!", pElement.Id) Return False End If Catch ex As Exception Logger.Error(ex) Return False End Try End Function End Class