Package org.ofbiz.base.util

Examples of org.ofbiz.base.util.UtilTimer


          * @return String An HTML String for display.
          */
    public String displayField(UIFieldInfo fieldInfo,
        Vector entityDetailsVector, String action, int row,
        boolean isSubsection, int tabOffset) {
        UtilTimer timer = new UtilTimer();

        if (TIMER) {
            timer.timerString(3,
                "[UIWebScreenSection.displayField] Start " +
                fieldInfo.getUiAttribute().getAttributeName());
        }

        StringBuffer displayHtml = new StringBuffer();
        int maxLength;
        GenericValue genericValue = null;
        String findAttributeValue = "";
        boolean protect = false;

        Debug.logVerbose("[displayField()] getEntityName()      -> " +
                fieldInfo.getUiAttribute().getUiEntity().getEntityName(), module);
        Debug.logVerbose("[displayField()] getAttributeName()       ->" +
                fieldInfo.getUiAttribute().getAttributeName(), module);

        // If the screen is being displayed in query mode, and the current field is not searchable, don't display it.
        Debug.logVerbose("[displayField()] action               -> " + action, module);
        Debug.logVerbose("[displayField()] getIsSearchable()    -> " +
                String.valueOf(fieldInfo.getIsSearchable()), module);

        if ((action.equals(ACTION_SHOW_QUERY) ||
                action.equals(ACTION_SHOW_QUERY_REPORT) ||
                action.equals(ACTION_SHOW_REPORT)) &&
                !fieldInfo.getIsSearchable()) {
            if (TIMER) {
                timer.timerString(3,
                    "[UIWebScreenSection.displayField] in query mode and not a searchable field");
            }

            return "";
        }

        // Get the details of the field's entity.  For example, if this is the account detail screen,
        // but the current field is the city from the Address entity, this will get the address entity
        // off the vector that was passed in.
        GenericValue fieldEntityDetails = null;
        Iterator entityDetailsIterator = entityDetailsVector.iterator();

        while (entityDetailsIterator.hasNext()) {
            GenericValue testEntity = (GenericValue) entityDetailsIterator.next();

            if (testEntity.getEntityName().equals(fieldInfo.getUiAttribute()
                                                               .getUiEntity()
                                                               .getEntityName())) {
                fieldEntityDetails = testEntity;
            }
        }

        if (fieldEntityDetails == null) {
            Debug.logVerbose(
                    "[displayField()] fieldEntityDetails is null.  Assuming a null field", module);

            if (TIMER) {
                timer.timerString(3,
                    "[UIWebScreenSection.displayField] fieldEntityDetails is null.  Assuming a null field");
            }

            return "";
        }

        // Get the details of the primary entity being displayed on the screen.  For example, if this is
        // the account detail screen, this will get the account entity off the vector that was passed in.
        GenericValue primaryEntityDetails = (GenericValue) entityDetailsVector.firstElement();

        // Get the current field's value passed in from the jsp script.
        String fieldValue = (String.valueOf(fieldEntityDetails.get(
                    fieldInfo.getUiAttribute().getAttributeName())) != null)
            ? String.valueOf(fieldEntityDetails.get(fieldInfo.getUiAttribute()
                                                             .getAttributeName()))
            : "";

        if ((fieldValue == null) || fieldValue.equals("null")) {
            fieldValue = "";
        }

        Debug.logVerbose("[displayField()] fieldValue           -> " +
                fieldValue, module);

        if (fieldInfo.getUiAttribute().getUiEntity().getEntityName().equals("")) {
            // Entity name not specified.
            Debug.logWarning(
                "[UIWebScreenSection.displayField]: Entity name not specified.", module);

            if (TIMER) {
                timer.timerString(3,
                    "[UIWebScreenSection.displayField] End (entity name not specified)");
            }

            return fieldValue;
        }

        if (fieldInfo.getUiAttribute().getAttributeName().equals("")) {
            // Field name not specified.
            Debug.logWarning(
                "[UIWebScreenSection.displayField]: Field name not specified.", module);

            if (TIMER) {
                timer.timerString(3,
                    "[UIWebScreenSection.displayField] End (attribute name not specified)");
            }

            return fieldValue;
        }

        if (action.equals(ACTION_SHOW) || action.equals(ACTION_INSERT) ||
                action.equals(ACTION_UPDATE) || action.equals(ACTION_QUERY) ||
                action.equals("") || (action == null) ||
                ((fieldInfo.getIsReadOnly() ||
                !fieldInfo.getUiScreenSectionEntity().getIsUpdateable()) &&
                !action.equals(ACTION_SHOW_QUERY) &&
                !action.equals(ACTION_SHOW_QUERY_REPORT) &&
                !action.equals(ACTION_SHOW_REPORT)) ||
                ((action.equals(ACTION_SHOW_UPDATE) ||
                action.equals(ACTION_QUERY_UPDATE)) &&
                fieldInfo.getUiAttribute().getIsPk())) {
            protect = true;
        }

        // If the action is SHOW_COPY, and this is a primary key field that is auto-generated, need to clear the value
        // before displaying.  The original value will be saved as is in case the primary key of the copied record
        // is needed for custom processing later.
        // Save a copy of the original field value in case we change the field value.
        String origFieldValue = fieldValue;

        if (UIWebUtility.getIsCopiedPrimaryKey(action, fieldInfo)) {
            fieldValue = "";

            Debug.logVerbose("[displayField()] Clearing primary key field.", module);
          
        }

        String htmlName = UIWebUtility.getHtmlName(getSectionName(), fieldInfo,
                row);
        String htmlNameOriginal = UIWebUtility.getHtmlNameOriginal(getSectionName(),
                fieldInfo, row);
        int tabIndex = tabOffset + fieldInfo.getDisplayOrder();
        UIDisplayObject uiDisplayObject = fieldInfo.getUiDisplayObject();

        try {
            uiDisplayObject.loadAttributes();

            Debug.logVerbose("[displayField()] Starting to decode field " +
                    htmlName + " of type " +
                    uiDisplayObject.getDisplayTypeId(), module);

            if (uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_SELECT)) {
                // Default to the generic UIDropDown class.
                String uiDropDownClassName = "";

                if (uiDisplayObject.getAttribEntityFindMethod().equals("CUSTOM_CLASS")) {
                    // A custom class has been constructed to handle the retrieval of entities from the data base.
                    // Need to use that class instead of the plain UIDropDown class.
                    uiDropDownClassName = uiDisplayObject.getAttribEntityFindDef();
                }

                UIDropDown uiDropDown = UIUtility.getUIDropDown(uiDropDownClassName);
                displayHtml.append(uiDropDown.displayFieldHtml(
                        uiDisplayObject, protect, htmlName, fieldValue,
                        entityDetailsVector, fieldInfo, action, tabIndex,
                        getUserInfo(), getDelegator()));

                if (TIMER) {
                    timer.timerString(3,
                        "[UIWebScreenSection.displayField] End " +
                        fieldInfo.getUiAttribute().getAttributeName() +
                        " (text - protected)");
                }
            } else if (uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_SEARCH_TEXT)) {
                // Search Text field.  Searches for a value and then turns into a select field.

                /*
                        * fieldName (htmlName) , entityName (ENTITY), idName (ENTITY_VALUE_DEF)
                        <script for='firstName' event='onblur()' language='JavaScript'>sendData(this, this.form);</script>
                        <script for='firstName' event='onchange()' language='JavaScript'>searchAgain(this, this.form);</script>
                        <p id='firstNameHolder' name='firstNameHolder' >
                        <input type="text" name="firstName" id="firstName" entityName="Contact" fieldName="firstName" idName="contactId" >
                        </p>
                */

                // Default to the generic UISearchField class.
                String uiSearchFieldClassName = "";

                if (uiDisplayObject.getAttribEntityFindMethod().equals("CUSTOM_CLASS")) {
                    // A custom class has been constructed to handle the retrieval of entities from the data base.
                    // Need to use that class instead of the plain UISearchField class.
                    uiSearchFieldClassName = uiDisplayObject.getAttribEntityFindDef();
                }

                UISearchField uiSearchField = UIUtility.getUISearchField(uiSearchFieldClassName);
                displayHtml.append(uiSearchField.displayFieldHtml(
                        uiDisplayObject, protect, htmlName, fieldValue,
                        entityDetailsVector, fieldInfo, action, tabIndex,
                        getUserInfo(), getDelegator()));

                if (TIMER) {
                    timer.timerString(3,
                        "[UIWebScreenSection.displayField] End " +
                        fieldInfo.getUiAttribute().getAttributeName() +
                        " (text - protected)");
                }
            } else if (uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_TEXT)) {
                // Text field
                if (!protect) {
                    // Mode is not protected, so create a text tag.
                    displayHtml.append("<INPUT TYPE=\"TEXT\" NAME=\"" +
                        htmlName + "\" TABINDEX=\"" + String.valueOf(tabIndex) +
                        "\" ");
                    displayHtml.append(UIWebUtility.translateAttribSize(
                            uiDisplayObject.getAttribSize()));
                    displayHtml.append(UIWebUtility.translateAttribMaxLength(
                            uiDisplayObject.getAttribMaxLength(),
                            String.valueOf(fieldInfo.getMaxLength())));
                    displayHtml.append(UIWebUtility.translateAttribDisabled(
                            uiDisplayObject.getAttribDisabled()));
                    displayHtml.append(UIWebUtility.translateAttribReadOnly(
                            uiDisplayObject.getAttribReadOnly()));
                    displayHtml.append(UIWebUtility.translateAttribAlign(
                            uiDisplayObject.getAttribAlign()));
                    displayHtml.append(UIWebUtility.translateAttribClass(
                            uiDisplayObject.getAttribClass(action,
                                fieldInfo.getIsMandatory())));
                    displayHtml.append(" VALUE='" + fieldValue + "'>");

                    if (fieldInfo.getIsMandatory() &&
                            !action.equals(ACTION_SHOW_QUERY) &&
                            !action.equals(ACTION_SHOW_QUERY_REPORT) &&
                            !action.equals(ACTION_SHOW_REPORT)) {
                        displayHtml.append("*\n");
                    }

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (text)");
                    }
                } else {
                    // In protect mode.  Just create a hidden field AND show the value as text in the table cell.
                    // Create an anchor tag if the ANCHOR_HREF_DEF attribute was set in the display object.
                    displayHtml.append(UIWebUtility.displayFieldReadOnly(
                            uiDisplayObject, htmlName, fieldValue, fieldValue,
                            entityDetailsVector));

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (text - protected)");
                    }
                }
            } else if (uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_FILE)) {
                // Text field
                if (!protect) {
                    // Mode is not protected, so create a text tag.
                    displayHtml.append("<INPUT TYPE=\"FILE\" NAME=\"" +
                        htmlName + "\" TABINDEX=\"" + String.valueOf(tabIndex) +
                        "\" ");
                    displayHtml.append(UIWebUtility.translateAttribSize(
                            uiDisplayObject.getAttribSize()));
                    displayHtml.append(UIWebUtility.translateAttribMaxLength(
                            uiDisplayObject.getAttribMaxLength(),
                            String.valueOf(fieldInfo.getMaxLength())));
                    displayHtml.append(UIWebUtility.translateAttribDisabled(
                            uiDisplayObject.getAttribDisabled()));
                    displayHtml.append(UIWebUtility.translateAttribReadOnly(
                            uiDisplayObject.getAttribReadOnly()));
                    displayHtml.append(UIWebUtility.translateAttribAlign(
                            uiDisplayObject.getAttribAlign()));
                    displayHtml.append(UIWebUtility.translateAttribClass(
                            uiDisplayObject.getAttribClass(action,
                                fieldInfo.getIsMandatory())));
                    displayHtml.append(" VALUE='" + fieldValue + "'>");

                    if (fieldInfo.getIsMandatory() &&
                            !action.equals(ACTION_SHOW_QUERY) &&
                            !action.equals(ACTION_SHOW_QUERY_REPORT) &&
                            !action.equals(ACTION_SHOW_REPORT)) {
                        displayHtml.append("*\n");
                    }

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (text)");
                    }
                } else {
                    // In protect mode.  Just create a hidden field AND show the value as text in the table cell.
                    // Create an anchor tag if the ANCHOR_HREF_DEF attribute was set in the display object.
                    displayHtml.append(UIWebUtility.displayFieldReadOnly(
                            uiDisplayObject, htmlName, fieldValue, fieldValue,
                            entityDetailsVector));

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (text - protected)");
                    }
                }
            } else if (uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_CHECKBOX)) {
                // Check box
                if (!protect) {
                    // Mode is not protected, so create an input tag.
                    displayHtml.append("<INPUT TYPE=\"CHECKBOX\" NAME=\"" +
                        htmlName + "\" TABINDEX=\"" + String.valueOf(tabIndex) +
                        "\" ");

                    if (fieldValue.equals(
                                uiDisplayObject.getAttribCheckedValue())) {
                        displayHtml.append("CHECKED ");
                    }

                    displayHtml.append(UIWebUtility.translateAttribSize(
                            uiDisplayObject.getAttribSize()));
                    displayHtml.append(UIWebUtility.translateAttribDisabled(
                            uiDisplayObject.getAttribDisabled()));
                    displayHtml.append(UIWebUtility.translateAttribReadOnly(
                            uiDisplayObject.getAttribReadOnly()));
                    displayHtml.append(UIWebUtility.translateAttribClass(
                            uiDisplayObject.getAttribClass(action,
                                fieldInfo.getIsMandatory())));
                    displayHtml.append(">\n");

                    if (fieldInfo.getIsMandatory() &&
                            !action.equals(ACTION_SHOW_QUERY) &&
                            !action.equals(ACTION_SHOW_QUERY_REPORT) &&
                            !action.equals(ACTION_SHOW_REPORT)) {
                        displayHtml.append("*\n");
                    }

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (checkbox)");
                    }
                } else {
                    // In protect mode.  Just create a hidden field AND show the value as text in the table cell.
                    displayHtml.append("<INPUT TYPE=\"HIDDEN\" NAME=\"" +
                        htmlName + "\" VALUE='" + fieldValue + "'>\n");

                    if (fieldValue.equals(
                                uiDisplayObject.getAttribCheckedValue())) {
                        displayHtml.append(uiDisplayObject.getAttribCheckedDisplay());
                    } else if (fieldValue.equals(
                                uiDisplayObject.getAttribUncheckedValue())) {
                        displayHtml.append(uiDisplayObject.getAttribUncheckedDisplay());
                    } else {
                        displayHtml.append("Undefined");
                    }

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (checkbox - protected)");
                    }
                }
            } else if (uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_NUMBER) ||
                    uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_CURRENCY) ||
                    uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_PERCENT)) {
                // Number or currency field
                String formattedNumberString = fieldValue;

                if ((fieldValue != null) && !fieldValue.equals("")) {
                    NumberFormat numberFormat = null;

                    if (uiDisplayObject.getDisplayTypeId().equals("NUMBER")) {
                        numberFormat = NumberFormat.getInstance();
                    } else if (uiDisplayObject.getDisplayTypeId().equals("CURRENCY")) {
                        numberFormat = NumberFormat.getCurrencyInstance();
                    } else if (uiDisplayObject.getDisplayTypeId().equals("PERCENT")) {
                        numberFormat = NumberFormat.getPercentInstance();
                    }

                    Debug.logVerbose(
                            "[displayField()] Starting to parse number from " +
                            fieldValue, module);

                    DecimalFormat decimalParser = new DecimalFormat();
                    ParsePosition parsePosition = new ParsePosition(0);
                    Number parsedNumber = decimalParser.parse(fieldValue,
                            parsePosition);

                    Debug.logVerbose(
                            "[displayField()] Finished parsing number: " +
                            String.valueOf(parsedNumber), module);

                    formattedNumberString = numberFormat.format(parsedNumber);

                    // Change the original and current value strings to match the formatted value so a false change won't
                    // be detected when the user navigates away from this screen section.
                    origFieldValue = formattedNumberString;

                    if (!UIWebUtility.getIsCopiedPrimaryKey(action, fieldInfo)) {
                        fieldValue = formattedNumberString;
                    }
                }

                if (!protect) {
                    // Mode is not protected, so create a text tag.
                    displayHtml.append("<INPUT TYPE=\"TEXT\" NAME=\"" +
                        htmlName + "\" TABINDEX=\"" + String.valueOf(tabIndex) +
                        "\" ");
                    displayHtml.append(UIWebUtility.translateAttribSize(
                            uiDisplayObject.getAttribSize()));
                    displayHtml.append(UIWebUtility.translateAttribMaxLength(
                            uiDisplayObject.getAttribMaxLength(),
                            String.valueOf(fieldInfo.getMaxLength())));
                    displayHtml.append(UIWebUtility.translateAttribDisabled(
                            uiDisplayObject.getAttribDisabled()));
                    displayHtml.append(UIWebUtility.translateAttribReadOnly(
                            uiDisplayObject.getAttribReadOnly()));
                    displayHtml.append(UIWebUtility.translateAttribAlign(
                            uiDisplayObject.getAttribAlign()));
                    displayHtml.append(UIWebUtility.translateAttribClass(
                            uiDisplayObject.getAttribClass(action,
                                fieldInfo.getIsMandatory())));
                    displayHtml.append(" VALUE=\"" + formattedNumberString +
                        "\">");

                    if (fieldInfo.getIsMandatory() &&
                            !action.equals(ACTION_SHOW_QUERY) &&
                            !action.equals(ACTION_SHOW_QUERY_REPORT) &&
                            !action.equals(ACTION_SHOW_REPORT)) {
                        displayHtml.append("*\n");
                    }

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (number)");
                    }
                } else {
                    // In protect mode.  Just create a hidden field AND show the value as text in the table cell.
                    displayHtml.append(UIWebUtility.displayFieldReadOnly(
                            uiDisplayObject, htmlName, fieldValue,
                            formattedNumberString, entityDetailsVector));

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (number - protected)");
                    }
                }
            } else if (uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_DATE) ||
                    uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_TIME) ||
                    uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_DATETIME)) {
                // Date, DateTime, or Time field
                String formattedDateString = fieldValue;

                if ((fieldValue != null) && !fieldValue.equals("")) {
                    Debug.logVerbose(
                            "-->[UIFieldsInfo.displayField()] Field value is not null.", module);

                    SimpleDateFormat dateFormat = null;

                    // Try to parse as an sql DATETIME column.
                    String parseMask = "yyyy-MM-dd HH:mm:ss.S";

                    Debug.logVerbose(
                            "[displayField()] Starting to parse date from " +
                            fieldValue + " using mask " + parseMask, module);

                    SimpleDateFormat dateParser = new SimpleDateFormat(parseMask);
                    ParsePosition parsePosition = new ParsePosition(0);
                    Date parsedDate = dateParser.parse(fieldValue, parsePosition);

                    if (parsedDate == null) {
                        // Try to parse as an sql DATE column.
                        parseMask = "yyyy-MM-dd";

                        Debug.logVerbose(
                                "[displayField()] Starting to parse date from " +
                                fieldValue + " using mask " + parseMask, module);

                        dateParser = new SimpleDateFormat(parseMask);
                        parsedDate = dateParser.parse(fieldValue, parsePosition);
                    }

                    Debug.logVerbose("-->[UIFieldsInfo.displayField()] Finished parsing date.", module);

                    if (parsedDate != null) {
                         Debug.logVerbose("-->[UIFieldsInfo.displayField()] Parsed date is not null.", module);
                        if (uiDisplayObject.getDisplayTypeId().equals("DATE")) {
                            if (uiDisplayObject.getAttribDisplayMask().equals("")) {
                                // Use system default date mask.
                                Debug.logVerbose(
                                        "[displayField()] DATE: using standard date format.", module);

                                dateFormat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT);
                            } else {
                                // Use display mask specified in the display object.
                                Debug.logVerbose(
                                        "[displayField()] DATE: using display mask - \"" +
                                        uiDisplayObject.getAttribDisplayMask() +
                                        "\"", module);

                                dateFormat = new SimpleDateFormat(uiDisplayObject.getAttribDisplayMask());
                            }
                        } else if (uiDisplayObject.getDisplayTypeId().equals("TIME")) {
                            if (uiDisplayObject.getAttribDisplayMask().equals("")) {
                                // Use system default date mask.
                                Debug.logVerbose(
                                        "[displayField()] TIME: using standard time format.", module);

                                dateFormat = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.SHORT);
                            } else {
                                // Use display mask specified in the display object.
                                Debug.logVerbose(
                                        "[displayField()] TIME: using display mask - \"" +
                                        uiDisplayObject.getAttribDisplayMask() +
                                        "\"", module);

                                dateFormat = new SimpleDateFormat(uiDisplayObject.getAttribDisplayMask());
                            }
                        } else if (uiDisplayObject.getDisplayTypeId().equals("DATETIME")) {
                            if (uiDisplayObject.getAttribDisplayMask().equals("")) {
                                // Use system default date mask.
                                Debug.logVerbose(
                                        "[displayField()] DATETIME: using standard datetime format.", module);

                                dateFormat = (SimpleDateFormat) DateFormat.getDateTimeInstance();
                            } else {
                                // Use display mask specified in the display object.
                                Debug.logVerbose(
                                        "[displayField()] DATETIME: using display mask - \"" +
                                        uiDisplayObject.getAttribDisplayMask() +
                                        "\"", module);

                                dateFormat = new SimpleDateFormat(uiDisplayObject.getAttribDisplayMask());
                            }
                        }

                        formattedDateString = dateFormat.format(parsedDate);
                    } else {
                        Debug.logVerbose("-->[UIFieldsInfo.displayField()] Parsed date is null.", module);

                        formattedDateString = "";
                    }

                    // Change the original and current value strings to match the formatted value so a false change
                    // won't be detected when the user navigates away from this screen section.
                    origFieldValue = formattedDateString;

                    if (!UIWebUtility.getIsCopiedPrimaryKey(action, fieldInfo)) {
                        fieldValue = formattedDateString;
                    }
                }

                if (!protect) {
                    // Mode is not protected, so create a text tag.
                    displayHtml.append("<INPUT TYPE=\"TEXT\" NAME=\"" +
                        htmlName + "\" TABINDEX=\"" + String.valueOf(tabIndex) +
                        "\" ");
                    displayHtml.append(UIWebUtility.translateAttribSize(
                            uiDisplayObject.getAttribSize()));
                    displayHtml.append(UIWebUtility.translateAttribMaxLength(
                            uiDisplayObject.getAttribMaxLength(),
                            String.valueOf(fieldInfo.getMaxLength())));
                    displayHtml.append(UIWebUtility.translateAttribDisabled(
                            uiDisplayObject.getAttribDisabled()));
                    displayHtml.append(UIWebUtility.translateAttribReadOnly(
                            uiDisplayObject.getAttribReadOnly()));
                    displayHtml.append(UIWebUtility.translateAttribAlign(
                            uiDisplayObject.getAttribAlign()));
                    displayHtml.append(UIWebUtility.translateAttribClass(
                            uiDisplayObject.getAttribClass(action,
                                fieldInfo.getIsMandatory())));
                    displayHtml.append(" VALUE=\"" + formattedDateString +
                        "\">\n");

                    if (uiDisplayObject.getDisplayTypeId().equals("DATE") ||
                            uiDisplayObject.getDisplayTypeId().equals("DATETIME")) {
                        boolean isDateTime = true;

                        if (uiDisplayObject.getDisplayTypeId().equals("DATE")) {
                            isDateTime = false;
                        }

                        String formName = "";
                        String fieldName = "";

                        if (isSubsection) {
                            // This section is being displayed as a subsection, so there isn't a form
                            // for just this section.  Need to use the one called "compositeForm".
                            formName = "document." +
                                UIWebUtility.HTML_COMPOSITE_FORM_NAME;
                        } else {
                            // This section is being alone, so a form is being generated by this class.
                            // Use the form with the section name in it.
                            formName = "document." + getSectionName() + "Form";
                        }

                        fieldName = formName + "." + htmlName;

                        // Use onfocus and onblur events to set variable to make the onBeforeUnload
                        // script skip the popup message.
                        displayHtml.append(" <A\n" + "   TABINDEX=\"" +
                            String.valueOf(tabIndex) + "\"\n" +
                            "   TARGET=\"_self\"\n " +
                            "   HREF=\"javascript:show_calendar('" + fieldName +
                            "', " + fieldName + ".value, " +
                            (isDateTime ? "1" : "0") + ");\"\n" +
                            "   onfocus=\"checkForChanges = false;\"\n" +
                            "   onblur=\"checkForChanges = true;\"\n" + " >\n");

                        displayHtml.append(
                            "  <IMG SRC='/sfaimages/cal.gif' WIDTH='16' HEIGHT='16' BORDER='0' " +
                            "ALT='Click here for calendar'>\n");
                        displayHtml.append(" </A>\n");
                    }

                    if (fieldInfo.getIsMandatory() &&
                            !action.equals(ACTION_SHOW_QUERY) &&
                            !action.equals(ACTION_SHOW_QUERY_REPORT) &&
                            !action.equals(ACTION_SHOW_REPORT)) {
                        displayHtml.append("*\n");
                    }

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (date)");
                    }
                } else {
                    // In protect mode.  Just create a hidden field AND show the value as text in the table cell.
                    displayHtml.append(UIWebUtility.displayFieldReadOnly(
                            uiDisplayObject, htmlName, fieldValue,
                            formattedDateString, entityDetailsVector));

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (date - protected)");
                    }
                }
            } else if (uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_TEXTAREA)) {
                // Text field
                if (!protect) {
                    // Mode is not protected, so create a text tag.
                    displayHtml.append("<TEXTAREA NAME=\"" + htmlName +
                        "\" TABINDEX=\"" + String.valueOf(tabIndex) + "\" ");
                    displayHtml.append(UIWebUtility.translateAttribCols(
                            uiDisplayObject.getAttribCols()));
                    displayHtml.append(UIWebUtility.translateAttribRows(
                            uiDisplayObject.getAttribRows()));
                    displayHtml.append(UIWebUtility.translateAttribDisabled(
                            uiDisplayObject.getAttribDisabled()));
                    displayHtml.append(UIWebUtility.translateAttribReadOnly(
                            uiDisplayObject.getAttribReadOnly()));
                    displayHtml.append(UIWebUtility.translateAttribAlign(
                            uiDisplayObject.getAttribAlign()));
                    displayHtml.append(UIWebUtility.translateAttribWrap(
                            uiDisplayObject.getAttribWrap()));
                    displayHtml.append(UIWebUtility.translateAttribClass(
                            uiDisplayObject.getAttribClass(action,
                                fieldInfo.getIsMandatory())));
                    displayHtml.append(">" + fieldValue + "</TEXTAREA>\n");

                    if (fieldInfo.getIsMandatory() &&
                            !action.equals(ACTION_SHOW_QUERY) &&
                            !action.equals(ACTION_SHOW_QUERY_REPORT) &&
                            !action.equals(ACTION_SHOW_REPORT)) {
                        displayHtml.append("*\n");
                    }

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (text area)");
                    }
                } else {
                    // In protect mode.  Just create a hidden field AND show the value as text in the table cell.
                    displayHtml.append(UIWebUtility.displayFieldReadOnly(
                            uiDisplayObject, htmlName, fieldValue, StringHelper.textToHtml(fieldValue),
                            entityDetailsVector));

                    if (TIMER) {
                        timer.timerString(3,
                            "[UIWebScreenSection.displayField] End " +
                            fieldInfo.getUiAttribute().getAttributeName() +
                            " (protected text area)");
                    }
                }
            } else if (uiDisplayObject.getDisplayTypeId().equals(UIDisplayObject.DISPLAY_TYPE_HIDDEN)) {
                // Hidden field
                displayHtml.append("<INPUT TYPE=\"HIDDEN\" NAME=\"" + htmlName +
                    "\" VALUE='" + fieldValue + "'>\n");

                if (TIMER) {
                    timer.timerString(3,
                        "[UIWebScreenSection.displayField] End " +
                        fieldInfo.getUiAttribute().getAttributeName() +
                        " (hidden field)");
                }
            } else {
                // Unknown display type. Use the plain value.
                if (TIMER) {
                    timer.timerString(3,
                        "[UIWebScreenSection.displayField] End " +
                        fieldInfo.getUiAttribute().getAttributeName() +
                        " (unknown display type)");
                }

View Full Code Here


        }
        if (rname.indexOf('/') > 0) {
            rname = rname.substring(0, rname.indexOf('/'));
        }

        UtilTimer timer = null;
        if (Debug.timingOn()) {
            timer = new UtilTimer();
            timer.setLog(true);
            timer.timerString("[" + rname + "(Domain:" + request.getScheme() + "://" + request.getServerName() + ")] Request Begun, encoding=[" + charset + "]", module);
        }

        // Setup the CONTROL_PATH for JSP dispatching.
        String contextPath = request.getContextPath();
        if (contextPath == null || "/".equals(contextPath)) {
            contextPath = "";
        }
        request.setAttribute("_CONTROL_PATH_", contextPath + request.getServletPath());
        if (Debug.verboseOn())
            Debug.logVerbose("Control Path: " + request.getAttribute("_CONTROL_PATH_"), module);

        // for convenience, and necessity with event handlers, make security and delegator available in the request:
        // try to get it from the session first so that we can have a delegator/dispatcher/security for a certain user if desired
        Delegator delegator = null;
        String delegatorName = (String) session.getAttribute("delegatorName");
        if (UtilValidate.isNotEmpty(delegatorName)) {
            delegator = DelegatorFactory.getDelegator(delegatorName);
        }
        if (delegator == null) {
            delegator = (Delegator) getServletContext().getAttribute("delegator");
        }
        if (delegator == null) {
            Debug.logError("[ControlServlet] ERROR: delegator not found in ServletContext", module);
        } else {
            request.setAttribute("delegator", delegator);
            // always put this in the session too so that session events can use the delegator
            session.setAttribute("delegatorName", delegator.getDelegatorName());
        }

        LocalDispatcher dispatcher = (LocalDispatcher) session.getAttribute("dispatcher");
        if (dispatcher == null) {
            dispatcher = (LocalDispatcher) getServletContext().getAttribute("dispatcher");
        }
        if (dispatcher == null) {
            Debug.logError("[ControlServlet] ERROR: dispatcher not found in ServletContext", module);
        }
        request.setAttribute("dispatcher", dispatcher);

        Authorization authz = (Authorization) session.getAttribute("authz");
        if (authz == null) {
            authz = (Authorization) getServletContext().getAttribute("authz");
        }
        if (authz == null) {
            Debug.logError("[ControlServlet] ERROR: authorization not found in ServletContext", module);
        }
        request.setAttribute("authz", authz); // maybe we should also add the value to 'security'

        Security security = (Security) session.getAttribute("security");
        if (security == null) {
            security = (Security) getServletContext().getAttribute("security");
        }
        if (security == null) {
            Debug.logError("[ControlServlet] ERROR: security not found in ServletContext", module);
        }
        request.setAttribute("security", security);

        request.setAttribute("_REQUEST_HANDLER_", requestHandler);
       
        ServletContextHashModel ftlServletContext = new ServletContextHashModel(this, BeansWrapper.getDefaultInstance());
        request.setAttribute("ftlServletContext", ftlServletContext);

        // setup some things that should always be there
        UtilHttp.setInitialRequestInfo(request);
        VisitHandler.getVisitor(request, response);

        // set the Entity Engine user info if we have a userLogin
        String visitId = VisitHandler.getVisitId(session);
        if (UtilValidate.isNotEmpty(visitId)) {
            GenericDelegator.pushSessionIdentifier(visitId);
        }

        // display details on the servlet objects
        if (Debug.verboseOn()) {
            logRequestInfo(request);
        }

        // some containers call filters on EVERY request, even forwarded ones, so let it know that it came from the control servlet
        request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, Boolean.TRUE);

        String errorPage = null;
        try {
            // the ServerHitBin call for the event is done inside the doRequest method
            requestHandler.doRequest(request, response, null, userLogin, delegator);
        } catch (RequestHandlerException e) {
            Throwable throwable = e.getNested() != null ? e.getNested() : e;
            Debug.logError(throwable, "Error in request handler: ", module);
            StringUtil.HtmlEncoder encoder = new StringUtil.HtmlEncoder();
            request.setAttribute("_ERROR_MESSAGE_", encoder.encode(throwable.toString()));
            errorPage = requestHandler.getDefaultErrorPage(request);
        } catch (Exception e) {
            Debug.logError(e, "Error in request handler: ", module);
            StringUtil.HtmlEncoder encoder = new StringUtil.HtmlEncoder();
            request.setAttribute("_ERROR_MESSAGE_", encoder.encode(e.toString()));
            errorPage = requestHandler.getDefaultErrorPage(request);
        }

        // Forward to the JSP
        // if (Debug.infoOn()) Debug.logInfo("[" + rname + "] Event done, rendering page: " + nextPage, module);
        // if (Debug.timingOn()) timer.timerString("[" + rname + "] Event done, rendering page: " + nextPage, module);

        if (errorPage != null) {
            Debug.logError("An error occurred, going to the errorPage: " + errorPage, module);

            RequestDispatcher rd = request.getRequestDispatcher(errorPage);

            // use this request parameter to avoid infinite looping on errors in the error page...
            if (request.getAttribute("_ERROR_OCCURRED_") == null && rd != null) {
                request.setAttribute("_ERROR_OCCURRED_", Boolean.TRUE);
                Debug.logError("Including errorPage: " + errorPage, module);

                // NOTE DEJ20070727 after having trouble with all of these, try to get the page out and as a last resort just send something back
                try {
                    rd.include(request, response);
                } catch (Throwable t) {
                    Debug.logWarning("Error while trying to send error page using rd.include (will try response.getOutputStream or response.getWriter): " + t.toString(), module);

                    String errorMessage = "ERROR rendering error page [" + errorPage + "], but here is the error text: " + request.getAttribute("_ERROR_MESSAGE_");
                    try {
                        if (UtilJ2eeCompat.useOutputStreamNotWriter(getServletContext())) {
                            response.getOutputStream().print(errorMessage);
                        } else {
                            response.getWriter().print(errorMessage);
                        }
                    } catch (Throwable t2) {
                        try {
                            int errorToSend = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
                            Debug.logWarning("Error while trying to write error message using response.getOutputStream or response.getWriter: " + t.toString() + "; sending error code [" + errorToSend + "], and message [" + errorMessage + "]", module);
                            response.sendError(errorToSend, errorMessage);
                        } catch (Throwable t3) {
                            // wow, still bad... just throw an IllegalStateException with the message and let the servlet container handle it
                            throw new IllegalStateException(errorMessage);
                        }
                    }
                }

            } else {
                if (rd == null) {
                    Debug.logError("Could not get RequestDispatcher for errorPage: " + errorPage, module);
                }

                String errorMessage = "<html><body>ERROR in error page, (infinite loop or error page not found with name [" + errorPage + "]), but here is the text just in case it helps you: " + request.getAttribute("_ERROR_MESSAGE_") + "</body></html>";
                if (UtilJ2eeCompat.useOutputStreamNotWriter(getServletContext())) {
                    response.getOutputStream().print(errorMessage);
                } else {
                    response.getWriter().print(errorMessage);
                }
            }
        }

        // sanity check: make sure we don't have any transactions in place
        try {
            // roll back current TX first
            if (TransactionUtil.isTransactionInPlace()) {
                Debug.logWarning("*** NOTICE: ControlServlet finished w/ a transaction in place! Rolling back.", module);
                TransactionUtil.rollback();
            }

            // now resume/rollback any suspended txs
            if (TransactionUtil.suspendedTransactionsHeld()) {
                int suspended = TransactionUtil.cleanSuspendedTransactions();
                Debug.logWarning("Resumed/Rolled Back [" + suspended + "] transactions.", module);
            }
        } catch (GenericTransactionException e) {
            Debug.logWarning(e, module);
        }

        // run these two again before the ServerHitBin.countRequest call because on a logout this will end up creating a new visit
        if (response.isCommitted() && request.getSession(false) == null) {
            // response committed and no session, and we can't get a new session, what to do!
            // without a session we can't log the hit, etc; so just do nothing; this should NOT happen much!
            Debug.logError("Error in ControlServlet output where response isCommitted and there is no session (probably because of a logout); not saving ServerHit/Bin information because there is no session and as the response isCommitted we can't get a new one. The output was successful, but we just can't save ServerHit/Bin info.", module);
        } else {
            try {
                UtilHttp.setInitialRequestInfo(request);
                VisitHandler.getVisitor(request, response);
                if (requestHandler.trackStats(request)) {
                    ServerHitBin.countRequest(webappName + "." + rname, request, requestStartTime, System.currentTimeMillis() - requestStartTime, userLogin);
                }
            } catch (Throwable t) {
                Debug.logError(t, "Error in ControlServlet saving ServerHit/Bin information; the output was successful, but can't save this tracking information. The error was: " + t.toString(), module);
            }
        }
        if (Debug.timingOn()) timer.timerString("[" + rname + "(Domain:" + request.getScheme() + "://" + request.getServerName() + ")] Request Done", module);

        // sanity check 2: make sure there are no user or session infos in the delegator, ie clear the thread
        GenericDelegator.clearUserIdentifierStack();
        GenericDelegator.clearSessionIdentifierStack();
    }
