Search in sources :

Example 31 with MTable

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

the class TableCreateColumns method doIt.

//	prepare
/**
	 * 	Process
	 *	@return info
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    if (p_AD_Table_ID == 0)
        throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_AD_Table_ID);
    log.info("EntityType=" + p_EntityType + ", AllTables=" + p_AllTables + ", AD_Table_ID=" + p_AD_Table_ID);
    //
    Connection conn = null;
    try {
        conn = DB.getConnectionRO();
        AdempiereDatabase db = DB.getDatabase();
        DatabaseMetaData md = conn.getMetaData();
        String catalog = db.getCatalog();
        String schema = db.getSchema();
        if (p_AllTables)
            addTable(md, catalog, schema);
        else {
            MTable table = new MTable(getCtx(), p_AD_Table_ID, get_TrxName());
            if (table == null || table.get_ID() == 0)
                throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_AD_Table_ID);
            log.info(table.getTableName() + ", EntityType=" + p_EntityType);
            String tableName = table.getTableName();
            if (DB.isOracle())
                tableName = tableName.toUpperCase();
            // globalqss 2005-10-24
            if (DB.isPostgreSQL())
                tableName = tableName.toLowerCase();
            // end globalqss 2005-10-24
            ResultSet rs = md.getColumns(catalog, schema, tableName, null);
            addTableColumn(rs, table);
        }
        return "#" + m_count;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : AdempiereDatabase(org.compiere.db.AdempiereDatabase) MTable(org.compiere.model.MTable) AdempiereSystemError(org.compiere.util.AdempiereSystemError) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 32 with MTable

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

the class TableCreateColumns method addTable.

//	doIt
/**
	 * 	Add Table
	 *	@param md meta data
	 *	@param catalog catalog
	 *	@param schema schema
	 *	@throws Exception
	 */
private void addTable(DatabaseMetaData md, String catalog, String schema) throws Exception {
    // globalqss 2005-10-25
    // ResultSet rs = md.getTables(catalog, schema, null, null);
    ResultSet rs;
    if (DB.isPostgreSQL())
        rs = md.getTables(catalog, schema, null, new String[] { "TABLE", "VIEW" });
    else
        rs = md.getTables(catalog, schema, null, null);
    // end globalqss 2005-10-25
    while (rs.next()) {
        String tableName = rs.getString("TABLE_NAME");
        String tableType = rs.getString("TABLE_TYPE");
        //	Try to find
        MTable table = MTable.get(getCtx(), tableName);
        //	Create new ?
        if (table == null) {
            String tn = tableName.toUpperCase();
            if (//	temp table
            tn.startsWith("T_SELECTION") || //	print trl views
            tn.endsWith("_VT") || //	views
            tn.endsWith("_V") || //	views
            tn.endsWith("_V1") || //	asset tables not yet
            tn.startsWith("A_A") || //	asset tables not yet
            tn.startsWith("A_D") || //	oracle system tables
            tn.indexOf('$') != -1 || //	explain plan
            tn.indexOf("EXPLAIN") != -1) {
                log.fine("Ignored: " + tableName + " - " + tableType);
                continue;
            }
            //
            log.info(tableName + " - " + tableType);
            //	Create New
            table = new MTable(getCtx(), 0, get_TrxName());
            table.setEntityType(p_EntityType);
            table.setName(tableName);
            table.setTableName(tableName);
            table.setIsView("VIEW".equals(tableType));
            if (!table.save())
                continue;
        }
        //	Check Columns
        if (DB.isOracle())
            tableName = tableName.toUpperCase();
        // globalqss 2005-10-24
        if (DB.isPostgreSQL())
            tableName = tableName.toLowerCase();
        // end globalqss 2005-10-24
        ResultSet rsC = md.getColumns(catalog, schema, tableName, null);
        addTableColumn(rsC, table);
    }
}
Also used : MTable(org.compiere.model.MTable) ResultSet(java.sql.ResultSet)

Example 33 with MTable

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

the class ExportFormatGenerator method doIt.

//	prepare
/**
     * Generate Export Format
     *
     * @return info
     * @throws Exception
     */
protected String doIt() throws Exception {
    MWindow window = new MWindow(getCtx(), getWindowId(), get_TrxName());
    MTab[] tabs = window.getTabs(true, get_TrxName());
    for (MTab tab : tabs) {
        MTable table = null;
        String format = null;
        if (iscludesonlytheTabsthatInsertrecords() && tab.isInsertRecord()) {
            table = new MTable(getCtx(), tab.getAD_Table_ID(), get_TrxName());
            format = createFormat(table);
        } else if (!iscludesonlytheTabsthatInsertrecords()) {
            table = new MTable(getCtx(), tab.getAD_Table_ID(), get_TrxName());
            format = createFormat(table);
        }
        if (tab.getTabLevel() > level) {
            parentTable = table.getTableName();
            formatValue = format;
        }
    }
    return "ok";
}
Also used : MTable(org.compiere.model.MTable) MTab(org.compiere.model.MTab) MWindow(org.compiere.model.MWindow)

Example 34 with MTable

use of org.compiere.model.MTable 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 35 with MTable

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

the class QueryTest method testFirstOnly.

public void testFirstOnly() throws Exception {
    MTable t = new Query(getCtx(), "AD_Table", "AD_Table_ID=?", getTrxName()).setParameters(new Object[] { 318 }).firstOnly();
    assertEquals("Invalid table ID", 318, t.get_ID());
    //
    assertExceptionThrown(null, DBException.class, new Runnable() {

        public void run() {
            new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName()).setParameters(new Object[] { "C_Invoice", "M_InOut" }).setOrderBy("TableName").firstOnly();
        }
    });
}
Also used : MTable(org.compiere.model.MTable) Query(org.compiere.model.Query)

Aggregations

MTable (org.compiere.model.MTable)67 PO (org.compiere.model.PO)18 ResultSet (java.sql.ResultSet)16 SQLException (java.sql.SQLException)16 PreparedStatement (java.sql.PreparedStatement)15 MColumn (org.compiere.model.MColumn)15 Query (org.compiere.model.Query)13 ArrayList (java.util.ArrayList)10 Properties (java.util.Properties)9 AdempiereException (org.adempiere.exceptions.AdempiereException)7 ADLoginRequest (pl.x3E.adInterface.ADLoginRequest)7 XFireFault (org.codehaus.xfire.fault.XFireFault)5 POInfo (org.compiere.model.POInfo)5 Trx (org.compiere.util.Trx)5 DataField (pl.x3E.adInterface.DataField)5 DataRow (pl.x3E.adInterface.DataRow)5 ModelCRUD (pl.x3E.adInterface.ModelCRUD)5 List (java.util.List)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 POSaveFailedException (org.adempiere.pipo.exception.POSaveFailedException)4