def processURIIOCondensedTypeQuery(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    nType = kd.typeManager.getType(root.text)
    print(root.text)
    if nType is None:
        return "<failed>Could not find type " + root.text + " in this Knowledge Domain</failed>"
    cType = nType.condense()
    xmlStr = "<condensedType><name>" + cType.type + "</name>"
    strlocked = "no"
    if nType.locked:
        strlocked = "yes"
    strsysdef = "no"
    if nType.systemDefined:
        strsysdef = "yes"
    xmlStr += "<locked>" + strlocked + "</locked>"
    xmlStr += "<systemDefined>" + strsysdef + "</systemDefined>"
    xmlStr += "<parent>"
    if nType.parent is not None:
         xmlStr += nType.parent.type
    xmlStr += "</parent>"
    xmlStr += "<superTypes>"
    for st in cType.supertypes:
        xmlStr += "<superType>" + st + "</superType>"
    xmlStr += "</superTypes>"
    xmlStr += "<properties>"
    for ap in cType.properties:
        xmlStr += "<property>" + ap.name + "<type>" + ap.type + "</type></property>"
    xmlStr += "</properties>"
    xmlStr += "</condensedType>"

    return xmlStr
def processURIIOTypeDelete(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    if kd.typeManager.getType(root.text) is None:
        return "<failed>type " + root.text + " does not exist in the type dictionary.</failed>"
    kd.typeManager.delete(root.text)
    return kd.typeManager.getCriteria().asXML()
def processPredicateDefinitionQuery(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    criteria = kd.predicateDefinitionManager.getCriteria()
    for child in root:
        if child.tag == "name":
            criteria.name = child.text
        elif child.tag == "reflective":
            if child.text == "true":
                criteria.reflective = True
            else:
                criteria.reflective = False
        elif child.tag == "transitive":
            if child.text == "true":
                criteria.transitive = True
            else:
                criteria.transitive = False
        elif child.tag == "subject":
            if kd.typeManager.getType(child.text) is not None:
                criteria.subject = kd.typeManager.getType(child.text)
        elif child.tag == "object":
            if kd.typeManager.getType(child.text) is not None:
                criteria.object = kd.typeManager.getType(child.text)
    criteria.resolve()
    return criteria.asXML()
def processURIIOTypeSave(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    for child in root:
        if child.tag == "type":
            nType = URIIOType(None, "")
            for ch in child:
                if ch.tag == "name":
                    nType.type = ch.text
                elif ch.tag == "property":
                    prop = URIIOTypeProperty(ch.text)
                    for c in ch:
                        if c.tag == "type":
                            prop.type = c.text
                    nType.properties.append(prop)
                elif ch.tag == "parent":
                    p = kd.typeManager.getType(ch.text)
                    if p is not None:
                        nType.parent = p
                elif ch.tag == "locked":
                    if ch.text == "yes":
                        nType.locked = True
                    else:
                        nType.locked = False
            kd.typeManager.saveType(nType)

    return kd.typeManager.getCriteria().asXML()
def processRemovePredicate(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    suburi = ""
    objuri = ""
    predname = ""
    for child in root:
        if child.tag == "subject":
            suburi = child.text
        elif child.tag == "object":
            objuri = child.text
        elif child.tag == "predicate":
            predname = child.text
    sub = kd.URIIOManager.getURIIO(suburi)
    if sub is None:
        return "<failed>Subject with URI " + suburi + "is not known in this Knowledge Domain</failed>"
    obj = kd.URIIOManager.getURIIO(objuri)
    if obj is None:
        return "<failed>Subject with URI " + objuri + "is not known in this Knowledge Domain</failed>"
    pred = kd.predicateDefinitionManager.getPredicate(predname)
    if pred is None:
        return "<failed>Predicate with name " + predname + " is not known in this Knowledge Domain</failed>"
    kd.predicateManager.removePredicate(sub, pred, obj)

    return "<success></success>"
def processURIIODelete(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    for child in root:
        if child.tag == "URI":
            kd.URIIOManager.removeURIIO(child.text)
    return "<success></sucess>"
def processURIIOTypeQuery(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    criteria = kd.typeManager.getCriteria()

    """
    built the criteria object
    """
    for child in root:
        if child.tag == "name":
            ctp = kd.typeManager.getType(child.text)
            if ctp is not None:
                criteria.type = ctp
            for ch in child:
                if ch.tag == "exactType":
                    if ch.text == "yes":
                        criteria.exactType = True

        if child.tag == "properties":
            for aptag in child:
                if aptag.tag == "property":
                    criteria.atomicProperties.append(aptag.text)

    criteria.resolve()

    return criteria.asXML()
def processURIIOQuery(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    criteria = kd.URIIOManager.getCriteria()

    for child in root:
        if child.tag == "type":
            criteria.TypeRestrictions.append(child.text)
        elif child.tag == "property":
            name = ""
            value = ""
            for propdef in child:
                if propdef.tag == "name":
                    name = propdef.text
                elif propdef.tag == "value":
                    value = propdef.text
            criteria.addPropertyRestriction(name, value)
        elif child.tag == "predicate":
            pred = ""
            obj = ""
            for preddef in child:
                if preddef.tag == "name":
                    pred = preddef.text
                elif preddef.tag == "object":
                    obj = preddef.text
            criteria.addPredicateRestrition(pred, obj)
        elif child.tag == "uriio":
            criteria.addURIRestriction(child.text)

    criteria.resolve()

    return criteria.asXML()
def processPredicateDefinitionDelete(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    for child in root:
        if child.tag == "name":
            kd.predicateDefinitionManager.deletePredicate(child.text)
            return "<success></success>"
    return "<failed>You have to include a name tag to your query.</failed>"
def addDataToKnowledgeInstance(root):
    kdmanager = KnowledgeDomainManager()
    domain = kdmanager.getDomain()
    usrmanager = UserManager()
    userBox = usrmanager.getUserBoxForUser("jonathan")

    domain.dataSourceManager.addToKnowledgeInstance(userBox, None, domain)
    return "<success></success>"
def processURIIOBaseTypeQuery(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    nType = kd.typeManager.getType("type")
    xmlStr = "<baseType>"
    xmlStr += "<name>" + nType.type + "</name>"
    xmlStr += "</baseType>"
    return xmlStr
def processURIIOSubTypesQuery(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    nType = kd.typeManager.getType(root.text)
    xmlStr = "<subTypes>"
    for child in nType.children:
        xmlStr += "<subType>" + child.type + "</subType>"
    xmlStr += "</subTypes"
    return xmlStr
def processCardinalityForURIIO(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    uri = ""
    for child in root:
        if child.tag == "uri":
            uri = child.text
    uriio = kd.URIIOManager.getURIIO(uri)
    if uriio is not None:
        return "<cardinality><uri>" + uri + "</uri><cardinality>" + str(kd.predicateManager.getCardinality(uriio)) + "</cardinality></cardinality>"
def processURIIOAddType(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    uri = ""
    type = ""
    for child in root:
        if child.tag == "uri":
            uri = child.text
        elif child.tag == "type":
            type = child.text
    uriio = kd.URIIOManager.getURIIO(uri)
    uriio.type.append(kd.typeManager.getType(type))
    return "<success></sucess>"
def processURIIORemoveProperty(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    uri = ""
    propname = ""
    for child in root:
        if child.tag == "uri":
            uri = child.text
        elif child.tag == "property":
            for propdef in child:
                if propdef.tag == "name":
                    propname = propdef.text
    uriio = kd.URIIOManager.getURIIO(uri)
    uriio.removeProperty(propname)
    return "<success></sucess>"
def processPredicateURIQuery(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    uri = ""
    for child in root:
        if child.tag == "uri":
            uri = child.text
    uriio = kd.URIIOManager.getURIIO(uri)
    if uriio is None:
        return "<failed>The URIIO with URI '" + uri + "' does not exist in this Knowledge Domain.</failed>"
    list = kd.predicateManager.getPredicatesForURIIO(uriio)
    output = "<predicates>"
    for p in list:
        output += "<predicate>"
        output += "<subject>" + p.subject.type + "</subject>"
        output += "<predicate>" + p.predicate.name + "</predicate>"
        output += "<object>" + p.object.type + "</object>"
        output += "</predicate>"
    output += "</predicates>"
    return output
def processPredicateQuery(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    predname = ""
    for child in root:
        if child.tag == "predicate":
            predname = child.text
    preddef = kd.predicateDefinitionManager.getPredicate(predname)
    if preddef is None:
        return "<failed>The predicate with name '" + predname + "' does not exist in this Knowledge Domain.</failed>"
    list = kd.predicateManager.getPredicatesForPredicateDefinition(preddef)
    output = "<predicates>"
    for p in list:
        output += "<predicate>"
        output += "<subject>" + p.subject.type + "</subject>"
        output += "<predicate>" + p.predicate.name + "</predicate>"
        output += "<object>" + p.object.type + "</object>"
        output += "</predicate>"
    output += "</predicates>"
    return output
def processSaveDataSource(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    for dstype in root:
        if dstype.tag == "XLSXSource":
            src = DataSourceXLSX("")
            for ch in dstype:
                if ch.tag == "file":
                    src.sourceFile = ch.text
                elif ch.tag == "template":
                    kdmanager = KnowledgeDomainManager()
                    kd = kdmanager.getDomain()
                    tpt = kd.templateManager.getTemplate(ch.text)
                    if tpt is not None:
                        src.template = tpt
            kd.dataSourceManager.addDataSource(src)
        if dstype.tag == "SQLiteSource":
            src = DataSourceSQLite("")
            for ch in dstype:
                if ch.tag == "file":
                    src.sourceFile = ch.text
                elif ch.tag == "template":
                    kdmanager = KnowledgeDomainManager()
                    kd = kdmanager.getDomain()
                    tpt = kd.templateManager.getTemplate(ch.text)
                    if tpt is not None:
                        src.template = tpt
            kd.dataSourceManager.addDataSource(src)

    return "<success></success>"
def processPredicateDefinition(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    name = ""
    description = ""
    reflective = False
    transitive = False
    subject = None
    object = None
    for child in root:
        if child.tag == "name":
            name = child.text
        elif child.tag == "description":
            description = child.text
        elif child.tag == "reflective":
            if child.text == "true":
                reflective = True
        elif child.tag == "transitive":
            if child.text == "true":
                transitive = True
        elif child.tag == "subject":
            if kd.typeManager.getType(child.text) is not None:
                subject = child.text
        elif child.tag == "object":
            if kd.typeManager.getType(child.text) is not None:
                object = child.text

    if object == "":
        return "<failed>The type <" + object + "> is not a known type in this knowledge domain</failed>"
    if subject == "":
        return "<failed>The type <" + subject + "> is not a known type in this knowledge domain</failed>"
    if name == "":
         return "<failed>No name passed!</failed>"

    kd.predicateDefinitionManager.addPredicate(name, description, reflective, transitive, subject, object)
    return "<success></success>"
def processSaveTemplate(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    for ttype in root:
        if ttype.tag == "XLSXTemplate":
            t = TemplateXLSX()
            for objtempl in ttype:
                odef = XLSXObjectTemplate("type")
                for obchild in objtempl:
                    if obchild.tag == "type":
                       odef.type = obchild.text
                    elif obchild.tag == "property":
                        prop = XLSXObjectPropertyTemplate(0,0,0,0)
                        for p in obchild:
                            if p.tag == "x":
                                prop.x = int(p.text)
                            elif p.tag == "y":
                                prop.y = int(p.text)
                            elif p.tag == "namex":
                                prop.namex = int(p.text)
                            elif p.tag == "namey":
                                prop.namey = int(p.text)
                            elif p.tag == "name":
                                prop.name = p.text
                        odef.properties.append(prop)
                    elif obchild.tag == "predicateConnector":
                        conn = XLSXObjectConnectorTemplate(0,0,"type", "", "has relation to", True)
                        for c in obchild:
                            if c.tag == "connectorField":
                                for cc in c:
                                    if cc.tag == "x":
                                        conn.x = int(cc.text)
                                    elif cc.tag == "y":
                                        conn.y = int(cc.text)
                            elif c.tag == "connectsToType":
                                conn.type = c.text
                            elif c.tag == "connectsOnProperty":
                                conn.property = c.text
                            elif c.tag == "connectorPredicate":
                                conn.predicate = c.text
                            elif c.tag == "subject":
                                sub = True
                                if c.text == "no":
                                    sub = False
                                conn.subject = sub
                        odef.predicateConnectors.append(conn)
                t.objectTemplates.append(odef)
            kd.templateManager.setTemplate(t)
        elif ttype.tag == "SQLiteTemplate":
            t = TemplateSQLite()
            for tables in ttype:
                if tables.tag == "table":
                    table = SQLTable("")
                    for td in tables:
                        if td.tag == "name":
                            table.name = td.text
                        elif td.tag == "type":
                            table.type = td.text
                        elif td.tag == "columns":
                            for cd in td:
                                if cd.tag == "column":
                                    column = SQLColumn("", 0)
                                    for cdd in cd:
                                        if cdd.tag == "name":
                                            column.name = cdd.text
                                        elif cdd.tag == "number":
                                            column.number = int(cdd.text)
                                    table.columns.append(column)
                    t.tables.append(table)
            kd.templateManager.setTemplate(t)


    return "<success></success>"
def processURIIOTypePurge(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    kd.typeManager.purgeDictionary()
    return kd.typeManager.getCriteria().asXML()
def processURIIONew(root):
    kdmanager = KnowledgeDomainManager()
    kd = kdmanager.getDomain()
    uriio = kd.URIIOManager.newURIIO()
    return uriio.asXML()