Exemple #1
0
  public Map<String, Object> complexTypeHandler(String propertyName, EntityMetadata em) {

    Map<String, Object> map = new HashMap<>();

    // 1st level

    for (String property : em.getPropertyVocabularyKeySet()) {

      if (property.startsWith(propertyName) && !property.equals(propertyName)) {

        if (em.isPropertyList(property)) {
          // Complex

          Map<String, Object> subMap = complexTypeHandler(property, em);
          map.put(em.getSimplePropertyName(property), subMap);

        } else {
          // SIMPLE
          Pattern pattern = Pattern.compile("(?:" + propertyName + ")(?:\\.)(.*)");
          Matcher matcher = pattern.matcher(property);
          while (matcher.find()) {
            String propertyCapture = matcher.group(1);
            if (em.getSimplePropertyName(property).equals(propertyCapture)) {
              ArrayList<String> obj = new ArrayList<String>();
              if (em.isPropertyNumber(property)) {
                obj.add(0, "double");
              } else if (em.isPropertyDate(property)) {
                obj.add(0, "date");
              } else if (em.isPropertyBoolean(property)) {
                obj.add(0, "boolean");
              } else {
                obj.add(0, "string");
              }

              String description = em.getTermValue(property, "TERM_DESCRIPTION");
              description = (null != description) ? description : "";
              obj.add(1, description);

              map.put(em.getSimplePropertyName(property), obj);
            }
          }
        }
      }
    }

    return map;
  }
 private String getAbsoluteId(
     String baseUri, String entitySetName, Entity entity, EntityMetadata entityMetadata) {
   String absId = "";
   for (String key : entityMetadata.getIdFields()) {
     EntityProperty prop =
         entity.getProperties().getProperty(entityMetadata.getSimplePropertyName(key));
     if (prop != null) {
       absId +=
           absId.isEmpty()
               ? (!baseUri.endsWith("/") ? baseUri + "/" : baseUri) + entitySetName
               : ",";
       if (entityMetadata.isPropertyNumber(prop.getFullyQualifiedName())) {
         absId += "(" + entityMetadata.getPropertyValueAsString(prop) + ")";
       } else {
         absId += "('" + entityMetadata.getPropertyValueAsString(prop) + "')";
       }
     }
   }
   return absId;
 }
Exemple #3
0
  public boolean runGenerator(String inputPath, Metadata metadata, String outputPath) {

    // load the resource
    resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
    Resource resource = resourceSet.getResource(URI.createFileURI(inputPath), true);

    if (metadata != null) {
      Map<String, Object> entitiesMap = new HashMap<String, Object>();
      for (State key :
          Iterables.<State>filter(
              IteratorExtensions.<EObject>toIterable(resource.getAllContents()), State.class)) {
        String entity = key.getEntity().getName();
        if (StringUtils.isNotEmpty(entity)) {
          try {
            EntityMetadata em = metadata.getEntityMetadata(entity);
            if (null != em) {
              Map<String, Object> entityPropMap = new HashMap<String, Object>();
              for (String propertySimple : em.getTopLevelProperties()) {
                if (!em.isPropertyList(propertySimple)) {

                  ArrayList<String> obj = new ArrayList<String>();

                  String propertyName = em.getSimplePropertyName(propertySimple);

                  if (em.isPropertyNumber(propertySimple)) {
                    obj.add(0, "double");
                  } else if (em.isPropertyDate(propertySimple)) {
                    obj.add(0, "date");
                  } else if (em.isPropertyTime(propertySimple)) {
                    obj.add(0, "dateTime");
                  } else if (em.isPropertyBoolean(propertySimple)) {
                    obj.add(0, "boolean");
                  } else {
                    obj.add(0, "string");
                  }

                  String description = em.getTermValue(propertySimple, "TERM_DESCRIPTION");
                  description = (null != description) ? description : "";
                  obj.add(1, description);

                  entityPropMap.put(propertyName, obj);

                } else {
                  String propertyName = em.getSimplePropertyName(propertySimple);
                  entityPropMap.put(propertyName, complexTypeHandler(propertySimple, em));
                }
              }
              entitiesMap.put(entity, entityPropMap);
            }
          } catch (Exception e) {
            System.out.println("Entity Not found: " + entity);
          }
        }
      }
      resource.getResourceSet().getLoadOptions().put("Metadata", entitiesMap);
    }

    // validate the resource
    List<Issue> list = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
    if (!list.isEmpty()) {
      for (Issue issue : list) {
        listener.notify(issue.toString());
      }

      return false;
    }

    // configure and start the generator
    fileAccess.setOutputPath(outputPath);
    generator.doGenerate(resource, fileAccess);

    return true;
  }