package org.gbif.registry.metadata;

import org.gbif.api.model.registry.Citation;
import org.gbif.api.model.registry.Contact;
import org.gbif.api.model.registry.Dataset;
import org.gbif.api.model.registry.Organization;
import org.gbif.api.model.registry.eml.KeywordCollection;
import org.gbif.api.model.registry.eml.geospatial.BoundingBox;
import org.gbif.api.model.registry.eml.geospatial.GeospatialCoverage;
import org.gbif.api.model.registry.eml.temporal.DateRange;
import org.gbif.api.model.registry.eml.temporal.TemporalCoverage;
import org.gbif.api.vocabulary.ContactType;
import org.gbif.api.vocabulary.Language;

import java.io.StringWriter;
import java.net.URI;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.google.common.base.CharMatcher;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * Test class for DublinCoreWriter.
 *
 * @author cgendreau
 */
public class DublinCoreWriterTest {

  //take if from the test/resources folder since the live one was down for days in April 2016
  //http://www.openarchives.org/OAI/2.0/oai_dc.xsd
  private static final String OAI_2_0_DC_SCHEMA = "xsd/oai_dc.xsd";

  @Test
  public void testWrite() throws Exception {
    Dataset d = new Dataset();
    d.setKey(UUID.fromString("bdd601cc-00a7-431c-9724-d5b03170fcb2"));
    //d.setDoi(new DOI("10.1234/5679"));
    d.setTitle("This is a keyboard dataset");
    d.setDataLanguage(Language.FRENCH);

    Calendar calendar = Calendar.getInstance();
    calendar.set(2015, 8, 14);
    d.setPubDate(calendar.getTime());
    d.setDescription("Description d'un dataset sur les claviers qwerty");

    d.setHomepage(URI.create("http:///www.gbif.org"));
    d.setPurpose("Le but de ce dataset est de ...");
    List<KeywordCollection> listKeyWordColl = Lists.newArrayList();
    KeywordCollection keyword = new KeywordCollection();
    keyword.addKeyword("keyboard");
    keyword.addKeyword("qwerty");
    listKeyWordColl.add(keyword);
    d.setKeywordCollections(listKeyWordColl);

    //try additional properties
    Map<String, Object> additionalProperties = Maps.newHashMap();
    additionalProperties.put(DublinCoreWriter.ADDITIONAL_PROPERTY_OCC_COUNT, 3l);
    additionalProperties.put(DublinCoreWriter.ADDITIONAL_PROPERTY_DC_FORMAT, "application/dwca+zip");

    List<Citation> citationList = Lists.newArrayList();
    Citation citation = new Citation();
    citation.setText("Qwerty U (2015). The World Register of Keyboards. 2015-08-09");
    citationList.add(citation);
    d.setBibliographicCitations(citationList);

    Contact originatorContact = new Contact();
    originatorContact.setFirstName("Carey");
    originatorContact.setLastName("Price");
    originatorContact.setType(ContactType.ORIGINATOR);
    originatorContact.setPrimary(true);

    //add the same name twice sith a different ContactType to make sure it will appear only once at the end
    Contact metadataAuthorContact = new Contact();
    metadataAuthorContact.setFirstName("Carey");
    metadataAuthorContact.setLastName("Price");
    metadataAuthorContact.setType(ContactType.METADATA_AUTHOR);
    metadataAuthorContact.setPrimary(true);

    // ADMINISTRATIVE_POINT_OF_CONTACT should be displayed first in the generated DublinCore document
    Contact administrativeContact = new Contact();
    administrativeContact.setFirstName("Patrick");
    administrativeContact.setLastName("Roy");
    administrativeContact.setType(ContactType.ADMINISTRATIVE_POINT_OF_CONTACT);

    d.setContacts(Lists.newArrayList(originatorContact, metadataAuthorContact, administrativeContact));

    d.setGeographicCoverageDescription("Description de la portée");
    Date endDate = calendar.getTime();
    //subtract a day
    calendar.roll(Calendar.DAY_OF_MONTH, false);
    Date startDate = calendar.getTime();
    TemporalCoverage tp = new DateRange(startDate, endDate);
    d.setTemporalCoverages(Lists.newArrayList(tp));

    d.setGeographicCoverageDescription("Plusieurs pays");
    GeospatialCoverage geospatialCoverage = new GeospatialCoverage();
    geospatialCoverage.setBoundingBox(new BoundingBox(-10, 10, 20, -20));
    geospatialCoverage.setDescription("Defined by bounding box");
    d.setGeographicCoverages(Lists.newArrayList(geospatialCoverage));

    Organization organization = new Organization();
    organization.setTitle("Qwerty U");

    StringWriter writer = new StringWriter();
    DublinCoreWriter.newInstance().writeTo(organization, d, additionalProperties, writer);

    //Load test file
    String expectedContent = FileUtils.readFileToString(org.gbif.utils.file.FileUtils.getClasspathFile("dc/qwerty_dc.xml"));

    //compare without the whitespace characters
    String expectedFileContent = CharMatcher.WHITESPACE.removeFrom(expectedContent);
    String actualFileContent = CharMatcher.WHITESPACE.removeFrom(writer.toString());

    assertEquals(expectedFileContent, actualFileContent);


    // ensure we have a valid XML file according to the schema
    XMLValidator.assertXMLAgainstXSD(writer.toString(), org.gbif.utils.file.FileUtils.classpath2Filepath(OAI_2_0_DC_SCHEMA));

  }

}

@@ -1846,17 +1846,16 @@
 %22http://
-/
 www.gbif

Fix very old typo