###############################################################################
##                                                                           ##
##                        ALEXANDRIA DIGITAL LIBRARY                         ##
##                 University of California at Santa Barbara                 ##
##                                                                           ##
## ------------------------------------------------------------------------- ##
##                                                                           ##
##     Copyright (c) 2005 by the Regents of the University of California     ##
##                            All rights reserved                            ##
##                                                                           ##
## Redistribution and use in source and binary forms, with or without        ##
## modification, are permitted provided that the following conditions are    ##
## met:                                                                      ##
##                                                                           ##
##     1. Redistributions of source code must retain the above copyright     ##
##        notice, this list of conditions, and the following disclaimer.     ##
##                                                                           ##
##     2. Redistributions in binary form must reproduce the above copyright  ##
##        notice, this list of conditions, and the following disclaimer in   ##
##        the documentation and/or other materials provided with the         ##
##        distribution.                                                      ##
##                                                                           ##
##     3. All advertising materials mentioning features or use of this       ##
##        software must display the following acknowledgement: This product  ##
##        includes software developed by the Alexandria Digital Library,     ##
##        University of California at Santa Barbara, and its contributors.   ##
##                                                                           ##
##     4. Neither the name of the University nor the names of its            ##
##        contributors may be used to endorse or promote products derived    ##
##        from this software without specific prior written permission.      ##
##                                                                           ##
## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY ##
## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ##
## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE   ##
## DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR  ##
## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL    ##
## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS   ##
## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)     ##
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,       ##
## STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN  ##
## ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           ##
## POSSIBILITY OF SUCH DAMAGE.                                               ##
##                                                                           ##
###############################################################################

# $Header: /export/home/gjanee/mm/RCS/ADN_mapping.py,v 1.3 2005/06/08 17:26:29 gjanee Exp $

# DESCRIPTION
#
#     This is a mapping from the ADN (ADEPT/DLESE/NASA) metadata
#     framework version 0.6.50, as it has been interpreted and used by
#     the DLESE collections, to the ADL bucket, browse, and access
#     metadata views.
#
#     The ADN framework is defined at
#     <http://www.dlese.org/Metadata/adn-item/index.htm>.  See
#     <http://www.alexandria.ucsb.edu/~gjanee/archive/2003/adn-mapping.html>
#     for more information on the mapping.
#
#     See <http://www.alexandria.ucsb.edu/~gjanee/mm/tutorial.html>
#     for instructions on how to invoke this mapping.
#
# AUTHOR
#
#     Greg Janee
#     gjanee@alexandria.ucsb.edu
#
# HISTORY
#
#     $Log: ADN_mapping.py,v $
#     Revision 1.3  2005/06/08 17:26:29  gjanee
#     Added support for XML namespaces.
#
#     Revision 1.2  2005/03/10 21:37:42  gjanee
#     Minor improvements.
#
#     Revision 1.1  2005/02/11 01:01:38  gjanee
#     Initial revision
#

from ADL_mapper import *
input()

import ADL_buckets
import DLESE_buckets

TAG = "tag:adn.dlese.org,2003:"

namespace("A", "http://adn.dlese.org")

# ----------------------------------------
# ADL:TITLES

map("adl:titles",
    "/A:itemRecord/A:general/A:title",
    ("[ADN] Title", TAG+"Title"))

# ----------------------------------------
# ADL:GEOGRAPHIC-LOCATIONS

GEOCOVERAGE = "/A:itemRecord/A:geospatialCoverages/A:geospatialCoverage/"

map("adl:geographic-locations",
    [GEOCOVERAGE+"A:boundBox", "A:northCoord", "A:southCoord", "A:eastCoord",
     "A:westCoord"],
    ("[ADN] Bounding box", TAG+"Bounding_box"))

# ----------------------------------------
# ADL:DATES

def fltConvertPresent (v):
    if v[1].lower() == "present":
        return (v[0], "9999")
    else:
        return v

TIMECOVERAGE = "/A:itemRecord/A:temporalCoverages/A:timeAndPeriod/"

map("adl:dates",
    [TIMECOVERAGE+"A:timeInfo/A:timeAD", "A:begin@date", "A:end@date"],
    ("[ADN] Time AD", TAG+"Time_AD"),
    prefilters=fltConvertPresent)

# ----------------------------------------
# ADL:FORMATS

mapConstant("adl:formats",
    ("ADL Object Formats", "Online"))

# ----------------------------------------
# ADL:ASSIGNED-TERMS

def fltStripDlesePrefix0 (v):
    if v[0][:6] == "DLESE:":
        return (v[0][6:],)
    else:
        return v

def fltRemoveEscapeTerms0 (v):
    if v[0].lower() == "other" or v[0].lower() == "to be supplied":
        return None
    else:
        return v

