コード例 #1
0
    def loadCrfItem(self, formOid, itemOid, metadata):
        """Load CRF item details from ODM metadata
        """
        item = None

        self.logger.info("CRF form OID: " + formOid)
        self.logger.info("CRF item OID: " + itemOid)

        # Check if file path is setup
        if metadata:
            documentTree = ET.ElementTree((ET.fromstring(str(metadata))))

            # Locate ItemDefs data in XML file via XPath
            for itemElement in documentTree.iterfind('.//odm:ItemDef[@OID="' + itemOid + '"]', namespaces=nsmaps):
                # Check FormOID, normally I would do it in XPath but python does not support contaions wildcard
                if itemElement.attrib["{http://www.openclinica.org/ns/odm_ext_v130/v3.1}FormOIDs"].find(formOid) != -1:
                    item = Item()
                    item.oid = itemElement.attrib['OID']
                    item.name = itemElement.attrib['Name']
                    item.description = itemElement.attrib['Comment']
                    item.dataType = itemElement.attrib['DataType']

                    for itemDetails in itemElement:
                        if (str(itemDetails.tag)).strip() == "{http://www.openclinica.org/ns/odm_ext_v130/v3.1}ItemDetails":
                            for detailsElement in itemDetails:
                                if (str(detailsElement.tag)).strip() == "{http://www.openclinica.org/ns/odm_ext_v130/v3.1}ItemPresentInForm":
                                    for presentForm in detailsElement:
                                        if (str(presentForm.tag)).strip() == "{http://www.openclinica.org/ns/odm_ext_v130/v3.1}LeftItemText":
                                            item.label = presentForm.text

        # Return resulting CRT item
        return item
コード例 #2
0
    def loadCrfItem(self, formOid, itemOid, metadata):
        """Load CRF item details from ODM metadata
        """
        item = None

        self.logger.info("CRF form OID: " +  formOid)
        self.logger.info("CRF item OID: " + itemOid)

        # Check if file path is setup
        if (metadata):
            documentTree = ET.ElementTree((ET.fromstring(str(metadata))))

            # Locate ItemDefs data in XML file via XPath
            for itemElement in documentTree.iterfind('.//odm:ItemDef[@OID="' + itemOid + '"]', namespaces=nsmaps):
                # Check FormOID, normally I would do it in XPath but python does not support contaions wildcard
                if itemElement.attrib["{http://www.openclinica.org/ns/odm_ext_v130/v3.1}FormOIDs"].find(formOid) != -1:
                    item = Item()
                    item.oid = itemElement.attrib['OID']
                    item.name = itemElement.attrib['Name']
                    item.description = itemElement.attrib['Comment']
                    item.dataType = itemElement.attrib['DataType']

                    for itemDetails in itemElement:
                        if (str(itemDetails.tag)).strip() == "{http://www.openclinica.org/ns/odm_ext_v130/v3.1}ItemDetails":
                            for detailsElement in itemDetails:
                                if (str(detailsElement.tag)).strip() == "{http://www.openclinica.org/ns/odm_ext_v130/v3.1}ItemPresentInForm":
                                    for presentForm in detailsElement:
                                        if (str(presentForm.tag)).strip() == "{http://www.openclinica.org/ns/odm_ext_v130/v3.1}LeftItemText":
                                            item.label = presentForm.text

        # Return resulting CRT item
        return item
