package com.ciandt.techgallery.service.util;

import com.googlecode.objectify.Ref;

import com.ciandt.techgallery.persistence.model.TechGalleryUser;
import com.ciandt.techgallery.persistence.model.TechnologyComment;
import com.ciandt.techgallery.service.model.TechnologyCommentTO;

import java.util.ArrayList;
import java.util.List;

/**
 * TechnologyCommentConverter methods.
 * 
 * @author Felipe Ibrahim
 *
 */
public class TechnologyCommentConverter {

  private TechGalleryUserTransformer tgUserTransformer = new TechGalleryUserTransformer();
  /**
   * Transform entity from datastore into response entity which is transient.
   * 
   * @param entity from datastore
   * @return transient entity
   */
  public static TechnologyCommentTO fromEntityToTransient(TechnologyComment entity) {
    TechnologyCommentTO commentTO = new TechnologyCommentTO();
    TechGalleryUserTransformer userTransformer = new TechGalleryUserTransformer();
    commentTO.setId(entity.getId());
    commentTO.setComment(entity.getComment());
    commentTO.setTechnologyId(entity.getTechnology().get().getId());
    commentTO.setCreation(entity.getTimestamp());
    commentTO.setAuthor(userTransformer.transformTo(entity.getAuthor().get()));

    return commentTO;
  }
  
  /**
   * Transform a list of entity from datastore into list of response entity which is transient.
   * 
   * @param list entity from datastore
   * @return list transient entity
   */
  public static List<TechnologyCommentTO> fromEntityToTransient(List<TechnologyComment> entities) {
    
    List<TechnologyCommentTO> commentsTO = new ArrayList<TechnologyCommentTO>();
    for (TechnologyComment entity : entities) {
      commentsTO.add(fromEntityToTransient(entity));
    }

    return commentsTO;
  }

  /**
   * Transform entity from response which is transient into datastore entity which can be persisted.
   * 
   * @param transient entity
   * @return entity from datastore
   */
  public TechnologyComment fromTransientToEntity(TechnologyCommentTO tranzient) {
    TechnologyComment commentEntity = new TechnologyComment();
    commentEntity.setId(tranzient.getId());
    commentEntity.setComment(tranzient.getComment());
    //commentEntity.setTechnology(tranzient.getTechnologyId());
    TechGalleryUser author = tgUserTransformer.transformFrom(tranzient.getAuthor());
    Ref<TechGalleryUser> ref = Ref.create(author);
    commentEntity.setAuthor(ref);
    return commentEntity;
  }
}

@@ -39,16 +39,78 @@
 .util;%0A%0A
+import com.google.api.server.spi.response.NotFoundException;%0A%0A
 import c
@@ -313,33 +313,215 @@
 ice.
-model.TechnologyCommentTO
+TechnologyService;%0Aimport com.ciandt.techgallery.service.TechnologyServiceImpl;%0Aimport com.ciandt.techgallery.service.model.TechnologyCommentTO;%0Aimport com.ciandt.techgallery.service.model.TechnologyResponse
 ;%0A%0Ai
@@ -694,16 +694,87 @@
 rter %7B%0A%0A
+  private TechnologyService techService = new TechnologyServiceImpl();%0A
   privat
@@ -856,16 +856,96 @@
 rmer();%0A
+  private TechnologyTransformer techTransformer = new TechnologyTransformer();%0A%0A
   /**%0A  
@@ -1638,18 +1638,16 @@
 TO;%0A  %7D%0A
-  
 %0A  /**%0A 
@@ -1927,20 +1927,16 @@
 ties) %7B%0A
-    
 %0A    Lis
@@ -2318,24 +2318,55 @@
 m datastore%0A
+   * @throws NotFoundException%0A
    */%0A  publ
@@ -2438,16 +2438,47 @@
 anzient)
+%0A      throws NotFoundException
  %7B%0A    T
@@ -2641,10 +2641,8 @@
     
-//
 comm
@@ -2665,16 +2665,113 @@
 hnology(
+Ref.create(techTransformer.transformFrom(%0A        (TechnologyResponse) techService.getTechnology(
 tranzien
@@ -2786,24 +2786,27 @@
 hnologyId())
+)))
 ;%0A    TechGa

Comment converter supports technology ref