Search in sources :

Example 1 with X_WS_WebServiceFieldInput

use of org.idempiere.webservices.model.X_WS_WebServiceFieldInput in project idempiere by idempiere.

the class ModelADServiceImpl method queryData.

public WindowTabDataDocument queryData(ModelCRUDRequestDocument req) {
    Trx trx = null;
    try {
        getCompiereService().connect();
        CompiereService m_cs = getCompiereService();
        WindowTabDataDocument ret = WindowTabDataDocument.Factory.newInstance();
        WindowTabData resp = ret.addNewWindowTabData();
        ModelCRUD modelCRUD = req.getModelCRUDRequest().getModelCRUD();
        String serviceType = modelCRUD.getServiceType();
        ADLoginRequest reqlogin = req.getModelCRUDRequest().getADLoginRequest();
        String err = login(reqlogin, webServiceName, "queryData", serviceType);
        if (err != null && err.length() > 0) {
            resp.setError(err);
            return ret;
        }
        // Validate parameters vs service type
        validateCRUD(modelCRUD);
        Properties ctx = m_cs.getCtx();
        String tableName = modelCRUD.getTableName();
        Map<String, Object> reqCtx = getRequestCtx();
        MWebServiceType m_webservicetype = getWebServiceType();
        // get the PO for the tablename and record ID
        MTable table = MTable.get(ctx, tableName);
        if (table == null)
            throw new IdempiereServiceFault("Web service type " + m_webservicetype.getValue() + ": table " + tableName + " not found", new QName("queryData"));
        int roleid = reqlogin.getRoleID();
        MRole role = MRole.get(ctx, roleid);
        // start a trx
        String trxName = localTrxName;
        if (trxName == null) {
            trxName = Trx.createTrxName("ws_modelQueryData");
            manageTrx = true;
        }
        trx = Trx.get(trxName, true);
        if (manageTrx)
            trx.setDisplayName(getClass().getName() + "_" + webServiceName + "_queryData");
        StringBuilder sqlBuilder = new StringBuilder(role.addAccessSQL("SELECT * FROM " + tableName, tableName, true, MRole.SQL_RO));
        ArrayList<Object> sqlParaList = new ArrayList<Object>();
        PO holderPo = table.getPO(0, trxName);
        POInfo poinfo = POInfo.getPOInfo(ctx, table.getAD_Table_ID());
        if (modelCRUD.getDataRow() != null) {
            DataRow dr = modelCRUD.getDataRow();
            DataField[] fields = dr.getFieldArray();
            StandardResponseDocument stdRet = StandardResponseDocument.Factory.newInstance();
            StandardResponse stdResp = stdRet.addNewStandardResponse();
            StandardResponseDocument retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_BEFORE_PARSE, holderPo, fields, trx, reqCtx, stdResp, stdRet);
            if (retResp != null) {
                throw new IdempiereServiceFault(retResp.getStandardResponse().getError(), new QName("queryData"));
            }
            retResp = scanFields(fields, m_webservicetype, holderPo, poinfo, trx, stdResp, stdRet);
            if (retResp != null) {
                throw new IdempiereServiceFault(retResp.getStandardResponse().getError(), new QName("queryData"));
            }
            for (DataField field : modelCRUD.getDataRow().getFieldArray()) {
                if (m_webservicetype.isInputColumnNameAllowed(field.getColumn())) {
                    // Jan Thielemann Solution for query using the sentence like
                    X_WS_WebServiceFieldInput inputField = m_webservicetype.getFieldInput(field.getColumn());
                    I_AD_Column col = inputField.getAD_Column();
                    String sqlType = DisplayType.getSQLDataType(col.getAD_Reference_ID(), col.getColumnName(), col.getFieldLength());
                    if (sqlType.contains("CHAR"))
                        sqlBuilder.append(" AND ").append(field.getColumn()).append(" LIKE ?");
                    else
                        sqlBuilder.append(" AND ").append(field.getColumn()).append("=?");
                    sqlParaList.add(holderPo.get_Value(field.getColumn()));
                // End Jan Thielemann Solution for query using the sentence like
                } else if (m_webservicetype.getFieldInput(field.getColumn()) == null) {
                    // If not even ctx variable column
                    throw new IdempiereServiceFault("Web service type " + m_webservicetype.getValue() + ": input column " + field.getColumn() + " not allowed", new QName("queryData"));
                }
            }
        }
        if (modelCRUD.getFilter() != null && modelCRUD.getFilter().length() > 0) {
            String sql = parseSQL(" WHERE " + modelCRUD.getFilter(), sqlParaList, holderPo, poinfo, reqCtx);
            sqlBuilder.append(" AND ").append(sql.substring(6));
        }
        int cnt = 0;
        int rowCnt = 0;
        int offset = modelCRUD.getOffset();
        int limit = modelCRUD.getLimit();
        PreparedStatement pstmtquery = null;
        ResultSet rsquery = null;
        try {
            pstmtquery = DB.prepareStatement(sqlBuilder.toString(), trxName);
            DB.setParameters(pstmtquery, sqlParaList);
            rsquery = pstmtquery.executeQuery();
            // Angelo Dabala' (genied) must create just one DataSet, moved outside of the while loop
            DataSet ds = resp.addNewDataSet();
            while (rsquery.next()) {
                cnt++;
                if ((offset >= cnt) || (limit > 0 && offset + limit < cnt))
                    continue;
                rowCnt++;
                DataRow dr = ds.addNewDataRow();
                for (int i = 0; i < poinfo.getColumnCount(); i++) {
                    String columnName = poinfo.getColumnName(i);
                    if (m_webservicetype.isOutputColumnNameAllowed(columnName)) {
                        DataField dfid = dr.addNewField();
                        dfid.setColumn(columnName);
                        if (rsquery.getObject(columnName) instanceof byte[])
                            dfid.setVal(new String(Base64.encodeBase64(rsquery.getBytes(columnName))));
                        else
                            dfid.setVal(rsquery.getString(columnName));
                    }
                }
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, e.getLocalizedMessage(), e);
            throw new IdempiereServiceFault(e);
        } finally {
            DB.close(rsquery, pstmtquery);
            rsquery = null;
            pstmtquery = null;
        }
        resp.setSuccess(true);
        resp.setRowCount(rowCnt);
        resp.setNumRows(rowCnt);
        resp.setTotalRows(cnt);
        resp.setStartRow(offset);
        return ret;
    } finally {
        if (manageTrx && trx != null)
            trx.close();
        getCompiereService().disconnect();
    }
}
Also used : ADLoginRequest(org.idempiere.adInterface.x10.ADLoginRequest) MWebServiceType(org.idempiere.webservices.model.MWebServiceType) DataSet(org.idempiere.adInterface.x10.DataSet) MRole(org.compiere.model.MRole) ArrayList(java.util.ArrayList) Properties(java.util.Properties) DataRow(org.idempiere.adInterface.x10.DataRow) StandardResponseDocument(org.idempiere.adInterface.x10.StandardResponseDocument) WindowTabDataDocument(org.idempiere.adInterface.x10.WindowTabDataDocument) ModelCRUD(org.idempiere.adInterface.x10.ModelCRUD) ResultSet(java.sql.ResultSet) Trx(org.compiere.util.Trx) X_WS_WebServiceFieldInput(org.idempiere.webservices.model.X_WS_WebServiceFieldInput) StandardResponse(org.idempiere.adInterface.x10.StandardResponse) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) QName(javax.xml.namespace.QName) I_AD_Column(org.compiere.model.I_AD_Column) WindowTabData(org.idempiere.adInterface.x10.WindowTabData) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) XmlValueOutOfRangeException(org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException) POInfo(org.compiere.model.POInfo) MTable(org.compiere.model.MTable) DataField(org.idempiere.adInterface.x10.DataField) PO(org.compiere.model.PO)