コード例 #3
0
    def getStudyCasebookEvents(self, data, thread=None):
        """Get study casebook subject events
        """
        if data:
            ocBaseUrl = data[0]
            studyOid = data[1]
            subjectOid = data[2]

        method = studyOid + "/" + subjectOid + "/*/*"
        results = []

        r = self._ocRequest(ocBaseUrl, method)

        if r.status_code == 200:
            if "ClinicalData" in r.json():
                if "SubjectData" in r.json()["ClinicalData"]:
                    if "StudyEventData" in r.json(
                    )["ClinicalData"]["SubjectData"]:

                        eventData = r.json(
                        )["ClinicalData"]["SubjectData"]["StudyEventData"]

                        # Multiple events
                        if type(eventData) is list:
                            for ed in eventData:
                                event = Event()
                                event.eventDefinitionOID = ed["@StudyEventOID"]
                                event.status = ed["@OpenClinica:Status"]

                                dateString = ed["@OpenClinica:StartDate"]
                                format = ""
                                # Is it only date or datetime
                                if len(dateString) == 11:
                                    format = "%d-%b-%Y"
                                elif len(dateString) == 20:
                                    format = "%d-%b-%Y %H:%M:%S"

                                event.startDate = datetime.strptime(
                                    dateString, format)
                                event.studyEventRepeatKey = ed[
                                    "@StudyEventRepeatKey"]

                                # Subject Age At Event is optional (because collect birth date is optional)
                                if "OpenClinica:SubjectAgeAtEvent" in ed:
                                    event.subjectAgeAtEvent = ed[
                                        "OpenClinica:SubjectAgeAtEvent"]

                                # Resulting eCRFs
                                if "FormData" in ed:
                                    formData = ed["FormData"]

                                    # Multiple forms
                                    if type(formData) is list:
                                        for frm in formData:
                                            crf = Crf()
                                            crf.oid = frm["@FormOID"]
                                            crf.version = frm[
                                                "@OpenClinica:Version"]
                                            crf.status = frm[
                                                "@OpenClinica:Status"]
                                            event.addCrf(crf)
                                    # Only one form in event
                                    elif type(formData) is dict:
                                        frm = formData
                                        crf = Crf()
                                        crf.oid = frm["@FormOID"]
                                        crf.version = frm[
                                            "@OpenClinica:Version"]
                                        crf.status = frm["@OpenClinica:Status"]
                                        event.addCrf(crf)

                                # + automatically schedule default version only (if it is not)
                                eventFormOids = []

                                eventDefinition = r.json(
                                )["Study"]["MetaDataVersion"]["StudyEventDef"]
                                if type(eventDefinition) is list:
                                    for ed in eventDefinition:
                                        formRef = ed["FormRef"]
                                        if type(formRef) is list:
                                            for fr in formRef:
                                                eventFormOids.append(
                                                    fr["@FormOID"])
                                        elif type(formRef) is dict:
                                            eventFormOids.append(
                                                formRef["@FormOID"])
                                elif type(eventDefinition) is dict:
                                    ed = eventDefinition
                                    formRef = ed["FormRef"]
                                    if type(formRef) is list:
                                        for fr in formRef:
                                            eventFormOids.append(
                                                fr["@FormOID"])
                                    elif type(formRef) is dict:
                                        eventFormOids.append(
                                            formRef["@FormOID"])

                                formDefinition = r.json(
                                )["Study"]["MetaDataVersion"]["FormDef"]
                                if type(formDefinition) is list:
                                    for fd in formDefinition:
                                        if fd["@OID"] in eventFormOids:
                                            if not event.hasScheduledCrf(
                                                    fd["@OID"]):
                                                if fd["OpenClinica:FormDetails"][
                                                        "OpenClinica:PresentInEventDefinition"][
                                                            "@IsDefaultVersion"] == "Yes":
                                                    event.addCrf(
                                                        Crf(
                                                            fd["@OID"],
                                                            fd["@Name"]))

                                            else:
                                                if fd["OpenClinica:FormDetails"][
                                                        "OpenClinica:PresentInEventDefinition"][
                                                            "@IsDefaultVersion"] == "Yes":
                                                    crf = event.getCrf(
                                                        fd["@OID"])
                                                    crf.name = fd["@Name"]

                                            # Collect references to ItemGroups in any case
                                            igRef = fd["ItemGroupRef"]
                                            if type(igRef) is list:
                                                for igr in igRef:
                                                    itemGroupOids.append(
                                                        igr["@ItemGroupOID"])
                                            elif type(igRef) is dict:
                                                igr = igRef
                                                itemGroupOids.append(
                                                    igr["@ItemGroupOID"])

                                elif type(formDefinition) is dict:
                                    fd = formDefinition
                                    if fd["@OID"] in eventFormOids:
                                        if not event.hasScheduledCrf(
                                                fd["@OID"]):
                                            if fd["OpenClinica:FormDetails"][
                                                    "OpenClinica:PresentInEventDefinition"][
                                                        "@IsDefaultVersion"] == "Yes":
                                                event.addCrf(
                                                    Crf(
                                                        fd["@OID"],
                                                        fd["@Name"]))

                                        else:
                                            if fd["OpenClinica:FormDetails"][
                                                    "OpenClinica:PresentInEventDefinition"][
                                                        "@IsDefaultVersion"] == "Yes":
                                                crf = event.getCrf(fd["@OID"])
                                                crf.name = fd["@Name"]

                                        # Collect references to ItemGroups in any case
                                        igRef = fd["ItemGroupRef"]
                                        if type(igRef) is list:
                                            for igr in igRef:
                                                itemGroupOids.append(
                                                    igr["@ItemGroupOID"])
                                        elif type(igRef) is dict:
                                            igr = igRef
                                            itemGroupOids.append(
                                                igr["@ItemGroupOID"])

                                # Discover items base on theri refOIDs
                                itemOids = []
                                itemGroupDefinition = r.json(
                                )["Study"]["MetaDataVersion"]["ItemGroupDef"]
                                if type(itemGroupDefinition) is list:
                                    for igd in itemGroupDefinition:
                                        if igd["@OID"] in itemGroupOids:
                                            crf = event.getCrf(igd[
                                                "OpenClinica:ItemGroupDetails"][
                                                    "OpenClinica:PresentInForm"]
                                                               ["@FormOID"])

                                            group = ItemGroup()
                                            group.oid = igd["@OID"]
                                            group.name = igd["@Name"]

                                            iRef = igd["ItemRef"]
                                            if type(iRef) is list:
                                                for ir in iRef:
                                                    item = Item()
                                                    item.oid = ir["@ItemOID"]
                                                    group.addItem(item)
                                                    itemOids.append(
                                                        ir["@ItemOID"])
                                            elif type(iRef) is dict:
                                                ir = iRef
                                                item = Item()
                                                item.oid = ir["@ItemOID"]
                                                group.addItem(item)
                                                itemOids.append(ir["@ItemOID"])

                                            crf.addItemGroup(group)

                                elif type(itemGroupDefinition) is dict:
                                    igd = itemGroupDefinition
                                    if igd["@OID"] in itemGroupOids:
                                        crf = event.getCrf(
                                            igd["OpenClinica:ItemGroupDetails"]
                                            ["OpenClinica:PresentInForm"]
                                            ["@FormOID"])

                                        group = ItemGroup()
                                        group.oid = igd["@OID"]
                                        group.name = igd["@Name"]

                                        iRef = igd["ItemRef"]
                                        if type(iRef) is list:
                                            for ir in iRef:
                                                item = Item()
                                                item.oid = ir["@ItemOID"]
                                                group.addItem(item)
                                                itemOids.append(ir["@ItemOID"])
                                        elif type(iRef) is dict:
                                            ir = iRef
                                            item = Item()
                                            item.oid = ir["@ItemOID"]
                                            group.addItem(item)
                                            itemOids.append(ir["@ItemOID"])

                                        crf.addItemGroup(group)

                                # Setup items for CRF in event
                                itemDefinition = r.json(
                                )["Study"]["MetaDataVersion"]["ItemDef"]
                                if type(itemDefinition) is list:
                                    for itemDef in itemDefinition:
                                        if itemDef["@OID"] in itemOids:
                                            crf = event.getCrf(itemDef[
                                                "@OpenClinica:FormOIDs"])

                                            item = Item()
                                            item.oid = itemDef["@OID"]
                                            item.name = itemDef["@Name"]
                                            item.dataType = itemDef[
                                                "@DataType"]
                                            item.description = itemDef[
                                                "@Comment"]

                                            gr = crf.findGroupForItem(item.oid)
                                            item.itemGroupOid = gr.oid

                                            crf.items.append(item)

                                elif type(itemDefinition) is dict:
                                    itemDef = itemDefinition
                                    if itemDef["@OID"] in itemOids:
                                        crf = event.getCrf(
                                            itemDef["@OpenClinica:FormOIDs"])

                                        item = Item()
                                        item.oid = itemDef["@OID"]
                                        item.name = itemDef["@Name"]
                                        item.dataType = itemDef["@DataType"]
                                        item.description = itemDef["@Comment"]

                                        gr = crf.findGroupForItem(item.oid)
                                        item.itemGroupOid = gr.oid

                                        crf.items.append(item)

                                results.append(event)
                        # Only one event reported
                        elif type(eventData) is dict:
                            ed = eventData

                            event = Event()
                            event.eventDefinitionOID = ed["@StudyEventOID"]
                            event.status = ed["@OpenClinica:Status"]
                            dateString = ed["@OpenClinica:StartDate"]

                            format = ""
                            # Is it only date or datetime
                            if len(dateString) == 11:
                                format = "%d-%b-%Y"
                            elif len(dateString) == 20:
                                format = "%d-%b-%Y %H:%M:%S"

                            event.startDate = datetime.strptime(
                                dateString, format)
                            event.studyEventRepeatKey = ed[
                                "@StudyEventRepeatKey"]

                            # Subject Age At Event is optional (because collect birth date is optional)
                            if "OpenClinica:SubjectAgeAtEvent" in ed:
                                event.subjectAgeAtEvent = ed[
                                    "OpenClinica:SubjectAgeAtEvent"]

                            # Resulting eCRFs
                            if "FormData" in ed:
                                formData = ed["FormData"]

                                # Multiple forms
                                if type(formData) is list:
                                    for frm in formData:
                                        crf = Crf()
                                        crf.oid = frm["@FormOID"]
                                        crf.version = frm[
                                            "@OpenClinica:Version"]
                                        crf.status = frm["@OpenClinica:Status"]
                                        event.addCrf(crf)
                                # Only one form in event
                                elif type(formData) is dict:
                                    frm = formData
                                    crf = Crf()
                                    crf.oid = frm["@FormOID"]
                                    crf.version = frm["@OpenClinica:Version"]
                                    crf.status = frm["@OpenClinica:Status"]
                                    event.addCrf(crf)

                            # + automatically default version (if it is not)
                            eventFormOids = []
                            eventDefinition = r.json(
                            )["Study"]["MetaDataVersion"]["StudyEventDef"]
                            if type(eventDefinition) is list:
                                for ed in eventDefinition:
                                    formRef = ed["FormRef"]
                                    if type(formRef) is list:
                                        for fr in formRef:
                                            eventFormOids.append(
                                                fr["@FormOID"])
                                    elif type(formRef) is dict:
                                        eventFormOids.append(
                                            formRef["@FormOID"])
                            elif type(eventDefinition) is dict:
                                ed = eventDefinition
                                formRef = ed["FormRef"]
                                if type(formRef) is list:
                                    for fr in formRef:
                                        eventFormOids.append(fr["@FormOID"])
                                elif type(formRef) is dict:
                                    eventFormOids.append(formRef["@FormOID"])

                            itemGroupOids = []
                            formDefinition = r.json(
                            )["Study"]["MetaDataVersion"]["FormDef"]
                            if type(formDefinition) is list:
                                for fd in formDefinition:
                                    if fd["@OID"] in eventFormOids:
                                        if not event.hasScheduledCrf(
                                                fd["@OID"]):
                                            if fd["OpenClinica:FormDetails"][
                                                    "OpenClinica:PresentInEventDefinition"][
                                                        "@IsDefaultVersion"] == "Yes":
                                                event.addCrf(
                                                    Crf(
                                                        fd["@OID"],
                                                        fd["@Name"]))
                                        else:
                                            if fd["OpenClinica:FormDetails"][
                                                    "OpenClinica:PresentInEventDefinition"][
                                                        "@IsDefaultVersion"] == "Yes":
                                                crf = event.getCrf(fd["@OID"])
                                                crf.name = fd["@Name"]

                                        # Collect references to ItemGroups in any case
                                        igRef = fd["ItemGroupRef"]
                                        if type(igRef) is list:
                                            for igr in igRef:
                                                itemGroupOids.append(
                                                    igr["@ItemGroupOID"])
                                        elif type(igRef) is dict:
                                            igr = igRef
                                            itemGroupOids.append(
                                                igr["@ItemGroupOID"])

                            elif type(formDefinition) is dict:
                                fd = formDefinition
                                if fd["@OID"] in eventFormOids:
                                    if not event.hasScheduledCrf(fd["@OID"]):
                                        if fd["OpenClinica:FormDetails"][
                                                "OpenClinica:PresentInEventDefinition"][
                                                    "@IsDefaultVersion"] == "Yes":
                                            event.addCrf(
                                                Crf(fd["@OID"], fd["@Name"]))
                                    else:
                                        if fd["OpenClinica:FormDetails"][
                                                "OpenClinica:PresentInEventDefinition"][
                                                    "@IsDefaultVersion"] == "Yes":
                                            crf = event.getCrf(fd["@OID"])
                                            crf.name = fd["@Name"]

                                    # Collect references to ItemGroups in any case
                                    igRef = fd["ItemGroupRef"]
                                    if type(igRef) is list:
                                        for igr in igRef:
                                            itemGroupOids.append(
                                                igr["@ItemGroupOID"])
                                    elif type(igRef) is dict:
                                        igr = igRef
                                        itemGroupOids.append(
                                            igr["@ItemGroupOID"])

                            # Discover items base on theri refOIDs
                            itemOids = []
                            itemGroupDefinition = r.json(
                            )["Study"]["MetaDataVersion"]["ItemGroupDef"]
                            if type(itemGroupDefinition) is list:
                                for igd in itemGroupDefinition:
                                    if igd["@OID"] in itemGroupOids:

                                        crf = event.getCrf(
                                            igd["OpenClinica:ItemGroupDetails"]
                                            ["OpenClinica:PresentInForm"]
                                            ["@FormOID"])

                                        group = ItemGroup()
                                        group.oid = igd["@OID"]
                                        group.name = igd["@Name"]

                                        iRef = igd["ItemRef"]
                                        if type(iRef) is list:
                                            for ir in iRef:
                                                item = Item()
                                                item.oid = ir["@ItemOID"]
                                                group.addItem(item)
                                                itemOids.append(ir["@ItemOID"])
                                        elif type(iRef) is dict:
                                            ir = iRef
                                            item = Item()
                                            item.oid = ir["@ItemOID"]
                                            group.addItem(item)
                                            itemOids.append(ir["@ItemOID"])

                                        crf.addItemGroup(group)

                            elif type(itemGroupDefinition) is dict:
                                igd = itemGroupDefinition
                                if igd["@OID"] in itemGroupOids:

                                    crf = event.getCrf(
                                        igd["OpenClinica:ItemGroupDetails"]
                                        ["OpenClinica:PresentInForm"]
                                        ["@FormOID"])

                                    group = ItemGroup()
                                    group.oid = igd["@OID"]
                                    group.name = igd["@Name"]

                                    iRef = igd["ItemRef"]
                                    if type(iRef) is list:
                                        for ir in iRef:
                                            item = Item()
                                            item.oid = ir["@ItemOID"]
                                            group.addItem(item)
                                            itemOids.append(ir["@ItemOID"])
                                    elif type(iRef) is dict:
                                        ir = iRef
                                        item = Item()
                                        item.oid = ir["@ItemOID"]
                                        group.addItem(item)
                                        itemOids.append(ir["@ItemOID"])

                                    crf.addItemGroup(group)

                            # Setup items for CRF in event
                            itemDefinition = r.json(
                            )["Study"]["MetaDataVersion"]["ItemDef"]
                            if type(itemDefinition) is list:
                                for itemDef in itemDefinition:
                                    if itemDef["@OID"] in itemOids:
                                        crf = event.getCrf(
                                            itemDef["@OpenClinica:FormOIDs"])

                                        item = Item()
                                        item.oid = itemDef["@OID"]
                                        item.name = itemDef["@Name"]
                                        item.dataType = itemDef["@DataType"]
                                        item.description = itemDef["@Comment"]

                                        gr = crf.findGroupForItem(item.oid)
                                        item.itemGroupOid = gr.oid

                                        crf.items.append(item)

                            elif type(itemDefinition) is dict:
                                itemDef = itemDefinition
                                if itemDef["@OID"] in itemOids:
                                    crf = event.getCrf(
                                        itemDef["@OpenClinica:FormOIDs"])

                                    item = Item()
                                    item.oid = itemDef["@OID"]
                                    item.name = itemDef["@Name"]
                                    item.dataType = itemDef["@DataType"]
                                    item.description = itemDef["@Comment"]

                                    gr = crf.findGroupForItem(item.oid)
                                    item.itemGroupOid = gr.oid

                                    crf.items.append(item)

                            results.append(event)
        if thread:
            thread.emit(QtCore.SIGNAL("finished(QVariant)"), results)
            return None
        else:
            return results
