use of org.idempiere.webservices.model.MWebServiceType 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();
}
}
use of org.idempiere.webservices.model.MWebServiceType 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();
}
}
use of org.idempiere.webservices.model.MWebServiceType 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();
}
}
use of org.idempiere.webservices.model.MWebServiceType in project idempiere by idempiere.
the class AbstractService method authenticate.
/**
* Authenticate user for requested service type
* @param webServiceValue
* @param methodValue
* @param serviceTypeValue
* @param m_cs
* @return
*/
protected String authenticate(String webServiceValue, String methodValue, String serviceTypeValue, CompiereService m_cs) {
MWebService m_webservice = MWebService.get(m_cs.getCtx(), webServiceValue);
if (m_webservice == null || !m_webservice.isActive())
return "Web Service " + webServiceValue + " not registered";
X_WS_WebServiceMethod m_webservicemethod = m_webservice.getMethod(methodValue);
if (m_webservicemethod == null || !m_webservicemethod.isActive())
return "Method " + methodValue + " not registered";
MWebServiceType m_webservicetype = null;
String key = m_cs.getAD_Client_ID() + "|" + m_webservice.getWS_WebService_ID() + "|" + m_webservicemethod.getWS_WebServiceMethod_ID() + "|" + serviceTypeValue;
synchronized (s_WebServiceTypeCache) {
m_webservicetype = s_WebServiceTypeCache.get(m_cs.getCtx(), key, e -> new MWebServiceType(m_cs.getCtx(), e));
if (m_webservicetype == null) {
m_webservicetype = new Query(m_cs.getCtx(), MWebServiceType.Table_Name, "AD_Client_ID IN (0,?) AND WS_WebService_ID=? AND WS_WebServiceMethod_ID=? AND Value=?", null).setOnlyActiveRecords(true).setParameters(m_cs.getAD_Client_ID(), m_webservice.getWS_WebService_ID(), m_webservicemethod.getWS_WebServiceMethod_ID(), serviceTypeValue).setOrderBy(// IDEMPIERE-3394 give precedence to tenant defined if there are system+tenant
"AD_Client_ID DESC").first();
if (m_webservicetype != null) {
s_WebServiceTypeCache.put(key, m_webservicetype, e -> new MWebServiceType(Env.getCtx(), e));
}
}
}
if (m_webservicetype == null)
return "Service type " + serviceTypeValue + " not configured";
getHttpServletRequest().setAttribute("MWebServiceType", m_webservicetype);
int AD_Role_ID = Env.getAD_Role_ID(m_cs.getCtx());
key = AD_Role_ID + "|" + m_webservicetype.get_ID();
synchronized (s_RoleAccessCache) {
Boolean bAccess = s_RoleAccessCache.get(key);
if (bAccess == null) {
// Check if role has access on web-service
String hasAccess = DB.getSQLValueStringEx(null, ROLE_ACCESS_SQL, AD_Role_ID, AD_Role_ID, m_webservicetype.get_ID());
bAccess = "Y".equals(hasAccess);
s_RoleAccessCache.put(key, bAccess);
}
if (!bAccess.booleanValue()) {
return "Web Service Error: Login role does not have access to the service type";
}
}
String ret = invokeLoginValidator(null, m_cs.getCtx(), m_webservicetype, IWSValidator.TIMING_ON_AUTHORIZATION);
if (ret != null && ret.length() > 0)
return ret;
m_cs.refreshLastAuthorizationTime();
return null;
}
use of org.idempiere.webservices.model.MWebServiceType in project idempiere by idempiere.
the class ModelADServiceImpl method validateParameter.
private String validateParameter(String parameterName, String string) {
MWebServiceType m_webservicetype = getWebServiceType();
X_WS_WebService_Para para = m_webservicetype.getParameter(parameterName);
if (para == null && (string == null || string.length() == 0))
// if parameter not configured but didn't receive value (optional param)
return null;
if (para == null)
throw new IdempiereServiceFault("Web service type " + m_webservicetype.getValue() + ": invalid parameter " + parameterName, new QName("validateParameter"));
if (X_WS_WebService_Para.PARAMETERTYPE_Constant.equals(para.getParameterType())) {
if (string == null || string.length() == 0) {
if (log.isLoggable(Level.INFO))
log.log(Level.INFO, "Web service type " + m_webservicetype.getValue() + ": constant parameter " + parameterName + " set to " + para.getConstantValue());
return para.getConstantValue();
} else if (!para.getConstantValue().equals(string)) {
log.log(Level.WARNING, "Web service type " + m_webservicetype.getValue() + ": constant parameter " + parameterName + " changed to " + para.getConstantValue());
return para.getConstantValue();
}
}
// it must be parameter FREE
return string;
}
Aggregations