Search in sources :

Example 11 with Dataset

use of com.google.cloud.automl.v1beta1.Dataset 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 12 with Dataset

use of com.google.cloud.automl.v1beta1.Dataset in project idempiere by idempiere.

the class QueryDataLookup 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<Integer, KeyNamePair>();
        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();
        ModelCRUDRequestDocument crudDocument = ModelCRUDRequestDocument.Factory.newInstance();
        ModelCRUDRequest crudRequest = crudDocument.addNewModelCRUDRequest();
        crudRequest.setADLoginRequest(login);
        ModelCRUD crud = crudRequest.addNewModelCRUD();
        crud.setRecordID(0);
        crud.setFilter(filter);
        crud.setAction(ModelCRUD.Action.READ);
        crud.setServiceType(serviceType);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
        Element element = document.createElementNS("http://idempiere.org/ADInterface/1_0", "queryData");
        Node domNode = document.importNode(crudDocument.getDomNode().getFirstChild(), true);
        document.appendChild(element);
        element.appendChild(domNode);
        body.addDocument(document);
        // 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) {
                    Integer id = Integer.valueOf(key);
                    dataMap.put(id, new KeyNamePair(id, 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 : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DataSet(org.idempiere.adInterface.x10.DataSet) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) SOAPConnection(javax.xml.soap.SOAPConnection) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) WindowTabDataDocument(org.idempiere.adInterface.x10.WindowTabDataDocument) Document(org.w3c.dom.Document) ModelCRUDRequestDocument(org.idempiere.adInterface.x10.ModelCRUDRequestDocument) SOAPMessage(javax.xml.soap.SOAPMessage) ModelCRUDRequestDocument(org.idempiere.adInterface.x10.ModelCRUDRequestDocument) DataRow(org.idempiere.adInterface.x10.DataRow) URL(java.net.URL) WindowTabDataDocument(org.idempiere.adInterface.x10.WindowTabDataDocument) ModelCRUD(org.idempiere.adInterface.x10.ModelCRUD) SOAPPart(javax.xml.soap.SOAPPart) SOAPConnectionFactory(javax.xml.soap.SOAPConnectionFactory) MessageFactory(javax.xml.soap.MessageFactory) WindowTabData(org.idempiere.adInterface.x10.WindowTabData) SOAPBody(javax.xml.soap.SOAPBody) ModelCRUDRequest(org.idempiere.adInterface.x10.ModelCRUDRequest) DataField(org.idempiere.adInterface.x10.DataField) DocumentBuilder(javax.xml.parsers.DocumentBuilder) KeyNamePair(org.compiere.util.KeyNamePair)

Example 13 with Dataset

use of com.google.cloud.automl.v1beta1.Dataset in project java-automl by googleapis.

the class ModelApi method createModel.

// [START automl_vision_create_model]
/**
 * Demonstrates using the AutoML client to create a model.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param dataSetId the Id of the dataset to which model is created.
 * @param modelName the Name of the model.
 * @param trainBudget the Budget for training the model.
 */
static void createModel(String projectId, String computeRegion, String dataSetId, String modelName, String trainBudget) {
    // Instantiates a client
    try (AutoMlClient client = AutoMlClient.create()) {
        // A resource that represents Google Cloud Platform location.
        LocationName projectLocation = LocationName.of(projectId, computeRegion);
        // Set model metadata.
        ImageClassificationModelMetadata imageClassificationModelMetadata = Long.valueOf(trainBudget) == 0 ? ImageClassificationModelMetadata.newBuilder().build() : ImageClassificationModelMetadata.newBuilder().setTrainBudget(Long.valueOf(trainBudget)).build();
        // Set model name and model metadata for the image dataset.
        Model myModel = Model.newBuilder().setDisplayName(modelName).setDatasetId(dataSetId).setImageClassificationModelMetadata(imageClassificationModelMetadata).build();
        // Create a model with the model metadata in the region.
        OperationFuture<Model, OperationMetadata> response = client.createModelAsync(projectLocation, myModel);
        System.out.println(String.format("Training operation name: %s", response.getInitialFuture().get().getName()));
        System.out.println("Training started...");
    } catch (IOException | ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : ImageClassificationModelMetadata(com.google.cloud.automl.v1beta1.ImageClassificationModelMetadata) Model(com.google.cloud.automl.v1beta1.Model) IOException(java.io.IOException) OperationMetadata(com.google.cloud.automl.v1beta1.OperationMetadata) ExecutionException(java.util.concurrent.ExecutionException) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient) LocationName(com.google.cloud.automl.v1beta1.LocationName)

Example 14 with Dataset

use of com.google.cloud.automl.v1beta1.Dataset in project java-automl by googleapis.

the class ImportDataset method importDataset.

// Import a dataset
static void importDataset(String projectId, String datasetId, String path) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    Duration totalTimeout = Duration.ofMinutes(45);
    RetrySettings retrySettings = RetrySettings.newBuilder().setTotalTimeout(totalTimeout).build();
    AutoMlSettings.Builder builder = AutoMlSettings.newBuilder();
    builder.importDataSettings().setRetrySettings(retrySettings).build();
    AutoMlSettings settings = builder.build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create(settings)) {
        // Get the complete path of the dataset.
        DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
        // Get multiple Google Cloud Storage URIs to import data from
        GcsSource gcsSource = GcsSource.newBuilder().addAllInputUris(Arrays.asList(path.split(","))).build();
        // Import data from the input URI
        InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build();
        System.out.println("Processing import...");
        // Start the import job
        OperationFuture<Empty, OperationMetadata> operation = client.importDataAsync(datasetFullId, inputConfig);
        System.out.format("Operation name: %s%n", operation.getName());
        // If you want to wait for the operation to finish, adjust the timeout appropriately. The
        // operation will still run if you choose not to wait for it to complete. You can check the
        // status of your operation using the operation's name.
        Empty response = operation.get(45, TimeUnit.MINUTES);
        System.out.format("Dataset imported. %s%n", response);
    } catch (TimeoutException e) {
        System.out.println("The operation's polling period was not long enough.");
        System.out.println("You can use the Operation's name to get the current status.");
        System.out.println("The import job is still running and will complete as expected.");
        throw e;
    }
}
Also used : RetrySettings(com.google.api.gax.retrying.RetrySettings) Empty(com.google.protobuf.Empty) GcsSource(com.google.cloud.automl.v1beta1.GcsSource) DatasetName(com.google.cloud.automl.v1beta1.DatasetName) Duration(org.threeten.bp.Duration) InputConfig(com.google.cloud.automl.v1beta1.InputConfig) AutoMlSettings(com.google.cloud.automl.v1beta1.AutoMlSettings) OperationMetadata(com.google.cloud.automl.v1beta1.OperationMetadata) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient) TimeoutException(java.util.concurrent.TimeoutException)

