package uk.ac.reload.editor.upload;

import java.io.File;
import java.io.IOException;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 *
 * @author Gonzalo
 */
public class CourseManifestReader {

    private String courseTitle = "Untitled";
    private String courseDescription = "No description";

    public CourseManifestReader(File manifestFile) {
        SAXBuilder builder = new SAXBuilder(false);
        org.jdom.Document doc = null;
        try {
            doc = builder.build(manifestFile);

        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (JDOMException ex) {
            ex.printStackTrace();
        }

        org.jdom.Element raiz = doc.getRootElement();
        org.jdom.Namespace manifestNS = raiz.getNamespace();
        org.jdom.Namespace metadataNS = raiz.getNamespace("imsmd");

        org.jdom.Element metadata = raiz.getChild("metadata", manifestNS);
        if (metadata != null) {
            org.jdom.Element lom = metadata.getChild("lom", metadataNS);
            if (lom != null) {
                org.jdom.Element general = lom.getChild("general", metadataNS);
                if (general != null) {
                    org.jdom.Element title = general.getChild("title", metadataNS);
                    org.jdom.Element description = general.getChild("description", metadataNS);

                    if (title != null) {
                        courseTitle = title.getChild("langstring", metadataNS).getTextTrim();
                    } else {
                        System.out.println("no existe entrada para el título");
                    }
                    if (description != null) {
                        courseDescription = description.getChild("langstring", metadataNS).getTextTrim();

                    } else {
                        System.out.println("no existe entrada para la descripcion");
                    }
                } else {
                    System.out.println("no existe entrada general");
                }
            } else {
                System.out.println("No existe entrada lom");
            }
        } else {
            System.out.println("No existen metadatos");
        }
        System.out.println("Title: " + courseTitle);
        System.out.println("Description: " + courseDescription);
    }

    public String getTitle() {
        return courseTitle;
    }

    public String getDescription() {
        return courseDescription;
    }
}
