Exemple #1
0
    def addClient(self, clientElement):
        """ Adds a client described in the espML to the database.

        @return It returns the exact same element received, though with the espid
        field populated with the new clients espid.
        """
        try:
            clientNetID = clientElement.getNetid()
            clientDescription = clientElement.getDescription()
            clientType = clientElement.getType()

            ssock = StringIO.StringIO()
            clientElement.export(ssock, 0)

            sqlError = self.sql(
                "INSERT INTO Clients (netid, description, type, xml) VALUES ('%s', '%s', '%s', '%s')"
                %
                (clientNetID, clientDescription, clientType, ssock.getvalue()))

            if sqlError:
                self.rollback()
                logging.error("SQLError: %s" % (sqlError[1]))
                errorElement = espml.error(ttype="sqlerror",
                                           message=sqlError,
                                           number=10)
                return errorElement

            clientEspID = self.insertId()
            sqlError = self.insertLocation('Clients', clientEspID,
                                           clientElement.getLocation())
            if sqlError[0] < 0:
                self.rollback()
                logging.error("SQLError: %s" % (sqlError[1]))
                errorElement = espml.error(ttype="sqlerror",
                                           message=sqlError[1],
                                           number=10)
                return errorElement

            self.commit()

        except Exception:
            #some python error occured. rollback the db
            self.rollback()
            import traceback
            #traceback.print_tb(sys.exc_info()[2])
            traceback.print_exc()
            logging.error("Unexpected Error: %s" % (sys.exc_info()[0]))
            errorElement = espml.error(ttype="unexpected error",
                                       message=sys.exc_info()[0],
                                       number=1)
            return errorElement

        logging.info("Successfully added client EspID: %s, NetID: %s" % (
            clientEspID,
            clientNetID,
        ))

        clientElement.setEspid(str(clientEspID))

        return clientElement
Exemple #2
0
    def addClient(self, clientElement):
        """ Adds a client described in the espML to the database.

        @return It returns the exact same element received, though with the espid
        field populated with the new clients espid.
        """
        try:
            clientNetID = clientElement.getNetid()
            clientDescription = clientElement.getDescription()
            clientType = clientElement.getType()
            
            ssock = StringIO.StringIO()
            clientElement.export(ssock, 0)

            sqlError = self.sql("INSERT INTO Clients (netid, description, type, xml) VALUES ('%s', '%s', '%s', '%s')"%(clientNetID, clientDescription, clientType, ssock.getvalue()))
            
            if sqlError:
                self.rollback()
                logging.error("SQLError: %s"%(sqlError[1]))
                errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                return errorElement

            clientEspID = self.insertId()
            sqlError = self.insertLocation('Clients', clientEspID, clientElement.getLocation())
            if sqlError[0]<0:
                self.rollback()
                logging.error("SQLError: %s"%(sqlError[1]))
                errorElement = espml.error(ttype="sqlerror", message=sqlError[1], number=10)
                return errorElement

            self.commit()
            
        except Exception:
            #some python error occured. rollback the db
            self.rollback()
            import traceback
            #traceback.print_tb(sys.exc_info()[2])
            traceback.print_exc()
            logging.error("Unexpected Error: %s"%(sys.exc_info()[0]))
            errorElement = espml.error(ttype="unexpected error", message=sys.exc_info()[0], number=1)
            return errorElement

        logging.info("Successfully added client EspID: %s, NetID: %s"%(clientEspID, clientNetID,))

        clientElement.setEspid(str(clientEspID))
        
        return clientElement
Exemple #3
0
    def addSystem(self, systemElement):
        """ Adds a system described in the espML to the database. If the system exists already,
        then it will be deleted and added again with the new values. This could be made more
        intelligent in the future where we update only the parts which changed.
        """
        try:
            systemNetID = systemElement.getNetid()
            fields = systemElement.getField()
            
            sqlError = self.sql("SELECT * FROM Systems WHERE netid='%s'"%(systemNetID,))
            
            if sqlError:
                logging.error("SQLError: %s"%(sqlError))
                errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                return errorElement

##             if len(self.fetchAll())>=1:
##                 #system uri already exists
##                 logging.warning("System with URI %s already exists. Delete it for recreation!", systemURI)
##                 sqlError = self.deleteSystem(espml)
##                 if sqlError[0] < 0:
##                     print "\tcould not delete old entry!\n\t%s"%(sqlError[1],)
##                     return (-1, "Entry already exists and could not be deleted!")
##                 #return (-2, "System URI already exists.")

            ssock = StringIO.StringIO()
            systemElement.export(ssock, 0)

            # process access and privacy control
            accessControlId = -1
            accessControlElement = systemElement.getAccesscontrol()
            if accessControlElement:
                (accessControlId, errormsg) = self.insertAccessControl(accessControlElement)
                if accessControlId == -1:
                    self.rollback()
                    errorElement = espml.error(ttype="accessControlError",
                                         message="could not insert access control: %s"%(errormsg,),
                                         number=11)
                    return errorElement

            privacyControlId = -1
            privacyControlElement = systemElement.getPrivacycontrol()
            if privacyControlElement:
                (privacyControlId, errormsg) = self.insertPrivacyControl(privacyControlElement)
                if privacyControlId == -1:
                    self.rollback()
                    errorElement = espml.error(ttype="privacyControlError",
                                         message="could not insert privacy control: %s"%(errormsg,),
                                         number=11)
                    return errorElement
                    

            sqlError = self.sql("INSERT INTO Systems (netid, accesscontrolid, privacycontrolid, xml) VALUES ('%s', %d, %d, '%s')"%(systemNetID, accessControlId, privacyControlId, ssock.getvalue()))
            
            if sqlError:
                self.rollback()
                logging.error("SQLError: %s"%(sqlError[1]))
                errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                return errorElement
            
            systemEspID = self.insertId()
            
            sqlError = self.insertLocation('Systems', systemEspID, systemElement.getLocation())
            if sqlError[0]<0:
                self.rollback()
                logging.error("SQLError: %s"%(sqlError[1]))
                errorElement = espml.error(ttype="sqlerror", message=sqlError[1], number=10)
                return errorElement


            for field in fields:
                ssock = StringIO.StringIO()
                field.export(ssock, 0)
                sqlError = self.sql("INSERT INTO Fields (systemEspID, fieldKey, xml) VALUES (" +\
                                    "%d, '%s', '%s')"%(systemEspID, str(field.getId()), ssock.getvalue()))
                
                if sqlError:
                    self.rollback()
                    logging.error("SQLError: %s"%(sqlError[1]))
                    errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                    return errorElement
                
                fieldId = self.insertId()
                
                sqlError = self.insertLocation('Fields', fieldId, field.getLocation())
                if sqlError[0]<0:
                    self.rollback()
                    logging.error("SQLError: %s"%(sqlError[1]))
                    errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                    return errorElement
                
                for platform in field.getPlatform():
                    ssock = StringIO.StringIO()
                    platform.export(ssock, 0)

                    sqlError = self.sql("INSERT INTO Platforms (fieldId, platformKey, xml) VALUES (" +\
                                         "%d, '%s', '%s')"%(fieldId, str(platform.getId()), ssock.getvalue()))

                    if sqlError:
                        self.rollback()
                        logging.error("SQLError: %s"%(sqlError[1]))
                        errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                        return errorElement
                    platformId = self.insertId()
                    
                    sqlError = self.insertLocation('Platforms', platformId, platform.getLocation())
                    if sqlError[0]<0:
                        self.rollback()
                        logging.error("SQLError: %s"%(sqlError[1]))
                        errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                        return errorElement
                    
                    for sensor in platform.getSensor():
                        ssock = StringIO.StringIO()
                        sensor.export(ssock, 0)

                        sqlError = self.sql("INSERT INTO Sensors (platformId, sensorKey, xml) VALUES (" +\
                                             "%d, '%s', '%s')"%(platformId, str(sensor.getId()), ssock.getvalue()))

                        if sqlError:
                            self.rollback()
                            logging.error("SQLError: %s"%(sqlError[1]))
                            errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                            return errorElement

                        sensorId = self.insertId()

                        sqlError = self.insertLocation('Sensors', sensorId, sensor.getLocation())
                        if sqlError[0]<0:
                            self.rollback()
                            logging.error("SQLError: %s"%(sqlError[1]))
                            errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                            return errorElement


            #associate a mediator. for now, we just choose the nearest mediator we can find.
            systemLocationElement = systemElement.getLocation()
            systemPoint = systemLocationElement.getPoint()
            if not systemPoint:
                self.rollback()
                errorElement = espml.error(ttype="locationerror", message="The system location needs to be a point!", number=1)
            pos = systemPoint[0].getPos().split(',')
            systemLat = float(pos[0])
            systemLong = float(pos[1])

            sqlError = self.sql("SELECT l.referenceId, ABS(p.latitude-%f)+ABS(p.longitude-%f) as dist \
            FROM Points as p, Locations as l, Mediators as m \
            WHERE p.locationId=l.id AND l.referenceId=m.espid ORDER BY dist LIMIT 1"%(systemLat, systemLong))

            if sqlError:
                self.rollback()
                logging.error("SQLError: %s"%(sqlError[1]))
                errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                return errorElement

            sqlResults = self.fetchAll()

            if not sqlResults:
                self.rollback()
                logging.error("No Mediator Error")
                errorElement = espml.error(ttype="mediatorerror", message="Sorry, we couldn't find a mediator for you. Please try again later.", number=1)
                return errorElement
                

            mediatorEspID = sqlResults[0][0]

            sqlError = self.sql("INSERT INTO MediatorSystems (mediatorEspId, systemEspId, description, xml) VALUES (%d, %d, '', '<system><espid>%d</espid><description /></system>')"%(mediatorEspID, systemEspID, systemEspID))

            if sqlError:
                self.rollback()
                logging.error("SQLError: %s"%(sqlError[1]))
                errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                return errorElement

            logging.info("Added mediator %s to system %s"%(mediatorEspID, systemEspID))

            self.commit()
        except Exception:
            #some python error occured. rollback the db
            self.rollback()
            import traceback
            #traceback.print_tb(sys.exc_info()[2])
            traceback.print_exc()
            logging.error("Unexpected Error: %s"%(sys.exc_info()[0]))
            errorElement = espml.error(ttype="unexpected error", message=sys.exc_info()[0], number=1)
            return errorElement

        logging.info("Successfully added system EspID: %s, NetID: %s"%(systemEspID, systemNetID,))
        systemElement.setEspid(str(systemEspID))
        return systemElement
