Search in sources :

Example 1 with IdempiereServiceFault

use of org.idempiere.webservices.fault.IdempiereServiceFault in project idempiere by idempiere.

the class ModelADServiceImpl method updateData.

public StandardResponseDocument updateData(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, "updateData", 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();
        int recordID = modelCRUD.getRecordID();
        resp.setRecordID(recordID);
        CompiereService m_cs = getCompiereService();
        MWebServiceType m_webservicetype = getWebServiceType();
        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 + "_updateData");
        // 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);
        PO po = table.getPO(recordID, trxName);
        if (po == null)
            return rollbackAndSetError(trx, resp, ret, true, "No Record " + recordID + " in " + tableName);
        POInfo poinfo = POInfo.getPOInfo(ctx, table.getAD_Table_ID());
        if (po.get_ColumnIndex("Processed") >= 0 && po.get_ValueAsBoolean("Processed")) {
            resp.setError("Record is processed and can not be updated");
            resp.setIsError(true);
            return ret;
        }
        DataRow dr = modelCRUD.getDataRow();
        StandardResponseDocument retResp = scanFields(dr.getFieldArray(), m_webservicetype, po, poinfo, trx, 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"));
        // 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) StandardResponse(org.idempiere.adInterface.x10.StandardResponse) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) Properties(java.util.Properties) DataRow(org.idempiere.adInterface.x10.DataRow) StandardResponseDocument(org.idempiere.adInterface.x10.StandardResponseDocument) POInfo(org.compiere.model.POInfo) MTable(org.compiere.model.MTable) ModelCRUD(org.idempiere.adInterface.x10.ModelCRUD) Trx(org.compiere.util.Trx) PO(org.compiere.model.PO)

Example 2 with IdempiereServiceFault

use of org.idempiere.webservices.fault.IdempiereServiceFault 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 3 with IdempiereServiceFault

use of org.idempiere.webservices.fault.IdempiereServiceFault in project idempiere by idempiere.

the class ModelADServiceImpl method deleteData.

// getList
public StandardResponseDocument deleteData(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, "deleteData", 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();
        int recordID = modelCRUD.getRecordID();
        resp.setRecordID(recordID);
        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 + "_deleteData");
        // 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);
        PO po = table.getPO(recordID, trxName);
        if (po == null)
            return rollbackAndSetError(trx, resp, ret, true, "No Record " + recordID + " in " + tableName);
        if (!po.delete(false))
            return rollbackAndSetError(trx, resp, ret, true, "Cannot delete record " + recordID + " in " + tableName + ": " + CLogger.retrieveErrorString("no log message"));
        // close the trx
        if (manageTrx && !trx.commit())
            return rollbackAndSetError(trx, resp, ret, true, "Cannot commit transaction after delete record " + recordID + " in " + tableName);
        return ret;
    } finally {
        if (manageTrx && trx != null)
            trx.close();
        getCompiereService().disconnect();
    }
}
Also used : ADLoginRequest(org.idempiere.adInterface.x10.ADLoginRequest) MTable(org.compiere.model.MTable) StandardResponse(org.idempiere.adInterface.x10.StandardResponse) ModelCRUD(org.idempiere.adInterface.x10.ModelCRUD) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) Trx(org.compiere.util.Trx) Properties(java.util.Properties) StandardResponseDocument(org.idempiere.adInterface.x10.StandardResponseDocument) PO(org.compiere.model.PO)

Example 4 with IdempiereServiceFault

use of org.idempiere.webservices.fault.IdempiereServiceFault in project idempiere by idempiere.

the class ModelADServiceImpl method setDocAction.

/*
	 * Model oriented web service to change DocAction for documents, i.e.
	 * Complete a Material Receipt WARNING!!! This web service complete
	 * documents not via workflow, so it jump over any approval step considered
	 * in document workflow To complete documents using workflow it's better to
	 * use the runProcess web service
	 */
public StandardResponseDocument setDocAction(ModelSetDocActionRequestDocument req) {
    Trx trx = null;
    try {
        getCompiereService().connect();
        StandardResponseDocument ret = StandardResponseDocument.Factory.newInstance();
        StandardResponse resp = ret.addNewStandardResponse();
        ModelSetDocAction modelSetDocAction = req.getModelSetDocActionRequest().getModelSetDocAction();
        String serviceType = modelSetDocAction.getServiceType();
        ADLoginRequest reqlogin = req.getModelSetDocActionRequest().getADLoginRequest();
        CompiereService m_cs = getCompiereService();
        String err = login(reqlogin, webServiceName, "setDocAction", serviceType);
        if (err != null && err.length() > 0) {
            resp.setError(err);
            resp.setIsError(true);
            return ret;
        }
        try {
            // Validate parameters
            modelSetDocAction.setTableName(validateParameter("tableName", modelSetDocAction.getTableName()));
            modelSetDocAction.setRecordID(validateParameter("recordID", modelSetDocAction.getRecordID()));
            modelSetDocAction.setDocAction(validateParameter("docAction", modelSetDocAction.getDocAction()));
        } catch (IdempiereServiceFault e) {
            resp.setError(e.getMessage());
            resp.setIsError(true);
            return ret;
        }
        Properties ctx = m_cs.getCtx();
        // start a trx
        String trxName = localTrxName;
        if (trxName == null) {
            trxName = Trx.createTrxName("ws_modelSetDocAction");
            manageTrx = true;
        }
        trx = Trx.get(trxName, true);
        if (manageTrx)
            trx.setDisplayName(getClass().getName() + "_" + webServiceName + "_setDocAction");
        Map<String, Object> requestCtx = getRequestCtx();
        String tableName = modelSetDocAction.getTableName();
        String recordIDVar = modelSetDocAction.getRecordIDVariable();
        int recordID = modelSetDocAction.getRecordID();
        if (recordIDVar != null && recordIDVar.startsWith("@")) {
            Integer retVal = (Integer) parseVariable(recordIDVar, null, null, requestCtx);
            if (retVal == null) {
                return rollbackAndSetError(trx, resp, ret, true, "Cannot resolve variable: " + recordIDVar);
            }
            recordID = retVal;
        }
        String docAction = modelSetDocAction.getDocAction();
        resp.setRecordID(recordID);
        // 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);
        PO po = table.getPO(recordID, trxName);
        if (po == null)
            return rollbackAndSetError(trx, resp, ret, true, "No Record " + recordID + " in " + tableName);
        // set explicitly the column DocAction to avoid automatic process of
        // default option
        po.set_ValueOfColumn("DocAction", docAction);
        if (!po.save())
            return rollbackAndSetError(trx, resp, ret, true, "Cannot save before set docAction: " + CLogger.retrieveErrorString("no log message"));
        MWebServiceType m_webservicetype = getWebServiceType();
        // For passing action to validators
        requestCtx.put("DocAction", docAction);
        // Fire Event
        StandardResponseDocument retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_BEFORE_PARSE, po, null, trx, requestCtx, resp, ret);
        if (retResp != null)
            return retResp;
        // call process it
        try {
            if (!((org.compiere.process.DocAction) po).processIt(docAction))
                return rollbackAndSetError(trx, resp, ret, true, Msg.parseTranslation(ctx, "@FailedProcessingDocument@: " + ((org.compiere.process.DocAction) po).getProcessMsg()));
        } catch (Exception e) {
            return rollbackAndSetError(trx, resp, ret, true, e.toString());
        }
        // Fire Event
        retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_BEFORE_SAVE, po, null, trx, requestCtx, resp, ret);
        if (retResp != null)
            return retResp;
        // close the trx
        if (!po.save())
            return rollbackAndSetError(trx, resp, ret, true, "Cannot save after set docAction: " + CLogger.retrieveErrorString("no log message"));
        // Fire Event
        retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_AFTER_SAVE, po, null, trx, requestCtx, resp, ret);
        if (retResp != null)
            return retResp;
        if (manageTrx && !trx.commit())
            return rollbackAndSetError(trx, resp, ret, true, "Cannot commit after docAction");
        // resp.setError("");
        resp.setIsError(false);
        POInfo poinfo = POInfo.getPOInfo(ctx, table.getAD_Table_ID());
        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) ModelSetDocAction(org.idempiere.adInterface.x10.ModelSetDocAction) StandardResponse(org.idempiere.adInterface.x10.StandardResponse) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) Properties(java.util.Properties) ModelSetDocAction(org.idempiere.adInterface.x10.ModelSetDocAction) SQLException(java.sql.SQLException) XmlValueOutOfRangeException(org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException) StandardResponseDocument(org.idempiere.adInterface.x10.StandardResponseDocument) POInfo(org.compiere.model.POInfo) MTable(org.compiere.model.MTable) Trx(org.compiere.util.Trx) PO(org.compiere.model.PO)

Example 5 with IdempiereServiceFault

use of org.idempiere.webservices.fault.IdempiereServiceFault in project idempiere by idempiere.

the class ModelADServiceImpl method parseVariable.

public static Object parseVariable(CompiereService cs, Map<String, Object> requestCtx, String name, String strValue) {
    String varName = strValue.substring(1);
    if (varName.charAt(0) == '#') {
        return cs.getCtx().getProperty(varName);
    } else {
        int indDot = varName.indexOf(".");
        if (indDot == -1) {
            // premitive data type
            return requestCtx.get(varName);
        } else {
            String tblName = varName.substring(0, indDot);
            String colName = varName.substring(indDot + 1);
            if (colName.indexOf(".") >= 0) {
                throw new IdempiereServiceFault(strValue + " contains un supported multi level object resolution", new QName("resolveCtxVariable"));
            }
            Object obj = requestCtx.get(tblName);
            if (obj == null || !(obj instanceof PO)) {
                throw new IdempiereServiceFault(" input column " + name + " can not found object of " + tblName + ". Request variable " + strValue + " can not resolved", new QName("resolveCtxVariable"));
            }
            PO refPO = (PO) obj;
            return refPO.get_Value(colName);
        }
    }
}
Also used : IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) QName(javax.xml.namespace.QName) PO(org.compiere.model.PO)

Aggregations

IdempiereServiceFault (org.idempiere.webservices.fault.IdempiereServiceFault)14 PO (org.compiere.model.PO)9 Properties (java.util.Properties)8 QName (javax.xml.namespace.QName)8 MTable (org.compiere.model.MTable)8 ADLoginRequest (org.idempiere.adInterface.x10.ADLoginRequest)8 MWebServiceType (org.idempiere.webservices.model.MWebServiceType)8 POInfo (org.compiere.model.POInfo)6 Trx (org.compiere.util.Trx)6 DataField (org.idempiere.adInterface.x10.DataField)6 DataRow (org.idempiere.adInterface.x10.DataRow)6 ModelCRUD (org.idempiere.adInterface.x10.ModelCRUD)6 StandardResponse (org.idempiere.adInterface.x10.StandardResponse)6 StandardResponseDocument (org.idempiere.adInterface.x10.StandardResponseDocument)6 SQLException (java.sql.SQLException)5 XmlValueOutOfRangeException (org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException)5 ArrayList (java.util.ArrayList)3 DataSet (org.idempiere.adInterface.x10.DataSet)3 WindowTabData (org.idempiere.adInterface.x10.WindowTabData)3 WindowTabDataDocument (org.idempiere.adInterface.x10.WindowTabDataDocument)3