All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsmart.zerocode.zerocodejavaexec.DbSqlExecutor Maven / Gradle / Ivy

The newest version!
package org.jsmart.zerocode.zerocodejavaexec;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.util.Arrays;
import org.jsmart.zerocode.zerocodejavaexec.pojo.DbResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DbSqlExecutor {
    private static final Logger LOGGER = LoggerFactory.getLogger(DbSqlExecutor.class);

    @Inject(optional = true)
    @Named("db.conn.user.name")
    private String dbUserName;

    @Inject(optional = true)
    @Named("db.conn.password")
    private String dbPassword;


    public static final String RESULTS_KEY = "results";

    public Map> fetchDbCustomers(String sqlStatement){

        Map> results = executeSelectSql(sqlStatement);

        return results;
    }

    public Map> fetchDbCustomersByName(String name){

        Map> results = executeSelectSql("any sql");

        DbResult foundResult = results.get(RESULTS_KEY).stream()
                .filter(thisRecord -> thisRecord.getName().equals(name))
                .findFirst()
                .get();

        results.put(RESULTS_KEY, Arrays.asList(foundResult));

        return results;
    }

    private Map> executeSelectSql(String sqlStatement) {
        LOGGER.debug("\n\nDB Connection user:{}, password:{}\n\n", dbUserName, dbPassword);

        /**
         * ----------------------------------------------------------------------------------
         * // Your code goes here. //
         * e.g.
         * - You can use JDBC-connection/spring JDBC template and fetch the results using
         * the above 'userName and password'
         * ----------------------------------------------------------------------------------
         */

        /**
         * Once you finished the DB execution and you will get the list of results from DB in
         * the 'results' list. Values hard coded below for the example understanding only.
         * In reality you get these results from the DB.
         */
        List results = new ArrayList<>();
        results.add(new DbResult(1, "Elon Musk"));
        results.add(new DbResult(2, "Jeff Bezos"));

        Map> resultsMap = new HashMap<>();
        resultsMap.put(RESULTS_KEY, results);

        return resultsMap;

    }

    public void setDbUserName(String dbUserName) {
        this.dbUserName = dbUserName;
    }

    public void setDbPassword(String dbPassword) {
        this.dbPassword = dbPassword;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy