/**
*
* AglutinatorFactory.java
*
* @author carlos@m16e.com
*/
package com.m16e.mpbiz.docs;
////////////////////////////////////////////////////////////
import com.m16e.mpbiz.tables.DbDoc;
import com.m16e.mpbiz.tables.DbDocAglutinator;
import com.m16e.mpbiz.tables.DbDocAglutinatorLines;
import com.m16e.mpbiz.tables.DbDocLin;
import com.m16e.tools.MpAppContext;
import com.m16e.tools.db.DatabaseException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
public class AglutinatorFactory
{
static Logger logger = Logger.getLogger( AglutinatorFactory.class );
////////////////////////////////////////////////////////////
public static final DbDocAglutinator getAglutinator( int tipoEntId, int tipoDocId, int docId )
throws SQLException, DatabaseException
{
DbDocAglutinator aglut =
DbDocAglutinator.getSingleRecord(
DbDocAglutinator.FN_MAIN_TIPO_ENT_ID + " = ? and " +
DbDocAglutinator.FN_MAIN_TIPO_DOC_ID + " = ? and " +
DbDocAglutinator.FN_MAIN_DOC_ID + " = ?",
tipoEntId, tipoDocId, docId );
return aglut;
}
////////////////////////////////////////////////////////////
public static final Integer getDocAglutinatorId(
int tipoEntId, int tipoDocId, int docId )
throws SQLException, DatabaseException
{
DbDocAglutinator aglut =
DbDocAglutinator.getSingleRecord(
DbDocAglutinator.FN_MAIN_TIPO_ENT_ID + " = ? and " +
DbDocAglutinator.FN_MAIN_TIPO_DOC_ID + " = ? and " +
DbDocAglutinator.FN_MAIN_DOC_ID + " = ?",
tipoEntId, tipoDocId, docId );
if( aglut != null )
return aglut.getDocAglutinatorId();
return null;
}
////////////////////////////////////////////////////////////
public static DbDocAglutinator getDocAglutinator( DbDocLin docLin )
throws SQLException, DatabaseException
{
String select =
"select * from " +
DbDocAglutinatorLines.T_NAME +
" where " +
DbDocAglutinatorLines.FN_TIPO_ENT_ID + " = " + docLin.getTipoEntId() + " and " +
DbDocAglutinatorLines.FN_TIPO_DOC_ID + " = " + docLin.getTipoDocId() + " and " +
DbDocAglutinatorLines.FN_DOC_ID + " = " + docLin.getDocId() + " and " +
DbDocAglutinatorLines.FN_DOC_LIN_ID + " = " + docLin.getDocLinId() + "";
Statement stmt =
MpAppContext.getInstance().getDatabase().getConnection().createStatement();
ResultSet rs = stmt.executeQuery( select );
DbDocAglutinatorLines dal = null;
if( rs.next() )
dal = new DbDocAglutinatorLines( rs );
rs.close();
DbDocAglutinator aglutinator = null;
if( dal != null )
{
aglutinator =
DbDocAglutinator.getById( dal.getDocAglutinatorId() );
}
else
{
logger.warn(
"Falied to get aglut for doc_lin: " +
docLin.toString() + ": " + docLin.getDescricao() );
select =
"select * from " +
DbDocAglutinatorLines.T_NAME +
" where " +
DbDocAglutinatorLines.FN_DOC_AGLUTINATOR_ID + " = " + docLin.getDocAglutinatorId();
stmt =
MpAppContext.getInstance().getDatabase().getConnection().createStatement();
rs = stmt.executeQuery( select );
if( rs.next() )
dal = new DbDocAglutinatorLines( rs );
aglutinator = null;
if( dal != null )
{
aglutinator =
DbDocAglutinator.getById( dal.getDocAglutinatorId() );
}
else
{
logger.warn(
"2nd Falied to get aglut for doc_lin: " +
docLin.toString() + ": " + docLin.getDescricao() );
}
}
return aglutinator;
}
////////////////////////////////////////////////////////////
public static DbDocAglutinator getDocAglutinator(
int tipoEntId, int tipoDocId, int docId, int docLinId )
throws SQLException, DatabaseException
{
DbDocAglutinatorLines dal =
DbDocAglutinatorLines.getSingleRecord(
DbDocAglutinatorLines.FN_TIPO_ENT_ID + " = ? and " +
DbDocAglutinatorLines.FN_TIPO_DOC_ID + " = ? and " +
DbDocAglutinatorLines.FN_DOC_ID + " = ? and " +
DbDocAglutinatorLines.FN_DOC_LIN_ID + " = ?",
tipoEntId,
tipoDocId,
docId,
docLinId );
DbDocAglutinator aglutinator = null;
if( dal != null )
{
aglutinator =
DbDocAglutinator.getById( dal.getDocAglutinatorId() );
}
return aglutinator;
}
////////////////////////////////////////////////////////////
/**
*
* @param aglutinatorList
* @param docLin
* @param stopOnFirstMatch
* @throws SQLException
* @throws DatabaseException
*/
public static void traverseDocAglutinatorTree(
DlAglutList aglutinatorList, DbDocLin docLin, boolean stopOnFirstMatch )
throws SQLException, DatabaseException
{
if( logger.isTraceEnabled() )
{
logger.trace(
String.format(
"docLin (%d, %d, %d, %d); stopOnFirstMatch: %b",
docLin.getTipoEntId(), docLin.getTipoDocId(), docLin.getDocId(),
docLin.getDocLinId(), stopOnFirstMatch ) );
}
DbDocAglutinator aglut =
getAglutinator( docLin.getTipoEntId(), docLin.getTipoDocId(), docLin.getDocId() );
if( aglut != null )
{
DlAglutinator dlAglutinator = new DlAglutinator( aglut, docLin );
if( !aglutinatorList.contains( dlAglutinator ) )
{
aglutinatorList.add( dlAglutinator );
if( stopOnFirstMatch )
return;
}
}
if( docLin.getOrgTipoEntId() != null &&
docLin.getOrgTipoDocId() != null &&
docLin.getOrgDocId() != null )
{
if( docLin.getOrgDocLinId() != null )
{
DbDocLin orgDl =
DbDocLin.getById(
docLin.getOrgTipoEntId(),
docLin.getOrgTipoDocId(),
docLin.getOrgDocId(),
docLin.getOrgDocLinId() );
if( orgDl != null )
traverseDocAglutinatorTree( aglutinatorList, orgDl, stopOnFirstMatch );
}
else
{
DbDoc doc =
DbDoc.getById(
docLin.getOrgTipoEntId(),
docLin.getOrgTipoDocId(),
docLin.getOrgDocId() );
aglut =
getAglutinator( doc.getTipoEntId(), doc.getTipoDocId(), doc.getDocId() );
if( aglut != null )
{
DlAglutinator dlAglutinator = new DlAglutinator( aglut, docLin );
if( !aglutinatorList.contains( dlAglutinator ) )
{
aglutinatorList.add( dlAglutinator );
if( stopOnFirstMatch )
return;
}
}
}
}
}
////////////////////////////////////////////////////////////
/**
* Gets an array of this document (and its ancestors) aglutinators,
* thus giving you a way to retrieve all the related aglutinators
* for a gine document.
* @param tipoEntId
* @param tipoDocId
* @param docId
* @return
* @throws java.sql.SQLException
* @throws com.m16e.tools.db.DatabaseException
*/
public static DlAglutList getDocAglutinatorList( MpBizDoc bizDoc )
throws SQLException, DatabaseException
{
DlAglutList aglutinatorList = new DlAglutList();
// // get this doc line list
// java.util.List<DbDocLin> docLines =
// DbDocLin.getList(
// false,
// DbDocLin.FN_TIPO_ENT_ID + " = ? and " +
// DbDocLin.FN_TIPO_DOC_ID + " = ? and " +
// DbDocLin.FN_DOC_ID + " = ?",
// DbDocLin.FN_DOC_LIN_ID,
// tipoEntId, tipoDocId, docId );
for( MpBizDocLin bdl : bizDoc.getDocLines() )
{
traverseDocAglutinatorTree( aglutinatorList, bdl.getDocLin(), false );
}
return aglutinatorList;
}
// ////////////////////////////////////////////////////////////
// public static Integer getOrgDocAglutinatorId( MpBizDoc bizDoc )
// throws SQLException, DatabaseException
// {
// java.util.List<MpBizDocLin> lines = bizDoc.getDocLines();
// for( MpBizDocLin mdl : lines )
// {
// DbDoc dOrg = mdl.getRootOrgDoc();
// if( dOrg != null )
// {
// DbDocAglutinator docAglutinator =
// DbDocAglutinator.getSingleRecord(
// DbDocAglutinator.FN_MAIN_TIPO_ENT_ID + " = ? and " +
// DbDocAglutinator.FN_MAIN_TIPO_DOC_ID + " = ? and " +
// DbDocAglutinator.FN_MAIN_DOC_ID + " = ?",
// dOrg.getTipoEntId(),
// dOrg.getTipoDocId(),
// dOrg.getDocId() );
//
// if( docAglutinator != null )
// {
// Integer id = docAglutinator.getDocAglutinatorId();
// return id;
// }
// }
// }
// return null;
// }
}
|