Example usage for java.util HashMap remove

List of usage examples for java.util HashMap remove

Introduction

In this page you can find the example usage for java.util HashMap remove.

Prototype

public V remove(Object key) 

Source Link

Document

Removes the mapping for the specified key from this map if present.

Usage

From source file:org.akaza.openclinica.control.submit.CreateOneDiscrepancyNoteServlet.java

@Override
protected void processRequest() throws Exception {
    FormProcessor fp = new FormProcessor(request);
    DiscrepancyNoteDAO dndao = new DiscrepancyNoteDAO(sm.getDataSource());

    int eventCRFId = fp.getInt(CreateDiscrepancyNoteServlet.EVENT_CRF_ID);
    request.setAttribute(CreateDiscrepancyNoteServlet.EVENT_CRF_ID, new Integer(eventCRFId));

    int parentId = fp.getInt(PARENT_ID);
    DiscrepancyNoteBean parent = parentId > 0 ? (DiscrepancyNoteBean) dndao.findByPK(parentId)
            : new DiscrepancyNoteBean();
    HashMap<Integer, DiscrepancyNoteBean> boxDNMap = (HashMap<Integer, DiscrepancyNoteBean>) session
            .getAttribute(BOX_DN_MAP);/*from   w  ww .  ja va  2 s  . co m*/
    boxDNMap = boxDNMap == null ? new HashMap<Integer, DiscrepancyNoteBean>() : boxDNMap;
    DiscrepancyNoteBean dn = boxDNMap.size() > 0 && boxDNMap.containsKey(Integer.valueOf(parentId))
            ? boxDNMap.get(Integer.valueOf(parentId))
            : new DiscrepancyNoteBean();
    int entityId = fp.getInt(ENTITY_ID, true);
    entityId = entityId > 0 ? entityId : parent.getEntityId();
    if (entityId == 0) {
        Validator.addError(errors, "newChildAdded" + parentId, respage.getString("note_cannot_be_saved"));
        logger.info("entityId is 0. Note saving can not be started.");
    }
    String entityType = fp.getString(ENTITY_TYPE, true);

    FormDiscrepancyNotes noteTree = (FormDiscrepancyNotes) session
            .getAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME);
    if (noteTree == null) {
        noteTree = new FormDiscrepancyNotes();
    }
    String ypos = fp.getString("ypos" + parentId);
    int refresh = 0;
    String field = fp.getString(ENTITY_FIELD, true);

    //String description = fp.getString("description" + parentId);
    int typeId = fp.getInt("typeId" + parentId);
    String detailedDes = fp.getString("detailedDes" + parentId);
    int resStatusId = fp.getInt(RES_STATUS_ID + parentId);
    int assignedUserAccountId = fp.getInt(SUBMITTED_USER_ACCOUNT_ID + parentId);
    String viewNoteLink = fp.getString("viewDNLink" + parentId);
    viewNoteLink = this.appendPageFileName(viewNoteLink, "fromBox", "1");

    Validator v = new Validator(request);
    v.addValidation("detailedDes" + parentId, Validator.NO_BLANKS);
    // v.addValidation("description" + parentId, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 255);
    v.addValidation("detailedDes" + parentId, Validator.LENGTH_NUMERIC_COMPARISON,
            NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 1000);
    v.addValidation("typeId" + parentId, Validator.NO_BLANKS);
    HashMap errors = v.validate();

    dn.setParentDnId(parentId);
    // dn.setDescription(description);
    dn.setDiscrepancyNoteTypeId(typeId);
    dn.setDetailedNotes(detailedDes);
    dn.setResolutionStatusId(resStatusId);
    if (typeId != DiscrepancyNoteType.ANNOTATION.getId()
            && typeId != DiscrepancyNoteType.REASON_FOR_CHANGE.getId()) {
        dn.setAssignedUserId(assignedUserAccountId);
    }
    if (DiscrepancyNoteType.ANNOTATION.getId() == dn.getDiscrepancyNoteTypeId()) {
        updateStudyEvent(entityType, entityId);
        updateStudySubjectStatus(entityType, entityId);
    }
    if (DiscrepancyNoteType.ANNOTATION.getId() == dn.getDiscrepancyNoteTypeId()
            || DiscrepancyNoteType.REASON_FOR_CHANGE.getId() == dn.getDiscrepancyNoteTypeId()) {
        dn.setResStatus(ResolutionStatus.NOT_APPLICABLE);
        dn.setResolutionStatusId(ResolutionStatus.NOT_APPLICABLE.getId());
    }
    if (DiscrepancyNoteType.FAILEDVAL.getId() == dn.getDiscrepancyNoteTypeId()
            || DiscrepancyNoteType.QUERY.getId() == dn.getDiscrepancyNoteTypeId()) {
        if (ResolutionStatus.NOT_APPLICABLE.getId() == dn.getResolutionStatusId()) {
            Validator.addError(errors, RES_STATUS_ID + parentId, restext.getString("not_valid_res_status"));
        }
    }
    //

    if (errors.isEmpty()) {
        HashMap<String, ArrayList<String>> results = new HashMap<String, ArrayList<String>>();
        ArrayList<String> mess = new ArrayList<String>();

        String column = fp.getString(ENTITY_COLUMN, true);

        dn.setOwner(ub);
        dn.setStudyId(currentStudy.getId());
        dn.setEntityId(entityId);
        dn.setEntityType(entityType);
        dn.setColumn(column);
        dn.setField(field);

        if (parentId > 0) {
            if (dn.getResolutionStatusId() != parent.getResolutionStatusId()) {
                parent.setResolutionStatusId(dn.getResolutionStatusId());
                dndao.update(parent);
                if (!parent.isActive()) {
                    logger.info(
                            "Failed to update resolution status ID for the parent dn ID = " + parentId + ". ");
                }
            }
            if (dn.getAssignedUserId() != parent.getAssignedUserId()) {
                parent.setAssignedUserId(dn.getAssignedUserId());
                if (parent.getAssignedUserId() > 0) {
                    dndao.updateAssignedUser(parent);
                } else {
                    dndao.updateAssignedUserToNull(parent);
                }
                if (!parent.isActive()) {
                    logger.info("Failed to update assigned user ID for the parent dn ID= " + parentId + ". ");
                }
            }
        } else {
            ypos = "0";
        }

        dn = (DiscrepancyNoteBean) dndao.create(dn);
        boolean success = dn.getId() > 0 ? true : false;
        if (success) {
            refresh = 1;
            dndao.createMapping(dn);
            success = dndao.isQuerySuccessful();
            if (success == false) {
                mess.add(restext.getString("failed_create_dn_mapping_for_dnId") + dn.getId() + ". ");
            }

            noteTree.addNote(eventCRFId + "_" + field, dn);
            noteTree.addIdNote(dn.getEntityId(), field);
            session.setAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME, noteTree);
            if (dn.getParentDnId() == 0) {
                // see issue 2659 this is a new thread, we will create
                // two notes in this case,
                // This way one can be the parent that updates as the
                // status changes, but one also stays as New.
                dn.setParentDnId(dn.getId());
                dn = (DiscrepancyNoteBean) dndao.create(dn);
                if (dn.getId() > 0) {
                    dndao.createMapping(dn);
                    if (!dndao.isQuerySuccessful()) {
                        mess.add(restext.getString("failed_create_dn_mapping_for_dnId") + dn.getId() + ". ");
                    }
                    noteTree.addNote(eventCRFId + "_" + field, dn);
                    noteTree.addIdNote(dn.getEntityId(), field);
                    session.setAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME, noteTree);
                } else {
                    mess.add(restext.getString("failed_create_child_dn_for_new_parent_dnId") + dn.getId()
                            + ". ");
                }
            }
        } else {
            mess.add(restext.getString("failed_create_new_dn") + ". ");
        }

        if (success) {
            if (boxDNMap.size() > 0 && boxDNMap.containsKey(parentId)) {
                boxDNMap.remove(parentId);
            }
            session.removeAttribute(BOX_TO_SHOW);
            /*
             * Copied from CreateDiscrepancyNoteServlet
             * Setting a marker to check
             * later while saving administrative edited data. This is needed to
             * make sure the system flags error while changing data for items
             * which already has a DiscrepanyNote
             */
            manageReasonForChangeState(session, eventCRFId + "_" + field);
            String email = fp.getString(EMAIL_USER_ACCOUNT + parentId);
            if (dn.getAssignedUserId() > 0 && "1".equals(email.trim())) {
                logger.info("++++++ found our way here");
                // generate email for user here
                StringBuffer message = new StringBuffer();

                dn = getNoteInfo(dn);

                // generate message here
                EmailEngine em = new EmailEngine(EmailEngine.getSMTPHost());
                UserAccountDAO userAccountDAO = new UserAccountDAO(sm.getDataSource());
                ItemDAO itemDAO = new ItemDAO(sm.getDataSource());
                ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
                ItemBean item = new ItemBean();
                ItemDataBean itemData = new ItemDataBean();

                StudyDAO studyDAO = new StudyDAO(sm.getDataSource());
                UserAccountBean assignedUser = (UserAccountBean) userAccountDAO
                        .findByPK(dn.getAssignedUserId());
                String alertEmail = assignedUser.getEmail();
                message.append(MessageFormat.format(respage.getString("mailDNHeader"),
                        assignedUser.getFirstName(), assignedUser.getLastName()));
                message.append("<A HREF='" + SQLInitServlet.getField("sysURL.base")
                        + "ViewNotes?module=submit&listNotes_f_discrepancyNoteBean.user="
                        + assignedUser.getName() + "&listNotes_f_entityName=" + dn.getEntityName() + "'>"
                        + SQLInitServlet.getField("sysURL.base") + "</A><BR/>");
                message.append(respage.getString("you_received_this_from"));
                StudyBean study = (StudyBean) studyDAO.findByPK(dn.getStudyId());

                if ("itemData".equalsIgnoreCase(entityType)) {
                    itemData = (ItemDataBean) iddao.findByPK(dn.getEntityId());
                    item = (ItemBean) itemDAO.findByPK(itemData.getItemId());
                }

                message.append(respage.getString("email_body_separator"));
                message.append(respage.getString("disc_note_info"));
                message.append(respage.getString("email_body_separator"));
                message.append(MessageFormat.format(respage.getString("mailDNParameters1"),
                        dn.getDetailedNotes(), ub.getName()));
                message.append(respage.getString("email_body_separator"));
                message.append(respage.getString("entity_information"));
                message.append(respage.getString("email_body_separator"));
                message.append(MessageFormat.format(respage.getString("mailDNParameters2"), study.getName(),
                        dn.getSubjectName()));

                if (!("studySub".equalsIgnoreCase(entityType) || "subject".equalsIgnoreCase(entityType))) {
                    message.append(
                            MessageFormat.format(respage.getString("mailDNParameters3"), dn.getEventName()));
                    if (!"studyEvent".equalsIgnoreCase(dn.getEntityType())) {
                        message.append(
                                MessageFormat.format(respage.getString("mailDNParameters4"), dn.getCrfName()));
                        if (!"eventCrf".equalsIgnoreCase(dn.getEntityType())) {
                            message.append(MessageFormat.format(respage.getString("mailDNParameters6"),
                                    item.getName()));
                        }
                    }
                }

                message.append(respage.getString("email_body_separator"));
                message.append(MessageFormat.format(respage.getString("mailDNThanks"), study.getName()));
                message.append(respage.getString("email_body_separator"));
                message.append(respage.getString("disclaimer"));
                message.append(respage.getString("email_body_separator"));
                message.append(respage.getString("email_footer"));

                /*
                 *
                 *
                 *
                 * Please select the link below to view the information
                 * provided. You may need to login to
                 * OpenClinica_testbed with your user name and password
                 * after selecting the link. If you receive a page
                 * cannot be displayed message, please make sure to
                 * select the Change Study/Site link in the upper right
                 * table of the page, select the study referenced above,
                 * and select the link again.
                 *
                 * https://openclinica.s-3.com/OpenClinica_testbed/
                 * ViewSectionDataEntry ?ecId=117&sectionId=142&tabId=2
                 */

                String emailBodyString = message.toString();
                String entityName = getEntityName(dn);
                sendEmail(alertEmail.trim(), EmailEngine.getAdminEmail(),
                        MessageFormat.format(respage.getString("mailDNSubject"), study.getName(), entityName),
                        emailBodyString, true, null, null, true);
            }

            String close = fp.getString("close" + parentId);
            //session.setAttribute(CLOSE_WINDOW, "true".equals(close)?"true":"");
            if ("true".equals(close)) {
                addPageMessage(respage.getString("note_saved_into_db"));
                addPageMessage(respage.getString("page_close_automatically"));
                forwardPage(Page.ADD_DISCREPANCY_NOTE_SAVE_DONE);
                logger.info("Should forwardPage to ADD_DISCREPANCY_NOTE_SAVE_DONE.");
            } else {
                if (parentId == dn.getParentDnId()) {
                    mess.add(restext.getString("a_new_child_dn_added"));
                    results.put("newChildAdded" + parentId, mess);
                    setInputMessages(results);
                } else {
                    addPageMessage(restext.getString("a_new_dn_thread_added"));
                }
            }

        } else {
            session.setAttribute(BOX_TO_SHOW, parentId + "");
        }

    } else {
        setInputMessages(errors);
        boxDNMap.put(Integer.valueOf(parentId), dn);
        session.setAttribute(BOX_TO_SHOW, parentId + "");
    }
    session.setAttribute(BOX_DN_MAP, boxDNMap);
    viewNoteLink = this.appendPageFileName(viewNoteLink, "refresh", refresh + "");
    viewNoteLink = this.appendPageFileName(viewNoteLink, "y", ypos != null && ypos.length() > 0 ? ypos : "0");

    getServletContext().getRequestDispatcher(viewNoteLink).forward(request, response);
    // forwardPage(Page.setNewPage(viewNoteLink, Page.VIEW_DISCREPANCY_NOTE.getTitle()));

}

From source file:org.apache.hadoop.hdfs.server.namenode.FSNamesystem.java

/**
 * We want "replication" replicates for the block, but we now have too many.  
 * In this method, copy enough nodes from 'srcNodes' into 'dstNodes' such that:
 *
 * srcNodes.size() - dstNodes.size() == replication
 *
 * We pick node that make sure that replicas are spread across racks and
 * also try hard to pick one with least free space.
 * The algorithm is first to pick a node with least free space from nodes
 * that are on a rack holding more than one replicas of the block.
 * So removing such a replica won't remove a rack. 
 * If no such a node is available,/*www.j  av a 2s.  c o  m*/
 * then pick a node with least free space
 */
void chooseExcessReplicates(Collection<DatanodeDescriptor> nonExcess, Block b, short replication,
        DatanodeDescriptor addedNode, DatanodeDescriptor delNodeHint) {
    // first form a rack to datanodes map and
    HashMap<String, ArrayList<DatanodeDescriptor>> rackMap = new HashMap<String, ArrayList<DatanodeDescriptor>>();
    for (Iterator<DatanodeDescriptor> iter = nonExcess.iterator(); iter.hasNext();) {
        DatanodeDescriptor node = iter.next();
        String rackName = node.getNetworkLocation();
        ArrayList<DatanodeDescriptor> datanodeList = rackMap.get(rackName);
        if (datanodeList == null) {
            datanodeList = new ArrayList<DatanodeDescriptor>();
        }
        datanodeList.add(node);
        rackMap.put(rackName, datanodeList);
    }

    // split nodes into two sets
    // priSet contains nodes on rack with more than one replica
    // remains contains the remaining nodes
    ArrayList<DatanodeDescriptor> priSet = new ArrayList<DatanodeDescriptor>();
    ArrayList<DatanodeDescriptor> remains = new ArrayList<DatanodeDescriptor>();
    for (Iterator<Entry<String, ArrayList<DatanodeDescriptor>>> iter = rackMap.entrySet().iterator(); iter
            .hasNext();) {
        Entry<String, ArrayList<DatanodeDescriptor>> rackEntry = iter.next();
        ArrayList<DatanodeDescriptor> datanodeList = rackEntry.getValue();
        if (datanodeList.size() == 1) {
            remains.add(datanodeList.get(0));
        } else {
            priSet.addAll(datanodeList);
        }
    }

    // pick one node to delete that favors the delete hint
    // otherwise pick one with least space from priSet if it is not empty
    // otherwise one node with least space from remains
    boolean firstOne = true;
    while (nonExcess.size() - replication > 0) {
        DatanodeInfo cur = null;
        long minSpace = Long.MAX_VALUE;

        // check if we can del delNodeHint
        if (firstOne && delNodeHint != null && nonExcess.contains(delNodeHint)
                && (priSet.contains(delNodeHint) || (addedNode != null && !priSet.contains(addedNode)))) {
            cur = delNodeHint;
        } else { // regular excessive replica removal
            Iterator<DatanodeDescriptor> iter = priSet.isEmpty() ? remains.iterator() : priSet.iterator();
            while (iter.hasNext()) {
                DatanodeDescriptor node = iter.next();
                long free = node.getRemaining();

                if (minSpace > free) {
                    minSpace = free;
                    cur = node;
                }
            }
        }

        firstOne = false;
        // adjust rackmap, priSet, and remains
        String rack = cur.getNetworkLocation();
        ArrayList<DatanodeDescriptor> datanodes = rackMap.get(rack);
        datanodes.remove(cur);
        if (datanodes.isEmpty()) {
            rackMap.remove(rack);
        }
        if (priSet.remove(cur)) {
            if (datanodes.size() == 1) {
                priSet.remove(datanodes.get(0));
                remains.add(datanodes.get(0));
            }
        } else {
            remains.remove(cur);
        }

        nonExcess.remove(cur);

        Collection<Block> excessBlocks = excessReplicateMap.get(cur.getStorageID());
        if (excessBlocks == null) {
            excessBlocks = new TreeSet<Block>();
            excessReplicateMap.put(cur.getStorageID(), excessBlocks);
        }
        if (excessBlocks.add(b)) {
            excessBlocksCount++;
            NameNode.stateChangeLog.debug("BLOCK* NameSystem.chooseExcessReplicates: " + "(" + cur.getName()
                    + ", " + b + ") is added to excessReplicateMap");
        }

        //
        // The 'excessblocks' tracks blocks until we get confirmation
        // that the datanode has deleted them; the only way we remove them
        // is when we get a "removeBlock" message.  
        //
        // The 'invalidate' list is used to inform the datanode the block 
        // should be deleted.  Items are removed from the invalidate list
        // upon giving instructions to the namenode.
        //
        addToInvalidatesNoLog(b, cur);
        NameNode.stateChangeLog.info("BLOCK* NameSystem.chooseExcessReplicates: " + "(" + cur.getName() + ", "
                + b + ") is added to recentInvalidateSets");
    }
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.wbuploader.UploadTable.java

/**
 * @param row//  w  w w.ja  va2s . c  o  m
 * @param recNum
 * @return
 * @throws UploaderException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws ParseException
 * @throws NoSuchMethodException
 */
protected List<ParentMatchInfo> getMatchInfoInternal(int row, int recNum, Set<Integer> invalidColNums,
        List<Pair<UploadTable, List<DataModelObjBase>>> matchChildrenParents)
        throws UploaderException, InvocationTargetException, IllegalAccessException, ParseException,
        NoSuchMethodException, InstantiationException, SQLException {
    //XXX still need to follow matchChildren links
    //XXX when doing children need to do all recnums - collector1 2 3 ...
    //XXX need to skip tables with invalid columns
    //XXX what about ...Attribute tables? Do they need special treatment?
    //XXX if CE is embedded? 

    int adjustedRecNum = uploadFields.size() == 1 ? 0 : recNum;
    //XXX assuming that an upload is NOT in progress!!
    wbCurrentRow = row;

    List<ParentMatchInfo> result = new Vector<ParentMatchInfo>();
    Vector<List<ParentMatchInfo>> parentMatches = new Vector<List<ParentMatchInfo>>();
    Vector<List<ParentMatchInfo>> childMatches = new Vector<List<ParentMatchInfo>>();
    List<UploadTable> childTables = new ArrayList<UploadTable>(specialChildren);

    if (this instanceof UploadTableTree && parentTables.size() == 0) {
        DataModelObjBase p = getParentRecord(recNum, this /*only the type of the 2nd param is relevant*/);
        List<DataModelObjBase> m = new ArrayList<DataModelObjBase>();
        m.add(p);
        List<ParentMatchInfo> pm = new ArrayList<ParentMatchInfo>();
        pm.add(new ParentMatchInfo(m, this, isBlankRow(row, uploader.getUploadData(), adjustedRecNum), false,
                recNum, false));
        parentMatches.add(pm);
    } else {
        for (Vector<ParentTableEntry> ptes : parentTables) {
            for (ParentTableEntry pte : ptes) {
                if (!pte.getImportTable().specialChildren.contains(this)
                        || !pte.getImportTable().needToMatchChild(tblClass)) {
                    if (pte.getImportTable().isOneToOneChild()) {
                        if (childTables.indexOf(pte.getImportTable()) == -1) {
                            childTables.add(pte.getImportTable());
                        }
                    } else {
                        parentMatches.add(pte.getImportTable().getMatchInfoInternal(row, adjustedRecNum,
                                invalidColNums, null));
                    }
                }
            }
        }
    }
    HashMap<UploadTable, DataModelObjBase> parentParams = new HashMap<UploadTable, DataModelObjBase>();
    boolean doMatch = true;
    boolean matched = false;
    boolean blankParentage = true;
    boolean blank = isBlankRow(row, uploader.getUploadData(), adjustedRecNum);
    boolean stopAlready = false;
    ArrayList<DataModelObjBase> matches = new ArrayList<DataModelObjBase>();
    ArrayList<DataModelObjBase> myMatches = new ArrayList<DataModelObjBase>();
    //XXX need to include matchChildrenParents in parentParams 
    ParentMatchInfo nearest = null;
    for (List<ParentMatchInfo> pm : parentMatches) {
        if (doMatch && pm.size() > 0) {
            nearest = pm.get(pm.size() - 1);
            int b = 2;
            while (pm.size() - b >= 0 && nearest.isSkipped() && !nearest.getStoppedAlready()) {
                nearest = pm.get(pm.size() - b++);
            }
            blankParentage &= nearest != null & nearest.isBlank();
            if (nearest.getMatches().size() == 1 || (!nearest.getStoppedAlready()
                    && nearest.getMatches().size() == 0 && (nearest.isBlank() || nearest.isSkipped()))) {
                DataModelObjBase match = nearest.getMatches().size() == 1 ? nearest.getMatches().get(0) : null;
                stopAlready = this instanceof UploadTableTree && nearest.getMatches().size() == 0
                        && (nearest.isBlank() || nearest.isSkipped());
                parentParams.put(nearest.getTable(), match);
            } else {
                doMatch = false;
                stopAlready = nearest.getStoppedAlready();
            }
        }
        result.addAll(pm);
    }
    if (doMatch && (!blank || !blankParentage)) {
        for (Vector<UploadField> ufs : uploadFields) {
            for (UploadField uf : ufs) {
                if (uf.getIndex() != -1) {
                    uf.setValue(uploader.getUploadData().get(row, uf.getIndex()));
                }
            }
        }
        //if (!this.getTblClass().equals(CollectingEvent.class)) {
        skipChildrenMatching.set(true);
        //}
        try {
            if (matchChildrenParents == null || matchChildrenParents.size() == 0) {
                if (!blank) {
                    findMatch(adjustedRecNum, false, myMatches, parentParams);
                } else if (nearest != null) {
                    myMatches.add(parentParams.get(nearest.getTable()));
                }
            } else {
                //XXX assuming matchChildrenParents is only being used for ...Attribute tables and will only have one item
                Pair<UploadTable, List<DataModelObjBase>> mp = matchChildrenParents.get(0);
                for (DataModelObjBase p : mp.getSecond()) {
                    parentParams.put(mp.getFirst(), p);
                    findMatch(adjustedRecNum, false, myMatches, parentParams);
                    parentParams.remove(mp.getFirst());
                }
            }
            matched = true;
        } finally {
            //if (!this.getTblClass().equals(CollectingEvent.class)) {
            skipChildrenMatching.set(false);
            //}
        }

    }

    //XXX add this table to matchChildrenParents
    //taking advantage of this mysterious structure to deal with ...Attribute tbls.
    if (matchChildrenParents == null) {
        matchChildrenParents = new ArrayList<Pair<UploadTable, List<DataModelObjBase>>>();
    }
    for (UploadTable ut : childTables) {
        for (int rc = 0; rc < ut.getUploadFields().size(); rc++) {
            if (!this.tblClass.equals(CollectionObject.class)) {
                matchChildrenParents.add(new Pair<UploadTable, List<DataModelObjBase>>(this, myMatches));
            }
            childMatches.add(ut.getMatchInfoInternal(row, rc, invalidColNums, matchChildrenParents));
            if (!this.tblClass.equals(CollectionObject.class)) {
                matchChildrenParents.remove(matchChildrenParents.size() - 1);
            }
        }
    }
    //XXX what the hell to do with childMatches??? Need to add them to result to get Agent, taxon matches, but how to 
    //Use them in findMatch?? Does findMatch need to be done first?? WTF?
    // XXX what the hell happens for matchchildren in findMatch??
    //OK. Current plan is to match children in a way similar to the above - create a parentParam for this object and pass
    //it to findMatch for the children...
    // XXX this is not the final word on matchchildren matches

    //XXX the new way of handling child matches...
    if (!this.tblClass.equals(CollectionObject.class) && childMatches.size() > 0) {
        List<DataModelObjBase> keeperMatches = new ArrayList<DataModelObjBase>(myMatches);
        for (DataModelObjBase mine : myMatches) {
            boolean go = true;
            for (List<ParentMatchInfo> cm : childMatches) {
                for (ParentMatchInfo pmi : cm) {
                    try {
                        ParentTableEntry pte = findParentTableEntry(pmi.getTable());
                        if (pte != null) {
                            //System.out.println(pte.getImportTable());
                            Method getter = pte.getGetter();
                            boolean invokeMe = getter.getDeclaringClass().equals(this.getTblClass());
                            if (pmi.getMatches().size() == 0) {
                                if (invokeMe) {
                                    go = pmi.isBlank() ? getter.invoke(mine) == null : false;
                                } else {
                                    //System.out.println("one-to-many count check");
                                    String sql = "select count(*) from "
                                            + pmi.getTable().getTable().getTableInfo().getName() + " where "
                                            + this.getTable().getTableInfo().getPrimaryKeyName() + " = "
                                            + mine.getId();
                                    go = pmi.isBlank() ? BasicSQLUtils.getCount(sql) == 0 : false;
                                }
                            } else {
                                for (DataModelObjBase theirs : pmi.getMatches()) {
                                    DataModelObjBase objA = (DataModelObjBase) (invokeMe ? getter.invoke(mine)
                                            : getter.invoke(theirs));
                                    DataModelObjBase objB = invokeMe ? theirs : mine;
                                    if (objA != null && objB != null && objA.getId().equals(objB.getId())) {
                                        //System.out.println("  match (" + invokeMe +")");
                                        //all is well
                                    } else {
                                        //System.out.println("  no match (" + invokeMe + ")");
                                        go = false;
                                        break;
                                    }
                                }
                            }
                        } else {
                            //System.out.println("no pte; wtf");
                            //whatever
                        }
                        if (!go) {
                            //childMatchesForLosers.add(cm);
                            break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (!go) {
                    keeperMatches.remove(mine);
                    break;
                }
            }
        }

        //remove incomplete matches
        for (int m = myMatches.size() - 1; m >= 0; m--) {
            if (keeperMatches.indexOf(myMatches.get(m)) == -1) {
                myMatches.remove(m);
            }
        }

        //if no matches, we know all children are also unmatched
        //technically should only clear/remove matches stemming from matches removed above, but... too much work for too little benefit
        if (myMatches.size() == 0) {
            for (List<ParentMatchInfo> cmis : childMatches) {
                for (ParentMatchInfo cmi : cmis) {
                    UploadTable cmit = cmi.getTable();
                    if (cmit.isMatchChild() || cmit.isOneToOneChild() || cmit.isZeroToOneMany()
                            || specialChildren.indexOf(cmit) >= 0) {
                        cmi.getMatches().clear();
                    }
                }
            }
        }

    }

    matches.addAll(myMatches);

    //I think this loop eliminates chains of no-match indications??
    boolean invalid = containsInvalidCol(adjustedRecNum, invalidColNums);
    for (List<ParentMatchInfo> cm : childMatches) {
        if (!invalid && matched) {
            result.addAll(cm);
        } else {
            for (int i = cm.size() - 1; i > -1; i--) {
                ParentMatchInfo mi = cm.get(i);
                if (mi.getTable().isOneToOneChild()) {
                    if (invalid) {
                        cm.remove(i);
                    } else if (!matched) {
                        mi.setIsSkipped(true);
                    }
                }
            }
            result.addAll(cm);
        }
    }

    if (!containsInvalidCol(adjustedRecNum, invalidColNums)) {
        //          if (!matched)
        //          {
        //             for (List<ParentMatchInfo> cm : childMatches)
        //             {
        //                for (ParentMatchInfo mi : cm)
        //                {
        //                   if (mi.getTable().isOneToOneChild())
        //                   {
        //                      mi.setIsSkipped(true);
        //                   }
        //                }
        //             }
        //          }
        result.add(new ParentMatchInfo(matches, this, blank && blankParentage, !matched, recNum, stopAlready));
    }

    return result;
}

From source file:com.cloud.hypervisor.xen.resource.CitrixResourceBase.java

protected HashMap<String, Pair<String, State>> deltaClusterSync(Connection conn) {
    final HashMap<String, Pair<String, State>> changes = new HashMap<String, Pair<String, State>>();

    synchronized (_cluster.intern()) {
        HashMap<String, Pair<String, State>> newStates = getAllVms(conn);
        if (newStates == null) {
            s_logger.warn("Unable to get the vm states so no state sync at this point.");
            return null;
        }/*from w  w w . j av  a2s . c o m*/
        HashMap<String, Pair<String, State>> oldStates = new HashMap<String, Pair<String, State>>(
                s_vms.size(_cluster));
        oldStates.putAll(s_vms.getClusterVmState(_cluster));

        for (final Map.Entry<String, Pair<String, State>> entry : newStates.entrySet()) {
            final String vm = entry.getKey();

            State newState = entry.getValue().second();
            String host_uuid = entry.getValue().first();
            final Pair<String, State> oldState = oldStates.remove(vm);

            //check if host is changed
            if (host_uuid != null && oldState != null) {
                if (!host_uuid.equals(oldState.first()) && newState != State.Stopped
                        && newState != State.Stopping) {
                    s_logger.warn("Detecting a change in host for " + vm);
                    changes.put(vm, new Pair<String, State>(host_uuid, newState));

                    s_logger.debug("11. The VM " + vm + " is in " + newState + " state");
                    s_vms.put(_cluster, host_uuid, vm, newState);
                    continue;
                }
            }

            if (newState == State.Stopped && oldState != null && oldState.second() != State.Stopping
                    && oldState.second() != State.Stopped) {
                newState = getRealPowerState(conn, vm);
            }

            if (s_logger.isTraceEnabled()) {
                s_logger.trace("VM " + vm + ": xen has state " + newState + " and we have state "
                        + (oldState != null ? oldState.toString() : "null"));
            }

            if (vm.startsWith("migrating")) {
                s_logger.warn("Migrating from xen detected.  Skipping");
                continue;
            }
            if (oldState == null) {
                s_vms.put(_cluster, host_uuid, vm, newState);
                s_logger.warn(
                        "Detecting a new state but couldn't find a old state so adding it to the changes: "
                                + vm);
                changes.put(vm, new Pair<String, State>(host_uuid, newState));
            } else if (oldState.second() == State.Starting) {
                if (newState == State.Running) {
                    s_logger.debug("12. The VM " + vm + " is in " + State.Running + " state");
                    s_vms.put(_cluster, host_uuid, vm, newState);
                } else if (newState == State.Stopped) {
                    s_logger.warn("Ignoring vm " + vm + " because of a lag in starting the vm.");
                }
            } else if (oldState.second() == State.Migrating) {
                if (newState == State.Running) {
                    s_logger.debug("Detected that an migrating VM is now running: " + vm);
                    s_vms.put(_cluster, host_uuid, vm, newState);
                }
            } else if (oldState.second() == State.Stopping) {
                if (newState == State.Stopped) {
                    s_logger.debug("13. The VM " + vm + " is in " + State.Stopped + " state");
                    s_vms.put(_cluster, host_uuid, vm, newState);
                } else if (newState == State.Running) {
                    s_logger.warn("Ignoring vm " + vm + " because of a lag in stopping the vm. ");
                }
            } else if (oldState.second() != newState) {
                s_logger.debug("14. The VM " + vm + " is in " + newState + " state was " + oldState.second());
                s_vms.put(_cluster, host_uuid, vm, newState);
                if (newState == State.Stopped) {
                    /*
                     * if (s_vmsKilled.remove(vm)) { s_logger.debug("VM " + vm + " has been killed for storage. ");
                     * newState = State.Error; }
                     */
                }
                changes.put(vm, new Pair<String, State>(host_uuid, newState));
            }
        }

        for (final Map.Entry<String, Pair<String, State>> entry : oldStates.entrySet()) {
            final String vm = entry.getKey();
            final State oldState = entry.getValue().second();
            String host_uuid = entry.getValue().first();

            if (s_logger.isTraceEnabled()) {
                s_logger.trace("VM " + vm + " is now missing from xen so reporting stopped");
            }

            if (oldState == State.Stopping) {
                s_logger.warn("Ignoring VM " + vm + " in transition state stopping.");
                s_vms.remove(_cluster, host_uuid, vm);
            } else if (oldState == State.Starting) {
                s_logger.warn("Ignoring VM " + vm + " in transition state starting.");
            } else if (oldState == State.Stopped) {
                s_logger.debug("VM missing " + vm + " old state stopped so removing.");
                s_vms.remove(_cluster, host_uuid, vm);
            } else if (oldState == State.Migrating) {
                s_logger.warn("Ignoring VM " + vm + " in migrating state.");
            } else {
                State newState = State.Stopped;
                s_logger.warn("The VM is now missing marking it as Stopped " + vm);
                changes.put(vm, new Pair<String, State>(host_uuid, newState));
            }
        }
    }
    return changes;
}

From source file:revaligner.service.FileAligner.java

public void fixTxlfTrgTags(org.dom4j.Document doc) throws Exception {
    org.dom4j.Element root = doc.getRootElement();
    List<org.dom4j.Element> list_transunit = root.selectNodes("//*[name() = 'trans-unit']");
    for (org.dom4j.Element e : list_transunit) {
        org.dom4j.Element source = e.element("source");
        org.dom4j.Element target = e.element("target");
        if ((target != null) && (!target.getText().equals(""))) {
            HashMap<String, List<Object>> src_map = new HashMap();
            boolean tagnodeappear = false;
            boolean textnodeappear = false;
            boolean sourcetagsurroundtext = true;
            int maxid = 1;
            for (int i = 0; i < source.content().size(); i++) {
                org.dom4j.Node node = (org.dom4j.Node) source.content().get(i);
                if (node.getNodeType() == 1) {
                    org.dom4j.Element tag = (org.dom4j.Element) node;
                    maxid = Math.max(maxid, Integer.parseInt(tag.attributeValue("id")));
                }/*from   w ww  . j a  v  a  2s.  c o m*/
            }
            maxid++;
            for (int i = 0; i < source.content().size(); i++) {
                org.dom4j.Node node = (org.dom4j.Node) source.content().get(i);
                if (node.getNodeType() == 1) {
                    if (textnodeappear) {
                        tagnodeappear = true;
                    }
                    org.dom4j.Element tag = (org.dom4j.Element) node;
                    String key = "";
                    if ((tag.getName().equals("ex")) || (tag.getName().equals("ept"))) {
                        key = tag.getName() + "#" + tag.attributeValue("rid");
                    } else if (tag.getName().equals("x")) {
                        key = tag.getName() + "#" + tag.attributeValue("ctype");
                    } else {
                        key = tag.getName();
                    }
                    if (src_map.containsKey(key)) {
                        ((List) src_map.get(key)).add(node.clone());
                    } else {
                        List<Object> list = new ArrayList();
                        list.add(node.clone());
                        src_map.put(key, list);
                    }
                } else {
                    if (tagnodeappear) {
                        sourcetagsurroundtext = false;
                    }
                    textnodeappear = true;
                }
            }
            HashMap<String, String> rid_map = new HashMap();
            List trg_contents = new ArrayList();
            boolean targetnotag = true;
            for (int i = 0; i < target.content().size(); i++) {
                org.dom4j.Node node = (org.dom4j.Node) target.content().get(i);
                if (node.getNodeType() == 1) {
                    org.dom4j.Element tag = (org.dom4j.Element) node;
                    String rid = tag.attributeValue("rid");
                    String key = "";
                    if ((tag.getName().equals("ph")) || (tag.getName().equals("bpt"))
                            || (tag.getName().equals("bx"))) {
                        key = tag.getName();
                        if (src_map.containsKey(key)) {
                            List list = (List) src_map.get(key);
                            trg_contents.add(list.get(0));
                            targetnotag = false;
                            rid_map.put(rid, ((org.dom4j.Element) list.get(0)).attributeValue("rid"));
                            list.remove(0);
                            if (list.size() == 0) {
                                src_map.remove(key);
                            }
                        }
                    } else if (tag.getName().equals("x")) {
                        key = tag.getName() + "#" + tag.attributeValue("ctype");
                        if (src_map.containsKey(key)) {
                            List list = (List) src_map.get(key);
                            trg_contents.add(list.get(0));
                            targetnotag = false;
                            list.remove(0);
                            if (list.size() == 0) {
                                src_map.remove(key);
                            }
                        } else if ((tag.getName().equals("x"))
                                && (tag.attributeValue("ctype").equals("x-tab"))) {
                            org.dom4j.Element tab = DocumentHelper.createElement("x");
                            tab.addAttribute("ctype", "x-tab");
                            tab.addAttribute("id", Integer.toString(maxid));
                            maxid++;
                            tab.addAttribute("equiv-text", " ");
                            trg_contents.add(tab);
                        } else if ((tag.getName().equals("x")) && (tag.attributeValue("ctype").equals("lb"))) {
                            org.dom4j.Element lb = DocumentHelper.createElement("x");
                            lb.addAttribute("ctype", "lb");
                            lb.addAttribute("id", Integer.toString(maxid));
                            maxid++;
                            lb.addAttribute("equiv-text", " ");
                            trg_contents.add(lb);
                        }
                    } else if ((tag.getName().equals("ex")) || (tag.getName().equals("ept"))) {
                        String mapped_rid = (String) rid_map.get(tag.attributeValue("rid"));
                        key = tag.getName() + "#" + mapped_rid;
                        if (src_map.containsKey(key)) {
                            List list = (List) src_map.get(key);
                            trg_contents.add(list.get(0));
                            rid_map.put(rid, ((org.dom4j.Element) list.get(0)).attributeValue("rid"));
                            list.remove(0);
                            if (list.size() == 0) {
                                src_map.remove(key);
                            }
                        }
                    }
                } else if (node.getNodeType() == 3) {
                    String text = node.getText();
                    if (text.contains("<br> ")) {
                        String key = "x#lb";
                        String[] ss = text.split("<br> ");
                        for (int s = 0; s < ss.length; s++) {
                            trg_contents.add(DocumentHelper.createText(ss[s]));
                            if (s < ss.length - 1) {
                                if (src_map.containsKey(key)) {
                                    List list = (List) src_map.get(key);
                                    trg_contents.add(list.get(0));
                                    targetnotag = false;
                                    list.remove(0);
                                    if (list.size() == 0) {
                                        src_map.remove(key);
                                    }
                                } else {
                                    org.dom4j.Element lb = DocumentHelper.createElement("x");
                                    lb.addAttribute("ctype", "lb");
                                    lb.addAttribute("id", Integer.toString(maxid));
                                    maxid++;
                                    lb.addAttribute("equiv-text", " ");
                                    trg_contents.add(lb);
                                }
                            }
                        }
                    } else if (text.contains("<br>")) {
                        String key = "x#lb";
                        String[] ss = text.split("<br> ");
                        for (int s = 0; s < ss.length; s++) {
                            trg_contents.add(DocumentHelper.createText(ss[s]));
                            if (s < ss.length - 1) {
                                if (src_map.containsKey(key)) {
                                    List list = (List) src_map.get(key);
                                    trg_contents.add(list.get(0));
                                    targetnotag = false;
                                    list.remove(0);
                                    if (list.size() == 0) {
                                        src_map.remove(key);
                                    }
                                } else {
                                    org.dom4j.Element lb = DocumentHelper.createElement("x");
                                    lb.addAttribute("ctype", "lb");
                                    lb.addAttribute("id", Integer.toString(maxid));
                                    maxid++;
                                    lb.addAttribute("equiv-text", " ");
                                    trg_contents.add(lb);
                                }
                            }
                        }
                    } else {
                        trg_contents.add(node);
                    }
                } else {
                    trg_contents.add(node);
                }
            }
            while (trg_contents.size() > 0) {
                org.dom4j.Node nd = (org.dom4j.Node) trg_contents.get(0);
                if (nd.getNodeType() == 3) {
                    if (nd.getText().trim().equals("")) {
                        trg_contents.remove(0);
                    } else {
                        nd.setText(nd.getText().replaceAll("^(\\s)+", ""));
                        break;
                    }
                } else if ((nd.getNodeType() == 1) && (nd.getName().equals("x"))
                        && (((org.dom4j.Element) nd).attributeValue("ctype").equals("lb"))) {
                    trg_contents.remove(0);
                } else {
                    if ((nd.getNodeType() != 1) || (!nd.getName().equals("x"))
                            || (!((org.dom4j.Element) nd).attributeValue("ctype").equals("x-tab"))) {
                        break;
                    }
                    trg_contents.remove(0);
                }
            }
            while (trg_contents.size() > 0) {
                org.dom4j.Node nd = (org.dom4j.Node) trg_contents.get(trg_contents.size() - 1);
                if (nd.getNodeType() == 3) {
                    if (nd.getText().trim().equals("")) {
                        trg_contents.remove(trg_contents.size() - 1);
                    } else {
                        nd.setText(nd.getText().replaceAll("(\\s)+$", ""));
                        break;
                    }
                } else if ((nd.getNodeType() == 1) && (nd.getName().equals("x"))
                        && (((org.dom4j.Element) nd).attributeValue("ctype").equals("lb"))) {
                    trg_contents.remove(trg_contents.size() - 1);
                } else {
                    if ((nd.getNodeType() != 1) || (!nd.getName().equals("x"))
                            || (!((org.dom4j.Element) nd).attributeValue("ctype").equals("x-tab"))) {
                        break;
                    }
                    trg_contents.remove(trg_contents.size() - 1);
                }
            }
            boolean isnotignoredsegment = true;
            if (trg_contents.size() == 1) {
                org.dom4j.Node node = (org.dom4j.Node) trg_contents.get(0);
                if ((node.getNodeType() == 3) && (node.getText().replaceFirst("", "").equals(""))) {
                    isnotignoredsegment = false;
                }
            }
            if ((targetnotag) && (sourcetagsurroundtext) && (isnotignoredsegment)) {
                boolean isleadingtag = true;
                int index = 0;
                for (int i = 0; i < source.content().size(); i++) {
                    org.dom4j.Node node = (org.dom4j.Node) source.content().get(i);
                    if (node.getNodeType() == 1) {
                        if (isleadingtag) {
                            trg_contents.add(index, node);
                            index++;
                        } else {
                            trg_contents.add(node);
                        }
                    } else {
                        isleadingtag = false;
                        index++;
                    }
                }
            }
            target.setContent(trg_contents);
        }
    }
}

From source file:com.pari.nm.utils.db.InventoryDBHelper.java

public static void updateIpToIdDetails(NetworkNode device) {
    PreparedStatement ps = null;//from  ww  w.  j a v a 2 s.  c  om
    Connection c = null;
    int id = device.getNodeId();
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    ResultSet rs = null;

    try {
        rs = DBHelper.executeQuery("select * from ip_to_id where id=" + id);

        while (rs.next()) {
            map.put(rs.getString("ipaddress"), rs.getInt("id"));
        }
    } catch (Exception ee) {
        logger.warn("Error while updating Ip To Id details for the device= " + id, ee);
    }

    try {
        rs.close();
    } catch (Exception ee) {
    }

    try {
        ArrayList alist = device.getAllInterfaces();
        Iterator it = alist.iterator();
        c = DBHelper.getConnection();
        ps = c.prepareStatement(DBHelperConstants.INSERT_IP_TO_ID);

        while (it.hasNext()) {
            NetworkInterface nIf = (NetworkInterface) it.next();

            if (nIf.getIpAddress() == null) {
                continue;
            }

            String ip = nIf.getIpAddress();

            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (Exception ee) {
            }

            rs = DBHelper
                    .executeQuery("select id from ip_to_id where id=" + id + " AND ipaddress='" + ip + "'");

            if ((rs != null) && rs.next()) {
                int devId = rs.getInt("ID");

                if (devId != id) {
                    try {
                        DBHelper.executeUpdate(
                                "delete from ip_to_id where id=" + id + " AND ipaddress='" + ip + "'");
                    } catch (Exception ee) {
                        logger.warn(
                                "Error while deleting from ip_to_id where id=" + id + " AND ipaddress='" + ip,
                                ee);
                    }
                } else {
                    map.remove(ip);

                    continue;
                }
            }

            try {
                ps.setString(1, ip);
                ps.setInt(2, id);
                ps.executeUpdate();
            } catch (Exception e) {
                logger.warn("Error while inserting into ip_to_id where id=" + id + " AND ipaddress='" + ip, e);
            }
        }

        // insert the device ip (in hsrp case none of the device interfaces
        // will have the hsrp ip address
        try {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ee) {
                }
            }

            rs = DBHelper.executeQuery(
                    "select * from ip_to_id where ipaddress='" + device.getIpAddr().toString() + "'");

            if ((rs == null) || !rs.next()) {
                try {
                    ps.setString(1, device.getIpAddr().toString());
                    ps.setInt(2, id);
                    ps.executeUpdate();
                    map.remove(device.getIpAddr().toString());
                } catch (Exception ee) {
                    logger.warn("Error while querying for ip to id", ee);
                }
            } else {
                map.remove(device.getIpAddr().toString());
            }
        } catch (Exception ee) {
            logger.warn("Error in updateIpToIdDetails", ee);
        }

        for (String ip : map.keySet()) {
            try {
                DBHelper.executeUpdate("delete from ip_to_id where ipaddress='" + ip + "'");
            } catch (Exception ee) {
            }
        }
    } catch (Exception ee) {
        logger.warn("Error in updateIpToIdDetails", ee);
    } finally {
        try {
            ps.close();
        } catch (Exception ee) {
        }

        try {
            rs.close();
        } catch (Exception ee) {
        }
        try {
            DBHelper.releaseConnection(c);
        } catch (Exception ee) {
        }
    }
}

From source file:org.akaza.openclinica.control.submit.DataEntryServlet.java

@Override
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    //JN:The following were the the global variables, moved as local.
    locale = LocaleResolver.getLocale(request);
    EventCRFBean ecb = (EventCRFBean) request.getAttribute(INPUT_EVENT_CRF);
    SectionBean sb = (SectionBean) request.getAttribute(SECTION_BEAN);
    ItemDataDAO iddao = new ItemDataDAO(getDataSource(), locale);
    HttpSession session = request.getSession();
    StudyBean currentStudy = (StudyBean) session.getAttribute("study");
    StudyUserRoleBean currentRole = (StudyUserRoleBean) session.getAttribute("userRole");
    SectionDAO sdao = new SectionDAO(getDataSource());
    /**/*  ww w .  j  av a2 s . c o  m*/
     * Determines whether the form was submitted. Calculated once in processRequest. The reason we don't use the normal means to determine if the form was
     * submitted (ie FormProcessor.isSubmitted) is because when we use forwardPage, Java confuses the inputs from the just-processed form with the inputs for
     * the forwarded-to page. This is a problem since frequently we're forwarding from one (submitted) section to the next (unsubmitted) section. If we use the
     * normal means, Java will always think that the unsubmitted section is, in fact, submitted. This member is guaranteed to be calculated before
     * shouldLoadDBValues() is called.
     */
    boolean isSubmitted = false;
    boolean hasGroup = false;

    EventCRFDAO ecdao = null;
    FormProcessor fp = new FormProcessor(request);
    logMe("Enterting DataEntry Servlet" + System.currentTimeMillis());
    EventDefinitionCRFDAO edcdao = new EventDefinitionCRFDAO(getDataSource());

    FormDiscrepancyNotes discNotes;

    panel.setStudyInfoShown(false);
    String age = "";
    UserAccountBean ub = (UserAccountBean) request.getSession().getAttribute(USER_BEAN_NAME);
    String instantAtt = CV_INSTANT_META + ecb.getCRFVersionId();

    //for 11958: repeating groups rows appear if validation returns to the same section
    int isFirstTimeOnSection = fp.getInt("isFirstTimeOnSection");
    request.setAttribute("isFirstTimeOnSection", isFirstTimeOnSection + "");

    if (fp.getString(GO_EXIT).equals("") && !isSubmitted && fp.getString("tabId").equals("")
            && fp.getString("sectionId").equals("")) {
        //HashMap unavailableCRF = getUnavailableCRFList();
        if (getCrfLocker().isLocked(ecb.getId())) {
            int userId = getCrfLocker().getLockOwner(ecb.getId());
            UserAccountDAO udao = new UserAccountDAO(getDataSource());
            UserAccountBean ubean = (UserAccountBean) udao.findByPK(userId);
            addPageMessage(resword.getString("CRF_unavailable") + " " + ubean.getName() + " "
                    + resword.getString("Currently_entering_data") + " " + resword.getString("Leave_the_CRF"),
                    request);

            forwardPage(Page.LIST_STUDY_SUBJECTS_SERVLET, request, response);
        } else {
            getCrfLocker().lock(ecb.getId(), ub.getId());
        }
    }

    if (!ecb.isActive()) {
        throw new InconsistentStateException(Page.LIST_STUDY_SUBJECTS_SERVLET,
                resexception.getString("event_not_exists"));
    }

    logMe("Enterting DataEntry Get the status/number of item discrepancy notes" + System.currentTimeMillis());
    // Get the status/number of item discrepancy notes
    DiscrepancyNoteDAO dndao = new DiscrepancyNoteDAO(getDataSource());
    ArrayList<DiscrepancyNoteBean> allNotes = new ArrayList<DiscrepancyNoteBean>();
    List<DiscrepancyNoteBean> eventCrfNotes = new ArrayList<DiscrepancyNoteBean>();
    List<DiscrepancyNoteThread> noteThreads = new ArrayList<DiscrepancyNoteThread>();
    // BWP: this try block is not necessary try {
    dndao = new DiscrepancyNoteDAO(getDataSource());
    allNotes = dndao.findAllTopNotesByEventCRF(ecb.getId());
    eventCrfNotes = dndao.findOnlyParentEventCRFDNotesFromEventCRF(ecb);
    if (!eventCrfNotes.isEmpty()) {
        allNotes.addAll(eventCrfNotes);

    }
    logMe("Entering DataEntry Create disc note threads out of the various notes" + System.currentTimeMillis());
    // Create disc note threads out of the various notes
    DiscrepancyNoteUtil dNoteUtil = new DiscrepancyNoteUtil();
    noteThreads = dNoteUtil.createThreadsOfParents(allNotes, getDataSource(), currentStudy, null, -1, true);
    // variables that provide values for the CRF discrepancy note header
    int updatedNum = 0;
    int openNum = 0;
    int closedNum = 0;
    int resolvedNum = 0;
    int notAppNum = 0;
    DiscrepancyNoteBean tempBean;

    for (DiscrepancyNoteThread dnThread : noteThreads) {
        /*
         * 3014: do not count parent beans, only the last child disc note of the thread.
         */
        tempBean = dnThread.getLinkedNoteList().getLast();
        if (tempBean != null) {
            if (ResolutionStatus.UPDATED.equals(tempBean.getResStatus())) {
                updatedNum++;
            } else if (ResolutionStatus.OPEN.equals(tempBean.getResStatus())) {
                openNum++;
            } else if (ResolutionStatus.CLOSED.equals(tempBean.getResStatus())) {
                // if (dn.getParentDnId() > 0){
                closedNum++;
                // }
            } else if (ResolutionStatus.RESOLVED.equals(tempBean.getResStatus())) {
                // if (dn.getParentDnId() > 0){
                resolvedNum++;
                // }
            } else if (ResolutionStatus.NOT_APPLICABLE.equals(tempBean.getResStatus())) {
                notAppNum++;
            }
        }

    }
    logMe("Entering DataEntry Create disc note threads out of the various notes DONE"
            + System.currentTimeMillis());
    request.setAttribute("updatedNum", updatedNum + "");
    request.setAttribute("openNum", openNum + "");
    request.setAttribute("closedNum", closedNum + "");
    request.setAttribute("resolvedNum", resolvedNum + "");
    request.setAttribute("notAppNum", notAppNum + "");

    String fromViewNotes = fp.getString("fromViewNotes");
    if (fromViewNotes != null && "1".equals(fromViewNotes)) {
        request.setAttribute("fromViewNotes", fromViewNotes);
    }

    logMe("Entering Create studySubjDao.. ++++stuff" + System.currentTimeMillis());
    StudySubjectDAO ssdao = new StudySubjectDAO(getDataSource());
    StudySubjectBean ssb = (StudySubjectBean) ssdao.findByPK(ecb.getStudySubjectId());
    // YW 11-07-2007, data entry could not be performed if its study subject
    // has been removed.
    // Notice: ViewSectionDataEntryServelet, ViewSectionDataEntryPreview,
    // PrintCRFServlet and PrintDataEntryServlet, have theirs own
    // processRequest
    Status s = ssb.getStatus();
    if ("removed".equalsIgnoreCase(s.getName()) || "auto-removed".equalsIgnoreCase(s.getName())) {
        addPageMessage(respage.getString("you_may_not_perform_data_entry_on_a_CRF")
                + respage.getString("study_subject_has_been_deleted"), request);
        request.setAttribute("id", new Integer(ecb.getStudySubjectId()).toString());
        session.removeAttribute(instantAtt);
        forwardPage(Page.VIEW_STUDY_SUBJECT_SERVLET, request, response);
    }
    // YW >>

    HashMap<String, String> newUploadedFiles = (HashMap<String, String>) session
            .getAttribute("newUploadedFiles");
    if (newUploadedFiles == null) {
        newUploadedFiles = new HashMap<String, String>();
    }
    request.setAttribute("newUploadedFiles", newUploadedFiles);
    if (!fp.getString("exitTo").equals("")) {
        request.setAttribute("exitTo", fp.getString("exitTo"));
    }
    //some EVENT CRF CHECK
    logMe("Entering some EVENT CRF CHECK" + System.currentTimeMillis());
    if (!fp.getString(GO_EXIT).equals("")) {
        session.removeAttribute(GROUP_HAS_DATA);
        session.removeAttribute("to_create_crf");
        session.removeAttribute("mayProcessUploading");
        //Removing the user and EventCRF from the locked CRF List
        getCrfLocker().unlock(ecb.getId());
        if (newUploadedFiles.size() > 0) {
            if (this.unloadFiles(newUploadedFiles)) {

            } else {
                String missed = "";
                Iterator iter = newUploadedFiles.keySet().iterator();
                while (iter.hasNext()) {
                    missed += " " + newUploadedFiles.get(iter.next());
                }
                addPageMessage(respage.getString("uploaded_files_not_deleted_or_not_exist") + ": " + missed,
                        request);
            }
        }
        session.removeAttribute("newUploadedFiles");
        addPageMessage(respage.getString("exit_without_saving"), request);
        // addPageMessage("You chose to exit the data entry page.");
        // changed bu jxu 03/06/2007- we should use redirection to go to
        // another servlet
        if (fromViewNotes != null && "1".equals(fromViewNotes)) {
            String viewNotesURL = (String) session.getAttribute("viewNotesURL");
            if (viewNotesURL != null && viewNotesURL.length() > 0) {
                response.sendRedirect(response.encodeRedirectURL(viewNotesURL));
            }
            return;
        }
        String fromResolvingNotes = fp.getString("fromResolvingNotes", true);
        String winLocation = (String) session.getAttribute(ViewNotesServlet.WIN_LOCATION);
        session.removeAttribute(instantAtt);
        if (!StringUtil.isBlank(fromResolvingNotes) && !StringUtil.isBlank(winLocation)) {
            response.sendRedirect(response.encodeRedirectURL(winLocation));
        } else {
            if (!fp.getString("exitTo").equals("")) {
                response.sendRedirect(response.encodeRedirectURL(fp.getString("exitTo")));
            } else
                response.sendRedirect(response.encodeRedirectURL("ListStudySubjects"));
        }
        // forwardPage(Page.SUBMIT_DATA_SERVLET);
        return;
    }
    logMe("Entering some EVENT CRF CHECK DONE" + System.currentTimeMillis());
    // checks if the section has items in item group
    // for repeating items
    // hasGroup = getInputBeans();
    hasGroup = checkGroups(fp, ecb);

    Boolean b = (Boolean) request.getAttribute(INPUT_IGNORE_PARAMETERS);
    isSubmitted = fp.isSubmitted() && b == null;
    // variable is used for fetching any null values like "not applicable"
    int eventDefinitionCRFId = 0;
    if (fp != null) {
        eventDefinitionCRFId = fp.getInt("eventDefinitionCRFId");
    }

    StudyBean study = (StudyBean) session.getAttribute("study");
    // constructs the list of items used on UI
    // tbh>>
    // logger.trace("trying event def crf id: "+eventDefinitionCRFId);
    logMe("Entering some EVENT DEF CRF CHECK " + System.currentTimeMillis());

    if (eventDefinitionCRFId <= 0) {
        // TODO we have to get that id before we can continue
        EventDefinitionCRFBean edcBean = edcdao.findByStudyEventIdAndCRFVersionId(study, ecb.getStudyEventId(),
                ecb.getCRFVersionId());
        eventDefinitionCRFId = edcBean.getId();
    }

    logMe("Entering some EVENT DEF CRF CHECK DONE " + System.currentTimeMillis());
    logMe("Entering some Study EVENT DEF CRF CHECK  " + System.currentTimeMillis());
    StudyEventDAO seDao = new StudyEventDAO(getDataSource());
    EventDefinitionCRFBean edcBean = (EventDefinitionCRFBean) edcdao.findByPK(eventDefinitionCRFId);
    EventDefinitionCRFBean edcb = (EventDefinitionCRFBean) edcdao.findByPK(eventDefinitionCRFId);
    request.setAttribute(EVENT_DEF_CRF_BEAN, edcb);//JN:Putting the event_def_crf_bean in the request attribute.

    StudyEventBean studyEventBean = (StudyEventBean) seDao.findByPK(ecb.getStudyEventId());
    edcBean.setId(eventDefinitionCRFId);

    StudyEventDefinitionDAO seddao = new StudyEventDefinitionDAO(getDataSource());
    StudyEventDefinitionBean studyEventDefinition = (StudyEventDefinitionBean) seddao
            .findByPK(edcBean.getStudyEventDefinitionId());

    CRFVersionDAO cvdao = new CRFVersionDAO(getDataSource());
    CRFVersionBean crfVersionBean = (CRFVersionBean) cvdao.findByPK(ecb.getCRFVersionId());

    Phase phase2 = Phase.INITIAL_DATA_ENTRY;
    if (getServletPage(request).equals(Page.DOUBLE_DATA_ENTRY_SERVLET)) {
        phase2 = Phase.DOUBLE_DATA_ENTRY;
    } else if (getServletPage(request).equals(Page.ADMIN_EDIT_SERVLET)) {
        phase2 = Phase.ADMIN_EDITING;
    }
    logMe("Entering ruleSets::: CreateAndInitializeRuleSet:::" + Thread.currentThread());
    logMe("Entering ruleSets::: CreateAndInitializeRuleSet:::" + Thread.currentThread() + "currentStudy?"
            + currentStudy + "studyEventDefinition" + studyEventDefinition + "crfVersionBean" + crfVersionBean
            + "studyEventBean" + studyEventBean + "ecb" + ecb);
    //  List<RuleSetBean> ruleSets = createAndInitializeRuleSet(currentStudy, studyEventDefinition, crfVersionBean, studyEventBean, ecb, true, request, response);
    // boolean shouldRunRules = getRuleSetService(request).shouldRunRulesForRuleSets(ruleSets, phase2);
    logMe("Entering getDisplayBean:::::Thread::::" + Thread.currentThread());
    DisplaySectionBean section = getDisplayBean(hasGroup, false, request, isSubmitted);
    //hasSCDItem has been initialized in getDisplayBean() which is online above

    VariableSubstitutionHelper.replaceVariables(section, study, ssb, studyEventDefinition, studyEventBean,
            dataSource);

    if (section.getSection().hasSCDItem()) {
        SimpleConditionalDisplayService cds0 = (SimpleConditionalDisplayService) SpringServletAccess
                .getApplicationContext(getServletContext()).getBean("simpleConditionalDisplayService");
        section = cds0.initConditionalDisplays(section);
    }
    logMe("Entering  Find out the id of the section's first field " + System.currentTimeMillis());

    // 2790: Find out the id of the section's first field
    String firstFieldId = getSectionFirstFieldId(section.getSection().getId());
    request.setAttribute("formFirstField", firstFieldId);

    // logger.trace("now trying event def crf id: "+eventDefinitionCRFId);
    // above is necessary to give us null values during DDE
    // ironically, this only covers vertical null value result sets
    // horizontal ones are covered in FormBeanUtil, tbh 112007
    logMe("Entering  displayItemWithGroups " + System.currentTimeMillis());
    //@pgawade 30-May-2012 Fix for issue 13963 - added an extra parameter 'isSubmitted' to method createItemWithGroups
    List<DisplayItemWithGroupBean> displayItemWithGroups = createItemWithGroups(section, hasGroup,
            eventDefinitionCRFId, request, isSubmitted);
    logMe("Entering  displayItemWithGroups end " + System.currentTimeMillis());
    this.getItemMetadataService().updateGroupDynamicsInSection(displayItemWithGroups,
            section.getSection().getId(), ecb);
    section.setDisplayItemGroups(displayItemWithGroups);
    DisplayTableOfContentsBean toc = TableOfContentsServlet.getDisplayBeanWithShownSections(getDataSource(),
            (DisplayTableOfContentsBean) request.getAttribute(TOC_DISPLAY),
            (DynamicsMetadataService) SpringServletAccess.getApplicationContext(getServletContext())
                    .getBean("dynamicsMetadataService"));
    request.setAttribute(TOC_DISPLAY, toc);
    LinkedList<Integer> sectionIdsInToc = TableOfContentsServlet.sectionIdsInToc(toc);

    // why do we get previousSec and nextSec here, rather than in
    // getDisplayBeans?
    // so that we can use them in forwarding the user to the previous/next
    // section
    // if the validation was successful
    logMe("Entering  displayItemWithGroups sdao.findPrevious  " + System.currentTimeMillis());
    int sIndex = TableOfContentsServlet.sectionIndexInToc(section.getSection(), toc, sectionIdsInToc);
    SectionBean previousSec = this.prevSection(section.getSection(), ecb, toc, sIndex);
    logMe("Entering  displayItemWithGroups sdao.findPrevious  end " + System.currentTimeMillis());
    SectionBean nextSec = this.nextSection(section.getSection(), ecb, toc, sIndex);
    section.setFirstSection(!previousSec.isActive());
    section.setLastSection(!nextSec.isActive());

    // this is for generating side info panel
    // and the information panel under the Title
    SubjectDAO subjectDao = new SubjectDAO(getDataSource());
    StudyDAO studydao = new StudyDAO(getDataSource());
    SubjectBean subject = (SubjectBean) subjectDao.findByPK(ssb.getSubjectId());

    // Get the study then the parent study
    logMe("Entering  Get the study then the parent study   " + System.currentTimeMillis());
    if (study.getParentStudyId() > 0) {
        // this is a site,find parent
        StudyBean parentStudy = (StudyBean) studydao.findByPK(study.getParentStudyId());
        request.setAttribute("studyTitle", parentStudy.getName());
        request.setAttribute("siteTitle", study.getName());
    } else {
        request.setAttribute("studyTitle", study.getName());
    }

    logMe("Entering  Get the study then the parent study end  " + System.currentTimeMillis());
    // Let us process the age
    if (currentStudy.getStudyParameterConfig().getCollectDob().equals("1")) {
        // YW 11-16-2007 erollment-date is used for calculating age.
        Date enrollmentDate = ssb.getEnrollmentDate();
        age = Utils.getInstacne().processAge(enrollmentDate, subject.getDateOfBirth());
    }
    //ArrayList beans = ViewStudySubjectServlet.getDisplayStudyEventsForStudySubject(ssb, getDataSource(), ub, currentRole);
    request.setAttribute("studySubject", ssb);
    request.setAttribute("subject", subject);
    //request.setAttribute("beans", beans);
    request.setAttribute("eventCRF", ecb);
    request.setAttribute("age", age);
    request.setAttribute("decryptedPassword",
            ((SecurityManager) SpringServletAccess.getApplicationContext(getServletContext())
                    .getBean("securityManager")).encrytPassword("root", getUserDetails()));

    // set up interviewer name and date
    fp.addPresetValue(INPUT_INTERVIEWER, ecb.getInterviewerName());

    if (ecb.getDateInterviewed() != null) {
        DateFormat local_df = new SimpleDateFormat(resformat.getString("date_format_string"),
                ResourceBundleProvider.getLocale());
        String idateFormatted = local_df.format(ecb.getDateInterviewed());
        fp.addPresetValue(INPUT_INTERVIEW_DATE, idateFormatted);
    } else {
        fp.addPresetValue(INPUT_INTERVIEW_DATE, "");
    }
    setPresetValues(fp.getPresetValues(), request);
    logMe("Entering Checks !submitted  " + System.currentTimeMillis());
    if (!isSubmitted) {
        // TODO: prevent data enterer from seeing results of first round of
        // data
        // entry, if this is second round
        // FLAG--SLOW HERE WHEN LOADING
        logMe("Entering Checks !submitted entered  " + System.currentTimeMillis());
        long t = System.currentTimeMillis();
        request.setAttribute(BEAN_DISPLAY, section);
        request.setAttribute(BEAN_ANNOTATIONS, getEventCRFAnnotations(request));
        session.setAttribute("shouldRunValidation", null);
        session.setAttribute("rulesErrors", null);
        session.setAttribute(DataEntryServlet.NOTE_SUBMITTED, null);

        discNotes = new FormDiscrepancyNotes();

        //            discNotes = (FormDiscrepancyNotes) session.getAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME);
        //            if (discNotes == null) {
        //                discNotes = new FormDiscrepancyNotes();
        //            }
        // << tbh 01/2010

        section = populateNotesWithDBNoteCounts(discNotes, section, request);
        populateInstantOnChange(request.getSession(), ecb, section);
        LOGGER.debug(
                "+++ just ran populateNotes, printing field notes: " + discNotes.getFieldNotes().toString());
        LOGGER.debug("found disc notes: " + discNotes.getNumExistingFieldNotes().toString());
        session.setAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME, discNotes);

        if (section.getSection().hasSCDItem()) {
            section = SCDItemDisplayInfo.generateSCDDisplayInfo(section,
                    this.getServletPage(request).equals(Page.INITIAL_DATA_ENTRY)
                            || this.getServletPage(request).equals(Page.ADMIN_EDIT_SERVLET)
                                    && !this.isAdminForcedReasonForChange(request));
        }

        int keyId = ecb.getId();
        session.removeAttribute(DoubleDataEntryServlet.COUNT_VALIDATE + keyId);

        setUpPanel(section);
        if (newUploadedFiles.size() > 0) {
            if (this.unloadFiles(newUploadedFiles)) {

            } else {
                String missed = "";
                Iterator iter = newUploadedFiles.keySet().iterator();
                while (iter.hasNext()) {
                    missed += " " + newUploadedFiles.get(iter.next());
                }
                addPageMessage(respage.getString("uploaded_files_not_deleted_or_not_exist") + ": " + missed,
                        request);
            }
        }
        logMe("Entering Checks !submitted entered end forwarding page " + System.currentTimeMillis());
        logMe("Time Took for this block" + (System.currentTimeMillis() - t));
        forwardPage(getJSPPage(), request, response);
        return;
    } else {
        logMe("Entering Checks !submitted not entered  " + System.currentTimeMillis());
        //
        // VALIDATION / LOADING DATA
        //
        // If validation is required for this round, we will go through
        // each item and add an appropriate validation to the Validator
        //
        // Otherwise, we will just load the data into the DisplayItemBean
        // so that we can write to the database later.
        //
        // Validation is required if two conditions are met:
        // 1. The user clicked a "Save" button, not a "Confirm" button
        // 2. In this type of data entry servlet, when the user clicks
        // a Save button, the inputs are validated
        //

        boolean validate = fp.getBoolean(INPUT_CHECK_INPUTS) && validateInputOnFirstRound();
        // did the user click a "Save" button?
        // is validation required in this type of servlet when the user
        // clicks
        // "Save"?
        // We can conclude that the user is trying to save data; therefore,
        // set a request
        // attribute indicating that default values for items shouldn't be
        // displayed
        // in the application UI that will subsequently be displayed
        // TODO: find a better, less random place for this
        // session.setAttribute(HAS_DATA_FLAG, true);

        // section.setCheckInputs(fp.getBoolean(INPUT_CHECK_INPUTS));
        errors = new HashMap();
        // ArrayList items = section.getItems();

        discNotes = (FormDiscrepancyNotes) session
                .getAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME);
        if (discNotes == null) {
            discNotes = new FormDiscrepancyNotes();
        }

        // populateNotesWithDBNoteCounts(discNotes, section);

        // all items- inlcude items in item groups and other single items
        List<DisplayItemWithGroupBean> allItems = section.getDisplayItemGroups();
        String attachedFilePath = Utils.getAttachedFilePath(currentStudy);

        DiscrepancyValidator v = new DiscrepancyValidator(request, discNotes);
        RuleValidator ruleValidator = new RuleValidator(request);
        LOGGER.debug("SZE 1  :: " + allItems.size());
        logMe("Looping inside !submitted  " + System.currentTimeMillis());
        for (int i = 0; i < allItems.size(); i++) {
            LOGGER.trace("===itering through items: " + i);
            DisplayItemWithGroupBean diwg = allItems.get(i);
            if (diwg.isInGroup()) {
                // for the items in groups
                DisplayItemGroupBean dgb = diwg.getItemGroup();
                List<DisplayItemGroupBean> dbGroups = diwg.getDbItemGroups();
                //dbGroups = diwg.getItemGroups();
                List<DisplayItemGroupBean> formGroups = new ArrayList<DisplayItemGroupBean>();
                LOGGER.debug("got db item group size " + dbGroups.size());

                if (validate) {
                    // int manualGroups = getManualRows(dbGroups2);
                    // logger.debug("+++ found manual rows from db group 2: " + manualGroups);
                    LOGGER.debug("===IF VALIDATE NOT A SINGLE ITEM: got to this part in the validation loop: "
                            + dgb.getGroupMetaBean().getName());
                    // TODO next marker tbh 112007
                    // formGroups = validateDisplayItemGroupBean(v,
                    // dgb,dbGroups, formGroups,
                    // ruleValidator,groupOrdinalPLusItemOid);
                    formGroups = validateDisplayItemGroupBean(v, dgb, dbGroups, formGroups, request, response);
                    LOGGER.debug("form group size after validation " + formGroups.size());
                } else {
                    LOGGER.debug("+++ELSE NOT A SINGLE ITEM: got to this part in the validation loop: "
                            + dgb.getGroupMetaBean().getName());
                    formGroups = loadFormValueForItemGroup(dgb, dbGroups, formGroups, eventDefinitionCRFId,
                            request);
                    LOGGER.debug("form group size without validation " + formGroups.size());
                }

                diwg.setItemGroup(dgb);
                diwg.setItemGroups(formGroups);

                allItems.set(i, diwg);

            } else {// other single items
                DisplayItemBean dib = diwg.getSingleItem();
                // dib = (DisplayItemBean) allItems.get(i);
                if (validate) {
                    // generate input name here?
                    // DisplayItemGroupBean dgb = diwg.getItemGroup();
                    String itemName = getInputName(dib);
                    // no Item group for single item, so just use blank
                    // string as parameter for inputName

                    dib = validateDisplayItemBean(v, dib, "", request);// this
                    // should be
                    // used,
                    // otherwise,
                    // DDE not
                    // working-jxu

                    LOGGER.debug("&&& found name: " + itemName);
                    LOGGER.debug("input VALIDATE " + itemName + ": " + fp.getString(itemName));
                    // dib.loadFormValue(fp.getString(itemName));
                    LOGGER.debug("input " + itemName + " has a response set of "
                            + dib.getMetadata().getResponseSet().getOptions().size() + " options");
                } else {
                    String itemName = getInputName(dib);
                    LOGGER.debug("input NONVALIDATE " + itemName + ": " + fp.getString(itemName));
                    // dib.loadFormValue(itemName);
                    dib = loadFormValue(dib, request);
                    // String itemName = getInputName(dib);
                    // dib = loadFormValue(itemName);
                }

                ArrayList children = dib.getChildren();
                for (int j = 0; j < children.size(); j++) {
                    DisplayItemBean child = (DisplayItemBean) children.get(j);
                    // DisplayItemGroupBean dgb = diwg.getItemGroup();
                    String itemName = getInputName(child);
                    child.loadFormValue(fp.getString(itemName));
                    if (validate) {
                        // child = validateDisplayItemBean(v, child,
                        // itemName, ruleValidator, groupOrdinalPLusItemOid,
                        // false);
                        child = validateDisplayItemBean(v, child, itemName, request);
                        // was null changed value 092007 tbh
                    } else {
                        // child.loadFormValue(itemName);
                        child = loadFormValue(child, request);
                    }
                    LOGGER.debug("Checking child value for " + itemName + ": " + child.getData().getValue());
                    children.set(j, child);
                }

                dib.setChildren(children);
                diwg.setSingleItem(runDynamicsItemCheck(dib, null, request));
                // logger.trace("just set single item on line 447:
                // "+dib.getData().getValue());
                // items.set(i, dib);
                LOGGER.debug(" I : " + i);
                allItems.set(i, diwg);

            }
        }
        logMe("Loop ended  " + System.currentTimeMillis());
        //JN: This is the list that contains all the scd-shown items.
        List<ItemBean> itemBeansWithSCDShown = new ArrayList<ItemBean>();
        if (validate && section.getSection().hasSCDItem()) {
            logMe(" Validate and Loop  " + System.currentTimeMillis());

            for (int i = 0; i < allItems.size(); ++i) {
                DisplayItemBean dib = allItems.get(i).getSingleItem();
                ItemFormMetadataBean ifmb = dib.getMetadata();
                if (ifmb.getParentId() == 0) {
                    if (dib.getScdData().getScdSetsForControl().size() > 0) {
                        //for control item
                        //dib has to loadFormValue first. Here loadFormValue has been done in validateDisplayItemBean(v, dib, "")
                        section.setShowSCDItemIds(SimpleConditionalDisplayService
                                .conditionalDisplayToBeShown(dib, section.getShowSCDItemIds()));
                    }
                    if (dib.getScdData().getScdItemMetadataBean().getScdItemFormMetadataId() > 0) {
                        //for scd item
                        //a control item is always before its scd item
                        dib.setIsSCDtoBeShown(
                                section.getShowSCDItemIds().contains(dib.getMetadata().getItemId()));
                        if (dib.getIsSCDtoBeShown())
                            itemBeansWithSCDShown.add(dib.getItem());

                        validateSCDItemBean(v, dib);
                    }
                    ArrayList<DisplayItemBean> children = dib.getChildren();
                    for (int j = 0; j < children.size(); j++) {
                        DisplayItemBean child = children.get(j);
                        if (child.getScdData().getScdSetsForControl().size() > 0) {
                            //for control item
                            //dib has to loadFormValue first. Here loadFormValue has been done in validateDisplayItemBean(v, dib, "")
                            section.setShowSCDItemIds(SimpleConditionalDisplayService
                                    .conditionalDisplayToBeShown(child, section.getShowSCDItemIds()));
                        }
                        if (child.getScdData().getScdItemMetadataBean().getScdItemFormMetadataId() > 0) {
                            //for scd item
                            //a control item is always before its scd item
                            child.setIsSCDtoBeShown(
                                    section.getShowSCDItemIds().contains(child.getMetadata().getItemId()));
                            if (child.getIsSCDtoBeShown())
                                itemBeansWithSCDShown.add(dib.getItem());
                            validateSCDItemBean(v, child);
                        }
                    }
                }
            }
            logMe(" Validate and Loop end  " + System.currentTimeMillis());
        }
        logMe(" Validate and Loop end  " + System.currentTimeMillis());
        //JN:calling again here, for the items with simple conditional display and rules.
        List<RuleSetBean> ruleSets = createAndInitializeRuleSet(currentStudy, studyEventDefinition,
                crfVersionBean, studyEventBean, ecb, true, request, response, itemBeansWithSCDShown);
        boolean shouldRunRules = getRuleSetService(request).shouldRunRulesForRuleSets(ruleSets, phase2);

        // this.getItemMetadataService().resetItemCounter();
        HashMap<String, ArrayList<String>> groupOrdinalPLusItemOid = null;
        groupOrdinalPLusItemOid = runRules(allItems, ruleSets, true, shouldRunRules, MessageType.ERROR, phase2,
                ecb, request);

        logMe("allItems  Loop begin  " + System.currentTimeMillis());
        for (int i = 0; i < allItems.size(); i++) {
            DisplayItemWithGroupBean diwg = allItems.get(i);
            if (diwg.isInGroup()) {
                // for the items in groups
                DisplayItemGroupBean dgb = diwg.getItemGroup();
                List<DisplayItemGroupBean> dbGroups = diwg.getDbItemGroups();
                //dbGroups = diwg.getItemGroups();
                // tbh 01/2010 change the group list?
                List<DisplayItemGroupBean> formGroups = new ArrayList<DisplayItemGroupBean>();
                // List<DisplayItemGroupBean> dbGroups2 = loadFormValueForItemGroup(dgb, dbGroups, formGroups, eventDefinitionCRFId);
                // jxu- this part need to be refined, why need to validate
                // items again?
                if (validate) {
                    // int manualGroups = getManualRows(dbGroups2);
                    // logger.debug("+++ found manual rows for db group2: " + manualGroups);
                    formGroups = validateDisplayItemGroupBean(v, dgb, dbGroups, formGroups, ruleValidator,
                            groupOrdinalPLusItemOid, request, response);
                    // formGroups = validateDisplayItemGroupBean(v, dgb,
                    // dbGroups, formGroups);
                    LOGGER.debug("*** form group size after validation " + formGroups.size());
                }
                diwg.setItemGroup(dgb);
                diwg.setItemGroups(formGroups);

                allItems.set(i, diwg);

            } else {// other single items
                DisplayItemBean dib = diwg.getSingleItem();
                // dib = (DisplayItemBean) allItems.get(i);
                if (validate) {
                    String itemName = getInputName(dib);
                    dib = validateDisplayItemBean(v, dib, "", ruleValidator, groupOrdinalPLusItemOid, false,
                            null, request);//
                    // / dib = validateDisplayItemBean(v, dib, "");// this
                }
                ArrayList children = dib.getChildren();
                for (int j = 0; j < children.size(); j++) {

                    DisplayItemBean child = (DisplayItemBean) children.get(j);
                    // DisplayItemGroupBean dgb = diwg.getItemGroup();
                    String itemName = getInputName(child);
                    child.loadFormValue(fp.getString(itemName));
                    if (validate) {
                        child = validateDisplayItemBean(v, child, "", ruleValidator, groupOrdinalPLusItemOid,
                                false, null, request);
                        // child = validateDisplayItemBean(v, child,
                        // itemName);
                    }
                    children.set(j, child);
                    LOGGER.debug(" J (children): " + j);
                }

                dib.setChildren(children);
                diwg.setSingleItem(runDynamicsItemCheck(dib, null, request));
                LOGGER.debug(" I : " + i);
                allItems.set(i, diwg);
            }
        }
        logMe("allItems  Loop end  " + System.currentTimeMillis());
        // YW, 2-1-2008 <<
        // A map from item name to item bean object.
        HashMap<String, ItemBean> scoreItems = new HashMap<String, ItemBean>();
        HashMap<String, String> scoreItemdata = new HashMap<String, String>();
        HashMap<String, ItemDataBean> oldItemdata = prepareSectionItemdataObject(sb.getId(), request);
        // hold all item names of changed ItemBean in current section
        TreeSet<String> changedItems = new TreeSet<String>();
        // holds complete disply item beans for checking against 'request
        // for change' restriction
        ArrayList<DisplayItemBean> changedItemsList = new ArrayList<DisplayItemBean>();
        // key is repeating item name, value is its display item group bean
        HashMap<String, DisplayItemGroupBean> changedItemsMap = new HashMap<String, DisplayItemGroupBean>();
        // key is itemid, value is set of itemdata-ordinal
        HashMap<Integer, TreeSet<Integer>> itemOrdinals = prepareItemdataOrdinals(request);

        // prepare item data for scoring
        updateDataOrdinals(allItems);
        section.setDisplayItemGroups(allItems);
        scoreItems = prepareScoreItems(request);
        scoreItemdata = prepareScoreItemdata(request);
        logMe("allItems 2 Loop begin  " + System.currentTimeMillis());
        for (int i = 0; i < allItems.size(); i++) {
            DisplayItemWithGroupBean diwb = allItems.get(i);
            if (diwb.isInGroup()) {
                List<DisplayItemGroupBean> dbGroups = diwb.getDbItemGroups();
                for (int j = 0; j < dbGroups.size(); j++) {
                    DisplayItemGroupBean displayGroup = dbGroups.get(j);
                    List<DisplayItemBean> items = displayGroup.getItems();
                    if ("remove".equalsIgnoreCase(displayGroup.getEditFlag())) {
                        for (DisplayItemBean displayItem : items) {
                            int itemId = displayItem.getItem().getId();
                            int ordinal = displayItem.getData().getOrdinal();
                            if (itemOrdinals.containsKey(itemId)) {
                                itemOrdinals.get(itemId).remove(ordinal);
                            }
                            if (scoreItemdata.containsKey(itemId + "_" + ordinal)) {
                                scoreItemdata.remove(itemId + "_" + ordinal);
                            }
                            changedItems.add(displayItem.getItem().getName());
                            changedItemsList.add(displayItem);

                            String formName = displayItem.getItem().getName();
                            // logger.debug("SET: formName:" + formName);
                            if (displayGroup.isAuto()) {
                                formName = getGroupItemInputName(displayGroup,
                                        displayGroup.getFormInputOrdinal(), displayItem);

                                LOGGER.debug("GET: changed formName to " + formName);

                            } else {
                                formName = getGroupItemManualInputName(displayGroup,
                                        displayGroup.getFormInputOrdinal(), displayItem);
                                LOGGER.debug("GET-MANUAL: changed formName to " + formName);
                            }
                            changedItemsMap.put(formName, displayGroup);
                            LOGGER.debug("adding to changed items map: " + formName);
                        }
                    }
                }

                List<DisplayItemGroupBean> dgbs = diwb.getItemGroups();
                int groupsize = dgbs.size();
                HashMap<Integer, Integer> maxOrdinals = new HashMap<Integer, Integer>();
                boolean first = true;
                for (int j = 0; j < dgbs.size(); j++) {
                    DisplayItemGroupBean displayGroup = dgbs.get(j);
                    List<DisplayItemBean> items = displayGroup.getItems();
                    boolean isAdd = "add".equalsIgnoreCase(displayGroup.getEditFlag()) ? true : false;
                    for (DisplayItemBean displayItem : items) {
                        ItemBean ib = displayItem.getItem();
                        String itemName = ib.getName();
                        int itemId = ib.getId();
                        if (first) {
                            maxOrdinals.put(itemId,
                                    iddao.getMaxOrdinalForGroup(ecb, sb, displayGroup.getItemGroupBean()));
                        }
                        ItemDataBean idb = displayItem.getData();
                        String value = idb.getValue();
                        scoreItems.put(itemName, ib);
                        int ordinal = displayItem.getData().getOrdinal();
                        if (isAdd && scoreItemdata.containsKey(itemId + "_" + ordinal)) {
                            int formMax = 1;
                            if (maxOrdinals.containsKey(itemId)) {
                                formMax = maxOrdinals.get(itemId);
                            }
                            int dbMax = iddao.getMaxOrdinalForGroup(ecb, sb, displayGroup.getItemGroupBean());
                            ordinal = ordinal >= dbMax ? formMax + 1 : ordinal;
                            maxOrdinals.put(itemId, ordinal);
                            displayItem.getData().setOrdinal(ordinal);
                            scoreItemdata.put(itemId + "_" + ordinal, value);
                        } else {
                            scoreItemdata.put(itemId + "_" + ordinal, value);
                        }
                        if (itemOrdinals.containsKey(itemId)) {
                            itemOrdinals.get(itemId).add(ordinal);
                        } else {
                            TreeSet<Integer> ordinalSet = new TreeSet<Integer>();
                            ordinalSet.add(ordinal);
                            itemOrdinals.put(itemId, ordinalSet);
                        }
                        if (isChanged(displayItem, oldItemdata, attachedFilePath)) {
                            changedItems.add(itemName);
                            changedItemsList.add(displayItem);
                            String formName = displayItem.getItem().getName();
                            // logger.debug("SET: formName:" + formName);
                            if (displayGroup.isAuto()) {
                                formName = getGroupItemInputName(displayGroup,
                                        displayGroup.getFormInputOrdinal(), displayItem);
                                LOGGER.debug("RESET: formName group-item-input:" + formName);

                            } else {
                                formName = getGroupItemManualInputName(displayGroup,
                                        displayGroup.getFormInputOrdinal(), displayItem);
                                LOGGER.debug("RESET: formName group-item-input-manual:" + formName);
                            }
                            changedItemsMap.put(formName, displayGroup);
                            LOGGER.debug("adding to changed items map: " + formName);
                        }
                    }
                    first = false;
                }
            } else {
                DisplayItemBean dib = diwb.getSingleItem();
                ItemBean ib = dib.getItem();
                ItemDataBean idb = dib.getData();
                int itemId = ib.getId();
                String itemName = ib.getName();
                String value = idb.getValue();
                scoreItems.put(itemName, ib);
                // for items which are not in any group, their ordinal is
                // set as 1
                TreeSet<Integer> ordinalset = new TreeSet<Integer>();
                ordinalset.add(1);
                itemOrdinals.put(itemId, ordinalset);
                scoreItemdata.put(itemId + "_" + 1, value);
                if (isChanged(idb, oldItemdata, dib, attachedFilePath)) {
                    changedItems.add(itemName);
                    changedItemsList.add(dib);
                    // changedItemsMap.put(dib.getItem().getName(), new
                    // DisplayItemGroupBean());
                }

                ArrayList children = dib.getChildren();
                for (int j = 0; j < children.size(); j++) {
                    DisplayItemBean child = (DisplayItemBean) children.get(j);
                    ItemBean cib = child.getItem();
                    scoreItems.put(cib.getName(), cib);
                    TreeSet<Integer> cordinalset = new TreeSet<Integer>();
                    cordinalset.add(1);
                    itemOrdinals.put(itemId, cordinalset);
                    scoreItemdata.put(cib.getId() + "_" + 1, child.getData().getValue());
                    if (isChanged(child.getData(), oldItemdata, child, attachedFilePath)) {
                        changedItems.add(itemName);
                        changedItemsList.add(child);
                        // changedItemsMap.put(itemName, new
                        // DisplayItemGroupBean());
                    }
                }
            }
        }
        logMe("allItems 2 Loop end  " + System.currentTimeMillis());
        // do calculation for 'calculation' and 'group-calculation' type
        // items
        // and write the result in DisplayItemBean's ItemDateBean - data
        ScoreItemValidator sv = new ScoreItemValidator(request, discNotes);
        // *** doing calc here, load it where? ***
        SessionManager sm = (SessionManager) request.getSession().getAttribute("sm");
        ScoreCalculator sc = new ScoreCalculator(sm, ecb, ub);
        logMe("allItems 3 Loop begin  " + System.currentTimeMillis());
        for (int i = 0; i < allItems.size(); i++) {
            DisplayItemWithGroupBean diwb = allItems.get(i);
            if (diwb.isInGroup()) {
                List<DisplayItemGroupBean> dgbs = diwb.getItemGroups();
                for (int j = 0; j < dgbs.size(); j++) {
                    DisplayItemGroupBean displayGroup = dgbs.get(j);

                    List<DisplayItemBean> items = displayGroup.getItems();
                    for (DisplayItemBean displayItem : items) {
                        ItemFormMetadataBean ifmb = displayItem.getMetadata();
                        int responseTypeId = ifmb.getResponseSet().getResponseTypeId();
                        if (responseTypeId == 8 || responseTypeId == 9) {
                            StringBuffer err = new StringBuffer();
                            ResponseOptionBean robBean = (ResponseOptionBean) ifmb.getResponseSet().getOptions()
                                    .get(0);
                            String value = "";

                            String inputName = "";
                            // note that we have to use
                            // getFormInputOrdinal() here, tbh 06/2009
                            if (displayGroup.isAuto()) {
                                inputName = getGroupItemInputName(displayGroup,
                                        displayGroup.getFormInputOrdinal(), displayItem);
                                LOGGER.debug("returning input name: " + inputName);
                            } else {
                                inputName = getGroupItemManualInputName(displayGroup,
                                        displayGroup.getFormInputOrdinal(), displayItem);
                                LOGGER.debug("returning input name: " + inputName);
                            }
                            if (robBean.getValue().startsWith("func: getexternalvalue")
                                    || robBean.getValue().startsWith("func: getExternalValue")) {

                                value = fp.getString(inputName);
                                LOGGER.debug("*** just set " + fp.getString(inputName) + " for line 815 "
                                        + displayItem.getItem().getName() + " with input name " + inputName);

                            } else {
                                value = sc.doCalculation(displayItem, scoreItems, scoreItemdata, itemOrdinals,
                                        err, displayItem.getData().getOrdinal());
                            }
                            displayItem.loadFormValue(value);
                            if (isChanged(displayItem, oldItemdata, attachedFilePath)) {
                                changedItems.add(displayItem.getItem().getName());
                                changedItemsList.add(displayItem);
                            }

                            request.setAttribute(inputName, value);
                            if (validate) {
                                displayItem = validateCalcTypeDisplayItemBean(sv, displayItem, inputName,
                                        request);
                                if (err.length() > 0) {
                                    Validation validation = new Validation(Validator.CALCULATION_FAILED);
                                    validation.setErrorMessage(err.toString());
                                    sv.addValidation(inputName, validation);
                                }
                            }
                        }
                    }
                }
            } else {
                DisplayItemBean dib = diwb.getSingleItem();
                ItemFormMetadataBean ifmb = dib.getMetadata();
                int responseTypeId = ifmb.getResponseSet().getResponseTypeId();
                if (responseTypeId == 8 || responseTypeId == 9) {
                    StringBuffer err = new StringBuffer();
                    ResponseOptionBean robBean = (ResponseOptionBean) ifmb.getResponseSet().getOptions().get(0);
                    String value = "";
                    if (robBean.getValue().startsWith("func: getexternalvalue")
                            || robBean.getValue().startsWith("func: getExternalValue")) {
                        String itemName = getInputName(dib);
                        value = fp.getString(itemName);
                        LOGGER.debug("just set " + fp.getString(itemName) + " for " + dib.getItem().getName());
                        LOGGER.debug("found in fp: " + fp.getString(dib.getItem().getName()));
                        // logger.debug("scoreitemdata: " +
                        // scoreItemdata.toString());
                    } else {
                        value = sc.doCalculation(dib, scoreItems, scoreItemdata, itemOrdinals, err, 1);
                    }
                    dib.loadFormValue(value);
                    if (isChanged(dib.getData(), oldItemdata, dib, attachedFilePath)) {
                        changedItems.add(dib.getItem().getName());
                        changedItemsList.add(dib);
                        // changedItemsMap.put(dib.getItem().getName(), new
                        // DisplayItemGroupBean());
                    }
                    String inputName = getInputName(dib);
                    request.setAttribute(inputName, value);
                    if (validate) {
                        dib = validateCalcTypeDisplayItemBean(sv, dib, "", request);
                        if (err.length() > 0) {
                            Validation validation = new Validation(Validator.CALCULATION_FAILED);
                            validation.setErrorMessage(err.toString());
                            sv.addValidation(inputName, validation);
                        }
                    }
                }

                ArrayList children = dib.getChildren();
                for (int j = 0; j < children.size(); j++) {
                    DisplayItemBean child = (DisplayItemBean) children.get(j);
                    ItemFormMetadataBean cifmb = child.getMetadata();
                    int resTypeId = cifmb.getResponseSet().getResponseTypeId();
                    if (resTypeId == 8 || resTypeId == 9) {
                        StringBuffer cerr = new StringBuffer();
                        child.getDbData().setValue(child.getData().getValue());
                        ResponseOptionBean crobBean = (ResponseOptionBean) cifmb.getResponseSet().getOptions()
                                .get(0);
                        String cvalue = "";
                        if (crobBean.getValue().startsWith("func: getexternalvalue")
                                || crobBean.getValue().startsWith("func: getExternalValue")) {
                            String itemName = getInputName(child);
                            cvalue = fp.getString(itemName);
                            LOGGER.debug(
                                    "just set " + fp.getString(itemName) + " for " + child.getItem().getName());

                        } else {
                            cvalue = sc.doCalculation(child, scoreItems, scoreItemdata, itemOrdinals, cerr, 1);
                        }
                        child.loadFormValue(cvalue);
                        if (isChanged(child.getData(), oldItemdata, child, attachedFilePath)) {
                            changedItems.add(child.getItem().getName());
                            changedItemsList.add(child);
                            // changedItemsMap.put(child.getItem().getName(),
                            // new DisplayItemGroupBean());
                        }
                        String cinputName = getInputName(child);
                        request.setAttribute(cinputName, cvalue);
                        if (validate) {
                            child = validateCalcTypeDisplayItemBean(sv, child, "", request);
                            if (cerr.length() > 0) {
                                Validation cvalidation = new Validation(Validator.CALCULATION_FAILED);
                                cvalidation.setErrorMessage(cerr.toString());
                                sv.addValidation(cinputName, cvalidation);
                            }
                        }
                    }
                    children.set(j, child);
                }
            }
        }

        logMe("allItems 3 Loop end  " + System.currentTimeMillis());
        // YW >>

        // we have to do this since we loaded all the form values into the
        // display
        // item beans above
        // section.setItems(items);
        // setting this AFTER we populate notes - will that make a difference?
        section.setDisplayItemGroups(allItems);

        // logger.debug("+++ try to populate notes");

        section = populateNotesWithDBNoteCounts(discNotes, section, request);
        populateInstantOnChange(request.getSession(), ecb, section);
        // logger.debug("+++ try to populate notes, got count of field notes: " + discNotes.getFieldNotes().toString());

        if (currentStudy.getStudyParameterConfig().getInterviewerNameRequired().equals("yes")) {
            v.addValidation(INPUT_INTERVIEWER, Validator.NO_BLANKS);
        }

        if (currentStudy.getStudyParameterConfig().getInterviewDateRequired().equals("yes")) {
            v.addValidation(INPUT_INTERVIEW_DATE, Validator.NO_BLANKS);
        }

        if (!StringUtil.isBlank(fp.getString(INPUT_INTERVIEW_DATE))) {
            v.addValidation(INPUT_INTERVIEW_DATE, Validator.IS_A_DATE);
            v.alwaysExecuteLastValidation(INPUT_INTERVIEW_DATE);
        }

        if (section.getSection().hasSCDItem()) {
            section = SCDItemDisplayInfo.generateSCDDisplayInfo(section,
                    this.getServletPage(request).equals(Page.INITIAL_DATA_ENTRY)
                            || this.getServletPage(request).equals(Page.ADMIN_EDIT_SERVLET)
                                    && !this.isAdminForcedReasonForChange(request));
        }

        // logger.debug("about to validate: " + v.getKeySet());
        errors = v.validate();

        // tbh >>
        if (this.isAdminForcedReasonForChange(request) && this.isAdministrativeEditing() && errors.isEmpty()) {
            // "You have changed data after this CRF was marked complete. "
            // +
            // "You must provide a Reason For Change discrepancy note for this item before you can save this updated information."
            String error = respage.getString("reason_for_change_error");
            // "Please enter a reason for change discrepancy note before saving."
            // ;
            int nonforcedChanges = 0;
            // change everything here from changed items list to changed
            // items map
            if (changedItemsList.size() > 0) {
                LOGGER.debug("found admin force reason for change: changed items " + changedItems.toString()
                        + " and changed items list: " + changedItemsList.toString() + " changed items map: "
                        + changedItemsMap.toString());
                logMe("DisplayItemBean  Loop begin  " + System.currentTimeMillis());
                for (DisplayItemBean displayItem : changedItemsList) {
                    String formName = getInputName(displayItem);

                    ItemDataBean idb = displayItem.getData();
                    ItemBean item_bean = displayItem.getItem();
                    ItemFormMetadataBean ifmb = displayItem.getMetadata();

                    LOGGER.debug("-- found group label " + ifmb.getGroupLabel());
                    if (!ifmb.getGroupLabel().equalsIgnoreCase("Ungrouped")
                            && !ifmb.getGroupLabel().equalsIgnoreCase(""))

                    {
                        // << tbh 11/2009 sometimes the group label is blank instead of ungrouped???
                        Iterator iter = changedItemsMap.entrySet().iterator();
                        while (iter.hasNext()) {
                            Map.Entry<String, DisplayItemGroupBean> pairs = (Map.Entry) iter.next();
                            String formName2 = pairs.getKey();
                            DisplayItemGroupBean dgb = pairs.getValue();
                            // logger.debug("found auto: " +
                            // dgb.isAuto());
                            String testFormName = "";
                            if (dgb.isAuto()) {
                                // testFormName = getGroupItemInputName(dgb, dgb.getFormInputOrdinal(), getManualRows(dgbs), displayItem);
                                testFormName = getGroupItemInputName(dgb, dgb.getFormInputOrdinal(),
                                        displayItem);
                            } else {
                                testFormName = getGroupItemManualInputName(dgb, dgb.getFormInputOrdinal(),
                                        displayItem);
                            }
                            LOGGER.debug("found test form name: " + testFormName);
                            // if our test is the same with both the display
                            // item and the display group ...
                            // logger.debug("comparing " +
                            // testFormName + " and " + formName2);
                            int existingNotes = dndao.findNumExistingNotesForItem(idb.getId());
                            if (testFormName.equals(formName2)) {
                                formName = formName2;
                                this.setReasonForChangeError(ecb, item_bean, idb, formName, error, request);
                                changedItemsMap.remove(formName2);
                                LOGGER.debug("form name changed: " + formName);
                                break;
                                // .., send it as the form name
                            }
                            // ... otherwise, don't touch it
                            // how to tell vs. manual and just plain input?
                        }

                    } else {
                        this.setReasonForChangeError(ecb, item_bean, idb, formName, error, request);
                        LOGGER.debug("form name added: " + formName);
                    }
                }
                logMe("DisplayItemBean  Loop end  " + System.currentTimeMillis());
            }
            if (nonforcedChanges > 0) {
                // do smething here?
            }
        }
        LOGGER.debug("errors here: " + errors.toString());
        // <<
        logMe("error check  Loop begin  " + System.currentTimeMillis());
        if (errors.isEmpty() && shouldRunRules) {
            LOGGER.debug("Errors was empty");
            if (session.getAttribute("rulesErrors") != null) {
                // rules have already generated errors, Let's compare old
                // error list with new
                // error list, if lists not same show errors.

                HashMap h = ruleValidator.validate();
                Set<String> a = (Set<String>) session.getAttribute("rulesErrors");
                Set<String> ba = h.keySet();
                Boolean showErrors = false;
                for (Object key : ba) {
                    if (!a.contains(key)) {
                        showErrors = true;
                    }
                }
                if (showErrors) {
                    errors = h;
                    if (errors.size() > 0) {
                        session.setAttribute("shouldRunValidation", "1");
                        session.setAttribute("rulesErrors", errors.keySet());
                    } else {
                        session.setAttribute("shouldRunValidation", null);
                        session.setAttribute("rulesErrors", null);
                    }
                } else {
                    session.setAttribute("shouldRunValidation", null);
                    session.setAttribute("rulesErrors", null);

                }

            } else if (session.getAttribute("shouldRunValidation") != null
                    && session.getAttribute("shouldRunValidation").toString().equals("1")) {
                session.setAttribute("shouldRunValidation", null);
                session.setAttribute("rulesErrors", null);
            } else {
                errors = ruleValidator.validate();
                if (errors.size() > 0) {
                    session.setAttribute("shouldRunValidation", "1");
                    session.setAttribute("rulesErrors", errors.keySet());
                }
            }
        }

        if (!errors.isEmpty()) {
            LOGGER.debug("threw an error with data entry...");
            // copying below three lines, tbh 03/2010
            String[] textFields = { INPUT_INTERVIEWER, INPUT_INTERVIEW_DATE };
            fp.setCurrentStringValuesAsPreset(textFields);
            setPresetValues(fp.getPresetValues(), request);
            // YW, 2-4-2008 <<
            logMe("!errors if  Loop begin  " + System.currentTimeMillis());
            HashMap<String, ArrayList<String>> siErrors = sv.validate();

            if (siErrors != null && !siErrors.isEmpty()) {
                Iterator iter = siErrors.keySet().iterator();
                while (iter.hasNext()) {
                    String fieldName = iter.next().toString();
                    errors.put(fieldName, siErrors.get(fieldName));
                }
            }
            // we should 'shift' the names here, tbh 02/2010
            // need: total number of rows, manual rows, all row names
            // plus: error names
            Iterator iter2 = errors.keySet().iterator();
            while (iter2.hasNext()) {
                String fieldName = iter2.next().toString();
                LOGGER.debug("found error " + fieldName);
            }
            //                for (int i = 0; i < allItems.size(); i++) {
            //                    DisplayItemWithGroupBean diwb = allItems.get(i);
            //
            //                    if (diwb.isInGroup()) {
            //                        List<DisplayItemGroupBean> dgbs = diwb.getItemGroups();
            //                        logger.debug("found manual rows " + getManualRows(dgbs) + " and total rows " + dgbs.size() + " from ordinal " + diwb.getOrdinal());
            //                    }
            //                }

            errors = reshuffleErrorGroupNamesKK(errors, allItems, request);
            reshuffleReasonForChangeHashAndDiscrepancyNotes(allItems, request, ecb);
            // reset manual rows, so that we can catch errors correctly
            // but it needs to be set per block of repeating items?  what if there are two or more?

            /*
            int manualRows = 0; // getManualRows(formGroups);
            for (int i = 0; i < allItems.size(); i++) {
            DisplayItemWithGroupBean diwb = allItems.get(i);
                    
            if (diwb.isInGroup()) {
                List<DisplayItemGroupBean> dgbs = diwb.getItemGroups();
                manualRows = getManualRows(dgbs);
            }
            }
            */
            //request.setAttribute("manualRows", new Integer(manualRows));
            Iterator iter3 = errors.keySet().iterator();
            while (iter3.hasNext()) {
                String fieldName = iter3.next().toString();
                LOGGER.debug("found error after shuffle " + fieldName);
            }
            //Mantis Issue: 8116. Parsist the markComplete chebox on error
            request.setAttribute("markComplete", fp.getString(INPUT_MARK_COMPLETE));
            // << tbh, 02/2010
            // YW >>
            // copied
            request.setAttribute(BEAN_DISPLAY, section);
            request.setAttribute(BEAN_ANNOTATIONS, fp.getString(INPUT_ANNOTATIONS));
            setInputMessages(errors, request);
            addPageMessage(respage.getString("errors_in_submission_see_below"), request);
            request.setAttribute("hasError", "true");
            // addPageMessage("To override these errors and keep the data as
            // you
            // entered it, click one of the \"Confirm\" buttons. ");
            // if (section.isCheckInputs()) {
            // addPageMessage("Please notice that you must enter data for
            // the
            // <b>required</b> entries.");
            // }
            // we do not save any DNs if we get here, so we have to set it back into session...
            session.setAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME, discNotes);
            // << tbh 01/2010
            setUpPanel(section);
            forwardPage(getJSPPage(), request, response);
        } else {
            //reshuffleReasonForChangeHashAndDiscrepancyNotes( allItems, request, ecb);
            LOGGER.debug("Do we hit this in save ?????");
            logMe("Do we hit this in save ????  " + System.currentTimeMillis());

            boolean success = true;
            boolean temp = true;

            // save interviewer name and date into DB
            ecb.setInterviewerName(fp.getString(INPUT_INTERVIEWER));
            if (!StringUtil.isBlank(fp.getString(INPUT_INTERVIEW_DATE))) {
                ecb.setDateInterviewed(fp.getDate(INPUT_INTERVIEW_DATE));
            } else {
                ecb.setDateInterviewed(null);
            }

            if (ecdao == null) {
                ecdao = new EventCRFDAO(getDataSource());
            }
            // set validator id for DDE
            DataEntryStage stage = ecb.getStage();
            if (stage.equals(DataEntryStage.INITIAL_DATA_ENTRY_COMPLETE)
                    || stage.equals(DataEntryStage.DOUBLE_DATA_ENTRY)) {
                ecb.setValidatorId(ub.getId());

            }
            /*
             * if(studyEventBean.getSubjectEventStatus().equals(SubjectEventStatus .SIGNED)){ if(edcBean.isDoubleEntry()){
             * ecb.setStage(DataEntryStage.DOUBLE_DATA_ENTRY_COMPLETE); }else{ ecb.setStage(DataEntryStage.INITIAL_DATA_ENTRY_COMPLETE); } }
             */

            // for Administrative editing
            if (studyEventBean.getSubjectEventStatus().equals(SubjectEventStatus.SIGNED)
                    && changedItemsList.size() > 0) {
                studyEventBean.setSubjectEventStatus(SubjectEventStatus.COMPLETED);
                studyEventBean.setUpdater(ub);
                studyEventBean.setUpdatedDate(new Date());
                seDao.update(studyEventBean);
            }

            // If the Study Subject's Satus is signed and we save a section
            // , change status to available
            LOGGER.debug("Status of Study Subject {}", ssb.getStatus().getName());
            if (ssb.getStatus() == Status.SIGNED && changedItemsList.size() > 0) {
                LOGGER.debug("Status of Study Subject is Signed we are updating");
                StudySubjectDAO studySubjectDao = new StudySubjectDAO(getDataSource());
                ssb.setStatus(Status.AVAILABLE);
                ssb.setUpdater(ub);
                ssb.setUpdatedDate(new Date());
                studySubjectDao.update(ssb);
            }
            if (ecb.isSdvStatus() && changedItemsList.size() > 0) {
                LOGGER.debug("Status of Study Subject is SDV we are updating");
                StudySubjectDAO studySubjectDao = new StudySubjectDAO(getDataSource());
                ssb.setStatus(Status.AVAILABLE);
                ssb.setUpdater(ub);
                ssb.setUpdatedDate(new Date());
                studySubjectDao.update(ssb);
                ecb.setSdvStatus(false);
                ecb.setSdvUpdateId(ub.getId());
            }

            ecb = (EventCRFBean) ecdao.update(ecb);

            // save discrepancy notes into DB
            FormDiscrepancyNotes fdn = (FormDiscrepancyNotes) session
                    .getAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME);
            dndao = new DiscrepancyNoteDAO(getDataSource());

            AddNewSubjectServlet.saveFieldNotes(INPUT_INTERVIEWER, fdn, dndao, ecb.getId(), "EventCRF",
                    currentStudy);
            AddNewSubjectServlet.saveFieldNotes(INPUT_INTERVIEW_DATE, fdn, dndao, ecb.getId(), "EventCRF",
                    currentStudy);

            // items = section.getItems();
            allItems = section.getDisplayItemGroups();
            int nextOrdinal = 0;

            LOGGER.debug("all items before saving into DB" + allItems.size());
            this.output(allItems);
            //TODO:Seems longer here, check this
            logMe("DisplayItemWithGroupBean allitems4 " + System.currentTimeMillis());
            for (int i = 0; i < allItems.size(); i++) {
                DisplayItemWithGroupBean diwb = allItems.get(i);

                // we don't write success = success && writeToDB here
                // since the short-circuit mechanism may prevent Java
                // from executing writeToDB.
                if (diwb.isInGroup()) {

                    List<DisplayItemGroupBean> dgbs = diwb.getItemGroups();
                    // using the above gets us the correct number of manual groups, tbh 01/2010
                    List<DisplayItemGroupBean> dbGroups = diwb.getDbItemGroups();
                    LOGGER.debug("item group size: " + dgbs.size());
                    LOGGER.debug("item db-group size: " + dbGroups.size());
                    for (int j = 0; j < dgbs.size(); j++) {
                        DisplayItemGroupBean displayGroup = dgbs.get(j);
                        List<DisplayItemBean> items = displayGroup.getItems();
                        // this ordinal will only useful to create a new
                        // item data
                        // update an item data won't touch its ordinal
                        //  int nextOrdinal = iddao.getMaxOrdinalForGroup(ecb, sb, displayGroup.getItemGroupBean()) + 1;

                        // Determine if any items in this group have data.  If so we need to undelete and previously deleted items.
                        boolean undelete = false;
                        for (DisplayItemBean displayItem : items) {
                            String currItemVal = displayItem.getData().getValue();
                            if (currItemVal != null && !currItemVal.equals("")) {
                                undelete = true;
                                break;
                            }
                        }

                        for (DisplayItemBean displayItem : items) {
                            String fileName = this.addAttachedFilePath(displayItem, attachedFilePath);
                            boolean writeDN = true;
                            displayItem.setEditFlag(displayGroup.getEditFlag());
                            LOGGER.debug("group item value: " + displayItem.getData().getValue());
                            //                if ("add".equalsIgnoreCase(displayItem.getEditFlag()) && fileName.length() > 0 && !newUploadedFiles.containsKey(fileName)) {
                            //                    displayItem.getData().setValue("");
                            //               }

                            //15350, this particular logic, takes into consideration that a DN is created properly as long as the item data record exists and it fails to get created when it doesnt.
                            //so, we are expanding the logic from writeToDb method to avoid creating duplicate records.
                            writeDN = writeDN(displayItem);
                            //pulling from dataset instead of database and correcting the flawed logic of using the database ordinals as max ordinal...
                            nextOrdinal = displayItem.getData().getOrdinal();

                            temp = writeToDB(displayItem, iddao, nextOrdinal, request);
                            LOGGER.debug("just executed writeToDB - 1");
                            LOGGER.debug("next ordinal: " + nextOrdinal);

                            // Undelete item if any item in the repeating group has data.
                            if (undelete && displayItem.getDbData() != null
                                    && displayItem.getDbData().isDeleted()) {
                                iddao.undelete(displayItem.getDbData().getId(), ub.getId());
                            }

                            if (temp && newUploadedFiles.containsKey(fileName)) {
                                newUploadedFiles.remove(fileName);
                            }
                            // maybe put ordinal in the place of j? maybe subtract max rows from next ordinal if j is gt
                            // next ordinal?
                            String inputName = getGroupItemInputName(displayGroup, j, displayItem);
                            // String inputName2 = getGroupItemManualInputName(displayGroup, j, displayItem);
                            if (!displayGroup.isAuto()) {
                                LOGGER.trace("not auto");
                                inputName = this.getGroupItemManualInputName(displayGroup, j, displayItem);

                            }
                            //htaycher last DN is not stored for new rows
                            //                                if (j == dgbs.size() - 1) {
                            //                                    // LAST ONE
                            //                                    logger.trace("last one");
                            //                                    int ordinal = j - this.getManualRows(dgbs);
                            //                                    logger.debug("+++ found manual rows from line 1326: " + ordinal);
                            //                                    inputName = getGroupItemInputName(displayGroup, ordinal, displayItem);
                            //                                }
                            // logger.trace("&&& we get previous looking at input name: " + inputName + " " + inputName2);
                            LOGGER.trace("&&& we get previous looking at input name: " + inputName);
                            // input name 2 removed from below
                            inputName = displayItem.getFieldName();
                            if (writeDN) {
                                AddNewSubjectServlet.saveFieldNotes(inputName, fdn, dndao,
                                        displayItem.getData().getId(), "itemData", currentStudy, ecb.getId());
                            }
                            success = success && temp;
                        }
                    }
                    for (int j = 0; j < dbGroups.size(); j++) {
                        DisplayItemGroupBean displayGroup = dbGroups.get(j);
                        //JN: Since remove button is gone, the following code can be commented out, however it needs to be tested? Can be tackled when handling discrepancy note w/repeating groups issues.
                        if ("remove".equalsIgnoreCase(displayGroup.getEditFlag())) {
                            List<DisplayItemBean> items = displayGroup.getItems();
                            for (DisplayItemBean displayItem : items) {
                                String fileName = this.addAttachedFilePath(displayItem, attachedFilePath);
                                displayItem.setEditFlag(displayGroup.getEditFlag());
                                LOGGER.debug("group item value: " + displayItem.getData().getValue());
                                //               if ("add".equalsIgnoreCase(displayItem.getEditFlag()) && fileName.length() > 0 && !newUploadedFiles.containsKey(fileName)) {
                                //                   displayItem.getData().setValue("");
                                //               }
                                temp = writeToDB(displayItem, iddao, 0, request);
                                LOGGER.debug("just executed writeToDB - 2");
                                if (temp && newUploadedFiles.containsKey(fileName)) {
                                    newUploadedFiles.remove(fileName);
                                }
                                // just use 0 here since update doesn't
                                // touch ordinal
                                success = success && temp;
                            }
                        }
                    }

                } else {
                    DisplayItemBean dib = diwb.getSingleItem();
                    // TODO work on this line

                    //  this.addAttachedFilePath(dib, attachedFilePath);
                    String fileName = addAttachedFilePath(dib, attachedFilePath);
                    boolean writeDN = writeDN(dib);
                    temp = writeToDB(dib, iddao, 1, request);
                    LOGGER.debug("just executed writeToDB - 3");
                    if (temp && (newUploadedFiles.containsKey(dib.getItem().getId() + "")
                            || newUploadedFiles.containsKey(fileName))) {
                        // so newUploadedFiles will contain only failed file
                        // items;
                        newUploadedFiles.remove(dib.getItem().getId() + "");
                        newUploadedFiles.remove(fileName);
                    }

                    String inputName = getInputName(dib);
                    LOGGER.trace("3 - found input name: " + inputName);
                    if (writeDN)
                        AddNewSubjectServlet.saveFieldNotes(inputName, fdn, dndao, dib.getData().getId(),
                                "itemData", currentStudy, ecb.getId());

                    success = success && temp;

                    ArrayList childItems = dib.getChildren();
                    for (int j = 0; j < childItems.size(); j++) {
                        DisplayItemBean child = (DisplayItemBean) childItems.get(j);
                        this.addAttachedFilePath(child, attachedFilePath);
                        writeDN = writeDN(child);
                        temp = writeToDB(child, iddao, 1, request);
                        LOGGER.debug("just executed writeToDB - 4");
                        if (temp && newUploadedFiles.containsKey(child.getItem().getId() + "")) {
                            // so newUploadedFiles will contain only failed
                            // file items;
                            newUploadedFiles.remove(child.getItem().getId() + "");
                        }
                        inputName = getInputName(child);
                        if (writeDN)
                            AddNewSubjectServlet.saveFieldNotes(inputName, fdn, dndao, child.getData().getId(),
                                    "itemData", currentStudy, ecb.getId());
                        success = success && temp;
                    }
                }
            }
            logMe("DisplayItemWithGroupBean allitems4 end " + System.currentTimeMillis());
            LOGGER.debug("running rules: " + phase2.name());
            List<Integer> prevShownDynItemDataIds = shouldRunRules
                    ? this.getItemMetadataService().getDynamicsItemFormMetadataDao()
                            .findShowItemDataIdsInSection(section.getSection().getId(), ecb.getCRFVersionId(),
                                    ecb.getId())
                    : new ArrayList<Integer>();
            logMe("DisplayItemWithGroupBean dryrun  start" + System.currentTimeMillis());
            HashMap<String, ArrayList<String>> rulesPostDryRun = runRules(allItems, ruleSets, false,
                    shouldRunRules, MessageType.WARNING, phase2, ecb, request);

            HashMap<String, ArrayList<String>> errorsPostDryRun = new HashMap<String, ArrayList<String>>();
            // additional step needed, run rules and see if any items are 'shown' AFTER saving data
            logMe("DisplayItemWithGroupBean dryrun  end" + System.currentTimeMillis());
            boolean inSameSection = false;
            logMe("DisplayItemWithGroupBean allitems4 " + System.currentTimeMillis());
            if (!rulesPostDryRun.isEmpty()) {
                // in same section?

                // iterate through the OIDs and see if any of them belong to this section
                Iterator iter3 = rulesPostDryRun.keySet().iterator();
                while (iter3.hasNext()) {
                    String fieldName = iter3.next().toString();
                    LOGGER.debug("found oid after post dry run " + fieldName);
                    // set up a listing of OIDs in the section
                    // BUT: Oids can have the group name in them.
                    int ordinal = -1;
                    String newFieldName = fieldName;
                    String[] fieldNames = fieldName.split("\\.");
                    if (fieldNames.length == 2) {
                        newFieldName = fieldNames[1];
                        // check items in item groups here?
                        if (fieldNames[0].contains("[")) {
                            int p1 = fieldNames[0].indexOf("[");
                            int p2 = fieldNames[0].indexOf("]");
                            try {
                                ordinal = Integer.valueOf(fieldNames[0].substring(p1 + 1, p2));
                            } catch (NumberFormatException e) {
                                ordinal = -1;
                            }
                            fieldNames[0] = fieldNames[0].substring(0, p1);
                        }
                    }
                    List<DisplayItemWithGroupBean> displayGroupsWithItems = section.getDisplayItemGroups();
                    //ArrayList<DisplayItemBean> displayItems = section.getItems();
                    for (int i = 0; i < displayGroupsWithItems.size(); i++) {
                        DisplayItemWithGroupBean itemWithGroup = displayGroupsWithItems.get(i);
                        if (itemWithGroup.isInGroup()) {
                            LOGGER.debug("found group: " + fieldNames[0]);
                            // do something there
                            List<DisplayItemGroupBean> digbs = itemWithGroup.getItemGroups();
                            LOGGER.debug("digbs size: " + digbs.size());
                            for (int j = 0; j < digbs.size(); j++) {
                                DisplayItemGroupBean displayGroup = digbs.get(j);
                                if (displayGroup.getItemGroupBean().getOid().equals(fieldNames[0])
                                        && displayGroup.getOrdinal() == ordinal - 1) {
                                    List<DisplayItemBean> items = displayGroup.getItems();

                                    for (int k = 0; k < items.size(); k++) {
                                        DisplayItemBean dib = items.get(k);
                                        if (dib.getItem().getOid().equals(newFieldName)) {
                                            //inSameSection = true;
                                            if (!dib.getMetadata().isShowItem()) {
                                                LOGGER.debug("found item in group "
                                                        + this.getGroupItemInputName(displayGroup, j, dib)
                                                        + " vs. " + fieldName + " and is show item: "
                                                        + dib.getMetadata().isShowItem());
                                                dib.getMetadata().setShowItem(true);
                                            }
                                            if (prevShownDynItemDataIds == null || !prevShownDynItemDataIds
                                                    .contains(dib.getData().getId())) {
                                                inSameSection = true;
                                                errorsPostDryRun.put(
                                                        this.getGroupItemInputName(displayGroup, j, dib),
                                                        rulesPostDryRun.get(fieldName));
                                            }
                                        }
                                        items.set(k, dib);
                                    }
                                    displayGroup.setItems(items);
                                    digbs.set(j, displayGroup);
                                }
                            }
                            itemWithGroup.setItemGroups(digbs);
                        } else {
                            DisplayItemBean displayItemBean = itemWithGroup.getSingleItem();
                            ItemBean itemBean = displayItemBean.getItem();
                            if (newFieldName.equals(itemBean.getOid())) {
                                //System.out.println("is show item for " + displayItemBean.getItem().getId() + ": " + displayItemBean.getMetadata().isShowItem());
                                //System.out.println("check run dynamics item check " + runDynamicsItemCheck(displayItemBean).getMetadata().isShowItem());
                                if (!displayItemBean.getMetadata().isShowItem()) {
                                    // double check there?
                                    LOGGER.debug("found item " + this.getInputName(displayItemBean) + " vs. "
                                            + fieldName + " and is show item: "
                                            + displayItemBean.getMetadata().isShowItem());
                                    // if is repeating, use the other input name? no

                                    displayItemBean.getMetadata().setShowItem(true);
                                    if (prevShownDynItemDataIds == null || !prevShownDynItemDataIds
                                            .contains(displayItemBean.getData().getId())) {
                                        inSameSection = true;
                                        errorsPostDryRun.put(this.getInputName(displayItemBean),
                                                rulesPostDryRun.get(fieldName));
                                    }
                                }
                            }
                            itemWithGroup.setSingleItem(displayItemBean);
                        }
                        displayGroupsWithItems.set(i, itemWithGroup);
                    }
                    logMe("DisplayItemWithGroupBean allitems4  end,begin" + System.currentTimeMillis());
                    // check groups
                    //List<DisplayItemGroupBean> itemGroups = new ArrayList<DisplayItemGroupBean>();
                    //itemGroups = section.getDisplayFormGroups();
                    //   But in jsp: section.displayItemGroups.itemGroup.groupMetaBean.showGroup
                    List<DisplayItemWithGroupBean> itemGroups = section.getDisplayItemGroups();
                    // List<DisplayItemGroupBean> newItemGroups = new ArrayList<DisplayItemGroupBean>();
                    for (DisplayItemWithGroupBean itemGroup : itemGroups) {
                        DisplayItemGroupBean displayGroup = itemGroup.getItemGroup();
                        if (newFieldName.equals(displayGroup.getItemGroupBean().getOid())) {
                            if (!displayGroup.getGroupMetaBean().isShowGroup()) {
                                inSameSection = true;
                                LOGGER.debug("found itemgroup " + displayGroup.getItemGroupBean().getOid()
                                        + " vs. " + fieldName + " and is show item: "
                                        + displayGroup.getGroupMetaBean().isShowGroup());
                                // hmmm how to set highlighting for a group?
                                errorsPostDryRun.put(displayGroup.getItemGroupBean().getOid(),
                                        rulesPostDryRun.get(fieldName));
                                displayGroup.getGroupMetaBean().setShowGroup(true);
                                // add necessary rows to the display group here????
                                // we have to set the items in the itemGroup for the displayGroup
                                loadItemsWithGroupRows(itemGroup, sb, edcb, ecb, request);

                            }
                        }
                        // newItemGroups.add(displayGroup);
                    }
                    logMe("DisplayItemWithGroupBean allitems4  end,end" + System.currentTimeMillis());
                    // trying to reset the display form groups here, tbh

                    // section.setItems(displayItems);
                    section.setDisplayItemGroups(displayGroupsWithItems);
                    populateInstantOnChange(request.getSession(), ecb, section);

                    // section.setDisplayFormGroups(newDisplayBean.getDisplayFormGroups());

                }
                //
                this.getItemMetadataService().updateGroupDynamicsInSection(displayItemWithGroups,
                        section.getSection().getId(), ecb);
                toc = TableOfContentsServlet.getDisplayBeanWithShownSections(getDataSource(),
                        (DisplayTableOfContentsBean) request.getAttribute(TOC_DISPLAY),
                        (DynamicsMetadataService) SpringServletAccess.getApplicationContext(getServletContext())
                                .getBean("dynamicsMetadataService"));
                request.setAttribute(TOC_DISPLAY, toc);
                sectionIdsInToc = TableOfContentsServlet.sectionIdsInToc(toc);
                sIndex = TableOfContentsServlet.sectionIndexInToc(section.getSection(), toc, sectionIdsInToc);
                previousSec = this.prevSection(section.getSection(), ecb, toc, sIndex);
                nextSec = this.nextSection(section.getSection(), ecb, toc, sIndex);
                section.setFirstSection(!previousSec.isActive());
                section.setLastSection(!nextSec.isActive());
                //
                // we need the following for repeating groups, tbh
                // >> tbh 06/2010
                // List<DisplayItemWithGroupBean> displayItemWithGroups2 = createItemWithGroups(section, hasGroup, eventDefinitionCRFId);

                // section.setDisplayItemGroups(displayItemWithGroups2);

                // if so, stay at this section
                LOGGER.debug(" in same section: " + inSameSection);
                if (inSameSection) {
                    // copy of one line from early on around line 400, forcing a re-show of the items
                    // section = getDisplayBean(hasGroup, true);// include all items, tbh
                    // below a copy of three lines from the if errors = true line, tbh 03/2010
                    String[] textFields = { INPUT_INTERVIEWER, INPUT_INTERVIEW_DATE };
                    fp.setCurrentStringValuesAsPreset(textFields);
                    setPresetValues(fp.getPresetValues(), request);
                    // below essetially a copy except for rulesPostDryRun
                    request.setAttribute(BEAN_DISPLAY, section);
                    request.setAttribute(BEAN_ANNOTATIONS, fp.getString(INPUT_ANNOTATIONS));
                    setInputMessages(errorsPostDryRun, request);
                    addPageMessage(respage.getString("your_answers_activated_hidden_items"), request);
                    request.setAttribute("hasError", "true");
                    request.setAttribute("hasShown", "true");

                    session.setAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME, discNotes);
                    setUpPanel(section);
                    forwardPage(getJSPPage(), request, response);
                }
            }

            if (!inSameSection) {// else if not in same section, progress as usual
                /*
                toc =
                TableOfContentsServlet.getDisplayBeanWithShownSections(getDataSource(), (DisplayTableOfContentsBean) request.getAttribute(TOC_DISPLAY),
                        (DynamicsMetadataService) SpringServletAccess.getApplicationContext(getServletContext()).getBean("dynamicsMetadataService"));
                request.setAttribute(TOC_DISPLAY, toc);
                sectionIdsInToc = TableOfContentsServlet.sectionIdsInToc(toc);
                sIndex = TableOfContentsServlet.sectionIndexInToc(section.getSection(), toc, sectionIdsInToc);
                previousSec = this.prevSection(section.getSection(), ecb, toc, sIndex);
                nextSec = this.nextSection(section.getSection(), ecb, toc, sIndex);
                section.setFirstSection(!previousSec.isActive());
                section.setLastSection(!nextSec.isActive());
                */
                // can we just forward page or do we actually need an ELSE here?
                // yes, we do. tbh 05/03/2010

                ArrayList<String> updateFailedItems = sc.redoCalculations(scoreItems, scoreItemdata,
                        changedItems, itemOrdinals, sb.getId());
                success = updateFailedItems.size() > 0 ? false : true;

                // now check if CRF is marked complete
                boolean markComplete = fp.getString(INPUT_MARK_COMPLETE).equals(VALUE_YES);
                boolean markSuccessfully = false; // if the CRF was marked
                // complete
                // successfully
                if (markComplete && section.isLastSection()) {
                    LOGGER.debug("need to mark CRF as complete");
                    markSuccessfully = markCRFComplete(request);
                    LOGGER.debug("...marked CRF as complete: " + markSuccessfully);
                    if (!markSuccessfully) {
                        request.setAttribute(BEAN_DISPLAY, section);
                        request.setAttribute(BEAN_ANNOTATIONS, fp.getString(INPUT_ANNOTATIONS));
                        setUpPanel(section);
                        forwardPage(getJSPPage(), request, response);
                        return;
                    }
                }

                // now write the event crf bean to the database
                String annotations = fp.getString(INPUT_ANNOTATIONS);
                setEventCRFAnnotations(annotations, request);
                Date now = new Date();
                ecb.setUpdatedDate(now);
                ecb.setUpdater(ub);
                ecb = (EventCRFBean) ecdao.update(ecb);
                success = success && ecb.isActive();

                StudyEventDAO sedao = new StudyEventDAO(getDataSource());
                StudyEventBean seb = (StudyEventBean) sedao.findByPK(ecb.getStudyEventId());
                seb.setUpdatedDate(now);
                seb.setUpdater(ub);
                seb = (StudyEventBean) sedao.update(seb);
                success = success && seb.isActive();

                request.setAttribute(INPUT_IGNORE_PARAMETERS, Boolean.TRUE);

                if (newUploadedFiles.size() > 0) {
                    if (this.unloadFiles(newUploadedFiles)) {

                    } else {
                        String missed = "";
                        Iterator iter = newUploadedFiles.keySet().iterator();
                        while (iter.hasNext()) {
                            missed += " " + newUploadedFiles.get(iter.next());
                        }
                        addPageMessage(
                                respage.getString("uploaded_files_not_deleted_or_not_exist") + ": " + missed,
                                request);
                    }
                }
                if (!success) {
                    // YW, 3-6-2008 <<
                    if (updateFailedItems.size() > 0) {
                        String mess = "";
                        for (String ss : updateFailedItems) {
                            mess += ss + ", ";
                        }
                        mess = mess.substring(0, mess.length() - 2);
                        addPageMessage(resexception.getString("item_save_failed_because_database_error") + mess,
                                request);
                    } else {
                        // YW>>
                        addPageMessage(resexception.getString("database_error"), request);
                    }
                    request.setAttribute(BEAN_DISPLAY, section);
                    session.removeAttribute(GROUP_HAS_DATA);
                    session.removeAttribute(HAS_DATA_FLAG);
                    session.removeAttribute(DDE_PROGESS);
                    session.removeAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME);
                    LOGGER.debug("try to remove to_create_crf");
                    session.removeAttribute("to_create_crf");
                    session.removeAttribute(instantAtt);

                    // forwardPage(Page.SUBMIT_DATA_SERVLET);
                    forwardPage(Page.LIST_STUDY_SUBJECTS_SERVLET, request, response);
                    // >> changed tbh, 06/2009
                } else {
                    boolean forwardingSucceeded = false;

                    if (!fp.getString(GO_PREVIOUS).equals("")) {
                        if (previousSec.isActive()) {
                            forwardingSucceeded = true;
                            request.setAttribute(INPUT_EVENT_CRF, ecb);
                            request.setAttribute(INPUT_SECTION, previousSec);
                            int tabNum = 0;
                            if (fp.getString("tab") == null) {
                                tabNum = 1;
                            } else {
                                tabNum = fp.getInt("tab");
                            }
                            request.setAttribute("tab", new Integer(tabNum - 1).toString());

                            //  forwardPage(getServletPage(request), request, response);
                            getServletContext().getRequestDispatcher(getServletPage(request)).forward(request,
                                    response);
                        }
                    } else if (!fp.getString(GO_NEXT).equals("")) {
                        if (nextSec.isActive()) {
                            forwardingSucceeded = true;
                            request.setAttribute(INPUT_EVENT_CRF, ecb);
                            request.setAttribute(INPUT_SECTION, nextSec);
                            int tabNum = 0;
                            if (fp.getString("tab") == null) {
                                tabNum = 1;
                            } else {
                                tabNum = fp.getInt("tab");
                            }
                            request.setAttribute("tab", new Integer(tabNum + 1).toString());
                            getServletContext().getRequestDispatcher(getServletPage(request)).forward(request,
                                    response);
                            //forwardPage(getServletPage(request), request, response);
                        }
                    }

                    if (!forwardingSucceeded) {
                        // request.setAttribute(TableOfContentsServlet.
                        // INPUT_EVENT_CRF_BEAN,
                        // ecb);
                        if (markSuccessfully) {
                            addPageMessage(respage.getString("data_saved_CRF_marked_complete"), request);
                            session.removeAttribute(AddNewSubjectServlet.FORM_DISCREPANCY_NOTES_NAME);
                            session.removeAttribute(GROUP_HAS_DATA);
                            session.removeAttribute(HAS_DATA_FLAG);
                            session.removeAttribute(DDE_PROGESS);
                            session.removeAttribute("to_create_crf");

                            request.setAttribute("eventId", new Integer(ecb.getStudyEventId()).toString());
                            forwardPage(Page.ENTER_DATA_FOR_STUDY_EVENT_SERVLET, request, response);
                        } else {
                            // use clicked 'save'
                            addPageMessage(respage.getString("data_saved_continue_entering_edit_later"),
                                    request);
                            request.setAttribute(INPUT_EVENT_CRF, ecb);
                            request.setAttribute(INPUT_EVENT_CRF_ID, new Integer(ecb.getId()).toString());
                            // forward to the next section if the previous one
                            // is not the last section
                            if (!section.isLastSection()) {
                                request.setAttribute(INPUT_SECTION, nextSec);
                                request.setAttribute(INPUT_SECTION_ID, new Integer(nextSec.getId()).toString());
                                session.removeAttribute("mayProcessUploading");
                            } else if (section.isLastSection()) { //JN ADDED TO avoid return down
                                // already the last section, should go back to
                                // view event page
                                session.removeAttribute(GROUP_HAS_DATA);
                                session.removeAttribute(HAS_DATA_FLAG);
                                session.removeAttribute(DDE_PROGESS);
                                session.removeAttribute("to_create_crf");
                                session.removeAttribute("mayProcessUploading");

                                request.setAttribute("eventId", new Integer(ecb.getStudyEventId()).toString());
                                if (fromViewNotes != null && "1".equals(fromViewNotes)) {
                                    String viewNotesPageFileName = (String) session
                                            .getAttribute("viewNotesPageFileName");
                                    session.removeAttribute("viewNotesPageFileName");
                                    session.removeAttribute("viewNotesURL");
                                    if (viewNotesPageFileName != null && viewNotesPageFileName.length() > 0) {
                                        // forwardPage(Page.setNewPage(viewNotesPageFileName, "View Notes"), request, response);
                                        getServletContext().getRequestDispatcher(viewNotesPageFileName)
                                                .forward(request, response);
                                    }
                                }
                                session.removeAttribute(instantAtt);
                                forwardPage(Page.ENTER_DATA_FOR_STUDY_EVENT_SERVLET, request, response);
                                return;

                            }

                            int tabNum = 0;
                            if (fp.getString("tab") == null) {
                                tabNum = 1;
                            } else {
                                tabNum = fp.getInt("tab");
                            }
                            if (!section.isLastSection()) {
                                request.setAttribute("tab", new Integer(tabNum + 1).toString());
                            }

                            //  forwardPage(getServletPage(request), request, response);
                            getServletContext().getRequestDispatcher(getServletPage(request)).forward(request,
                                    response);
                        }
                        // session.removeAttribute(AddNewSubjectServlet.
                        // FORM_DISCREPANCY_NOTES_NAME);
                        // forwardPage(Page.SUBMIT_DATA_SERVLET);
                    }
                }
            } // end of if-block for dynamic rules not in same section, tbh 05/2010
        } // end of save
    }

}

