Imports WINDREAMLib Imports WINDREAMLib.WMCOMEvent Imports WINDREAMLib.WMEntity Imports WINDREAMLib.WMObjectEditMode Imports WINDREAMLib.WMSearchOperator Imports WINDREAMLib.WMSearchRelation Imports WMOBRWSLib Imports WMOSRCHLib Imports WMCNNCTDLLLib Imports WMOTOOLLib Imports NLog ''' ''' MODULE: Windream ''' ''' VERSION: 0.0.0.1 ''' ''' DATE: 24.08.2018 ''' ''' DESCRIPTION: ''' ''' DEPENDENCIES: NLog, >= 4.5.8 ''' ''' PARAMETERS: ''' ''' PROPERTIES: ''' ''' EXAMPLES: ''' ''' REMARKS: ''' Public Class Windream2 #Region "Private Properties" Private _logger As Logger Private _loggerFactory As LogFactory Private ReadOnly _sessionDomain As String Private ReadOnly _sessionPassword As String Private ReadOnly _sessionUsername As String #End Region #Region "Public Properties" Public ReadOnly Property ClientDriveLetter As String Public ReadOnly Property ClientSupport64Bit As Boolean Public ReadOnly Property Session As WMSession Public ReadOnly Property SessionLoggedin As Boolean Public ReadOnly Property SessionReconnect As Boolean Public ReadOnly Property SessionServername As String ''' A list of object types that are available Public ReadOnly Property ObjectTypes As List(Of String) Get Dim types As WMObjects = GetObjectTypes() Dim list As New List(Of String) For Each type As WMObject In types list.Add(type.aName) Next Return list End Get End Property #End Region ''' ''' Creates a new Windream object and connects to a server with the provided options and credentials ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' Public Sub New(LogFactory As LogFactory, SessionReconnect As Boolean, ClientDriveLetter As String, ClientSupport64Bit As Boolean, SessionServerName As String, SessionUserName As String, SessionPassword As String, SessionDomain As String) _logger = LogFactory.GetCurrentClassLogger() _loggerFactory = LogFactory Dim oSession As WMSession = NewSession(SessionServerName, SessionUserName, SessionPassword, SessionDomain) If oSession Is Nothing Then Throw New Exceptions.SessionException() End If SessionLoggedin = True Me.SessionReconnect = SessionReconnect Me.ClientDriveLetter = ClientDriveLetter Me.ClientSupport64Bit = ClientSupport64Bit Me.SessionServername = SessionServerName _sessionUsername = SessionUserName _sessionPassword = SessionPassword _sessionDomain = SessionDomain End Sub Public Function NewSession(Optional ServerName As String = Nothing, Optional UserName As String = Nothing, Optional Password As String = Nothing, Optional Domain As String = Nothing) As WMSession Dim oBrowser As ServerBrowser Dim oConnect As WMConnect Dim oSession As WMSession Dim oCredentials As WMUserIdentity Dim oImpersonation As Boolean Dim oServerNameFromClient As Boolean Try oBrowser = New ServerBrowser() oConnect = New WMConnect() _logger.Info("Successfully created windream objects") Catch ex As Exception _logger.Error(ex, "Error while creating windream objects") Return Nothing End Try ' If no server was supplied, try to get the current server set in the client Try If ServerName Is Nothing OrElse ServerName.Length = 0 Then ServerName = oBrowser.GetCurrentServer oServerNameFromClient = True Else oServerNameFromClient = False End If Catch ex As Exception _logger.Error(ex, "Error while getting Servername") Return Nothing End Try _logger.Info("Servername: {0}", ServerName) _logger.Info("Servername aquired from client: {0}", oServerNameFromClient) 'TODO: Test connection to windream server ' If username, password and domain are set, login with impersonation ' Else, login with current credentials If UserName IsNot Nothing And Password IsNot Nothing And Domain IsNot Nothing Then oImpersonation = True oCredentials = New WMUserIdentity() With { .aServerName = ServerName, .aUserName = UserName, .aPassword = Password, .aDomain = Domain } oConnect.ModuleId = 9 Else oImpersonation = False oCredentials = New WMUserIdentity() With { .aServerName = ServerName } End If _logger.Info("Impersonated Login: {0}", oImpersonation) _logger.Info("Username: {0}", IIf(UserName IsNot Nothing, UserName, Environment.UserName)) _logger.Info("Domain: {0}", IIf(Domain IsNot Nothing, Domain, Environment.UserDomainName)) Try oSession = oConnect.Login(oCredentials) Catch ex As Exception _logger.Error(ex, "Error while logging in") Return Nothing End Try Try ' Standardmässig hinterlegen dass abgelegte Dateien keine Indexmaske öffnet oSession.SwitchEvents(WMCOMEventWMSessionNeedIndex, False) Catch ex As Exception _logger.Error(ex, "Could not SwitchEvents") Return Nothing End Try If oSession.aLoggedin = False Then _logger.Warn("Session created but user {0} could not be logged in", Environment.UserName) Return Nothing End If Return oSession End Function #Region "Public Methods" Public Function NewFolder(Path As String) As Boolean If Not TestSessionLoggedIn() Then Return False End If Try Path = GetNormalizedPath(Path) Dim oFolders As List(Of String) = Path.Split().ToList() Dim oTempSession As IWMSession2 = DirectCast(Session, IWMSession2) Dim oFolderObject As WMObject For Each oFolder In oFolders If TestFolderExists(Path) = False Then oFolderObject = oTempSession.GetNewWMObjectFS(WMEntityFolder, oFolder, WMObjectEditModeNoEdit) End If Next Return True Catch ex As Exception _logger.Error(ex) Return False End Try End Function Public Function NewFileVersion(Path As String, Comment As String) As Boolean If Not TestSessionLoggedIn() Then Return False End If Try Path = GetNormalizedPath(Path) Dim oFileObject As IWMObject6 oFileObject = Session.GetWMObjectByPath(WMEntityDocument, Path) oFileObject.CreateVersion2(False, Constants.HISTORY_NEW_FROM_VERSION, Comment) Return True Catch ex As Exception _logger.Error(ex) Return False End Try End Function Public Function LockFile(FileObject As WMObject) As Boolean Try FileObject.lock() Return True Catch ex As Exception _logger.Error(ex) Return False End Try End Function Public Function UnlockFile(FileObject As WMObject) As Boolean Try FileObject.unlock() Return True Catch ex As Exception _logger.Error(ex) Return False End Try End Function Public Function GetChoiceLists() As List(Of String) Dim oItems As New List(Of String) If TestSessionLoggedIn() = False Then Return oItems End If Try Dim oChoiceLists As WMObjects Dim oChoiceList As IWMObject2 'load list of choicelists oChoiceLists = Session.GetAllObjects(WMEntityChoiceList) For Each oChoiceList In oChoiceLists oItems.Add(oChoiceList.aName) Next Return oItems Catch ex As Exception _logger.Error(ex) Return oItems End Try End Function Public Function GetChoiceListItems(ChoiceListName As String) As List(Of String) Dim oItems As New List(Of String) Dim oChoicelist As WMObject If TestSessionLoggedIn() = False Then Return oItems End If Try Dim oTempSession As IWMSession2 = DirectCast(Session, IWMSession2) oChoicelist = oTempSession.GetWMObjectByName(WMEntityChoiceList, ChoiceListName) Catch ex As Exception _logger.Error(ex, "Could not get choice list") Return oItems End Try Try Dim oChoiceListItems As Object = oChoicelist.GetVariableValue("vItems") If oChoiceListItems Is Nothing Then Return oItems End If For Each oChoiceListItem In oChoiceListItems oItems.Add(oChoiceListItem) Next Return oItems Catch ex As Exception _logger.Error(ex, "Could not get choice list items") Return oItems End Try End Function Public Function GetIndiciesByObjecttype(ObjectTypeName As String) As List(Of String) If TestSessionLoggedIn() = False Then Return Nothing End If Dim oObjectType As WMObject Dim oIndexAttributes As WMObjectRelation Dim oIndexAttribute As WMObject Dim oIndex As WMObject Dim oRelProperties As WMObjectRelation Dim oTempSession As IWMSession2 Dim oIndicies As New List(Of String) Try ' den Objekttyp laden oTempSession = DirectCast(Session, IWMSession2) oObjectType = Session.GetWMObjectByName(WMEntityObjectType, ObjectTypeName) ' Beziehung zu Indizes des Objekttyp auslesen oIndexAttributes = oObjectType.GetWMObjectRelationByName("TypeAttributes") ' alle Indizes durchlaufen For j As Integer = 0 To oIndexAttributes.Count - 1 ' aktuellen Index auslesen oIndexAttribute = oIndexAttributes.Item(j) ' Eigenschaften des Index auslesen oRelProperties = oIndexAttribute.GetWMObjectRelationByName("Attribute") ' Index aus den Eigenschaften auslesen oIndex = oRelProperties.Item(0) ' Indexname speichern 'aIndexNames(j) = oIndex.aName oIndicies.Add(oIndex.aName) Next ' Indexarray zurückgeben 'Return aIndexNames Return oIndicies Catch ex As Exception _logger.Error(ex) Return oIndicies End Try End Function Public Function GetIndexType(IndexName As String) As Integer If TestSessionLoggedIn() = False Then Return Nothing End If Try Dim oTempSession As IWMSession2 = DirectCast(Session, IWMSession2) Dim oAttribute = oTempSession.GetWMObjectByName(WMEntityAttribute, IndexName) Dim oType = oAttribute.GetVariableValue("dwAttrType") Return oType Catch ex As Exception _logger.Error(ex) Return Nothing End Try End Function Public Function RemoveFile(Path As String) As Boolean If TestSessionLoggedIn() = False Then Return Nothing End If Try Dim oTempSession As IWMSession2 = DirectCast(Session, IWMSession2) Dim oWMObject As WMObject = oTempSession.GetWMObjectByName(WMEntityAttribute, Path) If oWMObject Is Nothing Then Return False End If oWMObject.Delete() Return True Catch ex As Exception _logger.Error(ex) Return False End Try End Function 'Public Function RemoveVectorIndexValue(Path As String, IndexName As String, ValueToDelete As String) As Boolean ' If TestSessionLoggedIn() = False Then ' Return False ' End If ' Try ' Dim oTempSession As IWMSession2 = DirectCast(Session, IWMSession2) ' Dim oWMObject As WMObject = oTempSession.GetWMObjectByName(WMEntityAttribute, Path) ' If oWMObject Is Nothing Then ' _logger.Warn("Could not find document {0}", Path) ' Return False ' End If ' Dim oVectorValues = oWMObject.GetVariableValue(IndexName) ' Dim oType As Integer = GetIndexType(IndexName) ' If oVectorValues Is Nothing Then ' _logger.Warn("Could not values of index {0}", IndexName) ' Return False ' End If ' Dim oValueFound As Boolean = False ' For Each oVectorValue In oVectorValues ' If oVectorValue = ValueToDelete Then ' oValueFound = True ' End If ' Next ' Dim oNewValues As New List(Of Object) ' If oValueFound Then ' oNewValues = oVectorValues ' oNewValues = oNewValues.Except(New List(Of Object) From {ValueToDelete}).ToList() ' End If ' Catch ex As Exception ' _logger.Error(ex) ' Return Nothing ' End Try 'End Function Public Function TestFolderExists(Path As String) As Boolean Return TestObjectExists(GetNormalizedPath(Path), WMEntityFolder) End Function Public Function TestFileExists(Path As String) As Boolean Return TestObjectExists(GetNormalizedPath(Path), WMEntityDocument) End Function Public Function TestUserExists(Username As String) As Boolean Return TestObjectExists(Username, WMEntityUser) End Function Public Function TestGroupExists(Groupname As String) As Boolean Return TestObjectExists(Groupname, WMEntityGroups) End Function #End Region #Region "Private Methods" Private Function GetNormalizedPath(Path As String) As String Dim oNormalizedPath = Path If Not Path.StartsWith("\") And Path.ToUpper().StartsWith(ClientDriveLetter.ToUpper) Then oNormalizedPath = Path.Substring(2) End If Return oNormalizedPath End Function Private Function GetObjectTypes() As WMObjects Dim oObjectTypes As WMObjects Try oObjectTypes = Session.GetWMObjectTypes(WMEntityDocument) Return oObjectTypes Catch ex As Exception _logger.Error(ex) Return Nothing End Try End Function Private Function TestSessionLoggedIn() As Boolean Try If Session.aLoggedin Then Return True Else _logger.Warn("There is no active WM-Session!") Return False End If Catch ex As Exception _logger.Error(ex) Return False End Try End Function Public Function TestObjectExists(ObjectName As String, ObjectType As WMEntity) As Boolean If TestSessionLoggedIn() = False Then Return False End If Try Dim oTempSession = DirectCast(Session, IWMSession2) Dim oObjectId = 0 Dim oObjectDbId = 0 Return oTempSession.WMObjectExists(ObjectType, ObjectName, oObjectId, oObjectDbId) Catch ex As Exception _logger.Error(ex, "Error while checking existence of WMObject {0} of type {1}", ObjectName, ObjectType.ToString) Return False End Try End Function #End Region End Class