NewWindow

From ADempiere ERP Wiki
Revision as of 09:37, 19 September 2006 by Kthiemann (talk | contribs) (How to create a new window in Adempiere)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Create new table (DB)

Create a new table via SQL in the database. The table must contain the Adempiere default columns (italic) if you want to create an Adempiere window for the table. Example:

CREATE TABLE XX_Material (
 AD_CLIENT_ID   	NUMBER(10)      NOT NULL,
 AD_ORG_ID      	NUMBER(10)     	NOT NULL,
 ISACTIVE       	CHAR(1 BYTE)    DEFAULT 'Y'          NOT NULL,
 CREATED        	DATE            DEFAULT SYSDATE      NOT NULL,
 CREATEDBY      	NUMBER(10)      NOT NULL,
 UPDATED        	DATE            DEFAULT SYSDATE      NOT NULL,
 UPDATEDBY      	NUMBER(10)      NOT NULL,
 XX_MATERIAL_ID     	INTEGER 	NOT NULL,
 MATNR     		SMALLINT 	NOT NULL,
 COLORNR    		SMALLINT 	NOT NULL,
 NAME      		VARCHAR(100)	NOT NULL,
 NAMESHORT  		VARCHAR(100)	NOT NULL
);
ALTER TABLE XX_Material ADD PRIMARY KEY (XX_MATERIAL_ID );

Create new table (Adempiere)

Create a new entry in the window Table and Column and set for DB Table Name the name of the new created DB-table (see step 1) (XX_Material).
Press Button Create Columns from DB. That creates for each db-tablecolumn a corresponding compiere Column.
Check the automatic created Columns, esspecialy the references (e.g. Number instead of Integer)

Generate Model

Use the class org.compiere.util.GenerateModel to generate a PO-Klass for new created tables. It should create a class X_XX_Material.java. Don't change this class, but you can extend it and put some logic in your extension (see example). The name of the extension-class has to be MMaterial.
Example: a method to get a MMaterial-Object with the given materialno and colorno.

public static MMaterial get(Properties ctx, int materialno, int colorno) {
    MMaterial retValue = null;
    String sql = "SELECT * FROM XX_Material WHERE MATNR=? AND COLORNR=?";
    PreparedStatement pstmt = null;
       try {
            pstmt = DB.prepareStatement (sql);
            pstmt.setInt(1, materialno);
            pstmt.setInt(2, colorno);
            final ResultSet rs = pstmt.executeQuery ();
            if (rs.next ()){
               retValue = new MMaterial(ctx, rs, null);
            }
            rs.close ();
            pstmt.close ();
            pstmt = null;
       } catch (SQLException e){
            s_log.log(Level.SEVERE, sql, e);
       } 
       try {
            if (pstmt != null){
               pstmt.close ();
            }
            pstmt = null;
       } catch (SQLException e)	{
            pstmt = null;
       }
       return retValue;
}

Create an Adempiere window

Create a new entry in the window Window Tab & Field with the name „XX_Material“. In the tab Tab create a new entry with the name „Material“ and select „XX_Material_XX_Material“ as it's table.
Use the button Create Fields to create fields for all columns of the table. In the tab Field Sequence you can choose the sequence of the fields in the new window and in the tab Field you can select some view-related attributes. Set the read-only flag for „XX_Material_ID“ (the id should be unchangeble).

Create a Reference

If you want to have a drop-down-box with values of the new table in other windows (like the drop-downs for bpartner-locations...) you need to create a Reference for the new table. Create a new entry in the window Reference with the name „XX_Material“ and select „Table Validation“ as Validation Type.
In the tab Table Validation select „XX_Material_XX_Material“ as table, „XX_Material_ID as Key Column and „Name“ as Display Column. The last one selects the column, that is used for the drop-down values. Remark: Close all windows (Reference and Window Tab & field) before you proceed. Compiere does not update the selectable references if you don't...
Now you can create a field with the name „Material“ in a compiere window of your choice and select „XX_Material_ID_Material“ as it's Column. After this, you will see a drop-down-box with all values of the name-column in that window.

You are done!