use of org.compiere.model.MEXPFormat in project adempiere by adempiere.
the class Export method doIt.
/**
* Process - Generate Export Format
* @return info
*/
protected String doIt() throws Exception {
outDocument = createNewDocument();
MClient client = MClient.get(getCtx(), p_AD_Client_ID);
log.info(client.toString());
// TODO - get proper Export Format!
int EXP_Format_ID = 1000000;
MTable table = MTable.get(getCtx(), AD_Table_ID);
log.info("Table = " + table);
PO po = table.getPO(p_Record_ID, get_TrxName());
if (po.get_KeyColumns().length > 1 || po.get_KeyColumns().length < 1) {
throw new Exception(Msg.getMsg(getCtx(), "ExportMultiColumnNotSupported"));
}
MEXPFormat exportFormat = new MEXPFormat(getCtx(), EXP_Format_ID, get_TrxName());
StringBuffer sql = new StringBuffer("SELECT * ").append("FROM ").append(table.getTableName()).append(" ").append("WHERE ").append(po.get_KeyColumns()[0]).append("=?");
if (exportFormat.getWhereClause() != null & !"".equals(exportFormat.getWhereClause())) {
sql.append(" AND ").append(exportFormat.getWhereClause());
}
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
pstmt.setInt(1, p_Record_ID);
rs = pstmt.executeQuery();
if (rs.next()) {
HashMap<String, Integer> variableMap = new HashMap<String, Integer>();
variableMap.put(TOTAL_SEGMENTS, new Integer(1));
Element rootElement = outDocument.createElement(exportFormat.getValue());
rootElement.appendChild(outDocument.createComment(exportFormat.getDescription()));
outDocument.appendChild(rootElement);
generateExportFormat(rootElement, exportFormat, rs, po, p_Record_ID, variableMap);
}
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
} catch (SQLException ex) {
/*ignored*/
}
rs = null;
pstmt = null;
}
/* int C_EDIProcessorType_ID = ediProcessor.getC_EDIProcessorType_ID();
X_C_EDIProcessorType ediProcessType = new X_C_EDIProcessorType(getCtx(), C_EDIProcessorType_ID, get_TrxName() );
String javaClass = ediProcessType.getJavaClass();
try {
Class clazz = Class.forName(javaClass);
IOutbandEdiProcessor outbandProcessor = (IOutbandEdiProcessor)clazz.newInstance();
outbandProcessor.process(getCtx(), ediProcessor, result.toString(), "C_Invoice-"+p_Record_ID+".txt", Trx.get( get_TrxName(), false ));
} catch (Exception e) {
result = new StringBuffer( e.toString() );
}
*/
addLog(0, null, null, Msg.getMsg(getCtx(), "ExportProcessResult") + "\n" + outDocument.toString());
return outDocument.toString();
}
use of org.compiere.model.MEXPFormat in project adempiere by adempiere.
the class Export method generateExportFormat.
/*
* Trifon Generate Export Format process; RESULT =
* <C_Invoice>
* <DocumentNo>101</DocumentNo>
* </C_Invoice>
*/
private void generateExportFormat(Element rootElement, MEXPFormat exportFormat, ResultSet rs, PO masterPO, int masterID, HashMap<String, Integer> variableMap) throws SQLException, Exception {
Collection<MEXPFormatLine> formatLines = exportFormat.getFormatLines();
@SuppressWarnings("unused") boolean elementHasValue = false;
for (MEXPFormatLine formatLine : formatLines) {
if (formatLine.getType().equals(X_EXP_FormatLine.TYPE_XMLElement)) {
// process single XML Attribute
// Create new element
Element newElement = outDocument.createElement(formatLine.getValue());
if (formatLine.getAD_Column_ID() == 0) {
throw new Exception(Msg.getMsg(getCtx(), "EXPColumnMandatory"));
}
MColumn column = MColumn.get(getCtx(), formatLine.getAD_Column_ID());
if (column == null) {
throw new Exception(Msg.getMsg(getCtx(), "EXPColumnMandatory"));
}
if (column.isVirtualColumn()) {
log.info("This is Virtual Column!");
} else {
}
//log.info("["+column.getColumnName()+"]");
Object value = rs.getObject(column.getColumnName());
String valueString = null;
if (value != null) {
valueString = value.toString();
} else {
if (formatLine.isMandatory()) {
throw new Exception(Msg.getMsg(getCtx(), "EXPFieldMandatory"));
}
}
/* if (column.getAD_Reference_ID() == DisplayType.Date) {
if (valueString != null) {
if (formatLines[i].getDateFormat() != null && !"".equals(formatLines[i].getDateFormat())) {
m_customDateFormat = new SimpleDateFormat( formatLines[i].getDateFormat() ); // "MM/dd/yyyy"
//Date date = m_customDateFormat.parse ( valueString );
valueString = m_customDateFormat.format(Timestamp.valueOf (valueString));
} else {
valueString = m_dateFormat.format (Timestamp.valueOf (valueString));
}
}
} else if (column.getAD_Reference_ID() == DisplayType.DateTime) {
if (valueString != null) {
if (formatLines[i].getDateFormat() != null && !"".equals(formatLines[i].getDateFormat())) {
m_customDateFormat = new SimpleDateFormat( formatLines[i].getDateFormat() ); // "MM/dd/yyyy"
//Date date = m_customDateFormat.parse ( valueString );
valueString = m_customDateFormat.format(Timestamp.valueOf (valueString));
} else {
valueString = m_dateTimeFormat.format (Timestamp.valueOf (valueString));
}
}
}*/
log.info("EXP Field - column=[" + column.getColumnName() + "]; value=" + value);
if (valueString != null && !"".equals(valueString) && !"null".equals(valueString)) {
Text newText = outDocument.createTextNode(valueString);
newElement.appendChild(newText);
rootElement.appendChild(newElement);
elementHasValue = true;
//increaseVariable(variableMap, formatLines[i].getVariableName()); // Increase value of Variable if any Variable
//increaseVariable(variableMap, TOTAL_SEGMENTS);
} else {
// Empty field.
}
} else if (formatLine.getType().equals(X_EXP_FormatLine.TYPE_XMLAttribute)) {
/* // Create new element
Element newElement = outDocument.createElement(formatLines[i].getValue());
if (hasContent) {
rootElement.appendChild(newElement);
}*/
if (formatLine.getAD_Column_ID() == 0) {
throw new Exception(Msg.getMsg(getCtx(), "EXPColumnMandatory"));
}
MColumn column = MColumn.get(getCtx(), formatLine.getAD_Column_ID());
if (column == null) {
throw new Exception(Msg.getMsg(getCtx(), "EXPColumnMandatory"));
}
if (column.isVirtualColumn()) {
log.info("This is Virtual Column!");
} else {
}
//log.info("["+column.getColumnName()+"]");
Object value = rs.getObject(column.getColumnName());
String valueString = null;
if (value != null) {
valueString = value.toString();
} else {
if (formatLine.isMandatory()) {
throw new Exception(Msg.getMsg(getCtx(), "EXPFieldMandatory"));
}
}
/* if (column.getAD_Reference_ID() == DisplayType.Date) {
if (valueString != null) {
if (formatLines[i].getDateFormat() != null && !"".equals(formatLines[i].getDateFormat())) {
m_customDateFormat = new SimpleDateFormat( formatLines[i].getDateFormat() ); // "MM/dd/yyyy"
//Date date = m_customDateFormat.parse ( valueString );
valueString = m_customDateFormat.format(Timestamp.valueOf (valueString));
} else {
valueString = m_dateFormat.format (Timestamp.valueOf (valueString));
}
}
} else if (column.getAD_Reference_ID() == DisplayType.DateTime) {
if (valueString != null) {
if (formatLines[i].getDateFormat() != null && !"".equals(formatLines[i].getDateFormat())) {
m_customDateFormat = new SimpleDateFormat( formatLines[i].getDateFormat() ); // "MM/dd/yyyy"
//Date date = m_customDateFormat.parse ( valueString );
valueString = m_customDateFormat.format(Timestamp.valueOf (valueString));
} else {
valueString = m_dateTimeFormat.format (Timestamp.valueOf (valueString));
}
}
}*/
log.info("EXP Field - column=[" + column.getColumnName() + "]; value=" + value);
if (valueString != null && !"".equals(valueString) && !"null".equals(valueString)) {
rootElement.setAttribute(formatLine.getValue(), valueString);
elementHasValue = true;
//increaseVariable(variableMap, formatLines[i].getVariableName()); // Increase value of Variable if any Variable
//increaseVariable(variableMap, TOTAL_SEGMENTS);
} else {
// Empty field.
}
} else if (formatLine.getType().equals(X_EXP_FormatLine.TYPE_EmbeddedEXPFormat)) {
// process Embedded Export Format
int embeddedFormat_ID = formatLine.getEXP_EmbeddedFormat_ID();
MEXPFormat embeddedFormat = new MEXPFormat(getCtx(), embeddedFormat_ID, get_TrxName());
MTable tableEmbedded = MTable.get(getCtx(), embeddedFormat.getAD_Table_ID());
log.info("Table Embedded = " + tableEmbedded);
StringBuffer sql = new StringBuffer("SELECT * ").append("FROM ").append(tableEmbedded.getTableName()).append(" ").append("WHERE ").append(masterPO.get_KeyColumns()[0]).append("=?");
if (embeddedFormat.getWhereClause() != null & !"".equals(embeddedFormat.getWhereClause())) {
sql.append(" AND ").append(embeddedFormat.getWhereClause());
}
ResultSet rsEmbedded = null;
PreparedStatement pstmt = null;
try {
pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
pstmt.setInt(1, masterID);
rsEmbedded = pstmt.executeQuery();
while (rsEmbedded.next()) {
//System.out.println("Trifon - tableEmbedded.getTableName()_ID = "+ tableEmbedded.getTableName() + "_ID");
int embeddedID = rsEmbedded.getInt(tableEmbedded.getTableName() + "_ID");
PO poEmbedded = tableEmbedded.getPO(embeddedID, get_TrxName());
Element embeddedElement = outDocument.createElement(formatLine.getValue());
embeddedElement.appendChild(outDocument.createComment(formatLine.getDescription()));
generateExportFormat(embeddedElement, embeddedFormat, rsEmbedded, poEmbedded, embeddedID, variableMap);
rootElement.appendChild(embeddedElement);
}
} finally {
try {
if (rsEmbedded != null)
rsEmbedded.close();
if (pstmt != null)
pstmt.close();
} catch (SQLException ex) {
}
rsEmbedded = null;
pstmt = null;
}
} else {
throw new Exception(Msg.getMsg(getCtx(), "EXPUnknownLineType"));
}
}
}
use of org.compiere.model.MEXPFormat in project adempiere by adempiere.
the class ImportHelper method getObjectFromFormat.
/**
* This Method gets the PO record, from the exportFormat
* @param ctx
* @param expFormat
* @param rootElement
* @param rootNodeName
* @param trxName
* @throws Exception
* */
private PO getObjectFromFormat(Properties ctx, MEXPFormat expFormat, Element rootElement, String rootNodeName, String trxName) throws Exception {
List<PO> values = null;
if (expFormat == null || rootElement == null || rootNodeName == null) {
throw new IllegalArgumentException("expFormat, rootNode and RootnodeName can't be null!");
}
log.info("expFormat = " + expFormat);
log.info("rootNode.getNodeName() = " + rootElement.getNodeName());
log.info("rootNodeName = " + rootNodeName);
if (rootElement.getParentNode() != null) {
log.info("rootNode.ParentName = " + rootElement.getParentNode().getNodeName());
}
// Get list with all Unique columns!
Collection<MEXPFormatLine> uniqueFormatLines = expFormat.getUniqueColumns();
if (uniqueFormatLines == null || uniqueFormatLines.size() < 1) {
throw new AdempiereException(Msg.getMsg(ctx, "EXPFormatLineNoUniqueColumns"));
}
int replication_id = 0;
Object[] cols = new Object[uniqueFormatLines.size()];
Object[] params = new Object[uniqueFormatLines.size()];
StringBuffer whereClause = new StringBuffer("");
int col = 0;
String formatLines = "";
for (MEXPFormatLine uniqueFormatLine : uniqueFormatLines) {
MColumn column = MColumn.get(ctx, uniqueFormatLine.getAD_Column_ID());
log.info("column = [" + column + "]");
String valuecol = column.getColumnName();
formatLines = formatLines + "|" + valuecol;
if (MEXPFormatLine.TYPE_XMLElement.equals(uniqueFormatLine.getType())) {
// XML Element
String xPath = null;
xPath = "" + uniqueFormatLine.getValue();
cols[col] = XMLHelper.getString(xPath, rootElement);
log.info("values[" + col + "]=" + cols[col]);
} else if (MEXPFormatLine.TYPE_XMLAttribute.equals(uniqueFormatLine.getType())) {
// XML Attribute
String xPath = null;
xPath = "@" + uniqueFormatLine.getValue();
cols[col] = XMLHelper.getString(xPath, rootElement);
log.info("values[" + col + "]=" + cols[col]);
} else if (MEXPFormatLine.TYPE_ReferencedEXPFormat.equals(uniqueFormatLine.getType())) {
// Referenced Export Format
log.info("referencedExpFormat.EXP_EmbeddedFormat_ID = " + uniqueFormatLine.getEXP_EmbeddedFormat_ID());
//get from cache
MEXPFormat referencedExpFormat = MEXPFormat.get(ctx, uniqueFormatLine.getEXP_EmbeddedFormat_ID(), trxName);
log.info("referencedExpFormat = " + referencedExpFormat);
int record_ID = 0;
// Find Record_ID by ???Value??? In fact by Columns set as Part Of Unique Index in Export Format!
Element referencedNode = ((Element) rootElement.getElementsByTagName(uniqueFormatLine.getValue()).item(0));
log.info("referencedNode = " + referencedNode);
if (referencedNode == null) {
throw new IllegalArgumentException("referencedNode can't be found!");
}
record_ID = getID(ctx, referencedExpFormat, referencedNode, uniqueFormatLine.getValue(), trxName);
log.info("record_ID = " + record_ID);
cols[col] = new Integer(record_ID);
} else {
// Export Format Line is not one of two possible values...ERROR
throw new Exception(Msg.getMsg(ctx, "EXPFormatLineNonValidType"));
}
if (DisplayType.DateTime == column.getAD_Reference_ID() || DisplayType.Date == column.getAD_Reference_ID()) {
Timestamp value = (Timestamp) handleDateTime(cols[col], column, uniqueFormatLine);
params[col] = value;
} else if (column.getAD_Reference_ID() == DisplayType.String) {
params[col] = (String) cols[col];
} else if (DisplayType.isID(column.getAD_Reference_ID()) || DisplayType.Integer == column.getAD_Reference_ID()) {
Object value = cols[col];
if (!Util.isEmpty(value.toString())) {
//double doubleValue = Double.parseDouble(value.toString());
value = new Integer(value.toString());
if (DisplayType.ID == column.getAD_Reference_ID()) {
replication_id = (Integer) value;
}
} else {
value = null;
}
params[col] = value;
} else if (DisplayType.isNumeric(column.getAD_Reference_ID())) {
valuecol = "Round(" + valuecol + ",2)";
params[col] = new BigDecimal((String) cols[col]).setScale(2, BigDecimal.ROUND_HALF_UP);
} else {
params[col] = cols[col];
}
if (col == 0) {
whereClause.append(" ").append(valuecol).append(" = ? ");
} else {
whereClause.append(" AND ").append(valuecol).append(" = ? ");
}
col++;
}
Query query = new Query(ctx, MTable.get(ctx, expFormat.getAD_Table_ID()), whereClause.toString(), trxName).setParameters(params);
values = query.list();
if (//The Return Object must be always one
values.size() > 1) {
throw new AdempiereException(Msg.getMsg(ctx, "EXPFormatLineNoUniqueColumns") + " : " + expFormat.getName() + "(" + formatLines + ")");
}
if (//Means that is a new record
values.size() <= 0) {
PO po = MTable.get(ctx, expFormat.getAD_Table_ID()).getPO(0, trxName);
if (replication_id > 0) {
po.set_CustomColumn(po.get_KeyColumns()[0], replication_id);
}
return po;
}
//Return the first (unique) record.
return values.get(0);
}
use of org.compiere.model.MEXPFormat in project adempiere by adempiere.
the class ImportHelper method getValueFromFormat.
/**
* Get the value from format
* @param line
* @param po
* @param rootElement
* @param result
* @param replicationType
* @return
* @throws Exception
*/
private Object getValueFromFormat(MEXPFormatLine line, PO po, Element rootElement, StringBuffer result, String replicationType) throws Exception {
Object value = null;
if (MEXPFormatLine.TYPE_XMLElement.equals(line.getType())) {
// XML Element
value = XMLHelper.getString(line.getValue(), rootElement);
log.info("value=[" + value + "]");
} else if (MEXPFormatLine.TYPE_ReferencedEXPFormat.equals(line.getType())) {
// Referenced Export Format
//get from cache
MEXPFormat referencedExpFormat = MEXPFormat.get(ctx, line.getEXP_EmbeddedFormat_ID(), po.get_TrxName());
log.info("referencedExpFormat = " + referencedExpFormat);
int refRecord_ID = 0;
// Find Record_ID by ???Value??? In fact by Columns set as Part Of Unique Index in Export Format!
String xPath = null;
xPath = "" + line.getValue() + "";
log.info("Seach for XML Element = " + xPath);
Element referencedNode = XMLHelper.getElement(xPath, rootElement);
log.info("referencedNode = " + referencedNode);
if (referencedNode != null) {
refRecord_ID = getID(ctx, referencedExpFormat, referencedNode, line.getValue(), po.get_TrxName());
log.info("refRecord_ID = " + refRecord_ID);
value = new Integer(refRecord_ID);
} else {
log.info("NULL VALUE FOR " + xPath.toString());
value = null;
}
log.info("value=[" + value + "]");
} else if (MEXPFormatLine.TYPE_EmbeddedEXPFormat.equals(line.getType())) {
if (po.is_Changed()) {
isChanged = true;
po.saveReplica(true);
} else {
return value;
}
// Embedded Export Format It is used for Parent-Son records like Order&OrderLine
//get from cache
MEXPFormat referencedExpFormat = MEXPFormat.get(ctx, line.getEXP_EmbeddedFormat_ID(), po.get_TrxName());
log.info("embeddedExpFormat = " + referencedExpFormat);
NodeList nodeList = XMLHelper.getNodeList("/" + rootElement.getNodeName() + "/" + line.getValue(), rootElement);
for (int j = 0; j < nodeList.getLength(); j++) {
Element referencedElement = (Element) nodeList.item(j);
log.info("EmbeddedEXPFormat - referencedElement.getNodeName = " + referencedElement.getNodeName());
PO embeddedPo = null;
// Import embedded PO
log.info("=== BEGIN RECURSION CALL ===");
embeddedPo = importElement(ctx, result, referencedElement, referencedExpFormat, replicationType, po.get_TrxName());
log.info("embeddedPo = " + embeddedPo);
if (!embeddedPo.is_Changed()) {
log.info("Object not changed = " + po.toString());
continue;
} else {
embeddedPo.saveReplica(true);
isChanged = true;
}
result.append(" Embedded Save Successful; ");
}
} else if (MEXPFormatLine.TYPE_XMLAttribute.equals(line.getType())) {
// XML Attribute
value = XMLHelper.getString("@" + line.getValue(), rootElement);
log.info("value=[" + value + "]");
} else {
// Export Format Line is not one of the possible values...ERROR
throw new Exception(Msg.getMsg(ctx, "EXPFormatLineNonValidType"));
}
return value;
}
use of org.compiere.model.MEXPFormat in project adempiere by adempiere.
the class ExportFormatGenerator method createFormat.
// doIt
private String createFormat(MTable table) throws SQLException {
log.info("Table Name:" + table.getTableName());
MColumn[] columns = table.getColumns(true);
String unique = null;
boolean isFieldName = false;
for (MColumn column : columns) {
if (column.isIdentifier() && column.getSeqNo() == 1) {
unique = column.getColumnName();
if (unique.equals("Name"))
isFieldName = true;
log.info("Unique Key" + unique);
break;
}
}
if (unique == null)
unique = "Name";
MEXPFormat format = null;
//String formatValue = table.getTableName()+"_"+unique;
String formatValue = table.getTableName();
log.info("Export Format Value:" + formatValue);
format = (MEXPFormat) exportFormats.get(formatValue);
if (format != null)
return format.getValue();
String where = " value = ? ";
Query sql = new Query(getCtx(), I_EXP_Format.Table_Name, where, get_TrxName()).setParameters(formatValue);
if (sql.match()) {
format = (MEXPFormat) sql.first();
exportFormats.put(format.getValue(), format);
return format.getValue();
}
format = MEXPFormat.getFormatByValueAD_Client_IDAndVersion(getCtx(), formatValue, getAD_Client_ID(), version, get_TrxName());
if (format == null)
format = new MEXPFormat(getCtx(), 0, get_TrxName());
format.setAD_Org_ID(0);
format.setValue(formatValue);
format.setName(table.getName());
format.setAD_Table_ID(table.getAD_Table_ID());
format.setDescription(table.getDescription());
format.setHelp(table.getHelp());
format.setVersion(version);
format.saveEx();
if (format != null)
exportFormats.put(format.getValue(), format);
int position = 10;
for (MColumn column : columns) {
if (iscludesonlythemandatorycolumns()) {
if (column.isMandatory())
createFormatLine(format, table, column, position, false);
} else
createFormatLine(format, table, column, position, false);
position++;
}
return format.getValue();
}
Aggregations