--- OSA_system2/OSAS_metadata.py 2005/02/11 18:11:52 1.2 +++ OSA_system2/OSAS_metadata.py 2010/02/15 19:09:10 1.17 @@ -6,9 +6,26 @@ from OFS.Folder import Folder from AccessControl import ClassSecurityInfo from Products.PageTemplates.PageTemplateFile import PageTemplateFile import os.path +import sys import xml.dom.minidom +import xml.dom.pulldom import OSAS_helpers -import zLOG +import logging + +#ersetzt logging +def logger(txt,method,txt2): + """logging""" + logging.info(txt+ txt2) + + +import string +try: + from xml.xpath import Evaluate +except: + from Ft.Xml.XPath import Evaluate +import xmlrpclib + +from types import * class OSAS_MetadataMapping(SimpleItem): """Einfaches Mapping Object""" @@ -31,7 +48,7 @@ class OSAS_MetadataMapping(SimpleItem): for fieldName in arglist.keys(): setattr(self,"md_"+fieldName,arglist[fieldName]) - + manage_options = SimpleItem.manage_options+( {'label':'Main Config','action':'changeMetadataMappingForm'}, ) @@ -58,9 +75,12 @@ class OSAS_MetadataMapping(SimpleItem): """get md value @param fieldName: Bezeichnung des gesuchten Metadatums @retunr: Value des Metadatums""" - - return getattr(self,"md_"+fieldName,(None,None,None)) - + + ret= getattr(self,"md_"+fieldName,(None,None,None,None,None)) + if len(ret)!= 4: # alte MD haben keine info ueber optional/required und listen werte + ret=ret+("","") + return ret + def isEmptyValue(self,fieldName): """teste ob fielname in diesem Metadatenschema definiert ist""" @@ -91,13 +111,13 @@ class OSAS_MetadataMapping(SimpleItem): arglist=self.REQUEST.form for fieldName in self.readFieldsFromParent(): - setattr(self,"md_"+fieldName,(arglist[fieldName],arglist['label_'+fieldName],arglist['explanation_'+fieldName])) + setattr(self,"md_"+fieldName,(arglist[fieldName],arglist['label_'+fieldName],arglist['explanation_'+fieldName],arglist['status_'+fieldName],arglist['values_'+fieldName])) if RESPONSE is not None: RESPONSE.redirect('manage_main') - + manage_workspace=changeMetadataMappingForm def manage_addMetadataMappingForm(self): """interface for adding the OSAS_root""" @@ -111,7 +131,7 @@ def manage_addMetadataMapping(self,idOfO argList={} for arg in self.fieldList: if not (arg in ['idOfObject','titleOfObject']): - argList[arg]=(self.REQUEST.form[arg],self.REQUEST.form['label_'+arg],self.REQUEST.form['explanation_'+arg]) + argList[arg]=(self.REQUEST.form[arg],self.REQUEST.form['label_'+arg],self.REQUEST.form['explanation_'+arg],self.REQUEST.form['status_'+arg],self.REQUEST.form['values_'+arg]) newObj=OSAS_MetadataMapping(idOfObject,titleOfObject,argList) self._setObject(idOfObject,newObj) @@ -121,13 +141,13 @@ def manage_addMetadataMapping(self,idOfO class OSAS_Metadata(Folder): - """Foldertype enthält methoden zur Halbautomatischen Erstellung von Metadatenfiles""" + """Foldertype enthaelt methoden zur Halbautomatischen Erstellung von Metadatenfiles""" security=ClassSecurityInfo() - def __init__(self,id,linklabel,description,fields): + def __init__(self,id,shortDescription,description,fields): """initialize a new instance""" self.id = id - self.linklabel =linklabel #label fuer link auf add page + self.shortDescription =shortDescription #label fuer link auf add page self.description=description #description of the method for link page self.fieldList=fields.split(",")[0:] @@ -171,29 +191,34 @@ class OSAS_Metadata(Folder): def generateMappingList(self): """Erzeuge Mapping""" mapping={} + for dict in self.__dict__: #print dict obj=getattr(self,dict) if hasattr(obj,'meta_type'): - if obj.meta_type=="OSAS_MetadataMapping": + if obj.meta_type=="OSAS_MetadataMapping__neu": mapping[obj.getId()]=obj.generateMappingHash() + return mapping def generateMappingForType(self,type,clean="yes"): """erzeuge spezifisches Mapping""" + hash=self.generateMappingList() - if hash.has_key(type): - if clean=="yes": - temp={} - for x in hash[type].keys(): - if not hash[type][x]=="": - temp[x]=hash[type][x] - return temp - else: - return hash[type] - else: - return {} + for key in hash.keys(): + if (key.lower() == type.lower()): + if clean=="yes": + temp={} + for x in hash[key].keys(): + if not hash[key][x]=="": + temp[x]=hash[key][x] + return temp + else: + + return hash[key] + + return {} def getFieldList(self): """erzeuge string aus fields""" @@ -202,6 +227,7 @@ class OSAS_Metadata(Folder): except: return "" + security.declarePublic('getFields') def getFields(self): """ausgabe der Felder""" return self.fieldList @@ -213,18 +239,28 @@ class OSAS_Metadata(Folder): return obj.title except: return id + + def getType(self,type): + """gib metadataobject type zurueck""" + + for obj in self.ZopeFind(self,obj_metatypes=['OSAS_MetadataMapping__neu']): + if obj[0]==type: + return obj + return (self.id,self) + + def getStoredTypes(self): - """Gebe gespeicherte typen zurück""" + """Gebe gespeicherte typen zurueck""" types=[] - for obj in self.ZopeFind(self,obj_metatypes=['OSAS_MetadataMapping_neu']): - if obj.title=="": - title=obj.id - else: - title=obj.title - types.append((obj.id,title)) + for obj in self.ZopeFind(self,obj_metatypes=['OSAS_MetadataMapping__neu']): + if obj[1].title=="": + title=obj[1].id + else: + title=obj[1].title + types.append((obj[1].id, title, obj[1])) return types @@ -241,19 +277,44 @@ class OSAS_Metadata(Folder): if RESPONSE is not None: RESPONSE.redirect('manage_main') - def getDisplayFieldsAsStr(self,path,server): - """Gebe display fields als string zurück + def getDisplayFieldsAsStr(self,indexMeta): + ret=[] + try: + if indexMeta and not (indexMeta==""): + dom=xml.dom.pulldom.parseString(indexMeta) + + for (event,node) in dom: + + if event == xml.dom.pulldom.START_ELEMENT and node.tagName=="bib": + dom.expandNode(node) + + try: + type=node.getAttribute('type') + mapping=getattr(self,type).generateMappingHash() + except: + type='generic' + mapping=getattr(self,type).generateMappingHash() + + for field in self.displayFields: + try: + ret.append(OSAS_helpers.getText(node.getElementsByTagName(mapping[field][0])[0].childNodes)) + except: + """nothing""" + + return "; ".join(ret) + else: + return "" + except: + return "" + def getDisplayFieldsAsStrOLD(self,indexMeta): + """Gebe display fields als string zurueck @param path: Pfad zum Object """ ret=[] - f=server.getFile(path+"/index.meta") - if not f: - return "" - try: - dom=xml.dom.minidom.parseString(f) + dom=xml.dom.minidom.parseString(indexMeta) except: - zLOG.LOG("OSAS_metadata (getDisplayFieldsAsStr)",zLOG.INFO,"Cannot parse: %s"%path) + logger("OSAS_metadata (getDisplayFieldsAsStr)",logging.INFO,"Cannot parse: %s"%indexMeta) try: bib = dom.getElementsByTagName("meta")[0].getElementsByTagName("bib")[0] except: @@ -280,9 +341,9 @@ class OSAS_Metadata(Folder): return pt() security.declarePublic('changeMetadata') - def changeMetadata(self,linklabel,description,fields,RESPONSE=None): + def changeMetadata(self,shortDescription,description,fields,RESPONSE=None): """Change Metadata""" - self.linklabel=linklabel + self.shortDescription=shortDescription self.description=description self.fieldList=fields.split(",")[0:] if RESPONSE is not None: @@ -295,6 +356,7 @@ class OSAS_Metadata(Folder): pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','importXMLSchemaForm.zpt')).__of__(self) return pt() + def importXMLSchema(self,file,RESPONSE=None): """import xmlschema, Metadatenschema wird eingelesen und entsprechende Metadatenmappings angelegt.""" @@ -322,16 +384,259 @@ class OSAS_Metadata(Folder): self._setObject(id,OSAS_MetadataMapping(id,id,argList)) if RESPONSE: RESPONSE.write("Wrote: %s"%id) - + + + def createMetadataFragment(self,type,path,prefix="",presets={}): + """fragment""" + self.REQUEST.SESSION['MDF_type']=type + self.REQUEST.SESSION['MDF_path']=path + self.REQUEST.SESSION['MDF_prefix']=prefix + self.REQUEST.SESSION['MDF_presets']=presets + + pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMetadataForm_fragment.zpt')).__of__(self) + return pt() + + def createMetadataForm(self,type="",path=""): + """createMetadataForm""" + self.REQUEST.SESSION['MDF_type']=type + self.REQUEST.SESSION['MDF_path']=path + + + pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMetadataForm_template.zpt')).__of__(self) + return pt() + + def readMetadata(self,MDF_path,MDF_type,MDF_prefix="",MDF_xpathStart="", MDF_addPath=None,MDF_identifyFields=None,newdoc=None,actualNode=None): + + if type(MDF_path)==ListType: + MDF_path=MDF_path[0] + + indexFile=os.path.join(MDF_path,'index.meta') + + server=xmlrpclib.Server(self.serverUrl) + + documentStr=server.getFile(indexFile) + + if documentStr: + newdoc=xml.dom.minidom.parseString(documentStr) + dom=newdoc.documentElement + actualNode=dom + else: + return {} + + if self.containerTag=="": + containerTag="doc" + else: + containerTag=self.containerTag + + if MDF_xpathStart=="": + dom=newdoc.documentElement + actualNode=dom + else: + #try to find xpath + if MDF_identifyFields: + query=[] + for field in MDF_identifyFields.keys(): + + query.append("""(%s="%s")"""%(field,MDF_identifyFields[field])) + querystr=" and ".join(query) + xpathStr=MDF_xpathStart+"[%s]"%querystr + + else: + xpathStr=MDF_xpathStart + + + xpathNodes=Evaluate(xpathStr,actualNode) + + + if len(xpathNodes)>0: + actualNode=xpathNodes[0] + else: + return {} + + ret={} + for node in actualNode.childNodes: + name=node.tagName + text=OSAS_helpers.getText(node.childNodes) + ret[name]=text + + return ret + + def writeMetadata(self,MDF_path,MDF_type,form,MDF_prefix="",MDF_xpathStart="", MDF_addPath=None,MDF_identifyFields=None,newdoc=None,actualNode=None): + """writeMetadata""" + #MDF_path="/tmp/index.meta" + if type(MDF_path)==ListType: + MDF_path=MDF_path[0] + + + indexFile=os.path.join(MDF_path,'index.meta') + + server=xmlrpclib.Server(self.serverUrl) + if newdoc: + if not actualNode: actualNode=newdoc + dom=newdoc + else: + documentStr=server.getFile(indexFile) + + if documentStr: + + newdoc=xml.dom.minidom.parseString(documentStr) + dom=newdoc.documentElement + actualNode=dom + + + else: + + impl=xml.dom.minidom.getDOMImplementation() + newdoc=None + + + if self.containerTag=="": + containerTag="doc" + else: + containerTag=self.containerTag + + create=None + if MDF_xpathStart=="": + if not newdoc: + newdoc=impl.createDocument(None,containerTag,None) + dom=newdoc.documentElement + actualNode=dom + else: + + #try to find xpath + if MDF_identifyFields: + query=[] + for field in MDF_identifyFields: + + query.append("""(%s="%s")"""%(field,form[MDF_prefix+"MD_"+field])) + querystr=" and ".join(query) + xpathStr=MDF_xpathStart+"[%s]"%querystr + + else: + xpathStr=MDF_xpathStart + + + xpathNodes=Evaluate(xpathStr,actualNode) + + + + + if len(xpathNodes)>0: + actualNode=xpathNodes[0] + + else: + #existiert nicht dann neue erzeugen + + if len(Evaluate(MDF_xpathStart,dom))>0: + + create=True + + splitted=MDF_xpathStart.split("/") + base="" + for element in splitted: + + if not (element=="") and not (element==containerTag): + base="/".join([base,element]) + + if not newdoc: + newdoc=impl.createDocument(None,element,None) + actualNode=newdoc.documentElement + dom=actualNode + else: + changed=None + + if not (MDF_addPath==base): + + + for childNode in actualNode.childNodes: + if getattr(childNode,'tagName','')==element: + actualNode=childNode + changed=1 + + if (os.path.normpath("/".join(["",containerTag,base]))==MDF_xpathStart) and create: + actualNode=actualNode.parentNode + changed=None + + if not changed: + namenode=newdoc.createElement(element) + + actualNode.appendChild(namenode) + actualNode=namenode + + + + + for name in self.REQUEST.form.keys(): + length=len(MDF_prefix) + if MDF_type and not (MDF_type == ""): + actualNode.setAttribute("type",MDF_type) + if name[0:3+length]==MDF_prefix+"MD_": + tagName=name[3+length:] + + #CHECK if element exists + for childNode in actualNode.childNodes: + if getattr(childNode,'tagName','')==tagName: + actualNode.removeChild(childNode).unlink() + + namenode=newdoc.createElement(tagName) + namenodetext=newdoc.createTextNode(self.REQUEST.form[name]) + namenode.appendChild(namenodetext) + actualNode.appendChild(namenode) + + ret=newdoc.toxml(encoding='utf-8') + zLOG.LOG("OSAS_metadata (writeMetadata)",zLOG.INFO,"write: %s"%ret) + + + server.writeMetaDataFile(indexFile,ret) + + + return newdoc,actualNode + + def writeMetadataFile(self,MDF_path,MDF_type,MDF_xpathStart="",newdoc=None,actualNode=None): + """writeMetaFile""" + + return self.writeMetadata(MDF_path,MDF_type,self.REQUEST.form,MDF_xpathStart=MDF_xpathStart,newdoc=newdoc,actualNode=actualNode) + + def isEmptyValue(self,fieldName): + """im generischen fall stets falsch""" + return 1 + + def getValue(self,fieldName): + """im generischen fall gleich fieldname""" + return fieldName,fieldName,"","","" + + def getList(self,list): + """return list""" + + if list=="": + return None + listsplit=[i.rstrip() for i in list.split("\n")] + return listsplit + + def showHelp(self,refType,genericTag): + """helptext""" + for reference in self.ZopeFind(self): + if reference[1].title==refType: + text=getattr(reference[1],'md_'+genericTag)[2] + return text + return "NO EXPLANATION" + + def showHelpTag(self,url,reftype,item): + """gen javascript for showhelp""" + url2=url+'/showHelp?refType=%s&genericTag=%s'%(reftype,item) + ret="""javascript:wd=window.open(\'%s\',\'Help\',\'width=300,height=250\');void(\'\');wd.focus();"""%url2 + return ret + + def manage_addMetadataForm(self): """interface for adding the OSAS_add_Metadata""" pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMetadataForm.zpt')).__of__(self) return pt() -def manage_addMetadata(self,id,linklabel,description,fields,RESPONSE=None): +def manage_addMetadata(self,id,shortDescription,description,fields,RESPONSE=None): """add the OSAS_root""" - newObj=OSAS_Metadata(id,linklabel,description,fields) + newObj=OSAS_Metadata(id,shortDescription,description,fields) self.Destination()._setObject(id,newObj) if RESPONSE is not None: RESPONSE.redirect('manage_main')