Search in sources :

Example 1 with UpdateExecutor

use of ambit2.db.UpdateExecutor in project ambit-mirror by ideaconsult.

the class CallableQueryResultsCreator method createQueryResults.

protected TaskResult createQueryResults(IQueryObject<IStructureRecord> target) throws Exception {
    Connection connection = null;
    UpdateExecutor xx = new UpdateExecutor();
    try {
        DBConnection dbc = new DBConnection(context);
        connection = dbc.getConnection();
        SessionID session = new SessionID();
        session.setName(folder);
        CreateQueryFolder qfolder = new CreateQueryFolder();
        qfolder.setObject(session);
        xx.setConnection(connection);
        xx.process(qfolder);
        ProcessorCreateQuery p = new ProcessorCreateQuery();
        p.setQueryName(queryName == null ? UUID.randomUUID().toString() : queryName);
        p.setProfile(template);
        p.setDelete(clearPreviousContent);
        p.setCopy(true);
        p.setStoredQuery(storedQuery);
        p.setSession(session);
        p.setConnection(connection);
        IStoredQuery q = p.process((IQueryObject<IStructureRecord>) target);
        return new TaskResult(String.format("%s/dataset/%s%d", applicationRootReference, DatasetStructuresResource.QR_PREFIX, q.getId()));
    } catch (Exception x) {
        Context.getCurrentLogger().log(Level.SEVERE, x.getMessage(), x);
        throw x;
    } finally {
        Context.getCurrentLogger().fine("Done");
        try {
            xx.close();
        } catch (Exception x) {
        }
        try {
            connection.close();
        } catch (Exception x) {
            Context.getCurrentLogger().warning(x.getMessage());
        }
    }
}
Also used : DBConnection(ambit2.rest.DBConnection) IStoredQuery(ambit2.db.search.IStoredQuery) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ProcessorCreateQuery(ambit2.db.processors.ProcessorCreateQuery) UpdateExecutor(ambit2.db.UpdateExecutor) Connection(java.sql.Connection) DBConnection(ambit2.rest.DBConnection) CreateQueryFolder(ambit2.db.update.queryfolder.CreateQueryFolder) SessionID(ambit2.db.SessionID) ResourceException(org.restlet.resource.ResourceException)

Example 2 with UpdateExecutor

use of ambit2.db.UpdateExecutor in project ambit-mirror by ideaconsult.

the class ProcessorCreateQuery method process.

