--- OSA_system2/OSAS_metadata.py 2005/02/11 20:06:57 1.3 +++ OSA_system2/OSAS_metadata.py 2005/03/03 20:41:00 1.4 @@ -7,8 +7,14 @@ from AccessControl import ClassSecurityI from Products.PageTemplates.PageTemplateFile import PageTemplateFile import os.path import xml.dom.minidom +import xml.dom.pulldom import OSAS_helpers import zLOG +import string +import xml.xpath +import xmlrpclib + +from types import * class OSAS_MetadataMapping(SimpleItem): """Einfaches Mapping Object""" @@ -58,9 +64,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,7 +100,7 @@ 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: @@ -111,7 +120,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],arglist['status_'+fieldName],arglist['values_'+fieldName]) newObj=OSAS_MetadataMapping(idOfObject,titleOfObject,argList) self._setObject(idOfObject,newObj) @@ -124,10 +133,10 @@ class OSAS_Metadata(Folder): """Foldertype enthält 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:] @@ -213,6 +222,16 @@ 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""" @@ -224,7 +243,7 @@ class OSAS_Metadata(Folder): title=obj.id else: title=obj.title - types.append((obj.id,title)) + types.append((obj.id,title,obj)) return types @@ -242,6 +261,31 @@ class OSAS_Metadata(Folder): RESPONSE.redirect('manage_main') def getDisplayFieldsAsStr(self,indexMeta): + ret=[] + + 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) + + def getDisplayFieldsAsStrOLD(self,indexMeta): """Gebe display fields als string zurück @param path: Pfad zum Object """ @@ -276,9 +320,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: @@ -291,6 +335,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.""" @@ -318,16 +363,201 @@ 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 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') + print "load index file",indexFile + server=xmlrpclib.Server(self.serverUrl) + if newdoc: + if not actualNode: actualNode=newdoc + dom=newdoc + else: + documentStr=server.getFile(indexFile) + print indexFile + print "ds",documentStr + if documentStr: + print "hhh" + newdoc=xml.dom.minidom.parseString(documentStr) + dom=newdoc.documentElement + actualNode=dom + + + else: + print "ho2" + 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 + + print xpathStr,actualNode + xpathNodes=xml.xpath.Evaluate(xpathStr,actualNode) + + + + + if len(xpathNodes)>0: + actualNode=xpathNodes[0] + + else: + #existiert nicht dann neue erzeugen + + if len(xml.xpath.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') + + 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=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')