Exemple #4
0
    def listSystems(self, location):
        """ Returns all the systems which are within the polygon described
        in the location parameter. The location parameter is a location xml object.
        """

        for polygon in location.getPolygon():
            polygonList = polygon.getPoslist().strip().split(' ')

        #we only want lat/long
        polygonList = [[float(x) for x in e.strip().split(',')][:2] for e in polygonList]
        latList = [e[0] for e in polygonList]
        longList = [e[1] for e in polygonList]

        #get all the lines and produce the XML for the fields in the systems.
        sqlError = self.sql("SELECT sys.netid, f.id, pl.id, l.id, s.id, p.id, l.referenceId, p.latitude, p.longitude, pl.xml FROM Systems as sys, Fields as f, Platforms as pl, Sensors as s, Locations as l, Points as p WHERE l.referenceTable='Platforms' AND l.id=p.locationId AND pl.id=l.referenceId AND s.platformId=pl.id AND pl.fieldId=f.id AND f.systemEspId=sys.espid AND p.latitude>=%f AND p.latitude<=%f AND p.longitude>=%f AND p.longitude<=%f ORDER BY f.id"%(min(latList), max(latList), min(longList), max(longList)))

        if sqlError:
            logging.error("SQLError: %s"%(sqlError))
            errorElement = error(ttype="sqlerror", message=sqlError, number=10)
            return errorElement


        sqlResults = self.fetchAll()

        points = []

        for point in sqlResults:
            if self._point_inside_polygon(point[7], point[8], polygonList):
                points.append(point)

        queryDoc = xml.dom.minidom.Document()
        queryElement = queryDoc.createElementNS("http://www.w3.org/2001/XMLSchema-instance", "query")
        queryDoc.appendChild(queryElement)
        
        systemHits = []

        first = 1
        activeFieldId = -1
        activeSystemURI = ''
        fieldXml = ''
        #print points
        for point in points:
            #build the response query xml document
            if (point[1] != activeFieldId) or (point[0] != activeSystemURI):
                if not first:
                    #create the sytemXML
                    systemXml = """
 <system>
 <id>"""+str(activeSystemURI)+"""</id>
   """ + fieldXml + """
  </field>
 </system>"""
                    systemHits.append(systemXml)

                first = 0
                sqlError = self.sql("SELECT xml FROM Fields WHERE id=%d"%(point[1]))
                
                if sqlError:
                    logging.error("SQLError: %s"%(sqlError))
                    errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                    return errorElement

                #remove the platforms from the field XML since we want to add onlythe ones which are inside the polygon
                fieldXml = self.filterElements(self.fetchOne()[0], 'platform')
                activeSystemURI = point[0]
                activeFieldId = point[1]
            
            fieldXml = self.addPlatform(fieldXml, point[-1])

        if not first:
            systemXml = """
 <system>
 <id>"""+str(activeSystemURI)+"""</id>
   """ + fieldXml + """
   </field>
 </system>"""
            systemHits.append(systemXml)
                                
        queryXml = """<?xml version="1.0" encoding="UTF-8"?>
<esp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="espml.xsd">
  <response type="systems">
""" + '\n'.join(systemHits) + """
  </response>
  <response type="mediator">

  </response>
</esp>"""

        doc = xml.dom.minidom.parseString(queryXml)
        return doc.toxml()