public IStoredQuery process(IQueryObject<IStructureRecord> target) throws AmbitException {
    if (target == null)
        throw new AmbitException("Undefined query!");
    if (qexec == null) {
        qexec = new QueryExecutor();
        qexec.setCloseConnection(false);
    }
    qexec.setConnection(connection);
    try {
        if (result == null) {
            if (!copy & (target instanceof QueryStoredResults))
                return ((QueryStoredResults) target).getFieldname();
            result = new StoredQuery(-1);
            result.setName(queryName == null ? UUID.randomUUID().toString() : queryName);
        } else {
            if (target instanceof QueryStoredResults) {
                if ((((QueryStoredResults) target).getId() == result.getId()) && (result.getId() > 0))
                    // if we are copying to the same query , don't delete!
                    delete = false;
            }
        }
        result.setQuery(target);
        connection.setAutoCommit(false);
        // create entry in the query table
        if (result.getId() <= 0) {
            // read by name and folder title
            ReadStoredQuery findIfExists = new ReadStoredQuery();
            findIfExists.setValue(result);
            // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            findIfExists.setFieldname(getSession().getName());
            ResultSet found = null;
            try {
                found = qexec.process(findIfExists);
                while (found.next()) {
                    IStoredQuery q = findIfExists.getObject(found);
                    result.setID(q.getID());
                    break;
                }
            } catch (Exception x) {
                logger.warning(x.getMessage());
            } finally {
                try {
                    found.close();
                } catch (Exception x) {
                }
            }
            // if still not found, add it! TODO refactor it to use UpdateExecutor
            if (result.getId() <= 0) {
                PreparedStatement s = connection.prepareStatement(CreateStoredQuery.sql_byname, Statement.RETURN_GENERATED_KEYS);
                s.setNull(1, Types.INTEGER);
                if (result.getName().length() > 255)
                    s.setString(2, result.getName().substring(0, 255));
                else
                    s.setString(2, result.getName());
                try {
                    s.setString(3, result.getQuery().toString() == null ? "Results" : result.getQuery().toString());
                } catch (Exception x) {
                    s.setString(3, "Results");
                }
                s.setString(4, getSession().getName());
                // execute
                if (s.executeUpdate() > 0) {
                    ResultSet rss = s.getGeneratedKeys();
                    while (rss.next()) result.setId(new Integer(rss.getInt(1)));
                    rss.close();
                }
                s.close();
            }
        }
        // sometimes we want to remove the old content
        if (delete && (result.getId() > 0)) {
            PreparedStatement s = null;
            try {
                s = connection.prepareStatement("Delete from query_results where idquery=?");
                s.setInt(1, result.getId());
                s.executeUpdate();
            } catch (Exception x) {
            } finally {
                try {
                    if (s != null)
                        s.close();
                } catch (Exception x) {
                }
            }
        }
        if (result.getId() > 0) {
            int rows = 0;
            if ((result.getQuery() instanceof IQueryRetrieval) && ((IQueryRetrieval) result.getQuery()).isPrescreen()) {
                rows = insertScreenedResults(result, (IQueryRetrieval<IStructureRecord>) result.getQuery());
            } else {
                rows = insertResults(result);
                if (rows > 0)
                    connection.commit();
                else
                    connection.rollback();
            }
            insertProfile(result, profile);
        }
        return result;
    } catch (Exception x) {
        try {
            connection.rollback();
        } catch (SQLException xx) {
        }
        throw new ProcessorException(this, x);
    } finally {
        try {
            close();
        } catch (Exception e) {
        }
    }
}
Also used : IStoredQuery(ambit2.db.search.IStoredQuery) StoredQuery(ambit2.db.search.StoredQuery) CreateStoredQuery(ambit2.db.update.storedquery.CreateStoredQuery) ReadStoredQuery(ambit2.db.update.storedquery.ReadStoredQuery) ProcessorException(ambit2.base.processors.ProcessorException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) IQueryRetrieval(net.idea.modbcum.i.IQueryRetrieval) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException) SQLException(java.sql.SQLException) ProcessorException(ambit2.base.processors.ProcessorException) IStoredQuery(ambit2.db.search.IStoredQuery) QueryExecutor(ambit2.db.search.QueryExecutor) ReadStoredQuery(ambit2.db.update.storedquery.ReadStoredQuery) ResultSet(java.sql.ResultSet) QueryStoredResults(ambit2.db.search.structure.QueryStoredResults) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException)

Example 3 with UpdateExecutor

use of ambit2.db.UpdateExecutor in project ambit-mirror by ideaconsult.

the class SubstanceWriterTest method delete.

