Esempio n. 1
0
 def connect(self, url, infos):
     try:
         transformer = getUrlTransformer(self.ctx)
         location = self._getUrl(transformer, infos)
         if location is None:
             code = getMessage(self.ctx, g_message, 111)
             msg = getMessage(self.ctx, g_message, 112,
                              (url, self._getInfo(infos)))
             raise self._getException(code, 1001, msg, self)
         msg = getMessage(self.ctx, g_message, 113, location.Main)
         logMessage(self.ctx, INFO, msg, 'Driver', 'connect()')
         name = self._getDataSourceName(location)
         sf = getSimpleFile(self.ctx)
         split = self._getSplitUrl(transformer, location, name)
         if not sf.isFolder(split):
             if sf.exists(split):
                 code = getMessage(self.ctx, g_message, 114)
                 msg = getMessage(self.ctx, g_message, 115, split)
                 raise self._getException(code, 1002, msg, self)
             self._splitDataBase(transformer, sf, location, split, name)
         datasource = self._getDataSource(transformer, location, name)
         connection = Connection(self.ctx, datasource, url, self._user,
                                 self._password, None, True)
         version = connection.getMetaData().getDriverVersion()
         msg = getMessage(self.ctx, g_message, 116, (version, self._user))
         logMessage(self.ctx, INFO, msg, 'Driver', 'connect()')
         return connection
     except SQLException as e:
         raise e
     except Exception as e:
         msg = getMessage(self.ctx, g_message, 117,
                          (e, traceback.print_exc()))
         logMessage(self.ctx, SEVERE, msg, 'Driver', 'connect()')
Esempio n. 2
0
 def _upload(self, dialog):
     service = 'com.sun.star.util.PathSubstitution'
     ps = createService(self.ctx, service)
     path = ps.substituteVariables('$(work)', True)
     service = 'com.sun.star.ui.dialogs.FilePicker'
     fp = createService(self.ctx, service)
     fp.setDisplayDirectory(path)
     fp.appendFilter(g_jar, '*.jar')
     fp.setCurrentFilter(g_jar)
     if fp.execute() == OK:
         url = getUrl(self.ctx, fp.getFiles()[0])
         if url.Name == g_jar:
             jar = '%s/%s' % (g_path, g_jar)
             target = getResourceLocation(self.ctx, g_identifier, jar)
             getSimpleFile(self.ctx).copy(url.Main, target)
             self._reloadVersion(dialog)
Esempio n. 3
0
 def _getUrl(self):
     path = getResourceLocation(self.ctx, g_identifier, g_path)
     url = '%s/dbversion.odb' % path
     if not getSimpleFile(self.ctx).exists(url):
         service = 'com.sun.star.sdb.DatabaseContext'
         datasource = createService(self.ctx, service).createInstance()
         datasource.URL = self._getDataSourceUrl(path)
         datasource.Settings.JavaDriverClass = g_class
         datasource.Settings.JavaDriverClassPath = self._getDataSourceClassPath(
             path)
         datasource.DatabaseDocument.storeAsURL(url, ())
     return url
Esempio n. 4
0
def getDataSourceUrl(ctx, dbname, plugin, register):
    error = None
    url = getResourceLocation(ctx, plugin, g_path)
    odb = '%s/%s.odb' % (url, dbname)
    if not getSimpleFile(ctx).exists(odb):
        dbcontext = ctx.ServiceManager.createInstance('com.sun.star.sdb.DatabaseContext')
        datasource = createDataSource(dbcontext, url, dbname)
        error = _createDataBase(ctx, datasource, url, dbname)
        if error is None:
            datasource.DatabaseDocument.storeAsURL(odb, ())
            if register:
                registerDataSource(dbcontext, dbname, odb)
    return url, error
Esempio n. 5
0
def getDataSource(ctx, dbname, plugin, register):
    location = getResourceLocation(ctx, plugin, g_path)
    url = '%s/%s.odb' % (location, dbname)
    dbcontext = createService(ctx, 'com.sun.star.sdb.DatabaseContext')
    if getSimpleFile(ctx).exists(url):
        odb = dbname if dbcontext.hasByName(dbname) else url
        datasource = dbcontext.getByName(odb)
        created = False
    else:
        datasource = createDataSource(dbcontext, location, dbname)
        created = True
    if register:
        registerDataSource(dbcontext, dbname, url)
    return datasource, url, created
