private void writeProperty(
     StreamWriter writer, EntityMetadata entityMetadata, EntityProperty property) {
   String elementText = entityMetadata.getPropertyValueAsString(property);
   writer.startElement(new QName(d, property.getName(), "d"));
   EdmType type =
       MetadataOData4j.termValueToEdmType(
           entityMetadata.getTermValue(property.getFullyQualifiedName(), TermValueType.TERM_NAME));
   boolean isNullable = entityMetadata.isPropertyNullable(property.getFullyQualifiedName());
   // Append Type Attribute
   if (!type.equals(EdmSimpleType.STRING)) {
     writer.writeAttribute(new QName(m, "type", "m"), type.getFullyQualifiedTypeName());
   }
   // Append Null attribute
   if (isNullable && (elementText.isEmpty()) && !type.equals(EdmSimpleType.STRING)) {
     writer.writeAttribute(new QName(m, "null", "m"), "true");
   }
   // Write the property text
   if (type.equals(EdmSimpleType.DATETIME) && !elementText.isEmpty()) {
     // Write dates in UTC format
     SimpleDateFormat formatUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
     formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
     writer.writeElementText(formatUTC.format((Date) property.getValue()));
   } else if (elementText != null) {
     writer.writeElementText(elementText);
   }
   writer.endElement();
 }
Ejemplo n.º 2
0
  public EntityInfo createEntityInfoFromEntityMetadata(
      String namespace, EntityMetadata entityMetadata) {
    FieldInfo keyInfo = null;
    List<FieldInfo> properties = new ArrayList<FieldInfo>();
    for (String propertyName : entityMetadata.getPropertyVocabularyKeySet()) {
      String type = entityMetadata.getTermValue(propertyName, TermValueType.TERM_NAME);
      if (entityMetadata.getTermValue(propertyName, TermIdField.TERM_NAME).equals("true")) {
        keyInfo =
            new FieldInfo(propertyName, javaType(MetadataOData4j.termValueToEdmType(type)), null);
      } else {
        List<String> annotations = new ArrayList<String>();
        if (type.equals(TermValueType.TIMESTAMP)) {
          annotations.add("@Temporal(TemporalType.TIMESTAMP)");
        } else if (type.equals(TermValueType.DATE)) {
          annotations.add("@Temporal(TemporalType.DATE)");
        } else if (type.equals(TermValueType.TIME)) {
          annotations.add("@Temporal(TemporalType.TIME)");
        }
        FieldInfo field =
            new FieldInfo(
                propertyName, javaType(MetadataOData4j.termValueToEdmType(type)), annotations);
        properties.add(field);
      }
    }

    // Check if user has specified the name of the JPA entities
    String jpaNamespace = System.getProperty("jpaNamespace");
    boolean isJpaEntity = (jpaNamespace == null || jpaNamespace.equals(namespace));
    return new EntityInfo(
        entityMetadata.getEntityName(),
        namespace,
        keyInfo,
        properties,
        new ArrayList<JoinInfo>(),
        isJpaEntity);
  }
Ejemplo n.º 3
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;
  }
Ejemplo n.º 4
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;
  }