예제 #1
0
    def initHistoryData(self,
                        historyFolder,
                        referencesFolder,
                        protocolId,
                        user,
                        activityIDRef={},
                        itemIDRef={}):
        from girderformindlogger.utility import jsonld_expander
        from girderformindlogger.models.item import Item as ItemModel

        activities = list(FolderModel().find(
            {'meta.protocolId': ObjectId(protocolId)}))
        items = list(ItemModel().find(
            {'meta.protocolId': ObjectId(protocolId)}))

        protocol = self.load(protocolId, force=True)
        schemaVersion = protocol['meta'].get('protocol',
                                             {}).get('schema:version', None)

        currentVersion = schemaVersion[0].get(
            '@value', '0.0.0') if schemaVersion else '0.0.0'

        activityIdToHistoryObj = {}
        for activity in activities:
            identifier = activity['meta'].get('activity', {}).get('url', None)
            if identifier:
                activityId = str(activity['_id'])
                if activityId in activityIDRef:
                    activity['_id'] = activityIDRef[activityId]
                    activityId = str(activityIDRef[activityId])

                activityIdToHistoryObj[
                    activityId] = jsonld_expander.insertHistoryData(
                        activity, identifier, 'activity', currentVersion,
                        historyFolder, referencesFolder, user)

        for item in items:
            identifier = item['meta'].get('screen', {}).get('url', None)
            if identifier:
                if str(item['_id']) in itemIDRef:
                    item['_id'] = itemIDRef[str(item['_id'])]

                if str(item['meta']['activityId']) in activityIDRef:
                    item['meta']['activityId'] = activityIDRef[str(
                        item['meta']['activityId'])]

                activityHistoryObj = activityIdToHistoryObj[str(
                    item['meta']['activityId'])]

                item['meta'].update({
                    'originalActivityId':
                    item['meta']['activityId'],
                    'activityId':
                    activityHistoryObj['_id']
                })

                jsonld_expander.insertHistoryData(item, identifier, 'screen',
                                                  currentVersion,
                                                  historyFolder,
                                                  referencesFolder, user)
예제 #2
0
    def __init__(self):
        super(Screen, self).__init__()
        self.resourceName = 'screen'
        self._model = ItemModel()

        # TODO: self.route('PUT', (':id',), self.deactivateScreen)
        self.route('GET', (':id', ), self.getScreen)
        # TODO: self.route('GET', (':id', 'files'), self.getFiles)
        # TODO: self.route('GET', (':id', 'download'), self.download)
        self.route('GET', (), self.getScreenByURL)
        self.route('POST', (), self.createScreen)
예제 #3
0
    def __init__(self):
        super(Item, self).__init__()
        self.resourceName = 'item'
        self._model = ItemModel()

        self.route('DELETE', (':id', ), self.deleteItem)
        self.route('GET', (), self.find)
        self.route('GET', (':id', ), self.getItem)
        self.route('GET', (':id', 'files'), self.getFiles)
        self.route('GET', (':id', 'download'), self.download)
        self.route('GET', (':id', 'rootpath'), self.rootpath)
        self.route('POST', (), self.createItem)
        self.route('PUT', (':id', ), self.updateItem)
        self.route('POST', (':id', 'copy'), self.copyItem)
        self.route('PUT', (':id', 'metadata'), self.setMetadata)
        self.route('DELETE', (':id', 'metadata'), self.deleteMetadata)