Example 15 with Dataset

use of com.google.cloud.automl.v1beta1.Dataset in project java-automl by googleapis.

the class ListDatasets method listDatasets.

// List the datasets
static void listDatasets(String projectId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
        // A resource that represents Google Cloud Platform location.
        LocationName projectLocation = LocationName.of(projectId, "us-central1");
        ListDatasetsRequest request = ListDatasetsRequest.newBuilder().setParent(projectLocation.toString()).build();
        // List all the datasets available in the region by applying filter.
        System.out.println("List of datasets:");
        for (Dataset dataset : client.listDatasets(request).iterateAll()) {
            // Display the dataset information
            System.out.format("%nDataset name: %s%n", dataset.getName());
            // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
            // required for other methods.
            // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
            String[] names = dataset.getName().split("/");
            String retrievedDatasetId = names[names.length - 1];
            System.out.format("Dataset id: %s%n", retrievedDatasetId);
            System.out.format("Dataset display name: %s%n", dataset.getDisplayName());
            System.out.println("Dataset create time:");
            System.out.format("\tseconds: %s%n", dataset.getCreateTime().getSeconds());
            System.out.format("\tnanos: %s%n", dataset.getCreateTime().getNanos());
            // [END automl_video_object_tracking_list_datasets_beta]
            // [END automl_tables_list_datasets]
            System.out.format("Video classification dataset metadata: %s%n", dataset.getVideoClassificationDatasetMetadata());
            // [END automl_video_classification_list_datasets_beta]
            // [START automl_video_object_tracking_list_datasets_beta]
            System.out.format("Video object tracking dataset metadata: %s%n", dataset.getVideoObjectTrackingDatasetMetadata());
            // [END automl_video_object_tracking_list_datasets_beta]
            // [START automl_tables_list_datasets]
            System.out.format("Tables dataset metadata: %s%n", dataset.getTablesDatasetMetadata());
        // [START automl_video_classification_list_datasets_beta]
        // [START automl_video_object_tracking_list_datasets_beta]
        }
    }
}
Also used : Dataset(com.google.cloud.automl.v1beta1.Dataset) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient) LocationName(com.google.cloud.automl.v1beta1.LocationName) ListDatasetsRequest(com.google.cloud.automl.v1beta1.ListDatasetsRequest)

Aggregations

IOException (java.io.IOException)15 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)13 Dataset (com.google.cloud.datalabeling.v1beta1.Dataset)9 ArrayList (java.util.ArrayList)9 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)8 Dataset (com.google.cloud.automl.v1.Dataset)8 LocationName (com.google.cloud.automl.v1.LocationName)7 LocationName (com.google.cloud.automl.v1beta1.LocationName)7 DataLabelingServiceClient (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient)7 ProjectName (com.google.cloud.datalabeling.v1beta1.ProjectName)7 OperationMetadata (com.google.cloud.automl.v1.OperationMetadata)6 Dataset (com.google.cloud.automl.v1beta1.Dataset)6 DataSet (org.openmuc.j62056.DataSet)6 DatasetName (com.google.cloud.automl.v1beta1.DatasetName)5 ListDatasetsPagedResponse (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse)5 ListDatasetsRequest (com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest)5 DataField (org.idempiere.adInterface.x10.DataField)5 DataRow (org.idempiere.adInterface.x10.DataRow)5 DataSet (org.idempiere.adInterface.x10.DataSet)5 WindowTabData (org.idempiere.adInterface.x10.WindowTabData)5