/*
 * Copyright (C) 2005-2010 Alfresco Software Limited.
 *
 * This file is part of Alfresco
 *
 * Alfresco is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see .
 */
package org.alfresco.repo.rendition.executer;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.model.filefolder.FileFolderServiceImpl;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.test_category.BaseSpringTestsCategory;
import org.alfresco.util.BaseAlfrescoSpringTest;
import org.alfresco.util.GUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.experimental.categories.Category;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
/**
 * @author Brian
 *
 */
@Category(BaseSpringTestsCategory.class)
public class XSLTFunctionsTest extends BaseAlfrescoSpringTest
{
    private final static Log log = LogFactory.getLog(XSLTFunctionsTest.class);
    private XSLTFunctions xsltFunctions;
    private SearchService searchService;
    private NodeRef companyHome;
    private FileFolderService fileFolderService;
    /* (non-Javadoc)
     * @see org.alfresco.util.BaseAlfrescoSpringTest#onSetUpInTransaction()
     */
    @SuppressWarnings("deprecation")
    @Override
    protected void onSetUpInTransaction() throws Exception
    {
        super.onSetUpInTransaction();
        this.searchService = (SearchService) this.applicationContext.getBean("SearchService");
        this.xsltFunctions = (XSLTFunctions) this.applicationContext.getBean("xsltFunctions");
        this.nodeService = (NodeService) this.applicationContext.getBean("NodeService");
        this.contentService = (ContentService) this.applicationContext.getBean("ContentService");
        this.fileFolderService = (FileFolderService) this.applicationContext.getBean("FileFolderService");
        ResultSet rs = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_XPATH, "/app:company_home");
        this.companyHome = rs.getNodeRef(0);
    }
    public void testSimplestParseXMLDocument()
    {
        FileInfo file = createXmlFile(companyHome);
        try
        {
            Document doc = xsltFunctions.parseXMLDocument(companyHome, file.getName());
            NodeList foodNodes = doc.getElementsByTagName("food");
            assertEquals(10, foodNodes.getLength());
        }
        catch (Exception ex)
        {
            log.error("Error!", ex);
            fail(ex.getMessage());
        }
    }
    public void testPathParseXMLDocument()
    {
        String path = "path/to/xml/files";
        List pathElements = Arrays.asList(path.split("/"));
        FileInfo folder = FileFolderServiceImpl.makeFolders(fileFolderService, companyHome, pathElements, ContentModel.TYPE_FOLDER);
        FileInfo file = createXmlFile(folder.getNodeRef());
        
        try
        {
            Document doc = xsltFunctions.parseXMLDocument(companyHome, path + "/" + file.getName());
            NodeList foodNodes = doc.getElementsByTagName("food");
            assertEquals(10, foodNodes.getLength());
        }
        catch (Exception ex)
        {
            log.error("Error!", ex);
            fail(ex.getMessage());
        }
    }
    
    public void testParseXMLDocuments()
    {
        String path = "path/to/xml/files";
        List pathElements = Arrays.asList(path.split("/"));
        FileInfo folder = FileFolderServiceImpl.makeFolders(fileFolderService, companyHome, pathElements, ContentModel.TYPE_FOLDER);
        FileInfo file1 = createXmlFile(folder.getNodeRef());
        FileInfo file2 = createXmlFile(folder.getNodeRef());
        FileInfo file3 = createXmlFile(folder.getNodeRef());
        FileInfo file4 = createXmlFile(folder.getNodeRef());
        FileInfo file5 = createXmlFile(folder.getNodeRef());
        
        try
        {
            Map xmlFileMap = xsltFunctions.parseXMLDocuments("cm:content", 
                    companyHome, "/" + path);
            assertEquals(5, xmlFileMap.size());
            Set names = new TreeSet(Arrays.asList(new String[] {file1.getName(), file2.getName(), 
                    file3.getName(), file4.getName(), file5.getName()}));
            names.removeAll(xmlFileMap.keySet());
            assertEquals(0, names.size());
            NodeList foodNodes = xmlFileMap.get(file3.getName()).getElementsByTagName("food");
            assertEquals(10, foodNodes.getLength());
        }
        catch (Exception ex)
        {
            log.error("Error!", ex);
            fail(ex.getMessage());
        }
    }
    
    /**
     * 
     */
    private FileInfo createXmlFile(NodeRef folder)
    {
        String name = GUID.generate();
        FileInfo testXmlFile = fileFolderService.create(folder, name + ".xml", ContentModel.TYPE_CONTENT);
        ContentWriter writer = contentService.getWriter(testXmlFile.getNodeRef(), ContentModel.PROP_CONTENT, true);
        writer.setMimetype("text/xml");
        writer.setEncoding("UTF-8");
        writer.putContent(sampleXML);
        return testXmlFile;
    }
    
    private String sampleXML = "" +
    "" +
    "" +
        "65" +
        "20" +
        "300" +
        "2400" +
        "300" +
        "25" +
        "50" +
    "" +
    "" +
        "Avocado Dip" +
        "Sunnydale" +
        "29" +
        "" +
        "11" +
        "3" +
        "5" +
        "210" +
        "2" +
        "0" +
        "1" +
        "" +
            "0" +
            "0" +
        "" +
        "" +
            "0" +
            "0" +
        "" +
    "" +
    "" +
        "Bagels, New York Style " +
        "Thompson" +
        "104" +
        "" +
        "4" +
        "1" +
        "0" +
        "510" +
        "54" +
        "3" +
        "11" +
        "" +
            "0" +
            "0" +
        "" +
        "" +
            "8" +
            "20" +
        "" +
    "" +
    "" +
        "Beef Frankfurter, Quarter Pound " +
        "Armitage" +
        "115" +
        "" +
        "32" +
        "15" +
        "65" +
        "1100" +
        "8" +
        "0" +
        "13" +
        "" +
            "0" +
            "2" +
        "" +
        "" +
            "1" +
            "6" +
        "" +
    "" +
    "" +
        "Chicken Pot Pie" +
        "Lakeson" +
        "198" +
        "" +
        "22" +
        "9" +
        "25" +
        "810" +
        "42" +
        "2" +
        "10" +
        "" +
            "20" +
            "2" +
        "" +
        "" +
            "2" +
            "10" +
        "" +
    "" +
    "" +
        "Cole Slaw" +
        "Fresh Quick" +
        "1.5" +
        "" +
        "0" +
        "0" +
        "0" +
        "15" +
        "5" +
        "2" +
        "1" +
        "" +
            "30" +
            "45" +
        "" +
        "" +
            "4" +
            "2" +
        "" +
    "" +
    "" +
        "Eggs" +
        "Goodpath" +
        "50" +
        "" +
        "4.5" +
        "1.5" +
        "215" +
        "65" +
        "1" +
        "0" +
        "6" +
        "" +
            "6" +
            "0" +
        "" +
        "" +
            "2" +
            "4" +
        "" +
    "" +
    "" +
        "Hazelnut Spread" +
        "Ferreira" +
        "2" +
        "" +
        "10" +
        "2" +
        "0" +
        "20" +
        "23" +
        "2" +
        "3" +
        "" +
            "0" +
            "0" +
        "" +
        "" +
            "6" +
            "4" +
        "" +
    "" +
    "" +
        "Potato Chips" +
        "Lees" +
        "28" +
        "" +
        "10" +
        "3" +
        "0" +
        "180" +
        "15" +
        "1" +
        "2" +
        "" +
            "0" +
            "10" +
        "" +
        "" +
            "0" +
            "0" +
        "" +
    "" +
    "" +
        "Soy Patties, Grilled" +
        "Gardenproducts" +
        "96" +
        "" +
        "5" +
        "0" +
        "0" +
        "420" +
        "10" +
        "4" +
        "9" +
        "" +
            "0" +
            "0" +
        "" +
        "" +
            "0" +
            "0" +
        "" +
    "" +
    "" +
        "Truffles, Dark Chocolate" +
        "Lyndon's" +
        "39" +
        "" +
        "19" +
        "14" +
        "25" +
        "10" +
        "16" +
        "1" +
        "1" +
        "" +
            "0" +
            "0" +
        "" +
        "" +
            "0" +
            "0" +
        "" +
    "" +
    "";
}