コード例 #4
0
    def getStudyCasebookEvents(self, data, thread=None):
        """Get study casebook subject events
        """
        if data:
            ocBaseUrl = data[0]
            studyOid = data[1]
            subjectOid = data[2]

        method = studyOid + "/" + subjectOid + "/*/*"
        results = []

        r = self._ocRequest(ocBaseUrl, method)

        if r.status_code == 200:
            if "ClinicalData" in r.json():
                if "SubjectData" in r.json()["ClinicalData"]:
                    if "StudyEventData" in r.json()["ClinicalData"]["SubjectData"]:
                        
                        eventData = r.json()["ClinicalData"]["SubjectData"]["StudyEventData"]

                        # Multiple events
                        if type(eventData) is list:
                            for ed in eventData:
                                event = Event()
                                event.eventDefinitionOID = ed["@StudyEventOID"]
                                event.status = ed["@OpenClinica:Status"]

                                dateString = ed["@OpenClinica:StartDate"]
                                format = ""
                                # Is it only date or datetime
                                if len(dateString) == 11:
                                    format = "%d-%b-%Y"
                                elif len(dateString) == 20:
                                    format = "%d-%b-%Y %H:%M:%S"

                                event.startDate = datetime.strptime(dateString, format)
                                event.studyEventRepeatKey = ed["@StudyEventRepeatKey"]

                                # Subject Age At Event is optional (because collect birth date is optional)
                                if "OpenClinica:SubjectAgeAtEvent" in ed:
                                    event.subjectAgeAtEvent = ed["OpenClinica:SubjectAgeAtEvent"]

                                # Resulting eCRFs
                                if "FormData" in ed:
                                    formData = ed["FormData"]

                                    # Multiple forms
                                    if type(formData) is list:
                                        for frm in formData:
                                            crf = Crf()
                                            crf.oid = frm["@FormOID"]
                                            crf.version = frm["@OpenClinica:Version"]
                                            crf.status = frm["@OpenClinica:Status"]
                                            event.addCrf(crf)
                                    # Only one form in event
                                    elif type(formData) is dict:
                                        frm  = formData
                                        crf = Crf()
                                        crf.oid = frm["@FormOID"]
                                        crf.version = frm["@OpenClinica:Version"]
                                        crf.status = frm["@OpenClinica:Status"]
                                        event.addCrf(crf)
                                
                                # + automatically schedule default version only (if it is not)
                                eventFormOids = []

                                eventDefinition = r.json()["Study"]["MetaDataVersion"]["StudyEventDef"]
                                if type(eventDefinition) is list:
                                    for ed in eventDefinition:
                                        formRef = ed["FormRef"]
                                        if type(formRef) is list:
                                            for fr in formRef:
                                                eventFormOids.append(fr["@FormOID"])
                                        elif type(formRef) is dict:
                                            eventFormOids.append(formRef["@FormOID"])
                                elif type(eventDefinition) is dict:
                                    ed = eventDefinition
                                    formRef = ed["FormRef"]
                                    if type(formRef) is list:
                                        for fr in formRef:
                                            eventFormOids.append(fr["@FormOID"])
                                    elif type(formRef) is dict:
                                        eventFormOids.append(formRef["@FormOID"])

                                formDefinition = r.json()["Study"]["MetaDataVersion"]["FormDef"]
                                if type(formDefinition) is list:
                                    for fd in formDefinition:
                                        if fd["@OID"] in eventFormOids:
                                            if not event.hasScheduledCrf(fd["@OID"]):
                                                if fd["OpenClinica:FormDetails"]["OpenClinica:PresentInEventDefinition"]["@IsDefaultVersion"] == "Yes":
                                                    event.addCrf(
                                                        Crf(fd["@OID"], fd["@Name"])
                                                    )

                                            else:
                                                if fd["OpenClinica:FormDetails"]["OpenClinica:PresentInEventDefinition"]["@IsDefaultVersion"] == "Yes":
                                                    crf = event.getCrf(fd["@OID"])
                                                    crf.name = fd["@Name"]

                                            # Collect references to ItemGroups in any case
                                            igRef = fd["ItemGroupRef"]
                                            if type(igRef) is list:
                                                for igr in igRef:
                                                    itemGroupOids.append(igr["@ItemGroupOID"])
                                            elif type (igRef) is dict:
                                                igr = igRef
                                                itemGroupOids.append(igr["@ItemGroupOID"])

                                elif type(formDefinition) is dict:
                                    fd = formDefinition
                                    if fd["@OID"] in eventFormOids:
                                        if not event.hasScheduledCrf(fd["@OID"]):
                                            if fd["OpenClinica:FormDetails"]["OpenClinica:PresentInEventDefinition"]["@IsDefaultVersion"] == "Yes":
                                                event.addCrf(
                                                    Crf(fd["@OID"], fd["@Name"])
                                                )

                                        else:
                                            if fd["OpenClinica:FormDetails"]["OpenClinica:PresentInEventDefinition"]["@IsDefaultVersion"] == "Yes":
                                                crf = event.getCrf(fd["@OID"])
                                                crf.name = fd["@Name"]

                                        # Collect references to ItemGroups in any case
                                        igRef = fd["ItemGroupRef"]
                                        if type(igRef) is list:
                                            for igr in igRef:
                                                itemGroupOids.append(igr["@ItemGroupOID"])
                                        elif type (igRef) is dict:
                                            igr = igRef
                                            itemGroupOids.append(igr["@ItemGroupOID"])

                                # Discover items base on theri refOIDs
                                itemOids = []
                                itemGroupDefinition = r.json()["Study"]["MetaDataVersion"]["ItemGroupDef"]
                                if type(itemGroupDefinition) is list:
                                    for igd in itemGroupDefinition:
                                        if igd["@OID"] in itemGroupOids:
                                            crf = event.getCrf(
                                                igd["OpenClinica:ItemGroupDetails"]["OpenClinica:PresentInForm"]["@FormOID"]
                                            )

                                            group = ItemGroup()
                                            group.oid = igd["@OID"]
                                            group.name = igd["@Name"]

                                            iRef = igd["ItemRef"]
                                            if type(iRef) is list:
                                                for ir in iRef:
                                                    item = Item()
                                                    item.oid = ir["@ItemOID"]
                                                    group.addItem(item)
                                                    itemOids.append(ir["@ItemOID"])
                                            elif type(iRef) is dict:
                                                ir = iRef
                                                item = Item()
                                                item.oid = ir["@ItemOID"]
                                                group.addItem(item)
                                                itemOids.append(ir["@ItemOID"])

                                            crf.addItemGroup(group)

                                elif type(itemGroupDefinition) is dict:
                                    igd = itemGroupDefinition
                                    if igd["@OID"] in itemGroupOids:
                                        crf = event.getCrf(
                                            igd["OpenClinica:ItemGroupDetails"]["OpenClinica:PresentInForm"]["@FormOID"]
                                        )

                                        group = ItemGroup()
                                        group.oid = igd["@OID"]
                                        group.name = igd["@Name"]

                                        iRef = igd["ItemRef"]
                                        if type(iRef) is list:
                                            for ir in iRef:
                                                item = Item()
                                                item.oid = ir["@ItemOID"]
                                                group.addItem(item)
                                                itemOids.append(ir["@ItemOID"])
                                        elif type(iRef) is dict:
                                            ir = iRef
                                            item = Item()
                                            item.oid = ir["@ItemOID"]
                                            group.addItem(item)
                                            itemOids.append(ir["@ItemOID"])

                                        crf.addItemGroup(group)

                                # Setup items for CRF in event
                                itemDefinition = r.json()["Study"]["MetaDataVersion"]["ItemDef"]
                                if type(itemDefinition) is list:
                                    for itemDef in itemDefinition:
                                        if itemDef["@OID"] in itemOids:
                                            crf = event.getCrf(itemDef["@OpenClinica:FormOIDs"])

                                            item = Item()
                                            item.oid = itemDef["@OID"]
                                            item.name = itemDef["@Name"]
                                            item.dataType = itemDef["@DataType"]
                                            item.description = itemDef["@Comment"]
                                            
                                            gr = crf.findGroupForItem(item.oid)
                                            item.itemGroupOid = gr.oid

                                            crf.items.append(item)

                                elif type(itemDefinition) is dict:
                                    itemDef = itemDefinition
                                    if itemDef["@OID"] in itemOids:
                                        crf = event.getCrf(itemDef["@OpenClinica:FormOIDs"])

                                        item = Item()
                                        item.oid = itemDef["@OID"]
                                        item.name = itemDef["@Name"]
                                        item.dataType = itemDef["@DataType"]
                                        item.description = itemDef["@Comment"]
                                        
                                        gr = crf.findGroupForItem(item.oid)
                                        item.itemGroupOid = gr.oid

                                        crf.items.append(item)


                                results.append(event)
                        # Only one event reported
                        elif type(eventData) is dict:
                            ed = eventData

                            event = Event()
                            event.eventDefinitionOID = ed["@StudyEventOID"]
                            event.status = ed["@OpenClinica:Status"]
                            dateString = ed["@OpenClinica:StartDate"]

                            format = ""
                            # Is it only date or datetime
                            if len(dateString) == 11:
                                format = "%d-%b-%Y"
                            elif len(dateString) == 20:
                                format = "%d-%b-%Y %H:%M:%S"

                            event.startDate = datetime.strptime(dateString, format)
                            event.studyEventRepeatKey = ed["@StudyEventRepeatKey"]

                            # Subject Age At Event is optional (because collect birth date is optional)
                            if "OpenClinica:SubjectAgeAtEvent" in ed:
                                event.subjectAgeAtEvent = ed["OpenClinica:SubjectAgeAtEvent"]

                            # Resulting eCRFs
                            if "FormData" in ed:
                                formData = ed["FormData"]

                                # Multiple forms
                                if type(formData) is list:
                                    for frm in formData:
                                        crf = Crf()
                                        crf.oid = frm["@FormOID"]
                                        crf.version = frm["@OpenClinica:Version"]
                                        crf.status = frm["@OpenClinica:Status"]
                                        event.addCrf(crf)
                                # Only one form in event
                                elif type(formData) is dict:
                                    frm  = formData
                                    crf = Crf()
                                    crf.oid = frm["@FormOID"]
                                    crf.version = frm["@OpenClinica:Version"]
                                    crf.status = frm["@OpenClinica:Status"]
                                    event.addCrf(crf)

                            # + automatically default version (if it is not)
                            eventFormOids = []
                            eventDefinition = r.json()["Study"]["MetaDataVersion"]["StudyEventDef"]
                            if type(eventDefinition) is list:
                                for ed in eventDefinition:
                                    formRef = ed["FormRef"]
                                    if type(formRef) is list:
                                        for fr in formRef:
                                            eventFormOids.append(fr["@FormOID"])
                                    elif type(formRef) is dict:
                                        eventFormOids.append(formRef["@FormOID"])
                            elif type(eventDefinition) is dict:
                                ed = eventDefinition
                                formRef = ed["FormRef"]
                                if type(formRef) is list:
                                    for fr in formRef:
                                        eventFormOids.append(fr["@FormOID"])
                                elif type(formRef) is dict:
                                    eventFormOids.append(formRef["@FormOID"])

                            itemGroupOids = []
                            formDefinition = r.json()["Study"]["MetaDataVersion"]["FormDef"]
                            if type(formDefinition) is list:
                                for fd in formDefinition:
                                    if fd["@OID"] in eventFormOids:
                                        if not event.hasScheduledCrf(fd["@OID"]):
                                            if fd["OpenClinica:FormDetails"]["OpenClinica:PresentInEventDefinition"]["@IsDefaultVersion"] == "Yes":
                                                event.addCrf(
                                                    Crf(fd["@OID"], fd["@Name"])
                                                )
                                        else:
                                            if fd["OpenClinica:FormDetails"]["OpenClinica:PresentInEventDefinition"]["@IsDefaultVersion"] == "Yes":
                                                crf = event.getCrf(fd["@OID"])
                                                crf.name = fd["@Name"]

                                        # Collect references to ItemGroups in any case
                                        igRef = fd["ItemGroupRef"]
                                        if type(igRef) is list:
                                            for igr in igRef:
                                                itemGroupOids.append(igr["@ItemGroupOID"])
                                        elif type (igRef) is dict:
                                            igr = igRef
                                            itemGroupOids.append(igr["@ItemGroupOID"])

                            elif type(formDefinition) is dict:
                                fd = formDefinition
                                if fd["@OID"] in eventFormOids:
                                    if not event.hasScheduledCrf(fd["@OID"]):
                                        if fd["OpenClinica:FormDetails"]["OpenClinica:PresentInEventDefinition"]["@IsDefaultVersion"] == "Yes":
                                            event.addCrf(
                                                Crf(fd["@OID"], fd["@Name"])
                                            )
                                    else:
                                        if fd["OpenClinica:FormDetails"]["OpenClinica:PresentInEventDefinition"]["@IsDefaultVersion"] == "Yes":
                                            crf = event.getCrf(fd["@OID"])
                                            crf.name = fd["@Name"]

                                    # Collect references to ItemGroups in any case
                                    igRef = fd["ItemGroupRef"]
                                    if type(igRef) is list:
                                        for igr in igRef:
                                            itemGroupOids.append(igr["@ItemGroupOID"])
                                    elif type (igRef) is dict:
                                        igr = igRef
                                        itemGroupOids.append(igr["@ItemGroupOID"])

                            # Discover items base on theri refOIDs
                            itemOids = []
                            itemGroupDefinition = r.json()["Study"]["MetaDataVersion"]["ItemGroupDef"]
                            if type(itemGroupDefinition) is list:
                                for igd in itemGroupDefinition:
                                    if igd["@OID"] in itemGroupOids:
                                        
                                        crf = event.getCrf(
                                            igd["OpenClinica:ItemGroupDetails"]["OpenClinica:PresentInForm"]["@FormOID"]
                                        )

                                        group = ItemGroup()
                                        group.oid = igd["@OID"]
                                        group.name = igd["@Name"]

                                        iRef = igd["ItemRef"]
                                        if type(iRef) is list:
                                            for ir in iRef:
                                                item = Item()
                                                item.oid = ir["@ItemOID"]
                                                group.addItem(item)
                                                itemOids.append(ir["@ItemOID"])
                                        elif type(iRef) is dict:
                                            ir = iRef
                                            item = Item()
                                            item.oid = ir["@ItemOID"]
                                            group.addItem(item)
                                            itemOids.append(ir["@ItemOID"])

                                        crf.addItemGroup(group)

                            elif type(itemGroupDefinition) is dict:
                                igd = itemGroupDefinition
                                if igd["@OID"] in itemGroupOids:

                                    crf = event.getCrf(
                                        igd["OpenClinica:ItemGroupDetails"]["OpenClinica:PresentInForm"]["@FormOID"]
                                    )
                                    
                                    group = ItemGroup()
                                    group.oid = igd["@OID"]
                                    group.name = igd["@Name"]

                                    iRef = igd["ItemRef"]
                                    if type(iRef) is list:
                                        for ir in iRef:
                                            item = Item()
                                            item.oid = ir["@ItemOID"]
                                            group.addItem(item)
                                            itemOids.append(ir["@ItemOID"])
                                    elif type(iRef) is dict:
                                        ir = iRef
                                        item = Item()
                                        item.oid = ir["@ItemOID"]
                                        group.addItem(item)
                                        itemOids.append(ir["@ItemOID"])

                                    crf.addItemGroup(group)

                            # Setup items for CRF in event
                            itemDefinition = r.json()["Study"]["MetaDataVersion"]["ItemDef"]
                            if type(itemDefinition) is list:
                                for itemDef in itemDefinition:
                                    if itemDef["@OID"] in itemOids:
                                        crf = event.getCrf(itemDef["@OpenClinica:FormOIDs"])

                                        item = Item()
                                        item.oid = itemDef["@OID"]
                                        item.name = itemDef["@Name"]
                                        item.dataType = itemDef["@DataType"]
                                        item.description = itemDef["@Comment"]

                                        gr = crf.findGroupForItem(item.oid)
                                        item.itemGroupOid = gr.oid
                                        
                                        crf.items.append(item)

                            elif type(itemDefinition) is dict:
                                itemDef = itemDefinition
                                if itemDef["@OID"] in itemOids:
                                    crf = event.getCrf(itemDef["@OpenClinica:FormOIDs"])

                                    item = Item()
                                    item.oid = itemDef["@OID"]
                                    item.name = itemDef["@Name"]
                                    item.dataType = itemDef["@DataType"]
                                    item.description = itemDef["@Comment"]
                                    
                                    gr = crf.findGroupForItem(item.oid)
                                    item.itemGroupOid = gr.oid

                                    crf.items.append(item)

                            results.append(event)
        if thread:
            thread.emit(QtCore.SIGNAL("finished(QVariant)"), results)
            return None
        else:
            return results