Example usage for java.util HashMap toString

List of usage examples for java.util HashMap toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:com.imaginary.home.cloud.CloudTest.java

private void setupDevices() throws Exception {
    if (token == null) {
        setupToken();/* w w  w.  ja  va  2s . co  m*/
    }
    HashMap<String, Object> action = new HashMap<String, Object>();

    action.put("action", "update");

    HashMap<String, Object> relay = new HashMap<String, Object>();

    ArrayList<Map<String, Object>> devices = new ArrayList<Map<String, Object>>();

    {
        HashMap<String, Object> device = new HashMap<String, Object>();

        device.put("systemId", "2");
        device.put("deviceId", "999");
        device.put("model", "XYZ900");
        device.put("on", false);
        device.put("deviceType", "powered");
        device.put("name", "Manipulation Test");
        device.put("description", "A test thing that turns off and on and will be manipulated by tests");
        devices.add(device);
    }
    relay.put("devices", devices);
    action.put("relay", relay);

    HttpClient client = getClient();

    HttpPut method = new HttpPut(cloudAPI + "/relay/" + relayKeyId);
    long timestamp = System.currentTimeMillis();

    method.addHeader("Content-Type", "application/json");
    method.addHeader("x-imaginary-version", VERSION);
    method.addHeader("x-imaginary-timestamp", String.valueOf(timestamp));
    method.addHeader("x-imaginary-api-key", relayKeyId);
    method.addHeader("x-imaginary-signature", CloudService.sign(relayKeySecret.getBytes("utf-8"),
            "put:/relay/" + relayKeyId + ":" + relayKeyId + ":" + token + ":" + timestamp + ":" + VERSION));

    //noinspection deprecation
    method.setEntity(new StringEntity((new JSONObject(action)).toString(), "application/json", "UTF-8"));

    HttpResponse response;
    StatusLine status;

    try {
        response = client.execute(method);
        status = response.getStatusLine();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CommunicationException(e);
    }

    if (status.getStatusCode() != HttpServletResponse.SC_NO_CONTENT) {
        Assert.fail("Failed to update state for relay  (" + status.getStatusCode() + ": "
                + EntityUtils.toString(response.getEntity()));
    }

    HttpGet get = new HttpGet(cloudAPI + "/device?locationId=" + URLEncoder.encode(locationId, "utf-8"));

    timestamp = System.currentTimeMillis();

    get.addHeader("Content-Type", "application/json");
    get.addHeader("x-imaginary-version", VERSION);
    get.addHeader("x-imaginary-timestamp", String.valueOf(timestamp));
    get.addHeader("x-imaginary-api-key", apiKeyId);
    get.addHeader("x-imaginary-signature", CloudService.sign(apiKeySecret.getBytes("utf-8"),
            "get:/device:" + apiKeyId + ":" + timestamp + ":" + VERSION));

    try {
        response = client.execute(get);
        status = response.getStatusLine();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CommunicationException(e);
    }
    if (status.getStatusCode() == HttpServletResponse.SC_OK) {
        String json = EntityUtils.toString(response.getEntity());
        JSONArray list = new JSONArray(json);

        for (int i = 0; i < list.length(); i++) {
            JSONObject device = list.getJSONObject(i);

            out("device -> " + device.toString());
            if (device.has("systemId") && device.getString("systemId").equals("2")) {
                if (device.has("vendorDeviceId") && device.getString("vendorDeviceId").equals("999")) {
                    testDeviceId = device.getString("deviceId");
                    break;
                }
            }
        }
    } else {
        Assert.fail("Failed to list devices  (" + status.getStatusCode() + ": "
                + EntityUtils.toString(response.getEntity()));
    }
    Assert.assertNotNull("Test device could not be found during setup", testDeviceId);
}

From source file:org.freebxml.omar.server.persistence.rdb.SQLPersistenceManagerImpl.java

/**
 * Checks each object being deleted to make sure that it does not have any currently existing references.
 * Objects must be fetched from the Cache or Server and not from the RequestContext??
 *
 * @throws ReferencesExistException if references exist to any of the RegistryObject ids specified in roIds
 *
 *///from w  w  w  . ja v a2 s.  c o  m
public void checkIfReferencesExist(ServerRequestContext context, List roIds) throws RegistryException {
    if (skipReferenceCheckOnRemove) {
        return;
    }

    Iterator iter = roIds.iterator();

    HashMap idToReferenceSourceMap = new HashMap();
    while (iter.hasNext()) {
        String id = (String) iter.next();

        StringBuffer query = new StringBuffer();
        query.append("SELECT id FROM RegistryObject WHERE objectType = ? UNION ");
        query.append("SELECT id FROM ClassificationNode WHERE parent = ? UNION ");
        query.append(
                "SELECT id FROM Classification WHERE classificationNode = ? OR classificationScheme = ? OR classifiedObject = ? UNION ");
        query.append(
                "SELECT id FROM ExternalIdentifier WHERE identificationScheme = ? OR registryObject = ? UNION ");
        query.append(
                "SELECT id FROM Association WHERE associationType = ? OR sourceObject = ? OR targetObject= ?  UNION ");
        query.append("SELECT id FROM AuditableEvent WHERE user_ = ? OR requestId = ? UNION ");
        query.append("SELECT id FROM Organization WHERE parent = ? UNION ");
        query.append("SELECT id FROM Registry where operator = ? UNION ");
        query.append("SELECT id FROM ServiceBinding WHERE service = ? OR targetBinding = ? UNION ");
        query.append(
                "SELECT id FROM SpecificationLink WHERE serviceBinding = ? OR specificationObject = ? UNION ");
        query.append("SELECT id FROM Subscription WHERE selector = ?  UNION ");
        query.append("SELECT s.parent FROM Slot s WHERE s.slotType = '" + bu.CANONICAL_DATA_TYPE_ID_ObjectRef
                + "' AND s.value = ?");

        PreparedStatement stmt = null;
        try {
            stmt = context.getConnection().prepareStatement(query.toString());
            stmt.setString(1, id);
            stmt.setString(2, id);
            stmt.setString(3, id);
            stmt.setString(4, id);
            stmt.setString(5, id);
            stmt.setString(6, id);
            stmt.setString(7, id);
            stmt.setString(8, id);
            stmt.setString(9, id);
            stmt.setString(10, id);
            stmt.setString(11, id);
            stmt.setString(12, id);
            stmt.setString(13, id);
            stmt.setString(14, id);
            stmt.setString(15, id);
            stmt.setString(16, id);
            stmt.setString(17, id);
            stmt.setString(18, id);
            stmt.setString(19, id);
            stmt.setString(20, id);
            log.trace("SQL = " + query.toString()); // HIEOS/BHT: (DEBUG)
            ResultSet rs = stmt.executeQuery();
            boolean result = false;

            ArrayList referenceSourceIds = new ArrayList();
            while (rs.next()) {
                String referenceSourceId = rs.getString(1);
                if (!roIds.contains(referenceSourceId)) {
                    referenceSourceIds.add(referenceSourceId);
                }
            }

            if (!referenceSourceIds.isEmpty()) {
                idToReferenceSourceMap.put(id, referenceSourceIds);
            }
        } catch (SQLException e) {
            throw new RegistryException(e);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException sqle) {
                log.error(ServerResourceBundle.getInstance().getString("message.CaughtException1"), sqle);
            }
        }
    }

    if (!idToReferenceSourceMap.isEmpty()) {
        //At least one ref exists to at least one object so throw exception
        String msg = ServerResourceBundle.getInstance().getString("message.referencesExist");
        msg += "\n" + idToReferenceSourceMap.toString();

        throw new ReferencesExistException(msg);
    }
}

From source file:it.cnr.icar.eric.server.persistence.rdb.SQLPersistenceManagerImpl.java

/**
 * Checks each object being deleted to make sure that it does not have any
 * currently existing references. Objects must be fetched from the Cache or
 * Server and not from the RequestContext??
 * /*from  w ww  .j a  va2  s .  c  o m*/
 * @throws ReferencesExistException
 *             if references exist to any of the RegistryObject ids
 *             specified in roIds
 * 
 */
@SuppressWarnings("static-access")
public void checkIfReferencesExist(ServerRequestContext context, List<String> roIds) throws RegistryException {
    if (skipReferenceCheckOnRemove) {
        return;
    }

    Iterator<String> iter = roIds.iterator();

    HashMap<String, ArrayList<String>> idToReferenceSourceMap = new HashMap<String, ArrayList<String>>();
    while (iter.hasNext()) {
        String id = iter.next();

        StringBuffer query = new StringBuffer();
        query.append("SELECT id FROM RegistryObject WHERE objectType = ? UNION ");
        query.append("SELECT id FROM ClassificationNode WHERE parent = ? UNION ");
        query.append(
                "SELECT id FROM Classification WHERE classificationNode = ? OR classificationScheme = ? OR classifiedObject = ? UNION ");
        query.append(
                "SELECT id FROM ExternalIdentifier WHERE identificationScheme = ? OR registryObject = ? UNION ");
        query.append(
                "SELECT id FROM Association WHERE associationType = ? OR sourceObject = ? OR targetObject= ?  UNION ");
        query.append("SELECT id FROM AuditableEvent WHERE user_ = ? OR requestId = ? UNION ");
        query.append("SELECT id FROM Organization WHERE parent = ? UNION ");
        query.append("SELECT id FROM Registry where operator = ? UNION ");
        query.append("SELECT id FROM ServiceBinding WHERE service = ? OR targetBinding = ? UNION ");
        query.append(
                "SELECT id FROM SpecificationLink WHERE serviceBinding = ? OR specificationObject = ? UNION ");
        query.append("SELECT id FROM Subscription WHERE selector = ?  UNION ");
        query.append("SELECT s.parent FROM Slot s WHERE s.slotType = '" + bu.CANONICAL_DATA_TYPE_ID_ObjectRef
                + "' AND s.value = ?");

        PreparedStatement stmt = null;
        try {
            stmt = context.getConnection().prepareStatement(query.toString());
            stmt.setString(1, id);
            stmt.setString(2, id);
            stmt.setString(3, id);
            stmt.setString(4, id);
            stmt.setString(5, id);
            stmt.setString(6, id);
            stmt.setString(7, id);
            stmt.setString(8, id);
            stmt.setString(9, id);
            stmt.setString(10, id);
            stmt.setString(11, id);
            stmt.setString(12, id);
            stmt.setString(13, id);
            stmt.setString(14, id);
            stmt.setString(15, id);
            stmt.setString(16, id);
            stmt.setString(17, id);
            stmt.setString(18, id);
            stmt.setString(19, id);
            stmt.setString(20, id);

            ResultSet rs = stmt.executeQuery();
            @SuppressWarnings("unused")
            boolean result = false;

            ArrayList<String> referenceSourceIds = new ArrayList<String>();
            while (rs.next()) {
                String referenceSourceId = rs.getString(1);
                if (!roIds.contains(referenceSourceId)) {
                    referenceSourceIds.add(referenceSourceId);
                }
            }

            if (!referenceSourceIds.isEmpty()) {
                idToReferenceSourceMap.put(id, referenceSourceIds);
            }
        } catch (SQLException e) {
            throw new RegistryException(e);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException sqle) {
                log.error(ServerResourceBundle.getInstance().getString("message.CaughtException1"), sqle);
            }
        }
    }

    if (!idToReferenceSourceMap.isEmpty()) {
        // At least one ref exists to at least one object so throw exception
        String msg = ServerResourceBundle.getInstance().getString("message.referencesExist");
        msg += "\n" + idToReferenceSourceMap.toString();

        throw new ReferencesExistException(msg);
    }
}

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

