/*******************************************************************************
$Source: /cvs/repositories/openii3/project/java/examples/org/any_openeai_enterprise/gateways/brg/executors/v1_0/BasicPersonQueryImpl.java,v $
$Revision: 1.3 $
*******************************************************************************/
/**********************************************************************
This file is part of the OpenEAI sample, reference implementation,
and deployment management suite created by Tod Jackson
(tod@openeai.org) and Steve Wheat (steve@openeai.org) at
the University of Illinois Urbana-Champaign.
Copyright (C) 2002 The OpenEAI Software Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For specific licensing details and examples of how this software
can be used to implement integrations for your enterprise, visit
http://www.OpenEai.org/licensing.
*/
package org.any_openeai_enterprise.gateways.brg.executors.v1_0;
import org.jdom.Element;
import org.jdom.Document;
import org.openeai.jms.consumer.commands.CommandException;
import org.openeai.dbpool.*;
import org.openeai.moa.objects.testsuite.*;
import org.openeai.moa.XmlEnterpriseObject;
import org.openeai.moa.jmsobjects.JmsEnterpriseObject;
import org.openeai.layouts.EnterpriseLayoutManager;
import java.sql.*;
import java.util.*;
import org.any_openeai_enterprise.gateways.brg.*;
import com.sct.moa.jmsobjects.person.v1_0.*;
import com.sct.moa.objects.resources.v1_0.*;
import org.any_openeai_enterprise.gateways.brg.RequestLogicExecutorBase;
import org.any_openeai_enterprise.gateways.brg.RequestLogicExecutor;
/**
* Provides Query-Request support for the com.sct.Person.BasicPerson object at the
* 1.0 release.
* <P>
* @author Tod Jackson (jackson4@uillinois.edu)
**/
public class BasicPersonQueryImpl
extends RequestLogicExecutorBase
implements RequestLogicExecutor {
private static EnterpriseConnectionPool m_connPool = null;
private static Object _lock = new Object();
private static final String BASICPERSON_NAME = "BasicPerson.v1_0";
public BasicPersonQueryImpl() {
}
public void execute(Document replyDoc) throws CommandException {
synchronized(_lock) {
if (m_connPool == null) {
try {
// initialize db connection pool that will be used to store information from the
// message into our own repository
m_connPool = (EnterpriseConnectionPool)getExecutorAppConfig().getObject(getProperties().getProperty("dbPoolName", "DbPool"));
}
catch (Exception e) {
logger.fatal("Error initializing database connection pool.");
logger.fatal(e.getMessage(), e);
throw new CommandException(e.getMessage(), e);
}
}
}
String queryObjectName = null;
if (getMessageObject() != null) {
queryObjectName = getMessageObject().getName();
logger.info("Query object name is: " + queryObjectName);
}
try {
// build the query object from the contents of the message (QueryFilter)
XmlEnterpriseObject xeo = (XmlEnterpriseObject)getExecutorAppConfig().getObject(queryObjectName + "." + generateRelease(getMessageRelease()));
xeo.buildObjectFromInput(getMessageObject());
// query for all jobs that match criteria passed in
java.util.List listOfJeos = query((LightweightPerson)xeo, getMessageObjectName(), getMessageRelease());
if (listOfJeos.size() > 0) {
logger.info("Adding " + listOfJeos.size() + " " +
getMessageObjectName() + " objects to the Provide-Reply document.");
// remove DataArea from primed provide reply document.
replyDoc.getRootElement().
getChild(DATA_AREA).
removeChildren(getMessageObjectName());
// add an Element to the provide document for each XmlEnterpriseObjectImpl stored in the List
logger.info("Building reply document...");
for (int j=0; j<listOfJeos.size(); j++) {
XmlEnterpriseObject x = (XmlEnterpriseObject)listOfJeos.get(j);
// Put the Element in the reply document...
Element eOutput = null;
EnterpriseLayoutManager outElm = x.getOutputLayoutManager("xml");
x.setOutputLayoutManager(outElm);
eOutput = (Element)x.buildOutputFromObject();
replyDoc.getRootElement().
getChild(DATA_AREA).
addContent(eOutput);
}
logger.info("Reply document built...");
}
else {
logger.info("No rows returned, returning empty reply...");
replyDoc.getRootElement().
getChild(DATA_AREA).
removeChildren(getMessageObjectName());
}
}
catch (Exception e) {
throw new CommandException(e.getMessage(), e);
}
}
protected java.util.List query(LightweightPerson lPerson,
String msgObjectName, String msgRelease) throws CommandException {
PreparedStatement queryStmt = null;
String queryString =
"SELECT SPRIDEN_ID, SPRIDEN_FIRST_NAME, SPRIDEN_MI, SPRIDEN_LAST_NAME " +
"FROM SPRIDEN WHERE " +
"SPRIDEN_ID = '" + lPerson.getInstitutionalId() + "' " +
"AND SPRIDEN_CHANGE_IND IS NULL";
java.util.ArrayList retList = new java.util.ArrayList();
java.sql.Connection conn = null;
EnterprisePooledConnection p = null;
try {
p = m_connPool.getExclusiveConnection();
conn = p.getConnection();
}
catch (Exception e) {
logger.fatal(e.getMessage(), e);
throw new CommandException(e.getMessage(), e);
}
logger.info("Retrieving " + msgObjectName +
" information for: " + lPerson.getInstitutionalId());
try {
queryStmt = conn.prepareStatement(queryString);
ResultSet results;
queryStmt.clearParameters();
results = queryStmt.executeQuery();
logger.info("Executed query, building BasicPerson-Provide document...");
int rowCount = 0;
BasicPerson bp = (BasicPerson)getExecutorAppConfig().getObject(BASICPERSON_NAME);
EnterpriseLayoutManager outElm = bp.getOutputLayoutManager("xml");
bp.setOutputLayoutManager(outElm);
// now build the BasicPerson object to return
while (results.next()) {
bp.setInstitutionalId(results.getString(1));
Name name = bp.newName();
name.setFirstName(results.getString(2));
name.setMiddleName(results.getString(3));
name.setLastName(results.getString(4));
bp.setName(name);
retrieveAddresses(conn, bp);
retList.add(bp);
}
logger.info("Returning " + retList.size() +
" BasicPerson records for: " + lPerson.getInstitutionalId());
queryStmt.close();
m_connPool.releaseExclusiveConnection(p);
return retList;
}
catch (Exception e) {
if (queryStmt != null) {
try {
queryStmt.close();
}
catch (Exception e1) { }
}
throw new CommandException(e.getMessage(), e);
}
finally {
try {
m_connPool.releaseExclusiveConnection(p);
}
catch (Exception e2) { }
}
}
protected void retrieveAddresses(java.sql.Connection conn, BasicPerson bPerson) throws CommandException {
PreparedStatement queryStmt = null;
String queryString =
"SELECT SPRADDR_ATYP_CODE, SPRADDR_FROM_DATE, SPRADDR_STREET_LINE1, " +
"SPRADDR_STREET_LINE3, SPRADDR_CITY, SPRADDR_STAT_CODE, SPRADDR_ZIP " +
"FROM SPRADDR, SPRIDEN WHERE " +
"SPRIDEN_ID = '" + bPerson.getInstitutionalId() + "' " +
"AND SPRIDEN_CHANGE_IND IS NULL " +
"AND SPRIDEN_PIDM = SPRADDR_PIDM";
// TODO: get state and address type from validation tables???
// State (STVSTAT)
// Type (?)
logger.info("Retrieving Address " +
" information for: " + bPerson.getInstitutionalId());
try {
queryStmt = conn.prepareStatement(queryString);
ResultSet results;
queryStmt.clearParameters();
results = queryStmt.executeQuery();
logger.info("Executed query, building Address objects...");
int rowCount = 0;
// now build the Address objects and add them to the BasicPerson passed in
while (results.next()) {
Address addr = bPerson.newAddress();
addr.setType(results.getString(1));
// get date from db and build an object from it...
Calendar calendar = new GregorianCalendar();
java.util.Date dbDate = new java.util.Date(results.getDate(2).getTime());
calendar.setTime(dbDate);
com.sct.moa.objects.resources.v1_0.Date effDate = addr.newEffectiveDate();
effDate.setMonth(Integer.toString(calendar.get(Calendar.MONTH)+1));
effDate.setDay(Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
effDate.setYear(Integer.toString(calendar.get(Calendar.YEAR)));
addr.setEffectiveDate(effDate);
addr.setStreet1(results.getString(3));
addr.setStreet2(results.getString(4));
addr.setCityOrLocality(results.getString(5));
addr.setStateOrProvince(results.getString(6));
addr.setZipOrPostalCode(results.getString(7));
bPerson.addAddress(addr);
}
logger.info("Added " + bPerson.getAddress().size() +
" Addresses to the BasicPerson: " + bPerson.getInstitutionalId());
queryStmt.close();
}
catch (Exception e) {
if (queryStmt != null) {
try {
queryStmt.close();
}
catch (Exception e1) { }
}
throw new CommandException(e.getMessage(), e);
}
}
}