154 lines
5.2 KiB
VB.net

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 EnvelopeDocument
Public Property Elements As New List(Of DocumentReceiverElement)
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 EnvelopeDocument)
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 DocumentReceiverElement
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 DocumentReceiverElement
Dim oElement = Elements.Where(Function(e) pGuid = e.Id).SingleOrDefault()
Return oElement
End Function
Public Function GetElement(pAnnotation As AnnotationStickyNote) As DocumentReceiverElement
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 DocumentReceiverElement() 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 DocumentReceiverElement)
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 DocumentReceiverElement) 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 DocumentReceiverElement) As Boolean
Try
' Element aus Datenbank löschen
If ElementModel.DeleteElement(pElement) Then
Dim oElement = New List(Of DocumentReceiverElement)() 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