View Full Code Here

                    entityCache = new HashMap<String, ModelEntity>();
                    List<ModelViewEntity> tempViewEntityList = FastList.newInstance();
                    List<Element> tempExtendEntityElementList = FastList.newInstance();

                    UtilTimer utilTimer = new UtilTimer();

                    for (ResourceHandler entityResourceHandler: entityResourceHandlers) {

                        // utilTimer.timerString("Before getDocument in file " + entityFileName);
                        Document document = null;

                        try {
                            document = entityResourceHandler.getDocument();
                        } catch (GenericConfigException e) {
                            throw new GenericEntityConfException("Error getting document from resource handler", e);
                        }
                        if (document == null) {
                            throw new GenericEntityConfException("Could not get document for " + entityResourceHandler.toString());
                        }

                        // utilTimer.timerString("Before getDocumentElement in " + entityResourceHandler.toString());
                        Element docElement = document.getDocumentElement();

                        if (docElement == null) {
                            return null;
                        }
                        docElement.normalize();
                        Node curChild = docElement.getFirstChild();

                        ModelInfo def = new ModelInfo();
                        def.populateFromElements(docElement);
                        int i = 0;

                        if (curChild != null) {
                            utilTimer.timerString("Before start of entity loop in " + entityResourceHandler.toString());
                            do {
                                boolean isEntity = "entity".equals(curChild.getNodeName());
                                boolean isViewEntity = "view-entity".equals(curChild.getNodeName());
                                boolean isExtendEntity = "extend-entity".equals(curChild.getNodeName());

                                if ((isEntity || isViewEntity) && curChild.getNodeType() == Node.ELEMENT_NODE) {
                                    i++;
                                    ModelEntity modelEntity = buildEntity(entityResourceHandler, (Element) curChild, i, def);
                                    // put the view entity in a list to get ready for the second pass to populate fields...
                                    if (isViewEntity) tempViewEntityList.add((ModelViewEntity) modelEntity);
                                } else if (isExtendEntity && curChild.getNodeType() == Node.ELEMENT_NODE) {
                                    tempExtendEntityElementList.add((Element) curChild);
                                }
                            } while ((curChild = curChild.getNextSibling()) != null);
                        } else {
                            Debug.logWarning("No child nodes found.", module);
                        }
                        utilTimer.timerString("Finished " + entityResourceHandler.toString() + " - Total Entities: " + i + " FINISHED");
                    }

                    // all entity elements in, now go through extend-entity elements and add their stuff
                    for (Element extendEntityElement: tempExtendEntityElementList) {
                        String entityName = UtilXml.checkEmpty(extendEntityElement.getAttribute("entity-name"));
View Full Code Here

        }
        if (rname.indexOf('/') > 0) {
            rname = rname.substring(0, rname.indexOf('/'));
        }

        UtilTimer timer = null;
        if (Debug.timingOn()) {
            timer = new UtilTimer();
            timer.setLog(true);
            timer.timerString("[" + rname + "] Request Begun, encoding=[" + charset + "]", module);
        }

        // Setup the CONTROL_PATH for JSP dispatching.
        String contextPath = request.getContextPath();
        if (contextPath == null || "/".equals(contextPath)) {
            contextPath = "";
        }
        request.setAttribute("_CONTROL_PATH_", contextPath + request.getServletPath());
        if (Debug.verboseOn())
            Debug.logVerbose("Control Path: " + request.getAttribute("_CONTROL_PATH_"), module);

        // for convenience, and necessity with event handlers, make security and delegator available in the request:
        // try to get it from the session first so that we can have a delegator/dispatcher/security for a certain user if desired
        Delegator delegator = null;
        String delegatorName = (String) session.getAttribute("delegatorName");
        if (UtilValidate.isNotEmpty(delegatorName)) {
            delegator = DelegatorFactory.getDelegator(delegatorName);
        }
        if (delegator == null) {
            delegator = (Delegator) getServletContext().getAttribute("delegator");
        }
        if (delegator == null) {
            Debug.logError("[ControlServlet] ERROR: delegator not found in ServletContext", module);
        } else {
            request.setAttribute("delegator", delegator);
            // always put this in the session too so that session events can use the delegator
            session.setAttribute("delegatorName", delegator.getDelegatorName());
        }

        LocalDispatcher dispatcher = (LocalDispatcher) session.getAttribute("dispatcher");
        if (dispatcher == null) {
            dispatcher = (LocalDispatcher) getServletContext().getAttribute("dispatcher");
        }
        if (dispatcher == null) {
            Debug.logError("[ControlServlet] ERROR: dispatcher not found in ServletContext", module);
        }
        request.setAttribute("dispatcher", dispatcher);

        Authorization authz = (Authorization) session.getAttribute("authz");
        if (authz == null) {
            authz = (Authorization) getServletContext().getAttribute("authz");
        }
        if (authz == null) {
            Debug.logError("[ControlServlet] ERROR: authorization not found in ServletContext", module);
        }
        request.setAttribute("authz", authz); // maybe we should also add the value to 'security'

        Security security = (Security) session.getAttribute("security");
        if (security == null) {
            security = (Security) getServletContext().getAttribute("security");
        }
        if (security == null) {
            Debug.logError("[ControlServlet] ERROR: security not found in ServletContext", module);
        }
        request.setAttribute("security", security);

        request.setAttribute("_REQUEST_HANDLER_", requestHandler);

        ServletContextHashModel ftlServletContext = new ServletContextHashModel(this, BeansWrapper.getDefaultInstance());
        request.setAttribute("ftlServletContext", ftlServletContext);

        // setup some things that should always be there
        UtilHttp.setInitialRequestInfo(request);
        VisitHandler.getVisitor(request, response);

        // set the Entity Engine user info if we have a userLogin
        String visitId = VisitHandler.getVisitId(session);
        if (UtilValidate.isNotEmpty(visitId)) {
            GenericDelegator.pushSessionIdentifier(visitId);
        }

        // display details on the servlet objects
        if (Debug.verboseOn()) {
            logRequestInfo(request);
        }

        // some containers call filters on EVERY request, even forwarded ones, so let it know that it came from the control servlet
        request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, Boolean.TRUE);

        String errorPage = null;
        try {
            // the ServerHitBin call for the event is done inside the doRequest method
            requestHandler.doRequest(request, response, null, userLogin, delegator);
        } catch (RequestHandlerException e) {
            Throwable throwable = e.getNested() != null ? e.getNested() : e;
            if (throwable instanceof IOException) {
                // when an IOException occurs (most of the times caused by the browser window being closed before the request is completed)
                // the connection with the browser is lost and so there is no need to serve the error page; a message is logged to record the event
                if (Debug.warningOn()) Debug.logWarning("Communication error with the client while processing the request: " + request.getAttribute("_CONTROL_PATH_") + request.getPathInfo(), module);
                if (Debug.verboseOn()) Debug.logVerbose(throwable, module);
            } else {
                Debug.logError(throwable, "Error in request handler: ", module);
                StringUtil.HtmlEncoder encoder = new StringUtil.HtmlEncoder();
                request.setAttribute("_ERROR_MESSAGE_", encoder.encode(throwable.toString()));
                errorPage = requestHandler.getDefaultErrorPage(request);
            }
        } catch (Exception e) {
            Debug.logError(e, "Error in request handler: ", module);
            StringUtil.HtmlEncoder encoder = new StringUtil.HtmlEncoder();
            request.setAttribute("_ERROR_MESSAGE_", encoder.encode(e.toString()));
            errorPage = requestHandler.getDefaultErrorPage(request);
        }

        // Forward to the JSP
        // if (Debug.infoOn()) Debug.logInfo("[" + rname + "] Event done, rendering page: " + nextPage, module);
        // if (Debug.timingOn()) timer.timerString("[" + rname + "] Event done, rendering page: " + nextPage, module);

        if (errorPage != null) {
            Debug.logError("An error occurred, going to the errorPage: " + errorPage, module);

            RequestDispatcher rd = request.getRequestDispatcher(errorPage);

            // use this request parameter to avoid infinite looping on errors in the error page...
            if (request.getAttribute("_ERROR_OCCURRED_") == null && rd != null) {
                request.setAttribute("_ERROR_OCCURRED_", Boolean.TRUE);
                Debug.logError("Including errorPage: " + errorPage, module);

                // NOTE DEJ20070727 after having trouble with all of these, try to get the page out and as a last resort just send something back
                try {
                    rd.include(request, response);
                } catch (Throwable t) {
                    Debug.logWarning("Error while trying to send error page using rd.include (will try response.getOutputStream or response.getWriter): " + t.toString(), module);

                    String errorMessage = "ERROR rendering error page [" + errorPage + "], but here is the error text: " + request.getAttribute("_ERROR_MESSAGE_");
                    try {
                        if (UtilJ2eeCompat.useOutputStreamNotWriter(getServletContext())) {
                            response.getOutputStream().print(errorMessage);
                        } else {
                            response.getWriter().print(errorMessage);
                        }
                    } catch (Throwable t2) {
                        try {
                            int errorToSend = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
                            Debug.logWarning("Error while trying to write error message using response.getOutputStream or response.getWriter: " + t.toString() + "; sending error code [" + errorToSend + "], and message [" + errorMessage + "]", module);
                            response.sendError(errorToSend, errorMessage);
                        } catch (Throwable t3) {
                            // wow, still bad... just throw an IllegalStateException with the message and let the servlet container handle it
                            throw new IllegalStateException(errorMessage);
                        }
                    }
                }

            } else {
                if (rd == null) {
                    Debug.logError("Could not get RequestDispatcher for errorPage: " + errorPage, module);
                }

                String errorMessage = "<html><body>ERROR in error page, (infinite loop or error page not found with name [" + errorPage + "]), but here is the text just in case it helps you: " + request.getAttribute("_ERROR_MESSAGE_") + "</body></html>";
                if (UtilJ2eeCompat.useOutputStreamNotWriter(getServletContext())) {
                    response.getOutputStream().print(errorMessage);
                } else {
                    response.getWriter().print(errorMessage);
                }
            }
        }

        // sanity check: make sure we don't have any transactions in place
        try {
            // roll back current TX first
            if (TransactionUtil.isTransactionInPlace()) {
                Debug.logWarning("*** NOTICE: ControlServlet finished w/ a transaction in place! Rolling back.", module);
                TransactionUtil.rollback();
            }

            // now resume/rollback any suspended txs
            if (TransactionUtil.suspendedTransactionsHeld()) {
                int suspended = TransactionUtil.cleanSuspendedTransactions();
                Debug.logWarning("Resumed/Rolled Back [" + suspended + "] transactions.", module);
            }
        } catch (GenericTransactionException e) {
            Debug.logWarning(e, module);
        }

        // run these two again before the ServerHitBin.countRequest call because on a logout this will end up creating a new visit
        if (response.isCommitted() && request.getSession(false) == null) {
            // response committed and no session, and we can't get a new session, what to do!
            // without a session we can't log the hit, etc; so just do nothing; this should NOT happen much!
            Debug.logError("Error in ControlServlet output where response isCommitted and there is no session (probably because of a logout); not saving ServerHit/Bin information because there is no session and as the response isCommitted we can't get a new one. The output was successful, but we just can't save ServerHit/Bin info.", module);
        } else {
            try {
                UtilHttp.setInitialRequestInfo(request);
                VisitHandler.getVisitor(request, response);
                if (requestHandler.trackStats(request)) {
                    ServerHitBin.countRequest(webappName + "." + rname, request, requestStartTime, System.currentTimeMillis() - requestStartTime, userLogin);
                }
            } catch (Throwable t) {
                Debug.logError(t, "Error in ControlServlet saving ServerHit/Bin information; the output was successful, but can't save this tracking information. The error was: " + t.toString(), module);
            }
        }
        if (Debug.timingOn()) timer.timerString("[" + rname + "] Request Done", module);

        // sanity check 2: make sure there are no user or session infos in the delegator, ie clear the thread
        GenericDelegator.clearUserIdentifierStack();
        GenericDelegator.clearSessionIdentifierStack();
    }