@Override
public void processRequest() throws Exception {
    resetPanel();//from w w w.jav a  2 s .c om
    panel.setStudyInfoShown(false);
    panel.setOrderedData(true);

    FormProcessor fp = new FormProcessor(request);
    // checks which module the requests are from
    String module = fp.getString(MODULE);
    // keep the module in the session
    session.setAttribute(MODULE, module);

    String action = request.getParameter("action");
    CRFVersionBean version = (CRFVersionBean) session.getAttribute("version");

    File xsdFile = new File(SpringServletAccess.getPropertiesDir(context) + "ODM1-3-0.xsd");
    File xsdFile2 = new File(SpringServletAccess.getPropertiesDir(context) + "ODM1-2-1.xsd");

    if (StringUtil.isBlank(action)) {
        logger.info("action is blank");
        request.setAttribute("version", version);
        forwardPage(Page.IMPORT_CRF_DATA);
    }
    if ("confirm".equalsIgnoreCase(action)) {
        String dir = SQLInitServlet.getField("filePath");
        if (!new File(dir).exists()) {
            logger.info("The filePath in datainfo.properties is invalid " + dir);
            addPageMessage(respage.getString("filepath_you_defined_not_seem_valid"));
            forwardPage(Page.IMPORT_CRF_DATA);
        }
        // All the uploaded files will be saved in filePath/crf/original/
        String theDir = dir + "crf" + File.separator + "original" + File.separator;
        if (!new File(theDir).isDirectory()) {
            new File(theDir).mkdirs();
            logger.info("Made the directory " + theDir);
        }
        // MultipartRequest multi = new MultipartRequest(request, theDir, 50 * 1024 * 1024);
        File f = null;
        try {
            f = uploadFile(theDir, version);

        } catch (Exception e) {
            logger.warn("*** Found exception during file upload***");
            e.printStackTrace();

        }
        if (f == null) {
            forwardPage(Page.IMPORT_CRF_DATA);
        }

        // TODO
        // validation steps
        // 1. valid xml - validated by file uploader below

        // LocalConfiguration config = LocalConfiguration.getInstance();
        // config.getProperties().setProperty(
        // "org.exolab.castor.parser.namespaces",
        // "true");
        // config
        // .getProperties()
        // .setProperty("org.exolab.castor.sax.features",
        // "http://xml.org/sax/features/validation,
        // http://apache.org/xml/features/validation/schema,
        // http://apache.org/xml/features/validation/schema-full-checking");
        // // above sets to validate against namespace

        Mapping myMap = new Mapping();
        // @pgawade 18-April-2011 Fix for issue 8394
        String ODM_MAPPING_DIRPath = CoreResources.ODM_MAPPING_DIR;
        myMap.loadMapping(ODM_MAPPING_DIRPath + File.separator + "cd_odm_mapping.xml");

        Unmarshaller um1 = new Unmarshaller(myMap);
        // um1.addNamespaceToPackageMapping("http://www.openclinica.org/ns/odm_ext_v130/v3.1", "OpenClinica");
        // um1.addNamespaceToPackageMapping("http://www.cdisc.org/ns/odm/v1.3"
        // ,
        // "ODMContainer");
        boolean fail = false;
        ODMContainer odmContainer = new ODMContainer();
        session.removeAttribute("odmContainer");
        try {

            // schemaValidator.validateAgainstSchema(f, xsdFile);
            // utf-8 compliance, tbh 06/2009
            InputStreamReader isr = new InputStreamReader(new FileInputStream(f), "UTF-8");
            odmContainer = (ODMContainer) um1.unmarshal(isr);

            logger.debug("Found crf data container for study oid: "
                    + odmContainer.getCrfDataPostImportContainer().getStudyOID());
            logger.debug("found length of subject list: "
                    + odmContainer.getCrfDataPostImportContainer().getSubjectData().size());
            // 2. validates against ODM 1.3
            // check it all below, throw an exception and route to a
            // different
            // page if not working

            // TODO this block of code needs the xerces serializer in order
            // to
            // work

            // StringWriter myWriter = new StringWriter();
            // Marshaller m1 = new Marshaller(myWriter);
            //
            // m1.setProperty("org.exolab.castor.parser.namespaces",
            // "true");
            // m1
            // .setProperty("org.exolab.castor.sax.features",
            // "http://xml.org/sax/features/validation,
            // http://apache.org/xml/features/validation/schema,
            // http://apache.org/xml/features/validation/schema-full-checking
            // ");
            //
            // m1.setMapping(myMap);
            // m1.setNamespaceMapping("",
            // "http://www.cdisc.org/ns/odm/v1.3");
            // m1.setSchemaLocation("http://www.cdisc.org/ns/odm/v1.3
            // ODM1-3.xsd");
            // m1.marshal(odmContainer);
            // if you havent thrown it, you wont throw it here
            addPageMessage(respage.getString("passed_xml_validation"));
        } catch (Exception me1) {
            me1.printStackTrace();
            // expanding it to all exceptions, but hoping to catch Marshal
            // Exception or SAX Exceptions
            logger.info("found exception with xml transform");
            //
            logger.info("trying 1.2.1");
            try {
                schemaValidator.validateAgainstSchema(f, xsdFile2);
                // for backwards compatibility, we also try to validate vs
                // 1.2.1 ODM 06/2008
                InputStreamReader isr = new InputStreamReader(new FileInputStream(f), "UTF-8");
                odmContainer = (ODMContainer) um1.unmarshal(isr);
            } catch (Exception me2) {
                // not sure if we want to report me2
                MessageFormat mf = new MessageFormat("");
                mf.applyPattern(respage.getString("your_xml_is_not_well_formed"));
                Object[] arguments = { me1.getMessage() };
                addPageMessage(mf.format(arguments));
                //
                // addPageMessage("Your XML is not well-formed, and does not
                // comply with the ODM 1.3 Schema. Please check it, and try
                // again. It returned the message: "
                // + me1.getMessage());
                // me1.printStackTrace();
                forwardPage(Page.IMPORT_CRF_DATA);
                // you can't really wait to forward because then you throw
                // NPEs
                // in the next few parts of the code
            }
        }
        // TODO need to output further here
        // 2.a. is the study the same one that the user is in right now?
        // 3. validates against study metadata
        // 3.a. is that study subject in that study?
        // 3.b. is that study event def in that study?
        // 3.c. is that site in that study?
        // 3.d. is that crf version in that study event def?
        // 3.e. are those item groups in that crf version?
        // 3.f. are those items in that item group?

        List<String> errors = getImportCRFDataService().validateStudyMetadata(odmContainer,
                ub.getActiveStudyId());
        if (errors != null) {
            // add to session
            // forward to another page
            logger.info(errors.toString());
            for (String error : errors) {
                addPageMessage(error);
            }
            if (errors.size() > 0) {
                // fail = true;
                forwardPage(Page.IMPORT_CRF_DATA);
            } else {
                addPageMessage(respage.getString("passed_study_check"));
                addPageMessage(respage.getString("passed_oid_metadata_check"));
            }

        }
        logger.debug("passed error check");
        // TODO ADD many validation steps before we get to the
        // session-setting below
        // 4. is the event in the correct status to accept data import?
        // -- scheduled, data entry started, completed
        // (and the event should already be created)
        // (and the event should be independent, ie not affected by other
        // events)

        Boolean eventCRFStatusesValid = getImportCRFDataService().eventCRFStatusesValid(odmContainer, ub);
        ImportCRFInfoContainer importCrfInfo = new ImportCRFInfoContainer(odmContainer, sm.getDataSource());
        // The eventCRFBeans list omits EventCRFs that don't match UpsertOn rules. If EventCRF did not exist and
        // doesn't match upsert, it won't be created.
        List<EventCRFBean> eventCRFBeans = getImportCRFDataService().fetchEventCRFBeans(odmContainer, ub);
        List<DisplayItemBeanWrapper> displayItemBeanWrappers = new ArrayList<DisplayItemBeanWrapper>();
        HashMap<String, String> totalValidationErrors = new HashMap<String, String>();
        HashMap<String, String> hardValidationErrors = new HashMap<String, String>();
        // The following map is used for setting the EventCRF status post import.
        HashMap<Integer, String> importedCRFStatuses = getImportCRFDataService()
                .fetchEventCRFStatuses(odmContainer);
        // @pgawade 17-May-2011 Fix for issue#9590 - collection of
        // eventCRFBeans is returned as null
        // when status of one the events in xml file is either stopped,
        // signed or locked.
        // Instead of repeating the code to fetch the events in xml file,
        // method in the ImportCRFDataService is modified for this fix.
        if (eventCRFBeans == null) {
            fail = true;
            addPageMessage(respage.getString("no_event_status_matching"));
        } else {
            ArrayList<Integer> permittedEventCRFIds = new ArrayList<Integer>();
            logger.info("found a list of eventCRFBeans: " + eventCRFBeans.toString());

            // List<DisplayItemBeanWrapper> displayItemBeanWrappers = new ArrayList<DisplayItemBeanWrapper>();
            // HashMap<String, String> totalValidationErrors = new
            // HashMap<String, String>();
            // HashMap<String, String> hardValidationErrors = new
            // HashMap<String, String>();
            logger.debug("found event crfs " + eventCRFBeans.size());
            // -- does the event already exist? if not, fail
            if (!eventCRFBeans.isEmpty()) {
                for (EventCRFBean eventCRFBean : eventCRFBeans) {
                    DataEntryStage dataEntryStage = eventCRFBean.getStage();
                    Status eventCRFStatus = eventCRFBean.getStatus();

                    logger.info("Event CRF Bean: id " + eventCRFBean.getId() + ", data entry stage "
                            + dataEntryStage.getName() + ", status " + eventCRFStatus.getName());
                    if (eventCRFStatus.equals(Status.AVAILABLE)
                            || dataEntryStage.equals(DataEntryStage.INITIAL_DATA_ENTRY)
                            || dataEntryStage.equals(DataEntryStage.INITIAL_DATA_ENTRY_COMPLETE)
                            || dataEntryStage.equals(DataEntryStage.DOUBLE_DATA_ENTRY_COMPLETE)
                            || dataEntryStage.equals(DataEntryStage.DOUBLE_DATA_ENTRY)) {
                        // actually want the negative
                        // was status == available and the stage questions, but
                        // when you are at 'data entry complete' your status is
                        // set to 'unavailable'.
                        // >> tbh 09/2008
                        // HOWEVER, when one event crf is removed and the rest
                        // are good, what happens???
                        // need to create a list and inform that one is blocked
                        // and the rest are not...
                        //
                        permittedEventCRFIds.add(new Integer(eventCRFBean.getId()));
                    } else {
                        // fail = true;
                        // addPageMessage(respage.getString(
                        // "the_event_crf_not_correct_status"));
                        // forwardPage(Page.IMPORT_CRF_DATA);
                    }
                }

                // so that we don't repeat this following message
                // did we exclude all the event CRFs? if not, pass, else fail
                if (eventCRFBeans.size() >= permittedEventCRFIds.size()) {
                    addPageMessage(respage.getString("passed_event_crf_status_check"));
                } else {
                    fail = true;
                    addPageMessage(respage.getString("the_event_crf_not_correct_status"));
                }
                // do they all have to have the right status to move
                // forward? answer from bug tracker = no
                // 5. do the items contain the correct data types?

                // 6. are all the related OIDs present?
                // that is to say, do we chain all the way down?
                // this is covered by the OID Metadata Check

                // 7. do the edit checks pass?
                // only then can we pass on to VERIFY_IMPORT_SERVLET

                // do we overwrite?

                // XmlParser xp = new XmlParser();
                // List<HashMap<String, String>> importedData =
                // xp.getData(f);

                // now we generate hard edit checks, and have to set that to the
                // screen. get that from the service, generate a summary bean to
                // set to either
                // page in the workflow, either verifyImport.jsp or import.jsp

                try {
                    List<DisplayItemBeanWrapper> tempDisplayItemBeanWrappers = new ArrayList<DisplayItemBeanWrapper>();
                    tempDisplayItemBeanWrappers = getImportCRFDataService().lookupValidationErrors(request,
                            odmContainer, ub, totalValidationErrors, hardValidationErrors,
                            permittedEventCRFIds);
                    logger.debug("generated display item bean wrappers " + tempDisplayItemBeanWrappers.size());
                    logger.debug("size of total validation errors: " + totalValidationErrors.size());
                    displayItemBeanWrappers.addAll(tempDisplayItemBeanWrappers);
                } catch (NullPointerException npe1) {
                    // what if you have 2 event crfs but the third is a fake?
                    fail = true;
                    logger.debug("threw a NPE after calling lookup validation errors");
                    System.out.println(ExceptionUtils.getStackTrace(npe1));
                    addPageMessage(respage.getString("an_error_was_thrown_while_validation_errors"));
                    // npe1.printStackTrace();
                } catch (OpenClinicaException oce1) {
                    fail = true;
                    logger.debug("threw an OCE after calling lookup validation errors "
                            + oce1.getOpenClinicaMessage());
                    addPageMessage(oce1.getOpenClinicaMessage());
                }
            } else if (!eventCRFStatusesValid) {
                fail = true;
                addPageMessage(respage.getString("the_event_crf_not_correct_status"));
            } else {
                fail = true;
                addPageMessage(respage.getString("no_event_crfs_matching_the_xml_metadata"));
            }
            // for (HashMap<String, String> crfData : importedData) {
            // DisplayItemBeanWrapper displayItemBeanWrapper =
            // testing(request,
            // crfData);
            // displayItemBeanWrappers.add(displayItemBeanWrapper);
            // errors = displayItemBeanWrapper.getValidationErrors();
            //
            // }
        }
        if (fail) {
            logger.debug("failed here - forwarding...");
            forwardPage(Page.IMPORT_CRF_DATA);
        } else {
            addPageMessage(respage.getString("passing_crf_edit_checks"));
            session.setAttribute("odmContainer", odmContainer);
            session.setAttribute("importedData", displayItemBeanWrappers);
            session.setAttribute("validationErrors", totalValidationErrors);
            session.setAttribute("hardValidationErrors", hardValidationErrors);
            session.setAttribute("importedCRFStatuses", importedCRFStatuses);
            session.setAttribute("importCrfInfo", importCrfInfo);
            // above are updated 'statically' by the method that originally
            // generated the wrappers; soon the only thing we will use
            // wrappers for is the 'overwrite' flag

            logger.debug("+++ content of total validation errors: " + totalValidationErrors.toString());
            SummaryStatsBean ssBean = getImportCRFDataService().generateSummaryStatsBean(odmContainer,
                    displayItemBeanWrappers, importCrfInfo);
            session.setAttribute("summaryStats", ssBean);
            // will have to set hard edit checks here as well
            session.setAttribute("subjectData", odmContainer.getCrfDataPostImportContainer().getSubjectData());
            forwardPage(Page.VERIFY_IMPORT_SERVLET);
        }
        // }
    }
}

