Search in sources :

Example 1 with ModelGetList

use of pl.x3E.adInterface.ModelGetList in project adempiere by adempiere.

the class ModelADServiceImpl method getList.

public WindowTabDataDocument getList(ModelGetListRequestDocument req) throws XFireFault {
    WindowTabDataDocument resdoc = WindowTabDataDocument.Factory.newInstance();
    WindowTabData res = resdoc.addNewWindowTabData();
    DataSet ds = res.addNewDataSet();
    ModelGetList modelGetList = req.getModelGetListRequest().getModelGetList();
    String serviceType = modelGetList.getServiceType();
    int cnt = 0;
    ADLoginRequest reqlogin = req.getModelGetListRequest().getADLoginRequest();
    String err = modelLogin(reqlogin, webServiceName, "getList", serviceType);
    if (err != null && err.length() > 0) {
        res.setError(err);
        res.setErrorInfo(err);
        res.setSuccess(false);
        return resdoc;
    }
    int roleid = reqlogin.getRoleID();
    // Validate parameters
    modelGetList.setADReferenceID(validateParameter("AD_Reference_ID", modelGetList.getADReferenceID()));
    modelGetList.setFilter(validateParameter("Filter", modelGetList.getFilter()));
    int ref_id = modelGetList.getADReferenceID();
    String filter = modelGetList.getFilter();
    if (filter == null || filter.length() == 0)
        filter = "";
    else
        filter = " AND " + filter;
    Properties ctx = m_cs.getM_ctx();
    X_AD_Reference ref = new X_AD_Reference(ctx, ref_id, null);
    String sql = null;
    ArrayList<String> listColumnNames = new ArrayList<String>();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    if (X_AD_Reference.VALIDATIONTYPE_ListValidation.equals(ref.getValidationType())) {
        // Fill List Reference
        String ad_language = Env.getAD_Language(ctx);
        boolean isBaseLanguage = Env.isBaseLanguage(ad_language, "AD_Ref_List");
        sql = isBaseLanguage ? "SELECT AD_Ref_List.AD_Ref_List_ID, AD_Ref_List.Value, AD_Ref_List.Name, AD_Ref_List.Description " + "FROM AD_Ref_List " + "WHERE AD_Ref_List.AD_Reference_ID=? AND AD_Ref_List.IsActive='Y' " + filter + " ORDER BY AD_Ref_List.Name" : "SELECT AD_Ref_List.AD_Ref_List_ID, AD_Ref_List.Value, AD_Ref_List_Trl.Name, AD_Ref_List_Trl.Description " + "FROM AD_Ref_List, AD_Ref_List_Trl " + "WHERE AD_Ref_List.AD_Reference_ID=? AND AD_Ref_List.IsActive='Y' AND AD_Ref_List_Trl.AD_Language=? AND AD_Ref_List.AD_Ref_List_ID=AD_Ref_List_Trl.AD_Ref_List_ID " + filter + " ORDER BY AD_Ref_List_Trl.Name";
        listColumnNames.add("AD_Ref_List_ID");
        listColumnNames.add("Value");
        listColumnNames.add("Name");
        listColumnNames.add("Description");
        try {
            pstmt = DB.prepareStatement(sql, null);
            pstmt.setInt(1, ref_id);
            if (!isBaseLanguage)
                pstmt.setString(2, ad_language);
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            res.setError(e.getMessage());
            res.setErrorInfo(sql);
            res.setSuccess(false);
            DB.close(rs, pstmt);
            rs = null;
            pstmt = null;
            throw new XFireFault(e.getClass().toString() + " " + e.getMessage() + " sql=" + sql, e.getCause(), new QName("getList"));
        }
    } else if (X_AD_Reference.VALIDATIONTYPE_TableValidation.equals(ref.getValidationType())) {
        // Fill values from a reference table
        MRole role = new MRole(ctx, roleid, null);
        String sqlrt = "SELECT * FROM AD_Ref_Table WHERE AD_Reference_ID=?";
        MRefTable rt = null;
        PreparedStatement pstmtrt = null;
        ResultSet rsrt = null;
        try {
            pstmtrt = DB.prepareStatement(sqlrt, null);
            pstmtrt.setInt(1, ref_id);
            rsrt = pstmtrt.executeQuery();
            if (rsrt.next())
                rt = new MRefTable(ctx, rsrt, null);
        } catch (Exception e) {
        // ignore this exception
        } finally {
            DB.close(rsrt, pstmtrt);
            rsrt = null;
            pstmtrt = null;
        }
        if (rt == null)
            throw new XFireFault("Web service type " + m_webservicetype.getValue() + ": reference table " + ref_id + " not found", new QName("getList"));
        MTable table = new MTable(ctx, rt.getAD_Table_ID(), null);
        MColumn column = new MColumn(ctx, rt.getAD_Key(), null);
        // TODO: if any value or identifier column is translated, then get them from trl table (and client has multilanguage documents enabled)
        sql = "SELECT " + column.getColumnName();
        listColumnNames.add(column.getColumnName());
        if (rt.isValueDisplayed()) {
            sql += ",Value";
            listColumnNames.add("Value");
        }
        String sqlident = "SELECT ColumnName FROM AD_Column WHERE AD_Table_ID=? AND IsActive='Y' AND IsIdentifier='Y' ORDER BY SeqNo";
        PreparedStatement pstmtident = null;
        ResultSet rsident = null;
        try {
            pstmtident = DB.prepareStatement(sqlident, null);
            pstmtident.setInt(1, rt.getAD_Table_ID());
            rsident = pstmtident.executeQuery();
            while (rsident.next()) {
                String colnameident = rsident.getString("ColumnName");
                if (rt.isValueDisplayed() && colnameident.equalsIgnoreCase("Value")) {
                // Value already added
                } else {
                    sql += "," + colnameident;
                    listColumnNames.add(colnameident);
                }
            }
        } catch (Exception e) {
        // ignore this exception
        } finally {
            DB.close(rsident, pstmtident);
            rsident = null;
            pstmtident = null;
        }
        sql += " FROM " + table.getTableName() + " WHERE IsActive='Y'";
        /** 2014-11-05 Carlos Parada Change for ReadOnly SQL Access */
        //sql = role.addAccessSQL(sql, table.getTableName(), true, true);
        sql = role.addAccessSQL(sql, table.getTableName(), true, false);
        /** End Carlos Parada */
        sql += filter;
        if (rt.getWhereClause() != null && rt.getWhereClause().length() > 0)
            sql += " AND " + rt.getWhereClause();
        if (rt.getOrderByClause() != null && rt.getOrderByClause().length() > 0)
            sql += " ORDER BY " + rt.getOrderByClause();
        try {
            pstmt = DB.prepareStatement(sql, null);
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            res.setError(e.getMessage());
            res.setErrorInfo(sql);
            res.setSuccess(false);
            DB.close(rs, pstmt);
            rs = null;
            pstmt = null;
            throw new XFireFault(e.getClass().toString() + " " + e.getMessage() + " sql=" + sql, e.getCause(), new QName("getList"));
        }
    } else {
    // Don't fill - wrong type
    }
    if (rs != null) {
        try {
            while (rs.next()) {
                cnt++;
                // Add values to the dataset
                DataRow dr = ds.addNewDataRow();
                for (String listColumnName : listColumnNames) {
                    if (m_webservicetype.isOutputColumnNameAllowed(listColumnName)) {
                        DataField dfid = dr.addNewField();
                        dfid.setColumn(listColumnName);
                        dfid.setVal(rs.getString(listColumnName));
                    }
                }
            }
            res.setSuccess(true);
        } catch (SQLException e) {
            res.setError(e.getMessage());
            res.setErrorInfo(sql);
            res.setSuccess(false);
            throw new XFireFault(e.getClass().toString() + " " + e.getMessage() + " sql=" + sql, e.getCause(), new QName("getList"));
        } finally {
            DB.close(rs, pstmt);
            rs = null;
            pstmt = null;
        }
    }
    res.setRowCount(cnt);
    res.setNumRows(cnt);
    res.setTotalRows(cnt);
    res.setStartRow(1);
    return resdoc;
}
Also used : MColumn(org.compiere.model.MColumn) ADLoginRequest(pl.x3E.adInterface.ADLoginRequest) DataSet(pl.x3E.adInterface.DataSet) SQLException(java.sql.SQLException) QName(javax.xml.namespace.QName) MRole(org.compiere.model.MRole) WindowTabData(pl.x3E.adInterface.WindowTabData) ArrayList(java.util.ArrayList) ModelGetList(pl.x3E.adInterface.ModelGetList) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) XFireFault(org.codehaus.xfire.fault.XFireFault) DataRow(pl.x3E.adInterface.DataRow) MRefTable(org.compiere.model.MRefTable) SQLException(java.sql.SQLException) WindowTabDataDocument(pl.x3E.adInterface.WindowTabDataDocument) X_AD_Reference(org.compiere.model.X_AD_Reference) MTable(org.compiere.model.MTable) DataField(pl.x3E.adInterface.DataField) ResultSet(java.sql.ResultSet)

Aggregations

PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 QName (javax.xml.namespace.QName)1 XFireFault (org.codehaus.xfire.fault.XFireFault)1 MColumn (org.compiere.model.MColumn)1 MRefTable (org.compiere.model.MRefTable)1 MRole (org.compiere.model.MRole)1 MTable (org.compiere.model.MTable)1 X_AD_Reference (org.compiere.model.X_AD_Reference)1 ADLoginRequest (pl.x3E.adInterface.ADLoginRequest)1 DataField (pl.x3E.adInterface.DataField)1 DataRow (pl.x3E.adInterface.DataRow)1 DataSet (pl.x3E.adInterface.DataSet)1 ModelGetList (pl.x3E.adInterface.ModelGetList)1 WindowTabData (pl.x3E.adInterface.WindowTabData)1 WindowTabDataDocument (pl.x3E.adInterface.WindowTabDataDocument)1