View Full Code Here

    public void checkDb(Map<String, ModelEntity> modelEntities, List<String> colWrongSize, List<String> messages, boolean checkPks, boolean checkFks, boolean checkFkIdx, boolean addMissing) {
        if (isLegacy) {
            throw new RuntimeException("Cannot run checkDb on a legacy database connection; configure a database helper (entityengine.xml)");
        }
        UtilTimer timer = new UtilTimer();
        timer.timerString("Start - Before Get Database Meta Data");

        // get ALL tables from this database
        TreeSet<String> tableNames = this.getTableNames(messages);
        TreeSet<String> fkTableNames = tableNames == null ? null : new TreeSet<String>(tableNames);
        TreeSet<String> indexTableNames = tableNames == null ? null : new TreeSet<String>(tableNames);

        if (tableNames == null) {
            String message = "Could not get table name information from the database, aborting.";
            if (messages != null) messages.add(message);
            Debug.logError(message, module);
            return;
        }
        timer.timerString("After Get All Table Names");

        // get ALL column info, put into hashmap by table name
        Map<String, Map<String, ColumnCheckInfo>> colInfo = this.getColumnInfo(tableNames, checkPks, messages);
        if (colInfo == null) {
            String message = "Could not get column information from the database, aborting.";
            if (messages != null) messages.add(message);
            Debug.logError(message, module);
            return;
        }
        timer.timerString("After Get All Column Info");

        // -make sure all entities have a corresponding table
        // -list all tables that do not have a corresponding entity
        // -display message if number of table columns does not match number of entity fields
        // -list all columns that do not have a corresponding field
        // -make sure each corresponding column is of the correct type
        // -list all fields that do not have a corresponding column

        timer.timerString("Before Individual Table/Column Check");

        ArrayList<ModelEntity> modelEntityList = new ArrayList<ModelEntity>(modelEntities.values());
        // sort using compareTo method on ModelEntity
        Collections.sort(modelEntityList);
        int curEnt = 0;
        int totalEnt = modelEntityList.size();
        List<ModelEntity> entitiesAdded = FastList.newInstance();
        for (ModelEntity entity: modelEntityList) {
            curEnt++;

            // if this is a view entity, do not check it...
            if (entity instanceof ModelViewEntity) {
                String entMessage = "(" + timer.timeSinceLast() + "ms) NOT Checking #" + curEnt + "/" + totalEnt + " View Entity " + entity.getEntityName();
                Debug.logVerbose(entMessage, module);
                if (messages != null) messages.add(entMessage);
                continue;
            }

            String entMessage = "(" + timer.timeSinceLast() + "ms) Checking #" + curEnt + "/" + totalEnt +
                " Entity " + entity.getEntityName() + " with table " + entity.getTableName(datasourceInfo);

            Debug.logVerbose(entMessage, module);
            if (messages != null) messages.add(entMessage);

            // -make sure all entities have a corresponding table
            if (tableNames.contains(entity.getTableName(datasourceInfo))) {
                tableNames.remove(entity.getTableName(datasourceInfo));

                if (colInfo != null) {
                    Map<String, ModelField> fieldColNames = FastMap.newInstance();
                    Iterator<ModelField> fieldIter = entity.getFieldsIterator();
                    while (fieldIter.hasNext()) {
                        ModelField field = fieldIter.next();
                        fieldColNames.put(field.getColName(), field);
                    }

                    Map<String, ColumnCheckInfo> colMap = colInfo.get(entity.getTableName(datasourceInfo));
                    if (colMap != null) {
                        for (ColumnCheckInfo ccInfo: colMap.values()) {
                            // -list all columns that do not have a corresponding field
                            if (fieldColNames.containsKey(ccInfo.columnName)) {
                                ModelField field = null;

                                field = fieldColNames.remove(ccInfo.columnName);
                                ModelFieldType modelFieldType = modelFieldTypeReader.getModelFieldType(field.getType());

                                if (modelFieldType != null) {
                                    // make sure each corresponding column is of the correct type
                                    String fullTypeStr = modelFieldType.getSqlType();
                                    String typeName;
                                    int columnSize = -1;
                                    int decimalDigits = -1;

                                    int openParen = fullTypeStr.indexOf('(');
                                    int closeParen = fullTypeStr.indexOf(')');
                                    int comma = fullTypeStr.indexOf(',');

                                    if (openParen > 0 && closeParen > 0 && closeParen > openParen) {
                                        typeName = fullTypeStr.substring(0, openParen);
                                        if (comma > 0 && comma > openParen && comma < closeParen) {
                                            String csStr = fullTypeStr.substring(openParen + 1, comma);
                                            try {
                                                columnSize = Integer.parseInt(csStr);
                                            } catch (NumberFormatException e) {
                                                Debug.logError(e, module);
                                            }

                                            String ddStr = fullTypeStr.substring(comma + 1, closeParen);
                                            try {
                                                decimalDigits = Integer.parseInt(ddStr);
                                            } catch (NumberFormatException e) {
                                                Debug.logError(e, module);
                                            }
                                        } else {
                                            String csStr = fullTypeStr.substring(openParen + 1, closeParen);
                                            try {
                                                columnSize = Integer.parseInt(csStr);
                                            } catch (NumberFormatException e) {
                                                Debug.logError(e, module);
                                            }
                                        }
                                    } else {
                                        typeName = fullTypeStr;
                                    }

                                    // override the default typeName with the sqlTypeAlias if it is specified
                                    if (UtilValidate.isNotEmpty(modelFieldType.getSqlTypeAlias())) {
                                        typeName = modelFieldType.getSqlTypeAlias();
                                    }

                                    // NOTE: this may need a toUpperCase in some cases, keep an eye on it, okay just compare with ignore case
                                    if (!ccInfo.typeName.equalsIgnoreCase(typeName)) {
                                        String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" +
                                            entity.getEntityName() + "] is of type [" + ccInfo.typeName + "] in the database, but is defined as type [" +
                                            typeName + "] in the entity definition.";
                                        Debug.logError(message, module);
                                        if (messages != null) messages.add(message);
                                    }
                                    if (columnSize != -1 && ccInfo.columnSize != -1 && columnSize != ccInfo.columnSize && (columnSize * 3) != ccInfo.columnSize) {
                                        String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" +
                                            entity.getEntityName() + "] has a column size of [" + ccInfo.columnSize +
                                            "] in the database, but is defined to have a column size of [" + columnSize + "] in the entity definition.";
                                        Debug.logWarning(message, module);
                                        if (messages != null) messages.add(message);
                                        if (columnSize > ccInfo.columnSize && colWrongSize != null) {
                                            // add item to list of wrong sized columns; only if the entity is larger
                                            colWrongSize.add(entity.getEntityName() + "." + field.getName());
                                        }
                                    }
                                    if (decimalDigits != -1 && decimalDigits != ccInfo.decimalDigits) {
                                        String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" +
                                            entity.getEntityName() + "] has a decimalDigits of [" + ccInfo.decimalDigits +
                                            "] in the database, but is defined to have a decimalDigits of [" + decimalDigits + "] in the entity definition.";
                                        Debug.logWarning(message, module);
                                        if (messages != null) messages.add(message);
                                    }

                                    // do primary key matching check
                                    if (checkPks && ccInfo.isPk && !field.getIsPk()) {
                                        String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" +
                                            entity.getEntityName() + "] IS a primary key in the database, but IS NOT a primary key in the entity definition. The primary key for this table needs to be re-created or modified so that this column is NOT part of the primary key.";
                                        Debug.logError(message, module);
                                        if (messages != null) messages.add(message);
                                    }
                                    if (checkPks && !ccInfo.isPk && field.getIsPk()) {
                                        String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" +
                                            entity.getEntityName() + "] IS NOT a primary key in the database, but IS a primary key in the entity definition. The primary key for this table needs to be re-created or modified to add this column to the primary key. Note that data may need to be added first as a primary key column cannot have an null values.";
                                        Debug.logError(message, module);
                                        if (messages != null) messages.add(message);
                                    }
                                } else {
                                    String message = "Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" + entity.getEntityName() +
                                        "] has a field type name of [" + field.getType() + "] which is not found in the field type definitions";
                                    Debug.logError(message, module);
                                    if (messages != null) messages.add(message);
                                }
                            } else {
                                String message = "Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" + entity.getEntityName() + "] exists in the database but has no corresponding field" + ((checkPks && ccInfo.isPk) ? " (and it is a PRIMARY KEY COLUMN)" : "");
                                Debug.logWarning(message, module);
                                if (messages != null) messages.add(message);
                            }
                        }

                        // -display message if number of table columns does not match number of entity fields
                        if (colMap.size() != entity.getFieldsSize()) {
                            String message = "Entity [" + entity.getEntityName() + "] has " + entity.getFieldsSize() + " fields but table [" + entity.getTableName(datasourceInfo) + "] has " + colMap.size() + " columns.";
                            Debug.logWarning(message, module);
                            if (messages != null) messages.add(message);
                        }
                    }

                    // -list all fields that do not have a corresponding column
                    for (String colName: fieldColNames.keySet()) {
                        ModelField field = (ModelField) fieldColNames.get(colName);
                        String message = "Field [" + field.getName() + "] of entity [" + entity.getEntityName() + "] is missing its corresponding column [" + field.getColName() + "]" + (field.getIsPk() ? " (and it is a PRIMARY KEY FIELD)" : "");

                        Debug.logWarning(message, module);
                        if (messages != null) messages.add(message);

                        if (addMissing) {
                            // add the column
                            String errMsg = addColumn(entity, field);

                            if (UtilValidate.isNotEmpty(errMsg)) {
                                message = "Could not add column [" + field.getColName() + "] to table [" + entity.getTableName(datasourceInfo) + "]: " + errMsg;
                                Debug.logError(message, module);
                                if (messages != null) messages.add(message);
                            } else {
                                message = "Added column [" + field.getColName() + "] to table [" + entity.getTableName(datasourceInfo) + "]" + (field.getIsPk() ? " (NOTE: this is a PRIMARY KEY FIELD, but the primary key was not updated automatically (not considered a safe operation), be sure to fill in any needed data and re-create the primary key)" : "");
                                Debug.logImportant(message, module);
                                if (messages != null) messages.add(message);
                            }
                        }
                    }
                }
            } else {
                String message = "Entity [" + entity.getEntityName() + "] has no table in the database";
                Debug.logWarning(message, module);
                if (messages != null) messages.add(message);

                if (addMissing) {
                    // create the table
                    String errMsg = createTable(entity, modelEntities, false);
                    if (UtilValidate.isNotEmpty(errMsg)) {
                        message = "Could not create table [" + entity.getTableName(datasourceInfo) + "]: " + errMsg;
                        Debug.logError(message, module);
                        if (messages != null) messages.add(message);
                    } else {
                        entitiesAdded.add(entity);
                        message = "Created table [" + entity.getTableName(datasourceInfo) + "]";
                        Debug.logImportant(message, module);
                        if (messages != null) messages.add(message);
                    }
                }
            }
        }

        timer.timerString("After Individual Table/Column Check");

        // -list all tables that do not have a corresponding entity
        for (String tableName: tableNames) {
            String message = "Table named [" + tableName + "] exists in the database but has no corresponding entity";
            Debug.logWarning(message, module);
            if (messages != null) messages.add(message);
        }

        // for each newly added table, add fk indices
        if (datasourceInfo.useFkIndices) {
            int totalFkIndices = 0;
            for (ModelEntity curEntity: entitiesAdded) {
                if (curEntity.getRelationsOneSize() > 0) {
                    totalFkIndices += this.createForeignKeyIndices(curEntity, datasourceInfo.constraintNameClipLength, messages);
                }
            }
            if (totalFkIndices > 0) Debug.logImportant("==== TOTAL Foreign Key Indices Created: " + totalFkIndices, module);
        }

        // for each newly added table, add fks
        if (datasourceInfo.useFks) {
            int totalFks = 0;
            for (ModelEntity curEntity: entitiesAdded) {
                totalFks += this.createForeignKeys(curEntity, modelEntities, datasourceInfo.constraintNameClipLength, datasourceInfo.fkStyle, datasourceInfo.useFkInitiallyDeferred, messages);
            }
            if (totalFks > 0) Debug.logImportant("==== TOTAL Foreign Keys Created: " + totalFks, module);
        }

        // for each newly added table, add declared indexes
        if (datasourceInfo.useIndices) {
            int totalDis = 0;
            for (ModelEntity curEntity: entitiesAdded) {
                if (curEntity.getIndexesSize() > 0) {
                    totalDis += this.createDeclaredIndices(curEntity, messages);
                }
            }
            if (totalDis > 0) Debug.logImportant("==== TOTAL Declared Indices Created: " + totalDis, module);
        }

        // make sure each one-relation has an FK
        if (checkFks) {
        //if (!justColumns && datasourceInfo.useFks && datasourceInfo.checkForeignKeysOnStart) {
            // NOTE: This ISN'T working for Postgres or MySQL, who knows about others, may be from JDBC driver bugs...
            int numFksCreated = 0;
            // TODO: check each key-map to make sure it exists in the FK, if any differences warn and then remove FK and recreate it

            // get ALL column info, put into hashmap by table name
            Map<String, Map<String, ReferenceCheckInfo>> refTableInfoMap = this.getReferenceInfo(fkTableNames, messages);

            // Debug.logVerbose("Ref Info Map: " + refTableInfoMap, module);

            if (refTableInfoMap == null) {
                // uh oh, something happened while getting info...
                if (Debug.verboseOn()) Debug.logVerbose("Ref Table Info Map is null", module);
            } else {
                for (ModelEntity entity: modelEntityList) {
                    String entityName = entity.getEntityName();
                    // if this is a view entity, do not check it...
                    if (entity instanceof ModelViewEntity) {
                        String entMessage = "NOT Checking View Entity " + entity.getEntityName();
                        Debug.logVerbose(entMessage, module);
                        if (messages != null) {
                            messages.add(entMessage);
                        }
                        continue;
                    }

                    // get existing FK map for this table
                    Map<String, ReferenceCheckInfo> rcInfoMap = refTableInfoMap.get(entity.getTableName(datasourceInfo));
                    // Debug.logVerbose("Got ref info for table " + entity.getTableName(datasourceInfo) + ": " + rcInfoMap, module);

                    // go through each relation to see if an FK already exists
                    Iterator<ModelRelation> relations = entity.getRelationsIterator();
                    boolean createdConstraints = false;
                    while (relations.hasNext()) {
                        ModelRelation modelRelation = relations.next();
                        if (!"one".equals(modelRelation.getType())) {
                            continue;
                        }

                        ModelEntity relModelEntity = modelEntities.get(modelRelation.getRelEntityName());
                        if (relModelEntity == null) {
                            Debug.logError("No such relation: " + entity.getEntityName() + " -> " + modelRelation.getRelEntityName(), module);
                            continue;
                        }
                        String relConstraintName = makeFkConstraintName(modelRelation, datasourceInfo.constraintNameClipLength);
                        ReferenceCheckInfo rcInfo = null;

                        if (rcInfoMap != null) {
                            rcInfo = rcInfoMap.get(relConstraintName);
                        }

                        if (rcInfo != null) {
                            rcInfoMap.remove(relConstraintName);
                        } else {
                            // if not, create one
                            String noFkMessage = "No Foreign Key Constraint [" + relConstraintName + "] found for entity [" + entityName + "]";
                            if (messages != null) messages.add(noFkMessage);
                            if (Debug.infoOn()) Debug.logInfo(noFkMessage, module);

                            if (addMissing) {
                                String errMsg = createForeignKey(entity, modelRelation, relModelEntity, datasourceInfo.constraintNameClipLength, datasourceInfo.fkStyle, datasourceInfo.useFkInitiallyDeferred);
                                if (UtilValidate.isNotEmpty(errMsg)) {
                                    String message = "Could not create foreign key " + relConstraintName + " for entity [" + entity.getEntityName() + "]: " + errMsg;
                                    Debug.logError(message, module);
                                    if (messages != null) messages.add(message);
                                } else {
                                    String message = "Created foreign key " + relConstraintName + " for entity [" + entity.getEntityName() + "]";
                                    Debug.logVerbose(message, module);
                                    if (messages != null) messages.add(message);
                                    createdConstraints = true;
                                    numFksCreated++;
                                }
                            }
                        }
                    }
                    if (createdConstraints) {
                        String message = "Created foreign key(s) for entity [" + entity.getEntityName() + "]";
                        Debug.logImportant(message, module);
                        if (messages != null) messages.add(message);
                    }

                    // show foreign key references that exist but are unknown
                    if (rcInfoMap != null) {
                        for (String rcKeyLeft: rcInfoMap.keySet()) {
                            String message = "Unknown Foreign Key Constraint " + rcKeyLeft + " found in table " + entity.getTableName(datasourceInfo);
                            Debug.logImportant(message, module);
                            if (messages != null) messages.add(message);
                        }
                    }
                }
            }
            if (Debug.infoOn()) Debug.logInfo("Created " + numFksCreated + " fk refs", module);
        }

        // make sure each one-relation has an index
        if (checkFkIdx) {
        //if (!justColumns && datasourceInfo.useFkIndices && datasourceInfo.checkFkIndicesOnStart) {
            int numIndicesCreated = 0;
            // TODO: check each key-map to make sure it exists in the index, if any differences warn and then remove the index and recreate it

            // get ALL column info, put into hashmap by table name
            Map<String, Set<String>> tableIndexListMap = this.getIndexInfo(indexTableNames, messages);

            // Debug.logVerbose("Ref Info Map: " + refTableInfoMap, module);

            if (tableIndexListMap == null) {
                // uh oh, something happened while getting info...
                if (Debug.verboseOn()) Debug.logVerbose("Ref Table Info Map is null", module);
            } else {
                for (ModelEntity entity: modelEntityList) {
                    String entityName = entity.getEntityName();
                    // if this is a view entity, do not check it...
                    if (entity instanceof ModelViewEntity) {
                        String entMessage = "NOT Checking View Entity " + entity.getEntityName();
                        Debug.logVerbose(entMessage, module);
                        if (messages != null) messages.add(entMessage);
                        continue;
                    }

                    // get existing index list for this table
                    Set<String> tableIndexList = tableIndexListMap.get(entity.getTableName(datasourceInfo));

                    // Debug.logVerbose("Got ind info for table " + entity.getTableName(datasourceInfo) + ": " + tableIndexList, module);

                    if (tableIndexList == null) {
                        // evidently no indexes in the database for this table, do the create all
                        this.createForeignKeyIndices(entity, datasourceInfo.constraintNameClipLength, messages);
                    } else {
                        // go through each relation to see if an FK already exists
                        boolean createdConstraints = false;
                        Iterator<ModelRelation> relations = entity.getRelationsIterator();
                        while (relations.hasNext()) {
                            ModelRelation modelRelation = relations.next();
                            if (!"one".equals(modelRelation.getType())) {
                                continue;
                            }

                            String relConstraintName = makeFkConstraintName(modelRelation, datasourceInfo.constraintNameClipLength);
                            if (tableIndexList.contains(relConstraintName)) {
                                tableIndexList.remove(relConstraintName);
                            } else {
                                // if not, create one
                                String noIdxMessage = "No Index [" + relConstraintName + "] found for entity [" + entityName + "]";
                                if (messages != null) messages.add(noIdxMessage);
                                if (Debug.infoOn()) Debug.logInfo(noIdxMessage, module);

                                if (addMissing) {
                                    String errMsg = createForeignKeyIndex(entity, modelRelation, datasourceInfo.constraintNameClipLength);
                                    if (UtilValidate.isNotEmpty(errMsg)) {
                                        String message = "Could not create foreign key index " + relConstraintName + " for entity [" + entity.getEntityName() + "]: " + errMsg;
                                        Debug.logError(message, module);
                                        if (messages != null) messages.add(message);
                                    } else {
                                        String message = "Created foreign key index " + relConstraintName + " for entity [" + entity.getEntityName() + "]";
                                        Debug.logVerbose(message, module);
                                        if (messages != null) messages.add(message);
                                        createdConstraints = true;
                                        numIndicesCreated++;
                                    }
                                }
                            }
                        }
                        if (createdConstraints) {
                            String message = "Created foreign key index/indices for entity [" + entity.getEntityName() + "]";
                            Debug.logImportant(message, module);
                            if (messages != null) messages.add(message);
                        }
                    }

                    // show foreign key references that exist but are unknown
                    if (tableIndexList != null) {
                        for (String indexLeft: tableIndexList) {
                            String message = "Unknown Index " + indexLeft + " found in table " + entity.getTableName(datasourceInfo);
                            Debug.logImportant(message, module);
                            if (messages != null) messages.add(message);
                        }
                    }
                }
            }
            if (Debug.infoOn()) Debug.logInfo("Created " + numIndicesCreated + " indices", module);
        }

        if (datasourceInfo.checkIndicesOnStart) {
            int numIndicesCreated = 0;
            // TODO: check each key-map to make sure it exists in the index, if any differences warn and then remove the index and recreate it

            // get ALL column info, put into hashmap by table name
            Map<String, Set<String>> tableIndexListMap = this.getIndexInfo(indexTableNames, messages);

            // Debug.logVerbose("Ref Info Map: " + refTableInfoMap, module);

            if (tableIndexListMap == null) {
                // uh oh, something happened while getting info...
                if (Debug.verboseOn()) Debug.logVerbose("Ref Table Info Map is null", module);
            } else {
                for (ModelEntity entity: modelEntityList) {
                    String entityName = entity.getEntityName();
                    // if this is a view entity, do not check it...
                    if (entity instanceof ModelViewEntity) {
                        String entMessage = "NOT Checking View Entity " + entity.getEntityName();
                        Debug.logVerbose(entMessage, module);
                        if (messages != null) messages.add(entMessage);
                        continue;
                    }

                    // get existing index list for this table
                    Set<String> tableIndexList = tableIndexListMap.get(entity.getTableName(datasourceInfo));

                    // Debug.logVerbose("Got ind info for table " + entity.getTableName(datasourceInfo) + ": " + tableIndexList, module);

                    if (tableIndexList == null) {
                        // evidently no indexes in the database for this table, do the create all
                        this.createDeclaredIndices(entity, messages);
                    } else {
                        // go through each indice to see if an indice already exists
                        boolean createdIndexes = false;
                        Iterator<ModelIndex> indexes = entity.getIndexesIterator();
                        while (indexes.hasNext()) {
                            ModelIndex modelIndex = indexes.next();

                            String relIndexName = makeIndexName(modelIndex, datasourceInfo.constraintNameClipLength);
                            if (tableIndexList.contains(relIndexName)) {
                                tableIndexList.remove(relIndexName);
                            } else {
                                // if not, create one
                                String noIdxMessage = "No Index [" + relIndexName + "] found for entity [" + entityName + "]";
                                if (messages != null) messages.add(noIdxMessage);
                                if (Debug.infoOn()) Debug.logInfo(noIdxMessage, module);

                                if (addMissing) {
                                    String errMsg = createDeclaredIndex(entity, modelIndex);
                                    if (UtilValidate.isNotEmpty(errMsg)) {
                                        String message = "Could not create index " + relIndexName + " for entity [" + entity.getEntityName() + "]: " + errMsg;
                                        Debug.logError(message, module);
                                        if (messages != null) messages.add(message);
                                    } else {
                                        String message = "Created index " + relIndexName + " for entity [" + entity.getEntityName() + "]";
                                        Debug.logVerbose(message, module);
                                        if (messages != null) messages.add(message);
                                        createdIndexes = true;
                                        numIndicesCreated++;
                                    }
                                }
                            }
                        }
                        if (createdIndexes) {
                            String message = "Created foreign key index/indices for entity [" + entity.getEntityName() + "]";
                            Debug.logImportant(message, module);
                            if (messages != null) messages.add(message);
                        }
                    }

                    // show Indexe key references that exist but are unknown
                    if (tableIndexList != null) {
                        for (String indexLeft: tableIndexList) {
                            String message = "Unknown Index " + indexLeft + " found in table " + entity.getTableName(datasourceInfo);
                            Debug.logImportant(message, module);
                            if (messages != null) messages.add(message);
                        }
                    }
                }
            }
            if (Debug.infoOn()) Debug.logInfo("Created " + numIndicesCreated + " indices", module);

        }


        timer.timerString("Finished Checking Entity Database");
    }
