Example usage for java.util Vector elements

List of usage examples for java.util Vector elements

Introduction

In this page you can find the example usage for java.util Vector elements.

Prototype

public Enumeration<E> elements() 

Source Link

Document

Returns an enumeration of the components of this vector.

Usage

From source file:com.flexoodb.engines.FlexJAXBDBDataEngine.java

public Collection<Object> runQuery(String query, Class c, boolean usedefaultimplementation) throws Exception {
    Vector v = new Vector();
    Connection conn = null;//w  ww.j  av a2 s . c o  m
    try {

        //System.out.println(">>>>orginal:["+query+"]");

        conn = (Connection) _pool.getConnection();
        String tablename = query.split("\\s")[3]; // always search the index!

        if (checkTable(tablename, conn, false)) {
            StringBuffer q = new StringBuffer("where ");

            if (query.toUpperCase().indexOf("WHERE") > 0) {
                String sub = query.substring(query.toUpperCase().indexOf("WHERE") + 5);

                sub = sub.replaceAll("<=", " &lteq; ");
                sub = sub.replaceAll(">=", " &gteq; ");
                sub = sub.replaceAll("<>", " &nteq; ");
                sub = sub.replaceAll("=", " = ");
                sub = sub.replaceAll(">", " > ");
                sub = sub.replaceAll("<", " < ");
                sub = sub.replaceAll("&lteq;", "<=");
                sub = sub.replaceAll("&gteq;", ">=");
                sub = sub.replaceAll("&nteq;", "<>").trim();
                sub = sub.replaceAll("&lt;", "<");
                sub = sub.replaceAll("&gt;", ">");

                //System.out.println("from:"+sub);
                boolean done = false;
                boolean id = false;
                int seq = 0;
                String col = null;
                String condition = null;
                while (!done) {
                    int x = sub.indexOf(" ");
                    String word = sub.substring(0, x < 0 ? sub.length() : x);
                    int wlen = word.length();

                    if (word.startsWith("'")) {
                        word = sub.substring(1, sub.indexOf("'", 1));
                        wlen = word.length() + 2;
                    }

                    //System.out.println("w:"+word+"< "+wlen+" wl:"+word.length());

                    // check if its a predicate
                    if (":like:=:>:<:<=:>=:<>:".indexOf(":" + word.toLowerCase() + ":") > -1) {
                        condition = word;
                        seq = 2;
                    } else if (":and:or:not:".indexOf(":" + word.toLowerCase() + ":") > -1) {
                        q.append(" " + word.trim() + " ");
                        seq = 0;
                    } else if (seq == 0)// it must be a field!
                    {
                        seq = 1; // fields sequence
                        if (word.trim().equalsIgnoreCase("parentid") || word.trim().equalsIgnoreCase("id")) {
                            q.append(" " + word.trim());
                            id = true;
                        } else if (word.trim().equalsIgnoreCase("order")) {
                            String[] order = sub.split("\\s");
                            if (!order[2].equalsIgnoreCase("id") && !order[2].equalsIgnoreCase("parentid")) {
                                // get the 3rd word -- ie the field
                                if (!q.toString().toUpperCase().endsWith("WHERE")) {
                                    q.append(" and ");
                                }

                                q.append(" (element='" + order[2] + "')");

                                q.append(" " + order[0] + " by value "
                                        + sub.substring(sub.indexOf(order[2]) + order[2].length()).trim());
                            } else {
                                q.append(" " + sub);
                            }
                            done = true;
                        } else if (word.trim().equalsIgnoreCase("element")
                                || word.trim().equalsIgnoreCase("limit") || word.trim().equalsIgnoreCase("desc")
                                || word.trim().equalsIgnoreCase("asc")) {
                            q.append(" " + sub);
                            done = true;
                        } else {

                            word = word.replaceAll("'", "\'").trim();
                            //q.append(" (element='"+word.trim().replaceAll("'","")+"'");
                            q.append(" (element='" + word + "'");
                            //col = word.trim().replaceAll("'","");
                            col = word;
                        }
                    } else if (seq == 2) {
                        //word = word.replaceAll("'"," ");
                        word = word.replaceAll("'", "\'");
                        if (id) {
                            q.append("" + condition + "'" + word.trim() + "'");
                        } else {
                            boolean valchanged = false;
                            try {
                                // we look for dates!
                                if (col != null) {
                                    Method met = c.getMethod(
                                            "get" + col.substring(0, 1).toUpperCase() + col.substring(1),
                                            (Class[]) null);
                                    Class c1 = (Class) met.getGenericReturnType();

                                    if (c1.getSimpleName().equalsIgnoreCase("XMLGregorianCalendar")
                                            && !word.isEmpty()) {
                                        //q.append(" and str_to_date(value,\"%Y-%m-%d\") "+condition+" '"+word.trim().replaceAll("'","")+"')");
                                        q.append(" and str_to_date(value,\"%Y-%m-%d\") " + condition + " '"
                                                + word.trim() + "')");
                                        valchanged = true;
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            if (!valchanged) {
                                //q.append(" and value "+condition+" '"+word.trim().replaceAll("'","")+"')");
                                q.append(" and value " + condition + " '" + word.trim() + "')");
                            }
                            col = null;
                        }

                        seq = 0;
                        condition = null;
                        id = false;
                    }

                    sub = sub.substring(wlen).trim();
                    if (x < 0 || sub.length() == 0) {
                        done = true;
                    }
                }

            } else {
                int tl = tablename.length();
                q = new StringBuffer(query.substring(query.indexOf(tablename) + tl));
            }

            PreparedStatement ps = null;
            boolean searchindex = false;
            if (!usedefaultimplementation) {
                ps = (PreparedStatement) conn.prepareStatement(
                        "select distinct id from " + tablename.toLowerCase() + " " + q.toString());
            } else {
                ps = (PreparedStatement) conn.prepareStatement(
                        "select distinct id from " + tablename.toLowerCase() + "_index " + q.toString());
                searchindex = true;
            }

            ResultSet rec = ps.executeQuery();

            //System.out.println(">>>Query:["+ps.toString()+"]");

            // check if a record was found
            while (rec != null && !rec.isClosed() && rec.next()) {
                String id = rec.getString("id");
                String xml = null;
                try {
                    Object o = null;
                    PreparedStatement ps2 = (PreparedStatement) conn
                            .prepareStatement("select id,parentid,content from " + tablename.toLowerCase()
                                    + " where id='" + id + "'");
                    ResultSet res = ps2.executeQuery();
                    // check if a record was found
                    if (res != null && res.next()) {
                        String i = res.getString("id");
                        String p = res.getString("parentid");
                        xml = res.getString("content");
                        o = new FlexContainer(_flexutils.getObject(FlexUtils.stripNonValidChars(xml), c));
                        ((FlexContainer) o).setId(i);
                        ((FlexContainer) o).setParentId(p);

                        ps2.close();
                    } else {
                        ps2.close();
                        if (searchindex) {
                            // then the values found must be orphans! we delete the index contents
                            removeValues(id, tablename, conn);
                        }
                    }

                    if (o != null) {
                        v.add(o);
                        Enumeration en = v.elements();
                        while (en.hasMoreElements()) {
                            en.nextElement();
                        }
                    }
                } catch (Exception g) {
                    g.printStackTrace();
                    System.out.println(">>>FlexJAXDBDataEngine Error due to XML:" + xml);
                    throw g;
                }
            }
        }
    } catch (Exception f) {
        f.printStackTrace();
        throw f;
    } finally {
        try {
            if (conn != null) {
                _pool.releaseConnection(conn);
            }

        } catch (Exception g) {
        }
    }
    return v;
}

From source file:sos.settings.SOSSettingsDialog.java

/**
 * Datensatz speichern/*from   w  w  w . ja v a 2s  .  co m*/
 * 
 * @return boolean Fehlerzustand
 * @throws Exception
 */

private boolean recordStore() throws Exception {
    this.debug(3, "recordStore");

    StringBuffer sqlStmt = null;

    sqlStmt = new StringBuffer("select ")
            .append("\"APPLICATION\",\"SECTION\",\"NAME\",\"VALUE\",\"DEFAULT_VALUE\",\"TITLE\",")
            .append("\"INPUT_TYPE\",\"INPUT_SIZE\",\"DISPLAY_TYPE\",\"DISPLAY_SIZE\",\"FORCED\",")
            .append("\"ENTRY_TYPE\",\"CREATED\",\"CREATED_BY\",\"MODIFIED\",\"MODIFIED_BY\" ")
            .append(" from " + this.settings.source + " ")
            .append(" where \"" + this.settings.entryApplication + "\"  = "
                    + this.dbQuoted(this.settings.application) + " and ")
            .append("       \"" + this.settings.entrySection + "\"      = "
                    + this.dbQuoted(this.settings.section) + " and ")
            .append("       \"" + this.settings.entryName + "\"         = "
                    + this.dbQuoted(this.settings.entry));

    try {
        this.record = this.connection.getSingle(sqlStmt.toString());

        sqlStmt = new StringBuffer("select \"DOCUMENTATION\"").append(" from " + this.settings.source + " ")
                .append(" where \"" + this.settings.entryApplication + "\"  = "
                        + this.dbQuoted(this.settings.application) + " and ")
                .append("       \"" + this.settings.entrySection + "\"      = "
                        + this.dbQuoted(this.settings.section) + " and ")
                .append("       \"" + this.settings.entryName + "\"         = "
                        + this.dbQuoted(this.settings.entry));

        try {
            byte[] documentation = this.connection.getBlob(sqlStmt.toString());
            if (documentation != null) {
                this.record.put("documentation", new String(documentation));
            } else {
                this.record.put("documentation", "");
            }
        } catch (Exception e) {
            this.setError(e.getMessage(), SOSClassUtil.getMethodName());
        }

    } catch (Exception e) {
        this.setError(e.getMessage(), SOSClassUtil.getMethodName());
    }

    if (this.error()) {
        return false;
    }

    Iterator it = this.record.entrySet().iterator();

    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        String param = entry.getKey().toString();
        String value = entry.getValue().toString();
        if (this.getRequestValue(param) != null) {
            if (param.equals("application") || param.equals("section") || param.equals("name")) {

                if (this.getIgnoreCase()) {
                    this.record.put(param, this.getRequestValue(param).trim());

                } else {
                    this.record.put(param, this.getRequestValue(param).trim().toLowerCase());
                }
            } else {
                this.record.put(param, this.getRequestValue(param).trim());

            }
        }
    }

    // Kommt aus dem FKC Editor
    if (this.getRequestValue(this.editorName) != null) {
        this.record.put("documentation", "");
        String editorValue = this.getRequestValue(this.editorName).trim();
        if (!editorValue.equals("")) {
            // \z - endet damit
            String copy = editorValue.toLowerCase();
            //String pattern = "^(<p>(&nbsp;)+[\\s]*</p>[\\s]*)+\\z";
            String pattern = "^(<p>(&nbsp;)*((<[a-zA-Z]*>)*(</[a-zA-Z]*>)*)*[\\s]*</p>[\\s]*)+\\z";
            Pattern p = Pattern.compile(pattern);
            Matcher matcher = p.matcher(copy);
            if (!matcher.find()) {
                this.record.put("documentation", editorValue);
            }
        }
    }

    HashMap backupRecord = (HashMap) this.record.clone();

    int entryType = 0;
    try {
        entryType = Integer.parseInt(this.record.get("entry_type").toString());
    } catch (Exception e) {
        this.record.put("entry_type", "0");
    }

    try {
        Integer.parseInt(this.record.get("input_type").toString());
    } catch (Exception e) {
        this.record.put("input_type", "0");
    }

    try {
        if (Integer.parseInt(this.record.get("input_size").toString()) <= 0) {
            this.record.put("input_size", this.defaultInputSize);
        }
    } catch (Exception e) {
        this.record.put("input_size", this.defaultInputSize);
    }

    try {
        Integer.parseInt(this.record.get("display_type").toString());
    } catch (Exception e) {
        this.record.put("display_type", "0");
    }

    try {
        if (Integer.parseInt(this.record.get("display_size").toString()) <= 0) {
            this.record.put("display_size", this.defaultDisplaySize);
        }
    } catch (Exception e) {
        this.record.put("display_size", this.defaultDisplaySize);
    }

    boolean isDocumentationUpdated = false;
    boolean emptyLongValue = false;

    if (this.range.equalsIgnoreCase("application")) {

        this.record.put(this.settings.entryApplication.toLowerCase(), this.record.get("name").toString());
        this.record.put(this.settings.entrySection.toLowerCase(), this.record.get("name").toString());

        try {
            int forced = Integer.parseInt(this.record.get("forced").toString());
            if (forced != 1 && forced != -1) {
                this.record.put("forced", "0");
            }
        } catch (Exception e) {
            this.record.put("forced", "0");
        }

        String recordDocumentation = this.record.get("documentation").toString();

        backupRecord.put("documentation", "");

        if (!recordDocumentation.trim().equals("")) {
            if (!this.record.get("name").toString().equals("")) {
                try {
                    this.connection.updateBlob(this.settings.source, "DOCUMENTATION",
                            recordDocumentation.getBytes(),
                            "\"APPLICATION\"=" + this.dbQuoted(this.settings.application)
                                    + " and \"SECTION\" = " + this.dbQuoted(this.settings.section)
                                    + " and \"NAME\" = " + this.dbQuoted(this.record.get("name").toString()));
                    backupRecord.put("documentation", recordDocumentation);
                    //this.record.remove("documentation"); // wegen BLOB
                    // beim this->db->update($this->record)
                    isDocumentationUpdated = true;
                } catch (Exception e) {
                    this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                }

            }
        }

        if (!this.error()) {

            if (this.record.get("entry_type").toString().equals("1")) {
                try {
                    String hasSchema = this.connection.getSingleValue("select count(*) from "
                            + this.settings.source + " where \"" + this.settings.entryApplication + "\" = "
                            + this.dbQuoted(this.settings.application) + " and \"" + this.settings.entrySection
                            + "\" = '" + this.settings.entrySchemaSection + "' and \"" + this.settings.entryName
                            + "\" = '" + this.settings.entrySchemaSection + "'");

                    if (hasSchema.equals("") || hasSchema.equals("0")) {
                        sqlStmt = new StringBuffer("insert into " + this.settings.source
                                + " (\"APPLICATION\", \"SECTION\", \"NAME\", \"VALUE\", \"TITLE\", \"INPUT_TYPE\", \"INPUT_SIZE\", \"DISPLAY_TYPE\", \"DISPLAY_SIZE\", \"FORCED\", \"ENTRY_TYPE\", \"CREATED\", \"CREATED_BY\", \"MODIFIED\", \"MODIFIED_BY\") ")
                                        .append(" values (" + this.dbQuoted(this.record.get("name").toString())
                                                + ",'" + this.settings.entrySchemaSection + "','"
                                                + this.settings.entrySchemaSection + "',0,'"
                                                + this.dialogSectionsSchemaTitle
                                                + "', 1, 0, 0, 10, 0, 0, %now, '" + this.settings.author
                                                + "', %now, '" + this.settings.author + "')");

                        try {
                            this.connection.execute(sqlStmt.toString());
                        } catch (Exception e) {
                            // break;
                            this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                        }
                    }
                } catch (Exception e) {
                    //break;
                    this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                }
            }

            if (!this.error()) {
                sqlStmt = new StringBuffer("update " + this.settings.source + " set \""
                        + this.settings.entryApplication + "\" = "
                        + this.dbQuoted(this.record.get("name").toString()) + " where \""
                        + this.settings.entryApplication + "\" = " + this.dbQuoted(this.settings.application));
            }
        }
    } else if (this.range.equalsIgnoreCase("section")) {

        this.record.put(this.settings.entryApplication.toLowerCase(), this.settings.application);
        this.record.put(this.settings.entrySection.toLowerCase(), this.record.get("name").toString());

        try {
            int forced = Integer.parseInt(this.record.get("forced").toString());
            if (forced != 1 && forced != -1) {
                this.record.put("forced", "0");
            }
        } catch (Exception e) {
            this.record.put("forced", "0");
        }

        String recordDocumentation = this.record.get("documentation").toString();

        backupRecord.put("documentation", "");

        if (!recordDocumentation.trim().equals("")) {
            if (!this.record.get("name").toString().equals("")) {
                try {
                    this.connection.updateBlob(this.settings.source, "DOCUMENTATION",
                            recordDocumentation.getBytes(),
                            "\"APPLICATION\"=" + this.dbQuoted(this.settings.application)
                                    + " and \"SECTION\" = " + this.dbQuoted(this.settings.section)
                                    + " and \"NAME\" = " + this.dbQuoted(this.settings.entry));
                    backupRecord.put("documentation", recordDocumentation);
                    //this.record.remove("documentation"); // wegen BLOB
                    // beim this->db->update($this->record)
                    isDocumentationUpdated = true;
                } catch (Exception e) {
                    //break();
                    this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                }

            }
        }

        if (!this.error()) {

            if (this.record.get("entry_type").toString().equals("1")) {
                sqlStmt = new StringBuffer("update " + this.settings.source + " set \"INPUT_TYPE\" = "
                        + this.record.get("input_type").toString() + ", \"INPUT_SIZE\" = "
                        + this.record.get("input_size").toString() + ", \"DISPLAY_TYPE\" = "
                        + this.record.get("display_type").toString() + ", \"DISPLAY_SIZE\" = "
                        + this.record.get("display_size").toString() + ", \"FORCED\" = "
                        + this.record.get("forced").toString() + ", \"ENTRY_TYPE\" = "
                        + this.record.get("entry_type").toString() + ", \"DEFAULT_VALUE\" = "
                        + this.dbQuoted(this.record.get("default_value").toString()))
                                .append(" where \"" + this.settings.entryApplication + "\" = "
                                        + this.dbQuoted(this.settings.application) + " and \""
                                        + this.settings.entrySection + "\" = "
                                        + this.dbQuoted(this.settings.section));

                try {
                    this.connection.execute(sqlStmt.toString());
                } catch (Exception e) {
                    // break;
                    this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                }
            }

            if (!this.error()) {
                sqlStmt = new StringBuffer(
                        "update " + this.settings.source + " set \"" + this.settings.entrySection + "\" = "
                                + this.dbQuoted(this.record.get("name").toString()) + " where \""
                                + this.settings.entryApplication + "\" = "
                                + this.dbQuoted(this.settings.application) + " and \""
                                + this.settings.entrySection + "\" = " + this.dbQuoted(this.settings.section));
            }
        }

    } // end section 
    else {

        if (this.getRequestValue("binary_value") != null) { // checkbox binre inhalt leeren
            this.record.put("value", "");
            emptyLongValue = true;
        }

        //////////////////////////////
        String input_type = this.record.get("input_type").toString();
        String display_type = this.record.get("display_type").toString();

        if (input_type.equals("5")) { // Dokument
            if (display_type.equals("4")) {// Versteckt . Behandlung wie bei input_type = 6

                this.record.put("value", "");
                this.record.put("input_size", "");
                this.record.put("display_type", "4"); // Hidden

                if (this.item.equals("upload")) {
                    if (this.inputImport != null && this.inputImport.length() > 0
                            && this.importOriginalFileName != null
                            && this.importOriginalFileName.length() > 0) {
                        //this.record.put("default_value",this.importOriginalFileName);
                        try {
                            this.connection.updateBlob(this.settings.source, "LONG_VALUE", this.inputImport,
                                    "\"" + this.settings.entryApplication + "\" = "
                                            + this.dbQuoted(this.settings.application) + " and \""
                                            + this.settings.entrySection + "\" = "
                                            + this.dbQuoted(this.settings.section) + " and \""
                                            + this.settings.entryName + "\" = "
                                            + this.dbQuoted(this.record.get("name").toString()));
                        } catch (Exception e) {
                            this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                            return false;
                        }
                    } else {
                        return false;
                    }
                }
            } // display_type = 4
            else { // textarea            
                this.record.put("input_size", "");
                this.record.put("display_type", "3"); // Textarea

                if (this.record.get("value").toString().trim().length() > 0) {
                    if (this.record.get("name").toString().length() > 0) {
                        try {
                            this.connection.updateBlob(this.settings.source, "LONG_VALUE",
                                    this.record.get("value").toString().getBytes(),
                                    "\"" + this.settings.entryApplication + "\" = "
                                            + this.dbQuoted(this.settings.application) + " and \""
                                            + this.settings.entrySection + "\" = "
                                            + this.dbQuoted(this.settings.section) + " and \""
                                            + this.settings.entryName + "\" = "
                                            + this.dbQuoted(this.record.get("name").toString()));
                        } catch (Exception e) {
                            this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                            return false;
                        }

                    }
                } else {
                    this.record.put("long_value", "");
                    emptyLongValue = true;
                }
                this.record.put("value", "");
            }
        } // input_type = 5 
        else if (input_type.equals("6")) { // Dokument binr

            this.record.put("value", "");
            this.record.put("input_size", "");
            this.record.put("display_type", "4"); // Hidden

            if (this.item.equals("upload")) {

                if (this.inputImport != null && this.inputImport.length() > 0
                        && this.importOriginalFileName != null && this.importOriginalFileName.length() > 0) {
                    //this.record.put("default_value",this.importOriginalFileName);
                    try {
                        this.connection.updateBlob(this.settings.source, "LONG_VALUE", this.inputImport,
                                "\"" + this.settings.entryApplication + "\" = "
                                        + this.dbQuoted(this.settings.application) + " and \""
                                        + this.settings.entrySection + "\" = "
                                        + this.dbQuoted(this.settings.section) + " and \""
                                        + this.settings.entryName + "\" = "
                                        + this.dbQuoted(this.record.get("name").toString()));
                    } catch (Exception e) {
                        this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                        return false;
                    }
                } else {
                    return false;
                }
            }
        } // input_type = 6
        else {
            emptyLongValue = true;
        }

        /////////////////////////////////////
        sqlStmt = new StringBuffer("");

        this.record.put(this.settings.entryApplication.toLowerCase(), this.settings.application);
        this.record.put(this.settings.entrySection.toLowerCase(), this.settings.section);

        String recordDocumentation = this.record.get("documentation").toString();
        if (!recordDocumentation.trim().equals("")) {
            if (!this.record.get("name").toString().equals("")) {
                try {
                    this.connection.updateBlob(this.settings.source, "DOCUMENTATION",
                            recordDocumentation.getBytes(),
                            "\"APPLICATION\"=" + this.dbQuoted(this.settings.application)
                                    + " and \"SECTION\" = " + this.dbQuoted(this.settings.section)
                                    + " and \"NAME\" = " + this.dbQuoted(this.settings.entry));
                    //this.record.remove("documentation"); // wegen BLOB
                    // beim this->db->update($this->record)
                    isDocumentationUpdated = true;
                } catch (Exception e) {
                    //break();
                    this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                }

            }
        }

        if (!this.error()) {

            if (this.applicationType > 0) {
                try {

                    // wir sind nicht direkt in Schema, sondern in Sektions die Schema benutzen
                    if (!this.settings.section.equals(this.settings.entrySchemaSection)) {
                        // file_name aus schema
                        if (display_type.equals("4") && (input_type.equals("5") || input_type.equals("6"))) {
                            String sql_def_value = "select \"DEFAULT_VALUE\"  from " + this.settings.source
                                    + " where \"" + this.settings.entryApplication + "\" = "
                                    + this.dbQuoted(this.settings.application) + " and \""
                                    + this.settings.entrySection + "\" = '" + this.settings.entrySchemaSection
                                    + "' and \"" + this.settings.entryName + "\" = "
                                    + this.dbQuoted(this.record.get("name").toString());
                            String def_value = this.connection.getSingleValue(sql_def_value);
                            this.record.put("default_value", def_value);
                        }
                    }

                    Vector results = this.connection.getArrayAsVector(" select \"SECTION\" from "
                            + this.settings.source + " where \"" + this.settings.entryApplication + "\" = "
                            + this.dbQuoted(this.settings.application) + " and \"" + this.settings.entrySection
                            + "\" <> '" + this.settings.entrySchemaSection + "' and \""
                            + this.settings.entryName + "\" = \"" + this.settings.entrySection + "\" and \""
                            + this.settings.entryName + "\" <> \"" + this.settings.entryApplication + "\"");

                    if (results.size() > 0) {
                        for (Enumeration el = results.elements(); el.hasMoreElements();) {
                            HashMap result = (HashMap) el.nextElement();

                            String sqlAdd = "";
                            String i_s = "NULL";
                            String input_size = this.record.get("input_size").toString();

                            if (input_type.equals("5") || input_type.equals("6")) { //document & document binary 
                                sqlAdd = ", \"DEFAULT_VALUE\" = "
                                        + this.dbQuoted(this.record.get("default_value").toString());
                            } else {
                                sqlAdd = ", \"DEFAULT_VALUE\"= "
                                        + this.dbQuoted(this.record.get("default_value").toString()) + " ";
                                sqlAdd += ", \"LONG_VALUE\"  = NULL ";
                            }

                            if (input_size.length() > 0) {
                                i_s = input_size;
                            }

                            sqlStmt = new StringBuffer(" update " + this.settings.source + " set \"NAME\" = "
                                    + this.dbQuoted(this.record.get("name").toString()) + ", \"TITLE\" = "
                                    + this.dbQuoted(this.record.get("title").toString()) + ", \"INPUT_TYPE\" = "
                                    + this.record.get("input_type").toString() + ", \"INPUT_SIZE\" = " + i_s
                                    + ", \"DISPLAY_TYPE\" = " + this.record.get("display_type").toString()
                                    + ", \"DISPLAY_SIZE\" = " + this.record.get("display_size").toString()
                                    + ", \"FORCED\" = " + this.record.get("forced").toString()
                                    + ", \"ENTRY_TYPE\" = " + this.record.get("entry_type").toString() + " "
                                    + sqlAdd + " ")
                                            .append(" where \"" + this.settings.entryApplication + "\" = "
                                                    + this.dbQuoted(this.settings.application) + " and \""
                                                    + this.settings.entrySection + "\" ="
                                                    + this.dbQuoted(result.get("section").toString())
                                                    + " and \"" + this.settings.entryName + "\" ="
                                                    + this.dbQuoted(this.settings.entry));

                            try {
                                this.connection.execute(sqlStmt.toString());
                            } catch (Exception e) {
                                //break;
                                this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                            }

                        }
                    }
                } catch (Exception e) {
                    //break;
                    this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                }
            }

        }

    }

    Date date = new Date();
    this.record.put("modified", this.dateFormat.format(date));
    this.record.put("modified_by", this.settings.author);

    if (!this.error()) {
        String sqlDocumentation = "";
        String sqlLongValue = "";

        if (isDocumentationUpdated == false) {
            sqlDocumentation = ", \"DOCUMENTATION\" = NULL ";
        }

        if (emptyLongValue == true) {
            sqlLongValue = ", \"LONG_VALUE\" = NULL ";
        }

        String created = this.record.get("created").toString();
        if (created.length() > 19) {
            created = created.substring(0, 19);
        }
        String modified = this.record.get("modified").toString();
        if (modified.length() > 19) {
            modified = modified.substring(0, 19);
        }

        String value = "";
        String defaultValue = "";
        String inputSize = "";

        if (this.record.get("value").toString().equals("")) {
            value = "NULL";
        } else {
            value = this.dbQuoted(this.record.get("value").toString());
        }

        if (this.record.get("default_value").toString().equals("")) {
            defaultValue = "NULL";
        } else {
            defaultValue = this.dbQuoted(this.record.get("default_value").toString());
        }

        if (this.record.get("input_size").toString().equals("")) {
            inputSize = "NULL";
        } else {
            inputSize = this.record.get("input_size").toString();
        }

        StringBuffer sql = new StringBuffer("update " + this.settings.source + " ")
                .append(" set \"APPLICATION\"   = " + this.dbQuoted(this.record.get("application").toString())
                        + ",")
                .append("     \"SECTION\"       = " + this.dbQuoted(this.record.get("section").toString())
                        + ",")
                .append("     \"NAME\"          = " + this.dbQuoted(this.record.get("name").toString()) + ",")
                .append("     \"VALUE\"         = " + value + ",")
                .append("     \"DEFAULT_VALUE\" = " + defaultValue + ",")
                .append("     \"TITLE\"         = " + this.dbQuoted(this.record.get("title").toString()) + ",")
                .append("     \"INPUT_TYPE\"    = " + this.record.get("input_type").toString() + ",")
                .append("     \"INPUT_SIZE\"    = " + inputSize + ",")
                .append("     \"DISPLAY_TYPE\"  = " + this.record.get("display_type").toString() + ",")
                .append("     \"DISPLAY_SIZE\"  = " + this.record.get("display_size").toString() + ",")
                .append("     \"FORCED\"        = " + this.record.get("forced").toString() + ",")
                .append("     \"ENTRY_TYPE\"    = " + this.record.get("entry_type").toString() + ",")
                .append("     \"CREATED\"       = %timestamp_iso('" + created + "'),")
                .append("     \"CREATED_BY\"    = '" + this.record.get("created_by").toString() + "',")
                .append("     \"MODIFIED\"      = %timestamp_iso('" + modified + "'),")
                .append("     \"MODIFIED_BY\"   = '" + this.record.get("modified_by").toString() + "' ")
                .append(sqlDocumentation).append(sqlLongValue)
                .append(" where \"" + this.settings.entryApplication + "\"  = "
                        + this.dbQuoted(this.settings.application) + " and ")
                .append("       \"" + this.settings.entrySection + "\"      = "
                        + this.dbQuoted(this.settings.section) + " and ")
                .append("       \"" + this.settings.entryName + "\"         = "
                        + this.dbQuoted(this.settings.entry));

        try {

            this.connection.execute(sql.toString());

            if (!sqlStmt.toString().equals("")) {
                try {
                    this.connection.execute(sqlStmt.toString());
                } catch (Exception e) {
                    this.setError(e.getMessage(), SOSClassUtil.getMethodName());
                }
            }
        } catch (Exception e) {
            this.setError(e.getMessage(), SOSClassUtil.getMethodName());
        }

    }

    if (!this.error()) {
        this.connection.commit();
        this.msg = this.rb.getMessage("sos.settings.dialog.msg_store");

    }

    if (this.error()) {
        this.connection.rollback();
        this.record = backupRecord;
        return false;
    }

    return true;
}

From source file:com.flexoodb.engines.FlexJAXBDBDataEngine.java

public Collection<Object> runQuery2(String query, Class c, boolean usedefaultimplementation) throws Exception {
    Vector v = new Vector();
    Connection conn = null;//  w w w . ja  v  a2s .  c  om
    try {
        conn = (Connection) _pool.getConnection();
        String tablename = query.split("\\s")[3]; // always search the index!

        if (checkTable(tablename, conn, false)) {
            StringBuffer q = new StringBuffer("where ");
            boolean hasid = false;
            if (query.toUpperCase().indexOf("WHERE") > 0) {
                String sub = query.substring(query.toUpperCase().indexOf("WHERE") + 5);

                sub = sub.replaceAll("<=", " &lteq; ");
                sub = sub.replaceAll(">=", " &gteq; ");
                sub = sub.replaceAll("<>", " &nteq; ");
                sub = sub.replaceAll("=", " = ");
                sub = sub.replaceAll(">", " > ");
                sub = sub.replaceAll("<", " < ");
                sub = sub.replaceAll("&lteq;", "<=");
                sub = sub.replaceAll("&gteq;", ">=");
                sub = sub.replaceAll("&nteq;", "<>").trim();

                //System.out.println("from:"+sub);
                boolean done = false;
                boolean id = false;
                int seq = 0;
                String col = null;
                String condition = null;
                while (!done) {
                    int x = sub.indexOf(" ");
                    String word = sub.substring(0, x < 0 ? sub.length() : x);
                    int wlen = word.length();

                    if (word.startsWith("'")) {
                        word = sub.substring(1, sub.indexOf("'", 1));
                        wlen = word.length() + 2;
                    }

                    //System.out.println("w:"+word+"< "+wlen+" wl:"+word.length());

                    // check if its a predicate
                    if (":like:=:>:<:<=:>=:<>:".indexOf(":" + word.toLowerCase() + ":") > -1) {
                        condition = word;
                        seq = 2;
                    } else if (":and:or:not:".indexOf(":" + word.toLowerCase() + ":") > -1) {
                        q.append(" " + word.trim() + " ");
                        seq = 0;
                    } else if (seq == 0)// it must be a field!
                    {
                        seq = 1; // fields sequence
                        if (word.trim().equalsIgnoreCase("parentid") || word.trim().equalsIgnoreCase("id")) {
                            q.append(" _a." + word.trim());
                            id = true;
                            hasid = true;
                        } else if (word.trim().equalsIgnoreCase("order")) {
                            String[] order = sub.split("\\s");
                            if (!order[2].equalsIgnoreCase("id") && !order[2].equalsIgnoreCase("parentid")) {
                                // get the 3rd word -- ie the field
                                if (!q.toString().toUpperCase().endsWith("WHERE")) {
                                    q.append(" and ");
                                }

                                q.append(" (_b.element='" + order[2] + "')");

                                q.append(" " + order[0] + " by _b.value "
                                        + sub.substring(sub.indexOf(order[2]) + order[2].length()).trim());
                            } else {
                                q.append(" " + sub);
                            }
                            done = true;
                        } else if (word.trim().equalsIgnoreCase("element")
                                || word.trim().equalsIgnoreCase("limit") || word.trim().equalsIgnoreCase("desc")
                                || word.trim().equalsIgnoreCase("asc")) {
                            q.append(" " + sub);
                            done = true;
                        } else {

                            word = word.replaceAll("'", "\'").trim();
                            //q.append(" (element='"+word.trim().replaceAll("'","")+"'");
                            q.append(" (_b.element='" + word + "'");
                            //col = word.trim().replaceAll("'","");
                            col = word;
                        }
                    } else if (seq == 2) {
                        //word = word.replaceAll("'"," ");
                        word = word.replaceAll("'", "\'");
                        if (id) {
                            q.append("" + condition + "'" + word.trim() + "' and _a.id=_b.id ");
                        } else {
                            boolean valchanged = false;
                            try {
                                // we look for dates!
                                if (col != null) {
                                    Method met = c.getMethod(
                                            "get" + col.substring(0, 1).toUpperCase() + col.substring(1),
                                            (Class[]) null);
                                    Class c1 = (Class) met.getGenericReturnType();

                                    if (c1.getSimpleName().equalsIgnoreCase("XMLGregorianCalendar")
                                            && !word.isEmpty()) {
                                        //q.append(" and str_to_date(value,\"%Y-%m-%d\") "+condition+" '"+word.trim().replaceAll("'","")+"')");
                                        q.append(" and str_to_date(_b.value,\"%Y-%m-%d\") " + condition + " '"
                                                + word.trim() + "')");
                                        valchanged = true;
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            if (!valchanged) {
                                //q.append(" and value "+condition+" '"+word.trim().replaceAll("'","")+"')");
                                q.append(" and _b.value " + condition + " '" + word.trim() + "')");
                            }
                            col = null;
                        }

                        seq = 0;
                        condition = null;
                        id = false;
                    }

                    sub = sub.substring(wlen).trim();
                    if (x < 0 || sub.length() == 0) {
                        done = true;
                    }
                }

                // restructure query with parenthesis
                int i = q.toString().indexOf("_a.id=_b.id  and");
                if (i > 0) {
                    String qf = q.toString();
                    qf = qf.substring(0, i + 16) + " (" + qf.substring(i + 17).trim() + ")";
                    q = new StringBuffer(qf);
                }

            } else {
                int tl = tablename.length();
                q = new StringBuffer(query.substring(query.indexOf(tablename) + tl));
            }

            PreparedStatement ps = null;
            boolean searchindex = false;

            System.out.println(">>>>111");

            String stmt = "select distinct " + (hasid ? "_a" : "_b") + ".id from " + tablename.toLowerCase()
                    + " _a, " + tablename.toLowerCase() + "_index _b " + q.toString();
            System.out.println(">>>>" + stmt);
            ps = (PreparedStatement) conn.prepareStatement(stmt);

            if (!usedefaultimplementation) {
                //ps = (PreparedStatement) conn.prepareStatement("select distinct a.id from "+tablename.toLowerCase()+" a, "+tablename.toLowerCase()+"_index b "+q.toString());
            } else {
                //ps = (PreparedStatement) conn.prepareStatement("select distinct a.id from "+tablename.toLowerCase()+"_index a"+q.toString());
                searchindex = true;
            }

            if (_showsql) {
                System.out.println(this.getClass().getName() + " SQL Query:>" + ps.toString() + "<<");
            }
            ResultSet rec = ps.executeQuery();
            // check if a record was found
            while (rec != null && !rec.isClosed() && rec.next()) {
                String id = rec.getString("id");
                try {
                    Object o = null;
                    PreparedStatement ps2 = (PreparedStatement) conn
                            .prepareStatement("select id,parentid,content from " + tablename.toLowerCase()
                                    + " where id='" + id + "'");
                    ResultSet res = ps2.executeQuery();
                    // check if a record was found
                    if (res != null && res.next()) {
                        String i = res.getString("id");
                        String p = res.getString("parentid");
                        o = new FlexContainer(_flexutils.getObject(res.getString("content"), c));
                        ((FlexContainer) o).setId(i);
                        ((FlexContainer) o).setParentId(p);

                        ps2.close();
                    } else {
                        ps2.close();
                        if (searchindex) {
                            // then the values found must be orphans! we delete the index contents
                            removeValues(id, tablename, conn);
                        }
                    }

                    if (o != null) {
                        v.add(o);
                        Enumeration en = v.elements();
                        while (en.hasMoreElements()) {
                            en.nextElement();
                        }
                    }
                } catch (Exception g) {
                    throw g;
                }
            }
        }
    } catch (Exception f) {
        throw f;
    } finally {
        try {
            if (conn != null) {
                _pool.releaseConnection(conn);
            }

        } catch (Exception g) {
        }
    }
    return v;
}