From source file:com.MainFiles.Functions.java

public String PIN_Verify(String strReferenceNo, String strNarration, String strProcCode, String strAmount,
        String strProcessingCode, String strClearPIN, String strPAN, String strExpiryDate,
        String strTerminalSerial, String strField35, String strField37, String strPhoneNumber,
        String strDebitAccount, String strCreditAccount, String strField68) {

    try {//from   www .j av a 2 s  .  c om
        PinBlock pin = new PinBlock();
        String response;
        String STAN = "" + GetSequenceNumber();
        String strPINBlock = pin.pinProcess(strClearPIN, trimLeftString(strPAN), PIN_VERIFICATION_ENCRYPT_KEYS);
        // String strPINBlock = generatePINBlock.DESEncrypt(strClearPIN, trimLeftString(strPAN), PIN_VERIFICATION_ENCRYPT_KEYS);
        String Track2Data = trimLeftString(strField35);
        String cardSequenceNumber = null;
        if (Track2Data != null && Track2Data.length() >= 3) {
            cardSequenceNumber = Track2Data.substring(Track2Data.length() - 3);
        }

        HashMap<String, String> data = new HashMap<String, String>();
        data.put("MTI", "0200"); // Message Type
        data.put("2", trimLeftString(strPAN)); // Primary Account Number (PAN)
        data.put("3", "320000");//Processing code
        data.put("4", "000000000000");//Amount
        data.put("7", (new SimpleDateFormat("MMddHHmmss")).format(new java.util.Date())); // Valuedate 
        data.put("11", STAN); // Transaction Unique Reference (STAN)
        data.put("12", (new SimpleDateFormat("hhmmss")).format(new java.util.Date())); // Time, local transaction
        data.put("13", (new SimpleDateFormat("MMdd")).format(new java.util.Date())); //Local Transaction Date
        data.put("14", strExpiryDate);// Card Expiry date
        data.put("15", (new SimpleDateFormat("MMdd")).format(new java.util.Date()));//Settlement date
        data.put("22", "902");//POS Entry Mode
        data.put("23", cardSequenceNumber);//Card Sequence Number
        data.put("25", "00");//POS Condition Mode
        data.put("26", "12");//POS Entry Mode
        data.put("28", "C00000000");//Amount, Transaction Fee
        data.put("30", "C00000000");//Amount, Transaction Processing Fee
        data.put("32", "639673");//Acquiring institution code(BIN)
        data.put("35", trimLeftString(strField35));//TRACK2 DATA
        data.put("37", strField37);//Retrieval Reference Number
        data.put("41", "22310001");//Card Acceptor terminal ID
        data.put("42", "TPDF           ");// Card Acceptor ID code
        data.put("43", "NMB House  Test        Dar Es Salaam  TZ");// Card Acceptor terminal ID
        data.put("49", "834");// Currency Code
        data.put("52", strPINBlock);// PIN BLOCK
        data.put("56", "1510");// Message reason code
        data.put("59", PadZeros(10, STAN));// Echo Data
        data.put("123", "21010121014C101");// POS Data Code

        String requestISO = iso.CreateISO(data);
        requestISO = requestISO.replace(strPINBlock, new String(HexString2Bytes(strPINBlock), "ISO8859_1"));
        String header = (new DataConversions()).IntegertoASCII(requestISO.length());
        requestISO = header + requestISO;

        if (ISO8583Adaptor.isConnectedClient) {
            ClientSocketHandler.chEv.getChannel().write(requestISO);
            //  Mask the PAN [replace PAN,Pin Block data during logging]
            String strnewpan = strPAN;
            strnewpan = strnewpan.replace(strPAN.substring(6, 14), "xxxxxxxxxxxxxx");
            String strField_35 = strField35.replace(strField35.substring(6, 32), "xxxxxxxxxxxxxxxxxxxxxxxxx");
            data.remove("2");
            data.remove("52");
            data.remove("35");
            data.put("2", strnewpan);
            data.put("35", strField_35);
            data.put("52", "*********************************");

            // Save map details on the OuterHashMap Collection
            data.put("88", strNarration);
            data.remove("3");
            data.put("3", strProcessingCode);
            data.put("4", strAmount);
            data.put("88", strNarration);
            data.put("65", strReferenceNo);
            data.put("68", strField68);
            data.put("100", strProcCode);
            data.put("102", strDebitAccount);
            data.put("103", strCreditAccount);
            data.put("104", strTerminalSerial);
            data.put("PhoneNumber", strPhoneNumber);
            data.put("timestamp", this.anyDate(""));

            OuterHoldingQueue.put(strField37, data);

        } else {
            String strMessageToPOS = "";
            System.out.println("Message Failed to be Sent Postilion");
            String strnewpan = strPAN;
            strnewpan = strnewpan.replace(strPAN.substring(6, 14), "xxxxxxxxxxxxxx");
            String strField_35 = strField35.replace(strField35.substring(6, 32), "xxxxxxxxxxxxxxxxxxxxxxxxx");
            data.remove("2");
            data.remove("52");
            data.remove("35");
            data.put("2", strnewpan);
            data.put("39", "Failed Connecting to Postilion");
            data.put("52", "*********************************");
            data.put("35", strField_35);
            data.put("timestamp", this.anyDate(""));

            strMessageToPOS += this.strResponseHeader(strField68) + "#";
            strMessageToPOS += "AGENT ID:  " + strTerminalSerial.toString() + "#";
            strMessageToPOS += "TRAN NUM:  " + strField37 + "#";
            strMessageToPOS += "--------------------------------" + "#";
            strMessageToPOS += "                                " + "#";
            strMessageToPOS += "        PIN VERIFICATION        " + "#";
            strMessageToPOS += "                                " + "#";
            strMessageToPOS += " FAILED CONNECTING TO POSTILION " + "#";
            strMessageToPOS += " " + "#";
            strMessageToPOS += this.strResponseFooter(strTerminalSerial.toString()) + "#";
            SendPOSResponse(strMessageToPOS, strField37);
        }
        /*
         data.put("39", "00");
                
         if (strDebitAccount.isEmpty() || strDebitAccount == null) {
         strDebitAccount = "20201200003";
         }
         data.put("88", strNarration);
         data.remove("3");
         data.put("3", strProcessingCode);
         data.put("4", strAmount);
         data.put("88", strNarration);
         data.put("65", strReferenceNo);
         data.put("100", strProcCode);
         data.put("102", strDebitAccount);
         data.put("103", strCreditAccount);
         data.put("104", strTerminalSerial);
         data.put("PhoneNumber", strPhoneNumber);
         data.put("timestamp", this.anyDate(""));
         OuterHoldingQueue.put(strField37, data);
         getSwitchResponse(data);
         */
        this.log(data.toString() + "\n\n", "PinVerification");

    } catch (Exception ex) {
        this.log("Error on Function PIN_Verify() " + ex.getMessage() + "\n" + this.StackTraceWriter(ex),
                "ERROR");
        return "";
    }
    return "";
}

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

/**
 * For each single item in this section which is a parent, get a DisplayItemBean corresponding to that item. Note that an item is a parent iff its parentId
 * == 0./*from   ww  w  .  j a v a 2  s.  c o m*/
 * @param sb
 *            The section whose items we are retrieving.
 * @param hasUngroupedItems
 * @param request TODO
 *
 * @return An array of DisplayItemBean objects, one per parent item in the section. Note that there is no guarantee on the ordering of the objects.
 * @throws Exception
 */
private ArrayList getParentDisplayItems(boolean hasGroup, SectionBean sb, EventDefinitionCRFBean edcb,
        ItemDAO idao, ItemFormMetadataDAO ifmdao, ItemDataDAO iddao, boolean hasUngroupedItems,
        HttpServletRequest request) throws Exception {
    ArrayList answer = new ArrayList();
    EventCRFBean ecb = (EventCRFBean) request.getAttribute(INPUT_EVENT_CRF);

    HashMap displayItems = new HashMap();

    // ArrayList items = idao.findAllParentsBySectionId(sb.getId());

    ArrayList items = new ArrayList();
    ArrayList itemsUngrped = new ArrayList();
    if (hasGroup) {
        // issue 1689: this method causes problems with items that have
        // been defined as grouped, then redefined as ungrouped; thus it
        // is "checked twice" with hasUngroupedItems.
        // See: FormBeanUtil.sectionHasUngroupedItems.
        if (hasUngroupedItems) {
            // @pgawade 05-Aug-2011 fix for issue 10628 - commented out the
            // if-else logic based on 'hasGroup' flag.
            // 'if' part is unchanged but else part is changed to be
            // executed always because that is to get the non-repeating and
            // ungrouped item details and was getting skipped in case the
            // section has repeating group items plus non-repeating group
            // items
            itemsUngrped = idao.findAllUngroupedParentsBySectionId(sb.getId(), sb.getCRFVersionId());
        }
        // however, if we have true:true, we exclude all grouped items
        // items.addAll(
        // idao.findAllGroupedParentsBySectionId(
        // sb.getId(), sb.getCRFVersionId()));
    }

    // else {
    LOGGER.trace("no item groups");
    // items = idao.findAllParentsBySectionId(sb.getId());
    items = idao.findAllNonRepeatingParentsBySectionId(sb.getId());
    items.addAll(itemsUngrped);
    // }
    LOGGER.debug("items size" + items.size());
    for (int i = 0; i < items.size(); i++) {
        DisplayItemBean dib = new DisplayItemBean();
        dib.setEventDefinitionCRF(edcb);
        ItemBean ib = (ItemBean) items.get(i);
        dib.setItem(ib);
        displayItems.put(new Integer(dib.getItem().getId()), dib);
    }

    ArrayList data = iddao.findAllBySectionIdAndEventCRFId(sb.getId(), ecb.getId());
    for (int i = 0; i < data.size(); i++) {
        ItemDataBean idb = (ItemDataBean) data.get(i);
        DisplayItemBean dib = (DisplayItemBean) displayItems.get(new Integer(idb.getItemId()));

        if (dib != null) {
            dib.setData(idb);
            dib.setDbData((ItemDataBean) BeanUtils.cloneBean(idb));
            displayItems.put(new Integer(idb.getItemId()), dib);
        }
    }

    ArrayList metadata = ifmdao.findAllBySectionId(sb.getId());
    for (int i = 0; i < metadata.size(); i++) {
        ItemFormMetadataBean ifmb = (ItemFormMetadataBean) metadata.get(i);
        DisplayItemBean dib = (DisplayItemBean) displayItems.get(new Integer(ifmb.getItemId()));
        if (dib != null) {
            // boolean showItem = false;
            boolean needsHighlighting = !ifmb.isShowItem();
            logMe("Entering thread before getting ItemMetadataService:::" + Thread.currentThread());
            boolean showItem = getItemMetadataService().isShown(ifmb.getItemId(), ecb, dib.getData());
            if (getServletPage(request).equals(Page.DOUBLE_DATA_ENTRY_SERVLET)) {
                showItem = getItemMetadataService().hasPassedDDE(ifmb, ecb, dib.getData());
            }
            // is the above needed for children items too?
            boolean passedDDE = getItemMetadataService().hasPassedDDE(ifmb, ecb, dib.getData());
            if (showItem) { // we are only showing, not hiding
                LOGGER.debug("set show item " + ifmb.getItemId() + " idb " + dib.getData().getId()
                        + " show item " + showItem + " passed dde " + passedDDE);
                // ifmb.setShowItem(showItem);
                ifmb.setShowItem(true);
            } else {
                LOGGER.debug("DID NOT set show item " + ifmb.getItemId() + " idb " + dib.getData().getId()
                        + " show item " + showItem + " passed dde " + passedDDE + " value "
                        + dib.getData().getValue());
            }

            dib.setMetadata(ifmb);
            displayItems.put(new Integer(ifmb.getItemId()), dib);
        }
    }

    Iterator hmIt = displayItems.keySet().iterator();
    while (hmIt.hasNext()) {
        Integer key = (Integer) hmIt.next();
        DisplayItemBean dib = (DisplayItemBean) displayItems.get(key);
        answer.add(dib);
        LOGGER.debug(
                "*** getting with key: " + key + " display item bean with value: " + dib.getData().getValue());
    }
    LOGGER.debug("*** test of the display items: " + displayItems.toString());

    return answer;
}

From source file:com.jhh.hdb.sqlparser.MySemanticAnalyzer.java

/**
 * Phase 1: (including, but not limited to):
 *
 * 1. Gets all the aliases for all the tables / subqueries and makes the
 * appropriate mapping in aliasToTabs, aliasToSubq 2. Gets the location of the
 * destination and names the clause "inclause" + i 3. Creates a map from a
 * string representation of an aggregation tree to the actual aggregation AST
 * 4. Creates a mapping from the clause name to the select expression AST in
 * destToSelExpr 5. Creates a mapping from a table alias to the lateral view
 * AST's in aliasToLateralViews//ww  w . j a  va 2 s . c o  m
 *
 * @param ast
 * @param qb
 * @param ctx_1
 * @throws SemanticException
 */
@SuppressWarnings({ "fallthrough", "nls" })
public boolean doPhase1(ASTNode ast, QB qb, Phase1Ctx ctx_1, PlannerContext plannerCtx)
        throws SemanticException {

    boolean phase1Result = true;
    QBParseInfo qbp = qb.getParseInfo();
    boolean skipRecursion = false;

    if (ast.getToken() != null) {
        skipRecursion = true;
        switch (ast.getToken().getType()) {
        case HiveParser.TOK_SELECTDI:
            qb.countSelDi();
            // fall through
        case HiveParser.TOK_SELECT:
            qb.countSel();
            qbp.setSelExprForClause(ctx_1.dest, ast);

            int posn = 0;
            if (((ASTNode) ast.getChild(0)).getToken().getType() == HiveParser.TOK_HINTLIST) {
                qbp.setHints((ASTNode) ast.getChild(0));
                posn++;
            }

            if ((ast.getChild(posn).getChild(0).getType() == HiveParser.TOK_TRANSFORM))
                queryProperties.setUsesScript(true);

            LinkedHashMap<String, ASTNode> aggregations = doPhase1GetAggregationsFromSelect(ast, qb,
                    ctx_1.dest);
            doPhase1GetColumnAliasesFromSelect(ast, qbp);
            qbp.setAggregationExprsForClause(ctx_1.dest, aggregations);
            qbp.setDistinctFuncExprsForClause(ctx_1.dest, doPhase1GetDistinctFuncExprs(aggregations));
            break;

        case HiveParser.TOK_WHERE:
            qbp.setWhrExprForClause(ctx_1.dest, ast);
            if (!SubQueryUtils.findSubQueries((ASTNode) ast.getChild(0)).isEmpty())
                queryProperties.setFilterWithSubQuery(true);
            break;

        case HiveParser.TOK_INSERT_INTO:
            String currentDatabase = SessionState.get().getCurrentDatabase();
            String tab_name = getUnescapedName((ASTNode) ast.getChild(0).getChild(0), currentDatabase);
            qbp.addInsertIntoTable(tab_name);

        case HiveParser.TOK_DESTINATION:
            ctx_1.dest = "insclause-" + ctx_1.nextNum;
            ctx_1.nextNum++;
            boolean isTmpFileDest = false;
            if (ast.getChildCount() > 0 && ast.getChild(0) instanceof ASTNode) {
                ASTNode ch = (ASTNode) ast.getChild(0);
                if (ch.getToken().getType() == HiveParser.TOK_DIR && ch.getChildCount() > 0
                        && ch.getChild(0) instanceof ASTNode) {
                    ch = (ASTNode) ch.getChild(0);
                    isTmpFileDest = ch.getToken().getType() == HiveParser.TOK_TMP_FILE;
                }
            }

            // is there a insert in the subquery
            if (qbp.getIsSubQ() && !isTmpFileDest) {
                throw new SemanticException(ErrorMsg.NO_INSERT_INSUBQUERY.getMsg(ast));
            }

            if (plannerCtx != null) {
                plannerCtx.setInsertToken(ast, isTmpFileDest);
            }

            qbp.setDestForClause(ctx_1.dest, (ASTNode) ast.getChild(0));
            handleInsertStatementSpecPhase1(ast, qbp, ctx_1);
            if (qbp.getClauseNamesForDest().size() > 1) {
                queryProperties.setMultiDestQuery(true);
            }
            break;

        case HiveParser.TOK_FROM:
            int child_count = ast.getChildCount();
            if (child_count != 1) {
                throw new SemanticException(generateErrorMessage(ast, "Multiple Children " + child_count));
            }

            // Check if this is a subquery / lateral view
            ASTNode frm = (ASTNode) ast.getChild(0);
            if (frm.getToken().getType() == HiveParser.TOK_TABREF) {
                processTable(qb, frm);
            } else if (frm.getToken().getType() == HiveParser.TOK_VIRTUAL_TABLE) {
                // Create a temp table with the passed values in it then rewrite this portion of the
                // tree to be from that table.
                ASTNode newFrom = genValuesTempTable(frm, qb);
                ast.setChild(0, newFrom);
                processTable(qb, newFrom);
            } else if (frm.getToken().getType() == HiveParser.TOK_SUBQUERY) {
                processSubQuery(qb, frm);
            } else if (frm.getToken().getType() == HiveParser.TOK_LATERAL_VIEW
                    || frm.getToken().getType() == HiveParser.TOK_LATERAL_VIEW_OUTER) {
                queryProperties.setHasLateralViews(true);
                processLateralView(qb, frm);
            } else if (isJoinToken(frm)) {
                processJoin(qb, frm);
                qbp.setJoinExpr(frm);
            } else if (frm.getToken().getType() == HiveParser.TOK_PTBLFUNCTION) {
                queryProperties.setHasPTF(true);
                processPTF(qb, frm);
            }
            break;

        case HiveParser.TOK_CLUSTERBY:
            // Get the clusterby aliases - these are aliased to the entries in the
            // select list
            queryProperties.setHasClusterBy(true);
            qbp.setClusterByExprForClause(ctx_1.dest, ast);
            break;

        case HiveParser.TOK_DISTRIBUTEBY:
            // Get the distribute by aliases - these are aliased to the entries in
            // the
            // select list
            queryProperties.setHasDistributeBy(true);
            qbp.setDistributeByExprForClause(ctx_1.dest, ast);
            if (qbp.getClusterByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.CLUSTERBY_DISTRIBUTEBY_CONFLICT.getMsg()));
            } else if (qbp.getOrderByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.ORDERBY_DISTRIBUTEBY_CONFLICT.getMsg()));
            }
            break;

        case HiveParser.TOK_SORTBY:
            // Get the sort by aliases - these are aliased to the entries in the
            // select list
            queryProperties.setHasSortBy(true);
            qbp.setSortByExprForClause(ctx_1.dest, ast);
            if (qbp.getClusterByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.CLUSTERBY_SORTBY_CONFLICT.getMsg()));
            } else if (qbp.getOrderByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.ORDERBY_SORTBY_CONFLICT.getMsg()));
            }

            break;

        case HiveParser.TOK_ORDERBY:
            // Get the order by aliases - these are aliased to the entries in the
            // select list
            queryProperties.setHasOrderBy(true);
            qbp.setOrderByExprForClause(ctx_1.dest, ast);
            if (qbp.getClusterByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.CLUSTERBY_ORDERBY_CONFLICT.getMsg()));
            }
            break;

        case HiveParser.TOK_GROUPBY:
        case HiveParser.TOK_ROLLUP_GROUPBY:
        case HiveParser.TOK_CUBE_GROUPBY:
        case HiveParser.TOK_GROUPING_SETS:
            // Get the groupby aliases - these are aliased to the entries in the
            // select list
            queryProperties.setHasGroupBy(true);
            if (qbp.getJoinExpr() != null) {
                queryProperties.setHasJoinFollowedByGroupBy(true);
            }
            if (qbp.getSelForClause(ctx_1.dest).getToken().getType() == HiveParser.TOK_SELECTDI) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.SELECT_DISTINCT_WITH_GROUPBY.getMsg()));
            }
            qbp.setGroupByExprForClause(ctx_1.dest, ast);
            skipRecursion = true;

            // Rollup and Cubes are syntactic sugar on top of grouping sets
            if (ast.getToken().getType() == HiveParser.TOK_ROLLUP_GROUPBY) {
                qbp.getDestRollups().add(ctx_1.dest);
            } else if (ast.getToken().getType() == HiveParser.TOK_CUBE_GROUPBY) {
                qbp.getDestCubes().add(ctx_1.dest);
            } else if (ast.getToken().getType() == HiveParser.TOK_GROUPING_SETS) {
                qbp.getDestGroupingSets().add(ctx_1.dest);
            }
            break;

        case HiveParser.TOK_HAVING:
            qbp.setHavingExprForClause(ctx_1.dest, ast);
            qbp.addAggregationExprsForClause(ctx_1.dest,
                    doPhase1GetAggregationsFromSelect(ast, qb, ctx_1.dest));
            break;

        case HiveParser.KW_WINDOW:
            if (!qb.hasWindowingSpec(ctx_1.dest)) {
                throw new SemanticException(generateErrorMessage(ast,
                        "Query has no Cluster/Distribute By; but has a Window definition"));
            }
            handleQueryWindowClauses(qb, ctx_1, ast);
            break;

        case HiveParser.TOK_LIMIT:
            qbp.setDestLimit(ctx_1.dest, new Integer(ast.getChild(0).getText()));
            break;

        case HiveParser.TOK_ANALYZE:
            // Case of analyze command

            String table_name = getUnescapedName((ASTNode) ast.getChild(0).getChild(0));

            qb.setTabAlias(table_name, table_name);
            qb.addAlias(table_name);
            qb.getParseInfo().setIsAnalyzeCommand(true);
            qb.getParseInfo().setNoScanAnalyzeCommand(this.noscan);
            qb.getParseInfo().setPartialScanAnalyzeCommand(this.partialscan);
            // Allow analyze the whole table and dynamic partitions
            HiveConf.setVar(conf, HiveConf.ConfVars.DYNAMICPARTITIONINGMODE, "nonstrict");
            HiveConf.setVar(conf, HiveConf.ConfVars.HIVEMAPREDMODE, "nonstrict");

            break;

        case HiveParser.TOK_UNIONALL:
            if (!qbp.getIsSubQ()) {
                // this shouldn't happen. The parser should have converted the union to be
                // contained in a subquery. Just in case, we keep the error as a fallback.
                throw new SemanticException(generateErrorMessage(ast, ErrorMsg.UNION_NOTIN_SUBQ.getMsg()));
            }
            skipRecursion = false;
            break;

        case HiveParser.TOK_INSERT:
            ASTNode destination = (ASTNode) ast.getChild(0);
            Tree tab = destination.getChild(0);

            // Proceed if AST contains partition & If Not Exists
            if (destination.getChildCount() == 2 && tab.getChildCount() == 2
                    && destination.getChild(1).getType() == HiveParser.TOK_IFNOTEXISTS) {
                String tableName = tab.getChild(0).getChild(0).getText();

                Tree partitions = tab.getChild(1);
                int childCount = partitions.getChildCount();
                HashMap<String, String> partition = new HashMap<String, String>();
                for (int i = 0; i < childCount; i++) {
                    String partitionName = partitions.getChild(i).getChild(0).getText();
                    Tree pvalue = partitions.getChild(i).getChild(1);
                    if (pvalue == null) {
                        break;
                    }
                    String partitionVal = stripQuotes(pvalue.getText());
                    partition.put(partitionName, partitionVal);
                }
                // if it is a dynamic partition throw the exception
                if (childCount != partition.size()) {
                    throw new SemanticException(
                            ErrorMsg.INSERT_INTO_DYNAMICPARTITION_IFNOTEXISTS.getMsg(partition.toString()));
                }
                Table table = null;
                try {
                    table = db.getTable(tableName);
                } catch (HiveException ex) {
                    throw new SemanticException(ex);
                }
                try {
                    Partition parMetaData = db.getPartition(table, partition, false);
                    // Check partition exists if it exists skip the overwrite
                    if (parMetaData != null) {
                        phase1Result = false;
                        skipRecursion = true;
                        LOG.info("Partition already exists so insert into overwrite "
                                + "skipped for partition : " + parMetaData.toString());
                        break;
                    }
                } catch (HiveException e) {
                    LOG.info("Error while getting metadata : ", e);
                }
                validatePartSpec(table, partition, (ASTNode) tab, conf, false);
            }
            skipRecursion = false;
            break;
        case HiveParser.TOK_LATERAL_VIEW:
        case HiveParser.TOK_LATERAL_VIEW_OUTER:
            // todo: nested LV
            assert ast.getChildCount() == 1;
            qb.getParseInfo().getDestToLateralView().put(ctx_1.dest, ast);
            break;
        case HiveParser.TOK_CTE:
            processCTE(qb, ast);
            break;
        default:
            skipRecursion = false;
            break;
        }
    }

    if (!skipRecursion) {
        // Iterate over the rest of the children
        int child_count = ast.getChildCount();
        for (int child_pos = 0; child_pos < child_count && phase1Result; ++child_pos) {
            // Recurse
            phase1Result = phase1Result && doPhase1((ASTNode) ast.getChild(child_pos), qb, ctx_1, plannerCtx);
        }
    }
    return phase1Result;
}

From source file:com.cabserver.handler.Admin.java

@POST
@Path("bookings/manual")
@Produces(MediaType.TEXT_HTML)/*w  ww.j ava2  s  .  c o  m*/
public Response addManualBookings(String jsonData) {
    // String data = "";
    HashMap<String, String> responseMap = new HashMap<String, String>();
    try {

        /*
         * log.info("addBookings >> before decoding =" + jsonData);
         */

        jsonData = (URLDecoder.decode(jsonData, "UTF-8"));

        /*
         * log.info("addBookings >> after decoding =" + jsonData);
         */

        // jsonData = jsonData.split("=")[1];

        if (jsonData.contains("=")) {
            jsonData = jsonData.split("=")[1];
            // log.info("addBookings >> = sign found");
        }

        /*
         * log.info("addBookings >> after split =" + jsonData);
         */

        TravelMaster tm = new TravelMaster();

        if (jsonData != null && jsonData.length() > 1) {

            // Gson gson = new Gson();
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(jsonData);

            // LoginInfo result = new LoginInfo();
            String phone = (String) obj.get("phone");
            // log.info("addBookings >> travellerPhone =" + phone);

            String name = (String) obj.get("name");
            // log.info("addBookings >> travellerName =" + name);

            String from = (String) obj.get("from");
            // log.info("addBookings >> from =" + from);
            String to = (String) obj.get("to");
            // log.info("addBookings >> to =" + to);

            String noOfPassengers = (String) obj.get("noOfPassengers");
            // log.info("noOfPassengers >> to =" + noOfPassengers);
            tm.setNoOfPassengers(Integer.parseInt(noOfPassengers));

            String mobileOperator = (String) obj.get("mobileOperator");
            // log.info("mobileOperator >> to =" + mobileOperator);
            tm.setMobileOperator(mobileOperator);

            String airline = (String) obj.get("airline");
            // log.info("airline >> to =" + airline);
            tm.setAirline(airline);

            String flightNumber = (String) obj.get("flightNumber");
            // log.info("flightNumber >> to =" + flightNumber);
            tm.setFlightNumber(flightNumber);

            String driverId = (String) obj.get("driverId");
            // log.info("driverId >> to =" + driverId);
            tm.setDriverId(driverId);

            tm.setBookingType(Integer.parseInt(ConfigDetails.constants.get("MANUAL_BOOKING_TYPE")));
            tm.setActivationStatus(
                    Integer.parseInt(ConfigDetails.constants.get("MANUAL_BOOKING_DEACTIVE_STATUS")));

            String date = (String) obj.get("date");
            String month = (String) obj.get("month");
            String year = (String) obj.get("year");

            String hour = (String) obj.get("hour");
            String min = (String) obj.get("min");
            String ampm = (String) obj.get("ampm");
            Calendar cal = Calendar.getInstance();

            if (phone != null && phone.length() >= 10) {
                tm.setTravellerPhone(phone);
                long id_from_db = DatabaseManager.searchUserMasterByPhone(phone);
                if (id_from_db == 0) {

                    UserMaster um = new UserMaster();

                    um.setAdminId(1 + "");
                    um.setCompanyId(101 + "");

                    if (name.contains(" ")) {

                        String tmpNameArry[] = name.split(" ");
                        um.setFirstName(tmpNameArry[0]);
                        um.setLastName(tmpNameArry[1]);

                    } else {
                        um.setFirstName(name);
                        um.setLastName(name);
                    }

                    um.setPhone(phone);
                    um.setAge(Integer.parseInt("99"));
                    um.setSex("u");
                    um.setUid("0000");
                    um.setAddress(from);
                    um.setPassword("12345");
                    um.setRole("user");
                    UserMaster userMaster = DatabaseManager.insertUserMaster(um);
                    if (userMaster.isDbInsertStatus()) {
                        tm.setUserId(userMaster.getUserId());

                    } else {
                        // / send formward error
                        responseMap.put("code", "200");
                        responseMap.put("msg", "Customer details error.");
                    }

                } else {

                    tm.setUserId(id_from_db + "");
                }

                if (Double.parseDouble(tm.getUserId()) > 0) {
                    tm.setTravellerName(name);
                    tm.setFrom(from);
                    tm.setTo(to);

                    Map fromLatLng;
                    Map toLatLng;
                    String addressStatus = "";
                    try {

                        fromLatLng = GsonJsonParser.getLatLongtByAddress(from);
                        tm.setFromLat((String) fromLatLng.get("lat"));
                        tm.setFromLongt((String) fromLatLng.get("longt"));

                        toLatLng = GsonJsonParser.getLatLongtByAddress(to);
                        tm.setToLat((String) toLatLng.get("lat"));
                        tm.setToLongt((String) toLatLng.get("lat"));

                    } catch (Exception e) {
                        addressStatus = ConfigDetails.constants.get("INCORRECT_ADDRESS_CODE");

                        tm.setFromLat("123");
                        tm.setFromLongt("123");
                        tm.setToLat("123");
                        tm.setToLongt("123");

                        log.info("addManualBookings >> Exception =" + e.getMessage());
                    }

                    cal.set(Calendar.DATE, Integer.parseInt(date));
                    cal.set(Calendar.MONTH, (MyUtil.getMonth(month) - 1));
                    cal.set(Calendar.YEAR, Integer.parseInt(year));
                    if (ampm.equalsIgnoreCase("PM")) {
                        if (hour.equalsIgnoreCase("12")) {
                            cal.set(Calendar.HOUR_OF_DAY, 12);
                        } else {
                            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour) + 12);
                        }

                    } else {
                        if (hour.equalsIgnoreCase("12")) {
                            cal.set(Calendar.HOUR_OF_DAY, 00);
                        } else {
                            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
                        }

                    }
                    cal.set(Calendar.MINUTE, Integer.parseInt(min));

                    tm.setDateTime(cal.getTime());

                    Calendar bookingTime = Calendar.getInstance();
                    bookingTime.setTimeInMillis(tm.getDateTime().getTime());

                    log.info("addManualBookings >> Manual Booking Time = "
                            + new Date(tm.getDateTime().getTime()));

                    int bookingType = TaxiBookingQuartz.validateAdminTime(bookingTime.getTimeInMillis());
                    log.info("addManualBookings >> booking scheduleType"
                            + "(2=ManualScheduled,1=NonScheduled) = " + bookingType);

                    if (bookingType <= 0) {
                        responseMap.put("code", ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
                        responseMap.put("msg", ConfigDetails.constants.get("ADMIN_MIN_BOOKING_TIME_ERROR_MSG"));
                    } else {

                        if (addressStatus
                                .equalsIgnoreCase(ConfigDetails.constants.get("INCORRECT_ADDRESS_CODE"))) {
                            tm.setBookingStatus(ConfigDetails.constants.get("INCORRECT_ADDRESS_MSG"));
                            tm.setBookingStatusCode(addressStatus);

                        } else {
                            tm.setBookingStatusCode(ConfigDetails.constants.get("BOOKING_CONFORMED_CODE"));
                            tm.setBookingStatus(ConfigDetails.constants.get("BOOKING_CONFORMED_MSG"));
                        }

                        // TBD for 2 Hours checking logic

                        long existingBookingValue = DatabaseManager.getBookingsForaDate(driverId, cal);

                        // TBD booking activation logic

                        if (existingBookingValue == 0) {

                            TravelMaster tmFrmDB = DatabaseManager.insertTravelMaster(tm);
                            if (tmFrmDB.isDbStatus()) {

                                int bookingStatus = TaxiBookingQuartz.scheduleTaxiBookingJob(tmFrmDB);

                                log.info("addManualBookings >> bookingSchedueStatus = " + bookingStatus);

                                if (bookingStatus == 1) {
                                    // Immediate manual booking insert and
                                    // driver assign

                                    boolean manualBookingActivationStatus = false;

                                    manualBookingActivationStatus = DatabaseManager
                                            .updateManualBookingActiveStatus(tmFrmDB.getBookingId());
                                    log.info("addManualBookings >> updating manualBookingActivationStatus "
                                            + "in TravelMaster =" + manualBookingActivationStatus);
                                    manualBookingActivationStatus = DatabaseManager
                                            .assignManualBookingDriverToDriverMaster(tmFrmDB.getBookingId(),
                                                    tmFrmDB.getDriverId());
                                    log.info("addManualBookings >> updating manualBookingActivationStatus "
                                            + "in DriverMaster =" + manualBookingActivationStatus);

                                    CacheBuilder.bookingsDataMap.put(Long.parseLong(tmFrmDB.getBookingId()),
                                            tmFrmDB);

                                    responseMap.put("code", "200");
                                    responseMap.put("msg",
                                            ConfigDetails.constants.get("BOOKING_CONFORMED_MSG"));
                                    responseMap.put("travellerPhone", phone);
                                    responseMap.put("bookingId",
                                            new Double(tmFrmDB.getBookingId()).longValue() + "");

                                    new NotificationThread(tm).start();

                                    log.info("addManualBookings >> "
                                            + "Booking Added. HTTP bookingStatus code is 200. responseMap="
                                            + responseMap.toString());

                                } else if (bookingStatus == 2) {

                                    new NotificationThread(tm).start();

                                    responseMap.put("code", "200");
                                    responseMap.put("msg",
                                            ConfigDetails.constants.get("BOOKING_CONFORMED_MSG"));

                                } else if (bookingStatus <= 0) {

                                    tmFrmDB.setBookingStatusCode(
                                            ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
                                    tmFrmDB.setBookingStatus(ConfigDetails.constants.get("BOOKING_FAILED_MSG"));
                                    DatabaseManager.updateBookingStatus(tmFrmDB);

                                    responseMap.put("code", ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
                                    if (bookingStatus < 0) {
                                        responseMap.put("msg",
                                                ConfigDetails.constants.get("MIN_BOOKING_TIME_ERROR_MSG"));
                                    } else if (bookingStatus == 0) {
                                        responseMap.put("msg", "Booking scheduling error.");
                                    }

                                }

                            } else {
                                responseMap.put("code", ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
                                responseMap.put("msg", "Booking creation error.");
                            }
                        } else {
                            responseMap.put("code", ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
                            responseMap.put("msg", "Driver has a booking at " + new Date(existingBookingValue));
                        }

                    }

                } else {
                    responseMap.put("code", ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
                    responseMap.put("msg", "Server data booking details.");
                }

            } else {

                responseMap.put("code", ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
                responseMap.put("msg", "Incorrect booking details.");
            }

        } else {
            responseMap.put("code", ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
            responseMap.put("msg", "Incorrect booking data.");
        }

    } catch (Exception e) {
        responseMap.put("code", ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
        responseMap.put("msg", "Incorrect booking data.");
        e.printStackTrace();
    }

    if (responseMap.size() < 1) {

        log.info("addManualBookings >> Bookings Error. HTTP bookingStatus code is "
                + ConfigDetails.constants.get("BOOKING_FAILED_CODE") + ".");

        responseMap.put("code", ConfigDetails.constants.get("BOOKING_FAILED_CODE"));
        responseMap.put("msg", "Server Error.");
        return Response.status(200).entity(jsonCreater(responseMap)).build();
    } else {
        return Response.status(200).entity(jsonCreater(responseMap)).build();
    }

}

From source file:org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.java

/**
 * Phase 1: (including, but not limited to):
 *
 * 1. Gets all the aliases for all the tables / subqueries and makes the
 * appropriate mapping in aliasToTabs, aliasToSubq 2. Gets the location of the
 * destination and names the clause "inclause" + i 3. Creates a map from a
 * string representation of an aggregation tree to the actual aggregation AST
 * 4. Creates a mapping from the clause name to the select expression AST in
 * destToSelExpr 5. Creates a mapping from a table alias to the lateral view
 * AST's in aliasToLateralViews/*from   w  ww .  j a  va  2  s. co  m*/
 *
 * @param ast
 * @param qb
 * @param ctx_1
 * @throws SemanticException
 */
@SuppressWarnings({ "fallthrough", "nls" })
public boolean doPhase1(ASTNode ast, QB qb, Phase1Ctx ctx_1, PlannerContext plannerCtx)
        throws SemanticException {

    boolean phase1Result = true;
    QBParseInfo qbp = qb.getParseInfo();
    boolean skipRecursion = false;

    if (ast.getToken() != null) {
        skipRecursion = true;
        switch (ast.getToken().getType()) {
        case HiveParser.TOK_SELECTDI:
            qb.countSelDi();
            // fall through
        case HiveParser.TOK_SELECT:
            qb.countSel();
            qbp.setSelExprForClause(ctx_1.dest, ast);

            int posn = 0;
            if (((ASTNode) ast.getChild(0)).getToken().getType() == HiveParser.QUERY_HINT) {
                ParseDriver pd = new ParseDriver();
                String queryHintStr = ast.getChild(0).getText();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("QUERY HINT: " + queryHintStr);
                }
                try {
                    ASTNode hintNode = pd.parseHint(queryHintStr);
                    qbp.setHints(hintNode);
                    posn++;
                } catch (ParseException e) {
                    throw new SemanticException("failed to parse query hint: " + e.getMessage(), e);
                }
            }

            if ((ast.getChild(posn).getChild(0).getType() == HiveParser.TOK_TRANSFORM))
                queryProperties.setUsesScript(true);

            LinkedHashMap<String, ASTNode> aggregations = doPhase1GetAggregationsFromSelect(ast, qb,
                    ctx_1.dest);
            doPhase1GetColumnAliasesFromSelect(ast, qbp);
            qbp.setAggregationExprsForClause(ctx_1.dest, aggregations);
            qbp.setDistinctFuncExprsForClause(ctx_1.dest, doPhase1GetDistinctFuncExprs(aggregations));
            break;

        case HiveParser.TOK_WHERE:
            qbp.setWhrExprForClause(ctx_1.dest, ast);
            if (!SubQueryUtils.findSubQueries((ASTNode) ast.getChild(0)).isEmpty())
                queryProperties.setFilterWithSubQuery(true);
            break;

        case HiveParser.TOK_INSERT_INTO:
            String currentDatabase = SessionState.get().getCurrentDatabase();
            String tab_name = getUnescapedName((ASTNode) ast.getChild(0).getChild(0), currentDatabase);
            qbp.addInsertIntoTable(tab_name, ast);

        case HiveParser.TOK_DESTINATION:
            ctx_1.dest = this.ctx.getDestNamePrefix(ast).toString() + ctx_1.nextNum;
            ctx_1.nextNum++;
            boolean isTmpFileDest = false;
            if (ast.getChildCount() > 0 && ast.getChild(0) instanceof ASTNode) {
                ASTNode ch = (ASTNode) ast.getChild(0);
                if (ch.getToken().getType() == HiveParser.TOK_DIR && ch.getChildCount() > 0
                        && ch.getChild(0) instanceof ASTNode) {
                    ch = (ASTNode) ch.getChild(0);
                    isTmpFileDest = ch.getToken().getType() == HiveParser.TOK_TMP_FILE;
                } else {
                    if (ast.getToken().getType() == HiveParser.TOK_DESTINATION
                            && ast.getChild(0).getType() == HiveParser.TOK_TAB) {
                        String fullTableName = getUnescapedName((ASTNode) ast.getChild(0).getChild(0),
                                SessionState.get().getCurrentDatabase());
                        qbp.getInsertOverwriteTables().put(fullTableName.toLowerCase(), ast);
                    }
                }
            }

            // is there a insert in the subquery
            if (qbp.getIsSubQ() && !isTmpFileDest) {
                throw new SemanticException(ErrorMsg.NO_INSERT_INSUBQUERY.getMsg(ast));
            }

            qbp.setDestForClause(ctx_1.dest, (ASTNode) ast.getChild(0));
            handleInsertStatementSpecPhase1(ast, qbp, ctx_1);

            if (qbp.getClauseNamesForDest().size() == 2) {
                // From the moment that we have two destination clauses,
                // we know that this is a multi-insert query.
                // Thus, set property to right value.
                // Using qbp.getClauseNamesForDest().size() >= 2 would be
                // equivalent, but we use == to avoid setting the property
                // multiple times
                queryProperties.setMultiDestQuery(true);
            }

            if (plannerCtx != null && !queryProperties.hasMultiDestQuery()) {
                plannerCtx.setInsertToken(ast, isTmpFileDest);
            } else if (plannerCtx != null && qbp.getClauseNamesForDest().size() == 2) {
                // For multi-insert query, currently we only optimize the FROM clause.
                // Hence, introduce multi-insert token on top of it.
                // However, first we need to reset existing token (insert).
                // Using qbp.getClauseNamesForDest().size() >= 2 would be
                // equivalent, but we use == to avoid setting the property
                // multiple times
                plannerCtx.resetToken();
                plannerCtx.setMultiInsertToken((ASTNode) qbp.getQueryFrom().getChild(0));
            }
            break;

        case HiveParser.TOK_FROM:
            int child_count = ast.getChildCount();
            if (child_count != 1) {
                throw new SemanticException(generateErrorMessage(ast, "Multiple Children " + child_count));
            }

            if (!qbp.getIsSubQ()) {
                qbp.setQueryFromExpr(ast);
            }

            // Check if this is a subquery / lateral view
            ASTNode frm = (ASTNode) ast.getChild(0);
            if (frm.getToken().getType() == HiveParser.TOK_TABREF) {
                processTable(qb, frm);
            } else if (frm.getToken().getType() == HiveParser.TOK_VIRTUAL_TABLE) {
                // Create a temp table with the passed values in it then rewrite this portion of the
                // tree to be from that table.
                ASTNode newFrom = genValuesTempTable(frm, qb);
                ast.setChild(0, newFrom);
                processTable(qb, newFrom);
            } else if (frm.getToken().getType() == HiveParser.TOK_SUBQUERY) {
                processSubQuery(qb, frm);
            } else if (frm.getToken().getType() == HiveParser.TOK_LATERAL_VIEW
                    || frm.getToken().getType() == HiveParser.TOK_LATERAL_VIEW_OUTER) {
                queryProperties.setHasLateralViews(true);
                processLateralView(qb, frm);
            } else if (isJoinToken(frm)) {
                processJoin(qb, frm);
                qbp.setJoinExpr(frm);
            } else if (frm.getToken().getType() == HiveParser.TOK_PTBLFUNCTION) {
                queryProperties.setHasPTF(true);
                processPTF(qb, frm);
            }
            break;

        case HiveParser.TOK_CLUSTERBY:
            // Get the clusterby aliases - these are aliased to the entries in the
            // select list
            queryProperties.setHasClusterBy(true);
            qbp.setClusterByExprForClause(ctx_1.dest, ast);
            break;

        case HiveParser.TOK_DISTRIBUTEBY:
            // Get the distribute by aliases - these are aliased to the entries in
            // the
            // select list
            queryProperties.setHasDistributeBy(true);
            qbp.setDistributeByExprForClause(ctx_1.dest, ast);
            if (qbp.getClusterByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.CLUSTERBY_DISTRIBUTEBY_CONFLICT.getMsg()));
            } else if (qbp.getOrderByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.ORDERBY_DISTRIBUTEBY_CONFLICT.getMsg()));
            }
            break;

        case HiveParser.TOK_SORTBY:
            // Get the sort by aliases - these are aliased to the entries in the
            // select list
            queryProperties.setHasSortBy(true);
            qbp.setSortByExprForClause(ctx_1.dest, ast);
            if (qbp.getClusterByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.CLUSTERBY_SORTBY_CONFLICT.getMsg()));
            } else if (qbp.getOrderByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.ORDERBY_SORTBY_CONFLICT.getMsg()));
            }

            break;

        case HiveParser.TOK_ORDERBY:
            // Get the order by aliases - these are aliased to the entries in the
            // select list
            queryProperties.setHasOrderBy(true);
            qbp.setOrderByExprForClause(ctx_1.dest, ast);
            if (qbp.getClusterByForClause(ctx_1.dest) != null) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.CLUSTERBY_ORDERBY_CONFLICT.getMsg()));
            }
            // If there are aggregations in order by, we need to remember them in qb.
            qbp.addAggregationExprsForClause(ctx_1.dest,
                    doPhase1GetAggregationsFromSelect(ast, qb, ctx_1.dest));
            break;

        case HiveParser.TOK_GROUPBY:
        case HiveParser.TOK_ROLLUP_GROUPBY:
        case HiveParser.TOK_CUBE_GROUPBY:
        case HiveParser.TOK_GROUPING_SETS:
            // Get the groupby aliases - these are aliased to the entries in the
            // select list
            queryProperties.setHasGroupBy(true);
            if (qbp.getJoinExpr() != null) {
                queryProperties.setHasJoinFollowedByGroupBy(true);
            }
            if (qbp.getSelForClause(ctx_1.dest).getToken().getType() == HiveParser.TOK_SELECTDI) {
                throw new SemanticException(
                        generateErrorMessage(ast, ErrorMsg.SELECT_DISTINCT_WITH_GROUPBY.getMsg()));
            }
            qbp.setGroupByExprForClause(ctx_1.dest, ast);
            skipRecursion = true;

            // Rollup and Cubes are syntactic sugar on top of grouping sets
            if (ast.getToken().getType() == HiveParser.TOK_ROLLUP_GROUPBY) {
                qbp.getDestRollups().add(ctx_1.dest);
            } else if (ast.getToken().getType() == HiveParser.TOK_CUBE_GROUPBY) {
                qbp.getDestCubes().add(ctx_1.dest);
            } else if (ast.getToken().getType() == HiveParser.TOK_GROUPING_SETS) {
                qbp.getDestGroupingSets().add(ctx_1.dest);
            }
            break;

        case HiveParser.TOK_HAVING:
            qbp.setHavingExprForClause(ctx_1.dest, ast);
            qbp.addAggregationExprsForClause(ctx_1.dest,
                    doPhase1GetAggregationsFromSelect(ast, qb, ctx_1.dest));
            break;

        case HiveParser.KW_WINDOW:
            if (!qb.hasWindowingSpec(ctx_1.dest)) {
                throw new SemanticException(generateErrorMessage(ast,
                        "Query has no Cluster/Distribute By; but has a Window definition"));
            }
            handleQueryWindowClauses(qb, ctx_1, ast);
            break;

        case HiveParser.TOK_LIMIT:
            if (ast.getChildCount() == 2) {
                qbp.setDestLimit(ctx_1.dest, new Integer(ast.getChild(0).getText()),
                        new Integer(ast.getChild(1).getText()));
            } else {
                qbp.setDestLimit(ctx_1.dest, new Integer(0), new Integer(ast.getChild(0).getText()));
            }
            break;

        case HiveParser.TOK_ANALYZE:
            // Case of analyze command

            String table_name = getUnescapedName((ASTNode) ast.getChild(0).getChild(0)).toLowerCase();

            qb.setTabAlias(table_name, table_name);
            qb.addAlias(table_name);
            qb.getParseInfo().setIsAnalyzeCommand(true);
            qb.getParseInfo().setNoScanAnalyzeCommand(this.noscan);
            qb.getParseInfo().setPartialScanAnalyzeCommand(this.partialscan);
            // Allow analyze the whole table and dynamic partitions
            HiveConf.setVar(conf, HiveConf.ConfVars.DYNAMICPARTITIONINGMODE, "nonstrict");
            HiveConf.setVar(conf, HiveConf.ConfVars.HIVEMAPREDMODE, "nonstrict");

            break;

        case HiveParser.TOK_UNIONALL:
            if (!qbp.getIsSubQ()) {
                // this shouldn't happen. The parser should have converted the union to be
                // contained in a subquery. Just in case, we keep the error as a fallback.
                throw new SemanticException(generateErrorMessage(ast, ErrorMsg.UNION_NOTIN_SUBQ.getMsg()));
            }
            skipRecursion = false;
            break;

        case HiveParser.TOK_INSERT:
            ASTNode destination = (ASTNode) ast.getChild(0);
            Tree tab = destination.getChild(0);

            // Proceed if AST contains partition & If Not Exists
            if (destination.getChildCount() == 2 && tab.getChildCount() == 2
                    && destination.getChild(1).getType() == HiveParser.TOK_IFNOTEXISTS) {
                String tableName = tab.getChild(0).getChild(0).getText();

                Tree partitions = tab.getChild(1);
                int childCount = partitions.getChildCount();
                HashMap<String, String> partition = new HashMap<String, String>();
                for (int i = 0; i < childCount; i++) {
                    String partitionName = partitions.getChild(i).getChild(0).getText();
                    Tree pvalue = partitions.getChild(i).getChild(1);
                    if (pvalue == null) {
                        break;
                    }
                    String partitionVal = stripQuotes(pvalue.getText());
                    partition.put(partitionName, partitionVal);
                }
                // if it is a dynamic partition throw the exception
                if (childCount != partition.size()) {
                    throw new SemanticException(
                            ErrorMsg.INSERT_INTO_DYNAMICPARTITION_IFNOTEXISTS.getMsg(partition.toString()));
                }
                Table table = null;
                try {
                    table = this.getTableObjectByName(tableName);
                } catch (HiveException ex) {
                    throw new SemanticException(ex);
                }
                try {
                    Partition parMetaData = db.getPartition(table, partition, false);
                    // Check partition exists if it exists skip the overwrite
                    if (parMetaData != null) {
                        phase1Result = false;
                        skipRecursion = true;
                        LOG.info("Partition already exists so insert into overwrite "
                                + "skipped for partition : " + parMetaData.toString());
                        break;
                    }
                } catch (HiveException e) {
                    LOG.info("Error while getting metadata : ", e);
                }
                validatePartSpec(table, partition, (ASTNode) tab, conf, false);
            }
            skipRecursion = false;
            break;
        case HiveParser.TOK_LATERAL_VIEW:
        case HiveParser.TOK_LATERAL_VIEW_OUTER:
            // todo: nested LV
            assert ast.getChildCount() == 1;
            qb.getParseInfo().getDestToLateralView().put(ctx_1.dest, ast);
            break;
        case HiveParser.TOK_CTE:
            processCTE(qb, ast);
            break;
        default:
            skipRecursion = false;
            break;
        }
    }

    if (!skipRecursion) {
        // Iterate over the rest of the children
        int child_count = ast.getChildCount();
        for (int child_pos = 0; child_pos < child_count && phase1Result; ++child_pos) {
            // Recurse
            phase1Result = phase1Result && doPhase1((ASTNode) ast.getChild(child_pos), qb, ctx_1, plannerCtx);
        }
    }
    return phase1Result;
}

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());
    /**// w  w  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
    }

}