Esempio n. 6
0
def getDataSourceUrl(ctx, dbname, plugin, register):
    error = None
    print("dbinit.getDataSourceUrl() 1")
    url = getResourceLocation(ctx, plugin, g_path)
    odb = '%s/%s.odb' % (url, dbname)
    print("dbinit.getDataSourceUrl() 2")
    dbcontext = createService(ctx, 'com.sun.star.sdb.DatabaseContext')
    print("dbinit.getDataSourceUrl() 3")
    if not getSimpleFile(ctx).exists(odb):
        print("dbinit.getDataSourceUrl() 4")
        datasource = createDataSource(dbcontext, url, dbname)
        error = createDataBase(ctx, datasource, url, dbname)
        print("dbinit.getDataSourceUrl() 5 %s" % error)
        if error is None:
            datasource.DatabaseDocument.storeAsURL(odb, ())
    if error is None and register:
        registerDataSource(dbcontext, dbname, odb)
    return url, error
Esempio n. 7
0
 def execute(self, command, id, environment):
     try:
         url = self.getIdentifier().getContentIdentifier()
         print("Content.execute() %s - %s - %s" %
               (command.Name, url, self.getIdentifier().Id))
         msg = "command.Name: %s" % command.Name
         logMessage(self.ctx, INFO, msg, "Content", "execute()")
         if command.Name == 'getCommandInfo':
             return CommandInfo(self._commandInfo)
         elif command.Name == 'getPropertySetInfo':
             return PropertySetInfo(self._propertySetInfo)
         elif command.Name == 'getPropertyValues':
             namedvalues = getPropertiesValues(self.ctx, self,
                                               command.Argument)
             return Row(namedvalues)
         elif command.Name == 'setPropertyValues':
             return setPropertiesValues(self.ctx, self, environment,
                                        command.Argument)
         elif command.Name == 'delete':
             self.MetaData.insertValue('Trashed', True)
             user = self.Identifier.User
             user.DataBase.updateContent(user.Id, self.Identifier.Id,
                                         'Trashed', True)
         elif command.Name == 'open':
             if self.IsFolder:
                 # Not Used: command.Argument.Properties - Implement me ;-)
                 select = self.Identifier.getFolderContent(self.MetaData)
                 msg += " IsFolder: %s" % self.IsFolder
                 logMessage(self.ctx, INFO, msg, "Content", "execute()")
                 return DynamicResultSet(self.ctx, select)
             elif self.IsDocument:
                 sf = getSimpleFile(self.ctx)
                 url, size = self.Identifier.getDocumentContent(
                     sf, self.MetaData, 0)
                 if not size:
                     title = self.MetaData.getValue('Title')
                     msg = "Error while downloading file: %s" % title
                     raise CommandAbortedException(msg, self)
                 sink = command.Argument.Sink
                 interfaces = getInterfaceTypes(sink)
                 datasink = uno.getTypeByName(
                     'com.sun.star.io.XActiveDataSink')
                 datastream = uno.getTypeByName(
                     'com.sun.star.io.XActiveDataStreamer')
                 isreadonly = self.MetaData.getValue('IsReadOnly')
                 if datasink in interfaces:
                     sink.setInputStream(sf.openFileRead(url))
                 elif not isreadonly and datastream in interfaces:
                     sink.setStream(sf.openFileReadWrite(url))
         elif command.Name == 'insert':
             # The Insert command is only used to create a new folder or a new document
             # (ie: File Save As).
             # It saves the content created by 'createNewContent' from the parent folder
             print("Content.execute() insert 1 - %s - %s" %
                   (self.IsFolder, self.Identifier.Id))
             if self.IsFolder:
                 mediatype = self.Identifier.User.Provider.Folder
                 self.MetaData.insertValue('MediaType', mediatype)
                 print("Content.execute() insert 2 ************** %s" %
                       mediatype)
                 if self.Identifier.insertNewContent(self.MetaData):
                     # Need to consum the new Identifier if needed...
                     self.Identifier.deleteNewIdentifier()
                 print("Content.execute() insert 3")
             elif self.IsDocument:
                 stream = command.Argument.Data
                 replace = command.Argument.ReplaceExisting
                 sf = getSimpleFile(self.ctx)
                 url = self.Identifier.User.Provider.SourceURL
                 target = '%s/%s' % (url, self.Identifier.Id)
                 if sf.exists(target) and not replace:
                     return
                 inputstream = uno.getTypeByName(
                     'com.sun.star.io.XInputStream')
                 if inputstream in getInterfaceTypes(stream):
                     sf.writeFile(target, stream)
                     mediatype = getMimeType(self.ctx, stream)
                     self.MetaData.insertValue('MediaType', mediatype)
                     stream.closeInput()
                     print("Content.execute() insert 2 ************** %s" %
                           mediatype)
                     if self.Identifier.insertNewContent(self.MetaData):
                         # Need to consum the new Identifier if needed...
                         self.Identifier.deleteNewIdentifier()
                         print("Content.execute() insert 3")
         elif command.Name == 'createNewContent' and self.IsFolder:
             return self.createNewContent(command.Argument)
         elif command.Name == 'transfer' and self.IsFolder:
             # Transfer command is used for document 'File Save' or 'File Save As'
             # NewTitle come from:
             # - Last segment path of 'XContent.getIdentifier().getContentIdentifier()' for OpenOffice
             # - Property 'Title' of 'XContent' for LibreOffice
             # If the content has been renamed, the last segment is the new Title of the content
             title = command.Argument.NewTitle
             source = command.Argument.SourceURL
             move = command.Argument.MoveData
             clash = command.Argument.NameClash
             print("Content.execute() transfert 1 %s - %s -%s - %s" %
                   (title, source, move, clash))
             # We check if 'NewTitle' is a child of this folder by recovering its id
             user = self.Identifier.User
             id = self.Identifier.User.DataBase.getChildId(
                 user.Id, self.Identifier.Id, title)
             if id is None:
                 print("Content.execute() transfert 2 %s" % id)
                 # Id could not be found: 'NewTitle' does not exist in the folder...
                 # For new document (File Save As) we use commands:
                 # - createNewContent: for creating an empty new Content
                 # - Insert at new Content for committing change
                 # To execute these commands, we must throw an exception
                 msg = "Couln't handle Url: %s" % source
                 raise InteractiveBadTransferURLException(msg, self)
             print("Content.execute() transfert 3 %s" % id)
             sf = getSimpleFile(self.ctx)
             if not sf.exists(source):
                 raise CommandAbortedException(
                     "Error while saving file: %s" % source, self)
             inputstream = sf.openFileRead(source)
             target = '%s/%s' % (self.Identifier.User.Provider.SourceURL,
                                 id)
             sf.writeFile(target, inputstream)
             inputstream.closeInput()
             # We need to update the Size
             user.DataBase.updateContent(user.Id, self.Identifier.Id,
                                         'Size', sf.getSize(target))
             if move:
                 pass  #must delete object
         elif command.Name == 'flush' and self.IsFolder:
             pass
     except CommandAbortedException as e:
         raise e
     except InteractiveBadTransferURLException as e:
         raise e
     except Exception as e:
         msg += " ERROR: %s" % e
         logMessage(self.ctx, SEVERE, msg, "Content", "execute()")
Esempio n. 8
0
 def execute(self, command, id, environment):
     try:
         msg = "command.Name: %s" % command.Name
         print("Content.execute() %s **********************************" %
               msg)
         logMessage(self.ctx, INFO, msg, 'Content', 'execute()')
         if command.Name == 'getCommandInfo':
             msg = "command.Name: %s ******************************************" % command.Name
             logMessage(self.ctx, INFO, msg, 'Content', 'execute()')
             return CommandInfo(self._commandInfo)
         elif command.Name == 'getPropertySetInfo':
             return PropertySetInfo(self._propertySetInfo)
         elif command.Name == 'getPropertyValues':
             namedvalues = getPropertiesValues(self.ctx, self,
                                               command.Argument)
             return Row(namedvalues)
         elif command.Name == 'setPropertyValues':
             return setPropertiesValues(self.ctx, self, environment,
                                        command.Argument)
         elif command.Name == 'delete':
             self.MetaData.insertValue(
                 'Trashed', self.Identifier.updateTrashed(True, False))
         elif command.Name == 'open':
             print("Content.execute() open 1")
             if self.IsFolder:
                 print("Content.execute() open 2")
                 # Not Used: command.Argument.Properties - Implement me ;-)
                 select = self.Identifier.getFolderContent(self.MetaData)
                 msg += " IsFolder: %s" % self.IsFolder
                 logMessage(self.ctx, INFO, msg, 'Content', 'execute()')
                 return DynamicResultSet(self.ctx, select)
             elif self.IsDocument:
                 print("Content.execute() open 3")
                 sf = getSimpleFile(self.ctx)
                 url, size = self.Identifier.getDocumentContent(
                     sf, self.MetaData, 0)
                 if not size:
                     print("Content.execute() open 4")
                     title = self.MetaData.getValue('Title')
                     msg = "Error while downloading file: %s" % title
                     raise CommandAbortedException(msg, self)
                 print("Content.execute() open 5")
                 s = command.Argument.Sink
                 sink = uno.getTypeByName('com.sun.star.io.XActiveDataSink')
                 stream = uno.getTypeByName(
                     'com.sun.star.io.XActiveDataStreamer')
                 isreadonly = self.MetaData.getValue('IsReadOnly')
                 if s.queryInterface(sink):
                     s.setInputStream(sf.openFileRead(url))
                 elif not isreadonly and s.queryInterface(stream):
                     s.setStream(sf.openFileReadWrite(url))
                 print("Content.execute() open 6")
         elif command.Name == 'insert':
             if self.IsFolder:
                 mediatype = self.Identifier.DataSource.Provider.Folder
                 self.MetaData.insertValue('MediaType', mediatype)
                 if self.Identifier.insertNewFolder(self.MetaData):
                     pass
                 #identifier = self.getIdentifier()
                 #ucp = getUcp(self.ctx, identifier.getContentProviderScheme())
                 #self.addPropertiesChangeListener(('Id', 'Name', 'Size', 'Trashed', 'Loaded'), ucp)
                 #propertyChange(self, 'Id', identifier.Id, CREATED | FOLDER)
                 #parent = identifier.getParent()
                 #event = getContentEvent(self, INSERTED, self, parent)
                 #ucp.queryContent(parent).notify(event)
             elif self.IsDocument:
                 # The Insert command is only used to create a new document (File Save As)
                 # it saves content from createNewContent from the parent folder
                 stream = command.Argument.Data
                 replace = command.Argument.ReplaceExisting
                 sf = getSimpleFile(self.ctx)
                 url = self.Identifier.DataSource.Provider.SourceURL
                 target = '%s/%s' % (url, self.Identifier.Id)
                 if sf.exists(target) and not replace:
                     pass
                 elif stream.queryInterface(
                         uno.getTypeByName('com.sun.star.io.XInputStream')):
                     sf.writeFile(target, stream)
                     mediatype = getMimeType(self.ctx, stream)
                     self.MetaData.insertValue('MediaType', mediatype)
                     stream.closeInput()
                     if self.Identifier.insertNewDocument(self.MetaData):
                         pass
                     #ucp = getUcp(self.ctx, identifier.getContentProviderScheme())
                     #self.addPropertiesChangeListener(('Id', 'Name', 'Size', 'Trashed', 'Loaded'), ucp)
                     #propertyChange(self, 'Id', identifier.Id, CREATED | FILE)
                     #parent = identifier.getParent()
                     #event = getContentEvent(self, INSERTED, self, parent)
                     #ucp.queryContent(parent).notify(event)
         elif command.Name == 'createNewContent' and self.IsFolder:
             return self.createNewContent(command.Argument)
         elif command.Name == 'transfer' and self.IsFolder:
             # Transfer command is used for document 'File Save' or 'File Save As'
             # NewTitle come from:
             # - Last segment path of 'XContent.getIdentifier().getContentIdentifier()' for OpenOffice
             # - Property 'Title' of 'XContent' for LibreOffice
             # If the content has been renamed, the last segment is the new Title of the content
             title = command.Argument.NewTitle
             source = command.Argument.SourceURL
             move = command.Argument.MoveData
             clash = command.Argument.NameClash
             # We check if 'command.Argument.NewTitle' is an Id
             if self.Identifier.isChildId(title):
                 id = title
             else:
                 # It appears that 'command.Argument.NewTitle' is not an Id but a Title...
                 # If 'NewTitle' exist and is unique in the folder, we can retrieve its Id
                 id = self.Identifier.selectChildId(title)
                 if not id:
                     # Id could not be found: NewTitle does not exist in the folder...
                     # For new document (File Save As) we use commands:
                     # - createNewContent: for creating an empty new Content
                     # - Insert at new Content for committing change
                     # To execute these commands, we must throw an exception
                     msg = "Couln't handle Url: %s" % source
                     raise InteractiveBadTransferURLException(msg, self)
             sf = getSimpleFile(self.ctx)
             if not sf.exists(source):
                 raise CommandAbortedException(
                     "Error while saving file: %s" % source, self)
             inputstream = sf.openFileRead(source)
             target = '%s/%s' % (
                 self.Identifier.DataSource.Provider.SourceURL, id)
             sf.writeFile(target, inputstream)
             inputstream.closeInput()
             # We need to commit change: Size is the property chainning all DataSource change
             if not self.Identifier.updateSize(id, self.Identifier.Id,
                                               sf.getSize(target)):
                 raise CommandAbortedException(
                     "Error while saving file: %s" % source, self)
             #ucb = getUcb(self.ctx)
             #identifier = ucb.createContentIdentifier('%s/%s' % (self.Identifier.BaseURL, title))
             #data = getPropertyValueSet({'Size': sf.getSize(target)})
             #content = ucb.queryContent(identifier)
             #executeContentCommand(content, 'setPropertyValues', data, environment)
             if move:
                 pass  #must delete object
         elif command.Name == 'flush' and self.IsFolder:
             pass
     except CommandAbortedException as e:
         raise e
     except InteractiveBadTransferURLException as e:
         raise e
     except Exception as e:
         msg += " ERROR: %s" % e
         print("Content.execute() ERROR: %s" % msg)
         logMessage(self.ctx, SEVERE, msg, 'Content', 'execute()')