Search in sources :

Example 6 with ModelCRUD

use of org.idempiere.adInterface.x10.ModelCRUD in project idempiere by idempiere.

the class CompositeServiceImpl method rollbackAndSetError.

/**
 * @param ctx
 * @param crud
 */
/*
	public void resolveContextCRUD(Properties ctx, ModelCRUD crud) {
		DataField fields[] = crud.getDataRow().getFieldArray();
		for (DataField field : fields) {
			String val = field.getVal();
			if (val != null && val.startsWith("#")) {
				// Replace value from context variables
				String key = val.substring(1);
				val = (String) ctx.get(key);
				field.setVal(val);
			}
		}
	}*/
/**
 * Rollback and set error on response
 * @param trx
 * @param compResp
 * @param respAggregator
 * @param string
 */
protected void rollbackAndSetError(Trx trx, CompositeResponse compResp, ArrayList<StandardResponse> respAggregator, String string) {
    if (respAggregator == null) {
        StandardResponse resp = compResp.addNewStandardResponse();
        resp.setError(string);
        resp.setIsError(true);
    } else {
        int ind = 0;
        StandardResponse[] orgArr = compResp.getStandardResponseArray();
        StandardResponse[] respArr = new StandardResponse[orgArr.length + respAggregator.size()];
        for (StandardResponse resp : orgArr) {
            respArr[ind++] = resp;
        }
        // Set all response as Failed
        for (StandardResponse resp : respAggregator) {
            resp.setIsRolledBack(true);
            respArr[ind++] = resp;
        }
        if (string != null && respAggregator.size() > 0) {
            StandardResponse resp = respAggregator.get(respAggregator.size() - 1);
            resp.setError(string);
            resp.setIsError(true);
        }
        compResp.setStandardResponseArray(respArr);
        respAggregator.clear();
    }
    trx.rollback();
    trx.close();
}
Also used : StandardResponse(org.idempiere.adInterface.x10.StandardResponse)

Example 7 with ModelCRUD

use of org.idempiere.adInterface.x10.ModelCRUD in project idempiere by idempiere.

the class ModelADServiceImpl method readData.

// updateData
public WindowTabDataDocument readData(ModelCRUDRequestDocument req) {
    try {
        getCompiereService().connect();
        WindowTabDataDocument ret = WindowTabDataDocument.Factory.newInstance();
        WindowTabData resp = ret.addNewWindowTabData();
        ModelCRUD modelCRUD = req.getModelCRUDRequest().getModelCRUD();
        String serviceType = modelCRUD.getServiceType();
        int cnt = 0;
        ADLoginRequest reqlogin = req.getModelCRUDRequest().getADLoginRequest();
        String err = login(reqlogin, webServiceName, "readData", serviceType);
        if (err != null && err.length() > 0) {
            resp.setError(err);
            return ret;
        }
        // Validate parameters vs service type
        try {
            validateCRUD(modelCRUD);
        } catch (IdempiereServiceFault e) {
            resp.setError(e.getMessage());
            return ret;
        }
        CompiereService m_cs = getCompiereService();
        MWebServiceType m_webservicetype = getWebServiceType();
        // start a trx
        String trxName = localTrxName;
        Properties ctx = m_cs.getCtx();
        String tableName = modelCRUD.getTableName();
        String recordIDVar = modelCRUD.getRecordIDVariable();
        int recordID = modelCRUD.getRecordID();
        if (recordIDVar != null && recordIDVar.startsWith("@")) {
            Integer retVal = (Integer) parseVariable(recordIDVar, null, null, getRequestCtx());
            if (retVal == null) {
                resp.setError("Cannot resolve variable: " + recordIDVar);
                return ret;
            }
            recordID = retVal;
        }
        // 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("readData"));
        PO po = table.getPO(recordID, trxName);
        if (po == null) {
            resp.setSuccess(false);
            resp.setRowCount(cnt);
            resp.setNumRows(cnt);
            resp.setTotalRows(cnt);
            resp.setStartRow(0);
            return ret;
        }
        cnt = 1;
        POInfo poinfo = POInfo.getPOInfo(ctx, table.getAD_Table_ID());
        DataSet ds = resp.addNewDataSet();
        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 (po.get_Value(i) != null) {
                    if (po.get_Value(i) instanceof byte[]) {
                        dfid.setVal(new String(Base64.encodeBase64((byte[]) po.get_Value(i))));
                    } else if (po.get_Value(i) instanceof Boolean) {
                        dfid.setVal((Boolean) po.get_Value(i) ? "Y" : "N");
                    } else {
                        dfid.setVal(po.get_Value(i).toString());
                    }
                } else
                    dfid.setVal(null);
            }
        }
        resp.setSuccess(true);
        resp.setRowCount(cnt);
        resp.setNumRows(cnt);
        resp.setTotalRows(cnt);
        resp.setStartRow(1);
        return ret;
    } finally {
        getCompiereService().disconnect();
    }
}
Also used : ADLoginRequest(org.idempiere.adInterface.x10.ADLoginRequest) MWebServiceType(org.idempiere.webservices.model.MWebServiceType) DataSet(org.idempiere.adInterface.x10.DataSet) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) QName(javax.xml.namespace.QName) WindowTabData(org.idempiere.adInterface.x10.WindowTabData) Properties(java.util.Properties) DataRow(org.idempiere.adInterface.x10.DataRow) POInfo(org.compiere.model.POInfo) WindowTabDataDocument(org.idempiere.adInterface.x10.WindowTabDataDocument) MTable(org.compiere.model.MTable) DataField(org.idempiere.adInterface.x10.DataField) ModelCRUD(org.idempiere.adInterface.x10.ModelCRUD) PO(org.compiere.model.PO)

Example 8 with ModelCRUD

use of org.idempiere.adInterface.x10.ModelCRUD in project idempiere by idempiere.

the class ModelADServiceImpl method createData.

public StandardResponseDocument createData(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 + "_createData");
        // 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(0, trxName);
        if (po == null)
            return rollbackAndSetError(trx, resp, ret, true, "Cannot create PO for " + tableName);
        POInfo poinfo = POInfo.getPOInfo(ctx, table.getAD_Table_ID());
        DataRow dr = modelCRUD.getDataRow();
        MWebServiceType m_webservicetype = getWebServiceType();
        Map<String, Object> requestCtx = getRequestCtx();
        DataField[] fields = dr.getFieldArray();
        StandardResponseDocument retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_BEFORE_PARSE, po, fields, trx, requestCtx, resp, ret);
        if (retResp != null)
            return retResp;
        retResp = scanFields(fields, m_webservicetype, po, poinfo, trx, resp, ret);
        if (retResp != null)
            return retResp;
        retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_AFTER_PARSE, po, fields, trx, requestCtx, resp, ret);
        if (retResp != null)
            return retResp;
        retResp = invokeWSValidator(m_webservicetype, IWSValidator.TIMING_BEFORE_SAVE, po, 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, po, 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) 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) DataField(org.idempiere.adInterface.x10.DataField) ModelCRUD(org.idempiere.adInterface.x10.ModelCRUD) Trx(org.compiere.util.Trx) PO(org.compiere.model.PO)

Example 9 with ModelCRUD

use of org.idempiere.adInterface.x10.ModelCRUD 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

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