/*******************************************************************************
$Source: /cvs/repositories/openii3/project/java/examples/org/any_openeai_enterprise/gateways/brg/executors/v1_0/BasicPersonCreateImpl.java,v $
$Revision: 1.2 $
*******************************************************************************/
/**********************************************************************
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 Create-Request support for the com.sct.Person.BasicPerson object at the
* 1.0 release.
* <P>
* @author Tod Jackson (jackson4@uillinois.edu)
**/
public class BasicPersonCreateImpl
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 BasicPersonCreateImpl() {
}
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()));
// getMessageObject returns an element (the DataArea in the create-request)
xeo.buildObjectFromInput(getMessageObject());
// create the BasicPerson
try {
create((BasicPerson)xeo, 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 create(BasicPerson bPerson,
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("Creating " + msgObjectName +
" information for: " + bPerson.getInstitutionalId());
try {
// spriden
int pidm = insertSpridenData(conn, bPerson);
// spbpers
insertSpbpersData(conn, pidm, bPerson);
// now do the addresses (spraddr)
for (int i=0; i<bPerson.getAddress().size(); i++) {
Address addr = bPerson.getAddress(i);
insertSpraddrData(conn, pidm, bPerson, addr, i+1);
}
// now the phones (sprtele)
// now the email addresses (goremal)
// publish the sync
logger.info("Publishing '" + msgObjectName + "-Create-Sync' message for InstitutionalId: " + bPerson.getInstitutionalId());
PubSubProducer pubSub = (PubSubProducer)m_producerPool.getExclusiveProducer();
logger.info("Publishing sync with the " + pubSub + " producer named " + pubSub.getProducerName());
bPerson.createSync(pubSub);
// 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 int insertSpridenData(java.sql.Connection conn, BasicPerson bPerson) throws SQLException {
PreparedStatement stmt = null;
String sqlString =
"INSERT INTO SPRIDEN " +
"(SPRIDEN_PIDM, SPRIDEN_ID, SPRIDEN_FIRST_NAME, SPRIDEN_MI, SPRIDEN_LAST_NAME, SPRIDEN_ACTIVITY_DATE) " +
"VALUES (?,?,?,?,?,SYSDATE)";
int pidm = retrieveNextPidm(conn);
logger.info("PIDM for person will be: " + pidm);
try {
stmt = conn.prepareStatement(sqlString);
stmt.clearParameters();
stmt.setInt(1, pidm);
stmt.setString(2, bPerson.getInstitutionalId());
stmt.setString(3, bPerson.getName().getFirstName());
stmt.setString(4, bPerson.getName().getMiddleName());
stmt.setString(5, bPerson.getName().getLastName());
int rc = stmt.executeUpdate();
logger.info("Inserted " + rc + " SPRIDEN rows for " +
bPerson.getInstitutionalId());
}
finally {
if (stmt != null) {
stmt.close();
}
}
return pidm;
}
protected void insertSpbpersData(java.sql.Connection conn, int pidm, BasicPerson bPerson) throws SQLException {
PreparedStatement stmt = null;
String sqlString =
"INSERT INTO SPBPERS " +
"(SPBPERS_PIDM, SPBPERS_SSN, SPBPERS_ACTIVITY_DATE) " +
"VALUES (?,?,SYSDATE)";
try {
stmt = conn.prepareStatement(sqlString);
stmt.clearParameters();
stmt.setInt(1, pidm);
stmt.setString(2, bPerson.getSocialSecurityNumber());
int rc = stmt.executeUpdate();
logger.info("Inserted " + rc + " SPBPERS rows for " +
bPerson.getInstitutionalId());
}
finally {
if (stmt != null) {
stmt.close();
}
}
}
protected void insertSpraddrData(java.sql.Connection conn, int pidm,
BasicPerson bPerson, Address addr, int addressSequenceNumber)
throws EnterpriseFieldException, SQLException {
PreparedStatement stmt = null;
String sqlString =
"INSERT INTO SPRADDR " +
"(SPRADDR_PIDM, SPRADDR_ATYP_CODE, SPRADDR_STREET_LINE1, " +
"SPRADDR_STREET_LINE2, SPRADDR_CITY, SPRADDR_STAT_CODE, SPRADDR_ZIP, " +
"SPRADDR_FROM_DATE, SPRADDR_SEQNO, SPRADDR_ACTIVITY_DATE) " +
"VALUES (?,?,?,?,?,?,?,?,?,SYSDATE)";
try {
stmt = conn.prepareStatement(sqlString);
stmt.clearParameters();
stmt.setInt(1, pidm);
// TODO: use reverse translations to get code
stmt.setString(2, addr.getType("Default"));
stmt.setString(3, addr.getStreet1());
stmt.setString(4, addr.getStreet2());
stmt.setString(5, addr.getCityOrLocality());
// TODO: use reverse translations to get code (STVSTAT?)
stmt.setString(6, addr.getStateOrProvince("Default"));
stmt.setString(7, 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(8, effDate);
stmt.setInt(9, addressSequenceNumber);
int rc = stmt.executeUpdate();
logger.info("Inserted " + rc + " SPRADDR rows for " +
bPerson.getInstitutionalId());
}
finally {
if (stmt != null) {
stmt.close();
}
}
}
protected int retrieveNextPidm(java.sql.Connection conn) throws SQLException {
PreparedStatement stmt = null;
String queryString =
"select max(spriden_pidm) from spriden";
logger.info("Retrieving next pidm...");
int nextPidm = 0;
try {
stmt = conn.prepareStatement(queryString);
ResultSet results;
stmt.clearParameters();
results = stmt.executeQuery();
// now increment the pidm
while (results.next()) {
nextPidm = results.getInt(1);
nextPidm++;
}
}
finally {
if (stmt != null) {
stmt.close();
}
}
return nextPidm;
}
}