Пример #1
0
  static void processProperties(
      JSONObject entity, ViewEntityModel viewModel, DbContext ctx, EntityModelVersion version) {
    JSONArray properties = entity.getJSONArray("properties");
    for (int p = 0; p < properties.length(); p++) {
      JSONObject property = properties.getJSONObject(p);
      EntityModel child;
      String alias;

      if (property.has("model")) {
        String model = property.getString("model");
        if (model.equals(viewModel.getRootModel().getName())) {
          child = viewModel.getRootModel();
          alias = viewModel.getRootAlias();
        } else {
          ViewEntityModel.EntityRelationship relationship =
              viewModel.getModel(property.getString("model"));
          child = relationship.getModel();
          alias = relationship.getAlias();
        }
      } else {
        child = viewModel;
        alias = viewModel.getRootAlias();
      }

      String propertyName = property.getString("name");
      if ("*".equals(propertyName)) {
        for (EntityProperty childProperty : child.getAllProperties(version.getVersion())) {
          String name = childProperty.getName();
          if (childProperty.getModel() instanceof ViewEntityModel
              && childProperty.getAlias() != null) name = childProperty.getAlias();
          EntityProperty cp =
              version.newProperty(
                  childProperty.getType(), name, childProperty.isNullable(), null, null);
          cp.setModelAlias(alias);
          cp.setOriginalModel(childProperty.getModel());
        }
      } else {
        EntityProperty childProperty = null;
        try {
          childProperty = child.getProperty(version.getVersion(), propertyName);
        } catch (IllegalArgumentException ignore) {
        }
        EntityProperty cp =
            version.newProperty(
                childProperty == null
                    ? EntityProperty.Type.valueOf(property.getString("type"))
                    : childProperty.getType(),
                property.getString("name"),
                !property.has("nullable") || property.getBoolean("nullable"),
                property.has("alias") ? property.getString("alias") : null,
                property.has("sql") ? property.getString("sql") : null);
        cp.setModelAlias(alias);
        if (childProperty != null) cp.setOriginalModel(childProperty.getModel());
      }
    }
  }
Пример #2
0
  static void processViewModel(
      DbContext ctx, JSONObject entity, ViewEntityModel viewModel, EntityModelVersion version) {
    processProperties(entity, viewModel, ctx, version);

    for (EntityProperty property : viewModel.getAllProperties(version.getVersion())) {
      if ("_id".equals(property.getName())
          && (property.getModelAlias() == null || property.getModel() == null)) {
        property.setModel(viewModel.getRootModel());
        property.setModelAlias(viewModel.getRootAlias());
      }
    }
  }
Пример #3
0
  static void processEntityView(JSONObject entity, DbContext ctx) {
    ViewEntityModel viewModel =
        ctx.currentVersion()
            .newViewModel(
                entity.getString("name"), !entity.has("render") || entity.getBoolean("render"));
    viewModel.setRootModel(ctx.getModel(entity.getString("rootModel")));
    viewModel.setRootAlias(entity.getString("rootAlias"));
    if (entity.has("groupBy")) viewModel.setGroupBy(entity.getString("groupBy"));

    EntityModelVersion v = viewModel.currentVersion();

    if (entity.has("models")) {
      JSONArray models = entity.getJSONArray("models");
      // add the models
      for (int m = 0; m < models.length(); m++) {
        JSONObject child = models.getJSONObject(m);
        EntityModel childModel = ctx.getModel(child.getString("name"));
        ViewEntityModel.Relationship relationship =
            ViewEntityModel.Relationship.valueOf(child.getString("type"));
        String alias = child.has("alias") ? child.getString("alias") : child.getString("name");
        viewModel.addModel(childModel, relationship, alias, child.getString("selection"));
      }
    }
    // process model properties
    processViewModel(ctx, entity, viewModel, v);
  }