public List<Entity> getLatestRequirementVersionsByProject(Key projectKey) {
   Query query =
       QueryBuilder.buildAncestorQuery(
           RequirementVersion.ENTITY_NAME,
           CompositeFilterOperator.AND,
           new HashMap<String, Object>(),
           projectKey);
   ArtifactVersionEntityManager artifactVersionEntityManager = new ArtifactVersionEntityManager();
   ArtifactEntityManager artifactEntityManager = new ArtifactEntityManager();
   List<Entity> allRequirementVersions =
       datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
   Map<Key, List<Entity>> requirementVersionsByArtifact = new HashMap<Key, List<Entity>>();
   for (Entity requirementVersion : allRequirementVersions) {
     Entity artifactVersion = artifactVersionEntityManager.find(requirementVersion.getParent());
     Key artifactKey = artifactVersion.getParent();
     if (requirementVersionsByArtifact.containsKey(artifactKey)) {
       requirementVersionsByArtifact.get(artifactKey).add(requirementVersion);
     } else {
       List<Entity> requirementVersionsList = new ArrayList<Entity>();
       requirementVersionsList.add(requirementVersion);
       requirementVersionsByArtifact.put(artifactKey, requirementVersionsList);
     }
   }
   List<ComparableRequirement> latestRequirementVersionsByArtifact =
       new ArrayList<ComparableRequirement>();
   for (Key artifactKey : requirementVersionsByArtifact.keySet()) {
     Integer version = -1;
     Entity candidate = null;
     Integer number = -1;
     for (Entity requirement : requirementVersionsByArtifact.get(artifactKey)) {
       Entity artifactVersion = artifactVersionEntityManager.find(requirement.getParent());
       Entity artifact = artifactEntityManager.find(artifactVersion.getParent());
       if (((Integer) artifactVersion.getProperty(ArtifactVersion.VERSION_NUMBER)) > version) {
         candidate = requirement;
         number = (Integer) artifact.getProperty(Artifact.NUMBER);
       }
     }
     latestRequirementVersionsByArtifact.add(new ComparableRequirement(candidate, number));
   }
   Collections.sort(latestRequirementVersionsByArtifact);
   List<Entity> correctList = new ArrayList<Entity>();
   for (ComparableRequirement cr : latestRequirementVersionsByArtifact) {
     correctList.add(cr.requirementVersion);
   }
   return correctList;
 }
  public Key persistRequirementOfExistingArtifact(
      Key artifactKey, String editorId, String editorEmail, String editedTitle, Text text) {
    ArtifactVersionEntityManager artifactVersionEntityManager = new ArtifactVersionEntityManager();

    Map<String, Object> requirementVersionProperties = new HashMap<String, Object>();
    requirementVersionProperties.put(RequirementVersion.TEXT, text);

    LogEntityManager logEntityManager = new LogEntityManager();
    Key key;
    Transaction txn = datastore.beginTransaction();
    try {
      Entity artifact = this.find(artifactKey);
      Entity project = this.find(artifact.getParent());
      logEntityManager.persist(
          project.getKey(),
          new Date(),
          "Usu�rio "
              + artifact.getProperty(Artifact.CREATOR_ID)
              + " alterou  o requisito "
              + artifact.getProperty(Artifact.NUMBER)
              + ": "
              + editedTitle
              + " ao projeto "
              + project.getProperty(Project.PROJECT_NAME));
      Key artifactVersionKey =
          artifactVersionEntityManager.persistVersionOfExistingArtifact(
              artifactKey, editorId, editorEmail, ArtifactOperation.EDIT, editedTitle, new Date());
      key =
          super.persist(
              RequirementVersion.ENTITY_NAME, requirementVersionProperties, artifactVersionKey);
      txn.commit();
      return key;
    } finally {
      if (txn.isActive()) {
        txn.rollback();
        throw new TransactionException();
      }
    }
  }
  public Key persistRequirementOfNewArtifact(
      Key projectKey, String editorId, String editorEmail, String artifactTitle, Text text) {
    ArtifactVersionEntityManager artifactVersionEntityManager = new ArtifactVersionEntityManager();

    Map<String, Object> requirementVersionProperties = new HashMap<String, Object>();
    requirementVersionProperties.put(RequirementVersion.TEXT, text);

    LogEntityManager logEntityManager = new LogEntityManager();
    Key key;
    Transaction txn = datastore.beginTransaction();
    try {
      Entity project = this.find(projectKey);
      int number = this.getLatestRequirementVersionsByProject(projectKey).size() + 1;
      Key artifactVersionKey =
          artifactVersionEntityManager.persistVersionOfNewArtifact(
              projectKey, editorId, editorEmail, artifactTitle, new Date(), number);
      key =
          super.persist(
              RequirementVersion.ENTITY_NAME, requirementVersionProperties, artifactVersionKey);
      logEntityManager.persist(
          project.getKey(),
          new Date(),
          "Usu�rio "
              + editorEmail
              + " adicionou o requisito  "
              + number
              + ": "
              + artifactTitle
              + " ao projeto "
              + project.getProperty(Project.PROJECT_NAME));
      txn.commit();
      return key;
    } finally {
      if (txn.isActive()) {
        txn.rollback();
        throw new TransactionException();
      }
    }
  }