view MPIWGThesaurus.py @ 4:fa8d698958f7

sorting by lastname Gerade nur f?r die Namen aus der Personenontologie nicht f?r die Tempor?ren Namen
author dwinter
date Tue, 11 Oct 2011 19:57:22 +0200
parents 6f4b30846fed
children 2773140337ad
line wrap: on
line source

#Klasse zum Taggen und Anzeigen der Tags von Projekte bzw. allgemein Webseiten des Institutes
#Tags werden in der Instanz abgespeichert
#Personenname kommen aus dem Virtuoso-Triplestore , lokal erzeugte zusaetliche Name werden dort abgespeichert
#Fuer die Suche über die Personenname wird das MetaDataManagerRestlet benoetigt.
#TODO: Zur Zeit werden die entsprechenden Server URLs hier in der Klasse definiert.


# TODO: generell verwaltung der tags im triple store und nicht in zope.
# TODO: mache server und namespaces konfigurierbar im project


from OFS.Folder import Folder
from Globals import package_home
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from BTrees.OOBTree import OOBTree
from BTrees.OOBTree import OOSet
from AccessControl import ClassSecurityInfo

#import os
import os.path
import logging
import time
import email.Utils
from types import ListType
import transaction
import random
import urllib2
import urllib

#Entry beschreibt jeweils einen Tag mit dem Namen "title", entries enthaelt eine Liste von Subtags (der Klasse Entry), die diesem Tag untergeordnet sind.
class Entry:
    """publicClass"""
    
    #===========================================================================
    # __init__
    #===========================================================================
    def __init__(self,title,entries=[]):
        self.title=title
        self.entries=entries
        
    def getTitle(self):
        """getTitle"""
        return self.title
    
#Beschriebt einen von einem Benutzer vorgeschlagenen Tag      
class SuggestedTag:
    """publicClass"""
    type=""
    userName=""
    def __init__(self,name,comment,type,userName):
        self.name=name
        self.comment=comment
        self.type=type
        self.userName=userName
        

