<commit_before>package org.mskcc.cgds.web_api;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;

import org.mskcc.cgds.dao.DaoCancerStudy;
import org.mskcc.cgds.dao.DaoTypeOfCancer;
import org.mskcc.cgds.dao.DaoException;
import org.mskcc.cgds.model.TypeOfCancer;
import org.mskcc.cgds.model.CancerStudy;

/**
 * Get all the types of cancer in the dbms.
 * Used by the Web API.
 * 
 * @author <NAME> <EMAIL>
 */
public class GetTypesOfCancer {

    public static String getTypesOfCancer() throws DaoException, ProtocolException {

        ArrayList<TypeOfCancer> typeOfCancerList = DaoTypeOfCancer.getAllTypesOfCancer();
        StringBuffer buf = new StringBuffer();
        if (typeOfCancerList.size() > 0) {

            buf.append("type_of_cancer_id\tname\n");
            for (TypeOfCancer typeOfCancer : typeOfCancerList) {
                buf.append(typeOfCancer.getTypeOfCancerId() + "\t");
                buf.append(typeOfCancer.getName() + "\n");
            }
            return buf.toString();
        } else {
            throw new ProtocolException ("No Types of Cancer Available.\n");
        }
    }

    /**
     * Gets all Cancer Studies stored in Remote CGDS Server.
     *
     * @return ArrayList of CancerStudy Objects.
     * @throws DaoException Database Access Exception.
     */
    public static String getCancerStudies() throws DaoException {
        ArrayList<CancerStudy> cancerStudyList = DaoCancerStudy.getAllCancerStudies();

        //  Before returning the list, sort it alphabetically
        Collections.sort(cancerStudyList, new CancerStudiesComparator());

        //  Then, insert "All" Cancer Types at beginning
        ArrayList<CancerStudy> finalCancerStudiesList = new ArrayList<CancerStudy>();
        CancerStudy allCancerStudy = new CancerStudy("All Cancer Types", "All Cancer Types",
                "all", "all", true);
        finalCancerStudiesList.add(allCancerStudy);
        finalCancerStudiesList.addAll(cancerStudyList);

        StringBuffer buf = new StringBuffer();
        buf.append("cancer_study_id\tname\tdescription\n");
        for (CancerStudy cancerStudy : finalCancerStudiesList) {
            // changed to output stable identifier, instead of internal integer identifer.
            buf.append(cancerStudy.getCancerStudyStableId() + "\t");
            buf.append(cancerStudy.getName() + "\t");
            buf.append(cancerStudy.getDescription() + "\n");
        }
        return buf.toString();
    }
}

/**
 * Compares Cancer Studies, so that we can sort them alphabetically.
 */
class CancerStudiesComparator implements Comparator {

    /**
     * Compare two cancer studies.
     * @param o  First Cancer Study.
     * @param o1 Second Cancer Study.
     * @return int indicating name sort order.
     */
    public int compare(Object o, Object o1) {
        CancerStudy study0 = (CancerStudy) o;
        CancerStudy study1 = (CancerStudy) o1;
        return study0.getName().compareTo(study1.getName());
    }
}<commit_msg>Fixes issue 70:  the 'all' cancer types is no longer returned<commit_after>package org.mskcc.cgds.web_api;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;

import org.mskcc.cgds.dao.DaoCancerStudy;
import org.mskcc.cgds.dao.DaoTypeOfCancer;
import org.mskcc.cgds.dao.DaoException;
import org.mskcc.cgds.model.TypeOfCancer;
import org.mskcc.cgds.model.CancerStudy;

/**
 * Get Types of Cancer and Cancer Studies.
 * Used by the Web API.
 * 
 * @author <NAME>, <NAME>.
 */
public class GetTypesOfCancer {

    /**
     * Gets all Types of Cancer.
     *
     * @return Tab-Delimited Text.
     * @throws DaoException Database AccessException.
     * @throws ProtocolException Protocol Error.
     */
    public static String getTypesOfCancer() throws DaoException, ProtocolException {
        ArrayList<TypeOfCancer> typeOfCancerList = DaoTypeOfCancer.getAllTypesOfCancer();
        StringBuffer buf = new StringBuffer();
        if (typeOfCancerList == null || typeOfCancerList.size() == 0) {
            throw new ProtocolException ("No Types of Cancer Available.");
        } else {
            buf.append("type_of_cancer_id\tname\n");
            for (TypeOfCancer typeOfCancer : typeOfCancerList) {
                buf.append(typeOfCancer.getTypeOfCancerId() + WebApiUtil.TAB);
                buf.append(typeOfCancer.getName() +  WebApiUtil.NEW_LINE);
            }
            return buf.toString();
        }
    }

    /**
     * Gets all Cancer Studies.
     *
     * @return Tab-Delimited Text.
     * @throws DaoException Database Access Error.
     * @throws ProtocolException Protocol Error.
     */
    public static String getCancerStudies() throws DaoException, ProtocolException {
        ArrayList<CancerStudy> cancerStudyList = DaoCancerStudy.getAllCancerStudies();

        if (cancerStudyList == null || cancerStudyList.size() ==0) {
            throw new ProtocolException ("No Cancer Studies Available.");
        } else {
            //  Before returning the list, sort it alphabetically
            Collections.sort(cancerStudyList, new CancerStudiesComparator());

            StringBuffer buf = new StringBuffer();
            buf.append("cancer_study_id\tname\tdescription\n");

            //  Iterate through all cancer studies
            for (CancerStudy cancerStudy : cancerStudyList) {
                buf.append(cancerStudy.getCancerStudyStableId() +  WebApiUtil.TAB);
                buf.append(cancerStudy.getName() +  WebApiUtil.TAB);
                buf.append(cancerStudy.getDescription() + WebApiUtil.NEW_LINE);
            }
            return buf.toString();
        }
    }
}

/**
 * Compares Cancer Studies, so that we can sort them alphabetically.
 */
class CancerStudiesComparator implements Comparator {

    /**
     * Compare two cancer studies.
     * @param o  First Cancer Study.
     * @param o1 Second Cancer Study.
     * @return int indicating name sort order.
     */
    public int compare(Object o, Object o1) {
        CancerStudy study0 = (CancerStudy) o;
        CancerStudy study1 = (CancerStudy) o1;
        return study0.getName().compareTo(study1.getName());
    }
}
{'fraction_non_alphanumeric': 0.10078892287876348, 'fraction_numerical': 0.0030590887135726937, 'mean_word_length': 2.724220623501199, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6033431', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1768, 'n_words': 437}