Exemplo n.º 1
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);
  }
Exemplo n.º 2
0
 public static void main(String[] args) throws IOException {
   FileInputStream input = new FileInputStream(args[0]);
   byte[] buf = new byte[input.available()];
   input.read(buf);
   input.close();
   JSONObject model = new JSONObject(new String(buf, "UTF-8"));
   DbContext.newContext(model, new File(args[1]), null, null).render();
 }
Exemplo n.º 3
0
  public static DbContext newContext(
      JSONObject model, File outputDir, String defaultPackage, String authority)
      throws IOException {
    EntityModelRegistry.clear();
    DbContext ctx =
        new DbContext(
            model.getString("defaultPackage"),
            authority == null ? model.getString("authority") : authority,
            model.getString("databaseName"),
            outputDir);

    JSONArray versions = model.getJSONArray("versions");
    for (int i = 0; i < versions.length(); i++) {
      JSONObject version = versions.getJSONObject(i);
      JSONArray entities = version.getJSONArray("entities");

      for (int e = 0; e < entities.length(); e++) {
        JSONObject entity = entities.getJSONObject(e);
        processEntity(entity, ctx, ctx.currentVersion());
      }

      if (i + 1 < versions.length()) {
        version = versions.getJSONObject(i + 1);
        ctx.newVersion(
            version.has("type")
                ? Version.VersionType.valueOf(version.getString("type"))
                : Version.VersionType.DIFF);
      }
    }

    for (int i = 0; i < versions.length(); i++) {
      JSONObject version = versions.getJSONObject(i);
      JSONObject defaultQueries = version.has("queries") ? version.getJSONObject("queries") : null;
      JSONArray entities = version.getJSONArray("entities");

      for (int e = 0; e < entities.length(); e++) {
        JSONObject entity = entities.getJSONObject(e);
        EntityModel entityModel = ctx.getModel(entity.getString("name"));
        processQueries(defaultQueries, entityModel, ctx.currentVersion());
      }
    }

    return ctx;
  }
Exemplo n.º 4
0
  static EntityModel processEntity(JSONObject entity, DbContext ctx, Version version) {
    if (entity.has("rootModel")) { // if there is a rootModel property, we're dealing with a view
      processEntityView(entity, ctx);
    } else { // otherwise an entity table
      EntityModelVersion v =
          ctx.currentVersion()
              .newModel(
                  entity.getString("name"), !entity.has("render") || entity.getBoolean("render"))
              .currentVersion();
      if (entity.has("properties")) {
        JSONArray properties = entity.getJSONArray("properties");
        for (int p = 0; p < properties.length(); p++) {
          JSONObject property = properties.getJSONObject(p);
          v.newProperty(
              property.has("type")
                  ? EntityProperty.Type.valueOf(property.getString("type"))
                  : EntityProperty.Type.UNKNOWN,
              property.getString("name"),
              property.has("nullable") && property.getBoolean("nullable"));
        }
      }
    }

    EntityModel m = ctx.getModel(entity.getString("name"));
    if (m == null)
      throw new NullPointerException("Model " + entity.getString("name") + " not found");
    if (entity.has("extensions")) {
      JSONArray extensions = entity.getJSONArray("extensions");
      for (int p = 0; p < extensions.length(); p++) {
        m.getExtensions().add(extensions.getString(p));
      }
    }

    if (entity.has("queries")) processQueries(entity.getJSONObject("queries"), m, version);

    if (entity.has("indexes")) {
      JSONArray indexes = entity.getJSONArray("indexes");
      for (int i = 0; i < indexes.length(); i++) {
        JSONObject index = indexes.getJSONObject(i);

        List<EntityIndexProperty> properties = new ArrayList<EntityIndexProperty>();

        if (index.has("properties")) {
          JSONArray indexProperties = index.getJSONArray("properties");
          for (int p = 0; p < indexProperties.length(); p++) {
            Object prop = indexProperties.get(p);
            EntityIndexProperty property = new EntityIndexProperty();
            if (prop instanceof String) {
              property.setProperty(m.getProperty(m.currentVersion().getVersion(), (String) prop));
              property.setOrder(EntityIndexProperty.Order.DEFAULT);
            } else {
              JSONObject o = (JSONObject) prop;
              property.setProperty(
                  m.getProperty(m.currentVersion().getVersion(), o.getString("name")));
              property.setOrder(
                  o.has("order")
                      ? EntityIndexProperty.Order.valueOf(index.getString("order"))
                      : EntityIndexProperty.Order.DEFAULT);
            }
            properties.add(property);
          }
        }

        m.currentVersion()
            .newIndex(
                index.has("type")
                    ? EntityIndex.Type.valueOf(index.getString("type"))
                    : EntityIndex.Type.DEFAULT,
                index.getString("name"),
                properties);
      }
    }

    return m;
  }