View Full Code Here

                    entityCache = new HashMap<String, ModelEntity>();
                    List<ModelViewEntity> tempViewEntityList = FastList.newInstance();
                    List<Element> tempExtendEntityElementList = FastList.newInstance();

                    UtilTimer utilTimer = new UtilTimer();

                    for (ResourceHandler entityResourceHandler: entityResourceHandlers) {

                        // utilTimer.timerString("Before getDocument in file " + entityFileName);
                        Document document = null;

                        try {
                            document = entityResourceHandler.getDocument();
                        } catch (GenericConfigException e) {
                            throw new GenericEntityConfException("Error getting document from resource handler", e);
                        }
                        if (document == null) {
                            throw new GenericEntityConfException("Could not get document for " + entityResourceHandler.toString());
                        }

                        // utilTimer.timerString("Before getDocumentElement in " + entityResourceHandler.toString());
                        Element docElement = document.getDocumentElement();

                        if (docElement == null) {
                            return null;
                        }
                        docElement.normalize();
                        Node curChild = docElement.getFirstChild();

                        ModelInfo def = new ModelInfo();
                        def.populateFromElements(docElement);
                        int i = 0;

                        if (curChild != null) {
                            utilTimer.timerString("Before start of entity loop in " + entityResourceHandler.toString());
                            do {
                                boolean isEntity = "entity".equals(curChild.getNodeName());
                                boolean isViewEntity = "view-entity".equals(curChild.getNodeName());
                                boolean isExtendEntity = "extend-entity".equals(curChild.getNodeName());

                                if ((isEntity || isViewEntity) && curChild.getNodeType() == Node.ELEMENT_NODE) {
                                    i++;
                                    ModelEntity modelEntity = buildEntity(entityResourceHandler, (Element) curChild, i, def);
                                    // put the view entity in a list to get ready for the second pass to populate fields...
                                    if (isViewEntity) tempViewEntityList.add((ModelViewEntity) modelEntity);
                                } else if (isExtendEntity && curChild.getNodeType() == Node.ELEMENT_NODE) {
                                    tempExtendEntityElementList.add((Element) curChild);
                                }
                            } while ((curChild = curChild.getNextSibling()) != null);
                        } else {
                            Debug.logWarning("No child nodes found.", module);
                        }
                        utilTimer.timerString("Finished " + entityResourceHandler.toString() + " - Total Entities: " + i + " FINISHED");
                    }

                    // all entity elements in, now go through extend-entity elements and add their stuff
                    for (Element extendEntityElement: tempExtendEntityElementList) {
                        String entityName = UtilXml.checkEmpty(extendEntityElement.getAttribute("entity-name"));
View Full Code Here

        this.handler = handler;
        this.dctx = dctx;
    }

    private Map<String, ModelService> getModelServices() {
        UtilTimer utilTimer = new UtilTimer();
        Document document;
        if (this.isFromURL) {
            // utilTimer.timerString("Before getDocument in file " + readerURL);
            document = getDocument(readerURL);

            if (document == null) {
                return null;
            }
        } else {
            // utilTimer.timerString("Before getDocument in " + handler);
            try {
                document = handler.getDocument();
            } catch (GenericConfigException e) {
                Debug.logError(e, "Error getting XML document from resource", module);
                return null;
            }
        }

        Map<String, ModelService> modelServices = FastMap.newInstance();
        if (this.isFromURL) {// utilTimer.timerString("Before getDocumentElement in file " + readerURL);
        } else {// utilTimer.timerString("Before getDocumentElement in " + handler);
        }

        Element docElement = document.getDocumentElement();
        if (docElement == null) {
            return null;
        }

        docElement.normalize();

        String resourceLocation = handler.getLocation();
        try {
            resourceLocation = handler.getURL().toExternalForm();
        } catch (GenericConfigException e) {
            Debug.logError(e, "Could not get resource URL", module);
        }

        int i = 0;
        Node curChild = docElement.getFirstChild();
        if (curChild != null) {
            if (this.isFromURL) {
                utilTimer.timerString("Before start of service loop in file " + readerURL);
            } else {
                utilTimer.timerString("Before start of service loop in " + handler);
            }

            do {
                if (curChild.getNodeType() == Node.ELEMENT_NODE && "service".equals(curChild.getNodeName())) {
                    i++;
                    Element curServiceElement = (Element) curChild;
                    String serviceName = UtilXml.checkEmpty(curServiceElement.getAttribute("name"));

                    // check to see if service with same name has already been read
                    if (modelServices.containsKey(serviceName)) {
                        Debug.logWarning("WARNING: Service " + serviceName + " is defined more than once, " +
                            "most recent will over-write previous definition(s)", module);
                    }

                    // utilTimer.timerString("  After serviceName -- " + i + " --");
                    ModelService service = createModelService(curServiceElement, resourceLocation);

                    // utilTimer.timerString("  After createModelService -- " + i + " --");
                    if (service != null) {
                        modelServices.put(serviceName, service);
                        // utilTimer.timerString("  After modelServices.put -- " + i + " --");
                        /*
                        int reqIn = service.getParameterNames(ModelService.IN_PARAM, false).size();
                        int optIn = service.getParameterNames(ModelService.IN_PARAM, true).size() - reqIn;
                        int reqOut = service.getParameterNames(ModelService.OUT_PARAM, false).size();
                        int optOut = service.getParameterNames(ModelService.OUT_PARAM, true).size() - reqOut;

                        if (Debug.verboseOn()) {
                            String msg = "-- getModelService: # " + i + " Loaded service: " + serviceName +
                                " (IN) " + reqIn + "/" + optIn + " (OUT) " + reqOut + "/" + optOut;

                            Debug.logVerbose(msg, module);
                        }
                        */
                    } else {
                        Debug.logWarning(
                            "-- -- SERVICE ERROR:getModelService: Could not create service for serviceName: " +
                            serviceName, module);
                    }

                }
            } while ((curChild = curChild.getNextSibling()) != null);
        } else {
            Debug.logWarning("No child nodes found.", module);
        }
        if (this.isFromURL) {
            utilTimer.timerString("Finished file " + readerURL + " - Total Services: " + i + " FINISHED");
            Debug.logImportant("Loaded [" + StringUtil.leftPad(Integer.toString(i), 3) + "] Services from " + readerURL, module);
        } else {
            utilTimer.timerString("Finished document in " + handler + " - Total Services: " + i + " FINISHED");
            if (Debug.importantOn()) {
                Debug.logImportant("Loaded [" + StringUtil.leftPad(Integer.toString(i), 3) + "] Services from " + resourceLocation, module);
            }
        }
        return modelServices;
View Full Code Here

            synchronized (ModelDataFileReader.class) {
                // must check if null again as one of the blocked threads can still enter
                if (modelDataFiles == null) { // now it's safe
                    modelDataFiles = new HashMap<String, ModelDataFile>();

                    UtilTimer utilTimer = new UtilTimer();

                    utilTimer.timerString("Before getDocument in file " + readerURL);
                    Document document = getDocument(readerURL);

                    if (document == null) {
                        modelDataFiles = null;
                        return null;
                    }

                    utilTimer.timerString("Before getDocumentElement in file " + readerURL);
                    Element docElement = document.getDocumentElement();

                    if (docElement == null) {
                        modelDataFiles = null;
                        return null;
                    }
                    docElement.normalize();
                    Node curChild = docElement.getFirstChild();

                    int i = 0;

                    if (curChild != null) {
                        utilTimer.timerString("Before start of dataFile loop in file " + readerURL);
                        do {
                            if (curChild.getNodeType() == Node.ELEMENT_NODE && "data-file".equals(curChild.getNodeName())) {
                                i++;
                                Element curDataFile = (Element) curChild;
                                String dataFileName = UtilXml.checkEmpty(curDataFile.getAttribute("name"));

                                // check to see if dataFile with same name has already been read
                                if (modelDataFiles.containsKey(dataFileName)) {
                                    Debug.logWarning("WARNING: DataFile " + dataFileName +
                                        " is defined more than once, most recent will over-write previous definition(s)", module);
                                }

                                // utilTimer.timerString("  After dataFileName -- " + i + " --");
                                ModelDataFile dataFile = createModelDataFile(curDataFile);

                                // utilTimer.timerString("  After createModelDataFile -- " + i + " --");
                                if (dataFile != null) {
                                    modelDataFiles.put(dataFileName, dataFile);
                                    // utilTimer.timerString("  After modelDataFiles.put -- " + i + " --");
                                    if (Debug.infoOn()) Debug.logInfo("-- getModelDataFile: #" + i + " Loaded dataFile: " + dataFileName, module);
                                } else
                                    Debug.logWarning("-- -- SERVICE ERROR:getModelDataFile: Could not create dataFile for dataFileName: " + dataFileName, module);

                            }
                        } while ((curChild = curChild.getNextSibling()) != null);
                    } else {
                        Debug.logWarning("No child nodes found.", module);
                    }
                    utilTimer.timerString("Finished file " + readerURL + " - Total Flat File Defs: " + i + " FINISHED");
                }
            }
        }
        return modelDataFiles;
    }
