@Test
  public void testJPAEntityFieldInfoAnnotations() {
    JPAResponderGen rg = new JPAResponderGen();

    EntityMetadata mdEntity = new EntityMetadata("Flight");
    Vocabulary voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    voc.setTerm(new TermIdField(true));
    mdEntity.setPropertyVocabulary("ID", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.TIMESTAMP));
    mdEntity.setPropertyVocabulary("departureDT", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.DATE));
    mdEntity.setPropertyVocabulary("departureDate", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.TIME));
    mdEntity.setPropertyVocabulary("departureTime", voc);
    EntityInfo p = rg.createEntityInfoFromEntityMetadata("AirlineModel", mdEntity);

    // Annotations
    FieldInfo dateOnlyFI = p.getFieldInfos().get(0);
    assertEquals(1, dateOnlyFI.getAnnotations().size());
    assertEquals("@Temporal(TemporalType.DATE)", dateOnlyFI.getAnnotations().get(0));

    FieldInfo timeFI = p.getFieldInfos().get(1);
    assertEquals(1, timeFI.getAnnotations().size());
    assertEquals("@Temporal(TemporalType.TIME)", timeFI.getAnnotations().get(0));

    FieldInfo dateFI = p.getFieldInfos().get(2);
    assertEquals(1, dateFI.getAnnotations().size());
    assertEquals("@Temporal(TemporalType.TIMESTAMP)", dateFI.getAnnotations().get(0));
  }
 /**
  * Method to prepare Complex type representation.
  *
  * @param writer
  * @param entityMetadata
  * @param propertyName
  * @param propertiesList
  * @param modelName
  */
 private void writePropertyComplexList(
     StreamWriter writer,
     EntityMetadata entityMetadata,
     EntityProperty property,
     String modelName) {
   @SuppressWarnings("unchecked")
   List<EntityProperties> propertiesList = (List<EntityProperties>) property.getValue();
   String name = entityMetadata.getEntityName() + "_" + property.getName();
   int parseCount = 0;
   for (EntityProperties properties : propertiesList) {
     String fqTypeName = modelName + Metadata.MODEL_SUFFIX + "." + name;
     // We should be able to differentiate List<ComplexType> with regular ComplexType
     if (entityMetadata.isPropertyList(property.getFullyQualifiedName())) {
       if (parseCount == 0) {
         writer.startElement(new QName(d, name, "d"));
         writer.writeAttribute(new QName(m, "type", "m"), "Bag(" + fqTypeName + ")");
         writer.startElement(new QName(d, "element", "d"));
         parseCount++;
       } else {
         writer.startElement(new QName(d, "element", "d"));
       }
     } else {
       writer.startElement(new QName(d, name, "d"));
       writer.writeAttribute(new QName(m, "type", "m"), fqTypeName);
     }
     writeProperties(writer, entityMetadata, properties, modelName);
     writer.endElement();
   }
   // For List<ComplexTypes> we should end the complex node here
   if (!propertiesList.isEmpty()
       && entityMetadata.isPropertyList(property.getFullyQualifiedName())) {
     writer.endElement();
   }
 }
 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();
 }
Example #4
0
 /*
  * Create a EntityModel object from a Metadata container
  */
 private EntityModel createEntityModelFromMetadata(String modelName, Metadata metadata) {
   // Create the entity model
   EntityModel entityModel = new EntityModel(modelName);
   for (EntityMetadata entityMetadata : metadata.getEntitiesMetadata().values()) {
     EMEntity emEntity = new EMEntity(entityMetadata.getEntityName());
     for (String fyllyQualifiedPropertyName : entityMetadata.getPropertyVocabularyKeySet()) {
       addProperties(entityMetadata, emEntity, fyllyQualifiedPropertyName);
     }
     entityModel.addEntity(emEntity);
   }
   return entityModel;
 }
  @Test
  public void testJPAEntityKeyInfo() {
    JPAResponderGen rg = new JPAResponderGen();

    EntityMetadata mdEntity = new EntityMetadata("Flight");
    Vocabulary voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    voc.setTerm(new TermIdField(true));
    mdEntity.setPropertyVocabulary("flightID", voc);
    EntityInfo p = rg.createEntityInfoFromEntityMetadata("AirlineModel", mdEntity);

    assertEquals("flightID", p.getKeyInfo().getName());
    assertEquals("Long", p.getKeyInfo().getType());
    assertEquals(0, p.getFieldInfos().size());
  }
  @Test
  public void testEntityInfo() {
    JPAResponderGen rg = new JPAResponderGen();

    EntityMetadata mdEntity = new EntityMetadata("Flight");
    Vocabulary voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    voc.setTerm(new TermIdField(true));
    mdEntity.setPropertyVocabulary("ID", voc);
    EntityInfo p = rg.createEntityInfoFromEntityMetadata("AirlineModel", mdEntity);

    assertEquals("Flight", p.getClazz());
    assertEquals("AirlineModel", p.getPackage());
    assertEquals("AirlineModel.Flight", p.getFQTypeName());
  }
