package com.bayer.edam.excelimport;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;

import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import com.univocity.parsers.csv.CsvWriter;
import com.univocity.parsers.csv.CsvWriterSettings;

/**
 * Generates an import file from a toolbox CSV export (see <a href="sql-code.sql">sql-code.sql</a>)
 * 
 * @author GEXCE
 */
public class ToolboxImportFileGenerator {
	private static String[] selection = { };
	
	private static Logger log = Logger.getLogger(ToolboxImportFileGenerator.class);

	private static String toolboxExportFilePath = "C:\\Temp\\Migration CS - PhotoGallery\\2017-12-12 example photo 5531.csv";
	private static String logfilePath = "C:\\Temp\\Migration CS - PhotoGallery\\2017-12-12 example photo 5531.csv.generator.log";
	private static String licenseGuidsFilePath = "C:\\Temp\\Migration CS - SQL Server\\Release_GUIDs.csv";
	
	// XXX Spreadsheet needs be be named "Records" in Excel for the importer
	private static String importFilePath = "C:\\Temp\\Migration CS - PhotoGallery\\2017-12-12 example photo 5531.generator.csv";	
	private static String uploadPath = "/Resources/Upload/ToolboxPhotoGallery";

	private static final Map<String, String> brandMap;
	private static final Map<String, String> unitMap;
	private static final Map<String, String> categoryMap;
	private static final Map<String, String> indicationMap;
	private static final Map<String, String> cropMap;

