/*  eds_holdings.go - Convert Google Scholar XML to EBSCO Discovery Service holdings 
    Copyright (C) 2012 Dan Scott <dscott@laurentian.ca>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package main

import "encoding/xml"
import "fmt"
import "io/ioutil"

type Coverage struct {
	FromYear   int    `xml:"from>year"`
	FromVolume int    `xml:"from>volume"`
	FromIssue  string `xml:"from>issue"`
	ToYear     int    `xml:"to>year"`
	ToVolume   int    `xml:"to>volume"`
	ToIssue    string `xml:"to>issue"`
}

type Result struct {
	XMLName   xml.Name   `xml:"item"`
	Id        int        `xml:"sfx_id"`
	Type      string     `xml:"object_type"`
	Titles    []string   `xml:"title"`
	ISSN      string     `xml:"issn"`
	EISSN     string     `xml:"eissn"`
	Coverages []Coverage `xml:"coverage"`
}

type Holding struct {
	XMLName xml.Name `xml:"institutional_holdings"`
	Results []Result `xml:"item"`
}

func getCoverageYears(covers []Coverage) (fromY int, toY int) {
	fromY = 9999
	toY = -1
	current := 0
	for _, cover := range covers {
		if cover.FromYear < fromY {
			fromY = cover.FromYear
		}
		if cover.ToYear == 0 {
			current = 1
		}
		if cover.ToYear > toY {
			toY = cover.ToYear
		}
	}
	if fromY == 9999 {
		fromY = -1
	}
	/* A current subscription overrides everything */
	if current == 1 {
		toY = -1
	}
	return fromY, toY
}

func main() {
	v := Holding{}
	/*
		data := `
	<institutional_holdings>
	 <item type="electronic">
	  <sfx_id>954921332001</sfx_id>
	  <object_type>JOURNAL</object_type>
	  <title>Publishers weekly</title>
	  <issn>0000-0019</issn>
	  <eissn>2150-4008</eissn>
	  <coverage>
	   <from>
	    <year>1997</year>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1997</year>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1995</year>
	    <issue>48</issue>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1990</year>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1990</year>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1990</year>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>2000</year>
	   </from>
	   <to></to>
	  </coverage>
	 </item>
	 <item type="electronic">
	  <sfx_id>954921332003</sfx_id>
	  <object_type>JOURNAL</object_type>
	  <title>Communications of the ACM</title>
	  <title>ASSOCIATION FOR COMPUTING MACHINERY COMMUNICATIONS OF THE ACM</title>
	  <title>COMMUNICATIONS OF THE ASSOCIATION FOR COMPUTING MACHINERY</title>
	  <title>ASSOCIATION FOR COMPUTING MACHINERY COMMUNICATIONS</title>
	  <issn>0001-0782</issn>
	  <eissn>1557-7317</eissn>
	  <coverage>
	   <from>
	    <year>1958</year>
	    <volume>1</volume>
	    <issue>1</issue>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1999</year>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1987</year>
	   </from>
	   <to>
	    <year>2001</year>
	   </to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1987</year>
	   </from>
	   <to>
	    <year>2001</year>
	   </to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1958</year>
	    <volume>1</volume>
	    <issue>1</issue>
	   </from>
	   <to>
	    <year>2011</year>
	    <volume>54</volume>
	    <issue>9</issue>
	   </to>
	  </coverage>
	 </item>
	 <item type="electronic">
	  <sfx_id>954921332004</sfx_id>
	  <object_type>JOURNAL</object_type>
	  <title>Planning</title>
	  <title>Planning (U.S.)</title>
	  <title>Planning (U.K.)</title>
	  <issn>0001-2610</issn>
	  <eissn>0001-2510</eissn>
	  <coverage>
	   <from>
	    <year>1993</year>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1993</year>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1987</year>
	   </from>
	   <to></to>
	  </coverage>
	 </item>
	 <item type="electronic">
	  <sfx_id>954921333005</sfx_id>
	  <object_type>JOURNAL</object_type>
	  <title>Abacus</title>
	  <title>ABACUS A JOURNAL OF ACCOUNTING FINANCE AND BUSINESS STUDIES</title>
	  <title>ABACUS OXFORD</title>
	  <issn>0001-3072</issn>
	  <eissn>1467-6281</eissn>
	  <coverage>
	   <from>
	    <year>1997</year>
	    <volume>33</volume>
	    <issue>1</issue>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1965</year>
	   </from>
	   <to></to>
	   <embargo>
	    <days_not_available>365</days_not_available>
	   </embargo>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1997</year>
	    <volume>33</volume>
	    <issue>1</issue>
	   </from>
	   <to>
	    <year>2012</year>
	    <volume>48</volume>
	    <issue>1</issue>
	   </to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1965</year>
	    <volume>1</volume>
	    <issue>1</issue>
	   </from>
	   <to></to>
	  </coverage>
	  <coverage>
	   <from>
	    <year>1997</year>
	   </from>
	   <to></to>
	  </coverage>
	 </item>
	</institutional_holdings>
	`
	*/

	file, err := ioutil.ReadFile("institutional_holding-laurentian.xml")
	err = xml.Unmarshal(file, &v)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
	for _, result := range v.Results {
		/* EBSCO says an ISSN is necessary, but this wipes out 56,000 results */
		if result.EISSN == "" && result.ISSN == "" {
			break
		}
		fmt.Printf("%s\t", result.Titles[0])
		if result.EISSN != "" {
			fmt.Printf("%s\t", result.EISSN)
		} else {
			fmt.Printf("%s\t", result.ISSN)
		}
		fromY, toY := getCoverageYears(result.Coverages)
		if fromY > 0 {
			fmt.Printf("%04d\t", fromY)
		} else {
			fmt.Printf("\t")
		}
		if toY > 0 {
			fmt.Printf("%04d\n", toY)
		} else {
			fmt.Printf("\n")
		}
	}
}
