Search in sources :

Example 41 with PO

use of org.compiere.model.PO 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 42 with PO

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

the class GridTabWrapper method getReferencedObject.

/**
	 * Load object that is referenced by given property. Example: getReferencedObject("M_Product", method) should load the M_Product record with ID given by M_Product_ID property name;
	 * 
	 * @param propertyName
	 * @param method
	 * @return
	 */
private final Object getReferencedObject(String propertyName, Method method) {
    final GridField idField = m_gridTab.getField(propertyName + "_ID");
    if (idField == null)
        return null;
    // Fetch Record_ID
    final Integer record_id = (Integer) m_gridTab.getValue(idField);
    if (record_id == null || record_id <= 0)
        return null;
    // Fetch TableName from returning class
    Class<?> cl = method.getReturnType();
    String tableName;
    try {
        tableName = (String) cl.getField("Table_Name").get(null);
    } catch (Exception e) {
        log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        return null;
    }
    // Load Persistent Object
    PO po = MTable.get(getCtx(), tableName).getPO(record_id, getTrxName());
    return po;
}
Also used : GridField(org.compiere.model.GridField) AdempiereException(org.adempiere.exceptions.AdempiereException) PO(org.compiere.model.PO)

Example 43 with PO

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

the class StorageEngine method getMA.

private static IInventoryAllocation[] getMA(IDocumentLine model) {
    final Properties ctx = model.getCtx();
    final String IDColumnName = model.get_TableName() + "_ID";
    final String tableName = getTableNameMA(model);
    final String trxName = model.get_TrxName();
    final String whereClause = IDColumnName + "=?";
    List<PO> list = new Query(ctx, tableName, whereClause, trxName).setClient_ID().setParameters(new Object[] { model.get_ID() }).setOrderBy(IDColumnName).list();
    IInventoryAllocation[] arr = new IInventoryAllocation[list.size()];
    return list.toArray(arr);
}
Also used : Query(org.compiere.model.Query) Properties(java.util.Properties) PO(org.compiere.model.PO)

Example 44 with PO

use of org.compiere.model.PO 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 45 with PO

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

the class ImportHelper method getID.

/**
	 * This Method gets the ID value of the current referenced element to put in the imported Object
	 * Exp: AD_Org_ID of the Product.
	 * Must be allays one result.
	 * @param ctx
	 * @param expFormat
	 * @param rootElement
	 * @param rootNodeName
	 * @param trxName
	 * @throws Exception
	 * */
private int getID(Properties ctx, MEXPFormat expFormat, Element rootElement, String rootNodeName, String trxName) throws Exception {
    int record_id = 0;
    PO po = getObjectFromFormat(ctx, expFormat, rootElement, rootNodeName, trxName);
    if (po != null) {
        record_id = po.get_ID();
    }
    log.info("record_id = " + record_id);
    return record_id;
}
Also used : PO(org.compiere.model.PO)

Aggregations

PO (org.compiere.model.PO)75 MTable (org.compiere.model.MTable)18 AdempiereException (org.adempiere.exceptions.AdempiereException)17 SQLException (java.sql.SQLException)16 Properties (java.util.Properties)13 BigDecimal (java.math.BigDecimal)11 Query (org.compiere.model.Query)8 Element (org.w3c.dom.Element)7 ArrayList (java.util.ArrayList)6 MEXPFormat (org.compiere.model.MEXPFormat)6 ADLoginRequest (pl.x3E.adInterface.ADLoginRequest)6 POInfo (org.compiere.model.POInfo)5 Trx (org.compiere.util.Trx)5 ModelCRUD (pl.x3E.adInterface.ModelCRUD)5 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 XFireFault (org.codehaus.xfire.fault.XFireFault)4 DataField (pl.x3E.adInterface.DataField)4 DataRow (pl.x3E.adInterface.DataRow)4 ParseException (java.text.ParseException)3