view MetaDataMapping.py @ 2:ac8e119b25ec

trying to make import from xml work
author casties
date Tue, 26 Jul 2011 11:55:19 +0200
parents e4bae49e657b
children 3dadf0d89261
line wrap: on
line source

from OFS.Folder import Folder
from OFS.SimpleItem import SimpleItem
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Globals import package_home
from AccessControl import ClassSecurityInfo
import os.path
import urllib
import logging
import urlparse

# TODO: which xml toolkit?
import amara
import xml.sax.saxutils
import xml.dom.minidom
import xml.etree.ElementTree as ET

# TODO: do we need this?
#from Products.OSA_system2 import OSAS_helpers
#from Products.OSA_system2.OSAS_metadata import OSAS_Metadata,OSAS_MetadataMapping
from OSAS_metadata import OSAS_MetadataMapping

from SrvTxtUtils import getAt


class MetaDataMapping(OSAS_MetadataMapping):
    """Metadata mapping object representing a fixed set of sub-elements."""
    
    meta_type="MetadataMapping"
    manage_options = SimpleItem.manage_options+(
        {'label':'Main Config','action':'manage_changeMetaDataMappingForm'},
        {'label':'Field order','action':'manage_changeFieldOrderForm'},
        )
    
    fields = {}
    """dict containing element descriptions"""
    
    fieldList = []
    """list of element names in preferred order"""
    
    def __init__(self,id,title,fields,fieldList=None):
        """init
        @param id: id
        @param title: title fuer zope navigation
        @param fields: dictionary mit Namen der zugelassenen Metadaten als key und Tripel  als Werte (human readable, tag version,explanation
        """
        self.id=id
        self.title=title
        for fieldName in fields.keys():
            self.fields[fieldName] = fields[fieldName]
        
        if fieldList is not None:
            self.fieldList = fieldList
        else:
            # create fieldList
            self.fieldList = fields.keys()
    

    def getFields(self):
        """returns dict of field names and field description dicts"""
        return fields
    
    def getFieldList(self):
        """returns ordered list of fields"""
        return fieldList
    
    
    
    
    
    
    
    
    # convenience method
    def getAt(self, array, idx, default=None):
        """returns element from array or default"""
        return getAt(array, idx, default)

    
    ## editing forms and methods
    
    manage_changeFieldOrderForm = PageTemplateFile('zpt/changeFieldOrder', globals())
    
    def manage_changeFieldOrder(self, fieldList=None, RESPONSE=None):
        """change the order of fields"""
        if fieldList is not None:
            newFieldList = []
            for f in fieldList.split(','):
                newFieldList.append(f.strip())
                
            # check
            if len(newFieldList) != len(self.fields.keys()):
                raise ValueError("Incorrect length of new field list!")
            
            for f in newFieldList:
                if f not in self.fields:
                    raise ValueError("Unknown field '%s'!"%f)

            self.fieldList = newFieldList

        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')
        
    manage_changeMetaDataMappingForm = PageTemplateFile('zpt/changeMetadataMapping', globals())
    
    def manage_changeMetaDataMapping(self,titleOfObject,RESPONSE=None):
        """change the MetapdataMapping"""
        fields={}
        fieldList=[]
        for idx in range(0,20):
            tag = self.REQUEST.form.get('field_'+str(idx), None)
            if tag:
                desc = {'tag': tag}
                desc['label'] = self.REQUEST.form.get('label_'+str(idx), None)
                desc['explanation'] = self.REQUEST.form.get('explanation_'+str(idx), None)
                desc['status'] = self.REQUEST.form.get('status_'+str(idx), None)
                desc['valuelist'] = self.REQUEST.form.get('valuelist_'+str(idx), None)
                desc['dcmap'] = self.REQUEST.form.get('dcmap'+str(idx), None)
                logging.debug("changeMetaDataMapping: desc=%s"%repr(desc))
                fields[tag] = desc
                fieldList.append(tag)
                #(self.REQUEST.form[arg],self.REQUEST.form['label_'+arg],self.REQUEST.form['explanation_'+arg],self.REQUEST.form['status_'+arg],self.REQUEST.form['values_'+arg])
            
        # replace data with new
        self.title = titleOfObject
        self.fields = fields
        self.fieldList = fieldList       
    
        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')

    
manage_addMetaDataMappingForm = PageTemplateFile('zpt/addMetadataMappingForm', globals())

def manage_addMetaDataMapping(self,idOfObject,titleOfObject,RESPONSE=None):
    """add the MetapdataMapping"""
    fields={}
    fieldList=[]
    for idx in range(1,20):
        tag = self.REQUEST.form.get('field_'+str(idx), None)
        if tag:
            desc = {'tag': tag}
            desc['label'] = self.REQUEST.form.get('label_'+str(idx), None)
            desc['explanation'] = self.REQUEST.form.get('explanation_'+str(idx), None)
            desc['status'] = self.REQUEST.form.get('status_'+str(idx), None)
            desc['valuelist'] = self.REQUEST.form.get('valuelist_'+str(idx), None)
            desc['dcmap'] = self.REQUEST.form.get('dcmap'+str(idx), None)
            logging.debug("addMetaDataMapping: desc=%s"%repr(desc))
            fields[tag] = desc
            fieldList.append(tag)
            #(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=MetaDataMapping(idOfObject,titleOfObject,fields,fieldList)
    self._setObject(idOfObject,newObj)
    if RESPONSE is not None:
        RESPONSE.redirect('manage_main')