/*******************************************************************************
$Source: /cvs/repositories/openii3/project/java/examples/org/any_openeai_enterprise/gateways/brg/executors/v1_0/BasicPersonUpdateImpl.java,v $
$Revision: 1.1 $
*******************************************************************************/
/**********************************************************************
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 org.openeai.jms.producer.*;
import org.openeai.config.EnterpriseFieldException;
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 Update-Request support for the com.sct.Person.BasicPerson object at the
* 1.0 release.
* <P>
* @author Tod Jackson (jackson4@uillinois.edu)
**/
public class BasicPersonUpdateImpl
extends RequestLogicExecutorBase
implements RequestLogicExecutor {
private static EnterpriseConnectionPool m_connPool = null;
private static Object _lock = new Object();
private static ProducerPool m_producerPool = null;
private static final String BASICPERSON_NAME = "BasicPerson.v1_0";
public BasicPersonUpdateImpl() {
}
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);
}
}
// retrieve Sync Publisher from AppConfig. This object will be used to publish
// sync messages when create, delete or update message actions are performed
// in this authoritative system.
if (m_producerPool == null) {
try {
m_producerPool = (ProducerPool)getExecutorAppConfig().getObject("SyncPublisher");
}
catch (Exception e) {
logger.fatal("Error retrieving 'SyncPublisher' PubSubProducer from AppConfig");
logger.fatal(e.getMessage(), e);
throw new CommandException(e.getMessage(), e);
}
}
}
try {
// build the message object from the content of the message (DataArea)
XmlEnterpriseObject xeo = (XmlEnterpriseObject)getExecutorAppConfig().
getObject(getMessageObjectName() + "." + generateRelease(getMessageRelease()));
XmlEnterpriseObject baselineXeo = (XmlEnterpriseObject)getExecutorAppConfig().
getObject(getMessageObjectName() + "." + generateRelease(getMessageRelease()));
// getMessageObject returns an element (the DataArea in the create-request)
xeo.buildObjectFromInput(getMessageObject());
baselineXeo.buildObjectFromInput(getBaseline());
// create the BasicPerson
try {
update((BasicPerson)xeo, (BasicPerson)baselineXeo, getMessageObjectName(), getMessageRelease());
}
catch (Exception e) {
String errMessage = "Exception occurred Creating the " + getMessageObjectName() +
" Java object in the database. Exception: " + e.getMessage();
logger.fatal(errMessage);
throw new CommandException(e.getMessage(), e);
}
logger.info("Reply document built...");
/*
// remove DataArea from primed provide reply document.
replyDoc.getRootElement().
getChild(DATA_AREA).
removeChildren(getMessageObjectName());
replyDoc.getRootElement().
getChild(DATA_AREA).
addContent(eOutput);
*/
}
catch (Exception e) {
throw new CommandException(e.getMessage(), e);
}
}
protected void update(BasicPerson bPerson, BasicPerson bPersonBaseline,
String msgObjectName, String msgRelease) throws CommandException {
java.sql.Connection conn = null;
EnterprisePooledConnection p = null;
try {
p = m_connPool.getExclusiveConnection();
conn = p.getConnection();
conn.setAutoCommit(false);
}
catch (Exception e) {
logger.fatal(e.getMessage(), e);
throw new CommandException(e.getMessage(), e);
}
logger.info("Updating " + msgObjectName +
" information for: " + bPerson.getInstitutionalId());
try {
conn.setAutoCommit(true);
int pidm = retrievePidm(conn, bPerson);
logger.info("PIDM is: " + pidm);
conn.setAutoCommit(false);
// spriden
updateSpridenData(conn, pidm, bPerson, bPersonBaseline);
// spbpers
updateSpbpersData(conn, pidm, bPerson, bPersonBaseline);
// now do the addresses (spraddr)
updateSpraddrData(conn, pidm, bPerson, bPersonBaseline);
// now the phones (sprtele)
// now the email addresses (goremal)
// publish the sync
logger.info("Publishing '" + msgObjectName + "-Update-Sync' message for InstitutionalId: " + bPerson.getInstitutionalId());
PubSubProducer pubSub = (PubSubProducer)m_producerPool.getExclusiveProducer();
bPerson.setBaseline(bPersonBaseline);
bPerson.updateSync(pubSub);
logger.info("Published sync message");
// commit the transaction
conn.commit();
logger.info("Committed the transaction.");
m_producerPool.releaseProducer(pubSub);
m_connPool.releaseExclusiveConnection(p);
}
catch (Exception e) {
try {
conn.rollback();
}
catch (Exception e1) { }
throw new CommandException(e.getMessage(), e);
}
finally {
try {
m_connPool.releaseExclusiveConnection(p);
}
catch (Exception e2) { }
}
}
protected void updateSpridenData(java.sql.Connection conn, int pidm, BasicPerson bPerson, BasicPerson bPersonBaseline) throws SQLException {
PreparedStatement stmt = null;
String sqlString =
"UPDATE SPRIDEN " +
"SET SPRIDEN_FIRST_NAME=?, SPRIDEN_MI=?, " +
"SPRIDEN_LAST_NAME=?, SPRIDEN_ACTIVITY_DATE=SYSDATE " +
"WHERE SPRIDEN_PIDM=?";
try {
logger.info("Updating SPRIDEN data for " +
bPerson.getInstitutionalId());
stmt = conn.prepareStatement(sqlString);
stmt.clearParameters();
stmt.setString(1, bPerson.getName().getFirstName());
stmt.setString(2, bPerson.getName().getMiddleName());
stmt.setString(3, bPerson.getName().getLastName());
stmt.setInt(4, pidm);
int rc = stmt.executeUpdate();
logger.info("Updated " + rc + " SPRIDEN rows for " +
bPerson.getInstitutionalId());
}
finally {
if (stmt != null) {
stmt.close();
}
}
}
protected void updateSpbpersData(java.sql.Connection conn, int pidm, BasicPerson bPerson, BasicPerson bPersonBaseline) throws SQLException {
PreparedStatement stmt = null;
String sqlString =
"UPDATE SPBPERS " +
"SET SPBPERS_SSN=?, SPBPERS_ACTIVITY_DATE=SYSDATE " +
"WHERE SPBPERS_PIDM = " + pidm;
try {
logger.info("Updating SPBPERS data for " +
bPerson.getInstitutionalId());
stmt = conn.prepareStatement(sqlString);
stmt.clearParameters();
stmt.setString(1, bPerson.getSocialSecurityNumber());
int rc = stmt.executeUpdate();
logger.info("Updated " + rc + " SPBPERS rows for " +
bPerson.getInstitutionalId());
}
finally {
if (stmt != null) {
stmt.close();
}
}
}
protected void updateSpraddrData(java.sql.Connection conn, int pidm, BasicPerson bPerson, BasicPerson bPersonBaseline) throws EnterpriseFieldException, SQLException {
PreparedStatement stmt = null;
String sqlString =
"UPDATE SPRADDR " +
"SET SPRADDR_STREET_LINE1=?, " +
"SPRADDR_STREET_LINE2=?, SPRADDR_CITY=?, SPRADDR_STAT_CODE=?, SPRADDR_ZIP=?, " +
"SPRADDR_FROM_DATE=?, SPRADDR_ACTIVITY_DATE=SYSDATE " +
"WHERE SPRADDR_PIDM = " + pidm + " " +
"AND SPRADDR_ATYP_CODE = ?";
try {
logger.info("Updating SPRADDR data for " +
bPerson.getInstitutionalId());
stmt = conn.prepareStatement(sqlString);
for (int i=0; i<bPerson.getAddress().size(); i++) {
stmt.clearParameters();
Address addr = bPerson.getAddress(i);
stmt.setString(1, addr.getStreet1());
stmt.setString(2, addr.getStreet2());
stmt.setString(3, addr.getCityOrLocality());
// TODO: use reverse translations to get code (STVSTAT?)
stmt.setString(4, addr.getStateOrProvince("Default"));
stmt.setString(5, addr.getZipOrPostalCode());
java.sql.Date effDate = null;
try {
effDate = new java.sql.Date(addr.getEffectiveDate().toDate().getTime());
}
catch (Exception e) {
logger.warn("Could not parse addresses effective date. " +
"Processing will continue. Exception: " + e.getMessage(), e);
}
stmt.setDate(6, effDate);
// TODO: use reverse translations to get code
stmt.setString(7, addr.getType("Default"));
int rc = stmt.executeUpdate();
if (rc == 0) {
// turn it into an insert
BasicPersonCreateImpl bpci = new BasicPersonCreateImpl();
bpci.insertSpraddrData(conn, pidm, bPerson, addr, i+1);
}
// TODO: figure out how to delete the ones that are no longer in the list
// of addresses included in this BasicPerson...
}
}
finally {
if (stmt != null) {
stmt.close();
}
}
}
protected int retrievePidm(java.sql.Connection conn, BasicPerson bPerson) throws SQLException {
PreparedStatement stmt = null;
String queryString =
"select spriden_pidm " +
"from spriden " +
"where spriden_id = '" + bPerson.getInstitutionalId() + "' " +
"and spriden_change_ind is null";
logger.info("Retrieving pidm for person...");
int pidm = 0;
try {
stmt = conn.prepareStatement(queryString);
ResultSet results;
stmt.clearParameters();
results = stmt.executeQuery();
// now increment the pidm
if (results.next()) {
pidm = results.getInt(1);
}
}
finally {
if (stmt != null) {
stmt.close();
}
}
return pidm;
}
}