From source file:com.clustercontrol.jobmanagement.factory.SelectJob.java

/**
 * ???????/*  w w  w.ja  v a 2  s .com*/
 *
 * @param sessionId ID
 * @param jobId ID
 * @param jobunitId ID
 * @param destFacilityId ?ID
 * @param destFacilityName ???
 * @param checksum ?
 * @param locale 
 * @return 
 * @throws JobInfoNotFound
 */
private String getFileJobDetailMessage(String sessionId, String jobunitId, String jobId, String destFacilityId,
        String destFacilityName, boolean checksum, Locale locale) throws JobInfoNotFound {

    final String START = "_START";
    final String END = "_END";
    final String FILE = "_FILE";
    final String RTN = "\n";

    StringBuilder message = new StringBuilder();

    if (sessionId == null || sessionId.length() == 0 || jobId == null || jobId.length() == 0) {
        return message.toString();
    }

    // UTILIUPDT_S??
    if (CreateHulftJob.isHulftMode()) {
        try {
            JobSessionJobEntity job = QueryUtil.getJobSessionJobPK(sessionId, jobunitId, jobId);
            JobSessionJobEntity jobUtliupdtS = QueryUtil.getJobSessionJobPK(sessionId, jobunitId,
                    job.getParentJobId() + CreateHulftJob.UTILIUPDT_S);
            if (jobUtliupdtS.getEndDate() != null) {
                if (jobUtliupdtS.getEndStatus() == null
                        || jobUtliupdtS.getEndStatus() != EndStatusConstant.TYPE_NORMAL) {
                    for (JobSessionNodeEntity node : jobUtliupdtS.getJobSessionNodeEntities()) {
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                }
            }
        } catch (JobInfoNotFound e) {
            m_log.debug("getFileJobDetailMessage() : " + e.getMessage());
        } catch (InvalidRole e) {
            m_log.info("getFileJobDetailMessage() : " + e.getMessage());
        }
    }

    //ID?
    Collection<JobSessionJobEntity> collection = QueryUtil.getChildJobSessionJobOrderByStartDate(sessionId,
            jobunitId, jobId);
    if (collection == null) {
        JobInfoNotFound je = new JobInfoNotFound("JobSessionJobEntity.findByStartDate"
                + ", [sessionId, parentJobId] = " + "[" + sessionId + ", " + jobId + "]");
        m_log.info("getFileJobDetailMessage() : " + je.getClass().getSimpleName() + ", " + je.getMessage());
        je.setSessionId(sessionId);
        je.setParentJobId(jobId);
        throw je;
    }

    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale);
    dateFormat.setTimeZone(HinemosTime.getTimeZone());
    HashMap<String, String> jobIdMap = new HashMap<String, String>();

    for (JobSessionJobEntity childSessionJob : collection) {
        JobInfoEntity childJob = childSessionJob.getJobInfoEntity();
        String childJobId = childSessionJob.getId().getJobId();

        if (childJobId.indexOf(CreateFileJob.GET_KEY) != -1) {
            //"_GET_CHECKSUM"??ID?
            String fileJobId = childJobId.replaceAll(CreateFileJob.GET_KEY, "");
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (childSessionJob.getEndStatus() == null
                            || childSessionJob.getEndStatus() != EndStatusConstant.TYPE_NORMAL)) {
                for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                    String nodeMessage = node.getMessage();
                    if (nodeMessage != null && nodeMessage.length() > 0) {
                        message.append(nodeMessage);
                        message.append(RTN);
                    }
                }
                jobIdMap.put(fileJobId, END);
            }

        } else if (childJobId.indexOf(CreateFileJob.ADD_KEY) != -1) {
            //"_GET_CHECKSUM"??ID?
            String fileJobId = childJobId.replaceAll(CreateFileJob.ADD_KEY, "");
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                if (childSessionJob.getEndStatus() == null
                        || childSessionJob.getEndStatus() != EndStatusConstant.TYPE_NORMAL) {
                    for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                    jobIdMap.put(fileJobId, END);
                }
            }

        } else if (childJobId.indexOf(CreateFileJob.GET_CHECKSUM) != -1) {
            //"_GET_CHECKSUM"??ID?
            String fileJobId = childJobId.replaceAll(CreateFileJob.GET_CHECKSUM, "");
            jobIdMap.put(fileJobId + FILE, childJob.getArgument());
            //????
            if (childSessionJob.getStartDate() != null) {
                String dateString = dateFormat.format(childSessionJob.getStartDate());
                String file = childJob.getArgument();
                String[] args = { dateString, file, destFacilityName };
                message.append(MessageConstant.MESSAGE_STARTED_TO_TRANSFER_FILE.getMessage(args));
                message.append(RTN);
                jobIdMap.put(fileJobId, START);
            }
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                if (childSessionJob.getEndStatus() == null
                        || childSessionJob.getEndStatus() != EndStatusConstant.TYPE_NORMAL) {
                    String dateString = dateFormat.format(childSessionJob.getEndDate());
                    String file = childJob.getArgument();
                    String[] args = { dateString, file, destFacilityName };
                    message.append(MessageConstant.MESSAGE_FAILED_TO_TRANSFER_FILE.getMessage(args));
                    message.append(RTN);
                    for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                    jobIdMap.put(fileJobId, END);
                }
            }

        } else if (childJobId.indexOf(CreateFileJob.FORWARD) != -1) {
            //"_FORWARD"??ID?
            String fileJobId = childJobId.replaceAll(CreateFileJob.FORWARD, "");
            //????
            if (childSessionJob.getStartDate() != null && !checksum) {
                String dateString = dateFormat.format(childSessionJob.getStartDate());
                String file = childJob.getArgument();
                String[] args = { dateString, file, destFacilityName };
                message.append(MessageConstant.MESSAGE_STARTED_TO_TRANSFER_FILE.getMessage(args));
                message.append(RTN);
                jobIdMap.put(fileJobId, START);
            }
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                String dateString = dateFormat.format(childSessionJob.getEndDate());
                String file = childJob.getArgument();
                String[] args = { dateString, file, destFacilityName };
                if (childSessionJob.getEndStatus() != null
                        && childSessionJob.getEndStatus() == EndStatusConstant.TYPE_NORMAL) {
                    if (!checksum) {
                        message.append(MessageConstant.MESSAGE_FINISHED_TRANSFERRING_FILE.getMessage(args));
                        message.append(RTN);
                        jobIdMap.put(fileJobId, END);
                    }
                } else {
                    message.append(MessageConstant.MESSAGE_FAILED_TO_TRANSFER_FILE.getMessage(args));
                    message.append(RTN);
                    for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                        m_log.debug(node.getId().getJobId());
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                    jobIdMap.put(fileJobId, END);
                }
            }
            if (!checksum) {
                jobIdMap.remove(fileJobId);
                jobIdMap.remove(fileJobId + FILE);
            }

        } else if (childJobId.indexOf(CreateFileJob.CHECK_CHECKSUM) != -1) {
            //"_CHECK_CHECKSUM"??ID?
            String fileJobId = childJobId.replaceAll(CreateFileJob.CHECK_CHECKSUM, "");
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                String dateString = dateFormat.format(childSessionJob.getEndDate());
                String file = jobIdMap.get(fileJobId + FILE);
                String[] args = { dateString, file, destFacilityName };
                if (childSessionJob.getEndStatus() != null
                        && childSessionJob.getEndStatus() == EndStatusConstant.TYPE_NORMAL) {
                    message.append(MessageConstant.MESSAGE_FINISHED_TRANSFERRING_FILE.getMessage(args));
                    message.append(RTN);
                } else {
                    message.append(MessageConstant.MESSAGE_FAILED_TO_TRANSFER_FILE.getMessage(args));
                    message.append(RTN);
                    for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                        m_log.debug(node.getId().getJobId());
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                    jobIdMap.put(fileJobId, END);
                }
            }
            jobIdMap.remove(fileJobId);
            jobIdMap.remove(fileJobId + FILE);

        } else if (childJobId.indexOf(CreateFileJob.DEL_KEY) != -1) {
            //"_DEL_KEY"??ID?
            String fileJobId = childJobId.replaceAll(CreateFileJob.DEL_KEY, "");
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                if (childSessionJob.getEndStatus() == null
                        || childSessionJob.getEndStatus() != EndStatusConstant.TYPE_NORMAL) {
                    for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                    jobIdMap.put(fileJobId, END);
                }
            }

        } else if (childJobId.indexOf(CreateHulftJob.UTILIUPDT_R) != -1) {
            //"_UTILIUPDT_R"??ID?
            String fileJobId = childJobId.replaceAll(CreateHulftJob.UTILIUPDT_R, "");
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                if (childSessionJob.getEndStatus() == null
                        || childSessionJob.getEndStatus() != EndStatusConstant.TYPE_NORMAL) {
                    for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                    jobIdMap.put(fileJobId, END);
                }
            }

        } else if (childJobId.indexOf(CreateHulftJob.UTILIUPDT_H_SND) != -1) {
            //"_UTLSEND"??ID?
            String fileJobId = childJobId.replaceAll(CreateHulftJob.UTILIUPDT_H_SND, "");
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                if (childSessionJob.getEndStatus() == null
                        || childSessionJob.getEndStatus() != EndStatusConstant.TYPE_NORMAL) {
                    for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                    jobIdMap.put(fileJobId, END);
                }
            }

        } else if (childJobId.indexOf(CreateHulftJob.UTILIUPDT_H_RCV) != -1) {
            //"_UTLSEND"??ID?
            String fileJobId = childJobId.replaceAll(CreateHulftJob.UTILIUPDT_H_RCV, "");
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                if (childSessionJob.getEndStatus() == null
                        || childSessionJob.getEndStatus() != EndStatusConstant.TYPE_NORMAL) {
                    for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                    jobIdMap.put(fileJobId, END);
                }
            }

        } else if (childJobId.indexOf(CreateHulftJob.UTLSEND) != -1) {
            //"_UTLSEND"??ID?
            String fileJobId = childJobId.replaceAll(CreateHulftJob.UTLSEND, "");
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                if (childSessionJob.getEndStatus() == null
                        || childSessionJob.getEndStatus() != EndStatusConstant.TYPE_NORMAL) {
                    for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                        String nodeMessage = node.getMessage();
                        if (nodeMessage != null && nodeMessage.length() > 0) {
                            message.append(nodeMessage);
                            message.append(RTN);
                        }
                    }
                    jobIdMap.put(fileJobId, END);
                }
            }

        } else if (childJobId.indexOf(CreateHulftJob.HULOPLCMD) != -1) {
            //"_HULOPLCMD"??ID?
            String fileJobId = childJobId.replaceAll(CreateHulftJob.HULOPLCMD, "");
            //????
            if (childSessionJob.getStartDate() != null && childSessionJob.getEndDate() != null
                    && (jobIdMap.get(fileJobId) == null || !jobIdMap.get(fileJobId).equals(END))) {
                // HULOPLCMD??????????????
                for (JobSessionNodeEntity node : childSessionJob.getJobSessionNodeEntities()) {
                    String nodeMessage = node.getMessage();
                    if (nodeMessage != null && nodeMessage.length() > 0) {
                        message.append(nodeMessage);
                        message.append(RTN);
                    }
                }
                jobIdMap.put(fileJobId, END);
            }
        }

        m_log.debug("getFileJobDetailMessage() : jobid=" + childJobId + ", message=" + message);
    }

    if (message.length() > 0) {
        message.setLength(message.length() - 1);
    }

    return message.toString();
}

