Search in sources :

Example 1 with MField

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

the class WindowCopy method doIt.

//	prepare
/**
	 * 	Process
	 *	@return message
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    log.info("doIt - To AD_Window_ID=" + p_AD_WindowTo_ID + ", From=" + p_AD_WindowFrom_ID);
    MWindow from = new MWindow(getCtx(), p_AD_WindowFrom_ID, get_TrxName());
    if (from.get_ID() == 0)
        throw new AdempiereUserError("@NotFound@ (from->) @AD_Window_ID@");
    MWindow to = new MWindow(getCtx(), p_AD_WindowTo_ID, get_TrxName());
    if (to.get_ID() == 0)
        throw new AdempiereUserError("@NotFound@ (to<-) @AD_Window_ID@");
    int tabCount = 0;
    int fieldCount = 0;
    MTab[] oldTabs = from.getTabs(false, get_TrxName());
    for (int i = 0; i < oldTabs.length; i++) {
        MTab oldTab = oldTabs[i];
        MTab newTab = new MTab(to, oldTab);
        if (newTab.save()) {
            tabCount++;
            //	Copy Fields
            MField[] oldFields = oldTab.getFields(false, get_TrxName());
            for (int j = 0; j < oldFields.length; j++) {
                MField oldField = oldFields[j];
                MField newField = new MField(newTab, oldField);
                if (newField.save())
                    fieldCount++;
                else
                    throw new AdempiereUserError("@Error@ @AD_Field_ID@");
            }
        } else
            throw new AdempiereUserError("@Error@ @AD_Tab_ID@");
    }
    return "@Copied@ #" + tabCount + "/" + fieldCount;
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MTab(org.compiere.model.MTab) MWindow(org.compiere.model.MWindow) MField(org.compiere.model.MField)

Example 2 with MField

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

the class TabCopy method doIt.

//	prepare
/**
	 * 	Process
	 *	@return message
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    log.info("To AD_Tab_ID=" + p_AD_TabTo_ID + ", From=" + p_AD_TabFrom_ID);
    MTab from = new MTab(getCtx(), p_AD_TabFrom_ID, get_TrxName());
    if (from.get_ID() == 0)
        throw new AdempiereUserError("@NotFound@ (from->) @AD_Tab_ID@");
    MTab to = new MTab(getCtx(), p_AD_TabTo_ID, get_TrxName());
    if (to.get_ID() == 0)
        throw new AdempiereUserError("@NotFound@ (to<-) @AD_Tab_ID@");
    if (from.getAD_Table_ID() != to.getAD_Table_ID())
        throw new AdempiereUserError("@Error@ @AD_Table_ID@");
    int count = 0;
    MField[] oldFields = from.getFields(false, get_TrxName());
    for (int i = 0; i < oldFields.length; i++) {
        MField oldField = oldFields[i];
        MField newField = new MField(to, oldField);
        if (newField.save())
            count++;
        else
            throw new AdempiereUserError("@Error@ @AD_Field_ID@");
    }
    return "@Copied@ #" + count;
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MTab(org.compiere.model.MTab) MField(org.compiere.model.MField)

Example 3 with MField

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

the class TabCreateFields method doIt.

//	prepare
/**
	 * 	Process
	 *	@return info
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    MTab tab = new MTab(getCtx(), p_AD_Tab_ID, get_TrxName());
    if (p_AD_Tab_ID == 0 || tab == null || tab.get_ID() == 0)
        throw new AdempiereSystemError("@NotFound@: @AD_Tab_ID@ " + p_AD_Tab_ID);
    log.info(tab.toString());
    //
    int count = 0;
    String sql = "SELECT * FROM AD_Column c " + "WHERE NOT EXISTS (SELECT * FROM AD_Field f " + "WHERE c.AD_Column_ID=f.AD_Column_ID" + //	#1
    " AND c.AD_Table_ID=?" + //	#2
    " AND f.AD_Tab_ID=?)" + //	#3
    " AND AD_Table_ID=?" + " AND NOT (Name LIKE 'Created%' OR Name LIKE 'Updated%')" + " AND IsActive='Y' " + "ORDER BY Name";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, get_TrxName());
        pstmt.setInt(1, tab.getAD_Table_ID());
        pstmt.setInt(2, tab.getAD_Tab_ID());
        pstmt.setInt(3, tab.getAD_Table_ID());
        rs = pstmt.executeQuery();
        while (rs.next()) {
            MColumn column = new MColumn(getCtx(), rs, get_TrxName());
            //
            MField field = new MField(tab);
            field.setColumn(column);
            if ("D".equals(column.getEntityType()))
                field.setEntityType(tab.getEntityType());
            else
                field.setEntityType(column.getEntityType());
            if (column.isKey())
                field.setIsDisplayed(false);
            if (field.save()) {
                addLog(0, null, null, column.getName());
                count++;
            }
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return "@Created@ #" + count;
}
Also used : MColumn(org.compiere.model.MColumn) AdempiereSystemError(org.compiere.util.AdempiereSystemError) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MTab(org.compiere.model.MTab) MField(org.compiere.model.MField)

Example 4 with MField

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

the class ASPGenerateLevel method addNodeToLevel.

//	doIt
private void addNodeToLevel(MTreeNode nn) {
    // Add Menu
    MMenu menu = new MMenu(getCtx(), nn.getNode_ID(), get_TrxName());
    if (menu.getAction().equals(MMenu.ACTION_Window)) {
        MWindow window = new MWindow(getCtx(), menu.getAD_Window_ID(), get_TrxName());
        int asp_window_id = DB.getSQLValueEx(get_TrxName(), "SELECT ASP_Window_ID FROM ASP_Window WHERE ASP_Level_ID = ? AND AD_Window_ID = ?", p_ASP_Level_ID, window.getAD_Window_ID());
        X_ASP_Window aspWindow = null;
        if (asp_window_id < 1) {
            // Add Window, Tabs and Fields (if IsGenerateFields)
            aspWindow = new X_ASP_Window(getCtx(), 0, get_TrxName());
            aspWindow.setASP_Level_ID(p_ASP_Level_ID);
            aspWindow.setAD_Window_ID(window.getAD_Window_ID());
            aspWindow.setASP_Status(p_ASP_Status);
            if (aspWindow.save()) {
                noWindows++;
                asp_window_id = aspWindow.getASP_Window_ID();
            }
        } else {
            aspWindow = new X_ASP_Window(getCtx(), asp_window_id, get_TrxName());
        }
        // tabs
        for (MTab tab : window.getTabs(true, get_TrxName())) {
            int asp_tab_id = DB.getSQLValueEx(get_TrxName(), "SELECT ASP_Tab_ID FROM ASP_Tab WHERE ASP_Window_ID = ? AND AD_Tab_ID = ?", asp_window_id, tab.getAD_Tab_ID());
            X_ASP_Tab aspTab = null;
            if (asp_tab_id < 1) {
                aspTab = new X_ASP_Tab(getCtx(), 0, get_TrxName());
                aspTab.setASP_Window_ID(asp_window_id);
                aspTab.setAD_Tab_ID(tab.getAD_Tab_ID());
                aspTab.setASP_Status(p_ASP_Status);
                aspTab.setAllFields(!p_IsGenerateFields);
                if (aspTab.save()) {
                    noTabs++;
                    asp_tab_id = aspTab.getASP_Tab_ID();
                }
            } else {
                aspTab = new X_ASP_Tab(getCtx(), asp_tab_id, get_TrxName());
            }
            // fields
            for (MField field : tab.getFields(true, get_TrxName())) {
                if (p_IsGenerateFields) {
                    if (DB.getSQLValueEx(get_TrxName(), "SELECT COUNT(*) FROM ASP_Field WHERE ASP_Tab_ID = ? AND AD_Field_ID = ?", aspTab.getASP_Tab_ID(), field.getAD_Field_ID()) < 1) {
                        X_ASP_Field aspField = new X_ASP_Field(getCtx(), 0, get_TrxName());
                        aspField.setASP_Tab_ID(aspTab.getASP_Tab_ID());
                        aspField.setAD_Field_ID(field.getAD_Field_ID());
                        aspField.setASP_Status(p_ASP_Status);
                        if (aspField.save())
                            noFields++;
                    }
                }
                // verify if a field is a button and assign permission to the corresponding process
                MColumn column = MColumn.get(getCtx(), field.getAD_Column_ID());
                if (column.getAD_Reference_ID() == DisplayType.Button) {
                    if (column.getAD_Process_ID() > 0) {
                        generateProcess(column.getAD_Process_ID());
                    }
                }
            }
        }
    } else if (menu.getAction().equals(MMenu.ACTION_Process) || menu.getAction().equals(MMenu.ACTION_Report)) {
        generateProcess(menu.getAD_Process_ID());
    } else if (menu.getAction().equals(MMenu.ACTION_Form)) {
        // Add Form
        MForm form = new MForm(getCtx(), menu.getAD_Form_ID(), get_TrxName());
        if (DB.getSQLValueEx(get_TrxName(), "SELECT COUNT(*) FROM ASP_Form WHERE ASP_Level_ID = ? AND AD_Form_ID = ?", p_ASP_Level_ID, form.getAD_Form_ID()) < 1) {
            X_ASP_Form aspForm = new X_ASP_Form(getCtx(), 0, get_TrxName());
            aspForm.setASP_Level_ID(p_ASP_Level_ID);
            aspForm.setAD_Form_ID(form.getAD_Form_ID());
            aspForm.setASP_Status(p_ASP_Status);
            if (aspForm.save())
                noForms++;
        }
    } else if (menu.getAction().equals(MMenu.ACTION_SmartBrowse)) {
        // Add Browse
        MBrowse browse = new MBrowse(getCtx(), menu.getAD_Browse_ID(), get_TrxName());
        if (DB.getSQLValueEx(get_TrxName(), "SELECT COUNT(*) FROM ASP_Browse WHERE ASP_Level_ID = ? AND AD_Browse_ID = ?", p_ASP_Level_ID, browse.getAD_Browse_ID()) < 1) {
            X_ASP_Browse aspBrowse = new X_ASP_Browse(getCtx(), 0, get_TrxName());
            aspBrowse.setASP_Level_ID(p_ASP_Level_ID);
            aspBrowse.setAD_Browse_ID(browse.getAD_Browse_ID());
            aspBrowse.setASP_Status(p_ASP_Status);
            if (aspBrowse.save())
                noBrowses++;
        }
    } else if (menu.getAction().equals(MMenu.ACTION_Task)) {
        // Add Task
        MTask task = new MTask(getCtx(), menu.getAD_Task_ID(), get_TrxName());
        if (DB.getSQLValueEx(get_TrxName(), "SELECT COUNT(*) FROM ASP_Task WHERE ASP_Level_ID = ? AND AD_Task_ID = ?", p_ASP_Level_ID, task.getAD_Task_ID()) < 1) {
            X_ASP_Task aspTask = new X_ASP_Task(getCtx(), 0, get_TrxName());
            aspTask.setASP_Level_ID(p_ASP_Level_ID);
            aspTask.setAD_Task_ID(task.getAD_Task_ID());
            aspTask.setASP_Status(p_ASP_Status);
            if (aspTask.save())
                noTasks++;
        }
    } else if (menu.getAction().equals(MMenu.ACTION_WorkFlow)) {
        generateWorkflow(menu.getAD_Workflow_ID());
    }
}
Also used : MColumn(org.compiere.model.MColumn) X_ASP_Field(org.compiere.model.X_ASP_Field) MTab(org.compiere.model.MTab) MMenu(org.compiere.model.MMenu) MWindow(org.compiere.model.MWindow) X_ASP_Tab(org.compiere.model.X_ASP_Tab) MBrowse(org.adempiere.model.MBrowse) MForm(org.compiere.model.MForm) MTask(org.compiere.model.MTask) X_ASP_Task(org.compiere.model.X_ASP_Task) X_ASP_Form(org.compiere.model.X_ASP_Form) X_ASP_Window(org.compiere.model.X_ASP_Window) MField(org.compiere.model.MField) X_ASP_Browse(org.eevolution.model.X_ASP_Browse)

Example 5 with MField

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

the class FieldElementHandler method startElement.

public void startElement(Properties ctx, Element element) throws SAXException {
    final String include_tabname = element.attributes.getValue("ADIncludeTabNameID");
    // Set Included Tab ID if this task was previously postponed 
    if (element.defer && element.recordId > 0 && include_tabname != null) {
        MField field = new MField(ctx, element.recordId, getTrxName(ctx));
        setIncluded_Tab_ID(ctx, field, include_tabname);
        field.saveEx();
        return;
    }
    PackIn packIn = (PackIn) ctx.get("PackInProcess");
    String elementValue = element.getElementValue();
    Attributes atts = element.attributes;
    log.info(elementValue + " " + atts.getValue("Name"));
    String entitytype = atts.getValue("EntityType");
    if (isProcessElement(ctx, entitytype)) {
        if (element.parent != null && element.parent.getElementValue().equals("tab") && element.parent.defer) {
            element.defer = true;
            return;
        }
        String name = atts.getValue("Name");
        String tabname = atts.getValue("ADTabNameID");
        String colname = atts.getValue("ADColumnNameID");
        String tableName = atts.getValue("ADTableNameID");
        int tableid = packIn.getTableId(tableName);
        if (tableid <= 0) {
            tableid = get_IDWithColumn(ctx, "AD_Table", "TableName", tableName);
            if (tableid > 0)
                packIn.addTable(tableName, tableid);
        }
        if (tableid <= 0) {
            element.defer = true;
            return;
        }
        int windowid = get_ID(ctx, "AD_Window", atts.getValue("ADWindowNameID"));
        if (windowid <= 0) {
            element.defer = true;
            return;
        }
        int columnid = packIn.getColumnId(tableName, colname);
        if (columnid <= 0) {
            columnid = get_IDWithMasterAndColumn(ctx, "AD_Column", "ColumnName", colname, "AD_Table", tableid);
            if (columnid > 0)
                packIn.addColumn(tableName, colname, columnid);
        }
        if (columnid <= 0) {
            element.defer = true;
            return;
        }
        int tabid = 0;
        if (element.parent != null && element.parent.getElementValue().equals("tab") && element.parent.recordId > 0) {
            tabid = element.parent.recordId;
        } else {
            StringBuffer sqlB = new StringBuffer("select AD_Tab_ID from AD_Tab where AD_Window_ID = " + windowid).append(" and Name = '" + tabname + "'").append(" and AD_Table_ID = ?");
            tabid = DB.getSQLValue(getTrxName(ctx), sqlB.toString(), tableid);
            if (element.parent != null && element.parent.getElementValue().equals("tab") && tabid > 0) {
                element.parent.recordId = tabid;
            }
        }
        if (tabid > 0) {
            StringBuffer sqlB = new StringBuffer("select AD_Field_ID from AD_Field where AD_Column_ID = ").append(columnid).append(" and AD_Tab_ID = ?");
            int id = DB.getSQLValue(getTrxName(ctx), sqlB.toString(), tabid);
            final MField m_Field = new MField(ctx, id, getTrxName(ctx));
            if (id <= 0 && atts.getValue("AD_Field_ID") != null && Integer.parseInt(atts.getValue("AD_Field_ID")) <= PackOut.MAX_OFFICIAL_ID)
                m_Field.setAD_Field_ID(Integer.parseInt(atts.getValue("AD_Field_ID")));
            int AD_Backup_ID = -1;
            String Object_Status = null;
            if (id > 0) {
                AD_Backup_ID = copyRecord(ctx, "AD_Field", m_Field);
                Object_Status = "Update";
            } else {
                Object_Status = "New";
                AD_Backup_ID = 0;
            }
            m_Field.setName(atts.getValue("Name"));
            m_Field.setAD_Column_ID(columnid);
            name = atts.getValue("ADFieldGroupNameID");
            id = get_IDWithColumn(ctx, "AD_FieldGroup", "Name", name);
            m_Field.setAD_FieldGroup_ID(id);
            m_Field.setAD_Tab_ID(tabid);
            m_Field.setEntityType(atts.getValue("EntityType"));
            m_Field.setIsSameLine(Boolean.valueOf(atts.getValue("SameLine")).booleanValue());
            m_Field.setIsCentrallyMaintained(Boolean.valueOf(atts.getValue("isCentrallyMaintained")).booleanValue());
            m_Field.setIsDisplayed(Boolean.valueOf(atts.getValue("Displayed")).booleanValue());
            // m_Field.setIsEncrypted(Boolean.valueOf(atts.getValue("isEncrypted")).booleanValue());
            m_Field.setIsFieldOnly(Boolean.valueOf(atts.getValue("isFieldOnly")).booleanValue());
            m_Field.setIsHeading(Boolean.valueOf(atts.getValue("isHeading")).booleanValue());
            m_Field.setIsReadOnly(Boolean.valueOf(atts.getValue("isReadOnly")).booleanValue());
            m_Field.setSeqNo(Integer.parseInt(atts.getValue("SeqNo")));
            m_Field.setDisplayLength(Integer.parseInt(atts.getValue("DisplayLength")));
            m_Field.setDescription(getStringValue(atts, "Description"));
            m_Field.setHelp(getStringValue(atts, "Help"));
            m_Field.setIsActive(atts.getValue("isActive") != null ? Boolean.valueOf(atts.getValue("isActive")).booleanValue() : true);
            String sortNo = getStringValue(atts, "SortNo");
            if (sortNo != null)
                m_Field.setSortNo(new BigDecimal(sortNo));
            m_Field.setDisplayLogic(getStringValue(atts, "DisplayLogic"));
            String Name = atts.getValue("ADReferenceNameID");
            id = get_IDWithColumn(ctx, "AD_Reference", "Name", Name);
            m_Field.setAD_Reference_ID(id);
            Name = atts.getValue("ADValRuleNameID");
            id = get_IDWithColumn(ctx, "AD_Val_Rule", "Name", Name);
            m_Field.setAD_Val_Rule_ID(id);
            Name = atts.getValue("ADReferenceNameValueID");
            id = get_IDWithColumn(ctx, "AD_Reference", "Name", Name);
            m_Field.setAD_Reference_Value_ID(id);
            m_Field.setInfoFactoryClass(getStringValue(atts, "InfoFactoryClass"));
            if ("Y".equals(atts.getValue("isMandatory")))
                m_Field.setIsMandatory(atts.getValue("isMandatory"));
            else if ("N".equals(atts.getValue("isMandatory")))
                m_Field.setIsMandatory(atts.getValue("isMandatory"));
            m_Field.setDefaultValue(atts.getValue("DefaultValue"));
            if (atts.getValue("IsDisplayedGrid") != null)
                m_Field.setIsDisplayedGrid(Boolean.valueOf(atts.getValue("IsDisplayedGrid")).booleanValue());
            if (atts.getValue("PreferredWidth") != null)
                m_Field.setPreferredWidth(Integer.parseInt(atts.getValue("PreferredWidth")));
            setIncluded_Tab_ID(ctx, m_Field, include_tabname);
            if (m_Field.save(getTrxName(ctx)) == true) {
                record_log(ctx, 1, m_Field.getName(), "Field", m_Field.get_ID(), AD_Backup_ID, Object_Status, "AD_Field", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Field"));
                element.recordId = m_Field.getAD_Field_ID();
            } else {
                record_log(ctx, 0, m_Field.getName(), "Field", m_Field.get_ID(), AD_Backup_ID, Object_Status, "AD_Field", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Field"));
                throw new POSaveFailedException("Failed to save field definition.");
            }
            // If Included Tab not found, then postpone this task for later processing 
            if (m_Field.getAD_Field_ID() > 0 && include_tabname != null && m_Field.getIncluded_Tab_ID() <= 0) {
                element.defer = true;
            }
        } else {
            element.defer = true;
            return;
        }
    } else {
        element.skip = true;
    }
}
Also used : PackIn(org.adempiere.pipo.PackIn) Attributes(org.xml.sax.Attributes) POSaveFailedException(org.adempiere.pipo.exception.POSaveFailedException) MField(org.compiere.model.MField) BigDecimal(java.math.BigDecimal)

Aggregations

MField (org.compiere.model.MField)6 MTab (org.compiere.model.MTab)5 MColumn (org.compiere.model.MColumn)3 MWindow (org.compiere.model.MWindow)2 X_ASP_Field (org.compiere.model.X_ASP_Field)2 X_ASP_Tab (org.compiere.model.X_ASP_Tab)2 AdempiereUserError (org.compiere.util.AdempiereUserError)2 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 MBrowse (org.adempiere.model.MBrowse)1 PackIn (org.adempiere.pipo.PackIn)1 POSaveFailedException (org.adempiere.pipo.exception.POSaveFailedException)1 MForm (org.compiere.model.MForm)1 MMenu (org.compiere.model.MMenu)1 MTask (org.compiere.model.MTask)1 X_ASP_Form (org.compiere.model.X_ASP_Form)1 X_ASP_Task (org.compiere.model.X_ASP_Task)1 X_ASP_Window (org.compiere.model.X_ASP_Window)1 AdempiereSystemError (org.compiere.util.AdempiereSystemError)1