Tuesday, May 24, 2011

Preparing value submitted bind variable query in ExecuteQuery

    public void executeQuery() {
        System.out.println("getQuery before executeQuery:" + getQuery());
        performCustomViewCriteriaValidation();
        super.executeQuery();
        List vcTempVals = new ArrayList();
        System.out.println("getQuery after executeQuery:" + getQuery());
        String formattedQuery = getQuery();

        ViewCriteria[] vcs =
            getApplyViewCriterias(ViewCriteria.CRITERIA_MODE_QUERY);

        if (vcs != null & vcs.length > 0) {
            for (ViewCriteria v : vcs) {
                for (ViewCriteriaRow vcr :
                     (List<ViewCriteriaRow>)v.getRows()) {
                    //                    ViewCriteriaRow vcr = (ViewCriteriaRow)v.first();
                    if (vcr != null) {
                        ViewCriteriaItem[] vcis = vcr.getCriteriaItemArray();
                        if (vcis != null && vcis.length > 0) {
                            for (int j = 0; j < vcis.length; j++) {
                                ViewCriteriaItem vci = vcis[j];
                                if (vci != null &&
                                    vci instanceof ViewCriteriaItemCompound) {
                                    ViewCriteriaItemCompound vcic =
                                        (ViewCriteriaItemCompound)vci;
                                    for (int k = 0; k < vcic.getItemCount();
                                         k++) {
                                        vci =
(ViewCriteriaItem)vcic.getItem(k);
                                        if (vci != null) {
                                            Object o = vci.getValue();
                                            Object on = vci.getColumnName();
                                            if (!vci.getOperator().equals(JboCompOper.OPER_IS_BLANK) &&
                                                !vci.getOperator().equals(JboCompOper.OPER_IS_NOT_BLANK) &&
                                                vci.getValue() != null) {
                                                System.out.println("vc attribute from compound:" +
                                                                   o);
                                                System.out.println("vc attribute name from compound:" +
                                                                   on);
                                                vcTempVals.add(vci.getValue());
                                            }
                                        }
                                    }
                                } else if (vci != null) {
                                    Object o = vci.getValue();
                                    Object on = vci.getColumnName();
                                    if (!vci.getOperator().equals(JboCompOper.OPER_IS_BLANK) &&
                                        !vci.getOperator().equals(JboCompOper.OPER_IS_NOT_BLANK) &&
                                        vci.getValue() != null) {
                                        System.out.println("vc attribute:" +
                                                           o);
                                        System.out.println("vc attribute name:" +
                                                           on);
                                        vcTempVals.add(vci.getValue());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        Pattern p = Pattern.compile(":vc_temp\\w*");
        String[] arr = p.split(formattedQuery);
        formattedQuery = "";
        for (int i = 0; i < arr.length; i++) {
            //                              System.out.println("mukka:" + arr[i]);
            if (i == arr.length - 1) {
                formattedQuery = formattedQuery + arr[i];
            } else {
                formattedQuery =
                        formattedQuery + arr[i] + "'" + vcTempVals.get(i) +
                        "'";
            }
        }

        ADFContext.getCurrent().getExpressionEvaluator().setValue("#{pageFlowScope.AllocExtractsQuery}",
                                                                  formattedQuery);
        System.out.println("end of executeQuery:" + formattedQuery);

    }

    private void performCustomViewCriteriaValidation() {
        ViewCriteria[] vcs =
            getApplyViewCriterias(ViewCriteria.CRITERIA_MODE_QUERY);
        Boolean isValidRequiredVc = true, isValidSelcRequiredVc=true;
        HashMap selcRequiredVcItems = new HashMap();

        if (vcs != null & vcs.length > 0) {
            for (ViewCriteria v : vcs) {
                ViewCriteriaRow vcr = (ViewCriteriaRow)v.first();
                if (vcr != null) {
                    ViewCriteriaItem[] vcis = vcr.getCriteriaItemArray();
                    if (vcis != null && vcis.length > 0) {
                        for (int j = 0; j < vcis.length; j++) {
                            ViewCriteriaItem vci = vcis[j];
                            if (vci != null &&
                                vci instanceof ViewCriteriaItemCompound) {
                                ViewCriteriaItemCompound vcic =
                                    (ViewCriteriaItemCompound)vci;
                                for (int k = 0; k < vcic.getItemCount(); k++) {
                                    vci = (ViewCriteriaItem)vcic.getItem(k);
                                    if (vci != null) {
                                        Object o = vci.getValue();
                                        Object on = vci.getColumnName();
                                        if (vci.VCITEM_REQUIRED ==
                                            vci.getRequired() &&
                                            vci.getValue() == null) {
                                            System.out.println("Null value for vc required attribute name from compound:" +
                                                               on);
                                            isValidRequiredVc = false;
                                            break;
                                        } else if (vci.VCITEM_SELECTIVELY_REQUIRED ==
                                                   vci.getRequired()) {
                                            System.out.println("vc selctively required attribute name from compound:" +
                                                               on);
                                            selcRequiredVcItems.put(on,o);
                                        }
                                    }
                                }
                            } else if (vci != null) {
                                Object o = vci.getValue();
                                Object on = vci.getColumnName();
                                if (vci.VCITEM_REQUIRED == vci.getRequired() &&
                                    vci.getValue() == null) {
                                    System.out.println("Null value for vc required attribute name:" +
                                                       on);
                                    isValidRequiredVc = false;
                                    break;
                                } else if (vci.VCITEM_SELECTIVELY_REQUIRED ==
                                           vci.getRequired()) {
                                    System.out.println("vc selctively required attribute name:" +
                                                       on);
                                    selcRequiredVcItems.put(on,o);
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!isValidRequiredVc) {
            throw new JboException("Please enter all required attributes for search!");
        }
        else if(selcRequiredVcItems.size() != 0) {
            isValidSelcRequiredVc = false;
            for(Object obj:selcRequiredVcItems.keySet()) {
                if(selcRequiredVcItems.get(obj) != null) {
                    isValidSelcRequiredVc = true;
                    break;
                }
            }
            if (!isValidSelcRequiredVc) {
                throw new JboException("Please enter any of the selectively required attribute for search!");
            }
        }
    }