edu.byu.wso2.apim.extensions.helpers.BYUEntityHelper.java Source code

Java tutorial

Introduction

Here is the source code for edu.byu.wso2.apim.extensions.helpers.BYUEntityHelper.java

Source

/*
 *    Copyright 2016 Brigham Young University
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 */

package edu.byu.wso2.apim.extensions.helpers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BYUEntityHelper {
    private static Log log = LogFactory.getLog(BYUEntityHelper.class);

    private static DataSource ds = null;

    public BYUEntityHelper(String dSName) {
        try {
            if (ds == null) {
                if (log.isDebugEnabled())
                    log.debug("BYUEntityHelper: looking up  datasource");

                ds = (DataSource) new InitialContext().lookup(dSName);

                if (log.isDebugEnabled())
                    log.debug("acquired datasource");
            }
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    public BYUEntity getBYUEntityFromNetId(String netid) {
        Connection con = null;
        Statement statement = null;
        ResultSet resultSet = null;
        BYUEntity byuEntity = null;
        try {
            con = ds.getConnection();
            if (log.isDebugEnabled())
                log.debug("connection acquired. creating statement and executing query");
            statement = con.createStatement();
            resultSet = statement.executeQuery("select * from pro.person where net_id = '" + netid + "'");
            if (resultSet.next()) {
                String byu_id = resultSet.getString("byu_id");
                String person_id = resultSet.getString("person_id");
                String net_id = resultSet.getString("net_id");
                String surname = resultSet.getString("surname");
                String rest_of_name = resultSet.getString("rest_of_name");
                if (log.isDebugEnabled())
                    log.debug("byu_id: " + byu_id + " person_id: " + person_id + " surname: " + surname
                            + " rest_of_name: " + rest_of_name);
                byuEntity = new BYUEntity(net_id, person_id, byu_id, surname, rest_of_name);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("resultset is empty");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
        }
        return byuEntity;
    }

    public BYUEntity getBYUEntityFromPersonId(String personid) {
        Connection con = null;
        Statement statement = null;
        ResultSet resultSet = null;
        BYUEntity byuEntity = null;
        try {
            con = ds.getConnection();
            if (log.isDebugEnabled())
                log.debug("connection acquired. creating statement and executing query");
            statement = con.createStatement();
            resultSet = statement.executeQuery("select * from pro.person where person_id = '" + personid + "'");
            if (resultSet.next()) {
                String byu_id = resultSet.getString("byu_id");
                String person_id = resultSet.getString("person_id");
                String net_id = resultSet.getString("net_id");
                String surname = resultSet.getString("surname");
                String rest_of_name = resultSet.getString("rest_of_name");
                if (log.isDebugEnabled())
                    log.debug("byu_id: " + byu_id + " person_id: " + person_id + " surname: " + surname
                            + " rest_of_name: " + rest_of_name);
                byuEntity = new BYUEntity(net_id, person_id, byu_id, surname, rest_of_name);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("resultset is empty");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
        }
        return byuEntity;
    }

    /* gets BYUEntity associated with a wso2 clientId (consumer_key) by checking the identity credential table for a mapping. */
    public BYUEntity getBYUEntityFromConsumerKey(String consumerKey) {
        Connection con = null;
        Statement statement = null;
        ResultSet resultSet = null;
        BYUEntity byuEntity = null;
        try {
            con = ds.getConnection();
            if (log.isDebugEnabled())
                log.debug("connection acquired. creating statement and executing query");
            statement = con.createStatement();
            resultSet = statement
                    .executeQuery("select * from pro.person p, iam.credential c where p.byu_id = c.byu_id "
                            + "and c.credential_type = 'WSO2_CLIENT_ID' and c.credential_id = '" + consumerKey
                            + "'");
            if (resultSet.next()) {
                String byu_id = resultSet.getString("byu_id");
                String person_id = resultSet.getString("person_id");
                String net_id = resultSet.getString("net_id");
                String surname = resultSet.getString("surname");
                String rest_of_name = resultSet.getString("rest_of_name");
                if (log.isDebugEnabled())
                    log.debug("byu_id: " + byu_id + " person_id: " + person_id + " surname: " + surname
                            + " rest_of_name: " + rest_of_name);
                byuEntity = new BYUEntity(net_id, person_id, byu_id, surname, rest_of_name);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("resultset is empty");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
        }
        return byuEntity;
    }
}