class MPIWGThesaurus(Folder):
    """MPIWGThesaurus main folder"""
    security=ClassSecurityInfo()

    meta_type="MPIWGThesaurus"
   
    TMP_PERSON_NS="http://ontologies.mpiwg-berlin.mpg.de/tempObjects/person/"
    ONTOLOGY_NS="http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/"
    #personproviderURL="http://127.0.0.1:8280/MetaDataManagerRestlet/person/"
    personproviderURL="http://virtuoso.mpiwg-berlin.mpg.de:8080/MetaDataManagerRestlet/person/"
    additionalNamesGraphURL="file://newpersonsFromProjects"
    
    #virtuosoServer="http://ontologies.mpiwg-berlin.mpg.de"
    virtuosoServer="http://virtuoso.mpiwg-berlin.mpg.de:8890"
    virtuosoDAV="/DAV/home/websiteuser/"
    virtuosoDAVUser="websiteuser"
    virtuosoDAVPW="w3s45us3"
    virtuosoGraph="file://newpersonsFromProjects"
    
    #BTrees fuer die Tags
    
    projectPersons= OOBTree()
    projectObjects= OOBTree()
    projectHistoricalPlaces= OOBTree()
    projectSuggestedTags= OOBTree()
    persons2Projects= OOBTree()
    objects2Projects=OOBTree()
    historicalPlaces2Projects=OOBTree()
    suggestedTags2Projects=OOBTree()
    personIDtoNames=OOBTree() # weist den personenIDs Klartextnamen aus dem Triplestore zu
    personsIDForSort=[] # liste (personenID,(nachname,vorname) sortiert nach dem tupel (nachname,vorname)
    suggestedPersons= OOBTree(); #TODO: das sollte die ueberfluessig werde und nur im triple store stehen.
    
    tagList=[]
    
    #default liste fuer tags, wird ueber das Managementinterface ueberschrieben
    approachesTxt="""A
    ---
    ent1
    ---
    B
    ---
    enta
    entb
    entc
    """
    
    disciplines=[]
    disciplinesTxt="""A
   B
   C
    """
    
    periods=[]
    periodsTxt="""PA
   PB
   PC
    """
    
    spaces=[]
    spacesTxt="""SPA
    ---
    sPent1
    ---
    SPB
    ---
    SPenta
    SPentb
    SPentc
    """
    technologies=[]
    technologiesTxt="""TPA
    ---
    TPent1
    ---
    TPB
    ---
    TPenta
    TPentb
    TPentc
    """
    
    knowledgeTransfers=[]
    knowledgeTransfersTxt="""KTPA
    ---
    TPent1
    ---
    TPB
    ---
    TPenta
    TPentb
    TPentc
    """
    
    
    
    
    
    
    manage_options = Folder.manage_options+(
        {'label':'Thesaurus entries','action':'changeMPIWGThesaurusForm'},
        {'label':'Manage Thesaurus','action':'manageMPIWGThesaurus'},
        #{'label':'Change Publications Special','action':'changePublications_specialForm'},
        )
    
    def __init__(self,id):
        """init"""
        self.id=id
       
        
    def index_html(self):
        """index file"""
              
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','index.html')).__of__(self)
        return pt()
    
    security.declareProtected('View management screens','changeMPIWGThesaurusForm')
    def changeMPIWGThesaurusForm(self):
        """form for changing the project"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','changeMPIWGThesaurusForm.zpt')).__of__(self)
        return pt()
    
    security.declareProtected('View management screens','manageMPIWGThesaurus')
    def manageMPIWGThesaurus(self):
        """form for changing the project"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','manageMPIWGThesaurus.zpt')).__of__(self)
        return pt()
    
    security.declareProtected('View management screens','changeMPIWGThesaurus')
    def changeMPIWGThesaurus(self,approachesTxt,disciplinesTxt,periodsTxt,spacesTxt,technologiesTxt,knowledgeTransfersTxt,RESPONSE=None):
        """change it"""
        self.approachesTxt=approachesTxt
        self.disciplinesTxt=disciplinesTxt
        self.periodsTxt=periodsTxt
        self.spacesTxt=spacesTxt
        self.technologiesTxt=technologiesTxt
        self.knowledgeTransfersTxt=knowledgeTransfersTxt
    
        if RESPONSE is not None:
            redirect(RESPONSE,'manage_main')
     
    #In der Anzeige soll der Name der zitierten Personen in Klartext angezeigt werden, ausserdem die Varianten, wie sie tatsaechlich
    #in den Projekten benutzt werden
    
    security.declareProtected('View management screens','collectPersonNamesToIds')
    def collectPersonNamesToIds(self):
        """Ordne Namen aus dem Triplestore bzw. die vergebenenden Label den Personen-Ids zu."""
        self.personIDtoNames=OOBTree() # loesche alle 
        self.personsIDForSort=[]
        personsIDForSortTmp=[]
        for personID in self.persons2Projects:
            mainName,sortName=self.getMainNameFromTripleStore(personID); # hole die hauptbezeichnung aus dem triplestore
            
            personNames=[]
            for project in self.persons2Projects.get(personID):
                #hole die personen aus dem projekte 
                logging.debug("Found:project:"+project)
                namesInProject = self.projectPersons.get(project)
                
                for nameInProjectTuple in namesInProject:
                    logging.debug(repr(nameInProjectTuple))
                    nameInProject=nameInProjectTuple[1]
                    logging.debug(nameInProjectTuple[0]+"??"+personID)
                    if nameInProjectTuple[0]==personID:
                        logging.debug("changing")
                        if mainName=="": # es gibt keinen Eintrag im Triplestore fuer name (sollte eigentlich nicht sein, ist dann ein Fehler dort)
                            mainName=nameInProject #nimm dann aber den ersten aus den projekten
                        if nameInProject!=mainName:
                            personNames.append(nameInProject)
            
            logging.debug(str(mainName)+"- foundalso :"+",".join(personNames))
            self.personIDtoNames.update({personID:(mainName,personNames)})
            personsIDForSortTmp.append((personID,sortName))
            
          
            
                                    
        #sortiere personIDs nach erstmal nach Nachnamene TODO: sortiere nach name,vorname
        def sort(x,y):
            logging.debug("sort:")
            logging.debug((x[1][0],y[1][0]))
            return cmp(x[1][0],y[1][0])
            
        personsIDForSortTmp.sort(cmp=sort)
           
        self.personsIDForSort=[x[0] for x in personsIDForSortTmp]
                                      
        self.personIDtoNames=self.personIDtoNames # make clear that it has changed #TODO: change this to variablen with names _p
    
    #Hole die Namen, die einer personID zugeordnet sind.
    def getNamesFromID(self,personID):
        retStr=""
        retStr+=self.personIDtoNames.get(personID)[0] # hole die Hauptbezeichnung
        additionalNames = self.personIDtoNames.get(personID)[1]
        if len(additionalNames)>0:
            retStr+=" ("+",".join(additionalNames)+","+")"
        return retStr
    
        
    #Hole die Hauptnamemsansetzung aus dem Triplestore ),falls vorhanden, wenn nicht ist der String leer.
    #ausserdem einen Namen zum sortieren, =  Tupel(nachname, vorname)
    def getMainNameFromTripleStore(self,personID):
        
        
        cmdString ="""select * where { <%s> <http://xmlns.com/foaf/0.1/name> ?name}"""%personID
    
        names= self.callSparql(cmdString)
        
        cmdString ="""select * where { <%s> <http://xmlns.com/foaf/0.1/lastName> ?name}"""%personID
    
        lastName= self.callSparql(cmdString)
        
        cmdString ="""select * where { <%s> <http://xmlns.com/foaf/0.1/firstName> ?name}"""%personID
    
        firstName= self.callSparql(cmdString)
        
        return names,(lastName,firstName)
    
    
    def callSparql(self,cmdString):

        auth_handler = urllib2.HTTPBasicAuthHandler()
        auth_handler.add_password(realm='sparql',
                          uri=self.virtuosoServer+"/sparql",
                          user=self.virtuosoDAVUser,
                          passwd=self.virtuosoDAVPW)
                          
        opener = urllib2.build_opener(auth_handler)
        opener.addheaders = [('Content-Type','application/sparql-query')]
        
        logging.debug(cmdString)
        try:
            logging.debug(self.virtuosoServer+"/sparql?" + urllib.urlencode({'query':cmdString,'default-graph-uri':self.virtuosoGraph,'named-graph-uri':'','format':'text/csv'}))
            #r= opener.open(self.virtuosoServer+"/sparql", urllib.urlencode({'query':cmdString,'default-graph-uri':self.virtuosoGraph,'named-graph-uri':'','format':'text/csv'}))
            r= opener.open(self.virtuosoServer+"/sparql", urllib.urlencode({'query':cmdString,'default-graph-uri':'','named-graph-uri':'','format':'text/csv'}))
            namesTxt=r.read()
        except urllib2.URLError, e:
            logging.error(e.code)
            logging.error(e.read())
            
            
            return
        logging.debug(namesTxt)
        names=namesTxt.split("\n")
        if len(names) < 2: #in der ersten Zeile stehen bei der Rückgabe die Spaltennamen, <2 heiss also es gibt keinen Eintrag
            return
         
        return names[1].replace('"','') # wir nehmen nur den ersten treffer
         
            
    security.declareProtected('View management screens','changeTags')            
    def changeTags(self,projectID,approaches=[],disciplines=[],periods=[],spaces=[],technologies=[],transfers=[],RESPONSE=None):
        """change tags"""
        self.changeTagFromList(projectID,"approaches", approaches)
        self.changeTagFromList(projectID,"disciplines", disciplines)
        self.changeTagFromList(projectID,"periods", periods)
        self.changeTagFromList(projectID,"spaces", spaces)
        self.changeTagFromList(projectID,"technologies", technologies)
        self.changeTagFromList(projectID,"transfers", transfers)
        
        self.projectObjects=self.projectObjects #necessesary to make the object persistent after the first call
        self.projectHistoricalPlaces=self.projectHistoricalPlaces #necessesary to make the object persistent after the first call
        self.projectPersons=self.projectPersons #necessesary to make the object persistent after the first call
        self.projectSuggestedTags=self.projectSuggestedTags
        
        self.persons2Projects=self.persons2Projects
        self.objects2Projects=self.objects2Projects
        self.historicalPlaces2Projects=self.historicalPlaces2Projects
        self.suggestedTags2Projects=self.suggestedTags2Projects;
        
        self.suggestedPersons=self.suggestedPersons;
        
    security.declareProtected('View management screens','deleteAllTags')                    
    def deleteAllTags(self):
        """deleteAllTags - TODO: take this out!!,"""
        setattr(self,"tags",None)
        
        return "DONE!"
    
    security.declareProtected('View management screens','deleteAllPersons')                    
    def deleteAllPersons(self):
        """deleteAllTags - TODO: take this out!!,"""
        self.projectPersons= OOBTree()
        self.persons2Projects= OOBTree()
        self.personIDtoNames=OOBTree()
        self.suggestedPersons= OOBTree(); #TODO: das sollte die ueberfluessig werde und nur im triple store stehen.
    
        
        return "DONE!"
    
    security.declareProtected('View management screens','deleteAllObjects')                    
    def deleteAllObjects(self):
        """deleteAllTags - TODO: take this out!!,"""
        logging.debug("delete all objects")
        self.projectObjects= OOBTree()
        self.objects2Projects=OOBTree()
    
        
        return "DONE!"
    
    def changeTagFromList(self,projectID,tagType,tagList):
        
        logging.debug("changing:"+projectID)
        logging.debug("asking tag::"+tagType)
        if type(tagList) is not ListType:
            tagList=[tagList]
        
    
        tags = getattr(self,"tags",None)
        if tags is None:
            logging.debug("Create new tags")
            tags=OOBTree()
            setattr(self,"tags",tags)
        
    
        tagsStored=tags.get(tagType,None)
        if tagsStored is None:
            tagsStored=OOBTree();
        
        
        
        for tag in tagsStored.keys(): #gehe durch alle gespeichteren tags
            logging.debug("checking:"+tag)
            currentApproach = tagsStored.get(tag,None)
            logging.debug(currentApproach)
            if currentApproach is None:
                currentApproach=OOSet()
            
            if tag in tagList: #dieser ist getagged
                currentApproach.update([projectID]);
                tagList.remove(tag); 
            else:
                if currentApproach.has_key(projectID):
                    currentApproach.remove(projectID);
                
            tagsStored.update({tag:currentApproach});
        
        for tag in tagList: # alle die nich nicht abearbeitet worden muessen noch angelegt werden
            logging.debug("adding:"+tag)
            newApproach=OOSet()
            newApproach.update([projectID]);
            tagsStored.insert(tag, newApproach);
        
        tags.update({tagType:tagsStored})
        
        transaction.commit()
        
    def getTitle(self,entry):
        return entry.title
    
    def getEntries(self,entry):
        return entry.entries
    
    #nimmt ein eine Text der Form 
    # TAG1
    # ---
    # SUBTAG1_1
    # SUBTAG1_2
    # ---
    # TAG2
    # ---
    # SUBTAG2_1
    # SUBTAG2_2
    #....
    # und erzeugt daraus eine entsprechende Liste von Entries
    def getEntriesFromTxt(self,txt):
        apps=[]
        lines=txt.split("\n");
        level=False
        for line in lines:
            line=line.rstrip().lstrip()
            if line=="":
                continue
            if line=="---":
                
                if level:
                    level=False
                else:
                    level=True
            else:
                if not level:
                    entry=Entry(line,[])
                    apps.append(entry)
                else:
                    entry.entries.append(Entry(line))
                
        return apps[0:]
    
    def getApproaches(self):
        
        self.tagList=self.getEntriesFromTxt(self.approachesTxt)    
        return self.tagList
    
  
    def getTagsJSON(self,projectID,tagType,RESPONSE=None):
        """get all selected tagList"""
        logging.debug("asking:"+projectID)
        logging.debug("asking tag::"+tagType)
        retList=[]
        tags = getattr(self,"tags",None)
        if tags is None:
            return;
        
        tagsStored=tags.get(tagType,None)
        if tagsStored is None:
            return;
        
        for tag in tagsStored.keys(): #gehe durch alle gespeichteren approached
            logging.debug("checking:"+tag)
            currentTag = tagsStored.get(tag,None)
            if currentTag is None:
                continue
            if currentTag.has_key(projectID):
                retList.append(""" "%s" """%tag)
        
        if RESPONSE:
            RESPONSE.setHeader('Content-Type', "application/json")
        
        logging.debug(retList)
        return "["+",".join(retList)+"]"
    
    def getDisciplines(self):
        
        self.disciplines=self.getEntriesFromTxt(self.disciplinesTxt)    
        return self.disciplines
    
    
    def getObjectsJSON(self,term="",RESPONSE=None):
        """ get all stored objects as json list starting with term"""
        term=term.lower();
        logging.debug("Asking:"+term)
        #objectList=["baus","berg","lurg"]
        objects = self.objects2Projects;

        objectList= [x for x in objects.keys()]   
       
        logging.debug("Create:"+repr(objectList))    
        retList=[]
        for object in objectList:
            if object.find(term)>-1:
                retList.append(""" "%s" """%object)
        ret="["
        ret+=",".join(retList)
        ret+="]"
        logging.debug("Objects:"+ret)
        if RESPONSE:
            RESPONSE.setHeader('Content-Type', "application/json")
        return ret
        
        
    def getHistoricalPlacesJSON(self,term="",RESPONSE=None):
        """ get all stored historicalPlaces as json list starting with term"""
        term=term.lower();
        logging.debug("Asking:"+term)
        #historicalPlaceList=["baus","berg","lurg"]
        historicalPlaces = self.historicalPlaces2Projects;

        historicalPlaceList= [x for x in historicalPlaces.keys()]   
       
        logging.debug("Create:"+repr(historicalPlaceList))    
        retList=[]
        for historicalPlace in historicalPlaceList:
            if historicalPlace.find(term)>-1:
                retList.append(""" "%s" """%historicalPlace)
        ret="["
        ret+=",".join(retList)
        ret+="]"
        logging.debug("historicalPlaces:"+ret)
        if RESPONSE:
            RESPONSE.setHeader('Content-Type', "application/json")
        return ret
        
        
    def getPeriods(self):
        
        self.periodes=self.getEntriesFromTxt(self.periodsTxt)    
        return self.periodes
    
    def getSpaces(self):
        
        self.spaces=self.getEntriesFromTxt(self.spacesTxt)    
        return self.spaces
    
    def getTechnologies(self):
        
        self.technologies=self.getEntriesFromTxt(self.technologiesTxt)    
        return self.technologies
    
    def getKnowledgeTransfers(self):
        
        self.knowledgeTransfers=self.getEntriesFromTxt(self.knowledgeTransfersTxt)    
        return self.knowledgeTransfers
    
    
    def getObjectsFromProject(self,projectID):
        return self.getFromProject(self.projectObjects,projectID);
    
    def getHistoricalPlacesFromProject(self,projectID):
        return self.getFromProject(self.projectHistoricalPlaces,projectID);
    
    def getSuggestedTagsFromProject(self,projectID):
        return self.getFromProject(self.projectSuggestedTags,projectID);
 
    
    def getPersonsFromProject(self,projectID):
        return self.getFromProject(self.projectPersons,projectID);
    
    def getFromProject(self,objects,projectID):  
        if objects.has_key(projectID):
            logging.debug("key exists:"+projectID)
            persons=objects.get(projectID);
        else:
            persons=OOSet()
            objects.update({projectID:persons})
            logging.debug("key does not exists:"+projectID)
            
        
        return persons
    
    def getHistoricalPlacesFromProjectAsHTML(self,projectID):
        """gertobjects"""
        return self.getFromProjectAsHTML(self.projectHistoricalPlaces, projectID, "HistoricalPlace");
 
    def getObjectsFromProjectAsHTML(self,projectID):
        """gertobjects"""
        return self.getFromProjectAsHTML(self.projectObjects, projectID, "Object");
    
    def getPersonsFromProjectAsHTML(self,projectID):
        """getpersons"""
        return self.getFromProjectAsHTML(self.projectPersons, projectID, "Person");
       
        
    def getFromProjectAsHTML(self,objects,projectID,type):
        """getpersons"""
        ret=""
        if objects.has_key(projectID):
            persons=objects.get(projectID);
        else:
            persons=OOSet()
            
        
        for person in persons:
            ret+="""<div class="removeButton">
        <span>%s </span><div style="display:inline" class="remove%s"><span style="display:none" class="removeValue">%s</span>remove</div>
        </div>"""%(person[1],type,person[0])
            
            
