Example usage for java.util TreeMap containsKey

List of usage examples for java.util TreeMap containsKey

Introduction

In this page you can find the example usage for java.util TreeMap containsKey.

Prototype

public boolean containsKey(Object key) 

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:com.cmart.DB.CassandraDBQuery.java

public ArrayList<Item> getCategoryItems(long categoryID, int page, int itemsPP, int sortCol, Boolean sortDec,
        Boolean getImages, int numImages, String[] hasItems, long lastSeenID) throws Exception {
    ArrayList<Item> items = new ArrayList<Item>();

    if (categoryID < 0 || page < 0 || itemsPP <= 0 || sortCol < 0)
        return items;
    if (sortDec == null || getImages == null || hasItems == null)
        return items;

    Connection conn = this.getConnection();
    Date nowdate = new Date(System.currentTimeMillis());

    if (conn != null) {
        try {//from   w  w  w .j a v  a  2 s .com
            PreparedStatement getPIKey = null;
            ResultSet pikeyrs = null;
            PreparedStatement getItemIDs = null;
            ResultSet itemids = null;
            PreparedStatement getItems = null;
            ResultSet itemsrs = null;

            TreeMap<Long, Long> notIn = new TreeMap<Long, Long>();
            for (String not : hasItems) {
                try {
                    Long l = Long.valueOf(not);
                    notIn.put(l, l);
                } catch (Exception e) {
                }
            }

            /*
             * 1. get item ids to get
             * 2. make the IN statement
             * 3. get the items
             * 4. sort to correct order
             */

            int factor = 1;
            int newitemsPP = itemsPP;
            int gotGood = 0;

            while (factor < 5 && gotGood < itemsPP) {
                try {
                    String CQL = "";
                    newitemsPP = newitemsPP * (factor * factor);
                    factor++;

                    switch (sortCol) {
                    case 1: {
                        /*
                         * 1. get the pikey for the last seen item
                         * 2. get the items that are after that pikey
                         */

                        long lastPIKey = 1;
                        if (lastSeenID > 0) {
                            getPIKey = conn.prepareStatement("SELECT pikey FROM items WHERE KEY=" + lastSeenID);
                            pikeyrs = getPIKey.executeQuery();

                            if (pikeyrs.next()) {
                                try {
                                    Long temp = pikeyrs.getLong("pikey");
                                    if (temp != null)
                                        lastPIKey = temp;
                                } catch (Exception e) {
                                }
                            }
                        }

                        long now = System.currentTimeMillis() * shortback;

                        // Get items by bid price
                        if (sortDec) {
                            // If there is no lastPIkey for reverse price, we want to make it max so that we
                            // get the most expensive item (but cassandra can't handle that, so make it 1 less)
                            if (lastPIKey == 1)
                                lastPIKey = (Long.MAX_VALUE - 1);
                            else
                                lastPIKey--;

                            // price bigger first (smallest key in rev keys, smallest key=biggest price)
                            if (categoryID > 0) {
                                CQL = "SELECT itemid FROM revpriceitems WHERE categoryid = '" + categoryID
                                        + "' AND pikey<=" + lastPIKey + " AND itemid>" + now + " LIMIT "
                                        + newitemsPP;
                            } else {
                                CQL = "SELECT itemid FROM revpriceitems WHERE catzero=0 AND pikey<=" + lastPIKey
                                        + " AND itemid>" + now + " LIMIT " + newitemsPP;
                            }
                        } else {
                            // lastPIKey has a min value of Long priceKey = ((long) (price*1000000000000l)) + (itemID % 10000000000l);
                            // for an item
                            if (lastPIKey == 1)
                                lastPIKey = tenzero - 1;
                            else
                                lastPIKey++;

                            // get the lowest priced items first (by current bid, smallest key = smallest price)
                            if (categoryID > 0) {
                                CQL = "SELECT itemid FROM priceitems WHERE categoryid = '" + categoryID
                                        + "' AND pikey>=" + lastPIKey + " AND itemid>=" + now + " LIMIT "
                                        + newitemsPP;
                            } else {
                                CQL = "SELECT itemid FROM priceitems WHERE catzero=0 AND pikey>=" + lastPIKey
                                        + " AND itemid>=" + now + " LIMIT " + newitemsPP;
                            }
                        }

                        break;
                    }
                    default: {
                        if (!sortDec) {
                            // Make only current items be returned
                            long now = System.currentTimeMillis() * shortback;
                            if (lastSeenID < now)
                                lastSeenID = now;
                            else
                                lastSeenID++;

                            // end date getting larger, i.e. item that expires earliest is selected first
                            if (categoryID > 0) {
                                CQL = "SELECT itemid FROM items WHERE categoryid = '" + categoryID
                                        + "' AND itemid>=" + lastSeenID + " LIMIT " + newitemsPP;
                            } else {
                                CQL = "SELECT itemid FROM items WHERE KEY>=" + lastSeenID + " LIMIT "
                                        + newitemsPP;
                            }
                        } else {
                            if (lastSeenID == 0)
                                lastSeenID = (Long.MAX_VALUE - 1);
                            else
                                lastSeenID--;
                            long now = System.currentTimeMillis() * shortback;

                            // end date getting smaller, i.e. item that expires last is first to be selected
                            if (categoryID > 0) {
                                CQL = "SELECT itemid FROM revtimeitems WHERE categoryid = '" + categoryID
                                        + "' AND itemid<" + lastSeenID + " AND itemid>" + now + " LIMIT "
                                        + newitemsPP;
                            } else {
                                CQL = "SELECT itemid FROM revtimeitems WHERE catzero=0 AND KEY>"
                                        + (Long.MAX_VALUE - lastSeenID) + " AND itemid>" + now + " LIMIT "
                                        + newitemsPP;
                            }
                        }

                        break;
                    }
                    }

                    getItemIDs = conn.prepareStatement(CQL);
                    itemids = getItemIDs.executeQuery();

                    StringBuilder ids = new StringBuilder();
                    ids.append("('0'");

                    while (itemids.next()) {
                        Long itemid = null;
                        try {
                            itemid = itemids.getLong("itemid");
                        } catch (Exception e) {
                        }
                        ;

                        if (itemid != null && !notIn.containsKey(itemid)) {
                            ids.append(",'");
                            ids.append(itemid);
                            ids.append("'");
                        }
                    }
                    ids.append(");");

                    getItems = conn.prepareStatement("SELECT * FROM items WHERE KEY IN " + ids.toString());
                    itemsrs = getItems.executeQuery();

                    int imgCount = 0;
                    while (itemsrs.next() && gotGood < newitemsPP) {
                        try {

                            // Cassandra can fail because items don't have all the info required
                            Item currentItem = null;
                            try {
                                ArrayList<Image> images = new ArrayList<Image>();
                                if (getImages && imgCount < numImages) {
                                    images = this.getItemImages(itemsrs.getLong("KEY"));
                                    imgCount++;
                                }

                                currentItem = new Item(itemsrs.getLong("KEY"), itemsrs.getString("name"),
                                        itemsrs.getString("description"), itemsrs.getInt("quantity"),
                                        itemsrs.getDouble("startprice"), itemsrs.getDouble("reserveprice"),
                                        itemsrs.getDouble("buynowprice"), itemsrs.getDouble("curbid"),
                                        itemsrs.getDouble("maxbid"), itemsrs.getInt("noofbids"),
                                        new Date(itemsrs.getLong("startdate")),
                                        new Date(itemsrs.getLong("enddate")), itemsrs.getLong("sellerid"),
                                        itemsrs.getLong("categoryid"), itemsrs.getString("thumbnail"), images);
                            } catch (Exception e) {
                            }

                            if (currentItem != null) {
                                if (!items.contains(currentItem) && currentItem.getEndDate().after(nowdate)) {
                                    items.add(currentItem);
                                    gotGood++;
                                }
                            }
                        } catch (NullPointerException e) {
                        }
                    }

                    // We now need to sort the items
                    switch (sortCol) {
                    case 1: {
                        // lowest price first
                        if (!sortDec) {
                            Collections.sort(items, new Comparator<Item>() {
                                public int compare(Item i1, Item i2) {
                                    return i1.getCurrentBid() < i2.getCurrentBid() ? -1 : 1;
                                }
                            });
                        }
                        // highest price first, we have reversed the comparator operator
                        else {
                            Collections.sort(items, new Comparator<Item>() {
                                public int compare(Item i1, Item i2) {
                                    return i1.getCurrentBid() < i2.getCurrentBid() ? 1 : -1;
                                }
                            });
                        }

                        break;
                    }
                    default: {
                        // Earliest expiration first
                        if (!sortDec) {
                            Collections.sort(items, new Comparator<Item>() {
                                public int compare(Item i1, Item i2) {
                                    return i1.getEndDate().before(i2.getEndDate()) ? -1 : 1;
                                }
                            });
                        }
                        // Latest expiration first
                        else {
                            Collections.sort(items, new Comparator<Item>() {
                                public int compare(Item i1, Item i2) {
                                    return i1.getEndDate().before(i2.getEndDate()) ? 1 : -1;
                                }
                            });
                        }

                        break;
                    }
                    }
                } catch (Exception e) {
                    System.err.println("CassandraQuery (getCategoryItems): Could not get the items");
                    e.printStackTrace();
                    throw e;
                } finally {
                    this.closeSmt(getPIKey);
                    this.close(pikeyrs);
                    this.closeSmt(getItemIDs);
                    this.close(itemids);
                    this.closeSmt(getItems);
                    this.close(itemsrs);
                }
            }
        } catch (Exception e) {
            System.err.println("CassandraQuery (getCategoryItems): Could not get the items");
            e.printStackTrace();
            throw e;
        } finally {

            this.closeConnection(conn);
        }
    }

    return items;

}

