// Decompiled by DJ v3.12.12.96 Copyright 2011 Atanas Neshkov  Date: 08/10/2012 8:47:26
// Home Page: http://members.fortunecity.com/neshkov/dj.html http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   BuscadorDeModificaciones.java

package project;

import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

// Referenced classes of package project:
//            AccederADatos, ConvertirXML

public class BuscadorDeModificaciones
{

    public BuscadorDeModificaciones(NodeList listOfQuestionsEnviada2, String modulo2, String tema2)
    {
        map = new TreeMap();
        dbf = null;
        db1 = null;
        listOfQuestionsEnviada = listOfQuestionsEnviada2;
        modulo = modulo2;
        tema = tema2;
        try
        {
            mapaFicherosZip = new TreeMap();
            dbf = DocumentBuilderFactory.newInstance();
            db1 = dbf.newDocumentBuilder();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            throw new IllegalStateException(ex.getMessage());
        }
        NodeList list = listOfQuestionsEnviada;
        for(int i = 0; i < list.getLength(); i++)
        {
            Element elemento = (Element)list.item(i);
            String id = "";
            if(elemento.getAttribute("id").length() == 1)
                id = "00" + elemento.getAttribute("id");
            else
            if(elemento.getAttribute("id").length() == 2)
                id = "0" + elemento.getAttribute("id");
            else
            if(elemento.getAttribute("id").length() == 3)
                id = elemento.getAttribute("id");
            map.put(id, elemento);
        }

        File carpeta = new File(System.getProperties().getProperty("user.dir") + "/java/updates");
        arrayDeFicheros = carpeta.listFiles();
        for(int i = 0; i < arrayDeFicheros.length; i++)
            try
            {
                if(arrayDeFicheros[i].getName().indexOf(".zip") == -1 || Integer.parseInt(arrayDeFicheros[i].getName().substring(0, arrayDeFicheros[i].getName().indexOf(".zip"))) >= AccederADatos.fecha || Integer.parseInt(arrayDeFicheros[i].getName().substring(0, arrayDeFicheros[i].getName().indexOf(".zip"))) <= Integer.parseInt(AccederADatos.leer("h.version")))
                    continue;
                mapaFicherosZip.put(arrayDeFicheros[i].getName(), arrayDeFicheros[i]);
                if(!AccederADatos.updateFilesFound && arrayDeFicheros[i].getName().endsWith(".zip"))
                    AccederADatos.updateFiles = AccederADatos.updateFiles + arrayDeFicheros[i].getName();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

        AccederADatos.mapaFicherosZip = mapaFicherosZip;
        AccederADatos.updateFilesFound = true;
        Iterator iterator = mapaFicherosZip.values().iterator();
        do
        {
            if(!iterator.hasNext())
                break;
            File ficheroActual = (File)iterator.next();
            try
            {
                if(ficheroActual.getName().endsWith(".zip"))
                {
                    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(ficheroActual)));
                    boolean found = false;
                    do
                    {
                        if(found)
                            break;
                        ZipEntry entry = zis.getNextEntry();
                        if(entry.getName().indexOf(".xml") != -1)
                        {
                            try
                            {
                                Document doc = db1.parse(zis);
                                listOfQuestions1 = doc.getElementsByTagName("del");
                                for(int j = 0; j < listOfQuestions1.getLength(); j++)
                                {
                                    Element elemento = (Element)listOfQuestions1.item(j);
                                    NodeList listOfIdiomas = elemento.getElementsByTagName("idioma");
                                    for(int a = 0; a < listOfIdiomas.getLength(); a++)
                                    {
                                        elemento = (Element)listOfIdiomas.item(a);
                                        if(elemento.getAttribute("id").compareToIgnoreCase(AccederADatos.idioma) != 0)
                                            continue;
                                        NodeList listOfModulos = elemento.getElementsByTagName("modulo");
                                        for(int b = 0; b < listOfModulos.getLength(); b++)
                                        {
                                            elemento = (Element)listOfModulos.item(b);
                                            if(elemento.getAttribute("id").compareToIgnoreCase(modulo) != 0)
                                                continue;
                                            String newModulo = elemento.getAttribute("id");
                                            NodeList newTemaList = elemento.getElementsByTagName("tema");
label0:
                                            for(int c = 0; c < newTemaList.getLength(); c++)
                                            {
                                                elemento = (Element)newTemaList.item(c);
                                                String newTema = elemento.getAttribute("id");
                                                if(elemento.getAttribute("id").compareToIgnoreCase(tema) != 0)
                                                    continue;
                                                NodeList newGradoList = elemento.getElementsByTagName("grado");
                                                int d = 0;
                                                do
                                                {
                                                    if(d >= newGradoList.getLength())
                                                        continue label0;
                                                    elemento = (Element)newGradoList.item(d);
                                                    String newGrado = elemento.getAttribute("id");
                                                    NodeList newTipoList = elemento.getElementsByTagName("tipo");
                                                    for(int e = 0; e < newTipoList.getLength(); e++)
                                                    {
                                                        elemento = (Element)newTipoList.item(e);
                                                        String newTipo = elemento.getAttribute("id");
                                                        NodeList newPreguntaList = elemento.getElementsByTagName("pregunta");
                                                        for(int f = 0; f < newPreguntaList.getLength(); f++)
                                                        {
                                                            elemento = (Element)newPreguntaList.item(f);
                                                            if(modulo.compareToIgnoreCase(newModulo) == 0 && tema.indexOf(newTema) != -1)
                                                                borrarPregunta(elemento.getAttribute("id"));
                                                        }

                                                    }

                                                    d++;
                                                } while(true);
                                            }

                                        }

                                    }

                                }

                                listOfQuestions1 = doc.getElementsByTagName("add");
                                for(int j = 0; j < listOfQuestions1.getLength(); j++)
                                {
                                    Element elemento = (Element)listOfQuestions1.item(j);
                                    NodeList listOfIdiomas = elemento.getElementsByTagName("idioma");
                                    for(int a = 0; a < listOfIdiomas.getLength(); a++)
                                    {
                                        elemento = (Element)listOfIdiomas.item(a);
                                        if(elemento.getAttribute("id").compareToIgnoreCase(AccederADatos.idioma) != 0)
                                            continue;
                                        NodeList listOfModulos = elemento.getElementsByTagName("modulo");
                                        for(int b = 0; b < listOfModulos.getLength(); b++)
                                        {
                                            elemento = (Element)listOfModulos.item(b);
                                            if(elemento.getAttribute("id").compareToIgnoreCase(modulo) != 0)
                                                continue;
                                            String newModulo = elemento.getAttribute("id");
                                            NodeList newTemaList = elemento.getElementsByTagName("tema");
label1:
                                            for(int c = 0; c < newTemaList.getLength(); c++)
                                            {
                                                elemento = (Element)newTemaList.item(c);
                                                String newTema = elemento.getAttribute("id");
                                                if(elemento.getAttribute("id").compareToIgnoreCase(tema) != 0)
                                                    continue;
                                                NodeList newGradoList = elemento.getElementsByTagName("grado");
                                                int d = 0;
                                                do
                                                {
                                                    if(d >= newGradoList.getLength())
                                                        continue label1;
                                                    elemento = (Element)newGradoList.item(d);
                                                    String newGrado = elemento.getAttribute("id");
                                                    NodeList newTipoList = elemento.getElementsByTagName("tipo");
                                                    for(int e = 0; e < newTipoList.getLength(); e++)
                                                    {
                                                        elemento = (Element)newTipoList.item(e);
                                                        String newTipo = elemento.getAttribute("id");
                                                        NodeList newPreguntaList = elemento.getElementsByTagName("pregunta");
                                                        for(int f = 0; f < newPreguntaList.getLength(); f++)
                                                        {
                                                            elemento = (Element)newPreguntaList.item(f);
                                                            elemento.setAttribute("Zip", ficheroActual.getName());
                                                            Element newEnunciadoElement = (Element)elemento.getElementsByTagName("enunciado").item(0);
                                                            if(modulo.compareToIgnoreCase(newModulo) == 0 && tema.indexOf(newTema) != -1)
                                                                aF1adirPregunta(elemento, newModulo, newTema, newGrado, newTipo);
                                                        }

                                                    }

                                                    d++;
                                                } while(true);
                                            }

                                        }

                                    }

                                }

                            }
                            catch(Exception e)
                            {
                                e.printStackTrace();
                            }
                            found = true;
                        }
                    } while(true);
                    zis.close();
                }
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        } while(true);
    }

    public NodeList createNewNodeList()
    {
        Document newdoc = db1.newDocument();
        Element root = newdoc.createElement("Root");
        newdoc.appendChild(root);
        Element nextQuestion;
        for(Iterator iterator = map.values().iterator(); iterator.hasNext(); root.appendChild(newdoc.importNode(nextQuestion, true)))
            nextQuestion = (Element)iterator.next();

        return newdoc.getElementsByTagName("question");
    }

    public void aF1adirPregunta(Element elemento, String mod, String tem, String gra, String tip)
    {
        String id = "";
        if(elemento.getAttribute("id").length() == 1)
            id = "00" + elemento.getAttribute("id");
        else
        if(elemento.getAttribute("id").length() == 2)
            id = "0" + elemento.getAttribute("id");
        else
        if(elemento.getAttribute("id").length() == 3)
            id = elemento.getAttribute("id");
        elemento.setAttribute("updated", "true");
        map.put(id, ConvertirXML.cambiarPregunta(elemento, mod, tem, gra, tip));
    }

    public void borrarPregunta(String id)
    {
        if(id.length() == 1)
            id = "00" + id;
        else
        if(id.length() == 2)
            id = "0" + id;
        else
        if(id.length() == 3)
            id = id;
        map.remove(id);
    }

    public static void main(String args1[])
        throws Exception
    {
    }

    File arrayDeFicheros[];
    NodeList listOfQuestions1;
    TreeMap map;
    TreeMap mapaFicherosZip;
    DocumentBuilderFactory dbf;
    DocumentBuilder db1;
    NodeList listOfQuestionsEnviada;
    String modulo;
    String tema;
}