package com.bayer.edam;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
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 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.
 *
 * <pre>
 * SELECT
 * a.AssetId, a.OriginalFileName, a.UserFileName, a.BCSNumber, a.Description, a.AssetStatusID, a.PublishDate,
 * a.CreatedOn, a.CreatedByUserID, a.UpdatedOn, a.UpdatedByUserID, a.CopyClaimFileName
 * ,c.AssetCategoryName
 * ,ai.AssetItemName
 * ,f.FocusItemID
 * ,fi.FocusItemName
 * ,img.OriginalSize
 * ,a.publishDate
 * ,a.archiveDate
 * 
 * FROM Assets a
 * LEFT JOIN AssetCategories c ON a.AssetCategoryID = c.AssetCategoryID
 * LEFT JOIN AssetItems ai ON a.AssetItemID = ai.AssetItemID
 * LEFT JOIN AssetFocusItemMap f ON a.AssetId = f.AssetID
 * LEFT JOIN FocusItems fi ON f.FocusItemID = fi.FocusItemID
 * LEFT JOIN AssetImages img ON a.AssetID = img.AssetID
 * 
 * WHERE 
 * a.AssetStatusID != 101
 * AND a.CreatedOn > Convert(datetime, '2012-07-01')
 * AND a.isDeleted = 0
 * AND (a.FocusID IS NULL OR a.FocusID != 3 OR (a.FocusID = 3 AND a.CreatedOn > Convert(datetime, '2016-01-01')))
 * --AND a.AssetID IN (23467,23392,22868,22488,22481,22440,22082,22021,21147,20731,
 * --19557,19048,18447,18446,18445,18444,16853,16171,15882,15313,14918,14400,
 * --13660,13641,13348,13323,13142,13135,12844,12814,12007)
 * </pre>
 * 
 * @author GEXCE
 */
public class ToolboxImportFileGenerator {
	private static Logger log = Logger.getLogger(ToolboxImportFileGenerator.class);

	private static String toolboxExportFilePath = "C:\\Temp\\Migration CS - SQL Server\\auszug.csv";
	private static String importFilePath = "C:\\Temp\\Migration CS - SQL Server\\importFile.csv";

	private static final Map<String, String> brandMap;
	private static final Map<String, String> unitMap;

	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");
		brandMap.put("Prot�g� FL", "TEMPORARY");
		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");

		// TODO to be checked by business
		brandMap.put("NULL", "TEMPORARY"); // Bayer/General?
		brandMap.put("131-Kote", "TEMPORARY");
		brandMap.put("Cotgard", "TEMPORARY");
		brandMap.put("Fluency Agent/Fluency Agent Advanced", "TEMPORARY"); // --> set both brands via special rule
		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("Poncho/VOTiVO 2", "TEMPORARY"); // set both 'Poncho' and 'Votivo' via special rule
		brandMap.put("Poncho 1250/VOTiVO 2", "TEMPORARY"); // set both 'Poncho 250/1250' and 'Votivo' via special rule (there is no Votivo
															// 2)
		brandMap.put("Velum", "Velum One");
		brandMap.put("Velum Prime 3", "Velum Prime");

		unitMap = new HashMap<String, String>();
		unitMap.put("Bayer/General", "Crop Protection");
		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");
	}

	private List<String[]> inputRows;
	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");

	public ToolboxImportFileGenerator() throws Exception {
		readToolboxExport();
		generateAssetDecriptions();
		writeImportFile();
	}

	private void generateAssetDecriptions() throws Exception {

		long processedLines = 0;
		long assetsIncluded = 0;
		long originalFilenameNull = 0;
		long noTargetBrand = 0;
		long noTargetBusinessUnit = 0;

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

			log.debug("Processing AssetID " + assetId);
			processedLines++;

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

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

			if (unitMap.get(brandMap.get(line[15])) == null) {
				log.warn("Skipping AssetID " + assetId + " - no target business unit for brand '" + brandMap.get(line[15]) + "'");
				noTargetBusinessUnit++;
				continue;
			}

			CropScienceAsset asset = assets.get(assetId);

			if (asset == null) { // Fields|byr_cs_country
				assetsIncluded++;
				asset = new CropScienceAsset(assetId);

				asset.assetPath = "/Resources/Upload/Toolbox";
				asset.filename = line[1];

				asset.byr_Title = line[2];
				asset.byr_cs_assetApproval = "No Approval"; // XXX "No Approval Required"?
				
				if (!line[17].equals("NULL"))
					asset.byr_cs_releaseDate = outputDateFormat.format(inputDateFormat.parse(line[17]));
				if (!line[18].equals("NULL"))
					asset.byr_cs_retireDate = outputDateFormat.format(inputDateFormat.parse(line[18]));

				asset.byr_cs_license_classification = "N/A";
				
				if (line[4] != null)
					asset.byr_cs_description = line[4] + "\n\n[Asset imported from Toolbox, ID " + asset.toolboxAssetId + "]";
				else
					asset.byr_cs_description = "[Asset imported from Toolbox, ID " + asset.toolboxAssetId + "]";
				
				
				asset.byr_cs_keywords_tl = "toolbox import";

				asset.byr_MarketingId = line[3];

				asset.appendClassification = "/Definitions/Common/Status/Released";
				
				asset.fileSize = Long.parseLong(line[16]);

				assets.put(assetId, asset);
			}

			if (asset.byr_cs_brand == null) {
				asset.byr_cs_brand = brandMap.get(line[15]);
				asset.byr_cs_businessUnit = unitMap.get(asset.byr_cs_brand);

			} else {
				asset.byr_cs_brand = asset.byr_cs_brand + "\n" + line[15];
			}

		}

		log.info("Processed lines: " + processedLines);
		log.info("Assets included: " + assetsIncluded);
		log.info("Original filname NULL: " + originalFilenameNull);
		log.info("No target brand: " + noTargetBrand);
		log.info("No target business unit: " + noTargetBusinessUnit);
	}

	private void readToolboxExport() {
		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));
	}

	private void writeImportFile() {
		List<Object[]> csvOutputList = new ArrayList<Object[]>();
		for (CropScienceAsset asset : assets.values()) {
			String[] csvLine = new String[] { asset.assetPath, asset.filename, asset.appendClassification, asset.byr_Title,
					asset.byr_MarketingId, asset.byr_cs_assetApproval, asset.byr_cs_license_classification, asset.byr_cs_brand, asset.byr_cs_businessUnit,
					asset.byr_cs_releaseDate, asset.byr_cs_retireDate, asset.byr_cs_description, asset.byr_cs_keywords_tl };
			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_license_classification", "fields|byr_cs_brand", "fields|byr_cs_businessUnit",
				"fields|byr_cs_releaseDate", "fields|byr_cs_retireDate", "fields|byr_cs_Description", "fields|byr_cs_keywords_tl");
		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, "ToolboxImportFileGenerator.log", true));
			log.setLevel(Level.INFO);

			new ToolboxImportFileGenerator();

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

}
