/*******************************************************************************
$Source: /cvs/repositories/openii3/project/java/examples/org/any_openeai_enterprise/gateways/uportal/BasicPersonSyncCommand.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) 2004 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.uportal;
// Core Java
import javax.jms.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.text.*;
// JDOM
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
// OpenEAI Foundation
import org.openeai.jms.consumer.commands.*;
import org.openeai.layouts.EnterpriseLayoutException;
import org.openeai.dbpool.*;
import org.openeai.config.*;
import org.openeai.xml.*;
import org.openeai.loggingutils.*;
// OpenEAI message objects.
import org.openeai.moa.objects.resources.Error;
// Any ERP Vendor message objects.
import com.any_erp_vendor.moa.jmsobjects.person.v1_0.BasicPerson;
import com.any_erp_vendor.moa.objects.resources.v1_0.Email;
/**
* This command handles BasicPerson.Update-Sync messages to apply changes to
* person data to the LDAP directory server.
* <P>
*
*/
public class BasicPersonSyncCommand extends AbstractPortalSyncCommand
implements SyncCommand {
private EnterpriseConnectionPool m_connPool = null;
private boolean m_publishErrorsForMissingEntries = false;
/**
* Constructor
*/
public BasicPersonSyncCommand(CommandConfig cConfig) throws
InstantiationException {
super(cConfig);
// Get the value of the publishErrorsForMissingEntries property.
if (getProperties().getProperty("publishErrorsForMissingEntries", "false")
.equalsIgnoreCase("true")) {
setPublishErrorsForMissingEntries(true);
}
logger.info("[BasicPersonSyncCommand] The value of " +
"publishErrorsForMissingEntries is: " +
getPublishErrorsForMissingEntries());
// Verify that all message objects required are in AppConfig.
try {
BasicPerson bPerson = (BasicPerson)getAppConfig()
.getObject("BasicPerson");
}
catch (EnterpriseConfigurationObjectException eoce) {
// An error occurred retrieving a required object from AppConfig. Log it
// and throw an exception.
String errMsg = "An error occurred retrieving a required object from " +
"AppConfig. The exception is: " + eoce.getMessage();
logger.fatal("[BasicPersonSyncCommand] " + errMsg);
throw new InstantiationException(errMsg);
}
logger.info("[BasicPersonSyncCommand] instantiated successfully.");
}
public void execute(int messageNumber, Message aMessage) {
// Initialize some working variables.
Properties props = getProperties();
// Create the input document from the JMS message passed in.
Document inDoc = null;
try { inDoc = initializeInput(messageNumber, aMessage); }
catch (JMSException jmse) {
// An error occurred creating the input document from the JMS message
// passed in. Log it and publish a sync error.
Error error = new Error();
error.setErrorNumber("OpenEAI-2101");
error.setErrorDescription("An error occurred creating the input " +
"document from the JMS message passed in. The exception is: " +
jmse.getMessage());
logger.fatal("[BasicPersonSyncCommand.execute]" +
error.getErrorDescription());
return;
}
// Get the message metadata.
Element eControlArea = getControlArea(inDoc.getRootElement());
String messageCategory = eControlArea.getAttribute("messageCategory")
.getValue();
String messageObject = eControlArea.getAttribute("messageObject")
.getValue();
String messageRelease = eControlArea.getAttribute("messageRelease")
.getValue();
String messageAction = eControlArea.getAttribute("messageAction")
.getValue();
String messageType = eControlArea.getAttribute("messageType").getValue();
logger.info("[BasicPersonSyncCommand.execute] Received a(n) " +
messageCategory + "." + messageObject + "." + messageAction + "-" +
messageType + " (release " + messageRelease + ") message.");
// Verify that this is a message we support.
ArrayList errors = new ArrayList();
if (messageObject.equalsIgnoreCase("BasicPerson") == false) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("OpenEAI-1002");
error.setErrorDescription("Message object '" + messageObject +
"' is not supported.");
errors.add(error);
}
if (messageType.equalsIgnoreCase("Sync") == false) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("OpenEAI-1002");
error.setErrorDescription("Message type '" + messageType + "' is not " +
"supported.");
errors.add(errors);
}
if (errors.size() > 0) {
ListIterator iterator = errors.listIterator();
while (iterator.hasNext()) {
Error error = (Error)iterator.next();
logger.fatal("[BasicPersonSyncCommand.execute] " +
error.getErrorDescription());
}
publishSyncError(eControlArea, errors);
return;
}
// Get two configured BasicPerson objects from AppConfig.
BasicPerson currentBasicPerson = null;
BasicPerson newBasicPerson = null;
try {
currentBasicPerson = (BasicPerson)getAppConfig().getObject("BasicPerson");
newBasicPerson = (BasicPerson)getAppConfig().getObject("BasicPerson");
}
catch (EnterpriseConfigurationObjectException ecoe) {
// An error occurred getting an object from AppConfig. Log it and
// publish a sync error message.
Error error = new Error();
error.setType("application");
error.setErrorNumber("OpenEAI-3001");
error.setErrorDescription("An error occurred getting an object from " +
"AppConfig. the exception is: " + ecoe.getMessage());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ecoe);
return;
}
// Handle a BasicPerson.Update-Sync
if (messageAction.equalsIgnoreCase("Update")) {
// Get the baseline state of the BasicPerson and build a BasicPerson
// object.
Element eBaselinePerson = inDoc.getRootElement().getChild("DataArea")
.getChild("BaselineData").getChild("BasicPerson");
try {
currentBasicPerson.buildObjectFromInput(eBaselinePerson);
}
catch (EnterpriseLayoutException ele) {
// An error occurred building the BasicPerson object from the
// BasicPerson element contained in the BaselineData element of the
// message. Log it and publish a sync error message.
Error error = new Error();
error.setType("system");
error.setErrorNumber("DirectoryServiceGateway-1001");
error.setErrorDescription("An error occurred building the BasicPerson "
+ "object from the BasicPerson element contained in the " +
"BaselineData element of the message. The exception is: " +
ele.getMessage());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ele);
return;
}
// Get the new state of the BasicPerson and build a BasicPerson
// object.
Element eNewPerson = inDoc.getRootElement().getChild("DataArea")
.getChild("NewData").getChild("BasicPerson");
try {
newBasicPerson.buildObjectFromInput(eNewPerson);
}
catch (EnterpriseLayoutException ele) {
// An error occurred building the BasicPerson object from thw
// BasicPerson element contained in the NewData element of
// the message. Log it and publish a sync error message.
Error error = new Error();
error.setType("system");
error.setErrorNumber("DirectoryServiceGateway-1002");
error.setErrorDescription("An error occurred building the " +
"BasicPerson object from the BasicPerson element contained in " +
"the NewData element of the message. The exception is: " +
ele.getMessage());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ele);
return;
}
// Determine whether the user exists in the portal database.
String instId = currentBasicPerson.getInstitutionalId();
PortalPerson portalPerson = null;
try { portalPerson = retrievePortalPerson(instId); }
catch (CommandException ce) {
// An error occurred querying the portal database to determine whether
// the user exists. Log it, publish a error message, and return.
String errMsg = "An error occurred querying the portal database to " +
"determine whether the user exists.";
logger.debug("[PasswordSyncCommand.execute] " + errMsg);
Error error = new Error();
error.setType("system");
error.setErrorNumber("UportalGateway-2001");
error.setErrorDescription(errMsg);
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ce);
return;
}
// If there are no matching entries and the publishErrorsForMissingEntries
// property is true, publish a error message indicating that the user does
// not exist. Otherwise, log the fact that the user does not exist and
// return.
if (portalPerson == null) {
if (getPublishErrorsForMissingEntries() == true) {
// --- Publish the sync error.
Error error = new Error();
error.setType("application");
error.setErrorNumber("UportalGateway-1005");
error.setErrorDescription("No user with user ID " + instId +
" exists in the portal. Cannot reset the password for this user.");
logger.fatal("[BasicPersonSyncCommand.execute] " +
error.getErrorDescription());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors);
return;
}
else {
// --- Just log it.
logger.info("[BasicPersonSyncCommand.execute] No user with user ID " +
instId + " exists in the portal. Cannot reset the password for " +
"this user.");
return;
}
}
// Otherwise, the user already exists in the portal, so determine if their
// name and e-mail have changed and, if so, update them.
else {
// Get the new first name, last name, and e-mail address.
String newFirstName = newBasicPerson.getName().getFirstName();
String newLastName = newBasicPerson.getName().getLastName();
String email;
List emailAddresses = newBasicPerson.getEmail();
ListIterator li = emailAddresses.listIterator();
switch (emailAddresses.size()) {
// If there are no e-mail addresses in the list, set the value of the
// e-mail address to null.
case 0:
email = null;
break;
// If there is one e-mail address in the list, that will become the
// value of the e-mail address.
case 1:
Email emailAddress = (Email)li.next();
email = emailAddress.getEmailAddress();
break;
// If there are multiple e-mail addresses in the list, uPortal can
// store only one of them, so get any one that has the preferred
// indicator set or take the first e-mail address in the list if
// none are preferred.
default:
emailAddress = (Email)li.next();
email = emailAddress.getEmailAddress();
while (li.hasNext()) {
emailAddress = (Email)li.next();
if (emailAddress.getPreferred() != null &&
emailAddress.getPreferred().equalsIgnoreCase("Yes")) {
email = emailAddress.getEmailAddress();
}
}
break;
}
// Determine whether the name or e-mail address has changed.
PortalPerson newPortalPerson = new PortalPerson(portalPerson);
newPortalPerson.setFirstName(newFirstName);
newPortalPerson.setLastName(newLastName);
newPortalPerson.setEmailAddress(email);
// If there has been a change, update the person information. Otherwise
// log the fact that no update is required.
if (newPortalPerson.equals(portalPerson) == false) {
logger.info("[BasicPersonSyncCommand.execute] Updating portal " +
"person information for portal person " + portalPerson
.getFirstName() + " " + portalPerson.getLastName() + ".");
logger.debug("[BasicPersonSyncCommand.execute] current portal " +
"person: " + portalPerson);
logger.debug("[BasicPersonSyncCommand.execute] new portal " +
"person: " + newPortalPerson);
try { updatePortalPerson(portalPerson, newPortalPerson); }
catch (CommandException ce) {
// An error occurred accessing the portal database to update the
// the portal user information. Log it, publish a error message,
// and return.
String errMsg = "An error occurred accessing the portal database " +
"to update portal user information.";
logger.debug("[BasicPersonSyncCommand.execute] " + errMsg);
Error error = new Error();
error.setType("system");
error.setErrorNumber("UportalGateway-2001");
error.setErrorDescription(errMsg);
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ce);
return;
}
}
else {
logger.info("[BasicPersonSyncCommand.execute] No change in relevant "
+ "information. No portal person information update required for " +
"portal person " + portalPerson
.getFirstName() + " " + portalPerson.getLastName() + " (userId=" +
portalPerson.getUserId() + ", userName=" + portalPerson
.getUserName() + ").");
}
}
}
// Otherwise, the message is unsupported. Log it and publish a sync error.
else {
Error error = new Error();
error.setType("application");
error.setErrorNumber("OpenEAI-1002");
error.setErrorDescription("Unsupported message action. This command only "
+ "supports an action of 'update'");
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors);
return;
}
}
private boolean getPublishErrorsForMissingEntries() {
return m_publishErrorsForMissingEntries;
}
private void setPublishErrorsForMissingEntries(boolean publishErrors) {
m_publishErrorsForMissingEntries = publishErrors;
}
}