Search in sources :

Example 1 with MEXPFormatLine

use of org.compiere.model.MEXPFormatLine 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"));
        }
    }
}
Also used : MColumn(org.compiere.model.MColumn) MEXPFormat(org.compiere.model.MEXPFormat) Query(org.compiere.model.Query) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) SQLException(java.sql.SQLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MEXPFormatLine(org.compiere.model.MEXPFormatLine) MTable(org.compiere.model.MTable) Collection(java.util.Collection) SimpleDateFormat(java.text.SimpleDateFormat) PO(org.compiere.model.PO)

Example 2 with MEXPFormatLine

use of org.compiere.model.MEXPFormatLine in project adempiere by adempiere.

the class ExportFormatGenerator method createFormatLine.

private int createFormatLine(MEXPFormat format, MTable table, MColumn col, int position, boolean force) throws SQLException {
    MEXPFormatLine formatLine = null;
    String formatlinevalue = col.getColumnName();
    formatLine = MEXPFormatLine.getFormatLineByValue(getCtx(), formatlinevalue, format.getEXP_Format_ID(), get_TrxName());
    if (formatLine == null)
        formatLine = new MEXPFormatLine(getCtx(), 0, get_TrxName());
    formatLine.setAD_Org_ID(0);
    formatLine.setEXP_Format_ID(format.getEXP_Format_ID());
    formatLine.setValue(formatlinevalue);
    formatLine.setName(col.getName());
    formatLine.setDescription(col.getDescription());
    formatLine.setHelp(col.getHelp());
    formatLine.setPosition(position);
    formatLine.setIsMandatory(col.isMandatory());
    if (force || (col.isIdentifier() && !col.isKey())) {
        formatLine.setIsPartUniqueIndex(true);
        formatLine.setIsActive(true);
    } else {
        formatLine.setIsActive(false);
    }
    MTable tabledir = null;
    if (col.getColumnName().equals(parentTable + "_ID") && DisplayType.isID(col.getAD_Reference_ID())) {
        MEXPFormat referenceFormat = null;
        referenceFormat = MEXPFormat.getFormatByValueAD_Client_IDAndVersion(getCtx(), parentTable + "_Key", getAD_Client_ID(), version, get_TrxName());
        if (referenceFormat == null) {
            referenceFormat = new MEXPFormat(getCtx(), 0, get_TrxName());
        }
        referenceFormat.setAD_Org_ID(0);
        referenceFormat.setValue(parentTable + "_Key");
        referenceFormat.setName(parentTable + "_Key");
        referenceFormat.setAD_Table_ID(MTable.getTable_ID(parentTable));
        referenceFormat.setDescription(table.getDescription());
        referenceFormat.setHelp(table.getHelp());
        referenceFormat.saveEx();
        int AD_Column_ID = DB.getSQLValue(get_TrxName(), "SELECT AD_Column_ID FROM AD_Column WHERE AD_Table_ID=(SELECT AD_Table_ID FROM AD_Table WHERE TableName=?) AND UPPER(ColumnName)='DOCUMENTNO'", parentTable);
        if (AD_Column_ID > 0) {
            //used if the export format is a document like invoice, etc.
            createFormatLine(referenceFormat, table, new MColumn(getCtx(), AD_Column_ID, get_TrxName()), 10, true);
            AD_Column_ID = 0;
            AD_Column_ID = DB.getSQLValue(get_TrxName(), "SELECT AD_Column_ID FROM AD_Column WHERE AD_Table_ID=(SELECT AD_Table_ID FROM AD_Table WHERE TableName=?) AND UPPER(ColumnName)='C_DOCTYPE_ID'", parentTable);
            if (AD_Column_ID > 0)
                createFormatLine(referenceFormat, table, new MColumn(getCtx(), AD_Column_ID, get_TrxName()), 20, true);
            formatLine.setValue(parentTable + "_Key");
            formatLine.setName("Key DocumentNo_C_DocType");
            formatLine.setAD_Column_ID(col.getAD_Column_ID());
            formatLine.setType(MEXPFormatLine.TYPE_ReferencedEXPFormat);
            formatLine.setEXP_EmbeddedFormat_ID(referenceFormat.getEXP_Format_ID());
            formatLine.saveEx();
            if (parentTable != null) {
                if (col.isParent() && col.getColumnName().contains(parentTable)) {
                    int reference = ((MEXPFormat) exportFormats.get(formatValue)).getEXP_Format_ID();
                    MEXPFormatLine embededformatLine = new MEXPFormatLine(getCtx(), 0, get_TrxName());
                    embededformatLine.setAD_Org_ID(0);
                    embededformatLine.setValue(format.getValue() + "_Embedded");
                    embededformatLine.setName("Embedded " + format.getName());
                    embededformatLine.setEXP_EmbeddedFormat_ID(formatLine.getEXP_Format_ID());
                    embededformatLine.setEXP_Format_ID(reference);
                    embededformatLine.setType(MEXPFormatLine.TYPE_EmbeddedEXPFormat);
                    embededformatLine.setAD_Column_ID(col.getAD_Column_ID());
                    embededformatLine.saveEx();
                }
            }
            log.info("Export Format Line:" + formatLine.getName());
            return formatLine.getEXP_FormatLine_ID();
        } else {
            AD_Column_ID = DB.getSQLValue(get_TrxName(), "SELECT AD_Column_ID FROM AD_Column WHERE AD_Table_ID=(SELECT AD_Table_ID FROM AD_Table WHERE TableName=?) AND UPPER(ColumnName)='NAME'", parentTable);
            if (AD_Column_ID > 0) {
                createFormatLine(referenceFormat, table, new MColumn(getCtx(), AD_Column_ID, get_TrxName()), 10, true);
            } else {
                AD_Column_ID = DB.getSQLValue(get_TrxName(), "SELECT AD_Column_ID FROM AD_Column WHERE AD_Table_ID=(SELECT AD_Table_ID FROM AD_Table WHERE TableName=?) AND UPPER(ColumnName)='VALUE'", parentTable);
                if (AD_Column_ID > 0) {
                    createFormatLine(referenceFormat, table, new MColumn(getCtx(), AD_Column_ID, get_TrxName()), 10, true);
                } else {
                    throw new AdempiereException("Table without name or value column");
                }
            }
            formatLine.setValue(parentTable + "_Key");
            formatLine.setName("Key " + col.getColumnName());
            formatLine.setAD_Column_ID(col.getAD_Column_ID());
            formatLine.setType(MEXPFormatLine.TYPE_ReferencedEXPFormat);
            formatLine.setEXP_EmbeddedFormat_ID(referenceFormat.getEXP_Format_ID());
            formatLine.saveEx();
            return formatLine.getEXP_FormatLine_ID();
        }
    }
    if (DisplayType.isID(col.getAD_Reference_ID()) && col.getAD_Reference_Value_ID() > 0) {
        int AD_Table_ID = DB.getSQLValue(get_TrxName(), "SELECT rt.AD_Table_ID FROM AD_Reference r INNER JOIN AD_Ref_Table rt ON (r.AD_Reference_ID=rt.AD_Reference_ID)  WHERE r.AD_Reference_ID=?", col.getAD_Reference_Value_ID());
        if (AD_Table_ID > 0) {
            tabledir = MTable.get(getCtx(), AD_Table_ID);
            formatLine.setValue(col.getColumnName() + "_Reference");
            formatLine.setName("Referenced " + tabledir.getTableName());
            formatLine.setAD_Column_ID(col.getAD_Column_ID());
            String format_value = createFormat(tabledir);
            int embedded = ((MEXPFormat) exportFormats.get(format_value)).getEXP_Format_ID();
            formatLine.setType(MEXPFormatLine.TYPE_ReferencedEXPFormat);
            formatLine.setEXP_EmbeddedFormat_ID(embedded);
            formatLine.saveEx();
            return formatLine.getEXP_FormatLine_ID();
        }
    }
    if (DisplayType.isID(col.getAD_Reference_ID()) && col.isKey() == false && DisplayType.ID != col.getAD_Reference_ID() && DisplayType.Image != col.getAD_Reference_ID()) {
        String tableName = col.getColumnName().substring(0, col.getColumnName().lastIndexOf("_ID"));
        log.info("Table Name:" + tableName);
        if (tableName == null) {
            log.info("Table Name: null");
            return 0;
        }
        tabledir = MTable.get(getCtx(), tableName);
        if (tabledir == null)
            return 0;
        //	throw new Exception ("Ilegal Table Name");
        formatLine.setValue(tabledir.getTableName() + "_Reference");
        formatLine.setName("Referenced " + tabledir.getTableName());
        //formatLine.setType(MEXPFormatLine.TYPE_XMLElement);
        if (tabledir != null) {
            String format_value = createFormat(tabledir);
            int embedded = ((MEXPFormat) exportFormats.get(format_value)).getEXP_Format_ID();
            formatLine.setType(MEXPFormatLine.TYPE_ReferencedEXPFormat);
            formatLine.setEXP_EmbeddedFormat_ID(embedded);
        } else
            formatLine.setType(MEXPFormatLine.TYPE_XMLElement);
    }
    formatLine.setAD_Column_ID(col.getAD_Column_ID());
    formatLine.saveEx();
    log.info("Export Format Line:" + formatLine.getName());
    return formatLine.getEXP_FormatLine_ID();
}
Also used : MEXPFormat(org.compiere.model.MEXPFormat) MColumn(org.compiere.model.MColumn) MEXPFormatLine(org.compiere.model.MEXPFormatLine) MTable(org.compiere.model.MTable) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 3 with MEXPFormatLine

use of org.compiere.model.MEXPFormatLine 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"));
        }
    }
}
Also used : MColumn(org.compiere.model.MColumn) MEXPFormat(org.compiere.model.MEXPFormat) SQLException(java.sql.SQLException) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MEXPFormatLine(org.compiere.model.MEXPFormatLine) MTable(org.compiere.model.MTable) ResultSet(java.sql.ResultSet) PO(org.compiere.model.PO)