예제 #4
0
    def getHistoryDataFromItemIRIs(self, protocolId, IRIGroup):
        from girderformindlogger.models.item import Item as ItemModel
        from girderformindlogger.utility import jsonld_expander

        protocol = self.load(protocolId, force=True)

        items = {}
        activities = {}
        itemReferences = {}
        result = {
            'items': items,
            'activities': activities,
            'itemReferences': itemReferences
        }

        if 'historyId' not in protocol.get('meta', {}):
            return result

        historyFolder = FolderModel().load(protocol['meta']['historyId'],
                                           force=True)
        if 'referenceId' not in historyFolder.get('meta', {}):
            return result

        referencesFolder = FolderModel().load(
            historyFolder['meta']['referenceId'], force=True)
        itemModel = ItemModel()

        for IRI in IRIGroup:
            reference = itemModel.findOne({
                'folderId': referencesFolder['_id'],
                'meta.identifier': IRI
            })
            if not reference:
                continue

            history = reference['meta']['history']

            for version in IRIGroup[IRI]:
                if version not in itemReferences:
                    itemReferences[version] = {}

                inserted = False
                for i in range(0, len(history)):
                    if self.compareVersions(version,
                                            history[i]['version']) <= 0:
                        if not history[i].get('reference', None):
                            continue

                        if history[i]['reference'] not in items:
                            (modelType,
                             referenceId) = history[i]['reference'].split('/')
                            model = MODELS()[modelType]().findOne(
                                {'_id': ObjectId(referenceId)})
                            items[history[i]
                                  ['reference']] = jsonld_expander.loadCache(
                                      model['cached'])

                            activityId = str(model['meta']['activityId'])

                            if activityId not in activities:
                                activities[
                                    activityId] = jsonld_expander.loadCache(
                                        FolderModel().load(
                                            activityId, force=True)['cached'])
                        if history[i]['reference']:
                            itemReferences[version][IRI] = history[i][
                                'reference']
                        inserted = True

                        break

                if not inserted:
                    itemReferences[version][
                        IRI] = None  # this is same as latest version

        return result
예제 #5
0
def cycleModels(IRIset, modelType=None, meta={}):
    from girderformindlogger.constants import HIERARCHY, REPROLIB_TYPES
    from girderformindlogger.models.folder import Folder as FolderModel
    from girderformindlogger.models.item import Item as ItemModel
    from girderformindlogger.utility.jsonld_expander import reprolibCanonize

    cachedDoc = None
    primary = [modelType] if isinstance(
        modelType, str) else [] if modelType is None else modelType
    secondary = [m for m in HIERARCHY if m not in primary]

    del modelType

    if len(primary):
        query = {
            '$and': [
                {  # search by type
                    '$or': [{
                        'meta.{}.@type'.format(modelType): {
                            "$in": [
                                t for t in [
                                    reprolibCanonize(REPROLIB_TYPES[modelType]
                                                     ), 'reproschema:{}'.
                                    format(suffix), 'reprolib:{}'.format(
                                        suffix), 'reprolib:schemas/{}'.format(
                                            suffix), suffix
                                ] if t is not None
                            ]
                            for suffix in
                            [REPROLIB_TYPES[modelType].split('/')[-1]]
                        }
                    } for modelType in primary if modelType in REPROLIB_TYPES]
                },
                {  # search by url
                    '$or': [{
                        'meta.{}.url'.format(modelType): {
                            '$in': list(IRIset)
                        }
                    } for modelType in primary if modelType in REPROLIB_TYPES]
                },
                *[{
                    'meta.{}'.format(key): meta[key]
                } for key in meta]
            ]
        }
        cachedDoc = (FolderModel()
                     if not any(['screen' in primary, 'item' in primary]) else
                     ItemModel()).findOne(query)
    if cachedDoc is None:
        query = {
            '$and': [{
                '$or': [{
                    'meta.{}.@type'.format(modelType): {
                        "$in": [
                            t for t in [
                                reprolibCanonize(REPROLIB_TYPES[modelType]),
                                'reproschema:{}'.format(
                                    suffix), 'reprolib:{}'.format(suffix),
                                'reprolib:schemas/{}'.format(suffix), suffix
                            ] if t is not None
                        ]
                        for suffix in
                        [REPROLIB_TYPES[modelType].split('/')[-1]]
                    }
                } for modelType in secondary if modelType in REPROLIB_TYPES]
            }, {
                '$or': [{
                    'meta.{}.url'.format(modelType): {
                        '$in': list(IRIset)
                    }
                } for modelType in secondary if modelType in REPROLIB_TYPES]
            }, *[{
                'meta.{}'.format(key): meta[key]
            } for key in meta]]
        }
        cachedDoc = FolderModel().findOne(query)
    if cachedDoc is None:
        cachedDoc = ItemModel().findOne(query)

    if cachedDoc is None:
        return (None, None)

    modelType = [
        rt for rt in list(REPROLIB_TYPES.keys())
        if '@type' in cachedDoc.get('meta', {}).get(rt, {})
    ]
    modelType = modelType[0] if len(modelType) else None

    print("Found {}/{}".format(modelType, str(cachedDoc['_id'])))
    return (modelType, cachedDoc)