Example 2 with X_WS_WebServiceFieldInput

use of org.idempiere.webservices.model.X_WS_WebServiceFieldInput in project idempiere by idempiere.

the class ModelADServiceImpl method scanFields.

public StandardResponseDocument scanFields(DataField[] fields, MWebServiceType m_webservicetype, PO po, POInfo poinfo, Trx trx, StandardResponse resp, StandardResponseDocument ret) {
    Map<String, Object> requestCtx = getRequestCtx();
    // Clear ctx
    Env.clearWinContext(Env.getCtx(), 0);
    for (DataField field : fields) {
        // Implement lookup
        X_WS_WebServiceFieldInput fieldInput = m_webservicetype.getFieldInput(field.getColumn());
        if (fieldInput != null) {
            // Is ctx variable
            if (fieldInput.getAD_Column_ID() == 0 && fieldInput.getColumnName() != null) {
                String varName = fieldInput.getColumnName();
                Class<?> columnClass = getVariableType(varName, fieldInput.getAD_Reference_ID());
                Object objVal = null;
                String val = field.getVal();
                if (val != null && val.length() > 0 && val.charAt(0) == '@')
                    objVal = parseVariable(field.getVal(), po, poinfo, requestCtx);
                else
                    objVal = convertToObj(field.getVal(), columnClass, varName);
                requestCtx.put(varName, objVal);
            } else {
                int idxcol = po.get_ColumnIndex(field.getColumn());
                if (idxcol < 0) {
                    // defined in security
                    return rollbackAndSetError(trx, resp, ret, true, "Web service type " + m_webservicetype.getValue() + ": input column " + field.getColumn() + " does not exist");
                } else {
                    try {
                        setValueAccordingToClass(po, poinfo, field, idxcol, fieldInput);
                    } catch (IdempiereServiceFault e) {
                        log.log(Level.WARNING, "Error setting value", e);
                        return rollbackAndSetError(trx, resp, ret, true, "Web service type " + m_webservicetype.getValue() + ": input column " + field.getColumn() + " value could not be set: " + e.getLocalizedMessage());
                    }
                }
            }
        } else {
            return rollbackAndSetError(trx, resp, ret, true, "Web service type " + m_webservicetype.getValue() + ": input column " + field.getColumn() + " not allowed");
        }
    }
    return null;
}
Also used : X_WS_WebServiceFieldInput(org.idempiere.webservices.model.X_WS_WebServiceFieldInput) DataField(org.idempiere.adInterface.x10.DataField) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault)