From source file:imitationNLG.SFX.java

public void createLists(File dataFile) {
    try {/*w  w w .  j  a  va 2 s . co m*/
        predicates = new ArrayList<>();
        attributes = new HashMap<>();
        attributeValuePairs = new HashMap<>();
        valueAlignments = new HashMap<>();

        String str = new String();
        boolean begin = false;
        try (BufferedReader br = new BufferedReader(new FileReader(dataFile))) {
            String s;
            while ((s = br.readLine()) != null) {
                if (s.startsWith("[")) {
                    begin = true;
                }
                if (begin) {
                    str += s;
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Bagel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Bagel.class.getName()).log(Level.SEVERE, null, ex);
        }

        JSONArray overArray = new JSONArray(str);

        for (int o = 0; o < overArray.length(); o++) {
            JSONArray arr = overArray.getJSONObject(o).getJSONArray("dial");
            for (int i = 0; i < arr.length(); i++) {
                String MRstr = "";
                String ref = "";
                MRstr = arr.getJSONObject(i).getJSONObject("S").getString("dact");
                ref = arr.getJSONObject(i).getJSONObject("S").getString("ref").replaceAll("-s", "s");

                if ((MRstr.startsWith("inform(") || MRstr.startsWith("inform_only")
                        || MRstr.startsWith("inform_no_match(") || MRstr.startsWith("?confirm(")
                        || MRstr.startsWith("?select(") || MRstr.startsWith("?request(")
                        || MRstr.startsWith("?reqmore(") || MRstr.startsWith("goodbye(")) && !ref.isEmpty()) {
                    String predicate = MRstr.substring(0, MRstr.indexOf("("));
                    if (!predicates.contains(predicate) && predicate != null) {
                        predicates.add(predicate);

                        if (!attributes.containsKey(predicate)) {
                            attributes.put(predicate, new HashSet<String>());
                        }
                        if (!datasetInstances.containsKey(predicate)) {
                            datasetInstances.put(predicate, new ArrayList<DatasetInstance>());
                        }
                    }

                    String attributesStr = MRstr.substring(MRstr.indexOf('(') + 1, MRstr.length() - 1);
                    HashMap<String, HashSet<String>> attributeValues = new HashMap<>();
                    if (!attributesStr.isEmpty()) {
                        HashMap<String, Integer> attrXIndeces = new HashMap<>();

                        String[] args = attributesStr.split(";");
                        if (attributesStr.contains("|")) {
                            System.out.println(attributesStr);
                            System.exit(0);
                        }
                        for (String arg : args) {
                            String attr = "";
                            String value = "";
                            if (arg.contains("=")) {
                                String[] subAttr = arg.split("=");
                                value = subAttr[1].toLowerCase();
                                attr = subAttr[0].toLowerCase().replaceAll("_", "");
                            } else {
                                attr = arg.replaceAll("_", "");
                            }
                            if (!attributes.get(predicate).contains(attr)) {
                                attributes.get(predicate).add(attr);
                            }
                            if (!attributeValues.containsKey(attr)) {
                                attributeValues.put(attr, new HashSet<String>());
                            }
                            if (value.isEmpty()) {
                                value = attr;
                            }

                            if (value.startsWith("\'")) {
                                value = value.substring(1, value.length() - 1);
                            }
                            if (value.toLowerCase().startsWith("x")) {
                                int index = 0;
                                if (!attrXIndeces.containsKey(attr)) {
                                    attrXIndeces.put(attr, 1);
                                } else {
                                    index = attrXIndeces.get(attr);
                                    attrXIndeces.put(attr, index + 1);
                                }
                                value = "x" + index;
                            }
                            if (value.isEmpty()) {
                                System.out.println("EMPTY VALUE");
                                System.exit(0);
                            }

                            attributeValues.get(attr).add(value.trim().toLowerCase());
                        }
                        for (String attr : attributeValues.keySet()) {
                            if (attributeValues.get(attr).contains("yes")
                                    && attributeValues.get(attr).contains("no")) {
                                System.out.println(MRstr);
                                System.out.println(attributeValues);
                                System.exit(0);
                            }
                        }
                    }

                    //REF
                    //DELEXICALIZATION
                    HashMap<String, HashMap<String, Integer>> attrValuePriorities = new HashMap<>();
                    HashMap<String, HashSet<String>> delexAttributeValues = new HashMap<>();
                    int prio = 0;
                    for (String attr : attributeValues.keySet()) {
                        if (!attr.isEmpty()) {
                            delexAttributeValues.put(attr, new HashSet<String>());
                            if (attr.equals("name") || attr.equals("type") || attr.equals("pricerange")
                                    || attr.equals("price") || attr.equals("phone") || attr.equals("address")
                                    || attr.equals("postcode") || attr.equals("area") || attr.equals("near")
                                    || attr.equals("food") || attr.equals("count")
                                    || attr.equals("goodformeal")) {
                                attrValuePriorities.put(attr, new HashMap<String, Integer>());
                                for (String value : attributeValues.get(attr)) {
                                    if (!value.equals("dont_care") && !value.equals("none")
                                            && !value.equals("empty") && !value.equals(attr)) {
                                        attrValuePriorities.get(attr).put(value, prio);
                                        prio++;
                                    } else {
                                        delexAttributeValues.get(attr).add(value);
                                    }
                                }
                            } else {
                                for (String value : attributeValues.get(attr)) {
                                    delexAttributeValues.get(attr).add(value);
                                }
                            }
                        }
                    }
                    boolean change = true;
                    while (change) {
                        change = false;
                        for (String attr1 : attrValuePriorities.keySet()) {
                            for (String value1 : attrValuePriorities.get(attr1).keySet()) {
                                for (String attr2 : attrValuePriorities.keySet()) {
                                    for (String value2 : attrValuePriorities.get(attr2).keySet()) {
                                        if (!value1.equals(value2) && value1.contains(value2)
                                                && attrValuePriorities.get(attr1).get(
                                                        value1) > attrValuePriorities.get(attr2).get(value2)) {
                                            int prio1 = attrValuePriorities.get(attr1).get(value1);
                                            int prio2 = attrValuePriorities.get(attr2).get(value2);
                                            attrValuePriorities.get(attr1).put(value1, prio2);
                                            attrValuePriorities.get(attr2).put(value2, prio1);
                                            change = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    HashMap<String, Integer> xCounts = new HashMap<>();
                    HashMap<String, String> delexMap = new HashMap<>();
                    ref = " " + ref + " ";
                    for (int p = 0; p < prio; p++) {
                        for (String attr : attrValuePriorities.keySet()) {
                            if (!xCounts.containsKey(attr)) {
                                xCounts.put(attr, 0);
                            }
                            for (String value : attrValuePriorities.get(attr).keySet()) {
                                if (attrValuePriorities.get(attr).get(value) == p) {
                                    if (!ref.contains(" " + value + " ") && !value.contains(" and ")
                                            && !value.contains(" or ")) {
                                        /*System.out.println(ref);
                                         System.out.println(attr);
                                         System.out.println(value);
                                         System.out.println(attrValuePriorities);*/
                                    } else if (!ref.contains(" " + value + " ")
                                            && (value.contains(" and ") || value.contains(" or "))) {
                                        String[] values = null;
                                        if (value.contains(" and ")) {
                                            values = value.split(" and ");
                                        } else if (value.contains(" or ")) {
                                            values = value.split(" or ");
                                        }
                                        for (int v = 0; v < values.length; v++) {
                                            if (!ref.contains(" " + values[v] + " ")) {
                                                /*System.out.println(ref);
                                                 System.out.println(attr);
                                                 System.out.println(value);
                                                 System.out.println(values[v]);
                                                 System.out.println(attrValuePriorities);*/
                                            } else {
                                                ref = ref.replace(" " + values[v] + " ", " " + SFX.TOKEN_X
                                                        + attr + "_" + xCounts.get(attr) + " ");
                                                ref = ref.replaceAll("  ", " ");
                                                delexAttributeValues.get(attr)
                                                        .add(SFX.TOKEN_X + attr + "_" + xCounts.get(attr));
                                                delexMap.put(SFX.TOKEN_X + attr + "_" + xCounts.get(attr),
                                                        values[v]);
                                                xCounts.put(attr, xCounts.get(attr) + 1);
                                            }
                                        }
                                    } else {
                                        ref = ref.replace(" " + value + " ",
                                                " " + SFX.TOKEN_X + attr + "_" + xCounts.get(attr) + " ");
                                        ref = ref.replaceAll("  ", " ");
                                        delexAttributeValues.get(attr)
                                                .add(SFX.TOKEN_X + attr + "_" + xCounts.get(attr));
                                        delexMap.put(SFX.TOKEN_X + attr + "_" + xCounts.get(attr), value);
                                        xCounts.put(attr, xCounts.get(attr) + 1);
                                    }
                                }
                            }
                        }
                    }
                    ref = ref.trim();
                    MeaningRepresentation MR = new MeaningRepresentation(predicate, delexAttributeValues,
                            MRstr);
                    MR.setDelexMap(delexMap);

                    ArrayList<String> mentionedValueSequence = new ArrayList<>();
                    ArrayList<String> mentionedAttributeSequence = new ArrayList<>();

                    ArrayList<String> realization = new ArrayList<>();
                    ArrayList<String> alignedRealization = new ArrayList<>();

                    String[] words = ref.replaceAll("([,.?!;:'])", " $1").split(" ");
                    HashMap<String, Integer> attributeXCount = new HashMap<>();
                    for (int w = 0; w < words.length; w++) {
                        String mentionedAttribute = "";
                        if (!words[w].trim().isEmpty()) {
                            int s = words[w].indexOf("[");
                            if (s != -1) {
                                int e = words[w].indexOf("]", s + 1);

                                String mentionedValue = words[w].substring(s, e + 1);
                                words[w] = words[w].replace(mentionedValue, "");
                                if (mentionedValue.contains("+") && !words[w].trim().isEmpty()) {
                                    mentionedAttribute = mentionedValue.substring(1,
                                            mentionedValue.indexOf("+"));

                                    if (MR.getAttributes().containsKey(mentionedAttribute)) {
                                        if (mentionedValueSequence.isEmpty()) {
                                            String v = mentionedValue.substring(1, mentionedValue.length() - 1)
                                                    .replaceAll("\\+", "=");
                                            if (v.endsWith("=X")) {
                                                //v = v.replace("=X", "=@@$$" + a + "$$@@");
                                                int a = 0;
                                                if (!attributeXCount.containsKey(mentionedAttribute)) {
                                                    attributeXCount.put(mentionedAttribute, 1);
                                                } else {
                                                    a = attributeXCount.get(mentionedAttribute);
                                                    attributeXCount.put(mentionedAttribute,
                                                            attributeXCount.get(mentionedAttribute) + 1);
                                                }
                                                v = v.replace("=X", "=x" + a);
                                            }
                                            mentionedValueSequence.add(v.toLowerCase());
                                        } else if (!mentionedValueSequence
                                                .get(mentionedValueSequence.size() - 1)
                                                .equals(mentionedValue)) {
                                            String v = mentionedValue.substring(1, mentionedValue.length() - 1)
                                                    .replaceAll("\\+", "=");
                                            if (v.endsWith("=X")) {
                                                //v = v.replace("=X", "=@@$$" + +a + "$$@@");
                                                int a = 0;
                                                if (!attributeXCount.containsKey(mentionedAttribute)) {
                                                    attributeXCount.put(mentionedAttribute, 1);
                                                } else {
                                                    a = attributeXCount.get(mentionedAttribute);
                                                    attributeXCount.put(mentionedAttribute,
                                                            attributeXCount.get(mentionedAttribute) + 1);
                                                }
                                                v = v.replace("=X", "=x" + a);
                                            }
                                            mentionedValueSequence.add(v.toLowerCase());
                                        }

                                        if (mentionedAttributeSequence.isEmpty()) {
                                            mentionedAttributeSequence.add(mentionedAttribute.toLowerCase());
                                        } else if (!mentionedAttributeSequence
                                                .get(mentionedAttributeSequence.size() - 1)
                                                .equals(mentionedAttribute)) {
                                            mentionedAttributeSequence.add(mentionedAttribute.toLowerCase());
                                        }
                                    }
                                } else if (!words[w].trim().isEmpty()) {
                                    mentionedAttribute = mentionedValue.substring(1,
                                            mentionedValue.length() - 1);

                                    if (!MR.getAttributes().containsKey(mentionedAttribute)) {
                                        mentionedAttribute = "";
                                    }
                                }
                            }
                            if (!words[w].trim().isEmpty()) {
                                if (words[w].trim().equals("thers")) {
                                    realization.add("there");
                                } else {
                                    realization.add(words[w].trim().toLowerCase());
                                }
                            }
                        }
                    }

                    for (String attr : MR.getAttributes().keySet()) {
                        for (String value : MR.getAttributes().get(attr)) {
                            if (attr.equals("name") && value.equals("none")) {
                                mentionedValueSequence.add(0, attr.toLowerCase() + "=" + value.toLowerCase());
                                mentionedAttributeSequence.add(0, attr.toLowerCase());
                            }
                        }
                    }

                    mentionedValueSequence.add(SFX.TOKEN_END);
                    mentionedAttributeSequence.add(SFX.TOKEN_END);
                    if (realization.size() > maxWordRealizationSize) {
                        maxWordRealizationSize = realization.size();
                    }

                    for (String word : realization) {
                        if (word.trim().matches("[,.?!;:']")) {
                            alignedRealization.add(SFX.TOKEN_PUNCT);
                        } else {
                            alignedRealization.add("[]");
                        }
                    }

                    //Calculate alignments
                    HashMap<String, HashMap<String, Double>> alignments = new HashMap<>();
                    for (String attr : MR.getAttributes().keySet()) {
                        /*if (attr.equals("name")
                         || attr.equals("type")
                         || attr.equals("pricerange")
                         || attr.equals("price")
                         || attr.equals("phone")
                         || attr.equals("address")
                         || attr.equals("postcode")
                         || attr.equals("area")
                         || attr.equals("near")
                         || attr.equals("food")
                         || attr.equals("count")
                         || attr.equals("goodformeal")) {*/

                        for (String value : MR.getAttributes().get(attr)) {
                            if (!value.equals("name=none") && !value.startsWith(SFX.TOKEN_X)
                                    && !(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+"))) {
                                String valueToCheck = value;
                                if (value.equals("no") || value.equals("yes") || value.equals("yes or no")
                                        || value.equals("none") || value.equals("dont_care")
                                        || value.equals("empty")) {
                                    valueToCheck = attr;
                                    alignments.put(valueToCheck + ":" + value, new HashMap<String, Double>());
                                } else {
                                    alignments.put(valueToCheck, new HashMap<String, Double>());
                                }
                                //For all ngrams
                                for (int n = 1; n < realization.size(); n++) {
                                    //Calculate all alignment similarities
                                    for (int r = 0; r <= realization.size() - n; r++) {
                                        boolean pass = true;
                                        for (int j = 0; j < n; j++) {
                                            if (realization.get(r + j).startsWith(SFX.TOKEN_X)
                                                    || alignedRealization.get(r + j).equals(SFX.TOKEN_PUNCT)
                                                    || StringNLPUtilities.isArticle(realization.get(r + j))
                                                    || realization.get(r + j).equalsIgnoreCase("and")
                                                    || realization.get(r + j).equalsIgnoreCase("or")) {
                                                pass = false;
                                            }
                                        }
                                        if (pass) {
                                            String align = "";
                                            String compare = "";
                                            String backwardCompare = "";
                                            for (int j = 0; j < n; j++) {
                                                align += (r + j) + " ";
                                                compare += realization.get(r + j);
                                                backwardCompare = realization.get(r + j) + backwardCompare;
                                            }
                                            align = align.trim();

                                            Double distance = Levenshtein.getSimilarity(
                                                    valueToCheck.toLowerCase(), compare.toLowerCase(), true,
                                                    false);
                                            Double backwardDistance = Levenshtein.getSimilarity(
                                                    valueToCheck.toLowerCase(), backwardCompare.toLowerCase(),
                                                    true, false);

                                            if (backwardDistance > distance) {
                                                distance = backwardDistance;
                                            }
                                            if (distance > 0.3) {
                                                if (value.equals("no") || value.equals("yes")
                                                        || value.equals("yes or no") || value.equals("none")
                                                        || value.equals("dont_care") || value.equals("empty")) {
                                                    alignments.get(valueToCheck + ":" + value).put(align,
                                                            distance);
                                                } else {
                                                    alignments.get(valueToCheck).put(align, distance);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        /*} else {
                         for (String value : MR.getAttributes().get(attr)) {
                         if (!value.equals("no")
                         && !value.equals("yes")
                         && !value.equals("none")) {
                         System.out.println(attr + " " + value);
                         }
                         }
                         }*/
                    }

                    HashSet<String> toRemove = new HashSet<>();
                    for (String value : alignments.keySet()) {
                        if (alignments.get(value).isEmpty()) {
                            toRemove.add(value);
                        }
                    }
                    for (String value : toRemove) {
                        alignments.remove(value);
                    }

                    while (!alignments.keySet().isEmpty()) {
                        Double max = Double.NEGATIVE_INFINITY;
                        String[] bestAlignment = new String[2];
                        for (String value : alignments.keySet()) {
                            for (String alignment : alignments.get(value).keySet()) {
                                if (alignments.get(value).get(alignment) > max) {
                                    max = alignments.get(value).get(alignment);
                                    bestAlignment[0] = value;
                                    bestAlignment[1] = alignment;
                                }
                            }
                        }

                        ArrayList<String> alignedStr = new ArrayList<>();
                        String[] coords = bestAlignment[1].split(" ");

                        if (coords.length == 1) {
                            alignedStr.add(realization.get(Integer.parseInt(coords[0].trim())));
                        } else {
                            for (int a = Integer.parseInt(coords[0].trim()); a <= Integer
                                    .parseInt(coords[coords.length - 1].trim()); a++) {
                                alignedStr.add(realization.get(a));
                            }
                        }

                        if (!valueAlignments.containsKey(bestAlignment[0])) {
                            valueAlignments.put(bestAlignment[0], new HashMap<ArrayList<String>, Double>());
                        }
                        valueAlignments.get(bestAlignment[0]).put(alignedStr, max);

                        alignments.remove(bestAlignment[0]);
                        for (String value : alignments.keySet()) {
                            HashSet<String> alignmentsToBeRemoved = new HashSet<>();
                            for (String alignment : alignments.get(value).keySet()) {
                                String[] othCoords = alignment.split(" ");
                                if (Integer.parseInt(coords[0].trim()) <= Integer.parseInt(othCoords[0].trim())
                                        && (Integer.parseInt(coords[coords.length - 1].trim()) >= Integer
                                                .parseInt(othCoords[0].trim()))
                                        || (Integer.parseInt(othCoords[0].trim()) <= Integer
                                                .parseInt(coords[0].trim())
                                                && Integer.parseInt(
                                                        othCoords[othCoords.length - 1].trim()) >= Integer
                                                                .parseInt(coords[0].trim()))) {
                                    alignmentsToBeRemoved.add(alignment);
                                }
                            }
                            for (String alignment : alignmentsToBeRemoved) {
                                alignments.get(value).remove(alignment);
                            }
                        }
                        toRemove = new HashSet<>();
                        for (String value : alignments.keySet()) {
                            if (alignments.get(value).isEmpty()) {
                                toRemove.add(value);
                            }
                        }
                        for (String value : toRemove) {
                            alignments.remove(value);
                        }
                    }
                    String previousAttr = "";
                    for (int a = alignedRealization.size() - 1; a >= 0; a--) {
                        if (alignedRealization.get(a).isEmpty() || alignedRealization.get(a).equals("[]")) {
                            if (!previousAttr.isEmpty()) {
                                alignedRealization.set(a, previousAttr);
                            }
                        } else if (!alignedRealization.get(a).equals(SFX.TOKEN_PUNCT)) {
                            previousAttr = alignedRealization.get(a);
                        } else {
                            previousAttr = "";
                        }
                    }
                    previousAttr = "";
                    for (int a = 0; a < alignedRealization.size(); a++) {
                        if (alignedRealization.get(a).isEmpty() || alignedRealization.get(a).equals("[]")) {
                            if (!previousAttr.isEmpty()) {
                                alignedRealization.set(a, previousAttr);
                            }
                        } else if (!alignedRealization.get(a).equals(SFX.TOKEN_PUNCT)) {
                            previousAttr = alignedRealization.get(a);
                        } else {
                            previousAttr = "";
                        }
                    }

                    //}
                    ArrayList<Action> realizationActions = new ArrayList<>();
                    for (int r = 0; r < realization.size(); r++) {
                        realizationActions.add(new Action(realization.get(r), alignedRealization.get(r)));
                    }

                    //boolean existing = false;
                    DatasetInstance DI = new DatasetInstance(MR, mentionedValueSequence,
                            mentionedAttributeSequence, realizationActions);
                    for (DatasetInstance existingDI : datasetInstances.get(predicate)) {
                        //if (existingDI.getMeaningRepresentation().equals(previousAMR)) {
                        //if (existingDI.getMeaningRepresentation().getAttributes().equals(MR.getAttributes())) {
                        if (existingDI.getMeaningRepresentation().getAttributes()
                                .equals(DI.getMeaningRepresentation().getAttributes())) {
                            //existing = true;
                            //existingDI.mergeDatasetInstance(mentionedValueSequence, mentionedAttributeSequence, realizationActions);
                            existingDI.mergeDatasetInstance(DI.getEvalMentionedValueSequences(),
                                    DI.getEvalMentionedAttributeSequences(), DI.getEvalRealizations());
                            DI.mergeDatasetInstance(existingDI.getEvalMentionedValueSequences(),
                                    existingDI.getEvalMentionedAttributeSequences(),
                                    existingDI.getEvalRealizations());
                        }
                    }
                    //if (!existing) {
                    //DatasetInstance DI = new DatasetInstance(MR, mentionedValueSequence, mentionedAttributeSequence, realizationActions);
                    datasetInstances.get(predicate).add(DI);
                    //}
                }
                //}
            }
        }
        /*int dis = 0;
         int slots = 0;
         for (String pred : datasetInstances.keySet()) {
         System.out.println("FOR PREDICATE: " + pred);
         System.out.println("=====");
         for (DatasetInstance di : datasetInstances.get(pred)) {
         System.out.println(di.getMeaningRepresentation().getPredicate());
         System.out.println(di.getMeaningRepresentation().getAttributes());
                
         System.out.println("***********");
         System.out.println("R: " + di.getEvalRealizations());
         System.out.println("=======================");
         dis++;
         slots += di.getMeaningRepresentation().getAttributes().size();
         }
         }
         System.out.println(dis);
         System.out.println((double) slots / (double) dis);*/
    } catch (JSONException ex) {
        ex.printStackTrace();
    }
}

From source file:ffx.potential.parsers.BiojavaFilter.java

/**
 * Assign force field atoms types to common chemistries using "biotype"
 * records./*  w  w w  .  j  a v  a  2  s  .  co  m*/
 */
public void assignAtomTypes() {
    /**
     * Create a new List to store bonds determined based on PDB atom names.
     */
    bondList = new ArrayList<>();

    /**
     * To Do: Look for cyclic peptides and disulfides.
     */
    Polymer[] polymers = activeMolecularAssembly.getChains();

    /**
     * Loop over chains.
     */
    if (polymers != null) {
        logger.info(format("\n Assigning atom types for %d chains.", polymers.length));
        for (Polymer polymer : polymers) {
            List<Residue> residues = polymer.getResidues();
            int numberOfResidues = residues.size();
            /**
             * Check if all residues are known amino acids.
             */
            boolean isProtein = true;
            if (!residues.isEmpty()) {
                //renameNTerminusHydrogens(residues.get(0)); Not safe to use until it distinguishes between true N-termini and N-terminal residues in general.
            }
            for (int residueNumber = 0; residueNumber < numberOfResidues; residueNumber++) {
                Residue residue = residues.get(residueNumber);
                String name = residue.getName().toUpperCase();
                boolean aa = false;
                for (AminoAcid3 amino : aminoAcidList) {
                    if (amino.toString().equalsIgnoreCase(name)) {
                        aa = true;
                        renameNonstandardHydrogens(residue);
                        break;
                    }
                }
                // Check for a patch.
                if (!aa) {
                    HashMap<String, AtomType> types = forceField.getAtomTypes(name);
                    if (types.isEmpty()) {
                        isProtein = false;
                        break;
                    } else {
                        logger.info(" Patch found for non-standard amino acid " + name);
                    }
                }
            }

            /**
             * If all the residues in this chain have known amino acids
             * names, then attempt to assign atom types.
             */
            if (isProtein) {
                try {
                    logger.info(format(" Amino acid chain %s", polymer.getName()));
                    double dist = properties.getDouble("chainbreak", 3.0);
                    // Detect main chain breaks!
                    List<List<Residue>> subChains = findChainBreaks(residues, dist);
                    for (List<Residue> subChain : subChains) {
                        assignAminoAcidAtomTypes(subChain);
                    }
                } catch (MissingHeavyAtomException missingHeavyAtomException) {
                    logger.severe(missingHeavyAtomException.toString());
                } catch (MissingAtomTypeException missingAtomTypeException) {
                    logger.severe(missingAtomTypeException.toString());
                }
                continue;
            }

            /**
             * Check if all residues have known nucleic acids names.
             */
            boolean isNucleicAcid = true;
            for (int residueNumber = 0; residueNumber < numberOfResidues; residueNumber++) {
                Residue residue = residues.get(residueNumber);
                String name = residue.getName().toUpperCase();
                /**
                 * Convert 1 and 2-character nucleic acid names to
                 * 3-character names.
                 */
                if (name.length() == 1) {
                    if (name.equals("A")) {
                        name = NucleicAcid3.ADE.toString();
                    } else if (name.equals("C")) {
                        name = NucleicAcid3.CYT.toString();
                    } else if (name.equals("G")) {
                        name = NucleicAcid3.GUA.toString();
                    } else if (name.equals("T")) {
                        name = NucleicAcid3.THY.toString();
                    } else if (name.equals("U")) {
                        name = NucleicAcid3.URI.toString();
                    }
                } else if (name.length() == 2) {
                    if (name.equals("YG")) {
                        name = NucleicAcid3.YYG.toString();
                    }
                }
                residue.setName(name);
                NucleicAcid3 nucleicAcid = null;
                for (NucleicAcid3 nucleic : nucleicAcidList) {
                    String nuc3 = nucleic.toString();
                    nuc3 = nuc3.substring(nuc3.length() - 3);
                    if (nuc3.equalsIgnoreCase(name)) {
                        nucleicAcid = nucleic;
                        break;
                    }
                }
                if (nucleicAcid == null) {
                    logger.info(format("Nucleic acid was not recognized %s.", name));
                    isNucleicAcid = false;
                    break;
                }
            }

            /**
             * If all the residues in this chain have known nucleic acids
             * names, then attempt to assign atom types.
             */
            if (isNucleicAcid) {
                try {
                    logger.info(format(" Nucleic acid chain %s", polymer.getName()));
                    assignNucleicAcidAtomTypes(residues);
                } catch (MissingHeavyAtomException missingHeavyAtomException) {
                    logger.severe(missingHeavyAtomException.toString());
                } catch (MissingAtomTypeException missingAtomTypeException) {
                    logger.severe(missingAtomTypeException.toString());
                }
            }
        }
    }

    // Assign ion atom types.
    ArrayList<MSNode> ions = activeMolecularAssembly.getIons();
    if (ions != null && ions.size() > 0) {
        logger.info(format(" Assigning atom types for %d ions.", ions.size()));
        for (MSNode m : ions) {
            Molecule ion = (Molecule) m;
            String name = ion.getResidueName().toUpperCase();
            HetAtoms hetatm = HetAtoms.valueOf(name);
            Atom atom = ion.getAtomList().get(0);
            if (ion.getAtomList().size() != 1) {
                logger.severe(format(" Check residue %s of chain %s.", ion.toString(), ion.getChainID()));
            }
            try {
                switch (hetatm) {
                case NA:
                    atom.setAtomType(findAtomType(2003));
                    break;
                case K:
                    atom.setAtomType(findAtomType(2004));
                    break;
                case MG:
                case MG2:
                    atom.setAtomType(findAtomType(2005));
                    break;
                case CA:
                case CA2:
                    atom.setAtomType(findAtomType(2006));
                    break;
                case CL:
                    atom.setAtomType(findAtomType(2007));
                    break;
                case ZN:
                case ZN2:
                    atom.setAtomType(findAtomType(2008));
                    break;
                case BR:
                    atom.setAtomType(findAtomType(2009));
                    break;
                default:
                    logger.severe(format(" Check residue %s of chain %s.", ion.toString(), ion.getChainID()));
                }
            } catch (Exception e) {
                String message = "Error assigning atom types.";
                logger.log(Level.SEVERE, message, e);
            }
        }
    }
    // Assign water atom types.
    ArrayList<MSNode> water = activeMolecularAssembly.getWaters();
    if (water != null && water.size() > 0) {
        logger.info(format(" Assigning atom types for %d waters.", water.size()));
        for (MSNode m : water) {
            Molecule wat = (Molecule) m;
            try {
                Atom O = buildHeavy(wat, "O", null, 2001);
                Atom H1 = buildHydrogen(wat, "H1", O, 0.96e0, null, 109.5e0, null, 120.0e0, 0, 2002);
                H1.setHetero(true);
                Atom H2 = buildHydrogen(wat, "H2", O, 0.96e0, H1, 109.5e0, null, 120.0e0, 0, 2002);
                H2.setHetero(true);
            } catch (Exception e) {
                String message = "Error assigning atom types to a water.";
                logger.log(Level.SEVERE, message, e);
            }
        }
    }

    // Assign small molecule atom types.
    ArrayList<Molecule> molecules = activeMolecularAssembly.getMolecules();
    for (MSNode m : molecules) {
        Molecule molecule = (Molecule) m;
        String moleculeName = molecule.getResidueName();
        logger.info(" Attempting to patch " + moleculeName);
        ArrayList<Atom> moleculeAtoms = molecule.getAtomList();
        boolean patched = true;
        HashMap<String, AtomType> types = forceField.getAtomTypes(moleculeName);
        /**
         * Assign atom types for all known atoms.
         */
        for (Atom atom : moleculeAtoms) {
            String atomName = atom.getName().toUpperCase();
            AtomType atomType = types.get(atomName);
            if (atomType == null) {
                logger.info(" No atom type was found for " + atomName + " of " + moleculeName + ".");
                patched = false;
                break;
            } else {
                atom.setAtomType(atomType);
                types.remove(atomName);
            }
        }
        /**
         * Create missing hydrogen atoms. Check for missing heavy atoms.
         */
        if (patched && !types.isEmpty()) {
            for (AtomType type : types.values()) {
                if (type.atomicNumber != 1) {
                    logger.info(" Missing heavy atom " + type.name);
                    patched = false;
                    break;
                }
            }
        }
        // Create bonds between known atoms.
        if (patched) {
            for (Atom atom : moleculeAtoms) {
                String atomName = atom.getName();
                String bonds[] = forceField.getBonds(moleculeName, atomName);
                if (bonds != null) {
                    for (String name : bonds) {
                        Atom atom2 = molecule.getAtom(name);
                        if (atom2 != null && !atom.isBonded(atom2)) {
                            buildBond(atom, atom2);
                        }
                    }
                }
            }
        }
        // Create missing hydrogen atoms.
        if (patched && !types.isEmpty()) {
            // Create a hashmap of the molecule's atoms
            HashMap<String, Atom> atomMap = new HashMap<String, Atom>();
            for (Atom atom : moleculeAtoms) {
                atomMap.put(atom.getName().toUpperCase(), atom);
            }
            for (String atomName : types.keySet()) {
                AtomType type = types.get(atomName);
                String bonds[] = forceField.getBonds(moleculeName, atomName.toUpperCase());
                if (bonds == null || bonds.length != 1) {
                    patched = false;
                    logger.info(" Check biotype for hydrogen " + type.name + ".");
                    break;
                }
                // Get the heavy atom the hydrogen is bonded to.
                Atom ia = atomMap.get(bonds[0].toUpperCase());
                Atom hydrogen = new Atom(0, atomName, ia.getAltLoc(), new double[3], ia.getResidueName(),
                        ia.getResidueNumber(), ia.getChainID(), ia.getOccupancy(), ia.getTempFactor(),
                        ia.getSegID());
                logger.fine(" Created hydrogen " + atomName + ".");
                hydrogen.setAtomType(type);
                hydrogen.setHetero(true);
                molecule.addMSNode(hydrogen);
                int valence = ia.getAtomType().valence;
                List<Bond> aBonds = ia.getBonds();
                int numBonds = aBonds.size();
                /**
                 * Try to find the following configuration: ib-ia-ic
                 */
                Atom ib = null;
                Atom ic = null;
                Atom id = null;
                if (numBonds > 0) {
                    Bond bond = aBonds.get(0);
                    ib = bond.get1_2(ia);
                }
                if (numBonds > 1) {
                    Bond bond = aBonds.get(1);
                    ic = bond.get1_2(ia);
                }
                if (numBonds > 2) {
                    Bond bond = aBonds.get(2);
                    id = bond.get1_2(ia);
                }

                /**
                 * Building the hydrogens depends on hybridization and the
                 * locations of other bonded atoms.
                 */
                logger.fine(" Bonding " + atomName + " to " + ia.getName() + " (" + numBonds + " of " + valence
                        + ").");
                switch (valence) {
                case 4:
                    switch (numBonds) {
                    case 3:
                        // Find the average coordinates of atoms ib, ic and id.
                        double b[] = ib.getXYZ(null);
                        double c[] = ib.getXYZ(null);
                        double d[] = ib.getXYZ(null);
                        double a[] = new double[3];
                        a[0] = (b[0] + c[0] + d[0]) / 3.0;
                        a[1] = (b[1] + c[1] + d[1]) / 3.0;
                        a[2] = (b[2] + c[2] + d[2]) / 3.0;
                        // Place the hydrogen at chiral position #1.
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, 1);
                        double e1[] = new double[3];
                        hydrogen.getXYZ(e1);
                        double ret[] = new double[3];
                        diff(a, e1, ret);
                        double l1 = r(ret);
                        // Place the hydrogen at chiral position #2.
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, -1);
                        double e2[] = new double[3];
                        hydrogen.getXYZ(e2);
                        diff(a, e2, ret);
                        double l2 = r(ret);
                        // Revert to #1 if it is farther from the average.
                        if (l1 > l2) {
                            hydrogen.setXYZ(e1);
                        }
                        break;
                    case 2:
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, 0);
                        break;
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 3:
                    switch (numBonds) {
                    case 2:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, ic, 0.0, 0);
                        break;
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 2:
                    switch (numBonds) {
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 1:
                    switch (numBonds) {
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                default:
                    logger.info(" Check biotype for hydrogen " + atomName + ".");
                    patched = false;
                }
                if (!patched) {
                    break;
                } else {
                    buildBond(ia, hydrogen);
                }
            }
        }
        if (!patched) {
            logger.log(Level.WARNING, format(" Deleting unrecognized molecule %s.", m.toString()));
            activeMolecularAssembly.deleteMolecule((Molecule) m);
        } else {
            logger.info(" Patch for " + moleculeName + " succeeded.");
        }
    }
}