Example #7
0
 // adds the child properties to the top most property
 private void addChildProperties(
     EntityMetadata entityMetadata, EMProperty parentProperty, List<String> childPropertyNames) {
   String fullyQualifiedPropertyName = parentProperty.getName();
   for (String childPropertyName : childPropertyNames) {
     fullyQualifiedPropertyName = fullyQualifiedPropertyName + "." + childPropertyName;
     EMProperty childProperty = null;
     if (parentProperty.hasChildProperty(childPropertyName)) {
       // this property already added to the parent property
       childProperty = parentProperty.getChildProperty(childPropertyName);
     } else {
       // this is a new child property so create and add to the parent property
       childProperty = new EMProperty(childPropertyName);
       parentProperty.addChildProperty(childProperty);
       Vocabulary propertyVocabulary =
           entityMetadata.getPropertyVocabulary(fullyQualifiedPropertyName);
       for (Term vocTerm : propertyVocabulary.getTerms()) {
         if (TermComplexGroup.TERM_NAME.equals(vocTerm.getName())) {
           // complex group term not added as the properties are built in tree structure
           continue;
         }
         childProperty.addVocabularyTerm(new EMTerm(vocTerm.getName(), vocTerm.getValue()));
       }
     }
     parentProperty = childProperty;
   }
 }
  @Test
  public void testJPAEntityFieldInfo() {
    JPAResponderGen rg = new JPAResponderGen();

    EntityMetadata mdEntity = new EntityMetadata("Flight");
    Vocabulary voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    voc.setTerm(new TermIdField(true));
    mdEntity.setPropertyVocabulary("ID", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    mdEntity.setPropertyVocabulary("flightID", voc);
    mdEntity.setPropertyVocabulary("number", new Vocabulary());
    mdEntity.setPropertyVocabulary("runway", new Vocabulary());
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.TIMESTAMP));
    mdEntity.setPropertyVocabulary("departureDT", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.DATE));
    mdEntity.setPropertyVocabulary("departureDate", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.TIME));
    mdEntity.setPropertyVocabulary("departureTime", voc);
    EntityInfo p = rg.createEntityInfoFromEntityMetadata("AirlineModel", mdEntity);

    assertEquals(6, p.getFieldInfos().size());
    assertTrue(p.getFieldInfos().contains(new FieldInfo("flightID", "Long", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("number", "String", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("runway", "String", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("departureDT", "java.util.Date", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("departureDate", "java.util.Date", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("departureTime", "java.util.Date", null)));
  }
Example #9
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;
 }
Example #11
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);
  }
Example #12
0
 // adds the property tree to the entity metadata
 private void addProperties(
     EntityMetadata entityMetadata, EMEntity entity, String fyllyQualifiedPropertyName) {
   List<String> propertyNames =
       new ArrayList<String>(Arrays.asList(fyllyQualifiedPropertyName.split("\\.")));
   String rootPropertyName = propertyNames.get(0);
   EMProperty rootProperty = null;
   if (entity.contains(rootPropertyName)) {
     // this property already added to the entity
     rootProperty = entity.getProperty(rootPropertyName);
   } else {
     // new property so create and add to the entity
     rootProperty = new EMProperty(rootPropertyName);
     entity.addProperty(rootProperty);
     Vocabulary vocabulary = entityMetadata.getPropertyVocabulary(rootPropertyName);
     for (Term term : vocabulary.getTerms()) {
       rootProperty.addVocabularyTerm(new EMTerm(term.getName(), term.getValue()));
     }
   }
   propertyNames.remove(0); // removes root property as it's already added to the entity
   addChildProperties(entityMetadata, rootProperty, propertyNames);
 }
  private void writeProperties(
      StreamWriter writer,
      EntityMetadata entityMetadata,
      EntityProperties entityProperties,
      String modelName) {
    assert (entityMetadata != null);
    // Loop round all properties writing out fields and MV and SV sets
    Map<String, EntityProperty> properties = entityProperties.getProperties();

    for (String propertyName : properties.keySet()) {
      // Work out what the property looks like by looking at the metadata
      EntityProperty property = (EntityProperty) properties.get(propertyName);
      boolean isComplex = entityMetadata.isPropertyComplex(property.getFullyQualifiedName());
      if (!isComplex) {
        // Simple field
        writeProperty(writer, entityMetadata, property);
      } else {
        // Complex List
        writePropertyComplexList(writer, entityMetadata, property, modelName);
      }
    }
  }
Example #14
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;
  }