View Full Code Here

                FieldTypeInfo fieldTypeInfo = EntityConfigUtil.getFieldTypeInfo(tempModelName);
                if (fieldTypeInfo == null) {
                    throw new IllegalArgumentException("Could not find a field-type definition with name \"" + tempModelName + "\"");
                }
                ResourceHandler fieldTypeResourceHandler = new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, fieldTypeInfo.resourceElement);
                UtilTimer utilTimer = new UtilTimer();
                utilTimer.timerString("[ModelFieldTypeReader.getModelFieldTypeReader] Reading field types from " + fieldTypeResourceHandler.getLocation());
                Document document = null;
                try {
                    document = fieldTypeResourceHandler.getDocument();
                } catch (GenericConfigException e) {
                    Debug.logError(e, module);
                    throw new IllegalStateException("Error loading field type file " + fieldTypeResourceHandler.getLocation());
                }
                Map<String, ModelFieldType> fieldTypeMap = createFieldTypeCache(document.getDocumentElement(), fieldTypeResourceHandler.getLocation());
                reader = new ModelFieldTypeReader(fieldTypeMap);
                readers.put(tempModelName, reader);
                utilTimer.timerString("[ModelFieldTypeReader.getModelFieldTypeReader] Read " + fieldTypeMap.size() + " field types");
            }
        }
        return reader;
    }
