@Override public MApiModel build() { MApiModel m = new MApiModel(entity, baseName, name, title, summary, description, javaTypes.toArray(Arrays2.EMPTY_CLASS_ARRAY), Builders.buildArray(properties.values(), new MApiProperty[properties.size()]), attrs, extension); m.getExtensions().putAll(this.extensions); return m; }
protected void writeModelWithinObject(WriteContext context, ApiMetadata m, JsonWriter w, MApiModel model) { if(!model.hasBaseModel()) { w.property(TYPE, OBJECT); } if(model.isEntity()) { w.property(X_ENTITY, true); } writeExtension(w, model.getExtension()); w.propertyOptional(TITLE, model.getTitle()); w.propertyOptional(SUMMARY, model.getSummary()); w.propertyOptional(DESCRIPTION, model.getDescription()); if(!model.hasBaseModel()) { writeModelProperties(context, m, w, model); }else{ w.property(ALL_OF, () -> { w.startArray(); //item1 : the base model. w.startObject() .property(REF, ref(model.getBaseName())) .endObject(); w.separator(); //item2 : self w.startObject() .property(TYPE, OBJECT); writeModelProperties(context, m, w, model); w.endObject(); w.endArray(); }); } }
/** * Returns the model with the given class. */ public MApiModel tryGetModel(Class<?> type) { for (MApiModel m : models.values()) { for (Class<?> javaType : m.getJavaTypes()) { if (type.equals(javaType)) { return m; } } } return null; }
@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"); } }
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()); }
for (MApiProperty p : am.getProperties()) { if (p.isReference()) { continue; for (String name : names) { if (name.equals("*")) { for (MApiProperty p : am.getProperties()) { if (p.isReference()) { continue; MApiProperty p = join.model.tryGetProperty(joinField); if (null == p) { throw new BadRequestException("Join property '" + name + "' not exists, check the 'select' query param"); MApiProperty p = am.tryGetProperty(name); if (null == p) { throw new BadRequestException("Property '" + name + "' not exists, check the 'select' query param");
protected void writeModelProperties(WriteContext context, ApiMetadata m, JsonWriter w, MApiModel model) { for(MApiProperty p : model.getProperties()) { if(p.isDiscriminator()) { w.property(DISCRIMINATOR, p.getName()); for(MApiProperty p : model.getProperties()) { if(isRequired(p)) { requiredProperties.add(p.getName()); w.startObject(); for(MApiProperty p : model.getProperties()) { w.property(propertyName(p.getName()), () -> { w.startObject();
@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");