# -*- coding: UTF-8 -*-

#from xml.dom.minidom import Document
import cPickle
import time
import os
import string
from Lists import Lists
from PCont import PCont


class rdfDc:

    def __init__(self,filename):
        self.pcont = PCont(filename)
        self.doc = self.pcont.doc

    def createUpload(self,data):
        lists = Lists()
        lists.loadLang()
        lists.loadCountries()
        rdfdc = self.doc.createElement("rdf:RDF")
        rdfdc.setAttribute("xmlns:rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#")
        rdfdc.setAttribute("xmlns:dc","http://purl.org/dc/elements/1.1/")
        self.doc.appendChild(rdfdc)
        rdfdesc = self.doc.createElement("rdf:Description")
        rdfdc.appendChild(rdfdesc)

        dc__title = self.doc.createElement("dc:title")
        dc__title_value = self.doc.createTextNode(data.dc__title.GetValue())
        dc__title.appendChild(dc__title_value);
        rdfdesc.appendChild(dc__title)

        dc__title__alternative = self.doc.createElement("dc:title:alternative")
        dc__title__alternative_value = self.doc.createTextNode(data.dc__title__alternative.GetValue())
        dc__title__alternative.appendChild(dc__title__alternative_value);
        rdfdesc.appendChild(dc__title__alternative)

        dc__date__created = self.doc.createElement("dc:date:created")
        dc__date__created_value = self.doc.createTextNode("%04d-%02d-%02d"
            %
            (
                data.date_yy.GetValue(),
                data.date_mm.GetValue(),
                data.date_dd.GetValue()
            )
        )
        dc__date__created.appendChild(dc__date__created_value);
        rdfdesc.appendChild(dc__date__created)
        
        dc__identifier = self.doc.createElement("dc:identifier")
        if (os.name == "nt"):
            l=data.dc__identifier.split('\\')
            data.dc__identifier = l[-1]
        else:
            data.dc__identifier = os.path.basename(data.dc__identifier)

        data.dc__identifier = data.dc__identifier.encode('utf8')
        data.dc__identifier = data.dc__identifier.translate(
                          string.maketrans(
                          ' \t\n\r\v\f!"#$%&\'()*+,-/:;<=>?@[\]^`{|}~',36*'_'))
        dc__identifier_value = self.doc.createTextNode(data.dc__identifier)
        dc__identifier.appendChild(dc__identifier_value);
        rdfdesc.appendChild(dc__identifier)

        dc__rights = self.doc.createElement("dc:rights")
        dc__rights_value = self.doc.createTextNode(data.dc__rights.GetValue())
        dc__rights.appendChild(dc__rights_value);
        rdfdesc.appendChild(dc__rights)

        dc__rights__license = self.doc.createElement("dc:rights:license")
        dc__rights__license_value = self.doc.createTextNode(data.dc__rights__license.GetValue())
        dc__rights__license.appendChild(dc__rights__license_value);
        rdfdesc.appendChild(dc__rights__license)

        dc__subject = self.doc.createElement("dc:subject")
        dc__subject_value = self.doc.createTextNode(data.dc__subject.GetValue())
        dc__subject.appendChild(dc__subject_value);
        rdfdesc.appendChild(dc__subject)

        dc__type = self.doc.createElement("dc:type")
        dc__type_value = self.doc.createTextNode(data.dc__type.GetValue())
        dc__type.appendChild(dc__type_value);
        rdfdesc.appendChild(dc__type)

#        dc__coverage = self.doc.createElement("dc:coverage")
#        dc__coverage_value = self.doc.createTextNode(data.dc__coverage.GetValue())
#        dc__coverage.appendChild(dc__coverage_value);
#        rdfdesc.appendChild(dc__coverage)

        dc__coverage__temporal = self.doc.createElement("dc:coverage:temporal")
        dc__coverage__temporal_value = self.doc.createTextNode(data.dc__coverage__temporal.GetValue())
        dc__coverage__temporal.appendChild(dc__coverage__temporal_value);
        rdfdesc.appendChild(dc__coverage__temporal)

        dc__coverage__spatial = self.doc.createElement("dc:coverage:spatial")
        dc__coverage__spatial_value = self.doc.createTextNode(lists.country[data.dc__coverage__spatial.GetValue()])
        dc__coverage__spatial.appendChild(dc__coverage__spatial_value);
        rdfdesc.appendChild(dc__coverage__spatial)

        dc__language = self.doc.createElement("dc:language")
        dc__language_value = self.doc.createTextNode(lists.lang[data.dc__language.GetValue()])
        dc__language.appendChild(dc__language_value);
        rdfdesc.appendChild(dc__language)

        dc__description__tableOfContent = self.doc.createElement("dc:description:tableOfContent")
        dc__description__tableOfContent_value = self.doc.createTextNode(data.dc__description__tableOfContent.GetValue())
        dc__description__tableOfContent.appendChild(dc__description__tableOfContent_value);
        rdfdesc.appendChild(dc__description__tableOfContent)

        dc__description__abstract = self.doc.createElement("dc:description:abstract")
        dc__description__abstract_value = self.doc.createTextNode(data.dc__description__abstract.GetValue())
        dc__description__abstract.appendChild(dc__description__abstract_value);
        rdfdesc.appendChild(dc__description__abstract)

        dc__description__long = self.doc.createElement("dc:description:long")
        dc__description__long_value = self.doc.createTextNode(data.dc__description__long.GetValue())
        dc__description__long.appendChild(dc__description__long_value);
        rdfdesc.appendChild(dc__description__long)

        dc__format__encoding = self.doc.createElement("dc:format:encoding")
        dc__format__encoding_value = self.doc.createTextNode(data.dc__format__encoding)
        dc__format__encoding.appendChild(dc__format__encoding_value);
        rdfdesc.appendChild(dc__format__encoding)

        dc__format__extent = self.doc.createElement("dc:format:extent")
        dc__format__extent_value = self.doc.createTextNode(data.dc__format__extent)
        dc__format__extent.appendChild(dc__format__extent_value);
        rdfdesc.appendChild(dc__format__extent)

        dc__creator = self.doc.createElement("dc:creator")
        dc__creator_value = self.doc.createTextNode(data.dc__creator.GetValue())
        dc__creator.appendChild(dc__creator_value);
        dc__creator.setAttribute('role',data.dc__creator__role.GetValue());
        rdfdesc.appendChild(dc__creator)

        dc__creator = self.doc.createElement("dc:contributor")
        dc__creator_value = self.doc.createTextNode(data.username.GetValue())
        dc__creator.appendChild(dc__creator_value);
        dc__creator.setAttribute('role','Importer');
        rdfdesc.appendChild(dc__creator)

        dc__contributor = self.doc.createElement("dc:contributor")
        dc__contributor_value = self.doc.createTextNode(data.dc__contributor.GetValue())
        dc__contributor.appendChild(dc__contributor_value);
        dc__contributor.setAttribute('role',data.dc__contributor__role.GetValue());
        rdfdesc.appendChild(dc__contributor)

    def pickle(self):
#        print self.doc.toprettyxml(indent="  ")
        jobid = int(time.time())
        FILE = open("spool/jobs_%d.pickle" % jobid, 'w')
        cPickle.dump(self.pcont, FILE)
        FILE.close()
        return(jobid)
