Diff for /OSA_system2/OSAS_metadata.py between versions 1.3 and 1.4

version 1.3, 2005/02/11 20:06:57 version 1.4, 2005/03/03 20:41:00
Line 7  from AccessControl import ClassSecurityI Line 7  from AccessControl import ClassSecurityI
 from Products.PageTemplates.PageTemplateFile import PageTemplateFile  from Products.PageTemplates.PageTemplateFile import PageTemplateFile
 import os.path  import os.path
 import xml.dom.minidom  import xml.dom.minidom
   import xml.dom.pulldom
 import OSAS_helpers  import OSAS_helpers
 import zLOG  import zLOG
   import string
   import xml.xpath
   import xmlrpclib
   
   from types import *
   
 class OSAS_MetadataMapping(SimpleItem):  class OSAS_MetadataMapping(SimpleItem):
     """Einfaches Mapping Object"""      """Einfaches Mapping Object"""
Line 59  class OSAS_MetadataMapping(SimpleItem): Line 65  class OSAS_MetadataMapping(SimpleItem):
         @param fieldName: Bezeichnung des gesuchten Metadatums          @param fieldName: Bezeichnung des gesuchten Metadatums
         @retunr: Value des 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):      def isEmptyValue(self,fieldName):
Line 91  class OSAS_MetadataMapping(SimpleItem): Line 100  class OSAS_MetadataMapping(SimpleItem):
         arglist=self.REQUEST.form          arglist=self.REQUEST.form
                   
         for fieldName in self.readFieldsFromParent():          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:          if RESPONSE is not None:
Line 111  def manage_addMetadataMapping(self,idOfO Line 120  def manage_addMetadataMapping(self,idOfO
     argList={}      argList={}
     for arg in self.fieldList:      for arg in self.fieldList:
         if not (arg in ['idOfObject','titleOfObject']):          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)      newObj=OSAS_MetadataMapping(idOfObject,titleOfObject,argList)
     self._setObject(idOfObject,newObj)      self._setObject(idOfObject,newObj)
Line 124  class OSAS_Metadata(Folder): Line 133  class OSAS_Metadata(Folder):
     """Foldertype enthält methoden zur Halbautomatischen Erstellung von Metadatenfiles"""      """Foldertype enthält methoden zur Halbautomatischen Erstellung von Metadatenfiles"""
     security=ClassSecurityInfo()      security=ClassSecurityInfo()
           
     def __init__(self,id,linklabel,description,fields):      def __init__(self,id,shortDescription,description,fields):
         """initialize a new instance"""          """initialize a new instance"""
         self.id = id          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.description=description #description of the method for link page
         self.fieldList=fields.split(",")[0:]          self.fieldList=fields.split(",")[0:]
                   
Line 214  class OSAS_Metadata(Folder): Line 223  class OSAS_Metadata(Folder):
         except:          except:
             return id              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):      def getStoredTypes(self):
         """Gebe gespeicherte typen zurück"""          """Gebe gespeicherte typen zurück"""
                   
Line 224  class OSAS_Metadata(Folder): Line 243  class OSAS_Metadata(Folder):
                  title=obj.id                   title=obj.id
              else:               else:
                  title=obj.title                   title=obj.title
              types.append((obj.id,title))               types.append((obj.id,title,obj))
   
         return types          return types
   
Line 242  class OSAS_Metadata(Folder): Line 261  class OSAS_Metadata(Folder):
             RESPONSE.redirect('manage_main')              RESPONSE.redirect('manage_main')
   
     def getDisplayFieldsAsStr(self,indexMeta):      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          """Gebe display fields als string zurück
         @param path: Pfad zum Object          @param path: Pfad zum Object
         """          """
Line 276  class OSAS_Metadata(Folder): Line 320  class OSAS_Metadata(Folder):
         return pt()          return pt()
           
     security.declarePublic('changeMetadata')      security.declarePublic('changeMetadata')
     def changeMetadata(self,linklabel,description,fields,RESPONSE=None):      def changeMetadata(self,shortDescription,description,fields,RESPONSE=None):
         """Change Metadata"""          """Change Metadata"""
         self.linklabel=linklabel          self.shortDescription=shortDescription
         self.description=description          self.description=description
         self.fieldList=fields.split(",")[0:]          self.fieldList=fields.split(",")[0:]
         if RESPONSE is not None:          if RESPONSE is not None:
Line 291  class OSAS_Metadata(Folder): Line 335  class OSAS_Metadata(Folder):
         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','importXMLSchemaForm.zpt')).__of__(self)          pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','importXMLSchemaForm.zpt')).__of__(self)
         return pt()          return pt()
   
       
     def importXMLSchema(self,file,RESPONSE=None):      def importXMLSchema(self,file,RESPONSE=None):
        """import xmlschema, Metadatenschema wird eingelesen und entsprechende Metadatenmappings angelegt."""         """import xmlschema, Metadatenschema wird eingelesen und entsprechende Metadatenmappings angelegt."""
   
Line 320  class OSAS_Metadata(Folder): Line 365  class OSAS_Metadata(Folder):
                RESPONSE.write("Wrote: %s"%id)                 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):  def manage_addMetadataForm(self):
     """interface for adding the OSAS_add_Metadata"""      """interface for adding the OSAS_add_Metadata"""
     pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMetadataForm.zpt')).__of__(self)      pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMetadataForm.zpt')).__of__(self)
     return pt()      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"""      """add the OSAS_root"""
     newObj=OSAS_Metadata(id,linklabel,description,fields)      newObj=OSAS_Metadata(id,shortDescription,description,fields)
     self.Destination()._setObject(id,newObj)      self.Destination()._setObject(id,newObj)
     if RESPONSE is not None:      if RESPONSE is not None:
         RESPONSE.redirect('manage_main')          RESPONSE.redirect('manage_main')

Removed from v.1.3  
changed lines
  Added in v.1.4


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>