Forked from Miguel000/Neo4jRelationshipsUniqueId.py
Last active
June 14, 2016 11:08
-
-
Save ffr4nz/147d7590d9d8d8867937bd2a96771692 to your computer and use it in GitHub Desktop.
Neo4jDynamicRelationUniqueId
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
The MIT License (MIT) | |
Copyright (c) 2016 sinfonier-project | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
""" | |
import basesinfonierdrain | |
import requests | |
import json | |
import base64 | |
import urllib | |
import traceback | |
class Neo4jDynamicRelationUniqueId(basesinfonierdrain.BaseSinfonierDrain): | |
def __init__(self): | |
basesinfonierdrain.BaseSinfonierDrain().__init__() | |
def userprepare(self): | |
# Get Param (get value of "param_name" from input box) | |
self.url = self.getParam("urlDatabase") | |
self.index = self.getParam("index") | |
self.param = self.getParam("param") | |
self.key = self.getParam("key") | |
self.relationListField = self.getParam("relationListField") | |
def userprocess(self): | |
try: | |
auth = base64.b64encode(self.param) | |
headers = {"Accept" : "application/json; charset=UTF-8","Content-Type": "application/json", "Authorization" : "Basic " + auth} | |
endUrlnode = "index/node/"+self.index+"?uniqueness=get_or_create" | |
self.log(str(self.config)) | |
relationshipList = self.getField(self.relationListField) | |
try: | |
if type(relationshipList) is list: | |
for i in range(len(relationshipList)): | |
idStart = "" | |
idEnd = "" | |
typeRelation = "" | |
crearRelation=True | |
item = relationshipList[i] | |
try: | |
typeRelation = str(item['rtype']) | |
idStart = str(self.getField(item['src'])) | |
idEnd = str(self.getField(item['dst'])) | |
endUrlnode = "index/node/"+self.index+"?uniqueness=get_or_create" | |
finalUrl = self.url + endUrlnode | |
payload = '{"key" : "'+self.key+'" , "value" : "'+idStart+'" }' | |
response = requests.post(finalUrl, headers=headers, data=payload) | |
self.log("DEFAULT ENTITY: " + str(response.json())) | |
idnodestart = str(response.json().get('metadata').get('id')) | |
payload = '{"key" : "'+self.key+'" , "value" : "'+idEnd+'" }' | |
response = requests.post(finalUrl, headers=headers, data=payload) | |
self.log("EXIST 2 ENTITY: " + str(response.json())) | |
idnodeend = str(response.json().get('metadata').get('id')) | |
urlrelations = str(response.json().get('incoming_relationships')) | |
response = requests.get(urlrelations, headers=headers) | |
self.log("EXIST RELATION: " + str(response.json())) | |
if response.text == "[ ]": | |
self.log("CREO RELACION") | |
crearRelation = True | |
else: | |
self.log("ENTRO SI EXISTE RELACIONES") | |
for j in response.json(): | |
self.log(str(j)) | |
existrelation = str(j['start']) | |
idstart = existrelation.split("/") | |
self.log(str(idstart)) | |
idfin = idstart[-1] | |
if idfin == idnodestart: | |
if j['metadata']['type'] == typeRelation: | |
self.log("TODO ES IGUAL, RELACION EXISTENTE") | |
crearRelation = False | |
if crearRelation: | |
self.log("ENTRO, nodo inicial: ") | |
endUrlnode = "node/"+idnodestart+"/relationships" | |
finalUrlrelation = self.url + endUrlnode | |
payload = '{"to": "'+idnodeend+'" , "type" : "'+typeRelation+'"}' | |
response = requests.post(finalUrlrelation, headers=headers, data=payload) | |
self.log("CREATE RELATION: " + str(response.json())) | |
except Exception, ex: | |
self.log("Una Entidad no tiene ningun valor : " + str(ex)) | |
self.log(str(traceback.format_exc())) | |
pass | |
except Exception, ex: | |
self.log("ERROR antes de empezar el bucle : t" + str(ex)) | |
except Exception, ex: | |
self.log("Error al inicio : "+str(ex)) | |
Neo4jDynamicRelationUniqueId().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment