Search in sources :

Example 6 with DataRow

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

use of org.idempiere.adInterface.x10.DataRow 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 8 with DataRow

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

the class Process method fillParameter.

// createProcessPage
private static MPInstance fillParameter(CompiereService m_cs, DataRow dr, MProcess process, Map<String, Object> requestCtx) throws Exception {
    MPInstance pInstance = new MPInstance(Env.getCtx(), process.getAD_Process_ID(), 0);
    pInstance.saveEx();
    DataField[] f = dr.getFieldArray();
    HashMap<String, DataField> fmap = new HashMap<String, DataField>();
    for (int i = 0; i < f.length; i++) fmap.put(f[i].getColumn(), f[i]);
    // 
    MProcessPara[] processParams = pInstance.getProcessParameters();
    for (int pi = 0; pi < processParams.length; pi++) {
        MPInstancePara iPara = new MPInstancePara(pInstance, processParams[pi].getSeqNo());
        iPara.setParameterName(processParams[pi].getColumnName());
        iPara.setInfo(processParams[pi].getName());
        String key = iPara.getParameterName();
        MProcessPara pPara = process.getParameter(key);
        if (pPara == null) {
            log.log(Level.SEVERE, "Parameter not found: " + key);
            continue;
        }
        int displayType = pPara.getAD_Reference_ID();
        String valueString = null;
        DataField dataField = fmap.get(key);
        if (dataField != null && !Util.isEmpty(dataField.getVal())) {
            valueString = dataField.getVal();
            if (requestCtx != null && valueString.charAt(0) == '@') {
                Object value = ModelADServiceImpl.parseVariable(m_cs, requestCtx, iPara.getParameterName(), valueString);
                valueString = value != null ? value.toString() : null;
            }
        } else if (dataField != null && !Util.isEmpty(dataField.getLval()))
            valueString = getLookupValue(pPara, dataField);
        if (isDataURI(valueString)) {
            valueString = "";
            iPara.setInfo("binary");
        }
        String valueString2 = null;
        if (pPara.isRange()) {
            dataField = fmap.get(key + "_2");
            if (dataField != null && !Util.isEmpty(dataField.getVal())) {
                valueString2 = dataField.getVal();
                if (requestCtx != null && valueString2.charAt(0) == '@') {
                    Object value = ModelADServiceImpl.parseVariable(m_cs, requestCtx, iPara.getParameterName(), valueString2);
                    valueString2 = value != null ? value.toString() : null;
                }
            } else if (dataField != null && !Util.isEmpty(dataField.getLval()))
                valueString2 = getLookupValue(pPara, dataField);
        }
        if (log.isLoggable(Level.FINE))
            log.fine("fillParameter - " + key + " = " + valueString);
        Object value = valueString;
        Object toValue = valueString2;
        if (valueString != null && valueString.length() == 0)
            value = null;
        if (value != null && (DisplayType.isList(displayType) || DisplayType.TableDir == displayType || DisplayType.Table == displayType) && value.equals("-1"))
            value = null;
        // No Value
        if (value == null && DisplayType.YesNo != pPara.getAD_Reference_ID()) {
            if (pPara.isMandatory())
                throw new Exception(" Parameter " + pPara.getName() + " is required.");
        } else {
            if (DisplayType.isText(displayType) && Util.isEmpty(String.valueOf(value)) && Util.isEmpty(String.valueOf(toValue))) {
                if (log.isLoggable(Level.FINE))
                    log.fine(pPara.getColumnName() + " - empty string");
                break;
            }
            // Convert to Type
            try {
                if (DisplayType.isNumeric(displayType) || DisplayType.isID(displayType)) {
                    BigDecimal bd = null;
                    if (value instanceof BigDecimal)
                        bd = (BigDecimal) value;
                    else if (value instanceof Integer)
                        bd = new BigDecimal(((Integer) value).intValue());
                    else
                        bd = new BigDecimal(value.toString());
                    iPara.setP_Number(bd);
                    if (log.isLoggable(Level.FINE))
                        log.fine("fillParameter - " + key + " = " + valueString + " (=" + bd + "=)");
                    if (pPara.isRange()) {
                        bd = null;
                        bd = new BigDecimal(valueString2.toString());
                        iPara.setP_Number_To(bd);
                    }
                } else if (DisplayType.isDate(displayType)) {
                    java.util.Date d;
                    if (value.toString().length() > 0) {
                        if (displayType == DisplayType.DateTime) {
                            try {
                                d = m_cs.dateTimeFormatJDBC.parse(value.toString());
                            } catch (ParseException e) {
                                d = m_cs.dateTimeFormat.parse(value.toString());
                            }
                        } else if (displayType == DisplayType.Time) {
                            try {
                                d = m_cs.timeFormatJDBC.parse(value.toString());
                            } catch (ParseException e) {
                                d = m_cs.timeFormat.parse(value.toString());
                            }
                        } else {
                            try {
                                d = m_cs.dateFormatJDBC.parse(value.toString());
                            } catch (ParseException e) {
                                d = m_cs.dateFormat.parse(value.toString());
                            }
                        }
                        Timestamp ts = new Timestamp(d.getTime());
                        iPara.setP_Date(ts);
                        if (log.isLoggable(Level.FINE))
                            log.fine("fillParameter - " + key + " = " + valueString + " (=" + ts + "=)");
                    }
                    if (pPara.isRange()) {
                        if (valueString2 != null && valueString2.length() > 0) {
                            if (displayType == DisplayType.DateTime) {
                                try {
                                    d = m_cs.dateTimeFormatJDBC.parse(valueString2);
                                } catch (ParseException e) {
                                    d = m_cs.dateTimeFormat.parse(valueString2);
                                }
                            } else if (displayType == DisplayType.Time) {
                                try {
                                    d = m_cs.timeFormatJDBC.parse(valueString2);
                                } catch (ParseException e) {
                                    d = m_cs.timeFormat.parse(valueString2);
                                }
                            } else {
                                try {
                                    d = m_cs.dateFormatJDBC.parse(valueString2);
                                } catch (ParseException e) {
                                    d = m_cs.dateFormat.parse(valueString2);
                                }
                            }
                            Timestamp ts = new Timestamp(d.getTime());
                            iPara.setP_Date_To(ts);
                            if (log.isLoggable(Level.FINE))
                                log.fine("fillParameterTo - " + key + " = " + valueString + " (=" + ts + "=)");
                        }
                    }
                } else if (DisplayType.YesNo == pPara.getAD_Reference_ID()) {
                    String bv = "N";
                    if (value == null)
                        bv = "N";
                    else // if (value.toString().toLowerCase().charAt(0)=='t')
                    {
                        if ("true".equalsIgnoreCase(valueString) || "y".equalsIgnoreCase(valueString))
                            bv = "Y";
                        else
                            bv = "N";
                    }
                    // bv ="Y";
                    iPara.setP_String(bv);
                } else {
                    iPara.setP_String(value.toString());
                }
                if (// kolec - ustawia wartosc dla parametru Lookup
                pPara.isLookup()) {
                    Lookup lok = pPara.getLookup();
                    if (lok != null) {
                        NamePair np = lok.getDirect(value, false, false);
                        if (np != null) {
                            iPara.setInfo(np.getName());
                        }
                    }
                } else {
                    if (value != null)
                        iPara.setInfo(value.toString());
                    if (valueString2 != null)
                        iPara.setInfo_To(valueString2);
                }
                // 
                iPara.saveEx();
            } catch (Exception e) {
                log.warning("fillParameter - " + key + " = " + valueString + " (" + value + ") " + value.getClass().getName() + " - " + e.getLocalizedMessage());
            }
        }
    // not null
    }
    // instance parameter loop
    // kolec - tego chyba brakowalo
    pInstance.saveEx();
    return pInstance;
}
Also used : MPInstancePara(org.compiere.model.MPInstancePara) HashMap(java.util.HashMap) NamePair(org.compiere.util.NamePair) MProcessPara(org.compiere.model.MProcessPara) Timestamp(java.sql.Timestamp) ParseException(java.text.ParseException) BigDecimal(java.math.BigDecimal) MPInstance(org.compiere.model.MPInstance) DataField(org.idempiere.adInterface.x10.DataField) Lookup(org.compiere.model.Lookup) MLookup(org.compiere.model.MLookup) ParseException(java.text.ParseException)