map("adl:assigned-terms",
    "/A:itemRecord/A:general/A:subjects/A:subject",
    ("[ADN] Subject", TAG+"Subject"),
    prefilters=[fltStripDlesePrefix0, fltRemoveEscapeTerms0])

map("adl:assigned-terms",
    "/A:itemRecord/A:general/A:keywords/A:keyword",
    ("[ADN] Keyword", TAG+"Keyword"))

consolidateTextualValues("adl:assigned-terms")

# ----------------------------------------
# ADL:SUBJECT-RELATED-TEXT

map("adl:subject-related-text",
    "/A:itemRecord/A:general/A:description",
    ("[ADN] Description", TAG+"Description"))

if len(get(GEOCOVERAGE+"A:detGeos/A:detGeo/A:detPlaces/A:place/A:name")) > 0:
    map("adl:subject-related-text",
        GEOCOVERAGE+"A:detGeos/A:detGeo/A:detPlaces/A:place/A:name",
        ("[ADN] Placename", TAG+"Placename"),
        id=1)
else:
    map("adl:subject-related-text",
        GEOCOVERAGE+"A:boundBox/A:bbPlaces/A:place/A:name",
        ("[ADN] Placename", TAG+"Placename"),
        id=2)

if len(get(GEOCOVERAGE+"A:detGeos/A:detGeo/A:detEvents/A:event/A:name")) > 0:
    map("adl:subject-related-text",
        GEOCOVERAGE+"A:detGeos/A:detGeo/A:detEvents/A:event/A:name",
        ("[ADN] Event name", TAG+"Event_name"),
        id=3)
else:
    map("adl:subject-related-text",
        GEOCOVERAGE+"A:boundBox/A:bbEvents/A:event/A:name",
        ("[ADN] Event name", TAG+"Event_name"),
        id=4)

map("adl:subject-related-text",
    TIMECOVERAGE+"A:periods/A:period/A:name",
    ("[ADN] Named time period", TAG+"Named_time_period"))

consolidateTextualValues("adl:subject-related-text")

# ----------------------------------------
# ADL:ORIGINATORS

def fltProcessContributorName (v):
    (organization, first, middle, last, institution) = v
    if organization != None:
        return (organization,)
    else:
        name = ""
        if first != None: name = first
        if middle != None:
            if name != "": name += " "
            name += middle
        if last != None:
            if name != "": name += " "
            name += last
        if institution != None:
            if name != "": name += ", "
            name += institution
        return (name,)

CONTRIBUTOR = "/A:itemRecord/A:lifecycle/A:contributors/A:contributor"

map("adl:originators",
    [CONTRIBUTOR+"[@role='Author']", "A:organization/A:instName",
     "A:person/A:nameFirst", "A:person/A:nameMiddle", "A:person/A:nameLast",
     "A:person/A:instName"],
    ("[ADN] Author", TAG+"Author"),
    prefilters=fltProcessContributorName)

map("adl:originators",
    [CONTRIBUTOR+"[@role='Publisher']", "A:organization/A:instName",
     "A:person/A:nameFirst", "A:person/A:nameMiddle", "A:person/A:nameLast",
     "A:person/A:instName"],
    ("[ADN] Publisher", TAG+"Publisher"),
    prefilters=fltProcessContributorName)

# ----------------------------------------
# ADL:IDENTIFIERS

map("adl:identifiers",
    ["/A:itemRecord/A:metaMetadata/A:catalogEntries/A:catalog@entry",
     "=DLESE"],
    ("[ADN] Identification number", TAG+"Identification_number"))

# ----------------------------------------
# DLESE:GRADE-RANGES

def fltStripDlesePrefix1 (v):
    if v[1][:6] == "DLESE:":
        return (v[0], v[1][6:])
    else:
        return v

def fltRemoveEscapeTerms1 (v):
    if v[1].lower() == "to be supplied" or v[1].lower() == "not applicable":
        return None
    else:
        return v

map("dlese:grade-ranges",
    ["=DLESE Grade Ranges",
     "/A:itemRecord/A:educational/A:audiences/A:audience/A:gradeRange"],
    ("[ADN] Grade range", TAG+"Grade_range"),
    prefilters=[fltStripDlesePrefix1, fltRemoveEscapeTerms1])

# ----------------------------------------
# DLESE:RESOURCE-TYPES

def fltStripColonPrefix (v):
    i = v[1].rfind(":")
    if i >= 0:
        return (v[0], v[1][i+1:])
    else:
        return v

map("dlese:resource-types",
    ["=DLESE Resource Types",
     "/A:itemRecord/A:educational/A:resourceTypes/A:resourceType"],
    ("[ADN] Resource type", TAG+"Resource_type"),
    prefilters=[fltStripColonPrefix, fltRemoveEscapeTerms1])

# ----------------------------------------
# REQUIREMENTS & EXPECTATIONS

requirement("adl:titles", "1+")

expectation("adl:subject-related-text", "1+")
expectation("adl:identifiers", "1+")

output()
