use of org.folio.rest.persist.Criteria.Criteria in project mod-inventory-storage by folio-org.
the class ContributorTypeAPI method getContributorTypesByContributorTypeId.
@Validate
@Override
public void getContributorTypesByContributorTypeId(String contributorTypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
String tenantId = TenantTool.tenantId(okapiHeaders);
Criterion c = new Criterion(new Criteria().addField(idFieldName).setJSONB(false).setOperation("=").setValue("'" + contributorTypeId + "'"));
PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CONTRIBUTOR_TYPE_TABLE, ContributorType.class, c, true, reply -> {
try {
if (reply.failed()) {
String msg = PgExceptionUtil.badRequestMessage(reply.cause());
if (msg == null) {
internalServerErrorDuringGetById(reply.cause(), lang, asyncResultHandler);
return;
}
log.info(msg);
asyncResultHandler.handle(Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withPlainNotFound(msg)));
return;
}
@SuppressWarnings("unchecked") List<ContributorType> contributorType = (List<ContributorType>) reply.result().getResults();
if (contributorType.isEmpty()) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withPlainNotFound(contributorTypeId)));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withJsonOK(contributorType.get(0))));
}
} catch (Exception e) {
internalServerErrorDuringGetById(e, lang, asyncResultHandler);
}
});
} catch (Exception e) {
internalServerErrorDuringGetById(e, lang, asyncResultHandler);
}
});
}
use of org.folio.rest.persist.Criteria.Criteria in project raml-module-builder by folio-org.
the class JobAPI method getJobsJobconfsByJobconfsId.
@Validate
@Override
public void getJobsJobconfsByJobconfsId(String jobconfsId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
System.out.println("sending... getJobsJobconfsByJobconfsId");
try {
Criteria c = new Criteria();
c.addField("_id");
c.setOperation("=");
c.setValue(jobconfsId);
c.setJSONB(false);
vertxContext.runOnContext(v -> {
try {
PostgresClient.getInstance(vertxContext.owner()).get(RTFConsts.JOB_CONF_COLLECTION, JobConf.class, new Criterion(c), true, reply -> {
@SuppressWarnings("unchecked") List<JobConf> confs = (List<JobConf>) reply.result().getResults();
if (confs.isEmpty()) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withPlainNotFound("JobConf " + messages.getMessage(lang, "10008"))));
return;
}
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withJsonOK(confs.get(0))));
});
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
}
use of org.folio.rest.persist.Criteria.Criteria in project raml-module-builder by folio-org.
the class JobAPI method putJobsJobconfsByJobconfsIdJobsByJobId.
@Validate
@Override
public void putJobsJobconfsByJobconfsIdJobsByJobId(String jobId, String jobconfsId, String lang, Job entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
System.out.println("sending... putJobsJobconfsByJobconfsId");
try {
Criteria c = new Criteria();
c.addField("_id");
c.setOperation("=");
c.setValue(jobId);
c.setJSONB(false);
Criteria d = new Criteria();
d.addField("job_conf_id");
d.setOperation("=");
d.setValue(jobconfsId);
d.setJSONB(false);
vertxContext.runOnContext(v -> {
try {
PostgresClient.getInstance(vertxContext.owner()).update(RTFConsts.JOBS_COLLECTION, entity, new Criterion().addCriterion(c, "AND", d), true, reply -> {
if (reply.succeeded() && reply.result().getUpdated() == 0) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withPlainNotFound(jobId)));
} else {
try {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withNoContent()));
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
}
use of org.folio.rest.persist.Criteria.Criteria in project raml-module-builder by folio-org.
the class PostgresClientTransactionsIT method deleteTransaction.
private void deleteTransaction(TestContext context, String schema) {
PostgresClient c1 = PostgresClient.getInstance(vertx, TENANT);
Async async = context.async();
// create connection
c1.startTx(handler -> {
if (handler.succeeded()) {
Criteria c = new Criteria();
c.addField("'id'").setOperation(Criteria.OP_EQUAL).setValue("2");
c1.delete(handler, "z", new Criterion(c), reply -> {
if (reply.succeeded()) {
// make sure record is deleted when querying using this connection
// but since not committed yet we should still get it back
// when sending the query on a different connection
c1.get(handler, "z", SimplePojo.class, new Criterion(c), true, true, reply2 -> {
if (!reply2.succeeded()) {
context.fail(reply2.cause());
async.complete();
} else {
try {
int size = reply2.result().getResults().size();
context.assertEquals(0, size);
// call get() without the connection. since we did not commit yet
// this should still return the deleted record
c1.get("z", SimplePojo.class, new Criterion(c), true, true, reply3 -> {
if (!reply3.succeeded()) {
context.fail(reply3.cause());
async.complete();
} else {
int size2 = reply3.result().getResults().size();
context.assertEquals(1, size2);
// end transaction / commit
// doesnt seem like a good idea to reuse the handler within the get()
// which lives outside of this connection, but for testing ok.
c1.endTx(handler, done -> {
if (done.succeeded()) {
// record should have been deleted, so only one record should return
// not both
c1.select("SELECT jsonb FROM " + schema + ".z;", selectReply -> {
if (!selectReply.succeeded()) {
context.fail(selectReply.cause());
async.complete();
} else {
try {
int size3 = selectReply.result().getResults().size();
context.assertEquals(1, size3);
} catch (Exception e) {
e.printStackTrace();
context.fail(e.getMessage());
}
async.complete();
}
});
} else {
context.fail(done.cause());
}
});
}
});
} catch (Exception e) {
e.printStackTrace();
context.fail(e.getMessage());
async.complete();
}
}
});
} else {
context.fail(reply.cause());
}
});
} else {
context.fail(handler.cause());
}
});
async.await();
c1.closeClient(context.asyncAssertSuccess());
}
use of org.folio.rest.persist.Criteria.Criteria in project raml-module-builder by folio-org.
the class CriteriaTest method testCriteria.
@Test
public void testCriteria() {
try {
// pass schema so that type is known
Criteria schema = new Criteria("userdata.json");
schema.addField("'personal'").addField("'lastName'").setOperation("=").setValue("123");
assertEquals("(jsonb->'personal'->>'lastName') = '123'", schema.toString());
// pass schema so that type is known
schema = new Criteria("userdata.json");
schema.addField("'active'").setOperation("=").setValue("true");
assertEquals("(jsonb->>'active')::boolean = true", schema.toString());
// guess op type is numeric
schema = new Criteria();
schema.addField("'personal'").addField("'lastName'").setOperation("=").setValue("123");
assertEquals("(jsonb->'personal'->>'lastName')::numeric = 123", schema.toString());
// guess op type is string since not null, numeric or boolean
Criteria c = new Criteria();
c.addField("'price'").addField("'po_currency'").addField("'value'");
c.setOperation("like");
c.setValue("USD");
assertEquals("(jsonb->'price'->'po_currency'->>'value') like 'USD'", c.toString().trim());
// guess op type is boolean by checking operation
Criteria d = new Criteria();
d.addField("'rush'");
d.setOperation(Criteria.OP_IS_FALSE);
d.setValue(null);
assertEquals("(jsonb->>'rush')::boolean IS FALSE", d.toString().trim());
// guess op type is boolean by checking value
Criteria aa = new Criteria();
aa.addField("'rush'");
aa.setOperation("!=");
aa.setValue("true");
assertEquals("(jsonb->>'rush')::boolean != true", aa.toString().trim());
Criteria nb = new Criteria();
nb.addField("'transaction'");
nb.addField("'status'");
nb.setOperation("=");
nb.setValue("rollbackComplete");
nb.setArray(true);
assertEquals("(transaction->'status') = 'rollbackComplete'", nb.toString());
assertEquals("transaction", nb.getSelect().getSnippet());
assertEquals("jsonb_array_elements(jsonb->'transaction')", nb.getFrom().getSnippet());
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations