# $Id: manage.py,v 1.4 2003/04/17 14:17:08 em Exp $

from rdflib.TripleStore import TripleStore
from rdflib.URIRef import URIRef
from rdflib.Literal import Literal
from rdflib.BNode import BNode
from rdflib.Namespace import Namespace
from rdflib.constants import TYPE, VALUE, RDFS_LABEL

# header and footer information

header = 'header.inc.html'
footer = 'footer.inc.html'

# Create namespace objects associated with managing use cases

UCV = Namespace("http://www.w3.org/2001/sw/EO/usecases/vocab#")
DC = Namespace("http://purl.org/dc/elements/1.1/")

# some utility functions

def first(sequence):
    first = None        
    for result in sequence:
        first = first or result
        if isinstance(result, Literal):
            return first

def defaultLiteral(s):
    if store.objects(s, RDFS_LABEL):
        default = first(store.objects(s, RDFS_LABEL))
        if (default != None):
            return default
    if store.objects(s, VALUE):
        default = first(store.objects(s, VALUE))
        if (default != None):
            return default
    return s

def printObject(s, p):
    if s!= None or p!=None:
        for o in store.objects(s, p):
            if isinstance(o, Literal):
                print o,
            else:
                print defaultLiteral(o),
            print ","

def printFile( file_name ):
	file = open( file_name , "r")
	for line in file.readlines():
		print line,
	file.close();


# byProject routines

def printProject(project):

    print '<table summary="project-summary" class="project" width="100%" cellpadding="5" border="0">'

    print '<tr><td valign="top" class = "attribute">Title:</td><td class = "value">'
    printObject(project, DC["title"])
    print '</td></tr>'
    
    print '<tr><td valign="top" class = "attribute">Contact:</td><td class = "value">'
    printObject(project, UCV["contact"])
    print '</td></tr>'

    print '<tr><td valign="top" class = "attribute">Developed By:</td><td class = "value">'
    printObject(project, UCV["developedBy"])
    print '</td></tr>'

    print '<tr><td valign="top" class = "attribute">Subject:</td><td class = "value">'
    printObject(project, DC["subject"])
    print '</td></tr>'
    
    print '<tr><td valign="top" class = "attribute">Description:</td><td class = "value">'
    printObject(project, DC["description"])
    print '</td></tr>'
    
    print '</table>'
    
    print '<hr />'


def printProjects():

    #For each ucv:Project in the store print out the relavant information

    for project in store.subjects(TYPE, UCV["Project"]):

        printProject(project)


# byTopic routines

# byDomain routines



# Main routines

store = TripleStore()
                    
store.load("uc.rdf")

printFile(header)
printProjects()
printFile(footer)

# Serialize the store as RDF/XML to temp file
store.save("dump.rdf")

