def on_persist_event(self, document):
        client = kend.client.Client()

        document_id, doi = self._resolve(document)
        if document_id is not None:
            for annotation in document.annotations('PersistQueue'):
                if 'session:volatile' not in annotation:
                    try:
                        ka = kend.converter.Annotation.spineapi2kend(annotation, document_id)
                        ka.context = self._context_

                        updated = client.persistAnnotation(ka, context = self._context_)

                        if isinstance(updated, kend.model.Annotation):
                            for key in ('id', 'created', 'author', 'revision', 'edit', 'media_edit'):
                                annotation[key] = getattr(updated, key)
                            annotation.removePropertyAll('session:media')
                            for media in updated.media:
                                mediaDict = {}
                                for k in ['name', 'src', 'type', 'revision', 'size', 'edit']:
                                    if hasattr(media, k):
                                        mediaDict[k] = getattr(media, k)
                                annotation.insertProperty('session:media', urllib.urlencode(mediaDict))
                            document.removeAnnotation(annotation, 'PersistQueue')
                            document.addAnnotation(annotation)
                    except:
                        raise
                        pass

            for annotation in document.annotations(document.deletedItemsScratchId()):
                try:
                    if 'session:volatile' not in annotation:
                        ka = kend.converter.Annotation.spineapi2kend(annotation, document_id)
                        client.deleteAnnotation(ka)
                    document.removeAnnotation(annotation, document.deletedItemsScratchId())
                except:
                    raise
                    pass
    def on_persist_event(self, document):
        client = kend.client.Client()

        document_id = utopia.tools.utils.metadata(document,
                                                  'identifiers[utopia]')
        if document_id is not None:
            for annotation in document.annotations('PersistQueue'):
                if 'session:volatile' not in annotation:
                    try:
                        ka = kend.converter.Annotation.spineapi2kend(
                            annotation, document_id)
                        ka.context = self._context_

                        updated = client.persistAnnotation(
                            ka, context=self._context_)

                        if isinstance(updated, kend.model.Annotation):
                            for key in ('id', 'created', 'author', 'revision',
                                        'edit', 'media_edit'):
                                annotation[key] = getattr(updated, key)
                            annotation.removePropertyAll('session:media')
                            for media in updated.media:
                                mediaDict = {}
                                for k in [
                                        'name', 'src', 'type', 'revision',
                                        'size', 'edit'
                                ]:
                                    if hasattr(media, k):
                                        mediaDict[k] = getattr(media, k)
                                annotation.insertProperty(
                                    'session:media',
                                    urllib.urlencode(mediaDict))
                            document.removeAnnotation(annotation,
                                                      'PersistQueue')
                            document.addAnnotation(annotation)
                    except:
                        raise
                        pass

            for annotation in document.annotations(
                    document.deletedItemsScratchId()):
                try:
                    if 'session:volatile' not in annotation:
                        ka = kend.converter.Annotation.spineapi2kend(
                            annotation, document_id)
                        client.deleteAnnotation(ka)
                    document.removeAnnotation(annotation,
                                              document.deletedItemsScratchId())
                    document.removeAnnotation(annotation)
                except:
                    raise
                    pass
 def on_filter_event(self, document, data=None):
     for a in document.annotations():
         if a.get(
                 'author'
         ) == 'http://utopia.cs.manchester.ac.uk/users/11679' and a.get(
                 'concept') in ('Definition', 'DatabaseEntry'
                                ) and 'session:legacy' not in a:
             document.removeAnnotation(a)
             identifier = a.get('property:identifier', '')
             if identifier.startswith('http://bio2rdf.org/pdb:'):
                 # PDB entry
                 a2 = spineapi.Annotation()
                 a2['concept'] = 'DatabaseEntry'
                 a2['author'] = a['author']
                 a2['session:volatile'] = '1'
                 a2['session:legacy'] = '1'
                 a2['property:sourceDatabase'] = 'pdb'
                 a2['property:sourceDescription'] = '<p>The <a href="http://www.rcsb.org/">Protein Data Bank</a> of the Research Collaboratory for Structural Bioinformatics (<a href="http://home.rcsb.org/">RCSB</a>).</p>'
                 a2['property:identifier'] = identifier
                 a2['property:description'] = 'PDB entry {0}'.format(
                     identifier[-4:].upper())
                 if 'property:name' in a:
                     a2['property:name'] = a['property:name'][:-11]
                 if 'property:imageUrl' in a:
                     a2['property:imageUrl'] = a['property:imageUrl']
                 if 'property:molecularDescription' in a:
                     a2['property:molecularDescription'] = a[
                         'property:molecularDescription']
                 if 'property:webpageUrl' in a:
                     a2['property:webpageUrl'] = a['property:webpageUrl']
                 if 'property:embedded' in a:
                     a2['property:embedded'] = a['property:embedded']
                 for extent in a.extents():
                     a2.addExtent(extent)
                 for area in a.areas():
                     a2.addArea(area)
                 document.addAnnotation(a2)
             if identifier.startswith('http://dbpedia.org/resource/'):
                 # Wikipedia entry
                 a2 = spineapi.Annotation()
                 a2['concept'] = 'Definition'
                 a2['author'] = a['author']
                 a2['session:volatile'] = '1'
                 a2['session:legacy'] = '1'
                 a2['property:sourceDatabase'] = 'wikipedia'
                 a2['property:sourceDescription'] = '<p>Structured <a href="http://www.wikipedia.org/">Wikipedia</a> information provided by the <a href="http://DBpedia.org/">DBpedia</a> project.</p>'
                 a2['property:description'] = a.get('property:summary',
                                                    'Wikipedia entry')
                 if 'property:name' in a:
                     a2['property:name'] = a['property:name']
                 if 'property:identifier' in a:
                     a2['property:identifier'] = a['property:identifier']
                 if 'property:imageUrl' in a:
                     a2['property:imageUrl'] = a['property:imageUrl']
                 if 'property:summary' in a:
                     a2['property:summary'] = a['property:summary']
                 if 'property:webpageUrl' in a:
                     a2['property:webpageUrl'] = a['property:webpageUrl']
                 for extent in a.extents():
                     a2.addExtent(extent)
                 for area in a.areas():
                     a2.addArea(area)
                 document.addAnnotation(a2)
             if identifier.startswith(
                     'http://www.portlandpress.com/utopia/glick/'):
                 # Wikipedia entry
                 a2 = spineapi.Annotation()
                 a2['concept'] = 'Definition'
                 a2['author'] = a['author']
                 a2['session:volatile'] = '1'
                 a2['session:legacy'] = '1'
                 a2['property:sourceDatabase'] = 'glick'
                 a2['property:sourceDescription'] = '<p>David M. Glick\'s <a href="http://www.portlandpress.com/pp/books/online/glick/search.htm">Glossary of Biochemistry and Molecular Biology</a>.</p><p>Made available by <a href="http://www.portlandpress.com/">Portland Press Limited</a>.</p>'
                 a2['property:description'] = a[
                     'property:description'] + '<p><em>(Glick Glossary/Portland Press Ltd.)</em></p>'
                 a2['property:name'] = a['property:name']
                 for extent in a.extents():
                     a2.addExtent(extent)
                 for area in a.areas():
                     a2.addArea(area)
                 document.addAnnotation(a2)
 def on_filter_event(self, document, data = None):
     for a in document.annotations():
         if a.get('author') == 'http://utopia.cs.manchester.ac.uk/users/11679' and a.get('concept') in ('Definition', 'DatabaseEntry') and 'session:legacy' not in a:
             document.removeAnnotation(a)
             identifier = a.get('property:identifier', '')
             if identifier.startswith('http://bio2rdf.org/pdb:'):
                 # PDB entry
                 a2 = spineapi.Annotation()
                 a2['concept'] = 'DatabaseEntry'
                 a2['author'] = a['author']
                 a2['session:volatile'] = '1'
                 a2['session:legacy'] = '1'
                 a2['property:sourceDatabase'] = 'pdb'
                 a2['property:sourceDescription'] = '<p>The <a href="http://www.rcsb.org/">Protein Data Bank</a> of the Research Collaboratory for Structural Bioinformatics (<a href="http://home.rcsb.org/">RCSB</a>).</p>'
                 a2['property:identifier'] = identifier
                 a2['property:description'] = 'PDB entry {0}'.format(identifier[-4:].upper())
                 if 'property:name' in a:
                     a2['property:name'] = a['property:name'][:-11]
                 if 'property:imageUrl' in a:
                     a2['property:imageUrl'] = a['property:imageUrl']
                 if 'property:molecularDescription' in a:
                     a2['property:molecularDescription'] = a['property:molecularDescription']
                 if 'property:webpageUrl' in a:
                     a2['property:webpageUrl'] = a['property:webpageUrl']
                 if 'property:embedded' in a:
                     a2['property:embedded'] = a['property:embedded']
                 for extent in a.extents():
                     a2.addExtent(extent)
                 for area in a.areas():
                     a2.addArea(area)
                 document.addAnnotation(a2)
             if identifier.startswith('http://dbpedia.org/resource/'):
                 # Wikipedia entry
                 a2 = spineapi.Annotation()
                 a2['concept'] = 'Definition'
                 a2['author'] = a['author']
                 a2['session:volatile'] = '1'
                 a2['session:legacy'] = '1'
                 a2['property:sourceDatabase'] = 'wikipedia'
                 a2['property:sourceDescription'] = '<p>Structured <a href="http://www.wikipedia.org/">Wikipedia</a> information provided by the <a href="http://DBpedia.org/">DBpedia</a> project.</p>'
                 a2['property:description'] = a.get('property:summary', 'Wikipedia entry')
                 if 'property:name' in a:
                     a2['property:name'] = a['property:name']
                 if 'property:identifier' in a:
                     a2['property:identifier'] = a['property:identifier']
                 if 'property:imageUrl' in a:
                     a2['property:imageUrl'] = a['property:imageUrl']
                 if 'property:summary' in a:
                     a2['property:summary'] = a['property:summary']
                 if 'property:webpageUrl' in a:
                     a2['property:webpageUrl'] = a['property:webpageUrl']
                 for extent in a.extents():
                     a2.addExtent(extent)
                 for area in a.areas():
                     a2.addArea(area)
                 document.addAnnotation(a2)
             if identifier.startswith('http://www.portlandpress.com/utopia/glick/'):
                 # Wikipedia entry
                 a2 = spineapi.Annotation()
                 a2['concept'] = 'Definition'
                 a2['author'] = a['author']
                 a2['session:volatile'] = '1'
                 a2['session:legacy'] = '1'
                 a2['property:sourceDatabase'] = 'glick'
                 a2['property:sourceDescription'] = '<p>David M. Glick\'s <a href="http://www.portlandpress.com/pp/books/online/glick/search.htm">Glossary of Biochemistry and Molecular Biology</a>.</p><p>Made available by <a href="http://www.portlandpress.com/">Portland Press Limited</a>.</p>'
                 a2['property:description'] = a['property:description'] + '<p><em>(Glick Glossary/Portland Press Ltd.)</em></p>'
                 a2['property:name'] = a['property:name']
                 for extent in a.extents():
                     a2.addExtent(extent)
                 for area in a.areas():
                     a2.addArea(area)
                 document.addAnnotation(a2)