Exemple #5
0
    def addMediator(self, mediatorElement):
        """ Adds a mediator described in the espML to the database.

        @return It returns the exact same element received, though with the espid
        field populated with the new mediators espid.
        """
        try:
            mediatorNetID = mediatorElement.getNetid()
            mediatorDescription = mediatorElement.getDescription()
            mediatorType = mediatorElement.getType()
            
            ssock = StringIO.StringIO()
            mediatorElement.export(ssock, 0)

            sqlError = self.sql("INSERT INTO Mediators (netid, description, type, xml) VALUES ('%s', '%s', '%s', '%s')"%(mediatorNetID, mediatorDescription, mediatorType, ssock.getvalue()))
            
            if sqlError:
                self.rollback()
                logging.error("SQLError: %s"%(sqlError[1]))
                errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                return errorElement

            mediatorEspID = self.insertId()
            sqlError = self.insertLocation('Mediators', mediatorEspID, mediatorElement.getLocation())
            if sqlError[0]<0:
                self.rollback()
                logging.error("SQLError: %s"%(sqlError[1]))
                errorElement = espml.error(ttype="sqlerror", message=sqlError[1], number=10)
                return errorElement


            for systemElement in mediatorElement.getSystem():
                ssock = StringIO.StringIO()
                systemElement.export(ssock, 0)

                systemEspID = systemElement.getEspid()
                systemDescription = systemElement.getDescription()

                sqlError = self.sql("INSERT INTO MediatorSystems (mediatorEspId, systemEspId, description, xml) VALUES (%d, %d, '%s', '%s')"%(int(mediatorEspID), int(systemEspID), systemDescription, ssock.getvalue()))
            
                if sqlError:
                    self.rollback()
                    logging.error("SQLError: %s"%(sqlError[1]))
                    errorElement = espml.error(ttype="sqlerror", message=sqlError, number=10)
                    return errorElement
                
            self.commit()
            
        except Exception:
            #some python error occured. rollback the db
            self.rollback()
            import traceback
            #traceback.print_tb(sys.exc_info()[2])
            traceback.print_exc()
            logging.error("Unexpected Error: %s"%(sys.exc_info()[0]))
            errorElement = espml.error(ttype="unexpected error", message=sys.exc_info()[0], number=1)
            return errorElement

        logging.info("Successfully added mediator EspID: %s, NetID: %s"%(mediatorEspID, mediatorNetID,))

        mediatorElement.setEspid(str(mediatorEspID))
        
        return mediatorElement
