Search in sources :

Example 1 with M_Element

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

the class CopyColumnsFromTable method doIt.

//	prepare
/**
	 * 	Process
	 *	@return info
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    if (p_target_AD_Table_ID == 0)
        throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_target_AD_Table_ID);
    if (p_source_AD_Table_ID == 0)
        throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_source_AD_Table_ID);
    log.info("Source AD_Table_ID=" + p_source_AD_Table_ID + ", Target AD_Table_ID=" + p_target_AD_Table_ID);
    MTable targetTable = new MTable(getCtx(), p_target_AD_Table_ID, get_TrxName());
    MColumn[] targetColumns = targetTable.getColumns(true);
    //if (targetColumns.length > 0)
    //	// TODO: dictionary message
    //	throw new AdempiereSystemError("Target table must not have columns");
    MTable sourceTable = new MTable(getCtx(), p_source_AD_Table_ID, get_TrxName());
    MColumn[] sourceColumns = sourceTable.getColumns(true);
    for (int i = 0; i < sourceColumns.length; i++) {
        //[FR1784588] logic to validate exist columns
        boolean foundColumn = false;
        MColumn colTarget = new MColumn(targetTable);
        for (MColumn col : targetColumns) {
            String columnName = null;
            if (sourceColumns[i].getColumnName().equals(sourceTable.getTableName() + "_ID")) {
                columnName = new String(targetTable.getTableName() + "_ID");
            } else {
                columnName = sourceColumns[i].getColumnName();
            }
            if (col.getColumnName().equals(columnName)) {
                foundColumn = true;
                break;
            }
        }
        if (foundColumn)
            continue;
        // special case the key -> sourceTable_ID
        if (sourceColumns[i].getColumnName().equals(sourceTable.getTableName() + "_ID")) {
            String targetColumnName = new String(targetTable.getTableName() + "_ID");
            colTarget.setColumnName(targetColumnName);
            // if the element don't exist, create it 
            M_Element element = M_Element.get(getCtx(), targetColumnName);
            if (element == null) {
                element = new M_Element(getCtx(), targetColumnName, targetTable.getEntityType(), get_TrxName());
                if (targetColumnName.equalsIgnoreCase(targetTable.getTableName() + "_ID")) {
                    element.setColumnName(targetTable.getTableName() + "_ID");
                    element.setName(targetTable.getName());
                    element.setPrintName(targetTable.getName());
                }
                element.save(get_TrxName());
            }
            colTarget.setAD_Element_ID(element.getAD_Element_ID());
            colTarget.setName(targetTable.getName());
            colTarget.setDescription(targetTable.getDescription());
            colTarget.setHelp(targetTable.getHelp());
        } else {
            colTarget.setColumnName(sourceColumns[i].getColumnName());
            colTarget.setAD_Element_ID(sourceColumns[i].getAD_Element_ID());
            colTarget.setName(sourceColumns[i].getName());
            colTarget.setDescription(sourceColumns[i].getDescription());
            colTarget.setHelp(sourceColumns[i].getHelp());
        }
        colTarget.setVersion(sourceColumns[i].getVersion());
        colTarget.setAD_Val_Rule_ID(sourceColumns[i].getAD_Val_Rule_ID());
        colTarget.setDefaultValue(sourceColumns[i].getDefaultValue());
        colTarget.setFieldLength(sourceColumns[i].getFieldLength());
        colTarget.setIsKey(sourceColumns[i].isKey());
        colTarget.setIsParent(sourceColumns[i].isParent());
        colTarget.setIsMandatory(sourceColumns[i].isMandatory());
        colTarget.setIsTranslated(sourceColumns[i].isTranslated());
        colTarget.setIsIdentifier(sourceColumns[i].isIdentifier());
        colTarget.setSeqNo(sourceColumns[i].getSeqNo());
        colTarget.setIsEncrypted(sourceColumns[i].getIsEncrypted());
        colTarget.setAD_Reference_ID(sourceColumns[i].getAD_Reference_ID());
        colTarget.setAD_Reference_Value_ID(sourceColumns[i].getAD_Reference_Value_ID());
        colTarget.setIsActive(sourceColumns[i].isActive());
        colTarget.setVFormat(sourceColumns[i].getVFormat());
        colTarget.setCallout(sourceColumns[i].getCallout());
        colTarget.setIsUpdateable(sourceColumns[i].isUpdateable());
        colTarget.setAD_Process_ID(sourceColumns[i].getAD_Process_ID());
        colTarget.setValueMin(sourceColumns[i].getValueMin());
        colTarget.setValueMax(sourceColumns[i].getValueMax());
        colTarget.setIsSelectionColumn(sourceColumns[i].isSelectionColumn());
        colTarget.setReadOnlyLogic(sourceColumns[i].getReadOnlyLogic());
        colTarget.setIsSyncDatabase(sourceColumns[i].getIsSyncDatabase());
        colTarget.setIsAlwaysUpdateable(sourceColumns[i].isAlwaysUpdateable());
        colTarget.setColumnSQL(sourceColumns[i].getColumnSQL());
        colTarget.save(get_TrxName());
        // TODO: Copy translations
        m_count++;
    }
    //
    return "#" + m_count;
}
Also used : MColumn(org.compiere.model.MColumn) MTable(org.compiere.model.MTable) AdempiereSystemError(org.compiere.util.AdempiereSystemError) M_Element(org.compiere.model.M_Element)

Example 2 with M_Element

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

the class CalloutParameter method element.

/**
	 * Set the default values from Element
	 * 
	 * @param ctx
	 * @param WindowNo
	 * @param mTab
	 * @param mField
	 * @param value
	 * @return
	 */
public String element(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) {
    Integer AD_Element_ID = (Integer) value;
    if (AD_Element_ID == null || AD_Element_ID <= 0)
        return "";
    I_AD_Process_Para para = GridTabWrapper.create(mTab, I_AD_Process_Para.class);
    MTable table = MTable.get(ctx, para.Table_ID);
    MProcess process = MProcess.get(ctx, para.getAD_Process_ID());
    para.setEntityType(process.getEntityType());
    M_Element element = new M_Element(ctx, AD_Element_ID, null);
    if (element.getAD_Reference_ID() == DisplayType.ID) {
        String columnName = table.get_TableName() + "_ID";
        if (!columnName.equals(element.getColumnName())) {
            para.setAD_Reference_ID(DisplayType.TableDir);
        }
    }
    if (para.getColumnName() == null || para.getColumnName().length() <= 0)
        para.setColumnName(element.getColumnName());
    if (para.getFieldLength() <= 0)
        para.setFieldLength(element.getFieldLength());
    if (para.getAD_Reference_ID() <= 0)
        para.setAD_Reference_ID(element.getAD_Reference_ID());
    if (para.getAD_Reference_Value_ID() <= 0)
        para.setAD_Reference_Value_ID(element.getAD_Reference_Value_ID());
    if (para.getName() == null || para.getName().length() <= 0)
        para.setName(element.getName());
    if (para.getDescription() == null || para.getDescription().length() <= 0)
        para.setDescription(element.getDescription());
    if (para.getHelp() == null || para.getHelp().length() <= 0)
        para.setHelp(element.getHelp());
    return "";
}
Also used : MProcess(org.compiere.model.MProcess) MTable(org.compiere.model.MTable) I_AD_Process_Para(org.compiere.model.I_AD_Process_Para) M_Element(org.compiere.model.M_Element)

Example 3 with M_Element

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

the class SmallViewController method createVO.

//	createField
/**
	 * Create GridFieldVO from a MBrowseField field
	 * @param field the MBrowseField
	 * @param isTo true if the MBrowseField represents the "To" part of a range
	 * @param windowNo - the window number that will be used in the context
	 * @return GridFieldVO used to establish a GridField field.
	 */
private GridFieldVO createVO(MBrowseField field, boolean isTo, int windowNo) {
    //  Create Field
    GridFieldVO voBase = GridFieldVO.createStdField(field.getCtx(), windowNo, 0, 0, 0, false, false, false);
    String uniqueName = field.getAD_View_Column().getColumnName() + (isTo ? "_To" : "");
    voBase.isProcess = true;
    voBase.IsDisplayed = true;
    voBase.IsReadOnly = false;
    voBase.IsUpdateable = true;
    voBase.WindowNo = windowNo;
    M_Element element = (M_Element) field.getAD_Element();
    voBase.ColumnName = element.getColumnName();
    //	FR [ 344 ]
    voBase.ColumnSQL = field.getAD_View_Column().getColumnSQL();
    //	BR [ 318 ]
    if (field.getAD_View_Column().getAD_Column_ID() > 0) {
        voBase.AD_Column_ID = field.getAD_View_Column().getAD_Column_ID();
        voBase.AD_Table_ID = field.getAD_View_Column().getAD_Column().getAD_Table_ID();
    }
    //	Set unique alias
    voBase.ColumnNameAlias = uniqueName;
    voBase.displayType = field.getAD_Reference_ID();
    voBase.AD_Reference_Value_ID = field.getAD_Reference_Value_ID();
    voBase.IsMandatory = field.isMandatory();
    voBase.IsAlwaysUpdateable = true;
    voBase.IsKey = field.isKey();
    voBase.DefaultValue = isTo ? field.getDefaultValue2() : field.getDefaultValue();
    voBase.DefaultValue2 = field.getDefaultValue2();
    voBase.InfoFactoryClass = field.getInfoFactoryClass();
    voBase.FieldLength = field.getFieldLength();
    voBase.ReadOnlyLogic = field.getReadOnlyLogic();
    voBase.DisplayLogic = field.getDisplayLogic();
    voBase.VFormat = field.getVFormat();
    voBase.ValueMin = field.getValueMin();
    voBase.ValueMax = field.getValueMax();
    voBase.ValidationCode = field.getAD_Val_Rule().getCode();
    //	FR [ 349 ]
    voBase.IsRange = field.isRange();
    voBase.Description = field.getDescription();
    voBase.Help = field.getHelp();
    voBase.Header = isTo ? Msg.getMsg(Env.getCtx(), "To") : field.getName();
    voBase.IsColumnSQLReference = true;
    voBase.initFinish();
    //	Return 
    return voBase;
}
Also used : GridFieldVO(org.compiere.model.GridFieldVO) M_Element(org.compiere.model.M_Element)

Example 4 with M_Element

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

the class DeveloperModeBL method createMessageOrElement0.

private boolean createMessageOrElement0(Properties ctx, final String adLanguage, final String text, boolean checkMessage, boolean checkElement) {
    final String trxName = null;
    final MMessage message = checkMessage ? MMessage.get(ctx, text) : null;
    if (message != null) {
        return checkInsertTrl(message, adLanguage);
    }
    final M_Element element = checkElement ? M_Element.get(ctx, text) : null;
    if (element != null) {
        return checkInsertTrl(element, adLanguage);
    }
    boolean createMessage = checkMessage && isValidMessageValue(text);
    boolean createElement = checkElement && isValidColumnName(text);
    if (!createElement && !createMessage) {
        return false;
    }
    if (createElement && createMessage) {
        // TODO: ask developer what to do
        createMessage = true;
        createElement = false;
    }
    if (createMessage) {
        final MMessage messageNew = new MMessage(ctx, 0, trxName);
        messageNew.setValue(text);
        messageNew.setMsgType(X_AD_Message.MSGTYPE_Information);
        messageNew.setMsgText(text);
        messageNew.setEntityType(getEntityType(ctx));
        // The save will trigger CCache reset for "AD_Message" which will clear message from Msg class
        messageNew.saveEx();
        logger.log(Level.WARNING, "Created: " + messageNew + ", Value=" + messageNew.getValue() + ", EntityType=" + messageNew.getEntityType(), new Exception());
    }
    if (createElement) {
        final M_Element elementNew = new M_Element(ctx, 0, trxName);
        elementNew.setColumnName(text);
        elementNew.setName(text);
        elementNew.setPrintName(text);
        elementNew.setEntityType(getEntityType(ctx));
        elementNew.saveEx();
        logger.log(Level.WARNING, "Created: " + element + ", ColumnName=" + elementNew.getColumnName() + ", EntityType=" + elementNew.getEntityType(), new Exception());
    }
    return createMessage || createElement;
}
Also used : MMessage(org.compiere.model.MMessage) M_Element(org.compiere.model.M_Element)

Example 5 with M_Element

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

the class TableCreateColumns method addTableColumn.

//	addTable
/**
	 * 	Add Table Column
	 *	@param rs result set with meta data
	 *	@param table table
	 *	@throws Exception
	 */
private void addTableColumn(ResultSet rs, MTable table) throws Exception {
    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
    while (rs.next()) {
        String tn = rs.getString("TABLE_NAME");
        if (!tableName.equalsIgnoreCase(tn))
            continue;
        String columnName = rs.getString("COLUMN_NAME");
        MColumn column = table.getColumn(columnName);
        if (column != null)
            continue;
        int dataType = rs.getInt("DATA_TYPE");
        String typeName = rs.getString("TYPE_NAME");
        String nullable = rs.getString("IS_NULLABLE");
        int size = rs.getInt("COLUMN_SIZE");
        int digits = rs.getInt("DECIMAL_DIGITS");
        //
        log.config(columnName + " - DataType=" + dataType + " " + typeName + ", Nullable=" + nullable + ", Size=" + size + ", Digits=" + digits);
        //
        column = new MColumn(table);
        // teo_sarca: bug [ 1640908 ] 
        column.set_TrxName(get_TrxName());
        column.setEntityType(p_EntityType);
        //
        M_Element element = M_Element.get(getCtx(), columnName);
        if (element == null) {
            element = new M_Element(getCtx(), columnName, p_EntityType, get_TrxName());
            //and the name & description from table
            if (columnName.equalsIgnoreCase(table.getTableName() + "_ID")) {
                element.setColumnName(table.getTableName() + "_ID");
                element.setName(table.getName());
                element.setPrintName(table.getName());
            }
            element.saveEx();
        }
        column.setColumnName(element.getColumnName());
        column.setName(element.getName());
        column.setDescription(element.getDescription());
        column.setHelp(element.getHelp());
        column.setAD_Element_ID(element.getAD_Element_ID());
        //
        column.setIsMandatory("NO".equals(nullable));
        // Key
        if (columnName.equalsIgnoreCase(tableName + "_ID")) {
            column.setIsKey(true);
            column.setAD_Reference_ID(DisplayType.ID);
            column.setIsUpdateable(false);
        } else // bug [ 1637912 ] 
        if (columnName.toUpperCase().endsWith("_ACCT") && size == 10)
            column.setAD_Reference_ID(DisplayType.Account);
        else // Account
        if (columnName.equalsIgnoreCase("C_Location_ID"))
            column.setAD_Reference_ID(DisplayType.Location);
        else // Product Attribute
        if (columnName.equalsIgnoreCase("M_AttributeSetInstance_ID"))
            column.setAD_Reference_ID(DisplayType.PAttribute);
        else // SalesRep_ID (=User)
        if (columnName.equalsIgnoreCase("SalesRep_ID")) {
            column.setAD_Reference_ID(DisplayType.Table);
            column.setAD_Reference_Value_ID(190);
        } else // ID
        if (columnName.toUpperCase().endsWith("_ID"))
            column.setAD_Reference_ID(DisplayType.TableDir);
        else // Date
        if (dataType == Types.DATE || dataType == Types.TIME || dataType == Types.TIMESTAMP || // || columnName.toUpperCase().indexOf("DATE") != -1
        columnName.equalsIgnoreCase("Created") || columnName.equalsIgnoreCase("Updated"))
            column.setAD_Reference_ID(DisplayType.DateTime);
        else // CreatedBy/UpdatedBy (=User)
        if (columnName.equalsIgnoreCase("CreatedBy") || columnName.equalsIgnoreCase("UpdatedBy")) {
            column.setAD_Reference_ID(DisplayType.Table);
            column.setAD_Reference_Value_ID(110);
            column.setIsUpdateable(false);
        } else //	Entity Type
        if (columnName.equalsIgnoreCase("EntityType")) {
            column.setAD_Reference_ID(DisplayType.Table);
            column.setAD_Reference_Value_ID(389);
        } else // CLOB
        if (dataType == Types.CLOB)
            column.setAD_Reference_ID(DisplayType.TextLong);
        else // BLOB
        if (dataType == Types.BLOB)
            column.setAD_Reference_ID(DisplayType.Binary);
        else // Amount
        if (columnName.toUpperCase().indexOf("AMT") != -1)
            column.setAD_Reference_ID(DisplayType.Amount);
        else // Qty
        if (columnName.toUpperCase().indexOf("QTY") != -1)
            column.setAD_Reference_ID(DisplayType.Quantity);
        else // Boolean
        if (size == 1 && (columnName.toUpperCase().startsWith("IS") || dataType == Types.CHAR))
            column.setAD_Reference_ID(DisplayType.YesNo);
        else // List
        if (size < 4 && dataType == Types.CHAR)
            column.setAD_Reference_ID(DisplayType.List);
        else // Name, DocumentNo
        if (columnName.equalsIgnoreCase("Name") || columnName.equals("DocumentNo")) {
            column.setAD_Reference_ID(DisplayType.String);
            column.setIsIdentifier(true);
            column.setSeqNo(1);
        } else // String, Text
        if (dataType == Types.CHAR || dataType == Types.VARCHAR || typeName.startsWith("NVAR") || typeName.startsWith("NCHAR")) {
            if (//	MultiByte	
            typeName.startsWith("N"))
                size /= 2;
            if (size > 255)
                column.setAD_Reference_ID(DisplayType.Text);
            else
                column.setAD_Reference_ID(DisplayType.String);
        } else // Number
        if (dataType == Types.INTEGER || dataType == Types.SMALLINT || dataType == Types.DECIMAL || dataType == Types.NUMERIC) {
            if (size == 10)
                column.setAD_Reference_ID(DisplayType.Integer);
            else
                column.setAD_Reference_ID(DisplayType.Number);
        } else
            //	??
            column.setAD_Reference_ID(DisplayType.String);
        column.setFieldLength(size);
        if (column.isUpdateable() && (table.isView() || columnName.equalsIgnoreCase("AD_Client_ID") || columnName.equalsIgnoreCase("AD_Org_ID") || columnName.toUpperCase().startsWith("CREATED") || columnName.toUpperCase().equals("UPDATED")))
            column.setIsUpdateable(false);
        // Check if is a possible selection column
        if (MColumn.isSuggestSelectionColumn(column.getColumnName(), false))
            column.setIsSelectionColumn(true);
        //	Done
        if (column.save()) {
            addLog(0, null, null, table.getTableName() + "." + column.getColumnName());
            m_count++;
        }
    }
//	while columns
}
Also used : MColumn(org.compiere.model.MColumn) M_Element(org.compiere.model.M_Element)

Aggregations

M_Element (org.compiere.model.M_Element)6 MColumn (org.compiere.model.MColumn)2 MTable (org.compiere.model.MTable)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 GridFieldVO (org.compiere.model.GridFieldVO)1 I_AD_Process_Para (org.compiere.model.I_AD_Process_Para)1 MMessage (org.compiere.model.MMessage)1 MProcess (org.compiere.model.MProcess)1 AdempiereSystemError (org.compiere.util.AdempiereSystemError)1 Trx (org.compiere.util.Trx)1