protected void applyOrderBy(CriteriaQuery query, OrderBy orderBy) { OrderBy.Item[] items = orderBy.items(); StringBuilder s = new StringBuilder(); for (int i = 0; i < items.length; i++) { if (i > 0) { s.append(','); } OrderBy.Item item = items[i]; String name = item.name(); MApiProperty ap = am.tryGetProperty(name); if (null == ap) { throw new BadRequestException("Property '" + name + "' not exists in model '" + am.getName() + "'"); } if (ap.isNotSortableExplicitly()) { throw new BadRequestException("Property '" + name + "' is not sortable!"); } if (Strings.isNotEmpty(query.alias())) { s.append(query.alias() + "." + name); } else { s.append(name); } if (!item.isAscending()) { s.append(" desc"); } } query.orderBy(s.toString()); }
@Override public Object apply(ActionParams params) { MApiModel am = am(); Object id = id(params); Map<String, Object> record = recordWithId(params); ModelExecutorContext context = new SimpleModelExecutorContext(api, dao, am, em, params); ModelUpdateExecutor executor = newUpdateExecutor(context); UpdateOneResult result = executor.partialUpdateOne(id, record); if (null != result.entity) { return ApiResponse.of(result.entity); } if (result.affectedRows > 0) { return ApiResponse.NO_CONTENT; } else { throw new NotFoundException(am.getName() + "' " + id.toString() + "' not found"); } }
@Override public Object apply(ActionParams params) { MApiModel am = am(); Object id = id(params); Map<String, Object> record = recordWithId(params); ModelExecutorContext context = new SimpleModelExecutorContext(api, dao, am, em, params); ModelUpdateExecutor executor = newUpdateExecutor(context); UpdateOneResult result = executor.replaceUpdateOne(id, record); if (null != result.entity) { return ApiResponse.of(result.entity); } if (result.affectedRows > 0) { return ApiResponse.NO_CONTENT; } else { throw new NotFoundException(am.getName() + "' " + id.toString() + "' not found"); } }
protected void checkProperty(ModelAndProp modelAndProp, String name) { boolean joined = modelAndProp.model != this.am; String modelDesc = (joined ? "joined " : "") + "model '" + modelAndProp.model.getName() + "'"; if (null == modelAndProp.property) { throw new BadRequestException("Property '" + name + "' not exists in " + modelDesc); } if (null == modelAndProp.field) { throw new BadRequestException("No mapping field '" + name + "' in " + modelDesc); } MApiProperty ap = modelAndProp.property; if (ap.isNotFilterableExplicitly()) { throw new BadRequestException("Property '" + name + "' is not filterable in " + modelDesc); } if (ap.isReference()) { throw new BadRequestException("Relation Property '" + name + "' is not filterable in " + modelDesc); } }
@Override public Object apply(ActionParams params) { MApiModel am = am(); ModelExecutorContext context = new SimpleModelExecutorContext(api, dao, am, em, params); ModelQueryExecutor executor = newQueryExecutor(context); Object id = id(params); QueryOptionsBase options = getWithId(params, 0); QueryOneResult result = executor.queryOne(id, options); if (null != result.getEntity()) { return ApiResponse.of(result.getEntity()); } else if (result.getRecord() == null) { throw new NotFoundException(am.getName() + " '" + id.toString() + "' not found"); } else { return ApiResponse.of(result.getRecord()); } }
@Override public Object apply(ActionParams params) { MApiModel am = api.getMetadata().getModel(model.getName()); ModelExecutorContext context = new SimpleModelExecutorContext(api, dao, am, em, params); ModelDeleteExecutor executor = newDeleteExecutor(context); Object id = id(params); DeleteOptions options = cascadeDelete ? getWithId(params, 0) : null; if (!cascadeDelete) { Request request = Request.tryGetCurrent(); String param = request.getParameter("cascade_delete"); if (!Strings.isEmpty(param) && Converts.toBoolean(param)) { throw new BadRequestException("Cascade delete not supported by this operation, check parameter 'cascade_delete'!"); } } DeleteOneResult result = executor.deleteOne(id, options); if (null != result.entity) { return ApiResponse.of(result.entity); } else { if (result.success) { return ApiResponse.NO_CONTENT; } else { throw new NotFoundException(am.getName() + " '" + id.toString() + "' not found"); } } }
throw new BadRequestException("No relation '" + join.getRelation() + "' in model '" + am.getName() + " or the relation is not joinable");