From source file:org.sakaiproject.tool.assessment.facade.AssessmentGradingFacadeQueries.java

public List getExportResponsesData(String publishedAssessmentId, boolean anonymous, String audioMessage,
        String fileUploadMessage, String noSubmissionMessage, boolean showPartAndTotalScoreSpreadsheetColumns,
        String poolString, String partString, String questionString, String textString, String rationaleString,
        String itemGradingCommentsString, Map useridMap, String responseCommentString) {
    ArrayList dataList = new ArrayList();
    ArrayList headerList = new ArrayList();
    ArrayList finalList = new ArrayList(2);
    PublishedAssessmentService pubService = new PublishedAssessmentService();

    HashSet publishedAssessmentSections = pubService
            .getSectionSetForAssessment(Long.valueOf(publishedAssessmentId));
    Double zeroDouble = new Double(0.0);
    HashMap publishedAnswerHash = pubService
            .preparePublishedAnswerHash(pubService.getPublishedAssessment(publishedAssessmentId));
    HashMap publishedItemTextHash = pubService
            .preparePublishedItemTextHash(pubService.getPublishedAssessment(publishedAssessmentId));
    HashMap publishedItemHash = pubService
            .preparePublishedItemHash(pubService.getPublishedAssessment(publishedAssessmentId));

    //Get this sorted to add the blank gradings for the questions not answered later.
    Set publishItemSet = new TreeSet(new ItemComparator());
    publishItemSet.addAll(publishedItemHash.values());

    int numSubmission = 1;
    String numSubmissionText = noSubmissionMessage;
    String lastAgentId = "";
    String agentEid = "";
    String firstName = "";
    String lastName = "";
    Set useridSet = new HashSet(useridMap.keySet());
    ArrayList responseList = null;
    boolean canBeExported = false;
    boolean fistItemGradingData = true;
    List list = getAllOrderedSubmissions(publishedAssessmentId);
    Iterator assessmentGradingIter = list.iterator();
    while (assessmentGradingIter.hasNext()) {

        // create new section-item-scores structure for this assessmentGrading
        Iterator sectionsIter = publishedAssessmentSections.iterator();
        HashMap sectionItems = new HashMap();
        TreeMap sectionScores = new TreeMap();
        while (sectionsIter.hasNext()) {
            PublishedSectionData publishedSection = (PublishedSectionData) sectionsIter.next();
            ArrayList itemsArray = publishedSection.getItemArraySortedForGrading();
            Iterator itemsIter = itemsArray.iterator();
            // Iterate through the assessment questions (items)
            HashMap itemsForSection = new HashMap();
            while (itemsIter.hasNext()) {
                ItemDataIfc item = (ItemDataIfc) itemsIter.next();
                itemsForSection.put(item.getItemId(), item.getItemId());
            }/*from  w  w  w . j  ava  2  s .c  o m*/
            sectionItems.put(publishedSection.getSequence(), itemsForSection);
            sectionScores.put(publishedSection.getSequence(), zeroDouble);
        }

        AssessmentGradingData assessmentGradingData = (AssessmentGradingData) assessmentGradingIter.next();
        String agentId = assessmentGradingData.getAgentId();
        responseList = new ArrayList();
        canBeExported = false;
        if (anonymous) {
            canBeExported = true;
            responseList.add(assessmentGradingData.getAssessmentGradingId());
        } else {
            if (useridMap.containsKey(assessmentGradingData.getAgentId())) {
                useridSet.remove(assessmentGradingData.getAgentId());
                canBeExported = true;
                try {
                    agentEid = userDirectoryService.getUser(assessmentGradingData.getAgentId()).getEid();
                    firstName = userDirectoryService.getUser(assessmentGradingData.getAgentId()).getFirstName();
                    lastName = userDirectoryService.getUser(assessmentGradingData.getAgentId()).getLastName();
                } catch (Exception e) {
                    log.error("Cannot get user");
                }
                responseList.add(lastName);
                responseList.add(firstName);
                responseList.add(agentEid);
                if (assessmentGradingData.getForGrade()) {
                    if (lastAgentId.equals(agentId)) {
                        numSubmission++;
                    } else {
                        numSubmission = 1;
                        lastAgentId = agentId;
                    }
                } else {
                    numSubmission = 0;
                    lastAgentId = agentId;
                }
                if (numSubmission == 0) {
                    numSubmissionText = noSubmissionMessage;
                } else {
                    numSubmissionText = String.valueOf(numSubmission);
                }
                responseList.add(numSubmissionText);
            }
        }

        if (canBeExported) {
            int sectionScoreColumnStart = responseList.size();
            if (showPartAndTotalScoreSpreadsheetColumns) {
                Double finalScore = assessmentGradingData.getFinalScore();
                if (finalScore != null) {
                    responseList.add((Double) finalScore.doubleValue()); // gopal - cast for spreadsheet numerics
                } else {
                    log.debug("finalScore is NULL");
                    responseList.add(0d);
                }
            }

            String assessmentGradingComments = "";
            if (assessmentGradingData.getComments() != null) {
                assessmentGradingComments = assessmentGradingData.getComments().replaceAll("<br\\s*/>", "");
            }
            responseList.add(assessmentGradingComments);

            Long assessmentGradingId = assessmentGradingData.getAssessmentGradingId();

            HashMap studentGradingMap = getStudentGradingData(
                    assessmentGradingData.getAssessmentGradingId().toString(), false);
            ArrayList grades = new ArrayList();
            grades.addAll(studentGradingMap.values());

            Collections.sort(grades, new QuestionComparator(publishedItemHash));

            //Add the blank gradings for the questions not answered in random pools.
            if (grades.size() < publishItemSet.size()) {
                int index = -1;
                for (Object pido : publishItemSet) {
                    index++;
                    PublishedItemData pid = (PublishedItemData) pido;
                    if (index == grades.size() || ((ItemGradingData) ((List) grades.get(index)).get(0))
                            .getPublishedItemId().longValue() != pid.getItemId().longValue()) {
                        //have to add the placeholder
                        List newList = new ArrayList();
                        newList.add(new EmptyItemGrading(pid.getSection().getSequence(), pid.getItemId(),
                                pid.getSequence()));
                        grades.add(index, newList);
                    }
                }
            }

            int questionNumber = 0;
            for (Object oo : grades) {
                // There can be more than one answer to a question, e.g. for
                // FIB with more than one blank or matching questions. So sort
                // by sequence number of answer. (don't bother to sort if just 1)

                List l = (List) oo;
                if (l.size() > 1)
                    Collections.sort(l, new AnswerComparator(publishedAnswerHash));

                String maintext = "";
                String rationale = "";
                String responseComment = "";

                boolean addRationale = false;
                boolean addResponseComment = false;

                boolean matrixChoices = false;
                TreeMap responsesMap = new TreeMap();
                // loop over answers per question
                int count = 0;
                ItemGradingData grade = null;
                //boolean isAudioFileUpload = false;
                boolean isFinFib = false;

                double itemScore = 0.0d;

                //Add the missing sequences!
                //To manage emi answers, could help with others too
                Map<Long, String> emiAnswerText = new TreeMap<Long, String>();
                for (Object ooo : l) {
                    grade = (ItemGradingData) ooo;
                    if (grade == null || EmptyItemGrading.class.isInstance(grade)) {
                        continue;
                    }
                    if (grade != null && grade.getAutoScore() != null) {
                        itemScore += grade.getAutoScore().doubleValue();
                    }

                    // now print answer data
                    log.debug("<br> " + grade.getPublishedItemId() + " " + grade.getRationale() + " "
                            + grade.getAnswerText() + " " + grade.getComments() + " " + grade.getReview());
                    Long publishedItemId = grade.getPublishedItemId();
                    ItemDataIfc publishedItemData = (ItemDataIfc) publishedItemHash.get(publishedItemId);
                    Long typeId = publishedItemData.getTypeId();
                    questionNumber = publishedItemData.getSequence();
                    if (typeId.equals(TypeIfc.FILL_IN_BLANK) || typeId.equals(TypeIfc.FILL_IN_NUMERIC)
                            || typeId.equals(TypeIfc.CALCULATED_QUESTION)) {
                        log.debug("FILL_IN_BLANK, FILL_IN_NUMERIC");
                        isFinFib = true;
                        String thistext = "";

                        Long answerid = grade.getPublishedAnswerId();
                        Long sequence = null;
                        if (answerid != null) {
                            AnswerIfc answer = (AnswerIfc) publishedAnswerHash.get(answerid);
                            if (answer != null) {
                                sequence = answer.getSequence();
                            }
                        }

                        String temptext = grade.getAnswerText();
                        if (temptext == null) {
                            temptext = "No Answer";
                        }
                        thistext = sequence + ": " + temptext;

                        if (count == 0)
                            maintext = thistext;
                        else
                            maintext = maintext + "|" + thistext;

                        count++;
                    } else if (typeId.equals(TypeIfc.MATCHING)) {
                        log.debug("MATCHING");
                        String thistext = "";

                        // for some question types we have another text field
                        Long answerid = grade.getPublishedAnswerId();
                        String temptext = "No Answer";
                        Long sequence = null;
                        if (answerid != null) {
                            AnswerIfc answer = (AnswerIfc) publishedAnswerHash.get(answerid);
                            if (answer != null) {
                                temptext = answer.getText();
                                if (temptext == null) {
                                    temptext = "No Answer";
                                }
                                sequence = answer.getItemText().getSequence();
                            } else if (answerid == -1) {
                                temptext = "None of the Above";
                                ItemTextIfc itemTextIfc = (ItemTextIfc) publishedItemTextHash
                                        .get(grade.getPublishedItemTextId());
                                sequence = itemTextIfc.getSequence();
                            }
                        } else {
                            ItemTextIfc itemTextIfc = (ItemTextIfc) publishedItemTextHash
                                    .get(grade.getPublishedItemTextId());
                            sequence = itemTextIfc.getSequence();
                        }
                        thistext = sequence + ": " + temptext;

                        if (count == 0)
                            maintext = thistext;
                        else
                            maintext = maintext + "|" + thistext;

                        count++;
                    } else if (typeId.equals(TypeIfc.IMAGEMAP_QUESTION)) {
                        log.debug("MATCHING");

                        ItemTextIfc itemTextIfc = (ItemTextIfc) publishedItemTextHash
                                .get(grade.getPublishedItemTextId());
                        Long sequence = itemTextIfc.getSequence();
                        String temptext = (grade.getIsCorrect()) ? "OK" : "No OK";

                        String thistext = sequence + ": " + temptext;

                        if (count == 0)
                            maintext = thistext;
                        else
                            maintext = maintext + "|" + thistext;

                        count++;
                    } else if (typeId.equals(TypeIfc.IMAGEMAP_QUESTION)) {
                        log.debug("MATCHING");

                        ItemTextIfc itemTextIfc = (ItemTextIfc) publishedItemTextHash
                                .get(grade.getPublishedItemTextId());
                        Long sequence = itemTextIfc.getSequence();
                        String temptext = (grade.getIsCorrect()) ? "OK" : "No OK";

                        String thistext = sequence + ": " + temptext;

                        if (count == 0)
                            maintext = thistext;
                        else
                            maintext = maintext + "|" + thistext;

                        count++;
                    } else if (typeId.equals(TypeIfc.IMAGEMAP_QUESTION)) {
                        log.debug("MATCHING");

                        ItemTextIfc itemTextIfc = (ItemTextIfc) publishedItemTextHash
                                .get(grade.getPublishedItemTextId());
                        Long sequence = itemTextIfc.getSequence();
                        String temptext = (grade.getIsCorrect()) ? "OK" : "No OK";

                        String thistext = sequence + ": " + temptext;

                        if (count == 0)
                            maintext = thistext;
                        else
                            maintext = maintext + "|" + thistext;

                        count++;
                    } else if (typeId.equals(TypeIfc.EXTENDED_MATCHING_ITEMS)) {
                        log.debug("EXTENDED_MATCHING_ITEMS");
                        String thistext = "";

                        // for some question types we have another text field
                        Long answerid = grade.getPublishedAnswerId();
                        String temptext = "No Answer";
                        Long sequence = null;

                        if (answerid != null) {
                            AnswerIfc answer = (AnswerIfc) publishedAnswerHash.get(answerid);
                            if (answer != null) {
                                temptext = answer.getLabel();
                                if (temptext == null) {
                                    temptext = "No Answer";
                                }
                                sequence = answer.getItemText().getSequence();
                            }
                        }

                        if (sequence == null) {
                            ItemTextIfc itemTextIfc = (ItemTextIfc) publishedItemTextHash
                                    .get(grade.getPublishedItemTextId());
                            if (itemTextIfc != null) {
                                sequence = itemTextIfc.getSequence();
                            }
                        }

                        if (sequence != null) {
                            thistext = emiAnswerText.get(sequence);
                            if (thistext == null) {
                                thistext = temptext;
                            } else {
                                thistext = thistext + temptext;
                            }
                            emiAnswerText.put(sequence, thistext);
                        } else {
                            // Orphaned answer: the answer item to which it refers was removed after the assessment was taken,
                            // as a result of editing the published assessment. This behaviour should be fixed, i.e. it should
                            // not be possible to get orphaned answer item references in the database.
                            sequence = new Long(99);
                            emiAnswerText.put(sequence, "Item Removed");
                        }
                    } else if (typeId.equals(TypeIfc.MATRIX_CHOICES_SURVEY)) {
                        log.debug("MATRIX_CHOICES_SURVEY");
                        // for this kind of question a responsesMap is generated
                        matrixChoices = true;
                        Long answerid = grade.getPublishedAnswerId();
                        String temptext = "No Answer";
                        Long sequence = null;
                        if (answerid != null) {
                            AnswerIfc answer = (AnswerIfc) publishedAnswerHash.get(answerid);
                            temptext = answer.getText();
                            if (temptext == null) {
                                temptext = "No Answer";
                            }
                            sequence = answer.getItemText().getSequence();
                        } else {
                            ItemTextIfc itemTextIfc = (ItemTextIfc) publishedItemTextHash
                                    .get(grade.getPublishedItemTextId());
                            sequence = itemTextIfc.getSequence();
                            log.debug(
                                    "Answerid null for " + grade.getPublishedItemId() + ". Adding " + sequence);
                            temptext = "No Answer";
                        }
                        responsesMap.put(sequence, temptext);
                    } else if (typeId.equals(TypeIfc.AUDIO_RECORDING)) {
                        log.debug("AUDIO_RECORDING");
                        maintext = audioMessage;
                        //isAudioFileUpload = true;               
                    } else if (typeId.equals(TypeIfc.FILE_UPLOAD)) {
                        log.debug("FILE_UPLOAD");
                        maintext = fileUploadMessage;
                        //isAudioFileUpload = true;
                    } else if (typeId.equals(TypeIfc.ESSAY_QUESTION)) {
                        log.debug("ESSAY_QUESTION");
                        if (grade.getAnswerText() != null) {
                            maintext = grade.getAnswerText();
                        }
                    } else {
                        log.debug("other type");
                        String thistext = "";

                        // for some question types we have another text field
                        Long answerid = grade.getPublishedAnswerId();
                        if (answerid != null) {
                            AnswerIfc answer = (AnswerIfc) publishedAnswerHash.get(answerid);
                            if (answer != null) {
                                String temptext = answer.getText();
                                if (temptext != null)
                                    thistext = temptext;
                            } else {
                                log.warn("Published answer for " + answerid + " is null");
                            }
                        }

                        if (count == 0)
                            maintext = thistext;
                        else
                            maintext = maintext + "|" + thistext;

                        count++;
                    }

                    // taking care of rationale
                    if (!addRationale && (typeId.equals(TypeIfc.MULTIPLE_CHOICE)
                            || typeId.equals(TypeIfc.MULTIPLE_CORRECT)
                            || typeId.equals(TypeIfc.MULTIPLE_CORRECT_SINGLE_SELECTION)
                            || typeId.equals(TypeIfc.TRUE_FALSE))) {
                        log.debug(
                                "MULTIPLE_CHOICE or MULTIPLE_CORRECT or MULTIPLE_CORRECT_SINGLE_SELECTION or TRUE_FALSE");
                        if (publishedItemData.getHasRationale() != null
                                && publishedItemData.getHasRationale()) {
                            addRationale = true;
                            rationale = grade.getRationale();
                            if (rationale == null) {
                                rationale = "";
                            }
                        }
                    }

                    //Survey - Matrix of Choices - Add Comment Field
                    if (typeId.equals(TypeIfc.MATRIX_CHOICES_SURVEY)) {
                        PublishedItemData pid = (PublishedItemData) publishedItemData;
                        if (pid.getAddCommentFlag()) {
                            addResponseComment = true;
                            if (responseComment.equals("") && grade.getAnswerText() != null) {
                                responseComment = grade.getAnswerText();
                            }
                        }
                    }
                } // inner for - answers

                if (!emiAnswerText.isEmpty()) {
                    if (maintext == null) {
                        maintext = "";
                    }
                    for (Entry<Long, String> entry : emiAnswerText.entrySet()) {
                        maintext = maintext + "|" + entry.getKey().toString() + ":" + entry.getValue();
                    }
                    if (maintext.startsWith("|")) {
                        maintext = maintext.substring(1);
                    }
                }
                Integer sectionSequenceNumber = null;
                if (grade == null || EmptyItemGrading.class.isInstance(grade)) {
                    sectionSequenceNumber = EmptyItemGrading.class.cast(grade).getSectionSequence();
                    questionNumber = EmptyItemGrading.class.cast(grade).getItemSequence();
                    // indicate that the student was not presented with this question
                    maintext = "-";
                } else {
                    sectionSequenceNumber = updateSectionScore(sectionItems, sectionScores,
                            grade.getPublishedItemId(), itemScore);
                }

                if (isFinFib && maintext.indexOf("No Answer") >= 0 && count == 1) {
                    maintext = "No Answer";
                } else if ("".equals(maintext)) {
                    maintext = "No Answer";
                }
                String itemGradingComments = "";
                // if question type is not matrix choices apply the original code
                if (!matrixChoices) {
                    responseList.add(maintext);
                    if (grade.getComments() != null) {
                        itemGradingComments = grade.getComments().replaceAll("<br\\s*/>", "");
                    }
                    responseList.add(itemGradingComments);
                } else {
                    // if there are questions not answered, a no answer response is added to the map
                    ItemDataIfc correspondingPublishedItemData = (ItemDataIfc) publishedItemHash
                            .get(grade.getPublishedItemId());
                    List correspondingItemTextArray = correspondingPublishedItemData.getItemTextArray();
                    log.debug("publishedItem is " + correspondingPublishedItemData.getText()
                            + " and number of rows " + correspondingItemTextArray.size());
                    if (responsesMap.size() < correspondingItemTextArray.size()) {
                        Iterator itItemTextHash = correspondingItemTextArray.iterator();
                        while (itItemTextHash.hasNext()) {
                            ItemTextIfc itemTextIfc = (ItemTextIfc) itItemTextHash.next();
                            if (!responsesMap.containsKey(itemTextIfc.getSequence())) {
                                log.debug("responsesMap does not contain answer to " + itemTextIfc.getText());
                                responsesMap.put(itemTextIfc.getSequence(), "No Answer");
                            }
                        }
                    }
                    Iterator it = responsesMap.entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry e = (Map.Entry) it.next();
                        log.debug("Adding to response list " + e.getKey() + " and " + e.getValue());
                        responseList.add(e.getValue());
                        if (grade.getComments() != null) {
                            itemGradingComments = grade.getComments().replaceAll("<br\\s*/>", "");
                        }
                        responseList.add(itemGradingComments);
                        itemGradingComments = "";
                    }
                }

                if (addRationale) {
                    responseList.add(rationale);
                }

                if (addResponseComment) {
                    responseList.add(responseComment);
                }

                // Only set header based on the first item grading data
                if (fistItemGradingData) {
                    //get the pool name
                    String poolName = null;
                    for (Iterator i = publishedAssessmentSections.iterator(); i.hasNext();) {
                        PublishedSectionData psd = (PublishedSectionData) i.next();
                        if (psd.getSequence().intValue() == sectionSequenceNumber) {
                            poolName = psd.getSectionMetaDataByLabel(SectionDataIfc.POOLNAME_FOR_RANDOM_DRAW);
                        }
                    }
                    if (!matrixChoices) {
                        headerList.add(makeHeader(partString, sectionSequenceNumber, questionString, textString,
                                questionNumber, poolString, poolName));
                        if (addRationale) {
                            headerList.add(makeHeader(partString, sectionSequenceNumber, questionString,
                                    rationaleString, questionNumber, poolString, poolName));
                        }
                        if (addResponseComment) {
                            headerList.add(makeHeader(partString, sectionSequenceNumber, questionString,
                                    responseCommentString, questionNumber, poolString, poolName));
                        }
                        headerList.add(makeHeader(partString, sectionSequenceNumber, questionString,
                                itemGradingCommentsString, questionNumber, poolString, poolName));
                    } else {
                        int numberRows = responsesMap.size();
                        for (int i = 0; i < numberRows; i = i + 1) {
                            headerList.add(makeHeaderMatrix(partString, sectionSequenceNumber, questionString,
                                    textString, questionNumber, i + 1, poolString, poolName));
                            if (addRationale) {
                                headerList
                                        .add(makeHeaderMatrix(partString, sectionSequenceNumber, questionString,
                                                rationaleString, questionNumber, i + 1, poolString, poolName));
                            }
                            if (addResponseComment) {
                                headerList.add(makeHeaderMatrix(partString, sectionSequenceNumber,
                                        questionString, responseCommentString, questionNumber, i + 1,
                                        poolString, poolName));
                            }
                            headerList.add(makeHeaderMatrix(partString, sectionSequenceNumber, questionString,
                                    itemGradingCommentsString, questionNumber, i + 1, poolString, poolName));
                        }
                    }
                }
            } // outer for - questions

            if (showPartAndTotalScoreSpreadsheetColumns) {
                if (sectionScores.size() > 1) {
                    Iterator keys = sectionScores.keySet().iterator();
                    while (keys.hasNext()) {
                        Double partScore = (Double) ((Double) sectionScores.get(keys.next())).doubleValue();
                        responseList.add(sectionScoreColumnStart++, partScore);
                    }
                }
            }

            dataList.add(responseList);

            if (fistItemGradingData) {
                fistItemGradingData = false;
            }
        }
    } // while

    if (!anonymous && useridSet.size() != 0) {
        Iterator iter = useridSet.iterator();
        while (iter.hasNext()) {
            String id = (String) iter.next();
            try {
                agentEid = userDirectoryService.getUser(id).getEid();
                firstName = userDirectoryService.getUser(id).getFirstName();
                lastName = userDirectoryService.getUser(id).getLastName();
            } catch (Exception e) {
                log.error("Cannot get user");
            }
            responseList = new ArrayList();
            responseList.add(lastName);
            responseList.add(firstName);
            responseList.add(agentEid);
            responseList.add(noSubmissionMessage);
            dataList.add(responseList);
        }
    }
    Collections.sort(dataList, new ResponsesComparator(anonymous));
    finalList.add(dataList);
    finalList.add(headerList);
    return finalList;
}