/*
 * Copyright (c) 2015 Memorial Sloan-Kettering Cancer Center.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS
 * FOR A PARTICULAR PURPOSE. The software and documentation provided hereunder
 * is on an "as is" basis, and Memorial Sloan-Kettering Cancer Center has no
 * obligations to provide maintenance, support, updates, enhancements or
 * modifications. In no event shall Memorial Sloan-Kettering Cancer Center be
 * liable to any party for direct, indirect, special, incidental or
 * consequential damages, including lost profits, arising out of the use of this
 * software and its documentation, even if Memorial Sloan-Kettering Cancer
 * Center has been advised of the possibility of such damage.
 */

/*
 * This file is part of cBioPortal.
 *
 * cBioPortal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.mskcc.cbio.portal.scripts;

import org.mskcc.cbio.portal.dao.*;
import org.mskcc.cbio.portal.util.*;
import org.mskcc.cbio.portal.model.TypeOfCancer;

import java.io.*;
import java.util.*;

/**
 * Load all the types of cancer and their names from a file.
 *
 * @author Arthur Goldberg goldberg@cbio.mskcc.org
 */
public class ImportTypesOfCancers {
    public static void main(String[] args) throws IOException, DaoException {
        try {
	    	if (args.length < 1) {
	            System.out.println("command line usage: importTypesOfCancer.pl <types_of_cancer.txt> <clobber>");
	            // an extra --noprogress option can be given to avoid the messages regarding memory usage and % complete
	            //use 2 for command line syntax errors:
	            System.exit(2);
	        }
	
	        ProgressMonitor.setConsoleModeAndParseShowProgress(args);
	
	        File file = new File(args[0]);
	        // default to clobber = true (existing behavior)
	        boolean clobber = (args.length > 1 && (args[1].equalsIgnoreCase("f") || args[1].equalsIgnoreCase("false"))) ? false : true;	
	        load(file, clobber);
        }
        catch (Exception e) {
	        ConsoleUtil.showWarnings();
	        //exit with error status:
	        System.err.println ("\nABORTED! Error:  " + e.getMessage());
	        System.exit(1);
        }
    }

    public static void load(File file, boolean clobber) throws IOException, DaoException {
		SpringUtil.initDataSource();
        if (clobber) DaoTypeOfCancer.deleteAllRecords(); //TODO - this option should not exist...in a relational DB it basically means the whole DB is cleaned-up...there should be more efficient ways to do this...and here it is probably an unwanted side effect. REMOVE??
        TypeOfCancer aTypeOfCancer = new TypeOfCancer();
        Scanner scanner = new Scanner(file);

        while(scanner.hasNextLine()) {
            String[] tokens = scanner.nextLine().split("\t", -1);
            if (tokens.length != 5) {
                throw new IOException(
                    "Cancer type file '" + file.getPath() +
                    "' is not a five-column tab-delimited file");
            }

            String typeOfCancerId = tokens[0].trim();
            aTypeOfCancer.setTypeOfCancerId(typeOfCancerId.toLowerCase());
            aTypeOfCancer.setName(tokens[1].trim());
            aTypeOfCancer.setClinicalTrialKeywords(tokens[2].trim().toLowerCase());
            aTypeOfCancer.setDedicatedColor(tokens[3].trim());
            aTypeOfCancer.setShortName(typeOfCancerId);
            aTypeOfCancer.setParentTypeOfCancerId(tokens[4].trim().toLowerCase());
            DaoTypeOfCancer.addTypeOfCancer(aTypeOfCancer);
        }
        ProgressMonitor.setCurrentMessage("Loaded " + DaoTypeOfCancer.getCount() + " TypesOfCancers.");
        ProgressMonitor.setCurrentMessage("Done.");
        ConsoleUtil.showMessages();
    }
    

}

@@ -2365,16 +2365,85 @@
 args);%0A%09
+        ProgressMonitor.setCurrentMessage(%22Loading cancer types...%22);
 %0A%09      
@@ -3735,24 +3735,36 @@
           %7D%0A
+            
 %0A           
@@ -3802,24 +3802,355 @@
 %5B0%5D.trim();%0A
+            //if not clobber, then existing cancer types should be skipped:%0A            if (!clobber && DaoTypeOfCancer.getTypeOfCancerById(typeOfCancerId.toLowerCase()) != null ) %7B%0A            %09ProgressMonitor.logWarning(%22Cancer type with id '%22 + typeOfCancerId + %22' already exists. Skipping.%22);%0A            %7D%0A            else %7B%0A%09
             
@@ -4204,32 +4204,33 @@
 toLowerCase());%0A
+%09
             aTyp
@@ -4258,32 +4258,33 @@
 ens%5B1%5D.trim());%0A
+%09
             aTyp
@@ -4343,32 +4343,33 @@
 toLowerCase());%0A
+%09
             aTyp
@@ -4411,24 +4411,25 @@
 3%5D.trim());%0A
+%09
             
@@ -4472,16 +4472,17 @@
 cerId);%0A
+%09
         
@@ -4556,16 +4556,17 @@
 ase());%0A
+%09
         
@@ -4613,24 +4613,38 @@
 eOfCancer);%0A
+            %7D%0A
         %7D%0A  
@@ -4680,24 +4680,29 @@
 entMessage(%22
+ --%3E 
 Loaded %22 + D
@@ -4731,29 +4731,31 @@
 t() + %22 
-TypesOfCancer
+new cancer type
 s.%22);%0A  

fix for existing cancer types