Search in sources :

Example 11 with DatabaseAccessException

use of org.adempiere.pipo.exception.DatabaseAccessException in project adempiere by adempiere.

the class ColumnElementHandler method startElement.

public void startElement(Properties ctx, Element element) throws SAXException {
    PackIn packIn = (PackIn) ctx.get("PackInProcess");
    String elementValue = element.getElementValue();
    Attributes atts = element.attributes;
    log.info(elementValue + " " + atts.getValue("ColumnName"));
    int success = 0;
    String entitytype = atts.getValue("EntityType");
    if (isProcessElement(ctx, entitytype)) {
        if (element.parent != null && element.parent.getElementValue().equals("table") && element.parent.defer) {
            element.defer = true;
            return;
        }
        String columnName = atts.getValue("ColumnName");
        String tableName = atts.getValue("ADTableNameID");
        int tableid = 0;
        if (element.parent != null && element.parent.getElementValue().equals("table") && element.parent.recordId > 0) {
            tableid = element.parent.recordId;
        } else {
            tableid = packIn.getTableId(tableName);
        }
        if (tableid <= 0) {
            tableid = get_IDWithColumn(ctx, "AD_Table", "TableName", tableName);
            if (tableid > 0)
                packIn.addTable(tableName, tableid);
        }
        int id = packIn.getColumnId(tableName, columnName);
        if (id <= 0) {
            id = get_IDWithMasterAndColumn(ctx, "AD_Column", "ColumnName", columnName, "AD_Table", tableid);
            if (id > 0) {
                packIn.addColumn(tableName, columnName, id);
            }
        }
        MColumn m_Column = new MColumn(ctx, id, getTrxName(ctx));
        if (id <= 0 && atts.getValue("AD_Column_ID") != null && Integer.parseInt(atts.getValue("AD_Column_ID")) <= PackOut.MAX_OFFICIAL_ID)
            m_Column.setAD_Column_ID(Integer.parseInt(atts.getValue("AD_Column_ID")));
        int AD_Backup_ID = -1;
        String Object_Status = null;
        if (id > 0) {
            AD_Backup_ID = copyRecord(ctx, "AD_Column", m_Column);
            Object_Status = "Update";
        } else {
            Object_Status = "New";
            AD_Backup_ID = 0;
        }
        m_Column.setColumnName(columnName);
        // Process
        String processName = atts.getValue("ADProcessNameID");
        int AD_Process_ID = get_IDWithColumn(ctx, "AD_Process", "Value", processName);
        if (AD_Process_ID <= 0) /** TODO PackOut version check 005 */
        {
            AD_Process_ID = get_IDWithColumn(ctx, "AD_Process", "Name", processName);
        }
        m_Column.setAD_Process_ID(AD_Process_ID);
        //
        String Name = atts.getValue("ADReferenceNameID");
        int referenceId = get_IDWithColumn(ctx, "AD_Reference", "Name", Name);
        m_Column.setAD_Reference_ID(referenceId);
        // log.info("Column ID ->"+id);
        Name = atts.getValue("ADTableNameID");
        id = get_IDWithColumn(ctx, "AD_Table", "TableName", Name);
        m_Column.setAD_Table_ID(id);
        Name = atts.getValue("ADValRuleNameID");
        id = get_IDWithColumn(ctx, "AD_Val_Rule", "Name", Name);
        m_Column.setAD_Val_Rule_ID(id);
        //Validate that reference id is for Table or List
        if (DisplayType.Table == referenceId || DisplayType.List == referenceId || DisplayType.Search == referenceId) {
            Name = atts.getValue("ADReferenceNameValueID");
            id = get_IDWithColumn(ctx, "AD_Reference", "Name", Name);
            m_Column.setAD_Reference_Value_ID(id);
        }
        m_Column.setCallout(getStringValue(atts, "Callout"));
        m_Column.setColumnSQL(getStringValue(atts, "ColumnSQL"));
        m_Column.setColumnName(atts.getValue("ColumnName"));
        m_Column.setDefaultValue(getStringValue(atts, "DefaultValue"));
        m_Column.setDescription(getStringValue(atts, "Description"));
        m_Column.setEntityType(atts.getValue("EntityType"));
        if (Integer.parseInt(atts.getValue("FieldLength")) > 0)
            m_Column.setFieldLength(Integer.parseInt(atts.getValue("FieldLength")));
        m_Column.setHelp(getStringValue(atts, "Help"));
        m_Column.setIsActive(atts.getValue("isActive") != null ? Boolean.valueOf(atts.getValue("isActive")).booleanValue() : true);
        m_Column.setIsAlwaysUpdateable((Boolean.valueOf(atts.getValue("isAlwaysUpdateable")).booleanValue()));
        // m_Column.setIsEncrypted(atts.getValue("isEncrypted"));
        m_Column.setIsIdentifier((Boolean.valueOf(atts.getValue("isIdentifier")).booleanValue()));
        m_Column.setIsKey((Boolean.valueOf(atts.getValue("isKey")).booleanValue()));
        m_Column.setIsMandatory((Boolean.valueOf(atts.getValue("isMandatory")).booleanValue()));
        m_Column.setIsParent((Boolean.valueOf(atts.getValue("isParent")).booleanValue()));
        m_Column.setIsSelectionColumn((Boolean.valueOf(atts.getValue("isSelectionColumn")).booleanValue()));
        m_Column.setIsSyncDatabase(atts.getValue("getIsSyncDatabase"));
        m_Column.setIsTranslated((Boolean.valueOf(atts.getValue("isTranslated")).booleanValue()));
        m_Column.setIsUpdateable((Boolean.valueOf(atts.getValue("isUpdateable")).booleanValue()));
        m_Column.setName(atts.getValue("Name"));
        m_Column.setReadOnlyLogic(getStringValue(atts, "ReadOnlyLogic"));
        if (Integer.parseInt(atts.getValue("SeqNo")) > 0)
            m_Column.setSeqNo(Integer.parseInt(atts.getValue("SeqNo")));
        m_Column.setVFormat(getStringValue(atts, "VFormat"));
        if (getStringValue(atts, "ValueMax") != null)
            m_Column.setValueMax(atts.getValue("ValueMax"));
        if (getStringValue(atts, "ValueMin") != null)
            m_Column.setValueMin(atts.getValue("ValueMin"));
        if (getStringValue(atts, "Version") != null)
            m_Column.setVersion(new BigDecimal(atts.getValue("Version")));
        m_Column.setInfoFactoryClass(getStringValue(atts, "InfoFactoryClass"));
        // Setup Element.
        id = get_IDWithColumn(ctx, "AD_Element", "ColumnName", m_Column.getColumnName());
        X_AD_Element adElement = new X_AD_Element(ctx, id, getTrxName(ctx));
        String Object_Status_col = Object_Status;
        if (adElement.getAD_Element_ID() == 0) {
            // Object_Status = "New";
            adElement.setColumnName(m_Column.getColumnName());
            adElement.setEntityType(m_Column.getEntityType());
            adElement.setPrintName(m_Column.getColumnName());
            adElement.setName(m_Column.getColumnName());
            if (adElement.save(getTrxName(ctx)) == true) {
                record_log(ctx, 1, m_Column.getName(), "Element", adElement.getAD_Element_ID(), AD_Backup_ID, "New", "AD_Element", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Element"));
            } else {
                record_log(ctx, 0, m_Column.getName(), "Element", adElement.getAD_Element_ID(), AD_Backup_ID, "New", "AD_Element", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Element"));
            }
        }
        Object_Status = Object_Status_col;
        m_Column.setAD_Element_ID(adElement.getAD_Element_ID());
        boolean recreateColumn = (m_Column.is_new() || m_Column.is_ValueChanged("AD_Reference_ID") || m_Column.is_ValueChanged("FieldLength") || m_Column.is_ValueChanged("ColumnName") || m_Column.is_ValueChanged("IsMandatory"));
        //ignore fieldlength change for clob and lob
        if (!m_Column.is_ValueChanged("AD_Reference_ID") && m_Column.is_ValueChanged("FieldLength")) {
            if (DisplayType.isLOB(m_Column.getAD_Reference_ID())) {
                recreateColumn = false;
            }
        }
        // nulls
        if (!recreateColumn) {
            String oldDefault = (String) m_Column.get_ValueOld("DefaultValue");
            String newDefault = m_Column.getDefaultValue();
            if (oldDefault != null && oldDefault.length() == 0)
                oldDefault = null;
            if (newDefault != null && newDefault.length() == 0)
                newDefault = null;
            if ((oldDefault == null && newDefault != null) || (oldDefault != null && newDefault == null)) {
                recreateColumn = true;
            } else if (oldDefault != null && newDefault != null) {
                if (!oldDefault.equals(newDefault))
                    recreateColumn = true;
            }
        }
        // Don't create database column for virtual columns
        boolean syncDatabase = "Y".equalsIgnoreCase(atts.getValue("getIsSyncDatabase"));
        if (recreateColumn) {
            if (m_Column.isVirtualColumn() || !syncDatabase)
                recreateColumn = false;
        }
        if (m_Column.save(getTrxName(ctx)) == true) {
            record_log(ctx, 1, m_Column.getName(), "Column", m_Column.get_ID(), AD_Backup_ID, Object_Status, "AD_Column", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Column"));
            element.recordId = m_Column.getAD_Column_ID();
        } else {
            record_log(ctx, 0, m_Column.getName(), "Column", m_Column.get_ID(), AD_Backup_ID, Object_Status, "AD_Column", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Column"));
            throw new POSaveFailedException("Failed to import column.");
        }
        if (recreateColumn || syncDatabase) {
            MTable table = new MTable(ctx, m_Column.getAD_Table_ID(), getTrxName(ctx));
            if (!table.isView() && !m_Column.isVirtualColumn()) {
                success = createColumn(ctx, table, m_Column, recreateColumn);
                if (success == 1) {
                    record_log(ctx, 1, m_Column.getColumnName(), "dbColumn", m_Column.get_ID(), 0, Object_Status, atts.getValue("ADTableNameID").toUpperCase(), get_IDWithColumn(ctx, "AD_Table", "TableName", atts.getValue("ADTableNameID").toUpperCase()));
                } else {
                    record_log(ctx, 0, m_Column.getColumnName(), "dbColumn", m_Column.get_ID(), 0, Object_Status, atts.getValue("ADTableNameID").toUpperCase(), get_IDWithColumn(ctx, "AD_Table", "TableName", atts.getValue("ADTableNameID").toUpperCase()));
                    throw new DatabaseAccessException("Failed to create column or related constraint for " + m_Column.getColumnName());
                }
            }
        }
    } else {
        element.skip = true;
    }
}
Also used : MColumn(org.compiere.model.MColumn) MTable(org.compiere.model.MTable) PackIn(org.adempiere.pipo.PackIn) Attributes(org.xml.sax.Attributes) POSaveFailedException(org.adempiere.pipo.exception.POSaveFailedException) X_AD_Element(org.compiere.model.X_AD_Element) BigDecimal(java.math.BigDecimal) DatabaseAccessException(org.adempiere.pipo.exception.DatabaseAccessException)

Example 12 with DatabaseAccessException

use of org.adempiere.pipo.exception.DatabaseAccessException in project adempiere by adempiere.

the class PrintFormatElementHandler method create.

public void create(Properties ctx, TransformerHandler document) throws SAXException {
    int AD_PrintFormat_ID = Env.getContextAsInt(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_AD_PrintFormat_ID);
    PackOut packOut = (PackOut) ctx.get("PackOutProcess");
    if (formats.contains(AD_PrintFormat_ID))
        return;
    formats.add(AD_PrintFormat_ID);
    AttributesImpl atts = new AttributesImpl();
    String sql = null;
    sql = "SELECT AD_PrintFormat_ID " + "FROM AD_PrintFormat " + "WHERE (AD_PrintFormat_ID IN " + "(SELECT AD_PrintFormatChild_ID FROM AD_PrintFormatItem WHERE AD_PrintFormat_ID = " + AD_PrintFormat_ID + " AND PrintFormatType = 'P' GROUP BY AD_PrintFormatChild_ID) OR AD_PrintFormat_ID = " + AD_PrintFormat_ID + ")";
    PreparedStatement pstmt = null;
    pstmt = DB.prepareStatement(sql, getTrxName(ctx));
    try {
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            X_AD_PrintFormat m_Printformat = new X_AD_PrintFormat(ctx, rs.getInt("AD_PrintFormat_ID"), null);
            if (m_Printformat.getAD_PrintPaper_ID() > 0)
                packOut.createPrintPaper(m_Printformat.getAD_PrintPaper_ID(), document);
            createPrintFormatBinding(atts, m_Printformat);
            document.startElement("", "", "printformat", atts);
            String sql2 = "SELECT * FROM AD_PrintFormatItem WHERE AD_PrintFormat_ID= " + m_Printformat.getAD_PrintFormat_ID() + " ORDER BY " + X_AD_PrintFormatItem.COLUMNNAME_SeqNo + "," + X_AD_PrintFormatItem.COLUMNNAME_AD_PrintFormatItem_ID;
            PreparedStatement pstmt2 = null;
            pstmt2 = DB.prepareStatement(sql2, getTrxName(ctx));
            try {
                ResultSet rs2 = pstmt2.executeQuery();
                while (rs2.next()) {
                    createItem(ctx, document, rs2.getInt("AD_PrintFormatItem_ID"));
                }
                rs2.close();
                pstmt2.close();
                pstmt2 = null;
            } finally {
                try {
                    if (pstmt2 != null)
                        pstmt2.close();
                } catch (Exception e) {
                }
                pstmt2 = null;
            }
            document.endElement("", "", "printformat");
        }
        rs.close();
        pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        if (e instanceof SAXException)
            throw (SAXException) e;
        else if (e instanceof SQLException)
            throw new DatabaseAccessException("Failed to export print format.", e);
        else if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException("Failed to export print format.", e);
    } finally {
        try {
            if (pstmt != null)
                pstmt.close();
        } catch (Exception e) {
        }
        pstmt = null;
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) SQLException(java.sql.SQLException) PackOut(org.adempiere.pipo.PackOut) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) X_AD_PrintFormat(org.compiere.model.X_AD_PrintFormat) POSaveFailedException(org.adempiere.pipo.exception.POSaveFailedException) SQLException(java.sql.SQLException) DatabaseAccessException(org.adempiere.pipo.exception.DatabaseAccessException) SAXException(org.xml.sax.SAXException) DatabaseAccessException(org.adempiere.pipo.exception.DatabaseAccessException) SAXException(org.xml.sax.SAXException)

Example 13 with DatabaseAccessException

use of org.adempiere.pipo.exception.DatabaseAccessException in project adempiere by adempiere.

the class FormAccessElementHandler method startElement.

public void startElement(Properties ctx, Element element) throws SAXException {
    String elementValue = element.getElementValue();
    log.info(elementValue);
    int roleid = 0;
    int formid = 0;
    StringBuffer sqlB = null;
    Attributes atts = element.attributes;
    if (atts.getValue("rolename") != null) {
        String name = atts.getValue("rolename");
        sqlB = new StringBuffer("SELECT AD_Role_ID FROM AD_Role WHERE Name= ?");
        roleid = DB.getSQLValue(getTrxName(ctx), sqlB.toString(), name);
    }
    if (atts.getValue("formname") != null) {
        String name = atts.getValue("formname");
        sqlB = new StringBuffer("SELECT AD_Form_ID FROM AD_Form WHERE Name= ?");
        formid = DB.getSQLValue(getTrxName(ctx), sqlB.toString(), name);
    }
    sqlB = new StringBuffer("SELECT count(*) FROM AD_Form_Access WHERE AD_Role_ID=? and AD_Form_ID=?");
    int count = DB.getSQLValue(getTrxName(ctx), sqlB.toString(), roleid, formid);
    String Object_Status = null;
    int AD_Backup_ID = -1;
    if (count > 0) {
        Object_Status = "Update";
        sqlB = new StringBuffer("UPDATE AD_Form_Access ").append("SET isActive = '" + atts.getValue("isActive")).append("', isReadWrite = '" + atts.getValue("isReadWrite")).append("' WHERE AD_Role_ID = " + roleid).append(" and AD_Form_ID = " + formid);
        int no = DB.executeUpdate(sqlB.toString(), getTrxName(ctx));
        if (no == -1) {
            log.info("Update to form access failed");
            throw new DatabaseAccessException("Update to form access failed");
        }
    } else {
        Object_Status = "New";
        AD_Backup_ID = 0;
        sqlB = new StringBuffer("INSERT INTO AD_Form_Access" + "(AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy, " + "AD_Role_ID, AD_Form_ID, isActive, isReadWrite) " + "VALUES(" + " " + Env.getAD_Client_ID(ctx) + ", " + Env.getAD_Org_ID(ctx) + ", " + Env.getAD_User_ID(ctx) + ", " + Env.getAD_User_ID(ctx) + ", " + roleid + ", " + formid + ", '" + atts.getValue("isActive") + "', '" + atts.getValue("isReadWrite") + "')");
        int no = DB.executeUpdate(sqlB.toString(), getTrxName(ctx));
        if (no == -1) {
            log.info("Insert to form access failed");
            throw new DatabaseAccessException("Insert to form access failed");
        }
    }
}
Also used : Attributes(org.xml.sax.Attributes) DatabaseAccessException(org.adempiere.pipo.exception.DatabaseAccessException)

Example 14 with DatabaseAccessException

use of org.adempiere.pipo.exception.DatabaseAccessException in project adempiere by adempiere.

the class ImpFormatElementHandler method create.

public void create(Properties ctx, TransformerHandler document) throws SAXException {
    int import_id = Env.getContextAsInt(ctx, X_AD_Package_Exp_Detail.COLUMNNAME_AD_ImpFormat_ID);
    if (formats.contains(import_id))
        return;
    formats.add(import_id);
    AttributesImpl atts = new AttributesImpl();
    X_AD_ImpFormat m_ImpFormat = new X_AD_ImpFormat(ctx, import_id, null);
    atts = createImpFormatBinding(atts, m_ImpFormat);
    document.startElement("", "", "impformat", atts);
    String sql = "SELECT * FROM AD_ImpFormat_Row WHERE AD_ImpFormat_ID= " + import_id + " ORDER BY " + X_AD_ImpFormat_Row.COLUMNNAME_AD_ImpFormat_Row_ID;
    PreparedStatement pstmt = null;
    pstmt = DB.prepareStatement(sql, getTrxName(ctx));
    try {
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            createImpFormatRow(ctx, document, rs.getInt("AD_ImpFormat_Row_ID"));
        }
        rs.close();
        pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, "ImpFormat", e);
        throw new DatabaseAccessException("Failed to export Import Format.", e);
    } finally {
        try {
            if (pstmt != null)
                pstmt.close();
        } catch (Exception e) {
        }
        pstmt = null;
    }
    document.endElement("", "", "impformat");
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) X_AD_ImpFormat(org.compiere.model.X_AD_ImpFormat) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) POSaveFailedException(org.adempiere.pipo.exception.POSaveFailedException) DatabaseAccessException(org.adempiere.pipo.exception.DatabaseAccessException) SAXException(org.xml.sax.SAXException) DatabaseAccessException(org.adempiere.pipo.exception.DatabaseAccessException)

Aggregations

DatabaseAccessException (org.adempiere.pipo.exception.DatabaseAccessException)14 POSaveFailedException (org.adempiere.pipo.exception.POSaveFailedException)10 PreparedStatement (java.sql.PreparedStatement)9 ResultSet (java.sql.ResultSet)9 SAXException (org.xml.sax.SAXException)9 AttributesImpl (org.xml.sax.helpers.AttributesImpl)9 SQLException (java.sql.SQLException)6 PackOut (org.adempiere.pipo.PackOut)5 Attributes (org.xml.sax.Attributes)5 BigDecimal (java.math.BigDecimal)1 X_AD_Browse_Access (org.adempiere.model.X_AD_Browse_Access)1 PackIn (org.adempiere.pipo.PackIn)1 MColumn (org.compiere.model.MColumn)1 MTable (org.compiere.model.MTable)1 Query (org.compiere.model.Query)1 X_AD_Element (org.compiere.model.X_AD_Element)1 X_AD_ImpFormat (org.compiere.model.X_AD_ImpFormat)1 X_AD_PrintFormat (org.compiere.model.X_AD_PrintFormat)1 X_AD_Process (org.compiere.model.X_AD_Process)1 X_AD_Reference (org.compiere.model.X_AD_Reference)1