#            ret+="""<div>
#        <input type="checkbox" name="persons_remove"
#         value="%s">
#        <span>%s</span>
#        </div>"""%(person[0],person[1])
#        
        logging.debug(type+":"+ret)
        return ret
    
    security.declareProtected('View management screens','removeHistoricalPlaceFromProject')                    
    def removeHistoricalPlaceFromProject(self,projectID,value):
        """remove"""
        logging.debug("removeHP:"+projectID+":"+value);
        
        HistoricalPlaces = self.getHistoricalPlacesFromProject(projectID)
        for HistoricalPlace in HistoricalPlaces.keys():
           
            logging.debug("trying:"+repr(HistoricalPlace[0])+"::"+repr(value)+"__")
            if HistoricalPlace[0]==value:
                logging.debug("---removed!")
                HistoricalPlaces.remove(HistoricalPlace)
        
        self.projectHistoricalPlaces.update({projectID:HistoricalPlaces})
        retstring = self.getHistoricalPlacesFromProjectAsHTML(projectID)
        logging.debug(retstring)
        return retstring
    
    security.declareProtected('View management screens','removeObjectFromProject') 
    def removeObjectFromProject(self,projectID,value):
        """remove"""
        logging.debug("remove:"+projectID+":"+value);
        
        objects = self.getObjectsFromProject(projectID)
        for object in objects.keys():
           
            logging.debug("trying:"+repr(object[0])+"::"+repr(value)+"__")
            if object[0]==value:
                logging.debug("---removed!")
                objects.remove(object)
        
        self.projectObjects.update({projectID:objects})
        retstring = self.getObjectsFromProjectAsHTML(projectID)
        logging.debug(retstring)
        return retstring
    
    security.declareProtected('View management screens','removePersonFromProject') 
    def removePersonFromProject(self,projectID,value):
        """remove"""
        logging.debug("remove:"+projectID+":"+value);
        
        persons = self.getPersonsFromProject(projectID)
        for person in persons.keys():
           
            logging.debug("trying:"+repr(person[0])+"::"+repr(value)+"__")
            if person[0]==value:
                logging.debug("---removed!")
                persons.remove(person)
        
        self.projectPersons.update({projectID:persons})
        retstring = self.getPersonsFromProjectAsHTML(projectID)
        logging.debug(retstring)
        return retstring
    
    #fuegt die neue Person zur personIDtoNames hinzu, für die spätere Anzeige in der Personenliste
    def addNameTOPersonIDNames(self,personID,label):
        personName = self.personIDtoNames.get(personID)
        if personName==None: # Anlegen falls noch nicht existent
            personName=(label,[])
        else:
            if (not label in personName[1]) and (not label==personName[0]): # hinzufuegen falls der neuen name noch nicht als bezeichnugn fuer personID eingetragen ist
                personName[1].append(label) 
    
        self.personIDtoNames.update({personID:personName})
        
    security.declareProtected('View management screens','addPersonToProject')     
    def addPersonToProject(self,projectID,value,label):
        """ add a person so the project"""
        persons=self.getPersonsFromProject(projectID);
        
        logging.debug("adding:"+projectID+":"+value+":"+label);
      
        persons.insert([value,label])
        self.projectPersons.update({projectID:persons})
        
        for personTuple in persons:
            #hole die abgespeicherten Projekte zu einer Person
            person=personTuple[0] #hole die ID
            
            
            if getattr(self,"persons2Projects",None)==None:
                setattr(self,"persons2Projects",OOBTree())
         
            projects = self.persons2Projects.get(person,None)
                   
            if projects==None:
                projects=OOSet();
            
            projects.insert(projectID )
            logging.debug("update:"+person)
            self.persons2Projects.update({person:projects})
        
        self.addNameTOPersonIDNames(value,label)
        retstring = self.getPersonsFromProjectAsHTML(projectID)
        logging.debug(retstring)
        transaction.commit()
        return retstring
    
    
    def getPersonsWithProjectIDs(self):
        """holt die getaggted Personen mit Projekten"""
        persons = self.persons2Projects;
        ret={}
        
        logging.debug("Check Person:"+repr(persons.keys()))
        personsList=[x for x in persons.keys()]
        
        def sort(x,y):
            sortNrx=self.personsIDForSort.index(x)
            sortNry=self.personsIDForSort.index(y)
            #logging.debug("INSORT***")
            #logging.debug((sortNrx,sortNry))
            return cmp(sortNrx,sortNry)
        
        logging.debug("SORT***")
        logging.debug(self.personsIDForSort) 
        logging.debug("SORT.list")
        logging.debug(personsList) 
        personsList.sort(cmp=sort)
        #for person in persons.keys():
        logging.debug("AFTERSORT***")
        logging.debug(personsList) 
        for person in personsList:
            logging.debug("Check:"+person)
            #TODO: person muss duch den namen von provider geholt werden
            list=[]
            for projectID in persons.get(person):
                list.append(self.getProjectDetails(projectID))
            
            ret[person]=list[0:]
       
        logging.debug("AFTERSORT-ret***")
        logging.debug(ret.keys()) 
        return ret,personsList
    
    def getHistoricalPlacesWithProjectIDs(self):
        """holt die getaggted Personen mit Projekten"""
        HistoricalPlaces = self.HistoricalPlaces2Projects;
        ret={}
        logging.debug("Check HistoricalPlace:"+repr(HistoricalPlaces.keys()))
        for HistoricalPlace in HistoricalPlaces.keys():
            logging.debug("Check:"+HistoricalPlace)
            #TODO: HistoricalPlace muss duch den namen von provuder geholt werden
            list=[]
            for projectID in HistoricalPlaces.get(HistoricalPlace):
                list.append(self.getProjectDetails(projectID))
            
            ret[HistoricalPlace]=list[0:]
        return ret
    
    def getObjectsWithProjectIDs(self):
        """holt die getaggted Personen mit Projekten"""
        objects = self.objects2Projects;
        ret={}
        logging.debug("Check Object:"+repr(objects.keys()))
        for object in objects.keys():
            logging.debug("Check:"+object)
            #TODO: object muss duch den namen von provuder geholt werden
            list=[]
            for projectID in objects.get(object):
                list.append(self.getProjectDetails(projectID))
            
            ret[object]=list[0:]
        return ret
    
    security.declareProtected('View management screens','listAllSuggestedTags')     
    def listAllSuggestedTags(self):
        """list all tags"""
        ret="<html><body>"
        for projectID in self.projectSuggestedTags:
            ret+="<h3>"+projectID+"</h3><table>"
            for tags in self.projectSuggestedTags.get(projectID):
                ret+="<tr><td>"+tags[0]+"</td><td>"+tags[1].type+"</td><td>"+tags[1].comment+"</td><td>"+tags[1].userName+"</td></tr>"
            ret+="</table>"
        ret+="</body></html>"
        logging.debug(ret);
        return ret
    
    security.declareProtected('View management screens','suggestTag')     
    def suggestTag(self,projectID,tagName,tagComment,tagType,tagUserName):
        """suggest a new tag"""
        """ add a person so the project"""
        SuggestedTags=self.getSuggestedTagsFromProject(projectID);
        
        logging.debug("suggested:"+projectID+":"+tagName);
        
     
        #TODO: Im moment noch keine Zuordnung zu einer ID.
        newTag = SuggestedTag(tagName,tagComment,tagType,tagUserName)
        SuggestedTags.insert([tagName,newTag])
        self.projectSuggestedTags.update({projectID:SuggestedTags})
        
       
        
        for SuggestedTagTuple in SuggestedTags:
            #hole die abgespeicherten Projekte zu einer Person
            person=SuggestedTagTuple[0] #hile die ID
            
            
            if getattr(self,"SuggestedTags2Projects",None)==None:
                setattr(self,"SuggestedTags2Projects",OOBTree())
         
            projects = self.SuggestedTags2Projects.get(person,None)
                   
            if projects==None:
                projects=OOSet();
            
            projects.insert(projectID )
            logging.debug("update:"+person)
            self.SuggestedTags2Projects.update({person:projects})
        
        
        
        #retstring = self.getSuggestedTagsFromProjectAsHTML(projectID)
        #logging.debug(retstring)
        
        
        
        #return retstring
        
    security.declareProtected('View management screens','addHistoricalPlaceToProject')     
    def addHistoricalPlaceToProject(self,projectID,value):
        """ add a person so the project"""
        HistoricalPlaces=self.getHistoricalPlacesFromProject(projectID);
        
        logging.debug("adding:"+projectID+":"+value);
        
     
        #TODO: Im moment noch keine Zuordnung zu einer ID.
        HistoricalPlaces.insert([value,value])
        self.projectHistoricalPlaces.update({projectID:HistoricalPlaces})
        
       
        
        for HistoricalPlaceTuple in HistoricalPlaces:
            #hole die abgespeicherten Projekte zu einer Person
            person=HistoricalPlaceTuple[0] #hile die ID
            
            
            if getattr(self,"HistoricalPlaces2Projects",None)==None:
                setattr(self,"HistoricalPlaces2Projects",OOBTree())
         
            projects = self.HistoricalPlaces2Projects.get(person,None)
                   
            if projects==None:
                projects=OOSet();
            
            projects.insert(projectID )
            logging.debug("update:"+person)
            self.HistoricalPlaces2Projects.update({person:projects})
        
        
        
        retstring = self.getHistoricalPlacesFromProjectAsHTML(projectID)
        logging.debug(retstring)
        
        
        
        return retstring
    
    security.declareProtected('View management screens','addObjectToProject')     
    def addObjectToProject(self,projectID,value):
        """ add a person so the project"""
        objects=self.getObjectsFromProject(projectID);
        
        logging.debug("adding:"+projectID+":"+value);
        
     
        #TODO: Im moment noch keine Zuordnung zu einer ID.
        objects.insert([value,value])
        self.projectObjects.update({projectID:objects})
        
       
        
        for objectTuple in objects:
            #hole die abgespeicherten Projekte zu einer Person
            person=objectTuple[0] #hile die ID
            
            
            if getattr(self,"objects2Projects",None)==None:
                setattr(self,"objects2Projects",OOBTree())
         
            projects = self.objects2Projects.get(person,None)
                   
            if projects==None:
                projects=OOSet();
            
            projects.insert(projectID )
            logging.debug("update:"+person)
            self.objects2Projects.update({person:projects})
        
        
        
        retstring = self.getObjectsFromProjectAsHTML(projectID)
        logging.debug(retstring)
        
        
        
        return retstring
    
    
    def getProjectsWithTag(self,tagType,tagName):
        tags = getattr(self,"tags",None)
        if tags is None:
            logging.debug("can't find Tags")
            return []
            
        
    
        tagsStored=tags.get(tagType,None)
        if tagsStored is None:
            logging.debug("can't find Tag:"+tagType)
            return []
        
        projects = tagsStored.get(tagName,None)
        if projects is None:
            logging.debug("can't find any projects for Tag:"+tagName+"("+tagType+")")
            return []
        
        ret=[]
        for project in projects:
            logging.debug("found:"+project)
            ret.append(self.getProjectDetails(project))

        return ret
    
    def getProjectDetails(self,getID):
        project=getattr(self.www_neu.projects,getID,None)
        if project is None:
            logging.debug("no project!! " +getID)
            return
        
        link="/en/research/projects/"+getID
        
        title=project.getContent("short_title")
        #persons=project.getPersonKeyList()
        persons=project.getContent('xdata_01')
        logging.debug("%s %s %s"%(link,title,persons))
        return link,title,persons
    
#        $.post("addNewPerson",{"projectID":
#                     $('#projectID').val(),
#                     "personName":$("#suggest_newPerson").val(),
#                     "personComment":$("#suggest_newPerson_comment").val()}
#               
#              );         
#    
    
    security.declareProtected('View management screens','addAndCreateNewPerson')     
    def addAndCreateNewPerson(self,projectID,personName,personComment,personUserName):
        """adds anew person to the project which is not in the authority file"""
        
        id = random.randint(0, 1000000); 
        idstring="http://ontologies.mpiwg-berlin.mpg.de/tempObjects/person/"+str(id)
        while self.suggestedPersons.has_key(idstring):
            id = random.randint(0, 1000000); 
            #idstring="http://ontologies.mpiwg-berlin.mpg.de/tempObjects/person/"+str(id);
            idstring=self.TEMP_PERSON_NS+str(id);
            
            
        self.suggestedPersons.update({idstring:(personName,personComment,projectID,personUserName)})
        self.createTempPersonInVirtuoso(projectID, idstring, personName, personComment) #TODO: add suername to the triplestore
        return self.addPersonToProject(projectID, idstring, personName);
       
    security.declareProtected('View management screens','createTempPersonInVirtuoso')     
    def createTempPersonInVirtuoso(self,projectID,personURI, personName,personComment):
        """add a new person to the triple store of tempory persons"""
        triples=[];
        personID="<"+personURI+">"
        triples.append((personID,"<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>","<http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/Person>"))
        triples.append((personID,"<http://erlangen-crm.org/110404/P3_has_note>",'"'+personComment+'"'))
        triples.append((personID,"<http://xmlns.com/foaf/0.1/name>",'"'+personName+'"'))
        triples.append((personID,"<http://ontologies.mpiwg-berlin.mpg.de/general/isMentionedOn>","<http://www.mpiwg-berlin.mpg.de/en/research/projects/"+projectID+">"))
        
        cmdString ="insert in GRAPH <"+self.virtuosoGraph+">  {"
        for triple in triples:
            cmdString+=" ".join(triple)+"."
        
        cmdString+="}"

        auth_handler = urllib2.HTTPBasicAuthHandler()
        auth_handler.add_password(realm='sparql',
                          uri=self.virtuosoServer+"/sparql",
                          user=self.virtuosoDAVUser,
                          passwd=self.virtuosoDAVPW)
                          
        opener = urllib2.build_opener(auth_handler)
        opener.addheaders = [('Content-Type','application/sparql-query')]
        
        logging.debug(cmdString)
        try:
            r= opener.open(self.virtuosoServer+"/sparql", urllib.urlencode({'query':cmdString,'default-graph-uri':self.virtuosoGraph,'named-graph-uri':None}))
            logging.debug(r.read())
        except urllib2.URLError, e:
            logging.error(e.code)
            logging.error(e.read())
            return
        
        opener2 = urllib2.build_opener()
        
       
        personIDencoded=urllib.quote(personURI,safe="")
        graphEncoded=urllib.quote(self.additionalNamesGraphURL,safe="")
        url = self.personproviderURL+"id/"+personIDencoded+"/"+graphEncoded
         
        try:
            request = urllib2.Request(url);
            request.get_method = lambda : 'PUT'
            r= opener2.open(request);
            logging.debug(r.read())
        except urllib2.URLError, e:
            logging.error(e.code)
            logging.error(e.read())
            return
        
                    
       
        return
        
def redirect(RESPONSE,url):
        """mache ein redirect mit einem angehaengten time stamp um ein reload zu erzwingen"""
        
        timeStamp=time.time()
        
        if url.find("?")>-1: #giebt es schon parameter
            addStr="&time=%s"
        else:
            addStr="?time=%s"
            
        RESPONSE.setHeader('Last-Modified',email.Utils.formatdate().split("-")[0]+'GMT')
        logging.error(email.Utils.formatdate()+' GMT')
        RESPONSE.redirect(url+addStr%timeStamp)
        
def manage_addMPIWGThesaurusForm(self):
    """form for adding the project"""
    pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMPIWGThesaurusForm.zpt')).__of__(self)
    return pt()



def manage_addMPIWGThesaurus(self,id,RESPONSE=None):
    """add it"""
    newObj=MPIWGThesaurus(id)

    self._setObject(id,newObj)


    if RESPONSE is not None:
        redirect(RESPONSE,'manage_main')