/**
 *    Copyright 2014 Thomas Naeff (github.com/thnaeff)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package ch.thn.gedcom.creator;

import ch.thn.gedcom.creator.structures.AbstractGedcomStructure;
import ch.thn.gedcom.creator.structures.GedcomEOF;
import ch.thn.gedcom.creator.structures.GedcomFamily;
import ch.thn.gedcom.creator.structures.GedcomHeader;
import ch.thn.gedcom.creator.structures.GedcomIndividual;
import ch.thn.gedcom.creator.structures.GedcomSubmitter;
import ch.thn.gedcom.data.GedcomTree;
import ch.thn.gedcom.store.GedcomStore;

/**
 *
 * @author Thomas Naeff (github.com/thnaeff)
 *
 */
public class GedcomCreatorUtil {
	
	/**
	 * Creates the gedcom structure according to the type of the given gedcom head node
	 * 
	 * @param store
	 * @param gedcomHeadNode
	 * @return
	 */
	public static AbstractGedcomStructure gedcomCreatorStructureFactory(
			GedcomStore store, GedcomTree gedcomHeadNode) {
		
		String structureName = gedcomHeadNode.getStructureName();
		
		switch (structureName) {
		case AbstractGedcomStructure.END_OF_FILE:
			return new GedcomEOF(store, gedcomHeadNode);
		case AbstractGedcomStructure.FAM_RECORD:
			return new GedcomFamily(store, gedcomHeadNode);
		case AbstractGedcomStructure.HEADER:
			return new GedcomHeader(store, gedcomHeadNode);
		case AbstractGedcomStructure.INDIVIDUAL_RECORD:
			return new GedcomIndividual(store, gedcomHeadNode);
		case AbstractGedcomStructure.SUBMITTER_RECORD:
			return new GedcomSubmitter(store, gedcomHeadNode);
		default:
			return null;
		}
		
	}
	
	/**
	 * Add the abstract structure to the structure storage based on its type. The 
	 * structure ID will be used if != <code>null</code>, otherwise the structure 
	 * ID is used for structures which have an ID (like individuals or families) 
	 * and <code>null</code> will be used for all others
	 * 
	 * @param structureStorage
	 * @param structure
	 * @param structureId
	 */
	public static void addStructureBasedOnType(
			GedcomCreatorStructureStorage structureStorage, 
			AbstractGedcomStructure structure, String structureId) {
		
		String structureName = structure.getStructureName();
		
		switch (structureName) {
		case AbstractGedcomStructure.END_OF_FILE:
			structureStorage.addEOF(structureId, (GedcomEOF)structure);
			break;
		case AbstractGedcomStructure.FAM_RECORD:
			if (structureId != null) {
				structureStorage.addFamily(structureId, (GedcomFamily)structure);
			} else {
				structureStorage.addFamily((GedcomFamily)structure);
			}
			break;
		case AbstractGedcomStructure.HEADER:
			structureStorage.addHeader(structureId, (GedcomHeader)structure);
			break;
		case AbstractGedcomStructure.INDIVIDUAL_RECORD:
			if (structureId != null) {
				structureStorage.addIndividual(structureId, (GedcomIndividual)structure);
			} else {
				structureStorage.addIndividual((GedcomIndividual)structure);
			}
			break;
		case AbstractGedcomStructure.SUBMITTER_RECORD:
			structureStorage.addSubmitter(structureId, (GedcomSubmitter)structure);
			break;
		default:
			throw new GedcomCreatorError("Unknown structure type " + structureName);
		}
		
	}

}

@@ -1162,16 +1162,51 @@
 til %7B%0A%09%0A
+%09private static int IDCount = 0;%0A%09%0A
 %09/**%0A%09 *
@@ -2758,16 +2758,47 @@
 F_FILE:%0A
+%09%09%09if (structureId != null) %7B%0A%09
 %09%09%09struc
@@ -2844,32 +2844,130 @@
 EOF)structure);%0A
+%09%09%09%7D else %7B%0A%09%09%09%09structureStorage.addEOF(String.valueOf(generateID()), (GedcomEOF)structure);%0A%09%09%09%7D%0A
 %09%09%09break;%0A%09%09case
@@ -3226,16 +3226,47 @@
 HEADER:%0A
+%09%09%09if (structureId != null) %7B%0A%09
 %09%09%09struc
@@ -3318,32 +3318,136 @@
 der)structure);%0A
+%09%09%09%7D else %7B%0A%09%09%09%09structureStorage.addHeader(String.valueOf(generateID()), (GedcomHeader)structure);%0A%09%09%09%7D%0A
 %09%09%09break;%0A%09%09case
@@ -3739,16 +3739,47 @@
 RECORD:%0A
+%09%09%09if (structureId != null) %7B%0A%09
 %09%09%09struc
@@ -3845,16 +3845,96 @@
 cture);%0A
+%09%09%09%7D else %7B%0A%09%09%09%09structureStorage.addSubmitter((GedcomSubmitter)structure);%0A%09%09%09%7D%0A
 %09%09%09break
@@ -4026,17 +4026,109 @@
 e);%0A%09%09%7D%0A
-
 %09%09%0A%09%7D
+%0A%09%0A%09/**%0A%09 * %0A%09 * %0A%09 * @return%0A%09 */%0A%09private static int generateID() %7B%0A%09%09return IDCount++;%0A%09%7D
 %0A%0A%7D%0A

Generates ID if ID=null