Example 3 with X_WS_WebServiceFieldInput

use of org.idempiere.webservices.model.X_WS_WebServiceFieldInput in project idempiere by idempiere.

the class ModelADServiceImpl method createUpdateData.

// createData
public StandardResponseDocument createUpdateData(ModelCRUDRequestDocument req) {
    Trx trx = null;
    try {
        getCompiereService().connect();
        StandardResponseDocument ret = StandardResponseDocument.Factory.newInstance();
        StandardResponse resp = ret.addNewStandardResponse();
        ModelCRUD modelCRUD = req.getModelCRUDRequest().getModelCRUD();
        String serviceType = modelCRUD.getServiceType();
        ADLoginRequest reqlogin = req.getModelCRUDRequest().getADLoginRequest();
        String err = login(reqlogin, webServiceName, "createData", serviceType);
        if (err != null && err.length() > 0) {
            resp.setError(err);
            resp.setIsError(true);
            return ret;
        }
        // Validate parameters vs service type
        try {
            validateCRUD(modelCRUD);
        } catch (IdempiereServiceFault e) {
            resp.setError(e.getMessage());
            resp.setIsError(true);
            return ret;
        }
        String tableName = modelCRUD.getTableName();
        CompiereService m_cs = getCompiereService();
        Properties ctx = m_cs.getCtx();
        // start a trx
        String trxName = localTrxName;
        if (trxName == null) {
            trxName = Trx.createTrxName("ws_modelCreateData");
            manageTrx = true;
        }
        trx = Trx.get(trxName, true);
        if (manageTrx)
            trx.setDisplayName(getClass().getName() + "_" + webServiceName + "_createUpdateData");
        // get the PO for the tablename and record ID
        MTable table = MTable.get(ctx, tableName);
        if (table == null)
            return rollbackAndSetError(trx, resp, ret, true, "No table " + tableName);
        DataRow dr = modelCRUD.getDataRow();
        DataField[] fields = dr.getFieldArray();
        PO holderPo = table.getPO(0, trxName);
        POInfo poinfo = POInfo.getPOInfo(ctx, table.getAD_Table_ID());
        MWebServiceType m_webservicetype = getWebServiceType();
        Map<String, Object> requestCtx = getRequestCtx();
        StandardResponseDocument retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_BEFORE_PARSE, holderPo, fields, trx, requestCtx, resp, ret);
        if (retResp != null)
            return retResp;
        retResp = scanFields(fields, m_webservicetype, holderPo, poinfo, trx, resp, ret);
        if (retResp != null)
            return retResp;
        retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_AFTER_PARSE, holderPo, fields, trx, requestCtx, resp, ret);
        if (retResp != null)
            return retResp;
        boolean isCreate = false;
        boolean isUpdate = false;
        String action = modelCRUD.getAction().toString();
        if (action.equals("Create"))
            isCreate = true;
        if (action.equals("Update"))
            isUpdate = true;
        if (action.equals("CreateUpdate")) {
            isCreate = true;
            isUpdate = true;
        }
        ArrayList<String> identifierList = m_webservicetype.getKeyColumns();
        // For update it is mandatory to pass key column
        if (isUpdate && identifierList.size() == 0) {
            return rollbackAndSetError(trx, resp, ret, true, "Web service type " + m_webservicetype.getValue() + ": There is no key column found ");
        }
        // Check for existing element
        int record_id = 0;
        ArrayList<Object> resovedValue = new ArrayList<Object>();
        if (identifierList.size() > 0) {
            StringBuilder sqlBuilder = new StringBuilder("Select ");
            sqlBuilder.append(table.getTableName()).append("_ID from ").append(table.getTableName()).append(" ot Where ");
            ArrayList<Object> sqlParaList = new ArrayList<Object>();
            for (String colName : identifierList) {
                X_WS_WebServiceFieldInput fieldInput = m_webservicetype.getFieldInput(colName);
                if (fieldInput.getIdentifierLogic() == null) {
                    if (holderPo.get_Value(colName) == null && fieldInput.isNullIdentifier()) {
                        sqlBuilder.append(" ot.").append(colName).append(" Is Null AND ");
                    } else if (holderPo.get_Value(colName) == null) {
                        return rollbackAndSetError(trx, resp, ret, true, "Web service type " + m_webservicetype.getValue() + ": Record Identifier column " + colName + " must be set");
                    } else {
                        sqlBuilder.append(" ot.").append(colName).append("=? AND ");
                        sqlParaList.add(holderPo.get_Value(colName));
                        resovedValue.add(holderPo.get_Value(colName));
                    }
                } else {
                    // SQL
                    String sql = parseSQL(fieldInput.getIdentifierLogic(), sqlParaList, holderPo, poinfo, requestCtx);
                    sqlBuilder.append(" ot.").append(colName).append(" = (").append(sql).append(") AND ");
                    resovedValue.add("DYN SQL");
                }
            }
            sqlBuilder.append(" ot.AD_Client_ID= ?");
            sqlParaList.add(Env.getAD_Client_ID(Env.getCtx()));
            String sql = sqlBuilder.toString();
            if (log.isLoggable(Level.INFO))
                log.info("Web service type " + m_webservicetype.getValue() + "SQL to check existing record " + sql);
            try {
                record_id = DB.getSQLValueEx(trxName, sql, sqlParaList);
            } catch (Exception e) {
                log.log(Level.SEVERE, "ExistingRecordCheck: Exception while executing SQL :" + sql);
                return rollbackAndSetError(trx, resp, ret, true, "Web service type " + m_webservicetype.getValue() + " Exception while executing sql :" + sql);
            }
        }
        if (record_id == -1)
            record_id = 0;
        if (!isCreate && record_id == 0) {
            resp.setError("No Record to update for " + table.getTableName() + " with (" + identifierList.toString() + ") = (" + resovedValue.toString() + ")");
            resp.setIsError(true);
            return ret;
        }
        if (record_id > 0 && !isUpdate) {
            resp.setError("Record already presents with " + table.getTableName() + "_ID = " + record_id);
            resp.setIsError(true);
            return ret;
        }
        PO po = table.getPO(record_id, trxName);
        if (po == null)
            return rollbackAndSetError(trx, resp, ret, true, "Cannot create PO for " + tableName);
        if (po.get_ColumnIndex("Processed") >= 0 && po.get_ValueAsBoolean("Processed")) {
            resp.setError("Record not updatable for " + table.getTableName() + "_ID = " + record_id);
            resp.setIsError(true);
            return ret;
        }
        // Setting value back from holder to new persistent po
        for (DataField field : fields) {
            int indx = poinfo.getColumnIndex(field.getColumn());
            if (indx != -1) {
                po.set_ValueNoCheck(field.getColumn(), holderPo.get_Value(field.getColumn()));
            }
        }
        retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_BEFORE_SAVE, holderPo, fields, trx, requestCtx, resp, ret);
        if (retResp != null)
            return retResp;
        if (!po.validForeignKeys())
            return rollbackAndSetError(trx, resp, ret, true, "Cannot save record in " + tableName + ": " + CLogger.retrieveErrorString("no log message"));
        if (!po.save())
            return rollbackAndSetError(trx, resp, ret, true, "Cannot save record in " + tableName + ": " + CLogger.retrieveErrorString("no log message"));
        retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_AFTER_SAVE, holderPo, fields, trx, requestCtx, resp, ret);
        if (retResp != null)
            return retResp;
        int recordID = po.get_ID();
        resp.setRecordID(recordID);
        // Update ctx variable for consecutive calls
        if (requestCtx != null) {
            requestCtx.put(po.get_TableName(), po);
        }
        // close the trx
        if (manageTrx && !trx.commit())
            return rollbackAndSetError(trx, resp, ret, true, "Cannot commit transaction after create record " + recordID + " in " + tableName);
        setOuputFields(resp, m_webservicetype, po, poinfo);
        return ret;
    } finally {
        if (manageTrx && trx != null)
            trx.close();
        getCompiereService().disconnect();
    }
}
Also used : ADLoginRequest(org.idempiere.adInterface.x10.ADLoginRequest) MWebServiceType(org.idempiere.webservices.model.MWebServiceType) ArrayList(java.util.ArrayList) Properties(java.util.Properties) DataRow(org.idempiere.adInterface.x10.DataRow) StandardResponseDocument(org.idempiere.adInterface.x10.StandardResponseDocument) ModelCRUD(org.idempiere.adInterface.x10.ModelCRUD) Trx(org.compiere.util.Trx) X_WS_WebServiceFieldInput(org.idempiere.webservices.model.X_WS_WebServiceFieldInput) StandardResponse(org.idempiere.adInterface.x10.StandardResponse) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) SQLException(java.sql.SQLException) XmlValueOutOfRangeException(org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException) POInfo(org.compiere.model.POInfo) MTable(org.compiere.model.MTable) DataField(org.idempiere.adInterface.x10.DataField) PO(org.compiere.model.PO)

Aggregations

DataField (org.idempiere.adInterface.x10.DataField)3 IdempiereServiceFault (org.idempiere.webservices.fault.IdempiereServiceFault)3 X_WS_WebServiceFieldInput (org.idempiere.webservices.model.X_WS_WebServiceFieldInput)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 XmlValueOutOfRangeException (org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException)2 MTable (org.compiere.model.MTable)2 PO (org.compiere.model.PO)2 POInfo (org.compiere.model.POInfo)2 Trx (org.compiere.util.Trx)2 ADLoginRequest (org.idempiere.adInterface.x10.ADLoginRequest)2 DataRow (org.idempiere.adInterface.x10.DataRow)2 ModelCRUD (org.idempiere.adInterface.x10.ModelCRUD)2 StandardResponse (org.idempiere.adInterface.x10.StandardResponse)2 StandardResponseDocument (org.idempiere.adInterface.x10.StandardResponseDocument)2 MWebServiceType (org.idempiere.webservices.model.MWebServiceType)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 QName (javax.xml.namespace.QName)1