0
|
1 from OFS.SimpleItem import SimpleItem
|
|
2 from Globals import package_home
|
|
3 import json
|
|
4 import SrvTxtUtils
|
|
5 import urllib
|
|
6 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
|
|
7 import os
|
|
8 import os.path
|
|
9 import logging
|
|
10
|
3
|
11 from lxml import etree
|
|
12
|
2
|
13 from BTrees.OOBTree import OOBTree
|
|
14
|
0
|
15 class JSONClient(SimpleItem):
|
2
|
16
|
0
|
17 meta_type="JSONClient"
|
|
18
|
1
|
19 manage_options=SimpleItem.manage_options+(
|
|
20 {'label':'Main Config','action':'changeJSONClientConfigPT'},
|
|
21 )
|
|
22
|
|
23
|
|
24
|
|
25 _v_jsonCache={}
|
|
26
|
0
|
27 def __init__(self,id,url):
|
|
28 """init"""
|
|
29 self.id=id
|
|
30 self.url=url
|
|
31
|
1
|
32 def changeJSONClientConfigPT(self):
|
|
33 """change form"""
|
|
34 pt=zptFile(self, 'zpt/changeJSONClientForm.zpt')
|
|
35 pt.content_type="text/html"
|
|
36 return pt()
|
|
37
|
|
38
|
|
39 def changeJSONClientConfig(self,url,RESPONSE=None):
|
2
|
40 """cahnge the JSON config"""
|
1
|
41 self.url=url
|
|
42 if RESPONSE is not None:
|
|
43 RESPONSE.redirect('manage_main')
|
|
44
|
|
45
|
|
46 def getEntityLink(self,entId):
|
3
|
47 return "https://openmind-ismi-dev.mpiwg-berlin.mpg.de/om4-ismi/browse/entityDetails.xhtml?eid="+str(entId)
|
1
|
48
|
|
49 def json(self,method,params={},cache=True):
|
0
|
50 """json aufruf"""
|
|
51
|
1
|
52
|
0
|
53 paramString=urllib.urlencode(params)
|
|
54 callUrl = self.url%(method,paramString)
|
2
|
55 logging.debug(callUrl)
|
1
|
56 if (cache is True) and (self._v_jsonCache.get(callUrl,None)!=None):
|
|
57 return self._v_jsonCache.get(callUrl)
|
|
58 else:
|
|
59
|
|
60 txt=SrvTxtUtils.getHttpData(callUrl)
|
|
61 logging.debug(txt)
|
|
62 obj= json.loads(txt)
|
|
63
|
|
64 self._v_jsonCache[callUrl]=obj
|
0
|
65 return obj
|
|
66
|
|
67
|
|
68 def mapEntityAttributesToData(self,entity,type):
|
3
|
69 logging.debug(entity)
|
|
70 attrs=entity.get('atts')
|
0
|
71 attrsHash={}
|
|
72 for attr in attrs:
|
3
|
73 attrsHash[attr.get('name')]=attr.get('ov')
|
0
|
74 attrsHash['@type']=type
|
|
75 return attrsHash
|
|
76
|
|
77 def getOV(self,entId):
|
2
|
78 if (entId)<0: #-1 falls keine Entity gefunden wurde
|
|
79 return""
|
3
|
80 obj=self.json("get_ent", {'id':entId})
|
|
81 return obj.get('ent').get('ov')
|
0
|
82
|
3
|
83 def getSingleRelationFromEntity(self,jsonHash,type="src_Rrels",relName=None,idType="src_id"):
|
2
|
84 ret = self.getRelationFromEntity(jsonHash,type,relName,maxNum=1)
|
|
85 if len(ret)==0:
|
|
86 return -1
|
|
87 else:
|
|
88 return ret[0].get(idType)
|
|
89
|
3
|
90 def getRelationFromEntity(self,jsonHash,type="src_rels",relName=None,maxNum=3000):
|
|
91 hash=jsonHash.get("ent")
|
0
|
92 logging.debug(type)
|
|
93 logging.debug(".................")
|
|
94 logging.debug(hash)
|
|
95 rels = hash.get(type,None)
|
|
96 logging.debug(".................")
|
|
97 logging.debug(rels)
|
|
98
|
|
99 if (rels is None) or (relName is None):
|
|
100 logging.debug("return")
|
|
101 return rels
|
|
102 ret=[]
|
|
103
|
|
104
|
|
105 counter=0
|
|
106 for rel in rels:
|
|
107 if rel.get("name","")==relName:
|
|
108 counter+=1
|
|
109 ret.append(rel)
|
|
110 if counter > maxNum:
|
|
111 break
|
|
112
|
|
113 logging.debug("++++++++++++++++++++++++++++++")
|
|
114 logging.debug(rels)
|
|
115 return ret
|
1
|
116
|
|
117 def getPubmanOutput(self,url="http://pubman.mpiwg-berlin.mpg.de/search/SearchAndExport?cqlQuery=escidoc.context.objid=escidoc:79281&exportFormat=APA&outputFormat=html_linked&sortKeys=&sortOrder=ascending&startRecord=1&maximumRecords=50",cache=True):
|
|
118 """getPubmanOutput"""
|
|
119
|
|
120 callUrl = url
|
0
|
121
|
1
|
122 if (cache is True) and (self._v_jsonCache.get(callUrl,None)!=None):
|
|
123 return self._v_jsonCache.get(callUrl)
|
|
124 else:
|
|
125
|
|
126 txt=SrvTxtUtils.getHttpData(callUrl)
|
|
127 logging.debug(txt)
|
|
128 #obj= json.loads(txt)
|
|
129
|
|
130 self._v_jsonCache[callUrl]=txt
|
|
131 return txt
|
|
132
|
|
133 def activateUrl(self,value):
|
|
134 #wenn ein wert eine url ist, dass mache daraus einen entsprechende a link.
|
|
135 if value.startswith("http://") or value.startswith("https://"):
|
|
136 return """<a class="activatedLink" href="%s">%s</a>"""%(value,value)
|
|
137 else:
|
|
138 return value
|
|
139
|
|
140
|
|
141 def getISMIIDfromBiographie(self,name):
|
|
142 #nimmt den Filenamen und gibt die ISMI Personen ID zurueck diese liegt auf dem Server in einer Datei bio2ismi.txt
|
|
143 data = str(getattr(self.biographies,'bio2ismi.txt'))
|
|
144 logging.debug(data)
|
|
145 for line in data.split("\n"):
|
|
146 splitted= line.split("\t")
|
|
147 filename=splitted[1].replace(r"\w","_")
|
|
148 filename=filename.replace(r"%\d+","_")
|
|
149 logging.debug(line)
|
|
150 if (len(splitted)> 1) and (name in filename):
|
|
151 return splitted[0]
|
|
152 return ""
|
|
153
|
|
154 def collectURL(self,url,name,REQUEST):
|
|
155 history=REQUEST.SESSION.get('history',None);
|
|
156 if history is None:
|
|
157 history=[]
|
|
158
|
|
159
|
|
160 if len(history)>0:#wenn der letzte Eintrag gleich dem jetzigen ist, dann nicht eintragen
|
|
161 urlStored,nameStored=history[-1]
|
|
162 if urlStored==url:
|
|
163 return self.createBreadcrumb(REQUEST);
|
|
164
|
|
165
|
|
166 history.append((url,name))
|
|
167 REQUEST.SESSION.set('history',history);
|
|
168 return self.createBreadcrumb(REQUEST);
|
|
169
|
|
170 def collectTriple(self,s,p,o,inverse=False,REQUEST=None,RESPONSE=None):
|
|
171 """collect a triple and redirect"""
|
|
172
|
|
173 history=REQUEST.SESSION.get('triple',None);
|
|
174 if history is None:
|
|
175 history=[]
|
|
176
|
|
177
|
|
178
|
|
179 if not (s,p,o) in history:
|
|
180
|
|
181 history.append((s,p,o))
|
|
182 REQUEST.SESSION.set('triple',history);
|
|
183
|
|
184 if inverse:
|
|
185 RESPONSE.redirect(s)
|
|
186 else:
|
|
187 RESPONSE.redirect(o)
|
|
188
|
|
189 def cleanHistoryAndTriple(self,REQUEST):
|
|
190 """clean history"""
|
|
191 REQUEST.SESSION.set('triple',[]);
|
|
192 REQUEST.SESSION.set('history',[]);
|
|
193 REQUEST.response.redirect(REQUEST['HTTP_REFERER'])
|
|
194
|
|
195 def getTripleAsRDF(self,REQUEST):
|
|
196 """show the triple as RDF"""
|
|
197 ret="""<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ismi="http://ismi.mpiwg-berlin.mpg.de/ns/"> """
|
|
198
|
|
199 rdfString="""<rdf:Description rdf:about="%s">
|
|
200 <ismi:%s rdf:resource="%s"/>
|
|
201 <ismi:has_ownValue>%s</ismi:has_ownValue>
|
|
202 </rdf:Description>"""
|
|
203
|
|
204 ownValueString="""<rdf:Description rdf:about="%s">
|
|
205 <ismi:has_ownValue>%s</ismi:has_ownValue>
|
|
206 </rdf:Description>"""
|
|
207
|
|
208 history=REQUEST.SESSION.get('triple',None);
|
|
209 if history is not None:
|
|
210 for triple in history:
|
|
211 s,p,o=triple
|
|
212 ownValue=self.getOV(s.split("=")[1])
|
|
213 ret+=rdfString%(s,p,o,ownValue)
|
|
214 ret+=ownValueString%(o,self.getOV(o.split("=")[1]))
|
|
215
|
|
216
|
|
217 ret+="</rdf:RDF>"
|
|
218
|
|
219 urlBase=REQUEST['URL0']
|
|
220 ret=ret.replace("../",urlBase+"ISMI/database/")
|
|
221 REQUEST.response.setHeader("Content-type", "text/rdf")
|
|
222 return ret
|
|
223 def createBreadcrumb(self,REQUEST):
|
|
224 history=REQUEST.SESSION.get('history',None);
|
|
225 if history is None:
|
|
226 return ""
|
|
227
|
|
228 ret=[]
|
|
229 for entry in history:
|
|
230 ret.append("""<a href="%s">%s</a>"""%entry)
|
|
231 return "-->".join(ret)
|
2
|
232
|
|
233 def filter(self,list,filterValue,letter,cache=True,cacheName=None):
|
|
234 """filters a list of jsonObjects, gives only elements where the filterValue, begins with letter."""
|
|
235
|
|
236 if cache and cacheName and (self._v_jsonCache.get(cacheName,None)!=None):
|
|
237 hash = self._v_jsonCache.get(cacheName).get(filterValue,None)
|
|
238 if hash is not None:
|
|
239 return hash.get(letter,[])
|
|
240
|
|
241
|
|
242 caches = self._v_jsonCache.get(cacheName,None)
|
|
243 if caches is None:
|
|
244 caches= OOBTree()
|
|
245
|
|
246 hash=caches.get(filterValue,None)
|
|
247 if hash is None:
|
|
248 hash=OOBTree()
|
|
249
|
|
250
|
|
251 for object in list:
|
|
252 sortValue = object.get(filterValue).lower()
|
|
253 if len(sortValue)>0:
|
|
254 val = sortValue[0]
|
|
255 if val=="[" and len(sortValue)>1:
|
|
256 val = sortValue[1]
|
|
257 else:
|
|
258 val=""
|
|
259
|
|
260 valList=hash.get(val,[])
|
|
261 valList.append(object)
|
|
262 hash.update({val:valList})
|
|
263
|
|
264 caches.update({filterValue:hash})
|
|
265
|
|
266 self._v_jsonCache.update({cacheName:caches})
|
|
267
|
|
268 return hash.get(letter,[]), hash.keys()
|
3
|
269
|
|
270
|
|
271 def xsltTransform(self,path=None,pathXML=None,pathXSL=None,entID=None):
|
|
272 """xlst"""
|
|
273 if path is not None:
|
|
274 pathXML=path+".xml"
|
|
275 pathXSL=path+".xsl"
|
|
276
|
|
277
|
|
278 logging.debug(pathXML)
|
|
279 logging.debug(pathXSL)
|
|
280
|
|
281 url=self.REQUEST['URL2']
|
|
282 xmlURL=url+pathXML
|
|
283 xslURL=url+pathXSL
|
|
284
|
|
285
|
|
286
|
|
287 logging.debug(xmlURL)
|
|
288 logging.debug(xslURL)
|
|
289
|
|
290
|
|
291
|
|
292 f= urllib.urlopen(xslURL);
|
|
293 txt= f.read()
|
|
294 xslt_root=etree.fromstring(txt)
|
|
295 #xslt_root= etree.parse(xslURL) #verstehe nicht warum das nicht geht??
|
|
296
|
|
297 f= urllib.urlopen(xmlURL+"?id="+entID);
|
|
298 txt= f.read()
|
|
299 xml=etree.fromstring(txt)
|
|
300 #xml= etree.parse(xmlURL+"?id="+entID)
|
|
301
|
|
302 transform=etree.XSLT(xslt_root)
|
|
303
|
|
304
|
|
305 result =transform(xml)
|
|
306 return unicode(result)
|
|
307
|
1
|
308
|
0
|
309 def manage_addJSONClient(self,id,url,RESPONSE=None):
|
|
310 """add a json client"""
|
|
311
|
|
312
|
|
313 newObj=JSONClient(id,url)
|
|
314 self._setObject(id,newObj)
|
|
315
|
|
316
|
|
317 if RESPONSE is not None:
|
|
318 RESPONSE.redirect('manage_main')
|
|
319
|
|
320 def manage_addJSONClientForm(self):
|
|
321 """form for adding JSONClient"""
|
|
322 pt=zptFile(self, 'zpt/AddJSONClientForm.zpt')
|
|
323
|
|
324 return pt()
|
3
|
325
|
|
326
|
|
327
|
0
|
328 def zptFile(self, path, orphaned=False):
|
|
329 """returns a page template file from the product"""
|
|
330 if orphaned:
|
|
331 # unusual case
|
|
332 pt=PageTemplateFile(os.path.join(package_home(globals()), path))
|
|
333 else:
|
|
334
|
|
335 pt=PageTemplateFile(os.path.join(package_home(globals()), path)).__of__(self)
|
|
336 return pt
|
|
337
|