View Full Code Here

                if (this.groupCache == null) {
                    // now it's safe
                    this.groupCache = new HashMap<String, String>();
                    this.groupNames = new TreeSet<String>();

                    UtilTimer utilTimer = new UtilTimer();
                    // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocument");

                    int i = 0;
                    for (ResourceHandler entityGroupResourceHandler: this.entityGroupResourceHandlers) {
                        Document document = null;

                        try {
                            document = entityGroupResourceHandler.getDocument();
                        } catch (GenericConfigException e) {
                            Debug.logError(e, "Error loading entity group model", module);
                        }
                        if (document == null) {
                            this.groupCache = null;
                            return null;
                        }

                        // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocumentElement");
                        Element docElement = document.getDocumentElement();
                        if (docElement == null) {
                            continue;
                        }
                        docElement.normalize();

                        Node curChild = docElement.getFirstChild();
                        if (curChild != null) {
                            utilTimer.timerString("[ModelGroupReader.getGroupCache] Before start of entity loop");
                            do {
                                if (curChild.getNodeType() == Node.ELEMENT_NODE && "entity-group".equals(curChild.getNodeName())) {
                                    Element curEntity = (Element) curChild;
                                    String entityName = UtilXml.checkEmpty(curEntity.getAttribute("entity")).intern();
                                    String groupName = UtilXml.checkEmpty(curEntity.getAttribute("group")).intern();

                                    if (groupName == null || entityName == null) continue;
                                    this.groupNames.add(groupName);
                                    this.groupCache.put(entityName, groupName);
                                    // utilTimer.timerString("  After entityEntityName -- " + i + " --");
                                    i++;
                                }
                            } while ((curChild = curChild.getNextSibling()) != null);
                        } else {
                            Debug.logWarning("[ModelGroupReader.getGroupCache] No child nodes found.", module);
                        }
                    }
                    utilTimer.timerString("[ModelGroupReader.getGroupCache] FINISHED - Total Entity-Groups: " + i + " FINISHED");
                }
            }
        }
        return this.groupCache;
    }
View Full Code Here

TOP

Related Classes of org.ofbiz.base.util.UtilTimer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.