Exemple #6
0
    def listSystems(self, location):
        """ Returns all the systems which are within the polygon described
        in the location parameter. The location parameter is a location xml object.
        """

        for polygon in location.getPolygon():
            polygonList = polygon.getPoslist().strip().split(' ')

        #we only want lat/long
        polygonList = [[float(x) for x in e.strip().split(',')][:2]
                       for e in polygonList]
        latList = [e[0] for e in polygonList]
        longList = [e[1] for e in polygonList]

        #get all the lines and produce the XML for the fields in the systems.
        sqlError = self.sql(
            "SELECT sys.netid, f.id, pl.id, l.id, s.id, p.id, l.referenceId, p.latitude, p.longitude, pl.xml FROM Systems as sys, Fields as f, Platforms as pl, Sensors as s, Locations as l, Points as p WHERE l.referenceTable='Platforms' AND l.id=p.locationId AND pl.id=l.referenceId AND s.platformId=pl.id AND pl.fieldId=f.id AND f.systemEspId=sys.espid AND p.latitude>=%f AND p.latitude<=%f AND p.longitude>=%f AND p.longitude<=%f ORDER BY f.id"
            % (min(latList), max(latList), min(longList), max(longList)))

        if sqlError:
            logging.error("SQLError: %s" % (sqlError))
            errorElement = error(ttype="sqlerror", message=sqlError, number=10)
            return errorElement

        sqlResults = self.fetchAll()

        points = []

        for point in sqlResults:
            if self._point_inside_polygon(point[7], point[8], polygonList):
                points.append(point)

        queryDoc = xml.dom.minidom.Document()
        queryElement = queryDoc.createElementNS(
            "http://www.w3.org/2001/XMLSchema-instance", "query")
        queryDoc.appendChild(queryElement)

        systemHits = []

        first = 1
        activeFieldId = -1
        activeSystemURI = ''
        fieldXml = ''
        #print points
        for point in points:
            #build the response query xml document
            if (point[1] != activeFieldId) or (point[0] != activeSystemURI):
                if not first:
                    #create the sytemXML
                    systemXml = """
 <system>
 <id>""" + str(activeSystemURI) + """</id>
   """ + fieldXml + """
  </field>
 </system>"""
                    systemHits.append(systemXml)

                first = 0
                sqlError = self.sql("SELECT xml FROM Fields WHERE id=%d" %
                                    (point[1]))

                if sqlError:
                    logging.error("SQLError: %s" % (sqlError))
                    errorElement = espml.error(ttype="sqlerror",
                                               message=sqlError,
                                               number=10)
                    return errorElement

                #remove the platforms from the field XML since we want to add onlythe ones which are inside the polygon
                fieldXml = self.filterElements(self.fetchOne()[0], 'platform')
                activeSystemURI = point[0]
                activeFieldId = point[1]

            fieldXml = self.addPlatform(fieldXml, point[-1])

        if not first:
            systemXml = """
 <system>
 <id>""" + str(activeSystemURI) + """</id>
   """ + fieldXml + """
   </field>
 </system>"""
            systemHits.append(systemXml)

        queryXml = """<?xml version="1.0" encoding="UTF-8"?>
<esp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="espml.xsd">
  <response type="systems">
""" + '\n'.join(systemHits) + """
  </response>
  <response type="mediator">

  </response>
</esp>"""

        doc = xml.dom.minidom.parseString(queryXml)
        return doc.toxml()
Exemple #7
0
    def addMediator(self, mediatorElement):
        """ Adds a mediator described in the espML to the database.

        @return It returns the exact same element received, though with the espid
        field populated with the new mediators espid.
        """
        try:
            mediatorNetID = mediatorElement.getNetid()
            mediatorDescription = mediatorElement.getDescription()
            mediatorType = mediatorElement.getType()

            ssock = StringIO.StringIO()
            mediatorElement.export(ssock, 0)

            sqlError = self.sql(
                "INSERT INTO Mediators (netid, description, type, xml) VALUES ('%s', '%s', '%s', '%s')"
                % (mediatorNetID, mediatorDescription, mediatorType,
                   ssock.getvalue()))

            if sqlError:
                self.rollback()
                logging.error("SQLError: %s" % (sqlError[1]))
                errorElement = espml.error(ttype="sqlerror",
                                           message=sqlError,
                                           number=10)
                return errorElement

            mediatorEspID = self.insertId()
            sqlError = self.insertLocation('Mediators', mediatorEspID,
                                           mediatorElement.getLocation())
            if sqlError[0] < 0:
                self.rollback()
                logging.error("SQLError: %s" % (sqlError[1]))
                errorElement = espml.error(ttype="sqlerror",
                                           message=sqlError[1],
                                           number=10)
                return errorElement

            for systemElement in mediatorElement.getSystem():
                ssock = StringIO.StringIO()
                systemElement.export(ssock, 0)

                systemEspID = systemElement.getEspid()
                systemDescription = systemElement.getDescription()

                sqlError = self.sql(
                    "INSERT INTO MediatorSystems (mediatorEspId, systemEspId, description, xml) VALUES (%d, %d, '%s', '%s')"
                    % (int(mediatorEspID), int(systemEspID), systemDescription,
                       ssock.getvalue()))

                if sqlError:
                    self.rollback()
                    logging.error("SQLError: %s" % (sqlError[1]))
                    errorElement = espml.error(ttype="sqlerror",
                                               message=sqlError,
                                               number=10)
                    return errorElement

            self.commit()

        except Exception:
            #some python error occured. rollback the db
            self.rollback()
            import traceback
            #traceback.print_tb(sys.exc_info()[2])
            traceback.print_exc()
            logging.error("Unexpected Error: %s" % (sys.exc_info()[0]))
            errorElement = espml.error(ttype="unexpected error",
                                       message=sys.exc_info()[0],
                                       number=1)
            return errorElement

        logging.info("Successfully added mediator EspID: %s, NetID: %s" % (
            mediatorEspID,
            mediatorNetID,
        ))

        mediatorElement.setEspid(str(mediatorEspID))

        return mediatorElement
Exemple #8
0
    def addSystem(self, systemElement):
        """ Adds a system described in the espML to the database. If the system exists already,
        then it will be deleted and added again with the new values. This could be made more
        intelligent in the future where we update only the parts which changed.
        """
        try:
            systemNetID = systemElement.getNetid()
            fields = systemElement.getField()

            sqlError = self.sql("SELECT * FROM Systems WHERE netid='%s'" %
                                (systemNetID, ))

            if sqlError:
                logging.error("SQLError: %s" % (sqlError))
                errorElement = espml.error(ttype="sqlerror",
                                           message=sqlError,
                                           number=10)
                return errorElement


