Você está na página 1de 13

package oracle.apps.xdo.oa.

cp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Properties;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import oracle.apps.fnd.common.Log;
import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.cp.request.CpContext;
import oracle.apps.fnd.cp.request.JavaConcurrentProgram;
import oracle.apps.fnd.cp.request.LogFile;
import oracle.apps.fnd.cp.request.OutFile;
import oracle.apps.fnd.cp.request.RemoteFile;
import oracle.apps.fnd.cp.request.ReqCompletion;
import oracle.apps.fnd.cp.request.ReqDetails;
import oracle.apps.fnd.util.NameValueType;
import oracle.apps.fnd.util.ParameterList;
import oracle.apps.xdo.XDOException;
import oracle.apps.xdo.batch.BurstingProcessorEngine;
import oracle.apps.xdo.batch.bursting.DocumentStatus;
import oracle.apps.xdo.common.config.ConfigLocation;
import oracle.apps.xdo.common.config.ConfigReader;
import oracle.apps.xdo.common.log.Logger;
import oracle.apps.xdo.common.log.XDOLogImpl;
import oracle.apps.xdo.common.net.XDOStreamHandlerFactory;
import oracle.apps.xdo.oa.util.OAHelper;
import oracle.apps.xdo.oa.util.OAXDOLogImpl;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;

public class JCP4XDOBurstingEngine


