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 ModelExporter method doIt.
/**
* Process
*
* @return info
*/
protected String doIt() throws Exception {
ExportHelper expHelper = new ExportHelper(getCtx(), p_AD_Client_ID);
MEXPFormat exportFormat = new MEXPFormat(getCtx(), p_EXP_Format_ID, get_TrxName());
File file = new File(p_FileName);
Document doc = expHelper.exportRecord(exportFormat, "", MReplicationStrategy.REPLICATION_TABLE, X_AD_ReplicationTable.REPLICATIONTYPE_Merge, ModelValidator.TYPE_AFTER_CHANGE);
// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
// tranFactory.setAttribute("indent-number", 4); //Adempiere-65 change
Transformer aTransformer = tranFactory.newTransformer();
aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
Source src = new DOMSource(doc);
// =================================== Write to String
Writer writer = new StringWriter();
Result dest2 = new StreamResult(writer);
aTransformer.transform(src, dest2);
// =================================== Write to Disk
try {
Result dest = new StreamResult(file);
aTransformer.transform(src, dest);
writer.flush();
writer.close();
} catch (TransformerException ex) {
ex.printStackTrace();
throw ex;
}
return "Exported";
}
use of org.compiere.model.MEXPFormat in project adempiere by adempiere.
the class ImportHelper method importXMLDocument.
/**
* Import XML Document
* @param result
* @param documentToBeImported
* @param trxName
* @throws Exception
* @throws SQLException
* @throws XPathExpressionException
*/
public void importXMLDocument(StringBuffer result, Document documentToBeImported, String trxName) throws Exception, SQLException, XPathExpressionException {
Element rootElement = documentToBeImported.getDocumentElement();
// Find which Export format to Load...
String AD_Client_Value = null;
AD_Client_Value = rootElement.getAttribute("AD_Client_Value");
log.info("AD_Client_Value = " + AD_Client_Value);
if (AD_Client_Value == null || Util.isEmpty(AD_Client_Value)) {
throw new Exception(Msg.getMsg(ctx, "XMLClientValueMandatory"));
}
String version = null;
version = rootElement.getAttribute("Version");
log.info("Version = " + version);
if (version == null || Util.isEmpty(version)) {
throw new Exception(Msg.getMsg(ctx, "XMLVersionAttributeMandatory"));
}
///Getting Attributes.
int ReplicationMode = new Integer(rootElement.getAttribute("ReplicationMode"));
String ReplicationType = rootElement.getAttribute("ReplicationType");
int ReplicationEvent = new Integer(rootElement.getAttribute("ReplicationEvent"));
MClient client = null;
client = getAD_ClientByValue(ctx, AD_Client_Value, trxName);
if (client == null) {
throw new Exception(Msg.getMsg(ctx, "XMLClientNotFound"));
}
log.info("XML ROOT AD_Client = " + client.toString());
String EXP_Format_Value = null;
EXP_Format_Value = rootElement.getNodeName();
log.info("EXP_Format_Value = " + EXP_Format_Value);
MEXPFormat expFormat = null;
expFormat = MEXPFormat.getFormatByValueAD_Client_IDAndVersion(ctx, EXP_Format_Value, client.getAD_Client_ID(), version, trxName);
if (expFormat == null || expFormat.getEXP_Format_ID() == 0) {
// Fall back to SYSTEM Client.
// Try to search Export format defined for SYSTEM Client!!!
MClient systemClient = null;
systemClient = MClient.get(ctx, 0);
if (systemClient == null) {
throw new Exception(Msg.getMsg(ctx, "XMLClientNotFound"));
}
log.info("SYSTEM Client = " + systemClient.toString());
expFormat = MEXPFormat.getFormatByValueAD_Client_IDAndVersion(ctx, EXP_Format_Value, systemClient.getAD_Client_ID(), version, trxName);
}
if (expFormat == null || expFormat.getEXP_Format_ID() == 0) {
throw new Exception(Msg.getMsg(ctx, "EXPFormatNotFound"));
}
log.info("expFormat = " + expFormat.toString());
isChanged = false;
PO po = importElement(ctx, result, rootElement, expFormat, ReplicationType, trxName);
if (ModelValidator.TYPE_BEFORE_DELETE == ReplicationEvent || ModelValidator.TYPE_BEFORE_DELETE_REPLICATION == ReplicationEvent || ModelValidator.TYPE_DELETE == ReplicationEvent)
;
else if (!po.is_Changed() && !isChanged) {
log.info("Object not changed = " + po.toString());
return;
}
if (po != null) {
Env.setContext(po.getCtx(), "#AD_Client_ID", po.getAD_Client_ID());
if (MReplicationStrategy.REPLICATION_TABLE == ReplicationMode) {
// Here must invoke other method else we get cycle...
if (ModelValidator.TYPE_BEFORE_DELETE == ReplicationEvent || ModelValidator.TYPE_BEFORE_DELETE_REPLICATION == ReplicationEvent || ModelValidator.TYPE_DELETE == ReplicationEvent) {
po.deleteEx(true);
} else {
if (X_AD_ReplicationTable.REPLICATIONTYPE_Broadcast.equals(ReplicationType)) {
MReplicationStrategy rplStrategy = new MReplicationStrategy(client.getCtx(), client.getAD_ReplicationStrategy_ID(), po.get_TrxName());
ExportHelper expHelper = new ExportHelper(client, rplStrategy);
expHelper.exportRecord(po, MReplicationStrategy.REPLICATION_TABLE, X_AD_ReplicationTable.REPLICATIONTYPE_Merge, ModelValidator.TYPE_AFTER_CHANGE);
po.saveReplica(true);
} else if (X_AD_ReplicationTable.REPLICATIONTYPE_Merge.equals(ReplicationType) || X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(ReplicationType)) {
po.saveReplica(true);
} else /*else if (X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(ReplicationType))
{
//Do nothing??
}*/
if (X_AD_ReplicationTable.REPLICATIONTYPE_Local.equals(ReplicationType)) {
//Do nothing??
} else {
// Replication Type is not one of the possible values...ERROR
throw new Exception(Msg.getMsg(ctx, "EXPReplicationTypeNonValidType"));
}
}
} else if (MReplicationStrategy.REPLICATION_DOCUMENT == ReplicationMode && X_AD_ReplicationDocument.REPLICATIONTYPE_Merge.equals(ReplicationType) && po instanceof DocAction) {
DocAction document = (DocAction) po;
String action = document.getDocAction();
String status = document.getDocStatus();
log.info("Document:" + document.toString() + " DocStauts:" + status + " DocAction:" + action);
if (ModelValidator.TIMING_AFTER_REVERSECORRECT == ReplicationEvent) {
if (status.equals(DocAction.STATUS_Reversed) && action.equals(DocAction.ACTION_None)) {
po.saveEx();
return;
}
}
if ((action.equals(DocAction.ACTION_Complete) && status.equals(DocAction.STATUS_InProgress)) || (action.equals(DocAction.ACTION_Close) && status.equals(DocAction.STATUS_Completed))) {
if (!document.processIt(action)) {
log.info("PO.toString() = can not " + po.get_Value("DocAction"));
}
po.saveEx();
} else {
po.saveEx();
return;
}
}
}
result.append("Save Successful ;");
}
use of org.compiere.model.MEXPFormat in project adempiere by adempiere.
the class ExportHelper method exportRecord.
/**
*
* Process - Generate Export Format
* @param po
* @param replicationMode
* @param replicationType
* @param replicationEvent
* @return info
* @throws Exception
*/
public String exportRecord(PO po, Integer replicationMode, String replicationType, Integer replicationEvent) throws Exception {
MClient client = MClient.get(po.getCtx(), clientId);
log.info("Client = " + client.toString());
log.info("po.getAD_Org_ID() = " + po.getAD_Org_ID());
log.info("po.get_TrxName() = " + po.get_TrxName());
if (po.get_TrxName() == null || po.get_TrxName().equals("")) {
po.set_TrxName("exportRecord");
}
log.info("Table = " + po.get_TableName());
if (po.get_KeyColumns().length < 1) {
//TODO: Create Mesagge.
throw new Exception(Msg.getMsg(po.getCtx(), "ExportNoneColumnKeyNotSupported"));
}
// TODO - get proper Export Format!
String version = "3.8.2";
//int EXP_Format_ID = 1000006;
MEXPFormat exportFormat = null;
//exportFormat = new MFormat(po.getCtx(), EXP_Format_ID, po.get_TrxName());
exportFormat = MEXPFormat.getFormatByAD_Client_IDAD_Table_IDAndVersion(po.getCtx(), clientId, po.get_Table_ID(), version, po.get_TrxName());
log.fine("exportFormat = " + exportFormat);
if (exportFormat == null || exportFormat.getEXP_Format_ID() == 0) {
// Fall back to System Client
MClient systemClient = MClient.get(po.getCtx(), 0);
log.info(systemClient.toString());
exportFormat = MEXPFormat.getFormatByAD_Client_IDAD_Table_IDAndVersion(po.getCtx(), 0, po.get_Table_ID(), version, po.get_TrxName());
if (exportFormat == null || exportFormat.getEXP_Format_ID() == 0) {
throw new Exception(Msg.getMsg(po.getCtx(), "EXPFormatNotFound"));
}
}
outDocument = createNewDocument();
HashMap<String, Integer> variableMap = new HashMap<String, Integer>();
Element rootElement = outDocument.createElement(exportFormat.getValue());
if (exportFormat.getDescription() != null && !"".equals(exportFormat.getDescription())) {
rootElement.appendChild(outDocument.createComment(exportFormat.getDescription()));
}
rootElement.setAttribute("AD_Client_Value", client.getValue());
rootElement.setAttribute("Version", exportFormat.getVersion());
rootElement.setAttribute("ReplicationMode", replicationMode.toString());
rootElement.setAttribute("ReplicationType", replicationType);
rootElement.setAttribute("ReplicationEvent", replicationEvent.toString());
outDocument.appendChild(rootElement);
generateExportFormat(rootElement, exportFormat, po, po.get_ID(), variableMap);
MEXPProcessor mExportProcessor = null;
mExportProcessor = new MEXPProcessor(po.getCtx(), replicationStrategy.getEXP_Processor_ID(), po.get_TrxName());
log.fine("ExportProcessor = " + mExportProcessor);
int EXP_ProcessorType_ID = 0;
EXP_ProcessorType_ID = mExportProcessor.getEXP_Processor_Type_ID();
MEXPProcessorType expProcessor_Type = new MEXPProcessorType(po.getCtx(), EXP_ProcessorType_ID, po.get_TrxName());
String javaClass = expProcessor_Type.getJavaClass();
try {
Class clazz = Class.forName(javaClass);
IExportProcessor exportProcessor = (IExportProcessor) clazz.newInstance();
exportProcessor.process(po.getCtx(), mExportProcessor, outDocument, Trx.get(po.get_TrxName(), false));
} catch (Exception e) {
log.severe(e.toString());
throw e;
}
return outDocument.toString();
}
use of org.compiere.model.MEXPFormat in project adempiere by adempiere.
the class ExportHelper method generateExportFormat.
/**
* Trifon Generate Export Format process; result =
* <C_Invoice>
* <DocumentNo>101</DocumentNo>
* </C_Invoice>
* @param rootElement
* @param exportFormat
* @param masterPO
* @param masterID
* @param variableMap
* @throws SQLException
* @throws Exception
*/
private void generateExportFormat(Element rootElement, MEXPFormat exportFormat, PO masterPO, int masterID, HashMap<String, Integer> variableMap) throws SQLException, Exception {
Collection<MEXPFormatLine> formatLines = exportFormat.getFormatLines();
@SuppressWarnings("unused") boolean elementHasValue = false;
//for (int i = 0; i < formatLines.length; i++) {
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());
log.info("Format Line Seach key: " + formatLine.getValue());
if (formatLine.getAD_Column_ID() == 0) {
throw new Exception(Msg.getMsg(masterPO.getCtx(), "EXPColumnMandatory"));
}
MColumn column = MColumn.get(masterPO.getCtx(), formatLine.getAD_Column_ID());
if (column == null) {
throw new Exception(Msg.getMsg(masterPO.getCtx(), "EXPColumnMandatory"));
}
if (column.isVirtualColumn()) {
log.info("This is Virtual Column!");
} else {
}
//log.info("["+column.getColumnName()+"]");
Object value = masterPO.get_Value(column.getColumnName());
String valueString = null;
if (value != null) {
valueString = value.toString();
} else {
// Could remove this exception and create empty XML Element when column do not have value.
if (formatLine.isMandatory()) {
//throw new Exception(Msg.getMsg (masterPO.getCtx(), "EXPFieldMandatory"));
}
}
if (column.getAD_Reference_ID() == DisplayType.Date) {
if (valueString != null) {
if (formatLine.getDateFormat() != null && !"".equals(formatLine.getDateFormat())) {
// "MM/dd/yyyy"
customDateFormat = new SimpleDateFormat(formatLine.getDateFormat());
valueString = customDateFormat.format(Timestamp.valueOf(valueString));
// Add "DateForamt attribute"
newElement.setAttribute("DateFormat", customDateFormat.toPattern());
} else {
newElement.setAttribute("DateFormat", valueString);
}
}
} else if (column.getAD_Reference_ID() == DisplayType.DateTime) {
if (valueString != null) {
if (formatLine.getDateFormat() != null && !"".equals(formatLine.getDateFormat())) {
// "MM/dd/yyyy"
customDateFormat = new SimpleDateFormat(formatLine.getDateFormat());
valueString = customDateFormat.format(Timestamp.valueOf(valueString));
// Add "DateForamt attribute"
newElement.setAttribute("DateFormat", customDateFormat.toPattern());
} else {
newElement.setAttribute("DateFormat", 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;
} else {
// Empty field.
if (formatLine.isMandatory()) {
Text newText = outDocument.createTextNode("");
newElement.appendChild(newText);
rootElement.appendChild(newElement);
elementHasValue = true;
}
}
} else if (formatLine.getType().equals(X_EXP_FormatLine.TYPE_XMLAttribute)) {
// process single XML Attribute
if (formatLine.getAD_Column_ID() == 0) {
throw new Exception(Msg.getMsg(masterPO.getCtx(), "EXPColumnMandatory"));
}
MColumn column = MColumn.get(masterPO.getCtx(), formatLine.getAD_Column_ID());
if (column == null) {
throw new Exception(Msg.getMsg(masterPO.getCtx(), "EXPColumnMandatory"));
}
if (column.isVirtualColumn()) {
log.info("This is Virtual Column!");
} else {
}
//log.info("["+column.getColumnName()+"]");
Object value = masterPO.get_Value(column.getColumnName());
String valueString = null;
if (value != null) {
valueString = value.toString();
} else {
if (formatLine.isMandatory()) {
throw new Exception(Msg.getMsg(masterPO.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;
} else {
// Empty field.
}
} else if (formatLine.getType().equals(X_EXP_FormatLine.TYPE_EmbeddedEXPFormat)) {
// process Embedded Export Format
int embeddedFormat_ID = formatLine.getEXP_EmbeddedFormat_ID();
//get from cache
MEXPFormat embeddedFormat = MEXPFormat.get(masterPO.getCtx(), embeddedFormat_ID, masterPO.get_TrxName());
MTable tableEmbedded = MTable.get(masterPO.getCtx(), embeddedFormat.getAD_Table_ID());
log.info("Table Embedded = " + tableEmbedded);
final StringBuffer whereClause = new StringBuffer(masterPO.get_KeyColumns()[0] + "=?");
if (embeddedFormat.getWhereClause() != null & !"".equals(embeddedFormat.getWhereClause())) {
whereClause.append(" AND ").append(embeddedFormat.getWhereClause());
}
Collection<PO> instances = new Query(masterPO.getCtx(), tableEmbedded.getTableName(), whereClause.toString(), masterPO.get_TrxName()).setClient_ID().setParameters(new Object[] { masterID }).list();
for (PO instance : instances) {
Element embeddedElement = outDocument.createElement(formatLine.getValue());
if (formatLine.getDescription() != null && !"".equals(formatLine.getDescription())) {
embeddedElement.appendChild(outDocument.createComment(formatLine.getDescription()));
}
generateExportFormat(embeddedElement, embeddedFormat, instance, instance.get_ID(), variableMap);
rootElement.appendChild(embeddedElement);
}
} else if (formatLine.getType().equals(X_EXP_FormatLine.TYPE_ReferencedEXPFormat)) {
// process Referenced Export Format
int embeddedFormat_ID = formatLine.getEXP_EmbeddedFormat_ID();
//get from cache
MEXPFormat embeddedFormat = MEXPFormat.get(masterPO.getCtx(), embeddedFormat_ID, masterPO.get_TrxName());
MTable tableEmbedded = MTable.get(masterPO.getCtx(), embeddedFormat.getAD_Table_ID());
log.info("Table Embedded = " + tableEmbedded);
final StringBuffer whereClause = new StringBuffer(tableEmbedded.getTableName() + "_ID =?");
if (embeddedFormat.getWhereClause() != null & !"".equals(embeddedFormat.getWhereClause())) {
whereClause.append(" AND ").append(embeddedFormat.getWhereClause());
}
String columnName = "";
if (formatLine.getAD_Reference_ID() == DisplayType.Table | formatLine.getAD_Reference_ID() == DisplayType.Search) {
MColumn column = MColumn.get(masterPO.getCtx(), formatLine.getAD_Column_ID());
columnName = column.getColumnName();
} else {
columnName = tableEmbedded.getTableName() + "_ID";
}
Object value = masterPO.get_Value(columnName);
if (value == null) {
continue;
}
Collection<PO> instances = new Query(masterPO.getCtx(), tableEmbedded.getTableName(), whereClause.toString(), masterPO.get_TrxName()).setClient_ID().setParameters(value).list();
for (PO instance : instances) {
Element embeddedElement = outDocument.createElement(formatLine.getValue());
if (formatLine.getDescription() != null && !"".equals(formatLine.getDescription())) {
embeddedElement.appendChild(outDocument.createComment(formatLine.getDescription()));
}
generateExportFormat(embeddedElement, embeddedFormat, instance, instance.get_ID(), variableMap);
rootElement.appendChild(embeddedElement);
}
} else {
throw new Exception(Msg.getMsg(masterPO.getCtx(), "EXPUnknownLineType"));
}
}
}
Aggregations