Java tutorial
/* * 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.helper; 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() { try { if (ds == null) { if (log.isDebugEnabled()) log.debug("CustomTokenGenerator: looking up datasource"); ds = (DataSource) new InitialContext().lookup("jdbc/BYUPRODB"); if (log.isDebugEnabled()) log.debug("acquired datasource"); } } catch (NamingException e) { e.printStackTrace(); } } public BYUEntityEmail getBYUEntityEmailFromNetId(String netid) { Connection con = null; Statement statement = null; ResultSet resultSet = null; BYUEntityEmail byuEntityEmail = new BYUEntityEmail(netid); try { con = ds.getConnection(); if (log.isDebugEnabled()) log.debug("connection acquired. creating statement and executing query"); statement = con.createStatement(); resultSet = statement.executeQuery( "select email_address_type, email_address from iam.email_address ea, iam.credential cred " + " where ea.byu_id = cred.byu_id" + " and cred.credential_type = 'NET_ID' " + " and cred.credential_id = '" + netid + "'"); while (resultSet.next()) { String emailAddressType = resultSet.getString("email_address_type"); String emailAddress = resultSet.getString("email_address"); byuEntityEmail.addEmail(emailAddressType, emailAddress); if (log.isDebugEnabled()) log.debug("netid - " + netid + " emailAddressType: " + emailAddressType + " emailAddress: " + emailAddress); } } 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 byuEntityEmail; } 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 + "'"); byuEntity = getByuEntity(resultSet); } 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; } private BYUEntity getByuEntity(ResultSet resultSet) throws SQLException { BYUEntity byuEntity = null; 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"); String surname_position = resultSet.getString("surname_position"); String prefix = resultSet.getString("prefix"); String suffix = resultSet.getString("suffix"); String sort_name = resultSet.getString("sort_name"); String preferred_firstname = resultSet.getString("preferred_first_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, surname_position, prefix, suffix, sort_name, preferred_firstname); } else { if (log.isDebugEnabled()) { log.debug("resultset is empty"); } } 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 + "'"); byuEntity = getByuEntity(resultSet); } 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; } }