implements JavaConcurrentProgram
{
public static final String RCS_ID = "$Header: JCP4XDOBurstingEngine.java 120.4
2010/04/07 16:37:06 bgkim ship $";
public static final boolean RCS_ID_RECORDED =
VersionInfo.recordClassVersion("$Header: JCP4XDOBurstingEngine.java 120.4
2010/04/07 16:37:06 bgkim ship $", "oracle.apps.xdo.oa.cp");

private CpContext ccntxt;


private LogFile lfile;

private boolean debug = false;


private Connection mJConn;
private String tempDir = null;
private Vector status = null;
private boolean burstingStatus = false;
String fileSeperator = System.getProperty("file.separator");

public JCP4XDOBurstingEngine() {}

public void runProgram(CpContext paramCpContext)


{
lfile = paramCpContext.getLogFile();
File localFile = new File(lfile.getFileName());
ccntxt = paramCpContext;

Properties localProperties = CPUtil.getSessionProperties(paramCpContext);

ReqCompletion localReqCompletion = paramCpContext.getReqCompletion();


String str1 = null;
String str2 = null;
int i = 0;
Object localObject1 = null;
Object localObject2 = null;
Object localObject3 = null;
String str3 = "N";
String str4 = null;
Object localObject4 = null;
Object localObject5 = null;
BurstingProcessorEngine localBurstingProcessorEngine = null;
int j = paramCpContext.getReqDetails().getRequestId();
Object localObject6;
try {
Class localClass = Class.forName("oracle.apps.xdo.common.MetaInfo");
localObject6 = localClass.getField("VERSION_SHORT");
str1 = (String)((Field)localObject6).get(null);
}
catch (Exception localException1) {
lfile.writeln(localException1.toString(), 0);
}

lfile.writeln("XML/BI Publisher Version : " + str1, 0);

try
{
tempDir = getSystemTempDirectory();

if ((tempDir == null) || (tempDir.length() == 0)) {


throw new NullPointerException("Error : property system-temp-dir has not
been set in XDO admin page or configuration file");
}

mJConn = paramCpContext.getJDBCConnection();
ParameterList localParameterList = paramCpContext.getParameterList();

while (localParameterList.hasMoreElements())
{

localObject6 = localParameterList.nextParameter();

str2 = str2 + ((NameValueType)localObject6).getName() + ":" +


((NameValueType)localObject6).getValue();

localProperties.put("user-variable:cp:" +
((NameValueType)localObject6).getName(), ((NameValueType)localObject6).getValue());

if (((NameValueType)localObject6).getName().equals("ReportRequestID"))
{
i = Integer.parseInt(((NameValueType)localObject6).getValue());
}
else if (((NameValueType)localObject6).getName().equals("DebugFlag"))
{
str3 = ((NameValueType)localObject6).getValue();
}
}

debug = (str3.equals("Y"));

if (debug) {
lfile.writeln("Request ID: " + j, 1);
lfile.writeln("All Parameters: " + localParameterList.getString(), 1);
lfile.writeln("Report Req ID: " + i, 1);
lfile.writeln("Debug Flag: " + str3, 1);
}

lfile.writeln("Updating request description", 0);


localObject6 = "update FND_CONCURRENT_REQUESTS set DESCRIPTION='Bursting '
|| ( select USER_CONCURRENT_PROGRAM_NAME from
FND_CONC_REQ_SUMMARY_V where request_id= :1 ) where request_id = :2 ";

OracleCallableStatement localOracleCallableStatement =
(OracleCallableStatement)mJConn.prepareCall((String)localObject6);

localOracleCallableStatement.setInt(1, i);
localOracleCallableStatement.setInt(2, j);
localOracleCallableStatement.execute();
localOracleCallableStatement.close();

mJConn.commit();

if (debug) { lfile.writeln("Updated description", 0);


}

lfile.writeln("Retrieving XML request information", 0);

localObject6 = "select a.OUTFILE_NODE_NAME, a.OUTFILE_NAME,


b.USER_CONCURRENT_PROGRAM_NAME from FND_CONCURRENT_REQUESTS a,
FND_CONC_REQ_SUMMARY_V b where a.request_id= :1 and a.request_id=b.request_id";

OraclePreparedStatement localOraclePreparedStatement =
(OraclePreparedStatement)mJConn.prepareStatement((String)localObject6);

localOraclePreparedStatement.setInt(1, i);

OracleResultSet localOracleResultSet =
(OracleResultSet)localOraclePreparedStatement.executeQuery();

localOracleResultSet.next();
String str5 = localOracleResultSet.getString(1);
String str6 = localOracleResultSet.getString(2);
String str7 = localOracleResultSet.getString(3);
localProperties.put("user-variable:cp:reportdescription", str7);
localProperties.put("user-variable:cp:parent_request_id",
Integer.toString(i));

localOracleResultSet.close();
localOraclePreparedStatement.close();

if (debug) { lfile.writeln("Node Name:" + str5, 1);


}
lfile.writeln("Preparing parameters", 0);

String str8 = paramCpContext.getOutFile().getFileName();

if (debug) { lfile.writeln(str4 + " output =" + str8, 1);


}
FileOutputStream localFileOutputStream = new FileOutputStream(str8);

RemoteFile localRemoteFile = new RemoteFile(paramCpContext, str5, str6,


"TEXT");
String str9 = localRemoteFile.getFile().getAbsolutePath();

if (debug) { lfile.writeln("inputfilename =" + str9, 1);


}
if ((str9 == null) || (str9.equals("")))
{

localReqCompletion.setCompletion(2, "Error has occured. Please check the


log file");

throw new NullPointerException("Error : null or invalid XML Data file.


Please check the parent request");
}

if (debug) { lfile.writeln("Data XML File:" + str9, 1);


}

InputStream localInputStream = getControlFile(i);

if (localInputStream == null)
{
localReqCompletion.setCompletion(2, "Error has occured while accessing
control file..");

throw new NullPointerException("Error : Error while accessing Bursting


Control file from Template Manager...");
}

lfile.writeln("Set Bursting parameters..", 1);

XDOStreamHandlerFactory.registerNewProtocols();

localBurstingProcessorEngine = new BurstingProcessorEngine();

localBurstingProcessorEngine.setTempDirectory(tempDir);

localBurstingProcessorEngine.setXMLAPI(localInputStream);

localBurstingProcessorEngine.setData(str9);

localProperties.put("burstng-source", "EBS");
set_OA_MEDIA_Property(localProperties);
AddCoreProperties(localProperties);

lfile.writeln("Bursting propertes.....", 1);


lfile.writeln(localProperties.toString(), 1);
localBurstingProcessorEngine.setConfig(localProperties);

lfile.writeln("Start bursting process..", 1);


if (debug)
{
try
{
XDOLogImpl localXDOLogImpl = new XDOLogImpl();
localXDOLogImpl.setDestination(new FileOutputStream(lfile.getFileName(),
true));
Logger.setLog(localXDOLogImpl);

Logger.setLevel(1);
}
catch (Exception localException3) {
lfile.writeln("Error during initiating the XDO log...", 1);
Logger.init();
Logger.setLevel(1);
}
}
status = localBurstingProcessorEngine.process();

lfile.writeln("Bursting process complete..", 1);

lfile.writeln("Generating Bursting Status Report..", 1);

BurstingStatusReport localBurstingStatusReport = new


BurstingStatusReport(status, ccntxt, i, str7, zipOutputFiles(status));

localBurstingStatusReport.generate();

localInputStream.close();

if (burstingStatus) {
localReqCompletion.setCompletion(0, "Request Completed Normal");
} else {
localReqCompletion.setCompletion(2, "");
}

}
catch (SQLException localSQLException)
{

lfile.writeln("--SQLException", 1);

lfile.writeln(getErrorStack(localSQLException), 1);

localReqCompletion.setCompletion(2, localSQLException.getMessage());

}
catch (XDOException localXDOException)
{

lfile.writeln("--XDOException", 1);
lfile.writeln(getErrorStack(localXDOException), 1);

localReqCompletion.setCompletion(2, localXDOException.getMessage());

}
catch (Exception localException2)
{

lfile.writeln("--Exception", 1);

lfile.writeln(localException2.getMessage(), 1);

lfile.writeln(getErrorStack(localException2), 1);

localReqCompletion.setCompletion(2, localException2.getMessage());

}
finally
{

paramCpContext.releaseJDBCConnection();
if (localBurstingProcessorEngine != null) {
localBurstingProcessorEngine.deletTemporaryOutputFiles();
}
}
}

private String getErrorStack(Exception paramException)


{
StringWriter localStringWriter = new StringWriter();
PrintWriter localPrintWriter = new PrintWriter(localStringWriter);
try
{
paramException.printStackTrace(localPrintWriter);
localPrintWriter.flush();
localPrintWriter.close();
localStringWriter.close();
return localStringWriter.toString();
}
catch (Exception localException) {}

return null;
}

private InputStream getControlFile(int paramInt)


throws SQLException
{
BLOB localBLOB = null;
OracleCallableStatement localOracleCallableStatement = null;
try
{
String str = "SELECT file_data FROM xdo_lobs l,fnd_concurrent_requests
r,fnd_concurrent_programs_vl cp,xdo_ds_definitions_b xdo,fnd_application fnd_app
WHERE r.request_id=:pRequestID and r.program_application_id = cp.application_id
and r.concurrent_program_id = cp.concurrent_program_id and fnd_app.application_id =
cp.application_id and fnd_app.application_short_name=l.application_short_name and
l.lob_code = cp.concurrent_program_name and l.lob_type='BURSTING_FILE'";

localOracleCallableStatement =
(OracleCallableStatement)mJConn.prepareCall(str);
localOracleCallableStatement.setInt(1, paramInt);

OracleResultSet localOracleResultSet =
(OracleResultSet)localOracleCallableStatement.executeQuery();
if (localOracleResultSet.next())
{
localBLOB = localOracleResultSet.getBLOB(1);
}

localOracleCallableStatement.close();
} catch (SQLException localSQLException) { localSQLException =

localSQLException;throw localSQLException;
} finally {}
return localBLOB.getBinaryStream();
}

private String getSystemTempDirectory()


{
String str = null;

str = getDBTempDirectory();

if ((str == null) || (str.length() == 0))


{
Properties localProperties = getDefaultSystemConfig();
str = localProperties.getProperty("system-temp-dir");
}
return str;
}
private Properties getDefaultSystemConfig()
{
Properties localProperties = null;
try
{
String str = ConfigLocation.getPath(0);
if ((str != null) && (!"".equals(str)))
{
localProperties = ConfigReader.read(str, ccntxt.getLocale().toString());
}

return localProperties;
}
catch (Exception localException)
{
localException =

localException;Logger.log(localException);return localProperties; } finally


{} return localProperties;
}

private String getDBTempDirectory()


{
str1 = null;
String str2 = "select value from xdo_config_values where property_code =
'SYSTEM_TEMP_DIR'";

OraclePreparedStatement localOraclePreparedStatement = null;


ResultSet localResultSet = null;

try
{
localOraclePreparedStatement =
(OraclePreparedStatement)ccntxt.getJDBCConnection().prepareStatement(str2);

localResultSet = localOraclePreparedStatement.executeQuery();
if (localResultSet.next()) {}

return localResultSet.getString(1);

}
catch (SQLException localSQLException)
{
Logger.log("--Error: Exception while querying for temp directory", 4);
}
finally
{
if (localResultSet != null)
{
try
{
localResultSet.close();
localResultSet = null;
}
catch (Exception localException5) {}
}

if (localOraclePreparedStatement != null)
{
try
{
localOraclePreparedStatement.close();
localOraclePreparedStatement = null;
}
catch (Exception localException6) {}
}
}
}

private String zipOutputFiles(Vector paramVector)


throws Exception
{
String str1 = ccntxt.getOutFile().getFileName();
String str2 = str1.substring(0, str1.lastIndexOf(".")) + ".zip";
Hashtable localHashtable = new Hashtable();
String str3 = null;
ZipOutputStream localZipOutputStream = new ZipOutputStream(new
FileOutputStream(str2));

for (Enumeration localEnumeration = paramVector.elements();


localEnumeration.hasMoreElements();)
{
DocumentStatus localDocumentStatus =
(DocumentStatus)localEnumeration.nextElement();
str3 = outputFile;

if ((outputFile != null) && (outputFile.length() > 0))


{
FileInputStream localFileInputStream = new FileInputStream(outputFile);
str3 = str3.substring(str3.lastIndexOf(fileSeperator) + 1);

if (localHashtable.contains(str3)) {
lfile.writeln("Found duplicate file name " + str3, 1);
str3 = getAlternateFileName(localHashtable, str3);
lfile.writeln("-->File name renamed as " + str3, 1);
}

localHashtable.put(str3, str3);
localZipOutputStream.putNextEntry(new ZipEntry(str3));
readWrite(localFileInputStream, localZipOutputStream);
localZipOutputStream.closeEntry();
localFileInputStream.close();
localFileInputStream = null;
}

if (status.equals("success"))
burstingStatus = true;
}
localZipOutputStream.finish();
localZipOutputStream.flush();
return str2;
}

public static int readWrite(InputStream paramInputStream, OutputStream


paramOutputStream)
throws Exception
{
int i = -1;
int j = 0;
byte[] arrayOfByte = new byte[' '];

while ((i = paramInputStream.read(arrayOfByte)) != -1)


{
paramOutputStream.write(arrayOfByte, 0, i);
j += i;
}
return j;
}

private void set_OA_MEDIA_Property(Properties paramProperties)


{
Logger.log("setOAProperties called.. ", 1);

try
{
XDOStreamHandlerFactory.registerNewProtocols();

if (ccntxt != null)
{

localObject = ccntxt.getLog();

if (localObject != null)
{
Logger.setLog(new OAXDOLogImpl((Log)localObject));
}
}

OAHelper.setOASystemProperties(ccntxt);

Object localObject = OAHelper.getProfile(ccntxt, "APPS_FRAMEWORK_AGENT");


if (localObject != null)
{
if (!((String)localObject).endsWith("/"))
{
localObject = (String)localObject + "/";
}

localObject = (String)localObject + "OA_MEDIA";

paramProperties.put("user-variable.OA_MEDIA", localObject);

Logger.log("APPS_FRAMEWORK_AGENT profile found. Setting it to user-


variable.OA_MEDIA property : " + (String)localObject, 1);

}
else
{

Logger.log("APPS_FRAMEWORK_AGENT profile not found.", 1);


}

}
catch (Exception localException)
{
Logger.log(localException);
}
}

private void AddCoreProperties(Properties paramProperties)


{
Properties localProperties = getDefaultSystemConfig();
Enumeration localEnumeration; if ((localProperties != null) &&
(localProperties.size() > 0)) {
for (localEnumeration = localProperties.keys();
localEnumeration.hasMoreElements();)
{
String str = (String)localEnumeration.nextElement();
paramProperties.put(str, localProperties.getProperty(str));
}
}
}

private String getAlternateFileName(Hashtable paramHashtable, String paramString)


{
String str = null;
for (int i = 0; i < paramHashtable.size(); i++) {
str = paramString.substring(0, paramString.lastIndexOf('.')) + i +
paramString.substring(paramString.lastIndexOf('.'));
if (!paramHashtable.contains(str))
return str;
}
return paramString.substring(0, paramString.lastIndexOf('.')) +
paramHashtable.size() + paramString.substring(paramString.lastIndexOf('.'));
}
}

Você também pode gostar