Example 9 with DataRow

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

the class GetListLookup method getData.

/* (non-Javadoc)
	 * @see org.compiere.model.Lookup#getData(boolean, boolean, boolean, boolean, boolean)
	 */
@Override
public ArrayList<Object> getData(boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary, boolean shortlist) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    SOAPConnectionFactory cf;
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        dataMap = new LinkedHashMap<Object, NamePair>();
        cf = SOAPConnectionFactory.newInstance();
        SOAPConnection conn = cf.createConnection();
        // Create a SOAPMessage instance
        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage message = mf.createMessage();
        // Create a SOAP envelope and body
        SOAPPart part = message.getSOAPPart();
        SOAPEnvelope env = part.getEnvelope();
        SOAPBody body = env.getBody();
        ModelGetListRequestDocument getListDocument = ModelGetListRequestDocument.Factory.newInstance();
        ModelGetListRequest getListRequest = getListDocument.addNewModelGetListRequest();
        getListRequest.setADLoginRequest(login);
        ModelGetList getList = getListRequest.addNewModelGetList();
        getList.setFilter(filter);
        getList.setServiceType(serviceType);
        body.addDocument((Document) getListDocument.getDomNode());
        // Invoke the service endpoint
        URL endpoint = new URL(endPoint);
        SOAPMessage responseMsg = null;
        try {
            responseMsg = conn.call(message, endpoint);
        } finally {
            conn.close();
        }
        if (responseMsg != null && responseMsg.getSOAPBody() != null) {
            if (responseMsg.getSOAPBody().hasFault()) {
                throw new RuntimeException(responseMsg.getSOAPBody().getFault().getFaultString());
            }
            WindowTabDataDocument responseDoc = WindowTabDataDocument.Factory.parse(responseMsg.getSOAPBody().getFirstChild().getFirstChild());
            WindowTabData windowTabData = responseDoc.getWindowTabData();
            if (windowTabData.isSetError()) {
                throw new RuntimeException(windowTabData.getError());
            }
            DataSet dataset = windowTabData.getDataSet();
            DataRow[] dataRows = dataset.getDataRowArray();
            for (DataRow dataRow : dataRows) {
                DataField[] dataFields = dataRow.getFieldArray();
                String key = null;
                String display = null;
                for (DataField dataField : dataFields) {
                    if (dataField.getColumn().equals(keyColumn)) {
                        key = dataField.getVal();
                    } else if (dataField.getColumn().equals(displayColumn)) {
                        display = dataField.getVal();
                    }
                }
                if (key != null && display != null) {
                    dataMap.put(key, new ValueNamePair(key, display));
                }
            }
        }
    } catch (Exception e) {
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException(e.getLocalizedMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
    return new ArrayList<Object>(dataMap.values());
}
Also used : DataSet(org.idempiere.adInterface.x10.DataSet) ArrayList(java.util.ArrayList) SOAPConnection(javax.xml.soap.SOAPConnection) ModelGetList(org.idempiere.adInterface.x10.ModelGetList) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) DataRow(org.idempiere.adInterface.x10.DataRow) URL(java.net.URL) WindowTabDataDocument(org.idempiere.adInterface.x10.WindowTabDataDocument) SOAPPart(javax.xml.soap.SOAPPart) ValueNamePair(org.compiere.util.ValueNamePair) SOAPConnectionFactory(javax.xml.soap.SOAPConnectionFactory) ModelGetListRequestDocument(org.idempiere.adInterface.x10.ModelGetListRequestDocument) MessageFactory(javax.xml.soap.MessageFactory) NamePair(org.compiere.util.NamePair) ValueNamePair(org.compiere.util.ValueNamePair) WindowTabData(org.idempiere.adInterface.x10.WindowTabData) SOAPBody(javax.xml.soap.SOAPBody) DataField(org.idempiere.adInterface.x10.DataField) ModelGetListRequest(org.idempiere.adInterface.x10.ModelGetListRequest)

Aggregations

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