##             if len(self.fetchAll())>=1:
##                 #system uri already exists
##                 logging.warning("System with URI %s already exists. Delete it for recreation!", systemURI)
##                 sqlError = self.deleteSystem(espml)
##                 if sqlError[0] < 0:
##                     print "\tcould not delete old entry!\n\t%s"%(sqlError[1],)
##                     return (-1, "Entry already exists and could not be deleted!")
##                 #return (-2, "System URI already exists.")

            ssock = StringIO.StringIO()
            systemElement.export(ssock, 0)

            # process access and privacy control
            accessControlId = -1
            accessControlElement = systemElement.getAccesscontrol()
            if accessControlElement:
                (accessControlId,
                 errormsg) = self.insertAccessControl(accessControlElement)
                if accessControlId == -1:
                    self.rollback()
                    errorElement = espml.error(
                        ttype="accessControlError",
                        message="could not insert access control: %s" %
                        (errormsg, ),
                        number=11)
                    return errorElement

            privacyControlId = -1
            privacyControlElement = systemElement.getPrivacycontrol()
            if privacyControlElement:
                (privacyControlId,
                 errormsg) = self.insertPrivacyControl(privacyControlElement)
                if privacyControlId == -1:
                    self.rollback()
                    errorElement = espml.error(
                        ttype="privacyControlError",
                        message="could not insert privacy control: %s" %
                        (errormsg, ),
                        number=11)
                    return errorElement

            sqlError = self.sql(
                "INSERT INTO Systems (netid, accesscontrolid, privacycontrolid, xml) VALUES ('%s', %d, %d, '%s')"
                % (systemNetID, accessControlId, privacyControlId,
                   ssock.getvalue()))

            if sqlError:
                self.rollback()
                logging.error("SQLError: %s" % (sqlError[1]))
                errorElement = espml.error(ttype="sqlerror",
                                           message=sqlError,
                                           number=10)
                return errorElement

            systemEspID = self.insertId()

            sqlError = self.insertLocation('Systems', systemEspID,
                                           systemElement.getLocation())
            if sqlError[0] < 0:
                self.rollback()
                logging.error("SQLError: %s" % (sqlError[1]))
                errorElement = espml.error(ttype="sqlerror",
                                           message=sqlError[1],
                                           number=10)
                return errorElement

            for field in fields:
                ssock = StringIO.StringIO()
                field.export(ssock, 0)
                sqlError = self.sql("INSERT INTO Fields (systemEspID, fieldKey, xml) VALUES (" +\
                                    "%d, '%s', '%s')"%(systemEspID, str(field.getId()), ssock.getvalue()))

                if sqlError:
                    self.rollback()
                    logging.error("SQLError: %s" % (sqlError[1]))
                    errorElement = espml.error(ttype="sqlerror",
                                               message=sqlError,
                                               number=10)
                    return errorElement

                fieldId = self.insertId()

                sqlError = self.insertLocation('Fields', fieldId,
                                               field.getLocation())
                if sqlError[0] < 0:
                    self.rollback()
                    logging.error("SQLError: %s" % (sqlError[1]))
                    errorElement = espml.error(ttype="sqlerror",
                                               message=sqlError,
                                               number=10)
                    return errorElement

                for platform in field.getPlatform():
                    ssock = StringIO.StringIO()
                    platform.export(ssock, 0)

                    sqlError = self.sql("INSERT INTO Platforms (fieldId, platformKey, xml) VALUES (" +\
                                         "%d, '%s', '%s')"%(fieldId, str(platform.getId()), ssock.getvalue()))

                    if sqlError:
                        self.rollback()
                        logging.error("SQLError: %s" % (sqlError[1]))
                        errorElement = espml.error(ttype="sqlerror",
                                                   message=sqlError,
                                                   number=10)
                        return errorElement
                    platformId = self.insertId()

                    sqlError = self.insertLocation('Platforms', platformId,
                                                   platform.getLocation())
                    if sqlError[0] < 0:
                        self.rollback()
                        logging.error("SQLError: %s" % (sqlError[1]))
                        errorElement = espml.error(ttype="sqlerror",
                                                   message=sqlError,
                                                   number=10)
                        return errorElement

                    for sensor in platform.getSensor():
                        ssock = StringIO.StringIO()
                        sensor.export(ssock, 0)

                        sqlError = self.sql("INSERT INTO Sensors (platformId, sensorKey, xml) VALUES (" +\
                                             "%d, '%s', '%s')"%(platformId, str(sensor.getId()), ssock.getvalue()))

                        if sqlError:
                            self.rollback()
                            logging.error("SQLError: %s" % (sqlError[1]))
                            errorElement = espml.error(ttype="sqlerror",
                                                       message=sqlError,
                                                       number=10)
                            return errorElement

                        sensorId = self.insertId()

                        sqlError = self.insertLocation('Sensors', sensorId,
                                                       sensor.getLocation())
                        if sqlError[0] < 0:
                            self.rollback()
                            logging.error("SQLError: %s" % (sqlError[1]))
                            errorElement = espml.error(ttype="sqlerror",
                                                       message=sqlError,
                                                       number=10)
                            return errorElement

            #associate a mediator. for now, we just choose the nearest mediator we can find.
            systemLocationElement = systemElement.getLocation()
            systemPoint = systemLocationElement.getPoint()
            if not systemPoint:
                self.rollback()
                errorElement = espml.error(
                    ttype="locationerror",
                    message="The system location needs to be a point!",
                    number=1)
            pos = systemPoint[0].getPos().split(',')
            systemLat = float(pos[0])
            systemLong = float(pos[1])

            sqlError = self.sql(
                "SELECT l.referenceId, ABS(p.latitude-%f)+ABS(p.longitude-%f) as dist \
            FROM Points as p, Locations as l, Mediators as m \
            WHERE p.locationId=l.id AND l.referenceId=m.espid ORDER BY dist LIMIT 1"
                % (systemLat, systemLong))

            if sqlError:
                self.rollback()
                logging.error("SQLError: %s" % (sqlError[1]))
                errorElement = espml.error(ttype="sqlerror",
                                           message=sqlError,
                                           number=10)
                return errorElement

            sqlResults = self.fetchAll()

            if not sqlResults:
                self.rollback()
                logging.error("No Mediator Error")
                errorElement = espml.error(
                    ttype="mediatorerror",
                    message=
                    "Sorry, we couldn't find a mediator for you. Please try again later.",
                    number=1)
                return errorElement

            mediatorEspID = sqlResults[0][0]

            sqlError = self.sql(
                "INSERT INTO MediatorSystems (mediatorEspId, systemEspId, description, xml) VALUES (%d, %d, '', '<system><espid>%d</espid><description /></system>')"
                % (mediatorEspID, systemEspID, systemEspID))

            if sqlError:
                self.rollback()
                logging.error("SQLError: %s" % (sqlError[1]))
                errorElement = espml.error(ttype="sqlerror",
                                           message=sqlError,
                                           number=10)
                return errorElement

            logging.info("Added mediator %s to system %s" %
                         (mediatorEspID, systemEspID))

            self.commit()
        except Exception:
            #some python error occured. rollback the db
            self.rollback()
            import traceback
            #traceback.print_tb(sys.exc_info()[2])
            traceback.print_exc()
            logging.error("Unexpected Error: %s" % (sys.exc_info()[0]))
            errorElement = espml.error(ttype="unexpected error",
                                       message=sys.exc_info()[0],
                                       number=1)
            return errorElement

        logging.info("Successfully added system EspID: %s, NetID: %s" % (
            systemEspID,
            systemNetID,
        ))
        systemElement.setEspid(str(systemEspID))
        return systemElement