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 Imports System.IO ''' ''' MODULE: Windream ''' ''' VERSION: 0.0.0.1 ''' ''' DATE: 27.08.2018 ''' ''' DESCRIPTION: ''' ''' DEPENDENCIES: NLog, >= 4.5.8 ''' ''' PARAMETERS: LogFactory, NLog.LogFactory ''' The LogFactory containing the current log config. Used to instanciate the class logger for this and any dependent class ''' ''' ClientDriveLetter, String ''' Drive Letter of the Windream Drive, should default to `W` ''' ''' ClientSupport64Bit, Boolean ''' Should this session support 64bit methods/functionality? ''' ''' SessionReconnect, Boolean ''' Should the session reconnect automatically when the connection to the server is lost? ''' ''' SessionServerName, String ''' Name of the server used in the connection. If this is `Nothing`, the current server defined in the client is used ''' ''' SessionUserName, String ''' Name of the user that is used in the connection. If this is `Nothing`, the currently signed in user is used ''' ''' SessionPassword, String ''' User-password that is used in the connection. If this is `Nothing`, the currently signed in user is used ''' ''' SessionDomain, String ''' User-domain that is used in the connection. If this is `Nothing`, the currently signed in user is used ''' ''' PROPERTIES: ClientDriveLetter, String (readonly) ''' ClientSupports64Bit, Boolean (readonly) ''' Session, IWMSession2 (readonly) ''' SessionLoggedin, Boolean (readonly) ''' SessionReconnect, Boolean (readonly) ''' SessionServername, String (readonly) ''' Objecttypes, List(Of String) (readonly) ''' ''' 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 ClientSupports64Bit As Boolean Public ReadOnly Property Session As IWMSession2 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) ' Create logger and save LogFactory for dependent classes _logger = LogFactory.GetCurrentClassLogger() _loggerFactory = LogFactory ' Create a session Dim oSession As IWMSession2 = NewSession(SessionServerName, SessionUserName, SessionPassword, SessionDomain) ' If no session could be created, exit If oSession Is Nothing Then Throw New Exceptions.SessionException() End If ' Set properties of currently established session Session = oSession SessionLoggedin = True Me.SessionReconnect = SessionReconnect Me.ClientDriveLetter = ClientDriveLetter Me.ClientSupports64Bit = 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 IWMSession2 Dim oBrowser As ServerBrowser Dim oConnect As IWMConnect2 Dim oSession As IWMSession2 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 LockObject(WMObject As WMObject) As Boolean Try WMObject.lock() Return True Catch ex As Exception _logger.Error(ex) Return False End Try End Function Public Function UnlockObject(WMObject As WMObject) As Boolean Try WMObject.unlock() Return True Catch ex As Exception _logger.Error(ex) Return False End Try End Function Public Function GetSearchDocuments(SearchFilePath As String, DocIdIndexName As String) As Dictionary(Of Integer, String) Dim oResult As New Dictionary(Of Integer, String) If TestSessionLoggedIn() = False Then Return oResult End If If TestFileExists(SearchFilePath) = False Then Return oResult End If Try Dim oFileInfo = New FileInfo(SearchFilePath) Dim oProfileName = oFileInfo.Name Dim oProfilePath = oFileInfo.DirectoryName Dim oSearchController As New WMOSearchController() oSearchController.CheckSearchProfile(SearchFilePath) Dim oSearchType = oSearchController.SearchProfileTargetProgID Dim oSearchProfileExSetttings As Integer = oSearchController.SearchProfileExSettings If oSearchProfileExSetttings = 0 Then oSearchProfileExSetttings = 7 End If Dim oSearch As WMSearch Select Case oSearchType.ToUpper() Case Constants.SEARCH_TYPE_QUICK_SEARCH Dim oQuickSearch As New WMQuickSearch() oQuickSearch.WMSession = Session oQuickSearch.ClearSearch() oQuickSearch.SearchProfilePath = SearchFilePath oQuickSearch.LoadSearchProfile(oProfileName) oSearch = oQuickSearch.GetSearch() Case Constants.SEARCH_TYPE_INDEX_SEARCH Dim oIndexSearch As New WMIndexSearch() oIndexSearch.WMSession = Session oIndexSearch.ClearSearch() oIndexSearch.SearchProfilePath = SearchFilePath oIndexSearch.LoadSearchProfile(oProfileName) oSearch = oIndexSearch.GetSearch() Case Constants.SEARCH_TYPE_OBJECTTYPE_SEARCH Dim oObjecttypeSearch As New WMObjectTypeSearch() oObjecttypeSearch.WMSession = Session oObjecttypeSearch.ClearSearch() oObjecttypeSearch.SearchProfilePath = SearchFilePath oObjecttypeSearch.LoadSearchProfile(oProfileName) oSearch = oObjecttypeSearch.GetSearch() Case Else _logger.Warn("{0} is not a valid search type", oSearchType) Return oResult End Select Dim oSearchResults As WMObjects = oSearch.Execute() If oSearchResults.Count = 0 Then Return oResult End If For Each oSearchResult As WMObject In oSearchResults Dim path As String = oSearchResult.aPath Dim docId As Integer = oSearchResult.GetVariableValue(DocIdIndexName) oResult.Add(docId, path) Next Return oResult Catch ex As Exception _logger.Error(ex) Return oResult 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 GetIndexValue(Path As String, IndexName As String) As List(Of String) Dim oResult As New List(Of String) If TestSessionLoggedIn() = False Then Return oResult End If Try Path = GetNormalizedPath(Path) Dim oWMObject As WMObject = Session.GetWMObjectByPath(WMEntityDocument, Path) If oWMObject Is Nothing Then Return oResult End If Dim oValues = oWMObject.GetVariableValue(IndexName) If oValues Is Nothing Then Return oResult End If If TypeOf oValues Is Object Then For Each oValue In oValues oResult.Add(oValue) Next Else oResult.Add(oValues) End If Return oResult Catch ex As Exception _logger.Error(ex) Return oResult End Try End Function Public Function SetFolderObjecttype(FolderPath As String, Objecttype As String) As Boolean If TestSessionLoggedIn() = False Then Return False End If Try FolderPath = GetNormalizedPath(FolderPath) If TestFolderExists(FolderPath) = False Then _logger.Warn("Folder {0} does not exist!", FolderPath) Return False End If Dim oWMFolder As WMObject = GetObjectByPath(FolderPath, WMEntityFolder) If LockObject(oWMFolder) = False Then _logger.Warn("Folder {0} could not be locked", FolderPath) End If If oWMFolder.aObjectType.aName <> Constants.OBJECT_TYPE_DEFAULT Then _logger.Warn("Objecttype for folder {0} has already been set!", FolderPath) End If Dim oObjecttype As WMObject = GetObjectByName(Objecttype, WMEntityObjectType) If oObjecttype Is Nothing Then _logger.Warn("Objecttype {0} does not exist!", Objecttype) Return False End If oWMFolder.aObjectType = oObjecttype oWMFolder.Save() If UnlockObject(oWMFolder) Then _logger.Warn("Folder {0} could not be unlocked", FolderPath) End If Return True Catch ex As Exception _logger.Error(ex) Return False 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 GetObjectByName(ObjectName As String, ObjectType As WMEntity) As WMObject If TestSessionLoggedIn() = False Then Return Nothing End If If TestObjectExists(ObjectName, ObjectType) = False Then Return Nothing End If Try Dim oWMObject As WMObject = Session.GetWMObjectByName(ObjectType, ObjectName) Return oWMObject Catch ex As Exception _logger.Error(ex) Return Nothing End Try End Function Private Function GetObjectByPath(ObjectName As String, ObjectType As WMEntity) As WMObject If TestSessionLoggedIn() = False Then Return Nothing End If If TestObjectExists(ObjectName, ObjectType) = False Then Return Nothing End If Try Dim oWMObject As WMObject = Session.GetWMObjectByPath(ObjectType, ObjectName) Return oWMObject 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 Private Function TestObjectExists(ObjectName As String, ObjectType As WMEntity) As Boolean If TestSessionLoggedIn() = False Then Return False End If Try Dim oObjectId = 0 Dim oObjectDbId = 0 Return Session.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