📄 impformat.java
字号:
}
catch (SQLException e)
{
Log.error ("ImpFormat.loadRows", e);
}
} // loadLines
/*************************************************************************/
/**
* Parse Line returns ArrayList of values
*
* @param line line
* @param withLabel true if with label
* @param trace create trace info
* @param ignoreEmpty - ignore empty fields
* @return Array of values
*/
public String[] parseLine (String line, boolean withLabel, boolean trace, boolean ignoreEmpty)
{
if (trace)
Log.trace(Log.l4_Data, "ImpFormat.parseLine - " + line);
ArrayList list = new ArrayList();
// for all columns
for (int i = 0; i < m_rows.size(); i++)
{
ImpFormatRow row = (ImpFormatRow)m_rows.get(i);
StringBuffer entry = new StringBuffer ();
// Label-Start
if (withLabel)
{
entry.append(row.getColumnName());
entry.append("=");
if (row.isString())
entry.append("'");
else if (row.isDate())
entry.append("TO_DATE('");
}
// Get Data
String info = null;
if (row.isConstant())
info = "Constant";
else if (m_formatType.equals(FORMATTYPE_FIXED))
{
// check length
if (row.getStartNo() > 0 && row.getEndNo() <= line.length())
info = line.substring(row.getStartNo()-1, row.getEndNo());
}
else
{
info = parseFlexFormat (line, m_formatType, row.getStartNo());
}
if (info == null)
info = "";
// Interpret Data
entry.append(row.parse(info));
// Label-End
if (withLabel)
{
if (row.isString())
entry.append("'");
else if (row.isDate())
entry.append("','YYYY-MM-DD HH24:MI:SS')"); // JDBC Timestamp format w/o miliseconds
}
if (!ignoreEmpty || (ignoreEmpty && info.length() != 0))
list.add(entry.toString());
//
if (trace)
Log.trace(Log.l5_DData, info + "=>" + entry.toString() + " (Length=" + info.length() + ")");
} // for all columns
String[] retValue = new String[list.size()];
list.toArray(retValue);
return retValue;
} // parseLine
/**
* Parse flexible line format.
* A bit inefficient as it always starts from the start
*
* @param line the line to be parsed
* @param formatType Comma or Tab
* @param fieldNo number of field to be returned
* @throws IllegalArgumentException if format unknows
* @return field in lime or ""
*/
private String parseFlexFormat (String line, String formatType, int fieldNo)
{
final char QUOTE = '"';
// check input
char delimiter = ' ';
if (formatType.equals(FORMATTYPE_COMMA))
delimiter = ',';
else if (formatType.equals(FORMATTYPE_TAB))
delimiter = '\t';
else
throw new IllegalArgumentException ("ImpFormat.parseFlexFormat - unknown format: " + formatType);
if (line == null || line.length() == 0 || fieldNo < 0)
return "";
// We need to read line sequentially as the fields may be delimited
// with quotes (") when fields contain the delimiter
// Example: "Artikel,bez","Artikel,""nr""",DEM,EUR
// needs to result in - Artikel,bez - Artikel,"nr" - DEM - EUR
int pos = 0;
int length = line.length();
for (int field = 1; field <= fieldNo && pos < length; field++)
{
StringBuffer content = new StringBuffer();
// two delimiter directly after each other
if (line.charAt(pos) == delimiter)
{
pos++;
continue;
}
// Handle quotes
if (line.charAt(pos) == QUOTE)
{
pos++; // move over beginning quote
while (pos < length)
{
// double quote
if (line.charAt(pos) == QUOTE && pos+1 < length && line.charAt(pos+1) == QUOTE)
{
content.append(line.charAt(pos++));
pos++;
}
// end quote
else if (line.charAt(pos) == QUOTE)
{
pos++;
break;
}
// normal character
else
content.append(line.charAt(pos++));
}
// we should be at end of line or a delimiter
if (pos < length && line.charAt(pos) != delimiter)
Log.trace(Log.l1_User, "ImpFormat.parseFlexFormat - Did not find delimiter at pos " + pos, line);
pos++; // move over delimiter
}
else // plain copy
{
while (pos < length && line.charAt(pos) != delimiter)
content.append(line.charAt(pos++));
pos++; // move over delimiter
}
if (field == fieldNo)
return content.toString();
}
// nothing found
return "";
} // parseFlexFormat
/*************************************************************************/
/**
* Insert/Update Database.
* @param ctx context
* @param line line
* @return true if inserted/updated
*/
public boolean updateDB (Properties ctx, String line)
{
if (line == null || line.trim().length() == 0)
{
Log.trace(10, "No Line");
return false;
}
String[] nodes = parseLine (line, true, false, true); // with label, no trace, ignore empty
if (nodes.length == 0)
{
Log.trace(10, "Nothing parsed from: " + line);
return false;
}
// Log.trace(Log.l4_Data, "ImpFormat.updateDB - listSize=" + nodes.length);
// Standard Fields
int AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID");
int AD_Org_ID = Env.getContextAsInt(ctx, "#AD_Org_ID");
int UpdatedBy = Env.getContextAsInt(ctx, "#AD_User_ID");
// Check if the record is already there ------------------------------
StringBuffer sql = new StringBuffer ("SELECT COUNT(*), MAX(")
.append(m_tablePK).append(") FROM ").append(m_tableName)
.append(" WHERE AD_Client_ID=").append(AD_Client_ID).append(" AND (");
//
String where1 = null;
String where2 = null;
String whereParentChild = null;
for (int i = 0; i < nodes.length; i++)
{
if (nodes[i].endsWith("=''") || nodes[i].endsWith("=0"))
;
else if (nodes[i].startsWith(m_tableUnique1 + "="))
where1 = nodes[i];
else if (nodes[i].startsWith(m_tableUnique2 + "="))
where2 = nodes[i];
else if (nodes[i].startsWith(m_tableUniqueParent + "=") || nodes[i].startsWith(m_tableUniqueChild + "="))
{
if (whereParentChild == null)
whereParentChild = nodes[i];
else
whereParentChild += " AND " + nodes[i];
}
}
StringBuffer find = new StringBuffer();
if (where1 != null)
find.append(where1);
if (where2 != null)
{
if (find.length() > 0)
find.append(" OR ");
find.append(where2);
}
if (whereParentChild != null && whereParentChild.indexOf(" AND ") != -1) // need to have both criteria
{
if (find.length() > 0)
find.append(" OR (").append(whereParentChild).append(")"); // may have only one
else
find.append(whereParentChild);
}
sql.append(find).append(")");
int count = 0;
int ID = 0;
try
{
if (find.length() > 0)
{
PreparedStatement pstmt = DB.prepareStatement(sql.toString());
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
count = rs.getInt(1);
if (count == 1)
ID = rs.getInt(2);
}
rs.close();
pstmt.close();
}
}
catch (SQLException e)
{
Log.error("ImpFormat.updateDB - " + sql.toString(), e);
return false;
}
// Insert Basic Record -----------------------------------------------
if (ID == 0)
{
ID = DB.getKeyNextNo(ctx, 0, m_tableName); // get ID
sql = new StringBuffer("INSERT INTO ")
.append(m_tableName).append("(").append(m_tablePK).append(",")
.append("AD_Client_ID,AD_Org_ID,Created,CreatedBy,Updated,UpdatedBy,IsActive") // StdFields
.append(") VALUES (").append(ID).append(",")
.append(AD_Client_ID).append(",").append(AD_Org_ID).append(",SysDate,").append(UpdatedBy)
.append(",SysDate,").append(UpdatedBy).append(",'Y'")
.append(")");
//
int no = DB.executeUpdate(sql.toString());
if (no != 1)
{
Log.error("ImpFormat.updateDB - Insert records=" + no + "; SQL=" + sql.toString());
return false;
}
Log.trace(Log.l6_Database, "New ID=" + ID, find);
}
else
Log.trace(Log.l6_Database, "Old ID=" + ID, find);
// Update Info -------------------------------------------------------
sql = new StringBuffer ("UPDATE ")
.append(m_tableName).append(" SET ");
for (int i = 0; i < nodes.length; i++)
sql.append(nodes[i]).append(","); // column=value
sql.append("IsActive='Y',Processed='N',I_IsImported='N',Updated=SysDate,UpdatedBy=").append(UpdatedBy);
sql.append(" WHERE ").append(m_tablePK).append("=").append(ID);
// Update Cmd
int no = DB.executeUpdate(sql.toString());
if (no != 1)
{
Log.error("ImpFormat.updateDB - ID=" + ID + " - rows updated=" + no);
return true;
}
return true;
} // updateDB
} // ImpFormat
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -