on 01-12-2007 11:03 PM
Hi experts,
In order to make some validations when getting file in File Adapter, was developed a EJB Module Adapter. When the validation failed, it must to be generate a throw error with a text that appears in Runtime workbench.
throw new ModuleException("Error al crear objeto Archivo para procesar el legacy: "+legacy);
Exception null
.Follow Below the Java Code of Module EJB:
package clasesSox;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.sap.aii.af.mp.module.ModuleException;
/**
*
@author martin.j.rodriguez
*/
public class Archivo extends XMLParser {
private Log log = LogFactory.getLog(Archivo.class);
private Config config;
private String nombre, tipoSistema, pais;
private Date fecha;
//private boolean procesado;
//private org.xml.sax.XMLReader xr;
private double totimp, importeParcial;
private String buscaTOTIMP, regImporte, buscaTOTLIN;
private String [] tags;
private InputStream inputStream;
private int totlin, regCuenta;
public Archivo(InputStream is, String legacy) throws ModuleException {
super();
Map properties = getConfig().getConfig(legacy);
if (properties == null) {
if (log.isErrorEnabled()){
log.error("Error al crear objeto Archivo para procesar el legacy: "legacy" - No se encontro la configuración.");
}
throw new ModuleException("Error al crear objeto Archivo para procesar el legacy: "+legacy);
}
this.buscaTOTIMP = (String)properties.get(Config.CAMPO_IMPORTE_TOTAL);
this.regImporte = (String)properties.get(Config.CAMPO_IMPORTE);
this.buscaTOTLIN = (String)properties.get(Config.CAMPO_LINEAS_TOTAL);
String tagstosearch = (String)properties.get(Config.CAMPO_LINEAS_CONTAR);
StringTokenizer st = new StringTokenizer(tagstosearch, ",");
tags = new String[st.countTokens()];
int i=0;
while(st.hasMoreTokens()){
String token = st.nextToken();
tags[i++] = token.trim();
}
setInputStream(is);
}
public List validaArchivo() throws ParserConfigurationException, SAXException, IOException {
regCuenta = 0;
importeParcial = 0;
DefaultHandler handler = this;
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(inputStream, handler);
String s = "";
roundValues();
String stotimp = new BigDecimal(totimp).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
String simporteParcial = new BigDecimal(importeParcial).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
if (log.isDebugEnabled()){
log.debug("Resultados: TOTIMP: "stotimp" - CONTEO: "+simporteParcial);
log.debug("Resultados: TOTLIN: "totlin" - CONTEO: "+regCuenta);
}
List results = new ArrayList();
if (totimp != importeParcial) {
s = "Error al contar el total del importe, deberia ser: "stotimp" y el calculado es: "+simporteParcial;
if (log.isDebugEnabled()){
log.debug(s);
}
results.add(s);
}
if (totlin != regCuenta) {
s = "Error al contar el total del lineas, deberia ser: "totlin" y el calculado es: "+regCuenta;
if (log.isDebugEnabled()){
log.debug(s);
}
results.add(s);
}
return results;
}
/**
*/
private void roundValues() {
totimp = roundValue(totimp);
importeParcial = roundValue(importeParcial);
}
/**
redondea un numero a 2 decimales
*/
private double roundValue(double d) {
BigDecimal bd = new BigDecimal(d);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
public void startElement(String namespaceURI, String lName, String qName, org.xml.sax.Attributes attrs) throws org.xml.sax.SAXException {
String etiqueta = getTagName(qName, lName);
//niveles.add(etiqueta);
// Cuenta registros
for (int i = 0;i< tags.length; i++){
if (etiqueta.equals(tags+)){
regCuenta++;
break;
}
}
}
public void endElement(String namespaceURI, String lName, String qName) throws org.xml.sax.SAXException {
String etiqueta = getTagName(qName, lName);
//niveles.remove(niveles.size() - 1);
// set valor totlin
if (buscaTOTLIN.equals(etiqueta)){
totlin = parseInt(valor);
}
// set valor totimp
if (buscaTOTIMP.equals(etiqueta)){
totimp = parseDouble(valor);
}
// suma importe parcial
if (regImporte.equals(etiqueta)){
importeParcial += parseDouble(valor);
}
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream texto) {
this.inputStream = texto;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getTipoSistema() {
return tipoSistema;
}
public void setTipoSistema(String tipoSistema) {
this.tipoSistema = tipoSistema;
}
public String getPais() {
return pais;
}
public void setPais(String pais) {
this.pais = pais;
}
public java.util.Date getFecha() {
return fecha;
}
public void setFecha(java.util.Date fecha) {
this.fecha = fecha;
}
/*
public static void main(String args[]) {
String fileName = "FI_MAS_SAP_RENBAN_####_AAAAMMDD_HHMMSS.TXT";
java.util.StringTokenizer st = new java.util.StringTokenizer(fileName, "_");
for (int i = 0; i < 5; i++)
st.nextToken();
String anioMes = st.nextToken();
if (anioMes != null)
if (anioMes.length() > 5) {
System.out.println(anioMes);
System.out.println(anioMes.substring(0, 4) + " " + anioMes.substring(4, 6));
}
}
*/
private double parseDouble(String number){
if (number == null || number.trim().length()==0 || number.trim().equals("/"))
return 0;
// remuevo los 0 a la izquierda que afectan al parseint
number = number.trim().replaceAll("^0+","");
number = number.replaceAll(",",".");
double result = 0;
try {
result = Double.parseDouble(number);
}
catch (NumberFormatException nfe){
System.err.println("Error parseando numero "+number);
}
return result;
}
private int parseInt(String number){
if (number == null || number.trim().length()==0 || number.trim().equals("/"))
return 0;
// remuevo los 0 a la izquierda que afectan al parseint
number = number.trim().replaceAll("^0+","");
number = number.replaceAll(",",".");
int result = 0;
try {
result = Integer.parseInt(number);
}
catch (NumberFormatException nfe){
System.err.println("Error parseando numero "+number);
}
return result;
}
/**
Carga la configuracion
@return
*/
protected Config getConfig(){
if (config == null){
ConfigParser cp = new ConfigParser();
config = cp.readConfig();
}
return config;
}
}
Could anyone help me about this problem ?
Thanks in advance.+
Hi,
do u think, u will get the error text in the RWB ? I don't think so, it will provide the output in the RWB.
The output of the Adapter module is nothing but XI -XML message. So this will not come
check it out for the how to work on Adapter Modules-
Hope this helps,
Regards,
Moorthy
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.