	static {
		brandMap = new HashMap<String, String>();
		brandMap.put("Bayer/General", "Bayer/General");
		brandMap.put("National Tradeshow", "Bayer/General");
		brandMap.put("Absolute", "Absolute");
		brandMap.put("Absolute Maxx", "Absolute Maxx");
		brandMap.put("Accrue", "TEMPORARY");
		brandMap.put("Adament", "TEMPORARY");
		brandMap.put("Admire Flex 4", "Admire Pro");
		brandMap.put("Admire Pro", "Admire Pro");
		brandMap.put("Aeris", "Aeris");
		brandMap.put("AFD Cotton Seed", "TEMPORARY");
		brandMap.put("AG Streptomycin", "TEMPORARY");
		brandMap.put("Aliette", "Aliette");
		brandMap.put("Alion", "Alion");
		brandMap.put("Alion HL", "Alion");
		brandMap.put("Allegiance", "Allegiance");
		brandMap.put("Argent", "Argent");
		brandMap.put("Arize", "TEMPORARY");
		brandMap.put("Assail", "TEMPORARY");
		brandMap.put("Asulox", "TEMPORARY");
		brandMap.put("Atlantis", "TEMPORARY");
		brandMap.put("Autumn", "Autumn");
		brandMap.put("Autumn Super", "Autumn Super");
		brandMap.put("Axiom", "Axiom");
		brandMap.put("Aztec", "TEMPORARY");
		brandMap.put("Aztec 2.1", "TEMPORARY");
		brandMap.put("Aztec 4.67", "TEMPORARY");
		brandMap.put("Aztec 4.68", "TEMPORARY");
		brandMap.put("Balance Bean", "Balance Bean");
		brandMap.put("Balance Flexx", "Balance Flexx");
		brandMap.put("Balance GT", "Balance GT");
		brandMap.put("Balance Pro", "Balance Pro");
		brandMap.put("Ballad Plus", "TEMPORARY");
		brandMap.put("Bayer, General", "Bayer/General");
		brandMap.put("Bayleton", "TEMPORARY");
		brandMap.put("Baytan", "TEMPORARY");
		brandMap.put("Baythroid", "Baythroid XL");
		brandMap.put("Baythroid XL", "Baythroid XL");
		brandMap.put("Belt", "Belt");
		brandMap.put("Betamix", "TEMPORARY");
		brandMap.put("Betanet", "TEMPORARY");
		brandMap.put("Betanex", "TEMPORARY");
		brandMap.put("Bollgard", "TEMPORARY");
		brandMap.put("Bollgard II", "TEMPORARY");
		brandMap.put("Bronate Advanced", "TEMPORARY");
		brandMap.put("Buctril", "TEMPORARY");
		brandMap.put("Calypso", "Calypso");
		brandMap.put("Capreno", "Capreno");
		brandMap.put("Celgard", "TEMPORARY");
		brandMap.put("Celpass", "TEMPORARY");
		brandMap.put("Celpril", "TEMPORARY");
		brandMap.put("Certified FiberMax", "FiberMax");
		brandMap.put("Cheyenne", "TEMPORARY");
		brandMap.put("Contain", "TEMPORARY");
		brandMap.put("COPeO Prime", "COPeO Prime");
		brandMap.put("Corvus", "Corvus");
		brandMap.put("Corvus Complete", "Corvus Complete");
		brandMap.put("Cotguard", "TEMPORARY");
		brandMap.put("Credenz", "Credenz");
		brandMap.put("Cruiser", "TEMPORARY");
		brandMap.put("CSI Safener", "TEMPORARY");
		brandMap.put("CSI Safener 500FS", "TEMPORARY");
		brandMap.put("Decis", "TEMPORARY");
		brandMap.put("Delaro", "Delaro");
		brandMap.put("Di-Syston", "TEMPORARY");
		brandMap.put("DiFlexx", "DiFlexx");
		brandMap.put("DiFlexx Duo", "DiFlexx Duo");
		brandMap.put("Distinguish", "TEMPORARY");
		brandMap.put("Dropp", "TEMPORARY");
		brandMap.put("Dropp SC", "TEMPORARY");
		brandMap.put("Elite", "TEMPORARY");
		brandMap.put("Emesto", "Emesto Silver");
		brandMap.put("Emesto Silver", "Emesto Silver");
		brandMap.put("Envidor", "Envidor");
		brandMap.put("Ethrel", "TEMPORARY");
		brandMap.put("EverGol", "EverGol");
		brandMap.put("Evolve", "TEMPORARY");
		brandMap.put("FiberMax", "FiberMax");
		brandMap.put("FiberMax One Ton Club", "FiberMax");
		brandMap.put("Finish", "Finish Pro");
		brandMap.put("Finish 6 Pro", "Finish Pro");
		brandMap.put("Flint", "Flint");
		brandMap.put("Fluency Agent", "Fluency Agent");
		brandMap.put("Fluency Agent Advanced", "Fluency Agent Advanced");
		brandMap.put("Folicur", "TEMPORARY");
		brandMap.put("Gaucho", "Gaucho");
		brandMap.put("Gaucho Grande", "Gaucho");
		brandMap.put("Gaucho MZ", "Gaucho");
		brandMap.put("Gaucho XT", "Gaucho");
		brandMap.put("Gem", "Gem");
		brandMap.put("Ginstar", "Ginstar EC");
		brandMap.put("Ginstar EC", "Ginstar EC");
		brandMap.put("Glyphomax", "TEMPORARY");
		brandMap.put("GlyTol", "GlyTol");
		brandMap.put("Grower Dividends", "Grower Dividends");
		brandMap.put("Growing Strong", "Bayer/General");
		brandMap.put("Gustafson", "TEMPORARY");
		brandMap.put("Guthion", "TEMPORARY");
		brandMap.put("HBK", "HBK");
		brandMap.put("Heads Up", "TEMPORARY");
		brandMap.put("Herculex", "TEMPORARY");
		brandMap.put("Herculex I", "TEMPORARY");
		brandMap.put("Hoegemeyer", "TEMPORARY");
		brandMap.put("Hoelon", "TEMPORARY");
		brandMap.put("Huskie", "Huskie");
		brandMap.put("Huskie Complete", "Huskie Complete");
		brandMap.put("ICP - Horticulture", "Bayer/General");
		brandMap.put("ICP - Innovation Plus", "Bayer/General");
		brandMap.put("ICP - Cereals", "Bayer/General");
		brandMap.put("ICP - Corn", "Bayer/General");
		brandMap.put("ICP - Multiple", "Bayer/General");
		brandMap.put("ICP - Southern Row Crops", "Bayer/General");
		brandMap.put("ICP - Soybeans", "Bayer/General");
		brandMap.put("Idol", "Idol");
		brandMap.put("Ignite", "TEMPORARY");
		brandMap.put("Ilevo", "Ilevo");
		brandMap.put("Innovation Plus", "Innovation Plus");
		brandMap.put("InSeed", "InSeed");
		brandMap.put("Intruder", "TEMPORARY");
		brandMap.put("InVigor", "InVigor");
		brandMap.put("Ipresso", "Ipresso");
		brandMap.put("Kodiak", "TEMPORARY");
		brandMap.put("Krovar", "TEMPORARY");
		brandMap.put("Larvin", "Larvin");
		brandMap.put("Laudis", "Laudis");
		brandMap.put("Laudis Flexx", "Laudis");
		brandMap.put("Leverage", "Leverage 360");
		brandMap.put("Leverage 2.7", "Leverage 360");
		brandMap.put("Leverage 360", "Leverage 360");
		brandMap.put("Liberty", "Liberty");
		brandMap.put("LibertyLink", "LibertyLink");
		brandMap.put("Luna", "Luna");
		brandMap.put("Mocap", "TEMPORARY");
		brandMap.put("Monitor", "TEMPORARY");
		brandMap.put("Movento", "Movento");
		brandMap.put("Movento HL", "Movento HL");
		brandMap.put("Nemacur", "TEMPORARY");
		brandMap.put("Nortron", "Nortron SC");
		brandMap.put("Nutrikote", "TEMPORARY");
		brandMap.put("Oberon", "Oberon");
		brandMap.put("Olympus", "Olympus");
		brandMap.put("Olympus Flex", "Olympus");
		brandMap.put("On Demand", "On Demand");
		brandMap.put("Option", "TEMPORARY");
		brandMap.put("Osprey", "Osprey");
		brandMap.put("Phaser", "TEMPORARY");
		brandMap.put("Poncho", "Poncho");
		brandMap.put("Poncho 1250", "Poncho 1250");
		brandMap.put("Poncho 250", "Poncho 1250");
		brandMap.put("Poncho 250/1250", "Poncho 1250");
		brandMap.put("Poncho 251", "Poncho 1250");
		brandMap.put("Poncho 500", "Poncho 1250");
		brandMap.put("Poncho Beta", "Poncho Beta");
		brandMap.put("Poncho/VOTiVO", "Poncho/VOTiVO");
		brandMap.put("Poncho/VOTiVO 2.0", "Poncho/VOTiVO 2.0");
		brandMap.put("Prep", "TEMPORARY");
		brandMap.put("Prep 6", "TEMPORARY");
		brandMap.put("Previcur", "Previcur Flex");
		brandMap.put("Previcur Flex", "Previcur Flex");
		brandMap.put("Proceed", "TEMPORARY");
		brandMap.put("Proceed Concentrate", "TEMPORARY");
		brandMap.put("Proceed MD", "TEMPORARY");
		brandMap.put("Producer Information Exchange (P.I.E.)", "Producer Information Exchange (P.I.E.)");
		brandMap.put("Proline", "Proline");
		brandMap.put("Proline 3+3", "Proline");
		brandMap.put("ProPulse", "ProPulse");
		brandMap.put("Prosaro", "Prosaro");
		brandMap.put("Prosper", "Prosper");
		brandMap.put("Prosper FX", "Prosper");
		brandMap.put("Protech Plus", "TEMPORARY");
		brandMap.put("ProTechPlus", "TEMPORARY");
		brandMap.put("Protégé", "TEMPORARY"); // XXX
		brandMap.put("Protégé FL", "TEMPORARY"); // XXX
		brandMap.put("Provado", "TEMPORARY");
		brandMap.put("Provost", "Provost");
		brandMap.put("Provost Opti", "Provost Opti");
		brandMap.put("Puma", "TEMPORARY");
		brandMap.put("Radius", "TEMPORARY");
		brandMap.put("Raxil", "Raxil");
		brandMap.put("Raxil Extra", "Raxil");
		brandMap.put("Raxil MD", "Raxil");
		brandMap.put("Raxil MD Extra", "Raxil");
		brandMap.put("Raxil MD Extra W", "Raxil");
		brandMap.put("Raxil MD W", "Raxil");
		brandMap.put("Raxil Pro MD", "Raxil Pro MD");
		brandMap.put("Raxil Pro Shield", "Raxil Pro Shield");
		brandMap.put("Raxil Sure", "Raxil");
		brandMap.put("Raxil XT", "Raxil");
		brandMap.put("Reason", "Reason");
		brandMap.put("Redigo", "Redigo");
		brandMap.put("Rely", "Rely 280");
		brandMap.put("Rely 200", "Rely 280");
		brandMap.put("Rely 280", "Rely 280");
		brandMap.put("Renounce", "TEMPORARY");
		brandMap.put("Requiem", "Requiem");
		brandMap.put("Respect the Rotation", "Respect the Rotation");
		brandMap.put("Rhizokote", "TEMPORARY");
		brandMap.put("Ricestar", "Ricestar");
		brandMap.put("Ricestar HT", "Ricestar");
		brandMap.put("Rimfire", "Rimfire Max");
		brandMap.put("Rimfire Max", "Rimfire Max");
		brandMap.put("Rovral", "TEMPORARY");
		brandMap.put("Scala", "Scala");
		brandMap.put("Scoparia", "Scoparia");
		brandMap.put("SeedGrowth", "Bayer/General");
		brandMap.put("Sencor", "TEMPORARY");
		brandMap.put("Sensat", "TEMPORARY");
		brandMap.put("Sepresto", "Sepresto");
		brandMap.put("Serenade", "Serenade");
		brandMap.put("Serenade ASO", "Serenade ASO");
		brandMap.put("Serenade Opti", "Serenade Optimum");
		brandMap.put("Serenade Optimum", "Serenade Optimum");
		brandMap.put("Serenade Soil", "Serenade Soil");
		brandMap.put("Sevin", "TEMPORARY");
		brandMap.put("Silverado", "TEMPORARY");
		brandMap.put("Sivanto", "Sivanto");
		brandMap.put("Sivanto Prime", "Sivanto Prime");
		brandMap.put("Soberan", "TEMPORARY");
		brandMap.put("Sonata", "Sonata");
		brandMap.put("Spin-Aid", "TEMPORARY");
		brandMap.put("Stance", "Stance");
		brandMap.put("Stoneville", "Stoneville");
		brandMap.put("Storcide II", "TEMPORARY");
		brandMap.put("Storicide II", "TEMPORARY");
		brandMap.put("Stratego", "Stratego");
		brandMap.put("Stratego YLD", "Stratego YLD");
		brandMap.put("Synapse", "TEMPORARY");
		brandMap.put("Temik", "TEMPORARY");
		brandMap.put("Temix", "TEMPORARY");
		brandMap.put("Tempo SC Ultra", "TEMPORARY");
		brandMap.put("Thiram", "Thiram");
		brandMap.put("Tops MZ", "TEMPORARY");
		brandMap.put("Tops MZ Gaucho", "TEMPORARY");
		brandMap.put("TriKote", "TEMPORARY");
		brandMap.put("Trilex", "Trilex");
		brandMap.put("Trilex 2000", "Trilex 2000");
		brandMap.put("Trilex 5000", "Trilex");
		brandMap.put("Trilex 6000", "Trilex");
		brandMap.put("Trilex Advanced", "Trilex ADV");
		brandMap.put("Trilex AL", "Trilex");
		brandMap.put("Trilex ADV", "Trilex ADV");
		brandMap.put("Trilex Optimum", "Trilex");
		brandMap.put("Trilex Rice Pack", "Trilex");
		brandMap.put("TrilexStar", "Trilex");
		brandMap.put("Trimax", "TEMPORARY");
		brandMap.put("Trimax Pro", "TEMPORARY");
		brandMap.put("TwinLink", "TwinLink");
		brandMap.put("TwinLink Plus", "TwinLink Plus");
		brandMap.put("Ultor", "Ultor");
		brandMap.put("Varro", "Varro");
		brandMap.put("Velum One", "Velum One");
		brandMap.put("Velum Prime", "Velum Prime");
		brandMap.put("Velum Total", "Velum Total");
		brandMap.put("Vitis", "TEMPORARY");
		brandMap.put("Vortex", "Vortex");
		brandMap.put("Votivo", "Votivo");
		brandMap.put("Whip 360", "TEMPORARY");
		brandMap.put("Wolverine", "Wolverine Advanced");
		brandMap.put("Wolverine Advanced", "Wolverine Advanced");
		brandMap.put("Wolverine PowerPak", "Wolverine Advanced");
		brandMap.put("Yield Shield", "TEMPORARY");

		// XXX additional mappings
		brandMap.put("NULL", "TEMPORARY"); // Bayer/General?
		brandMap.put("131-Kote", "TEMPORARY");
		brandMap.put("ICP - Food Chain", "Bayer/General");
		brandMap.put("ICP - Fruit", "Bayer/General");
		brandMap.put("ICP-Programs", "Bayer/General");
		brandMap.put("ICP - Veg/Potato", "Bayer/General");
		brandMap.put("Velum", "Velum One");
		brandMap.put("Velum Prime 3", "Velum Prime");
		brandMap.put("Fluency Agent/Fluency Agent Advanced", "TEMPORARY"); // set both brands via special rule
		brandMap.put("Poncho/VOTiVO 2", "TEMPORARY"); // set both 'Poncho' and 'Votivo' via special rule
		brandMap.put("Poncho 1250/VOTiVO 2", "TEMPORARY"); // set 'Poncho 250/1250' and 'Votivo' via special rule (there is no Votivo 2)
		

		unitMap = new HashMap<String, String>();
		unitMap.put("Bayer/General", "Bayer");
		unitMap.put("National Tradeshow", "Crop Protection");
		unitMap.put("Absolute", "Crop Protection");
		unitMap.put("Absolute Maxx", "Crop Protection");
		unitMap.put("Accrue", "Crop Protection");
		unitMap.put("Adament", "Crop Protection");
		unitMap.put("Admire Flex 4", "Crop Protection");
		unitMap.put("Admire Pro", "Crop Protection");
		unitMap.put("Aeris", "Crop Protection");
		unitMap.put("AFD Cotton Seed", "Seeds");
		unitMap.put("AG Streptomycin", "Crop Protection");
		unitMap.put("Aliette", "Crop Protection");
		unitMap.put("Alion", "Crop Protection");
		unitMap.put("Alion HL", "Crop Protection");
		unitMap.put("Allegiance", "Crop Protection");
		unitMap.put("Argent", "Crop Protection");
		unitMap.put("Arize", "Seeds");
		unitMap.put("Assail", "Crop Protection");
		unitMap.put("Asulox", "Crop Protection");
		unitMap.put("Atlantis", "Crop Protection");
		unitMap.put("Autumn", "Crop Protection");
		unitMap.put("Autumn Super", "Crop Protection");
		unitMap.put("Axiom", "Crop Protection");
		unitMap.put("Aztec", "Crop Protection");
		unitMap.put("Aztec 2.1", "Crop Protection");
		unitMap.put("Aztec 4.67", "Crop Protection");
		unitMap.put("Aztec 4.68", "Crop Protection");
		unitMap.put("Balance Bean", "Crop Protection");
		unitMap.put("Balance Flexx", "Crop Protection");
		unitMap.put("Balance GT", "Crop Protection");
		unitMap.put("Balance Pro", "Crop Protection");
		unitMap.put("Ballad Plus", "Crop Protection");
		unitMap.put("Bayer, General", "Crop Protection");
		unitMap.put("Bayleton", "Crop Protection");
		unitMap.put("Baytan", "Crop Protection");
		unitMap.put("Baythroid", "Crop Protection");
		unitMap.put("Baythroid XL", "Crop Protection");
		unitMap.put("Belt", "Crop Protection");
		unitMap.put("Betamix", "Crop Protection");
		unitMap.put("Betanet", "Crop Protection");
		unitMap.put("Betanex", "Crop Protection");
		unitMap.put("Bollgard", "Seeds");
		unitMap.put("Bollgard II", "Seeds");
		unitMap.put("Bronate Advanced", "Crop Protection");
		unitMap.put("Buctril", "Crop Protection");
		unitMap.put("Calypso", "Crop Protection");
		unitMap.put("Capreno", "Crop Protection");
		unitMap.put("Celgard", "Crop Protection");
		unitMap.put("Celpass", "Crop Protection");
		unitMap.put("Celpril", "Crop Protection");
		unitMap.put("Certified FiberMax", "Seeds");
		unitMap.put("Cheyenne", "Crop Protection");
		unitMap.put("Contain", "Crop Protection");
		unitMap.put("COPeO Prime", "Crop Protection");
		unitMap.put("Corvus", "Crop Protection");
		unitMap.put("Corvus Complete", "Crop Protection");
		unitMap.put("Cotguard", "Crop Protection");
		unitMap.put("Credenz", "Seeds");
		unitMap.put("Cruiser", "Crop Protection");
		unitMap.put("CSI Safener", "Crop Protection");
		unitMap.put("CSI Safener 500FS", "Crop Protection");
		unitMap.put("Decis", "Crop Protection");
		unitMap.put("Delaro", "Crop Protection");
		unitMap.put("Di-Syston", "Crop Protection");
		unitMap.put("DiFlexx", "Crop Protection");
		unitMap.put("DiFlexx Duo", "Crop Protection");
		unitMap.put("Distinguish", "Crop Protection");
		unitMap.put("Dropp", "Crop Protection");
		unitMap.put("Dropp SC", "Crop Protection");
		unitMap.put("Elite", "Crop Protection");
		unitMap.put("Emesto", "Crop Protection");
		unitMap.put("Emesto Silver", "Crop Protection");
		unitMap.put("Envidor", "Crop Protection");
		unitMap.put("Ethrel", "Crop Protection");
		unitMap.put("EverGol", "Crop Protection");
		unitMap.put("Evolve", "Crop Protection");
		unitMap.put("FiberMax", "Seeds");
		unitMap.put("FiberMax One Ton Club", "Seeds");
		unitMap.put("Finish", "Crop Protection");
		unitMap.put("Finish 6 Pro", "Crop Protection");
		unitMap.put("Flint", "Crop Protection");
		unitMap.put("Fluency Agent", "Crop Protection");
		unitMap.put("Fluency Agent Advanced", "Crop Protection");
		unitMap.put("Folicur", "Crop Protection");
		unitMap.put("Gaucho", "Crop Protection");
		unitMap.put("Gaucho Grande", "Crop Protection");
		unitMap.put("Gaucho MZ", "Crop Protection");
		unitMap.put("Gaucho XT", "Crop Protection");
		unitMap.put("Gem", "Crop Protection");
		unitMap.put("Ginstar", "Crop Protection");
		unitMap.put("Ginstar EC", "Crop Protection");
		unitMap.put("Glyphomax", "Crop Protection");
		unitMap.put("GlyTol", "Seeds");
		unitMap.put("Grower Dividends", "Crop Protection");
		unitMap.put("Growing Strong", "Crop Protection");
		unitMap.put("Gustafson", "Crop Protection");
		unitMap.put("Guthion", "Crop Protection");
		unitMap.put("HBK", "Seeds");
		unitMap.put("Heads Up", "Crop Protection");
		unitMap.put("Herculex", "Seeds");
		unitMap.put("Herculex I", "Seeds");
		unitMap.put("Hoegemeyer", "Seeds");
		unitMap.put("Hoelon", "Crop Protection");
		unitMap.put("Huskie", "Crop Protection");
		unitMap.put("Huskie Complete", "Crop Protection");
		unitMap.put("ICP - Horticulture", "Crop Protection");
		unitMap.put("ICP - Innovation Plus", "Crop Protection");
		unitMap.put("ICP - Cereals", "Crop Protection");
		unitMap.put("ICP - Corn", "Crop Protection");
		unitMap.put("ICP - Multiple", "Crop Protection");
		unitMap.put("ICP - Southern Row Crops", "Crop Protection");
		unitMap.put("ICP - Soybeans", "Crop Protection");
		unitMap.put("Idol", "Crop Protection");
		unitMap.put("Ignite", "Crop Protection");
		unitMap.put("Ilevo", "Crop Protection");
		unitMap.put("Innovation Plus", "Crop Protection");
		unitMap.put("InSeed", "Seeds");
		unitMap.put("Intruder", "Crop Protection");
		unitMap.put("InVigor", "Seeds");
		unitMap.put("Ipresso", "Crop Protection");
		unitMap.put("Kodiak", "Crop Protection");
		unitMap.put("Krovar", "Crop Protection");
		unitMap.put("Larvin", "Crop Protection");
		unitMap.put("Laudis", "Crop Protection");
		unitMap.put("Laudis Flexx", "Crop Protection");
		unitMap.put("Leverage", "Crop Protection");
		unitMap.put("Leverage 2.7", "Crop Protection");
		unitMap.put("Leverage 360", "Crop Protection");
		unitMap.put("Liberty", "Crop Protection");
		unitMap.put("LibertyLink", "Seeds");
		unitMap.put("Luna", "Crop Protection");
		unitMap.put("Mocap", "Crop Protection");
		unitMap.put("Monitor", "Crop Protection");
		unitMap.put("Movento", "Crop Protection");
		unitMap.put("Movento HL", "Crop Protection");
		unitMap.put("Nemacur", "Crop Protection");
		unitMap.put("Nortron", "Crop Protection");
		unitMap.put("Nutrikote", "Crop Protection");
		unitMap.put("Oberon", "Crop Protection");
		unitMap.put("Olympus", "Crop Protection");
		unitMap.put("Olympus Flex", "Crop Protection");
		unitMap.put("On Demand", "Crop Protection");
		unitMap.put("Option", "Crop Protection");
		unitMap.put("Osprey", "Crop Protection");
		unitMap.put("Phaser", "Crop Protection");
		unitMap.put("Poncho", "Crop Protection");
		unitMap.put("Poncho 1250", "Crop Protection");
		unitMap.put("Poncho 250", "Crop Protection");
		unitMap.put("Poncho 250/1250", "Crop Protection");
		unitMap.put("Poncho 251", "Crop Protection");
		unitMap.put("Poncho 500", "Crop Protection");
		unitMap.put("Poncho Beta", "Crop Protection");
		unitMap.put("Poncho/VOTiVO", "Crop Protection");
		unitMap.put("Poncho/VOTiVO 2.0", "Crop Protection");
		unitMap.put("Prep", "Crop Protection");
		unitMap.put("Prep 6", "Crop Protection");
		unitMap.put("Previcur", "Crop Protection");
		unitMap.put("Previcur Flex", "Crop Protection");
		unitMap.put("Proceed", "Crop Protection");
		unitMap.put("Proceed Concentrate", "Crop Protection");
		unitMap.put("Proceed MD", "Crop Protection");
		unitMap.put("Producer Information Exchange (P.I.E.)", "Crop Protection");
		unitMap.put("Proline", "Crop Protection");
		unitMap.put("Proline 3+3", "Crop Protection");
		unitMap.put("ProPulse", "Crop Protection");
		unitMap.put("Prosaro", "Crop Protection");
		unitMap.put("Prosper", "Crop Protection");
		unitMap.put("Prosper FX", "Crop Protection");
		unitMap.put("Protech Plus", "Crop Protection");
		unitMap.put("ProTechPlus", "Crop Protection");
		unitMap.put("Prot�g�", "Crop Protection");
		unitMap.put("Prot�g� FL", "Crop Protection");
		unitMap.put("Provado", "Crop Protection");
		unitMap.put("Provost", "Crop Protection");
		unitMap.put("Provost Opti", "Crop Protection");
		unitMap.put("Puma", "Crop Protection");
		unitMap.put("Radius", "Crop Protection");
		unitMap.put("Raxil", "Crop Protection");
		unitMap.put("Raxil Extra", "Crop Protection");
		unitMap.put("Raxil MD", "Crop Protection");
		unitMap.put("Raxil MD Extra", "Crop Protection");
		unitMap.put("Raxil MD Extra W", "Crop Protection");
		unitMap.put("Raxil MD W", "Crop Protection");
		unitMap.put("Raxil Pro MD", "Crop Protection");
		unitMap.put("Raxil Pro Shield", "Crop Protection");
		unitMap.put("Raxil Sure", "Crop Protection");
		unitMap.put("Raxil XT", "Crop Protection");
		unitMap.put("Reason", "Crop Protection");
		unitMap.put("Redigo", "Crop Protection");
		unitMap.put("Rely", "Crop Protection");
		unitMap.put("Rely 200", "Crop Protection");
		unitMap.put("Rely 280", "Crop Protection");
		unitMap.put("Renounce", "Crop Protection");
		unitMap.put("Requiem", "Crop Protection");
		unitMap.put("Respect the Rotation", "Crop Protection");
		unitMap.put("Rhizokote", "Crop Protection");
		unitMap.put("Ricestar", "Crop Protection");
		unitMap.put("Ricestar HT", "Crop Protection");
		unitMap.put("Rimfire", "Crop Protection");
		unitMap.put("Rimfire Max", "Crop Protection");
		unitMap.put("Rovral", "Crop Protection");
		unitMap.put("Scala", "Crop Protection");
		unitMap.put("Scoparia", "Crop Protection");
		unitMap.put("SeedGrowth", "Crop Protection");
		unitMap.put("Sencor", "Crop Protection");
		unitMap.put("Sensat", "Crop Protection");
		unitMap.put("Sepresto", "Crop Protection");
		unitMap.put("Serenade", "Crop Protection");
		unitMap.put("Serenade ASO", "Crop Protection");
		unitMap.put("Serenade Opti", "Crop Protection");
		unitMap.put("Serenade Optimum", "Crop Protection");
		unitMap.put("Serenade Soil", "Crop Protection");
		unitMap.put("Sevin", "Crop Protection");
		unitMap.put("Silverado", "Crop Protection");
		unitMap.put("Sivanto", "Crop Protection");
		unitMap.put("Sivanto Prime", "Crop Protection");
		unitMap.put("Soberan", "Crop Protection");
		unitMap.put("Sonata", "Crop Protection");
		unitMap.put("Spin-Aid", "Crop Protection");
		unitMap.put("Stance", "Crop Protection");
		unitMap.put("Stoneville", "Seeds");
		unitMap.put("Storcide II", "Crop Protection");
		unitMap.put("Storicide II", "Crop Protection");
		unitMap.put("Stratego", "Crop Protection");
		unitMap.put("Stratego YLD", "Crop Protection");
		unitMap.put("Synapse", "Crop Protection");
		unitMap.put("Temik", "Crop Protection");
		unitMap.put("Temix", "Crop Protection");
		unitMap.put("Tempo SC Ultra", "Crop Protection");
		unitMap.put("Thiram", "Crop Protection");
		unitMap.put("Tops MZ", "Crop Protection");
		unitMap.put("Tops MZ Gaucho", "Crop Protection");
		unitMap.put("TriKote", "Crop Protection");
		unitMap.put("Trilex", "Crop Protection");
		unitMap.put("Trilex 2000", "Crop Protection");
		unitMap.put("Trilex 5000", "Crop Protection");
		unitMap.put("Trilex 6000", "Crop Protection");
		unitMap.put("Trilex Advanced", "Crop Protection");
		unitMap.put("Trilex AL", "Crop Protection");
		unitMap.put("Trilex ADV", "Crop Protection");
		unitMap.put("Trilex Optimum", "Crop Protection");
		unitMap.put("Trilex Rice Pack", "Crop Protection");
		unitMap.put("TrilexStar", "Crop Protection");
		unitMap.put("Trimax", "Crop Protection");
		unitMap.put("Trimax Pro", "Crop Protection");
		unitMap.put("TwinLink", "Seeds");
		unitMap.put("TwinLink Plus", "Seeds");
		unitMap.put("Ultor", "Crop Protection");
		unitMap.put("Varro", "Crop Protection");
		unitMap.put("Velum One", "Crop Protection");
		unitMap.put("Velum Prime", "Crop Protection");
		unitMap.put("Velum Total", "Crop Protection");
		unitMap.put("Vitis", "Crop Protection");
		unitMap.put("Vortex", "Crop Protection");
		unitMap.put("Votivo", "Crop Protection");
		unitMap.put("Whip 360", "Crop Protection");
		unitMap.put("Wolverine", "Crop Protection");
		unitMap.put("Wolverine Advanced", "Crop Protection");
		unitMap.put("Wolverine PowerPak", "Crop Protection");
		unitMap.put("Yield Shield", "Crop Protection");

		// TODO to be checked by business
		unitMap.put("TEMPORARY", "Crop Protection");
		unitMap.put("Finish Pro", "Crop Protection");
		
		
		categoryMap = new HashMap<String, String>();
		categoryMap.put("Bag Tags : Bag Reguirements", "Seed Company Materials : Seed Bag Guidelines");
		categoryMap.put("Bag Tags : Bag Stencil", "Seed Company Materials : Seed Bag Stencil ");
		categoryMap.put("Bag Tags : Bag Tag", "Seed Company Materials : Seed Bag Tag  ");
		categoryMap.put("Bag Tags : Bag Tag Guidelines", "Seed Company Materials : Seed Bag Guidelines");
		// XXX categoryMap.put("ExcellenceinCommAwardEntries : Comm Award Entry", " ");
		categoryMap.put("ExcellenceinCommAwardEntries : Direct Mail", "Advertising - Print : Direct Mail");
		categoryMap.put("ExcellenceinCommAwardEntries : Presentation", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("ExcellenceinCommAwardEntries : Print Ad", "Advertising - Print : print ad");
		categoryMap.put("ExcellenceinCommAwardEntries : Radio Spot", "Advertising - Radio - Audio : Radio produced spot");
		categoryMap.put("ExcellenceinCommAwardEntries : TV Spot", "Advertising - TV : TV Produced Spot");
		categoryMap.put("ExcellenceinCommAwardEntries : Website", "Advertising - Digital : Web Content - Article-Copy");
		categoryMap.put("Field Signs : Bag Stencil", "Seed Company Materials : Seed Bag Stencil");
		categoryMap.put("Field Signs : Bag Tag", "Seed Company Materials : Seed Bag Tag");
		categoryMap.put("Field Signs : Decal", "Field Day - Tradeshow - Event Materials : Decal");
		categoryMap.put("Field Signs : Field Marker", "Field Day - Tradeshow - Event Materials : Field Marker Flag");
		categoryMap.put("Field Signs : Plot sign", "Field Day - Tradeshow - Event Materials : Plot"); 
		categoryMap.put("Field Signs : Storyboard", "Field Day - Tradeshow - Event Materials : Storyboard");
		categoryMap.put("Literature : Application Card", "Sales Literature - Collateral : Application Card");
		categoryMap.put("Literature : Brochure", "Sales Literature - Collateral : Brochure");
		categoryMap.put("Literature : Pest ID Card", "Sales Literature - Collateral : Fact");
		categoryMap.put("Literature : Portfolio Quick Guide", "Sales Literature - Collateral : Portfolio Quick Guide");
		categoryMap.put("Literature : Product Bulletin", "Sales Literature - Collateral : Product Bulletin ");
		categoryMap.put("Literature : Product Information Sheet", "Sales Literature - Collateral : Fact");
		categoryMap.put("Literature : Product Information Template", "Custom Templates : Sales-Product Sheet Template");
		categoryMap.put("Literature : Product Q&A", "Sales Literature - Collateral : FAQ Sheet ");
		categoryMap.put("Literature : Program Materials", "Program Materials : program detailer");
		categoryMap.put("Literature : Quick Guide Template", "Custom Templates : Sales-Product Sheet Template");
		categoryMap.put("Literature : Technical Bulletin", "Sales Literature - Collateral : Tech Sheet");
		categoryMap.put("Literature : Technology Use Manual", "Sales Literature - Collateral : TTUM");
		categoryMap.put("LocalCampaignTools : Copy Points", "Advertising - PR, Product Publicity : Talking Points");
		categoryMap.put("LocalCampaignTools : Direct Mail", "Advertising - Print : Direct Mail");
		categoryMap.put("LocalCampaignTools : Local Live Read Radio", "Advertising - Radio - Audio : Radio live-read - local rep use ");
		categoryMap.put("LocalCampaignTools : Local Print Ad", "Advertising - Print : Print ad - local rep use");
		categoryMap.put("LocalTradeshowsEventTools : Agenda template", "Field Day - Tradeshow - Event Materials : Agenda");
		categoryMap.put("LocalTradeshowsEventTools : Banner - 3'X4'", "Field Day - Tradeshow - Event Materials : Banner");
		categoryMap.put("LocalTradeshowsEventTools : Banner - 3'X8' ", "Field Day - Tradeshow - Event Materials : Banner"); // XXX
		categoryMap.put("LocalTradeshowsEventTools : Banner - 3'X8'", "Field Day - Tradeshow - Event Materials : Banner");
		categoryMap.put("LocalTradeshowsEventTools : Bannerup", "Field Day - Tradeshow - Event Materials : Banner Up");
		categoryMap.put("LocalTradeshowsEventTools : BannerUp Base (for dual-sided graphics)", "Field Day - Tradeshow - Event Materials : Banner Up");
		categoryMap.put("LocalTradeshowsEventTools : BannerUp Base (for single-sided graphics)", "Field Day - Tradeshow - Event Materials : Banner Up");
		categoryMap.put("LocalTradeshowsEventTools : BannerUp Graphic Cartridge (Custom) - dual sided", "Field Day - Tradeshow - Event Materials : Banner Up");
		categoryMap.put("LocalTradeshowsEventTools : BannerUp Graphic Cartridge (Custom) - single sided", "Field Day - Tradeshow - Event Materials : Banner Up");
		categoryMap.put("LocalTradeshowsEventTools : BannerUp Graphic Cartridge (Standard) - dual sided", "Field Day - Tradeshow - Event Materials : Banner Up");
		categoryMap.put("LocalTradeshowsEventTools : BannerUp Graphic Cartridge (Standard) - single sided", "Field Day - Tradeshow - Event Materials : Banner Up");
		categoryMap.put("LocalTradeshowsEventTools : Canopy tent (10' X 10')", "Field Day - Tradeshow - Event Materials : Tent");
		categoryMap.put("LocalTradeshowsEventTools : Display graphic - 12\"X24\" oval", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("LocalTradeshowsEventTools : Display graphic - 5\"X12\" oval", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("LocalTradeshowsEventTools : Display graphic package - 10' floor display", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("LocalTradeshowsEventTools : Display graphic package - tabletop", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("LocalTradeshowsEventTools : Displays - 10' floor display frame", "Field Day - Tradeshow - Event Materials : Popup Display");
		categoryMap.put("LocalTradeshowsEventTools : Displays - tabletop display frame", "Field Day - Tradeshow - Event Materials : Tabletop Display");
		categoryMap.put("LocalTradeshowsEventTools : Invitation template", "Field Day - Tradeshow - Event Materials : Invitation");
		categoryMap.put("LocalTradeshowsEventTools : Local Show/Event SetUp Suggestions", "Field Day - Tradeshow - Event Materials : Layout");
		categoryMap.put("LocalTradeshowsEventTools : Outdoor Sign", "Field Day - Tradeshow - Event Materials : Banner");
		categoryMap.put("LocalTradeshowsEventTools : Podium Sign", "Field Day - Tradeshow - Event Materials : Podium Sign");
		categoryMap.put("LocalTradeshowsEventTools : Tableskirt", "Field Day - Tradeshow - Event Materials : Table Cover");
		categoryMap.put("LocalTradeshowsEventTools : LocalTradeShowGraphic", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("LogosBrandTags : Brand Tag", "Logos - BrandTags : brand tag");
		categoryMap.put("LogosBrandTags : Logo", "Logos - BrandTags : logo");
		categoryMap.put("NationalCampaign : Campaign Summary", "Sales Literature - Collateral : Brand PPT");
		categoryMap.put("NationalCampaign : Dealer Point of Purchase", "Point of Sales Materials : POS Display");
		categoryMap.put("NationalCampaign : Google Ads", "Advertising - Digital : Search-PPC");
		categoryMap.put("NationalCampaign : National Digital/New Media", "Advertising - Digital : Web Content - Article-Copy");
		categoryMap.put("NationalCampaign : National Print", "Advertising - Print : print ad");
		categoryMap.put("NationalCampaign : National Radio", "Advertising - Radio - Audio : Radio produced spot");
		categoryMap.put("NationalCampaign : National TV", "Advertising - TV : TV Produced Spot");
		categoryMap.put("NationalCampaign : Outdoor Billboard", "Advertising - OOH : billboard");
		categoryMap.put("NationalCampaign : PPC (pay per click)", "Advertising - Digital : Search-PPC");
		categoryMap.put("NationalCampaign : Text Messaging", "Advertising - Digital : Text Messaging");
		categoryMap.put("NationalCampaign : Tweets", "Advertising - Digital : Social");
		categoryMap.put("NationalCampaign : Video", "Advertising - Digital : Web Content - Video");
		categoryMap.put("NationalCRMDirect : Direct Mail", "Advertising - Print : Direct Mail");
		categoryMap.put("NationalCRMDirect : Eblast", "Advertising - Digital : email");
		categoryMap.put("NationalPRCampaign : Audio Release", "Advertising - PR, Product Publicity : News Release");
		categoryMap.put("NationalPRCampaign : PR Overview", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("NationalPRCampaign : PR Results", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("NationalPRCampaign : Press Kit", "Advertising - PR, Product Publicity : Media Kit ");
		categoryMap.put("NationalPRCampaign : Press Release", "Advertising - PR, Product Publicity : News Release");
		categoryMap.put("NationalPRCampaign : Print Article", "Advertising - Print : print advertorial");
		categoryMap.put("NationalPRCampaign : Radio Interview", "Advertising - Radio - Audio : Radio interview");
		categoryMap.put("NationalPRCampaign : Talking Points", "Advertising - PR, Product Publicity : Talking Points");
		categoryMap.put("NationalPRCampaign : TV Interview", "Advertising - TV : TV Interview");
		categoryMap.put("NationalPRCampaign : Video", "Advertising - PR, Product Publicity : Testimonial");
		categoryMap.put("NationalProgramsPromotions : Program Ad", "Program Materials : Program Mailing-Ad");
		categoryMap.put("NationalProgramsPromotions : Program Mailing", "Program Materials : Program Mailing-Ad");
		categoryMap.put("NationalProgramsPromotions : Program Material", "Program Materials : program detailer");
		categoryMap.put("NationalProgramsPromotions : Program Materials", "Program Materials : program detailer");
		categoryMap.put("NationalProgramsPromotions : Program Rules", "Program Materials : program rules");
		categoryMap.put("SalesTools : Premium", "Field Day - Tradeshow - Event Materials/Premiums : Giveaways");
		categoryMap.put("SalesTools : Presentation", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("SalesTools : Presentation template", "Custom Templates : Presentation Template");
		categoryMap.put("SalesTools : Talking Points", "Advertising - PR, Product Publicity : Talking Points");
		categoryMap.put("SalesTools : Training Module", "Advertising - Digital : Web Content - Tool-Training Module");
		categoryMap.put("AgIssuesForum : NTS - Booth Graphics", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("AgIssuesForum : NTS - Summary/Review", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("AgIssuesForum : NTS - Talking Points", "Advertising - PR, Product Publicity : Talking Points");
		categoryMap.put("AgRetailersAssociation : NTS - Talking Points", "Advertising - PR, Product Publicity : Talking Points");
		categoryMap.put("ASFMRA : NTS - Talking Points", "Advertising - PR, Product Publicity : Talking Points");
		categoryMap.put("ASTA : NTS - Booth Graphics", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("ASTA : NTS - PR Results", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("ASTA : NTS - Talking Points", "Advertising - PR, Product Publicity : Talking Points");
		categoryMap.put("Beltwide : NTS - Booth Graphics", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("CAPCA : NTS - Booth Graphics", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("CAPCA : NTS - Booth/Event Photos", "Image : Photo - Event");
		categoryMap.put("CAPCA : NTS - PR Results", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("CAPCA : NTS - Talking Points", "Advertising - PR, Product Publicity : Talking Points");
		categoryMap.put("CAPCA : NTS - Videos", "Image : Photo - Event");
		categoryMap.put("CommodityClassic : NTS - Booth Graphics", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("Equipment : Product Information Sheet", "Sales Literature - Collateral : Fact");
		categoryMap.put("FarmProgress : NTS - Booth Graphics", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("FarmProgress : NTS - PR Results", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("NationalAerialApplicatorsConvention : NTS - Booth Graphics", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("NationalPotatoShow : NTS - Booth Graphics", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("NationalPotatoShow : NTS - PR Results", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("NationalPotatoShow : NTS - Summary/Review", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("NationalPotatoShow : NTS - Talking Points", "Advertising - PR, Product Publicity : Talking Points");
		categoryMap.put("NationalPotatoShow : NTS - Videos", "Image : Photo - Event");
		categoryMap.put("OurCompany : Company Statement", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("OurCompany : Company Values", "Advertising - PR, Product Publicity : Presentation");
		categoryMap.put("ProfitMaximizer : NTS - Booth Graphics", "Field Day - Tradeshow - Event Materials : Popup Graphic");
		categoryMap.put("PHOTO GALLERY : Crop", "Image : Photo - Crop");
		categoryMap.put("PHOTO GALLERY : Equipment", "Image : Photo - Equipment");
		categoryMap.put("PHOTO GALLERY : Event", "Image : Photo - Event");
		categoryMap.put("PHOTO GALLERY : Farmstead", "Image : Photo - Farmstead");
		categoryMap.put("PHOTO GALLERY : Person", "Image : Photo - Person");
		categoryMap.put("PHOTO GALLERY : Pest", "Image : Photo - Pest");
		
		
		indicationMap = new HashMap<String, String>();
//		indicationMap.put("Bayer/General", "");
//		indicationMap.put("National Tradeshow", "");
		indicationMap.put("Absolute", "Fungicides");
		indicationMap.put("Absolute Maxx", "Fungicides");
		indicationMap.put("Accrue", "Fungicides");
		indicationMap.put("Adament", "Fungicides");
		indicationMap.put("Admire Flex 4", "Insecticides");
		indicationMap.put("Admire Pro", "Insecticides");
		indicationMap.put("Aeris", "SeedGrowth");
		indicationMap.put("AFD Cotton Seed", "Seeds");
		indicationMap.put("AG Streptomycin", "SeedGrowth");
		indicationMap.put("Aliette", "Fungicides");
		indicationMap.put("Alion", "Herbicides");
		indicationMap.put("Alion HL", "Herbicides");
		indicationMap.put("Allegiance", "SeedGrowth");
		indicationMap.put("Argent", "SeedGrowth");
		indicationMap.put("Arize", "Seeds");
		indicationMap.put("Assail", "Insecticides");
		indicationMap.put("Asulox", "Herbicides");
		indicationMap.put("Atlantis", "Herbicides");
		indicationMap.put("Autumn", "Herbicides");
		indicationMap.put("Autumn Super", "Herbicides");
		indicationMap.put("Axiom", "Herbicides");
		indicationMap.put("Aztec", "Insecticides");
		indicationMap.put("Aztec 2.1", "Insecticides");
		indicationMap.put("Aztec 4.67", "Insecticides");
		indicationMap.put("Aztec 4.68", "Insecticides");
		indicationMap.put("Balance Bean", "Herbicides");
		indicationMap.put("Balance Flexx", "Herbicides");
		indicationMap.put("Balance GT", "Herbicides");
		indicationMap.put("Balance Pro", "Herbicides");
		indicationMap.put("Ballad Plus", "Herbicides");
//		indicationMap.put("Bayer, General", "");
		indicationMap.put("Bayleton", "Fungicides");
		indicationMap.put("Baytan", "SeedGrowth");
		indicationMap.put("Baythroid", "Insecticides");
		indicationMap.put("Baythroid XL", "Insecticides");
		indicationMap.put("Belt", "Insecticides");
		indicationMap.put("Betamix", "Herbicides");
		indicationMap.put("Betanet", "Herbicides");
		indicationMap.put("Betanex", "Herbicides");
		indicationMap.put("Bollgard", "Traits");
		indicationMap.put("Bollgard II", "Traits");
		indicationMap.put("Bronate Advanced", "Herbicides");
		indicationMap.put("Buctril", "Herbicides");
		indicationMap.put("Calypso", "Insecticides");
		indicationMap.put("Capreno", "Herbicides");
		indicationMap.put("Celgard", "SeedGrowth");
		indicationMap.put("Celpass", "SeedGrowth");
		indicationMap.put("Celpril", "SeedGrowth");
		indicationMap.put("Certified FiberMax", "Seeds");
		indicationMap.put("Cheyenne", "Herbicides");
		indicationMap.put("Contain", "SeedGrowth");
		indicationMap.put("COPeO Prime", "SeedGrowth");
		indicationMap.put("Corvus", "Herbicides");
		indicationMap.put("Corvus Complete", "Herbicides");
		indicationMap.put("Cotguard", "Fungicides");
		indicationMap.put("Credenz", "Seeds");
		indicationMap.put("Cruiser", "Insecticides");
		indicationMap.put("CSI Safener", "SeedGrowth");
		indicationMap.put("CSI Safener 500FS", "SeedGrowth");
		indicationMap.put("Decis", "Insecticides");
		indicationMap.put("Delaro", "Fungicides");
		indicationMap.put("Di-Syston", "Insecticides");
		indicationMap.put("DiFlexx", "Herbicides");
		indicationMap.put("DiFlexx Duo", "Herbicides");
		indicationMap.put("Distinguish", "Fungicides");
		indicationMap.put("Dropp", "Herbicides");
		indicationMap.put("Dropp SC", "Herbicides");
		indicationMap.put("Elite", "Fungicides");
		indicationMap.put("Emesto", "SeedGrowth");
		indicationMap.put("Emesto Silver", "SeedGrowth");
		indicationMap.put("Envidor", "Insecticides");
		indicationMap.put("Ethrel", "Harvest Aids/PGR");
		indicationMap.put("EverGol", "SeedGrowth");
		indicationMap.put("Evolve", "SeedGrowth");
		indicationMap.put("FiberMax", "Seeds");
		indicationMap.put("FiberMax One Ton Club", "Seeds");
		indicationMap.put("Finish", "Harvest Aids/PGR");
		indicationMap.put("Finish 6 Pro", "Harvest Aids/PGR");
		indicationMap.put("Flint", "Fungicides");
		indicationMap.put("Fluency Agent", "SeedGrowth");
		indicationMap.put("Fluency Agent Advanced", "SeedGrowth");
		indicationMap.put("Folicur", "Fungicides");
		indicationMap.put("Gaucho", "SeedGrowth");
		indicationMap.put("Gaucho Grande", "SeedGrowth");
		indicationMap.put("Gaucho MZ", "SeedGrowth");
		indicationMap.put("Gaucho XT", "SeedGrowth");
		indicationMap.put("Gem", "Fungicides");
		indicationMap.put("Ginstar", "Harvest Aids/PGR");
		indicationMap.put("Ginstar EC", "Harvest Aids/PGR");
		indicationMap.put("Glyphomax", "Herbicides");
		indicationMap.put("GlyTol", "Traits");
//		indicationMap.put("Grower Dividends", "");
//		indicationMap.put("Growing Strong", "");
		indicationMap.put("Gustafson", "Equipment");
		indicationMap.put("Guthion", "Insecticides");
		indicationMap.put("HBK", "Seeds");
		indicationMap.put("Heads Up", "SeedGrowth");
		indicationMap.put("Herculex", "Traits");
		indicationMap.put("Herculex I", "Traits");
		indicationMap.put("Hoegemeyer", "Seeds");
		indicationMap.put("Hoelon", "Herbicides");
		indicationMap.put("Huskie", "Herbicides");
		indicationMap.put("Huskie Complete", "Herbicides");
//		indicationMap.put("ICP - Horticulture", "");
//		indicationMap.put("ICP - Innovation Plus", "");
//		indicationMap.put("ICP - Cereals", "");
//		indicationMap.put("ICP - Corn", "");
//		indicationMap.put("ICP - Multiple", "");
//		indicationMap.put("ICP - Southern Row Crops", "");
//		indicationMap.put("ICP - Soybeans", "");
		indicationMap.put("Idol", "SeedGrowth");
		indicationMap.put("Ignite", "Herbicides");
		indicationMap.put("Ilevo", "SeedGrowth");
		indicationMap.put("Innovation Plus", "");
//		indicationMap.put("InSeed", "");
		indicationMap.put("Intruder", "Insecticides");
		indicationMap.put("InVigor", "Seeds");
		indicationMap.put("Ipresso", "SeedGrowth");
		indicationMap.put("Kodiak", "Fungicides");
		indicationMap.put("Krovar", "Herbicides");
		indicationMap.put("Larvin", "Insecticides");
		indicationMap.put("Laudis", "Herbicides");
		indicationMap.put("Laudis Flexx", "Herbicides");
		indicationMap.put("Leverage", "Insecticides");
		indicationMap.put("Leverage 2.7", "Insecticides");
		indicationMap.put("Leverage 360", "Insecticides");
		indicationMap.put("Liberty", "Herbicides");
		indicationMap.put("LibertyLink", "Traits");
		indicationMap.put("Luna", "Fungicides");
		indicationMap.put("Mocap", "Insecticides");
		indicationMap.put("Monitor", "Insecticides");
		indicationMap.put("Movento", "Insecticides");
		indicationMap.put("Movento HL", "Insecticides");
		indicationMap.put("Nemacur", "Insecticides");
		indicationMap.put("Nortron", "Herbicides");
		indicationMap.put("Nutrikote", "SeedGrowth");
		indicationMap.put("Oberon", "Insecticides");
		indicationMap.put("Olympus", "Herbicides");
		indicationMap.put("Olympus Flex", "Herbicides");
		indicationMap.put("On Demand", "Equipment");
		indicationMap.put("Option", "Herbicides");
		indicationMap.put("Osprey", "Herbicides");
		indicationMap.put("Phaser", "Insecticides");
		indicationMap.put("Poncho", "SeedGrowth");
		indicationMap.put("Poncho 1250", "SeedGrowth");
		indicationMap.put("Poncho 250", "SeedGrowth");
		indicationMap.put("Poncho 250/1250", "SeedGrowth");
		indicationMap.put("Poncho 251", "SeedGrowth");
		indicationMap.put("Poncho 500", "SeedGrowth");
		indicationMap.put("Poncho Beta", "SeedGrowth");
		indicationMap.put("Poncho/VOTiVO", "SeedGrowth");
		indicationMap.put("Poncho/VOTiVO 2.0", "SeedGrowth");
		indicationMap.put("Prep", "Harvest Aids/PGR");
		indicationMap.put("Prep 6", "Harvest Aids/PGR");
		indicationMap.put("Previcur", "Fungicides");
		indicationMap.put("Previcur Flex", "Fungicides");
		indicationMap.put("Proceed", "SeedGrowth");
		indicationMap.put("Proceed Concentrate", "SeedGrowth");
		indicationMap.put("Proceed MD", "SeedGrowth");
//		indicationMap.put("Producer Information Exchange (P.I.E.)", "");
		indicationMap.put("Proline", "Fungicides");
		indicationMap.put("Proline 3+3", "Fungicides");
		indicationMap.put("ProPulse", "Fungicides");
		indicationMap.put("Prosaro", "Fungicides");
		indicationMap.put("Prosper", "SeedGrowth");
		indicationMap.put("Prosper FX", "SeedGrowth");
		indicationMap.put("Protech Plus", "Insecticides");
		indicationMap.put("ProTechPlus", "Insecticides");
		indicationMap.put("Protégé", "Fungicides");
		indicationMap.put("Protégé FL", "Fungicides");
		indicationMap.put("Provado", "Insecticides");
		indicationMap.put("Provost", "Fungicides");
		indicationMap.put("Provost Opti", "Fungicides");
		indicationMap.put("Puma", "Herbicides");
		indicationMap.put("Radius", "Herbicides");
		indicationMap.put("Raxil", "SeedGrowth");
		indicationMap.put("Raxil Extra", "SeedGrowth");
		indicationMap.put("Raxil MD", "SeedGrowth");
		indicationMap.put("Raxil MD Extra", "SeedGrowth");
		indicationMap.put("Raxil MD Extra W", "SeedGrowth");
		indicationMap.put("Raxil MD W", "SeedGrowth");
		indicationMap.put("Raxil Pro MD", "SeedGrowth");
		indicationMap.put("Raxil Pro Shield", "SeedGrowth");
		indicationMap.put("Raxil Sure", "SeedGrowth");
		indicationMap.put("Raxil XT", "SeedGrowth");
		indicationMap.put("Reason", "Fungicides");
		indicationMap.put("Redigo", "SeedGrowth");
		indicationMap.put("Rely", "Herbicides");
		indicationMap.put("Rely 200", "Herbicides");
		indicationMap.put("Rely 280", "Herbicides");
		indicationMap.put("Renounce", "Insecticides");
		indicationMap.put("Requiem", "Insecticides");
		indicationMap.put("Respect the Rotation", "Herbicides");
		indicationMap.put("Rhizokote", "SeedGrowth");
		indicationMap.put("Ricestar", "Herbicides");
		indicationMap.put("Ricestar HT", "Herbicides");
		indicationMap.put("Rimfire", "Herbicides");
		indicationMap.put("Rimfire Max", "Herbicides");
		indicationMap.put("Rovral", "Fungicides");
		indicationMap.put("Scala", "Fungicides");
		indicationMap.put("Scoparia", "Herbicides");
		indicationMap.put("SeedGrowth", "SeedGrowth");
		indicationMap.put("Sencor", "Herbicides");
		indicationMap.put("Sensat", "SeedGrowth");
		indicationMap.put("Sepresto", "SeedGrowth");
		indicationMap.put("Serenade", "Fungicides");
		indicationMap.put("Serenade ASO", "Fungicides");
		indicationMap.put("Serenade Opti", "Fungicides");
		indicationMap.put("Serenade Optimum", "Fungicides");
		indicationMap.put("Serenade Soil", "Fungicides");
		indicationMap.put("Sevin", "Insecticides");
		indicationMap.put("Silverado", "Herbicides");
		indicationMap.put("Sivanto", "Insecticides");
		indicationMap.put("Sivanto Prime", "Insecticides");
		indicationMap.put("Soberan", "Herbicides");
		indicationMap.put("Sonata", "Fungicides");
		indicationMap.put("Spin-Aid", "Herbicides");
		indicationMap.put("Stance", "Harvest Aids/PGR");
		indicationMap.put("Stoneville", "Seeds");
		indicationMap.put("Storcide II", "SeedGrowth");
		indicationMap.put("Storicide II", "SeedGrowth");
		indicationMap.put("Stratego", "Fungicides");
		indicationMap.put("Stratego YLD", "Fungicides");
		indicationMap.put("Synapse", "Insecticides");
		indicationMap.put("Temik", "Insecticides");
		indicationMap.put("Temix", "Insecticides");
		indicationMap.put("Tempo SC Ultra", "Insecticides");
		indicationMap.put("Thiram", "SeedGrowth");
		indicationMap.put("Tops MZ", "SeedGrowth");
		indicationMap.put("Tops MZ Gaucho", "SeedGrowth");
		indicationMap.put("TriKote", "SeedGrowth");
		indicationMap.put("Trilex", "SeedGrowth");
		indicationMap.put("Trilex 2000", "SeedGrowth");
		indicationMap.put("Trilex 5000", "SeedGrowth");
		indicationMap.put("Trilex 6000", "SeedGrowth");
		indicationMap.put("Trilex Advanced", "SeedGrowth");
		indicationMap.put("Trilex AL", "SeedGrowth");
		indicationMap.put("Trilex ADV", "SeedGrowth");
		indicationMap.put("Trilex Optimum", "SeedGrowth");
		indicationMap.put("Trilex Rice Pack", "SeedGrowth");
		indicationMap.put("TrilexStar", "SeedGrowth");
		indicationMap.put("Trimax", "Insecticides");
		indicationMap.put("Trimax Pro", "Insecticides");
		indicationMap.put("TwinLink", "Traits");
		indicationMap.put("TwinLink Plus", "Traits");
		indicationMap.put("Ultor", "Insecticides");
		indicationMap.put("Varro", "Herbicides");
		indicationMap.put("Velum One", "Insecticides");
		indicationMap.put("Velum Prime", "Insecticides");
		indicationMap.put("Velum Total", "Insecticides");
		indicationMap.put("Vitis", "Insecticides");
		indicationMap.put("Vortex", "SeedGrowth");
		indicationMap.put("Votivo", "SeedGrowth");
		indicationMap.put("Whip 360", "Herbicides");
		indicationMap.put("Wolverine", "Herbicides");
		indicationMap.put("Wolverine Advanced", "Herbicides");
		indicationMap.put("Wolverine PowerPak", "Herbicides");
		indicationMap.put("Yield Shield", "Fungicides");
		
		cropMap = new HashMap<String, String>();
		cropMap.put("Berries", "Berries");
		cropMap.put("Brassica Vegetables", "Vegetable, Brassica");
		cropMap.put("Bulb Vegetables", "Vegetable, Bulb");
		cropMap.put("Cereals", "Cereals");
		cropMap.put("Citrus", "Citrus");
		cropMap.put("Corn", "Corn");
		cropMap.put("Cotton", "Cotton");
		cropMap.put("Cucurbits", "Cucurbits");
		cropMap.put("Fruiting Vegetables", "Vegetable, Fruiting");
		cropMap.put("Grapes", "Grapes");
		cropMap.put("Leafy Vegetables", "Vegetable, Leafy");
		cropMap.put("Legume Vegetables", "Vegetable, Legume");
		cropMap.put("Peanuts", "Peanuts");
		cropMap.put("Pome Fruit", "Fruit, Pome");
		cropMap.put("Rice", "Rice");
		cropMap.put("Root And Tuber Vegetables", "Vegetable, Root and Tuber");
		cropMap.put("Soybeans", "Soybeans");
		cropMap.put("Stone Fruit", "Fruit, Stone");
		cropMap.put("Sugarbeets", "Sugarbeets");
		cropMap.put("Tobacco", "Tobacco");
		cropMap.put("Tree Nuts", "Tree Nuts");
		cropMap.put("Tropical Fruits", "Fruit, Other");
		cropMap.put("Potatoes", "Potatoes");
		cropMap.put("Canola", "Canola");
		cropMap.put("Cereal", "Cereals");
		cropMap.put("Fruit Other", "Fruit, Other");
		cropMap.put("Fruit Pome", "Fruit, Pome");
		cropMap.put("Fruit Stone", "Fruit, Stone");
		cropMap.put("Grape", "Grapes");
		cropMap.put("Peanut", "Peanuts");
		cropMap.put("Potato", "Potatoes");
		cropMap.put("Sorghum", "Sorghum");
		cropMap.put("Soybean", "Soybeans");
		cropMap.put("Sugarbeet", "Sugarbeets");
		cropMap.put("Vegetable", "Vegetable, Other");
	}

	private List<String[]> inputRows;
	private HashMap<String, String> licenses = new HashMap<String, String>();
	private HashMap<Integer, CropScienceAsset> assets = new HashMap<Integer, CropScienceAsset>();
	private CsvParserSettings parserSettings = new CsvParserSettings();
	
	private SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	private SimpleDateFormat outputDateFormat = new SimpleDateFormat("MM/dd/yyyy");
	
	private HashSet<String> marketingIDs = new HashSet<String>();

	public ToolboxImportFileGenerator() throws Exception {
		parserSettings.getFormat().setLineSeparator("\r\n");
		parserSettings.getFormat().setDelimiter(';');
		parserSettings.getFormat().setCharToEscapeQuoteEscaping('"');
		parserSettings.setHeaderExtractionEnabled(false);

		CsvParser parser = new CsvParser(parserSettings);

		inputRows = parser.parseAll(new File(toolboxExportFilePath));
		
		List<String[]> licenseGuidRows = parser.parseAll(new File(licenseGuidsFilePath));
		for (String[] line : licenseGuidRows) {
			if (!licenses.containsKey(line[1])) {
				licenses.put(line[1], line[0]);
			}
			else {
				log.error("License " + line[1] + " has multiple keys");
			}
		}
		
		generateAssetDecriptions();
		writeImportFile();
	}

	private void generateAssetDecriptions() throws Exception {

		long lineNo = 0;
		HashSet<Integer> processedAssetIDs = new HashSet<Integer>();
		long assetsIncluded = 0;
		long originalFilenameNull = 0;
		long noTargetBrand = 0;
		long noTargetBusinessUnit = 0;
		long noAssetType = 0;
		long internalAssets = 0;
		long statusSkipped = 0;
		long assetTypeNull = 0;
		long releaseMissing = 0;

		for (String[] line : inputRows) {
			Integer assetId = Integer.parseInt(line[0]);

			log.debug("Processing AssetID " + assetId);
			lineNo++;
			processedAssetIDs.add(assetId);
			
			boolean photoGalleryItem = false;
			if (line.length > 52 && line[52].equals("3")) 
				photoGalleryItem = true;

			// check exclusion criteria
			if (line[1].equals("NULL")) {
				log.warn("Skipping AssetID " + assetId + ", line " + lineNo + " - OriginalFilename is 'NULL'");
				originalFilenameNull++;
				continue;
			}

			if (brandMap.get(line[14]) == null) {
				log.warn("Skipping AssetID " + assetId + ", line " + lineNo + " - no target brand for '" + line[14] + "'");
				noTargetBrand++;
				continue;
			}

			if (unitMap.get(brandMap.get(line[14])) == null) {
				log.warn("Skipping AssetID " + assetId + ", line " + lineNo + " - no target business unit for brand '" + brandMap.get(line[14]) + "'");
				noTargetBusinessUnit++;
				continue;
			}
			
			if (!line[25].equals("Publish") && !line[25].equals("Archive")) {
				log.info("Skipping AssetID " + assetId + ", line " + lineNo + " - status is '" + line[25] + "'");
				statusSkipped++;
				continue;
			}
			
			if (photoGalleryItem) {
				// Photo Gallery: Copy "KeyElementName" to "AssetItemName" in order to map category
				if (!line[11].equals("NULL")) {
					log.error("line[11] 'AssetCategoryName' should be empty for Photo Gallery asset!");
					System.exit(1);
				}
				line[11] = "PHOTO GALLERY";
				line[12] = line[42];
			}
			else if (line[26].equals("NULL")) {
				log.warn("Skipping AssetID " + assetId + ", line " + lineNo + " - asset type is " + line[26]);
				assetTypeNull++;
				continue;
			}

			CropScienceAsset asset = assets.get(assetId);

			if (asset == null) {
				assetsIncluded++;
				asset = new CropScienceAsset(assetId);

				asset.assetPath = uploadPath;
				asset.filename = line[1];

				asset.byr_Title = line[2];
				asset.byr_cs_assetApproval = "Approval Required";
				asset.byr_cs_country = "United States";

				asset.byr_MarketingId = line[3];
				if (!marketingIDs.add(asset.byr_MarketingId)) {
					CropScienceAsset assetWithMarketingID = null;
					for (CropScienceAsset ast : assets.values()) {
						if (ast.byr_MarketingId.equals(asset.byr_MarketingId)) {
							assetWithMarketingID = ast;
							break;
						}
					}
					
					log.error(asset.toolboxAssetId + ", line " + lineNo + ": marketing ID " + asset.byr_MarketingId + " already bound to asset " + assetWithMarketingID.toolboxAssetId);
				}
				
//				asset.fileSize = Long.parseLong(line[15]);
				
				if (!line[16].equals("NULL"))
					asset.byr_cs_releaseDate = outputDateFormat.format(inputDateFormat.parse(line[16]));
				if (!line[17].equals("NULL"))
					asset.byr_cs_retireDate = outputDateFormat.format(inputDateFormat.parse(line[17]));

				if (!photoGalleryItem)
					asset.byr_campaignYearLabel = line[18];
				
				if (!line[19].equals("NULL"))
					asset.byr_campaignTitle = line[19];
				
				if (line[20].equals("0")) { // "Internal"
					asset.byr_cs_IsShouldBePublished = "No";
				}
				else {
					internalAssets++;
					asset.byr_cs_IsShouldBePublished = "Yes";
				}
				
				if (line[24] != null && line[24].equals("HH Global"))
					asset.byr_cs_warehouseAvailable = "Yes";
				else
					asset.byr_cs_warehouseAvailable = "No";
				
				
				
				/* description, keywords and classifications */
				
				asset.byr_cs_description = "";
				if (line[4] != null)
					asset.byr_cs_description = line[4] + "\n\n\n";
				asset.byr_cs_description += "[Asset imported from Toolbox (ID " + asset.toolboxAssetId + "), created "
						+ outputDateFormat.format(inputDateFormat.parse(line[6])) + " by " + line[21] + " " + line[22] + " (" + line[23]
						+ ")]";
				
				if (photoGalleryItem) {
					if (!line[41].equals("NULL")  && line[40].length() > 0);
						injectDescription(asset, "Geographic Information: " + line[41]);
				}

				
				asset.byr_cs_keywords_tl = "toolbox import";
				
				if (photoGalleryItem && !line[44].equals("NULL") && line[44].length() > 0)
					asset.byr_cs_keywords_tl += "\n" + line[44];
				
				asset.appendClassification = "/Definitions/Agencies/Bayer/Bayer CS";
				
				if (line[25].equals("Publish"))
					asset.appendClassification += "\n/Definitions/Common/Status/Released";
				else // "Archive"
					asset.appendClassification += "\n/Definitions/Common/Status/Retired";
					
				asset.byr_licenseScope = "US";
				asset.byr_licenseOwner = "Bayer CropScience LP";
				
				
				// Photo Gallery
				if (photoGalleryItem) {
					
					asset.byr_cropYear = line[18];
					
					if (!line[40].equals("NULL") && line[40].length() > 0)
						asset.byr_cs_location = line[40];
					
					if (!line[49].equals("NULL") && line[49].length() > 0) {
						String event = line[49];
						if (!line[50].equals("NULL") && line[50].length() > 0)
							event += ": " + line[50];
						injectDescription(asset, event);
					}
					
					if (!line[51].equals("NULL") && line[51].length() > 0) {
						injectDescription(asset, "Names of persons in photograph: " + line[51]);
					}
				}

				assets.put(assetId, asset);
			}
			
			// determine asset type (from Toolbox "category")
			String assetCategory = line[11] + " : " + line[12];
			String targetType = categoryMap.get(assetCategory);
			if (targetType != null) {
				String categoryClassification = targetType.substring(0, targetType.indexOf(" : "));
				String subCategoryClassification = targetType.substring(targetType.indexOf(" : ") + 3);
				
				appendClassification(asset, "/Bayer/Crop Science/CategoryItem/" + categoryClassification);
				appendClassification(asset, "/Bayer/Crop Science/CategoryItem/" + categoryClassification + "/" + subCategoryClassification);
			}
			else {
				noAssetType++;
				log.warn(asset.toolboxAssetId + ", line " + lineNo + ": Could not map category '" + assetCategory + "' to any asset type");
			}
			
			if (photoGalleryItem) {
				if (cropMap.get(line[43]) != null) {
					appendClassification(asset, "/Bayer/Crop Science/Crops/" + cropMap.get(line[43]));
				}
				if (cropMap.get(line[45]) != null) {
					if (appendClassification(asset, "/Bayer/Crop Science/Crops/" + cropMap.get(line[45]))) {
						if (line[46] != null && !line[46].equals("NULL") && line[46].length() > 0) {
							injectDescription(asset, line[45] + " in stage " + line[46]);
						}
					}
				}
				
				if (!line[47].equals("NULL") && line[47].length() > 0) {
					String pest = line[47];
					if (!line[48].equals("NULL") && line[48].length() > 0)
						pest += ": " + line[48];
					injectDescription(asset, pest);
				}
			}

			
			// Special rules for brands
			if (line[14].equals("Fluency Agent/Fluency Agent Advanced")) {
				asset.brands.add(brandMap.get("Fluency Agent"));
				asset.brands.add(brandMap.get("Fluency Agent Advanced"));
			}
			else if (line[14].equals("Poncho/VOTiVO 2")) {
				asset.brands.add(brandMap.get("Poncho"));
				asset.brands.add(brandMap.get("Votivo"));
			}
			else if (line[14].equals("Poncho 1250/VOTiVO 2")) {
				asset.brands.add(brandMap.get("Poncho 1250"));
				asset.brands.add(brandMap.get("Votivo"));
			}
			else {
				// default
				asset.brands.add(brandMap.get(line[14]));
			}
			
			if (asset.byr_cs_businessUnit == null) {
				asset.byr_cs_businessUnit = unitMap.get(brandMap.get(line[14]));
			}
//			else if (!asset.byr_cs_businessUnit.equals(unitMap.get(brandMap.get(line[14])))) {
//				
//				String assignedBrands = "";
//				for (String brand : asset.brands) {
//					if (assignedBrands.length() > 0)
//						assignedBrands += ", ";
//					assignedBrands += brand;
//				}
//				
//				log.error("Asset " + asset.toolboxAssetId + ": Additional brand " + brandMap.get(line[14]) + " ("
//						+ unitMap.get(brandMap.get(line[14])) + ") does not belong to business unit " + asset.byr_cs_businessUnit
//						+ " of already assigned brand(s) " + assignedBrands);
//			}
			
			if (indicationMap.get(line[14]) != null)
				asset.indications.add(indicationMap.get(line[14]));
			
			if (!line[34].equals("NULL")) {
				if (licenses.containsKey(line[34])) {
					if (asset.licenseRecords.add(licenses.get(line[34]))) {

						if (line[30].equals("1"))
							asset.licenseTypes.add("License Agreement");
						else if (line[30].equals("2"))
							asset.licenseTypes.add("Written permission release");
						else if (line[30].equals("3"))
							asset.licenseTypes.add("Testimonial release");
						else if (line[30].equals("4"))
							asset.licenseTypes.add("Location release");
						else if (line[30].equals("5"))
							asset.licenseTypes.add("Talent/Music release");

						if (asset.byr_licenseInformation.length() > 0)
							asset.byr_licenseInformation += "\n";

						asset.byr_licenseInformation += "License imported from Toolbox. Licensor: " + line[32] + ", Creator/Company: "
								+ line[36] + " " + line[37] + "/" + line[38] + ", Toolbox upload date: "
								+ outputDateFormat.format(inputDateFormat.parse(line[33]));

						if (asset.byr_licenseInformation.length() > 3800)
							log.error("Asset ID " + asset.toolboxAssetId + ": License information too long - "
									+ asset.byr_licenseInformation.length());
					}
				}
				else {
					log.error("Asset ID " + asset.toolboxAssetId + ", line " + lineNo + ": Could not find release file " + line[34]);
					releaseMissing++;
				}
			}

		}

		log.info("Processed lines   : " + lineNo);
		log.info("Processed assets  : " + processedAssetIDs.size());
		log.info("Assets included   : " + assetsIncluded);
		log.info("Lines Skipped: Original filname NULL: " + originalFilenameNull);
		log.info("Lines Skipped: No target brand: " + noTargetBrand);
		log.info("Lines Skipped: No target business unit: " + noTargetBusinessUnit);
		log.info("Lines Skipped: Status is not 'Publish' or 'Archive': " + statusSkipped);
		log.info("Lines Skipped: Asset type NULL: " + assetTypeNull);
		log.info("Release missing: " + releaseMissing);
		log.info("No asset type: " + noAssetType);
		log.info("Internal assets: " + internalAssets);
	}
	
	private boolean injectDescription(CropScienceAsset asset, String desciption) {
		if (asset.byr_cs_description == null || asset.byr_cs_description.length() == 0) {
			asset.byr_cs_description = desciption;
			return true;
		}
		
		String[] descLines = asset.byr_cs_description.split("\n");
		for (int i = 0; i < descLines.length; i++) {
			if (descLines[i].equals(desciption))
				return false;
		}
		
		int position = asset.byr_cs_description.indexOf("\n\n[Asset imported from Toolbox");
		
		if (position < 0) {
			log.warn("No import description found");
			asset.byr_cs_description += "\n" + desciption;
			return true;
		}
		
		asset.byr_cs_description = asset.byr_cs_description.substring(0, position) + "\n" + desciption + asset.byr_cs_description.substring(position);
		return true;
	}
	
	private boolean appendClassification(CropScienceAsset asset, String classification) {
		if (asset.appendClassification == null || asset.appendClassification.length() == 0) {
			asset.appendClassification = classification;
			return true;
		}
		
		String[] setClassifications = asset.appendClassification.split("\n");
		
		for (int i = 0; i < setClassifications.length; i++) {
			if (setClassifications[i].equals(classification))
				return false;
		}
		
		asset.appendClassification += "\n" + classification;
		return true;
	}

	private void writeImportFile() {
		List<Object[]> csvOutputList = new ArrayList<Object[]>();
		for (CropScienceAsset asset : assets.values()) {
			
//			// XXX export only assets with license data
//			if (asset.licenseRecords.size() == 0)
//				continue;
			
//			if (!asset.filename.equals("BCS_SED_I4_6666_140x180_Concept_R2[1].jpg"))
//				continue;
			
			// XXX This filename is broken (because of the question mark)
			if (asset.filename.equals("Texas Cotton Growers Achieve Excellence with FiberMax?.mp4"))
				asset.filename = "CR0715FIBMAXA232V00R0.MP4";
			
			if (selection.length > 0) {
				boolean found = false;
				for (String s : selection) {
					if (s.equals(asset.filename))
						found = true;
				}
				if (!found)
					continue;
			}

			String brands = "";
			for (String brand : asset.brands) {
				if (brands.length() > 0)
					brands += "\n";
				brands += brand;
			}
			
			for (String indication : asset.indications)
				asset.appendClassification += "\n/Bayer/Crop Science/Indication/" + indication;
			
			String licenseRecords = "";
			for (String licenseRecord : asset.licenseRecords) {
				if (licenseRecords.length() > 0)
					licenseRecords += "\n";
				licenseRecords += licenseRecord;
			}
			
			String licenseTypes = "";
			for (String licenseType : asset.licenseTypes) {
				if (licenseTypes.length() > 0)
					licenseTypes += "\n";
				licenseTypes += licenseType;
			}
			if (licenseTypes.length() == 0)
				licenseTypes = "No License required";
			
			
			String[] csvLine = new String[] { asset.assetPath, asset.filename, asset.appendClassification, asset.byr_Title,
					asset.byr_MarketingId, asset.byr_cs_assetApproval, asset.byr_cs_businessUnit,
					brands, asset.byr_cs_releaseDate, asset.byr_cs_retireDate, asset.byr_cs_description, asset.byr_cs_keywords_tl,
					asset.byr_campaignYearLabel, asset.byr_campaignTitle, asset.byr_cs_IsShouldBePublished, asset.byr_cs_warehouseAvailable,
					asset.byr_cs_country, licenseRecords, licenseTypes, asset.byr_licenseScope, asset.byr_licenseOwner,
					asset.byr_licenseInformation, asset.byr_cs_location, asset.byr_cropYear };
			
			csvOutputList.add(csvLine);
		}

		CsvWriterSettings writerSettings = new CsvWriterSettings();
		writerSettings.setFormat(parserSettings.getFormat());

		CsvWriter writer = new CsvWriter(new File(importFilePath), writerSettings);
		writer.writeHeaders("Assetpath", "Filename", "Append Classification", "fields|byr_Title", "fields|byr_MarketingId",
				"fields|byr_cs_assetApproval", "fields|byr_cs_businessUnit", "fields|byr_cs_brand", "fields|byr_cs_releaseDate",
				"fields|byr_cs_retireDate", "fields|byr_cs_Description", "fields|byr_cs_keywords_tl", "fields|byr_campaignYearLabel",
				"fields|byr_campaignTitleLabel", "fields|byr_cs_IsShouldBePublished", "fields|byr_cs_warehouseAvailable",
				"fields|byr_cs_country", "fields|byr_cs_licenseRecord", "fields|byr_cs_licenseType", "fields|byr_licenseScope",
				"fields|byr_licenseOwner", "fields|byr_licenseInformation", "fields|byr_cs_location", "fields|byr_cropYear");
		writer.writeRowsAndClose(csvOutputList);
	}

	public static void main(String[] args) {
		try {
			Layout layout = new PatternLayout("%p %m%n"); // "%d{ISO8601} - %p %m%n"
			BasicConfigurator.configure(new ConsoleAppender(layout));
			BasicConfigurator.configure(new RollingFileAppender(layout, logfilePath, false));
			log.setLevel(Level.INFO);

			new ToolboxImportFileGenerator();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
