package nl.mpi.kinnate.gedcomimport;

import java.util.ArrayList;

/**
 * Document : GedcomLineStructure
 * Created on : Jul 27, 2012, 6:03:36 PM
 * Author : Peter Withers
 */
public class GedcomLineStructure extends ImportLineStructure {

    public GedcomLineStructure(String lineString, ArrayList<String> gedcomLevelStrings) throws ImportException {
        super(lineString, gedcomLevelStrings);
        isFileHeader = lineString.startsWith("0 HEAD");
        String[] lineParts = lineString.split(" ", 3);
        gedcomLevel = Integer.parseInt(lineParts[0]);
        if (isFileHeader) {
            currentID = lineParts[1];
            addFieldEntry(lineParts[1], null);
        } else if (!isFileHeader && gedcomLevel == 0) {
            currentID = lineParts[1];
//            currentName = lineParts[2];
            if (lineParts.length > 2) {
                setType(lineParts[2]);
                if (!currentID.startsWith("@") || !currentID.endsWith("@")) {
                    throw new ImportException("Incorrect gedcom identifier format: " + currentID);
                }
            }
        } else {
            if (lineParts.length > 2) {
                addFieldEntry(lineParts[1], lineParts[2]);
            } else {
                addFieldEntry(lineParts[1], "");
            }
        }
        while (gedcomLevelStrings.size() > gedcomLevel) {
            gedcomLevelStrings.remove(gedcomLevelStrings.size() - 1);
        }
        gedcomLevelStrings.add(lineParts[1]);
    }

    private void setType(String getdomType) {
        if (getdomType.equals("NOTE")) {
            entityType = "Gedcom Note";
        } else if (getdomType.equals("FAM")) {
            entityType = "Gedcom Family Group";
        } else if (getdomType.equals("INDI")) {
            // do not set a type for individuals
        } else if (getdomType.equals("OBJE")) {
            entityType = "Resource File";
        } else if (getdomType.equals("REPO")) {
            entityType = "Repository";
        } else if (getdomType.equals("SUBN")) {
            entityType = "Submission";
        } else if (getdomType.equals("SOUR")) {
            entityType = "Source";
        } else if (getdomType.equals("SUBM")) {
            entityType = "Submitter";
        } else {
            entityType = getdomType;
        }
    }

    @Override
    public boolean isContinueLine() {
        return hasCurrent() && getCurrent().currentName.equals("CONC");
    }

    @Override
    public boolean isContinueLineBreak() {
        return hasCurrent() && getCurrent().currentName.equals("CONT");
    }

    @Override
    public boolean isEndOfFileMarker() {
        return currentID != null && currentID.equals("TRLR");
    }

    public boolean isRelation() {
        return hasCurrent() && getCurrent().lineContents != null && getCurrent().lineContents.startsWith("@") && getCurrent().lineContents.endsWith("@");
    }
}

@@ -2388,32 +2388,37 @@
 eturn hasCurrent
+Field
 () && getCurrent
@@ -2409,32 +2409,37 @@
 () && getCurrent
+Field
 ().currentName.e
@@ -2534,32 +2534,37 @@
 eturn hasCurrent
+Field
 () && getCurrent
@@ -2563,16 +2563,21 @@
 tCurrent
+Field
 ().curre
@@ -2789,16 +2789,21 @@
 sCurrent
+Field
 () && ge
@@ -2802,32 +2802,37 @@
 () && getCurrent
+Field
 ().lineContents 
@@ -2844,32 +2844,37 @@
 ll && getCurrent
+Field
 ().lineContents.
@@ -2902,16 +2902,21 @@
 tCurrent
+Field
 ().lineC

Add an importer for TIP format by making the gedcom importer more generic. refs #2193