Example 4 with MEXPFormatLine

use of org.compiere.model.MEXPFormatLine 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);
}
Also used : MColumn(org.compiere.model.MColumn) MEXPFormat(org.compiere.model.MEXPFormat) Query(org.compiere.model.Query) Element(org.w3c.dom.Element) Timestamp(java.sql.Timestamp) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) AdempiereException(org.adempiere.exceptions.AdempiereException) BigDecimal(java.math.BigDecimal) MEXPFormatLine(org.compiere.model.MEXPFormatLine) AdempiereException(org.adempiere.exceptions.AdempiereException) PO(org.compiere.model.PO)

Example 5 with MEXPFormatLine

use of org.compiere.model.MEXPFormatLine in project adempiere by adempiere.

the class ImportHelper method importElement.

/**
	 *
	 * @param ctx
	 * @param result
	 * @param rootElement
	 * @param expFormat
	 * @param replicationType
	 * @param trxName
	 * @return
	 * @throws Exception
	 * @throws XPathExpressionException
	 */
private PO importElement(Properties ctx, StringBuffer result, Element rootElement, MEXPFormat expFormat, String replicationType, String trxName) throws Exception, XPathExpressionException {
    //Getting the Object for the replicate
    PO po = getObjectFromFormat(ctx, expFormat, rootElement, rootElement.getNodeName(), trxName);
    if (po == null) {
        throw new Exception(Msg.getMsg(ctx, "Can't Load PO Object"));
    }
    if (//If this is just for push and exists we do nothing
    X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(replicationType)) {
        if (po.get_ID() == 0) {
            return null;
        }
    }
    log.info("PO.toString() = " + po.toString());
    if (po.get_KeyColumns().length < 1) {
        throw new Exception(Msg.getMsg(ctx, "EDIMultiColumnNotSupported"));
    }
    Collection<MEXPFormatLine> formatLines = expFormat.getFormatLinesOrderedBy(MEXPFormatLine.COLUMNNAME_IsMandatory + " , " + MEXPFormatLine.COLUMNNAME_Position);
    if (formatLines == null || formatLines.size() < 1) {
        throw new Exception(Msg.getMsg(ctx, "EXPFormatNoLines"));
    }
    //  and set value of column!
    for (MEXPFormatLine formatLine : formatLines) {
        log.info("=================== Beginnig of Format Line ===============================");
        log.info("formatLine: [" + formatLine.toString() + "]");
        //Get the value
        Object value = getValueFromFormat(formatLine, po, rootElement, result, replicationType);
        if (value == null || value.toString().equals(""))
            continue;
        //Set the value
        setReplicaValues(value, formatLine, po, result);
    }
    return po;
}
Also used : MEXPFormatLine(org.compiere.model.MEXPFormatLine) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) AdempiereException(org.adempiere.exceptions.AdempiereException) PO(org.compiere.model.PO)

Aggregations

MEXPFormatLine (org.compiere.model.MEXPFormatLine)5 SQLException (java.sql.SQLException)4 MColumn (org.compiere.model.MColumn)4 MEXPFormat (org.compiere.model.MEXPFormat)4 PO (org.compiere.model.PO)4 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MTable (org.compiere.model.MTable)3 Element (org.w3c.dom.Element)3 ParseException (java.text.ParseException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 Query (org.compiere.model.Query)2 Text (org.w3c.dom.Text)2 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Timestamp (java.sql.Timestamp)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1