public int delete(SubstanceEndpointsBundle bundle, IRawReader<IStructureRecord> reader, Connection connection) throws Exception {
    /*
		 * DBSubstanceWriter writer; if (bundle != null) writer = new
		 * DBBundleStudyWriter(bundle, DBSubstanceWriter.datasetMeta(), new
		 * SubstanceRecord()); else writer = new
		 * DBSubstanceWriter(DBSubstanceWriter.datasetMeta(), new
		 * SubstanceRecord(), clearMeasurements, clearComposition);
		 * writer.setSplitRecord(splitRecord); writer.setConnection(connection);
		 * writer.open();
		 */
    UpdateExecutor<IQueryUpdate> writer = new UpdateExecutor<IQueryUpdate>();
    writer.setConnection(connection);
    DeleteMatrixValue q = new DeleteMatrixValue();
    q.setGroup(bundle);
    int records = 0;
    while (reader.hasNext()) {
        Object record = reader.next();
        if (record == null)
            continue;
        Assert.assertTrue(record instanceof SubstanceRecord);
        Assert.assertEquals("IUC4-efdb21bb-e79f-3286-a988-b6f6944d3734", ((SubstanceRecord) record).getSubstanceUUID());
        for (ProtocolApplication pa : ((SubstanceRecord) record).getMeasurements()) {
            Assert.assertTrue(pa instanceof ProtocolApplicationAnnotated);
            // System.out.println(((ProtocolApplicationAnnotated) pa)getRecords_to_delete());
            ProtocolApplicationAnnotated paa = (ProtocolApplicationAnnotated) pa;
            List<ValueAnnotated> vaa = paa.getRecords_to_delete();
            for (ValueAnnotated va : vaa) {
                q.setObject(va);
                writer.process(q);
            }
        }
        // writer.setImportedRecord((SubstanceRecord) record);
        // writer.process((IStructureRecord) record);
        records++;
    }
    writer.close();
    return records;
}
Also used : DeleteMatrixValue(ambit2.db.update.bundle.matrix.DeleteMatrixValue) ProtocolApplication(ambit2.base.data.study.ProtocolApplication) UpdateExecutor(net.idea.modbcum.p.UpdateExecutor) ValueAnnotated(ambit2.base.data.study.ValueAnnotated) SubstanceRecord(ambit2.base.data.SubstanceRecord) IQueryUpdate(net.idea.modbcum.i.query.IQueryUpdate) ProtocolApplicationAnnotated(ambit2.base.data.study.ProtocolApplicationAnnotated)

Example 4 with UpdateExecutor

use of ambit2.db.UpdateExecutor in project ambit-mirror by ideaconsult.

the class CallableWekaModelCreator method createReference.

@Override
protected TaskResult createReference(Connection connection) throws Exception {
    builder.setTrainingData(((RDFInstancesParser) batch).getInstances());
    UpdateExecutor<CreateModel> x = new UpdateExecutor<CreateModel>();
    try {
        model = createModel();
        CreateModel update = new CreateModel(model);
        x.setConnection(connection);
        x.process(update);
        writeAnnotations(model.getPredicted(), x);
        return new TaskResult(builder.getModelReporter().getURI(model));
    } catch (WekaException e) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(), e);
    } catch (AmbitException e) {
        Context.getCurrentLogger().severe(e.getMessage());
        if ((e.getCause() != null) && (e.getCause() instanceof WekaException))
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getCause().getMessage(), e.getCause());
        else
            throw e;
    } catch (Exception e) {
        Context.getCurrentLogger().severe(e.getMessage());
        throw e;
    } finally {
        try {
            x.close();
        } catch (Exception xx) {
        }
    }
}
Also used : WekaException(weka.core.WekaException) CreateModel(ambit2.db.update.model.CreateModel) UpdateExecutor(ambit2.db.UpdateExecutor) TaskResult(ambit2.rest.task.TaskResult) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) WekaException(weka.core.WekaException) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 5 with UpdateExecutor

use of ambit2.db.UpdateExecutor in project ambit-mirror by ideaconsult.

the class CallableWafflesModelCreator method createReference.

/**
 *	TODO download ARFF directly, instead of reading Weka instances in memory
 *	protected AbstractBatchProcessor createBatch(Object target) throws Exception{
 *		if (target == null) throw new Exception("No dataset URI");
 *
 *		File.createTempFile(prefix, suffix);
 *		DownloadTool.download(url, file);
 *		builder.setTrainingData(trainingData);
 *		return null;
 *	}
 */
@Override
protected TaskResult createReference(Connection connection) throws Exception {
    File trainingData = File.createTempFile("wfltrain_", ".arff");
    trainingData.deleteOnExit();
    Instances instances = ((RDFInstancesParser) batch).getInstances();
    instances.deleteAttributeAt(0);
    // sort attributes by name (i.e. attribute URI).
    List<Attribute> sorted = new ArrayList<Attribute>();
    for (int i = 0; i < instances.numAttributes(); i++) sorted.add(instances.attribute(i));
    Collections.sort(sorted, new Comparator<Attribute>() {

        @Override
        public int compare(Attribute o1, Attribute o2) {
            return o1.name().compareTo(o2.toString());
        }
    });
    StringBuilder order = null;
    for (int i = 0; i < sorted.size(); i++) {
        if (order == null)
            order = new StringBuilder();
        else
            order.append(",");
        order.append(sorted.get(i).index() + 1);
    }
    Reorder reorder = new Reorder();
    String[] options = new String[2];
    options[0] = "-R";
    options[1] = order.toString();
    reorder.setOptions(options);
    reorder.setInputFormat(instances);
    instances = Filter.useFilter(instances, reorder);
    SparseToNonSparse sp = new SparseToNonSparse();
    sp.setInputFormat(instances);
    Instances newInstances = Filter.useFilter(instances, sp);
    ArffSaver saver = new ArffSaver();
    saver.setInstances(newInstances);
    saver.setFile(trainingData);
    saver.writeBatch();
    // Leave the header only
    newInstances.delete();
    builder.setHeader(newInstances);
    builder.setTrainingData(trainingData);
    UpdateExecutor<CreateModel> x = new UpdateExecutor<CreateModel>();
    try {
        model = createModel();
        // trainingData.delete();
        CreateModel update = new CreateModel(model);
        x.setConnection(connection);
        x.process(update);
        writeAnnotations(model.getPredicted(), x);
        return new TaskResult(builder.getModelReporter().getURI(model));
    } catch (WekaException e) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(), e);
    } catch (Exception e) {
        Context.getCurrentLogger().severe(e.getMessage());
        throw e;
    } finally {
        try {
            x.close();
        } catch (Exception xx) {
        }
    }
}
Also used : WekaException(weka.core.WekaException) Attribute(weka.core.Attribute) ArrayList(java.util.ArrayList) ArffSaver(weka.core.converters.ArffSaver) WekaException(weka.core.WekaException) ResourceException(org.restlet.resource.ResourceException) Instances(weka.core.Instances) Reorder(weka.filters.unsupervised.attribute.Reorder) CreateModel(ambit2.db.update.model.CreateModel) SparseToNonSparse(weka.filters.unsupervised.instance.SparseToNonSparse) UpdateExecutor(ambit2.db.UpdateExecutor) TaskResult(ambit2.rest.task.TaskResult) ResourceException(org.restlet.resource.ResourceException) RDFInstancesParser(ambit2.rest.dataset.RDFInstancesParser) File(java.io.File)

Aggregations

UpdateExecutor (ambit2.db.UpdateExecutor)6 ResourceException (org.restlet.resource.ResourceException)5 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)4 ProcessorException (ambit2.base.processors.ProcessorException)3 CreateModel (ambit2.db.update.model.CreateModel)3 DBConnection (ambit2.rest.DBConnection)3 TaskResult (ambit2.rest.task.TaskResult)3 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 IStoredQuery (ambit2.db.search.IStoredQuery)2 QueryExecutor (ambit2.db.search.QueryExecutor)2 WekaException (weka.core.WekaException)2 SubstanceRecord (ambit2.base.data.SubstanceRecord)1 ProtocolApplication (ambit2.base.data.study.ProtocolApplication)1 ProtocolApplicationAnnotated (ambit2.base.data.study.ProtocolApplicationAnnotated)1 ValueAnnotated (ambit2.base.data.study.ValueAnnotated)1 IStructureRecord (ambit2.base.interfaces.IStructureRecord)1 ModelQueryResults (ambit2.core.data.model.ModelQueryResults)1 SessionID (ambit2.db.SessionID)1 ProcessorCreateQuery (ambit2.db.processors.ProcessorCreateQuery)1