Package com.getperka.flatpack.ext

Examples of com.getperka.flatpack.ext.EntityDescription


  @Override
  public Class<? extends HasUuid> readNotNull(JsonElement element, DeserializationContext context)
      throws Exception {
    String name = element.getAsString();
    EntityDescription entityDescription = typeContext.getEntityDescription(name);
    return entityDescription == null ? null : entityDescription.getEntityType();
  }
View Full Code Here


    STGroup group = new STGroupFile(getClass().getResource("js.stg"), "UTF8", '<', '>');
    group.registerRenderer(EntityDescription.class, new AttributeRenderer() {
      @Override
      public String toString(Object o, String formatString, Locale locale) {
        EntityDescription entity = (EntityDescription) o;
        if (entity.getTypeName().equals("baseHasUuid")) {
          return BaseHasUuid.class.getCanonicalName();
        }
        return entity.getTypeName();
      }
    });

    group.registerModelAdaptor(EntityDescription.class,
        new ObjectModelAdaptor() {
          @Override
          public Object getProperty(Interpreter interp, ST self, Object o,
              Object property, String propertyName)
              throws STNoSuchPropertyException {

            EntityDescription entity = (EntityDescription) o;
            if ("docString".equals(propertyName)) {
              return jsDocString(entity.getDocString());
            }

            else if ("canonicalName".equals(propertyName)) {
              return getPackageName(entity) + "." + upcase(entity.getTypeName());
            }

            else if ("supertype".equals(propertyName)) {
              EntityDescription supertype = entity.getSupertype();
              if (supertype == null) {
                supertype = baseHasUuid;
              }
              return supertype;
            }
View Full Code Here

  /**
   * Map a type name reference onto a real {@link Class} object via the {@link TypeContext}.
   */
  void resolveClass(Ident<Class<? extends HasUuid>> x) {
    EntityDescription desc = typeContext.getEntityDescription(x.getSimpleName());
    if (desc == null) {
      error("Unknown type " + x.getSimpleName());
      return;
    }
    x.setReferent(desc.getEntityType());
  }
View Full Code Here

        // data : { "fooEntity" : [ { ... }, { ... } ]
        reader.beginObject();
        while (JsonToken.NAME.equals(reader.peek())) {
          // Turn "fooEntity" into the actual FooEntity class
          String simpleName = reader.nextName();
          EntityDescription desc = typeContext.getEntityDescription(simpleName);
          if (desc == null) {
            if (ignoreUnresolvableTypes) {
              reader.skipValue();
              continue;
            } else {
              throw new UnsupportedOperationException("Cannot resolve type " + simpleName);
            }
          } else if (Modifier.isAbstract(desc.getEntityType().getModifiers())) {
            throw new UnsupportedOperationException("A subclass of " + simpleName
              + " must be used instead");
          }
          context.pushPath("allocating " + simpleName);
          try {
            // Find the Codex for the requested entity type
            EntityCodex<?> codex = (EntityCodex<?>) typeContext.getCodex(desc.getEntityType());

            // Take the n-many property objects and stash them for later decoding
            reader.beginArray();
            while (!JsonToken.END_ARRAY.equals(reader.peek())) {
              JsonObject chunk = jsonParser.parse(reader).getAsJsonObject();
View Full Code Here

    // EntityDescription are rendered as the FQN
    group.registerRenderer(EntityDescription.class, new AttributeRenderer() {

      @Override
      public String toString(Object o, String formatString, Locale locale) {
        EntityDescription entity = (EntityDescription) o;
        if (entity.getTypeName().equals("baseHasUuid")) {
          return BaseHasUuid.class.getCanonicalName();
        }
        return entity.getTypeName();
      }
    });

    group.registerModelAdaptor(ApiDescription.class,
        new ObjectModelAdaptor() {
          @Override
          public Object getProperty(Interpreter interp, ST self, Object o,
              Object property, String propertyName)
              throws STNoSuchPropertyException {
            ApiDescription apiDescription = (ApiDescription) o;
            if ("endpoints".equals(propertyName)) {
              List<EndpointDescription> sortedEndpoints = new ArrayList<EndpointDescription>(
                  apiDescription.getEndpoints());
              Collections.sort(sortedEndpoints, new Comparator<EndpointDescription>() {
                @Override
                public int compare(EndpointDescription e1, EndpointDescription e2) {
                  return e1.getPath().compareTo(e2.getPath());
                }
              });
              return sortedEndpoints;
            }
            else if ("importNames".equals(propertyName)) {
              Set<String> imports = new HashSet<String>();
              for (EndpointDescription e : apiDescription.getEndpoints()) {
                if (e.getEntity() != null) {
                  String type = objcTypeForType(e.getEntity());
                  if (isRequiredImport(type)) {
                    imports.add(type);
                  }
                }
                if (e.getReturnType() != null) {
                  String type = objcTypeForType(e.getReturnType());
                  if (isRequiredImport(type)) {
                    imports.add(type);
                  }
                }
              }
              List<String> sortedImports = new ArrayList<String>(imports);
              Collections.sort(sortedImports);
              return sortedImports;
            }
            return super.getProperty(interp, self, o, property, propertyName);
          }
        });

    group.registerModelAdaptor(EndpointDescription.class,
        new ObjectModelAdaptor() {
          @Override
          public Object getProperty(Interpreter interp, ST self, Object o,
              Object property, String propertyName)
              throws STNoSuchPropertyException {
            EndpointDescription end = (EndpointDescription) o;
            if ("docString".equals(propertyName)) {
              return doxygenDocString(end.getDocString());
            }
            else if ("methodName".equals(propertyName)) {
              StringBuilder sb = new StringBuilder();

              sb.append("- (" + getBuilderReturnType(end) + " *)");

              sb.append(getMethodizedPath(end));

              if (end.getEntity() != null) {
                if (end.getPathParameters() != null && end.getPathParameters().size() > 0) {
                  sb.append(" entity");
                }
                String type = objcTypeForType(end.getEntity());
                sb.append(":(" + type + " *)");
                String paramName = type;
                if (type.startsWith(classPrefix)) {
                  paramName = downcase(type.substring(classPrefix.length()));
                }
                sb.append(paramName);
              }

              return sb.toString();
            }

            else if ("requestBuilderClassName".equals(propertyName)) {
              return getBuilderReturnType(end);
            }

            else if ("requestBuilderBlockName".equals(propertyName)) {
              return getBuilderReturnType(end) + "Block";
            }

            else if ("entityReturnType".equals(propertyName)) {
              String type = objcFlatpackReturnType(end.getReturnType());
              return type.equals("void") ? null : type;
            }

            else if ("entityReturnName".equals(propertyName)) {
              if (end.getReturnType() == null) return null;

              String name = "result";
              if (end.getReturnType().getName() != null) {
                name = end.getReturnType().getName();
              }

              if (end.getReturnType().getListElement() != null) {
                if (end.getReturnType().getListElement().getName() != null) {
                  name = end.getReturnType().getListElement().getName();
                }
                name = pluralOf(name);
              }

              return name;
            }

            else if ("pathDecoded".equals(propertyName)) {
              // URL-decode the path in the endpoint description
              try {
                String decoded = URLDecoder.decode(end.getPath(), "UTF8");
                return decoded;
              } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
              }
            }
            return super.getProperty(interp, self, o, property, propertyName);
          }
        });

    group.registerModelAdaptor(EntityDescription.class,
        new ObjectModelAdaptor() {
          @Override
          public Object getProperty(Interpreter interp, ST self, Object o,
              Object property, String propertyName)
              throws STNoSuchPropertyException {

            EntityDescription entity = (EntityDescription) o;
            if ("importNames".equals(propertyName)) {
              Set<String> imports = new HashSet<String>();
              String type = requireNameForType(entity.getTypeName());
              if (isRequiredImport(type)) {
                imports.add(type);
              }
              for (Property p : entity.getProperties()) {
                String name = null;
                if (p.getType().getListElement() != null) {
                  name = objcTypeForType(p.getType().getListElement());
                }
                else {
                  name = objcTypeForProperty(p);
                }
                if (name != null && isRequiredImport(name)) {
                  imports.add(name);
                }
              }
              List<String> sortedImports = new ArrayList<String>(imports);
              Collections.sort(sortedImports);
              return sortedImports;
            }
            if ("docString".equals(propertyName)) {
              return doxygenDocString(entity.getDocString());
            }
            else if ("payloadName".equals(propertyName)) {
              return entity.getTypeName();
            }

            else if ("supertype".equals(propertyName)) {
              EntityDescription supertype = entity.getSupertype();
              if (supertype == null) {
                supertype = baseHasUuid;
              }
              return supertype;
            }
View Full Code Here

             */
            if (!keep && p.getImpliedProperty() != null) {
              keep = shouldKeep(p.getImpliedProperty().getGroupPermissions());
            }
          } else if (value instanceof EntityDescription) {
            EntityDescription e = (EntityDescription) value;
            if (e.getProperties().isEmpty()) {
              keep = false;
            } else if (!weakEntities.contains(e)
              && weakProperties.containsAll(e.getProperties())) {
              keep = false;
            } else {
              keep = true;
            }
          } else {
View Full Code Here

  /**
   * Filter entities that contain only properties defined by the given class.
   */
  public ApiDescriber ignoreEmptySubtypes(Class<? extends HasUuid> toIgnore) {
    EntityDescription describe = ctx.describe(toIgnore);
    weakEntities.add(describe);
    weakProperties.addAll(describe.getProperties());
    return this;
  }
View Full Code Here

    desc.setQueryParameters(queryParams.isEmpty() ? null : queryParams);
    return desc;
  }

  private EntityDescription describeEntity(Class<? extends HasUuid> clazz) throws IOException {
    EntityDescription toReturn = ctx.describe(clazz);
    if (!described.add(clazz)) {
      return toReturn;
    }

    // Attach interesting annotations
    toReturn.setDocAnnotations(extractInterestingAnnotations(clazz));

    // Attach the docstring
    Map<String, String> strings = getDocStrings(clazz);
    String docString = strings.get(clazz.getName());
    if (docString != null) {
      toReturn.setDocString(replaceLinks(docString));
    }

    // Iterate over the properties
    for (Iterator<Property> it = toReturn.getProperties().iterator(); it.hasNext();) {
      Property prop = it.next();
      propertiesToEntities.put(prop, toReturn);

      // Record a reference to (possibly) an entity type
      reference(prop.getType());
View Full Code Here

  /**
   * Traverse a type, looking for references to entities. This should be a visitor.
   */
  private void reference(Type type) {
    if (type.getName() != null && type.getEnumValues() == null) {
      EntityDescription description = ctx.getEntityDescription(type.getName());
      Class<? extends HasUuid> clazz = description.getEntityType();
      reference(clazz);
    }
    if (type.getListElement() != null) {
      reference(type.getListElement());
    }
View Full Code Here

    StringBuffer sb = new StringBuffer();
    Matcher m = linkPattern.matcher(docString);
    while (m.find()) {
      String name = m.group(1);
      // TODO: Support field references, API method references
      EntityDescription referenced = ctx.getEntityDescription(name);
      if (referenced == null) {
        // Just append the original text
        if (m.group(2) != null) {
          m.appendReplacement(sb, m.group(2));
        } else {
          m.appendReplacement(sb, m.group(1));
        }
      } else {
        /*
         * This is colluding with the viewer app, but it's much simpler than re-implementing another
         * {@link} replacement in the viewer.
         */
        String payloadName = referenced.getTypeName();
        String displayString = m.group(2) == null ? payloadName : m.group(2);
        m.appendReplacement(sb, "<entityReference payloadName='" + payloadName + "'>"
          + displayString + "</entityReference>");
      }
    }
View Full Code Here

TOP

Related Classes of com.getperka.flatpack.ext.EntityDescription

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.