package com.servinglynk.hmis.warehouse.service.converter; 

import java.time.ZoneId;
import java.util.Date;

import com.servinglynk.hmis.warehouse.core.model.Client;
import com.servinglynk.hmis.warehouse.core.model.Response;
import com.servinglynk.hmis.warehouse.model.ClientEntity;
import com.servinglynk.hmis.warehouse.model.QuestionEntity;
import com.servinglynk.hmis.warehouse.model.ResponseEntity;
public class ResponseConverterV3 {

   public static ResponseEntity modelToEntity (Response model ,ResponseEntity entity) {
       if(entity==null) entity = new ResponseEntity();
       entity.setId(model.getResponseId());
       entity.setRefused(model.isRefused());
       entity.setResponseText(model.getResponseText());
       entity.setPickListValueCode(model.getPickListValueCode());
       entity.setAppId(model.getAppId());
       entity.setHmisLink(model.getHmisLink());
       entity.setEffectiveDate(model.getEffectiveDate());
       if(model.getDedupClientId()!=null) entity.setDedupClientId(model.getDedupClientId());
//       entity.setClientId(model.getClientId());
       return entity;    
   }


   public static Response entityToModel (ResponseEntity entity, ClientEntity clientEntity) {
       Response model = new Response();
       model.setResponseId(entity.getId());
       model.setRefused(entity.isRefused());
       model.setResponseText(entity.getResponseText());
       model.setPickListValueCode(entity.getPickListValueCode());
       QuestionEntity questionEntity = entity.getQuestionEntity();
       if(questionEntity != null) {
    	   model.setQuestionId(questionEntity.getId());
       }
       model.setQuestionScore(entity.getQuestionScore());
       model.setAppId(entity.getAppId());
       if(entity.getSurveySectionEntity()!=null) model.setSectionId(entity.getSurveySectionEntity().getId());
       if(entity.getQuestionEntity()!=null) model.setQuestionId(entity.getQuestionEntity().getId());
       model.setClientId(clientEntity.getId());
       model.setSurveyId(entity.getSurveyEntity().getId());
       model.setSubmissionId(entity.getSubmissionId());
       model.setEffectiveDate(entity.getEffectiveDate());
       model.setDedupClientId(entity.getDedupClientId());
       model.setHmisLink(entity.getHmisLink());
       return model;
   }
   
   public static Response entityToModelDetail (ResponseEntity entity, ClientEntity clientEntity) {
       Response model = new Response();
       model.setResponseId(entity.getId());
       model.setRefused(entity.isRefused());
       model.setResponseText(entity.getResponseText());
       model.setPickListValueCode(entity.getPickListValueCode());
       model.setQuestionScore(entity.getQuestionScore());
       model.setAppId(entity.getAppId());
       if(entity.getSurveySectionEntity()!=null) model.setSectionId(entity.getSurveySectionEntity().getId());
       if(entity.getQuestionEntity()!=null) model.setQuestionId(entity.getQuestionEntity().getId());
       model.setClientId(clientEntity.getId());
       model.setSurveyId(entity.getSurveyEntity().getId());
       model.setSubmissionId(entity.getSubmissionId());
       model.setEffectiveDate(entity.getEffectiveDate());
       model.setDedupClientId(clientEntity.getDedupClientId());
       model.setHmisLink(entity.getHmisLink());
       if(entity.getClient()!=null) {
    	   Client client = new Client();
    	  if(clientEntity.getDob()!=null) client.setDob(Date.from(clientEntity.getDob().atZone(ZoneId.systemDefault()).toInstant()));
    	   client.setEmailAddress(clientEntity.getEmailAddress());
    	   client.setFirstName(clientEntity.getFirstName());
			client.setLastName(clientEntity.getLastName());
			client.setMiddleName(clientEntity.getMiddleName());
			client.setPhoneNumber(clientEntity.getPhoneNumber());
			client.setId(clientEntity.getId());
			model.setClient(client);
       }
       return model;
   }


}

@@ -102,16 +102,61 @@
 .Date;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A%0A
 import c
@@ -204,16 +204,16 @@
 Client;%0A
-
 import c
@@ -720,91 +720,244 @@
     
-entity.setResponseText(model.getResponseText());%0A       entity.setPickListValueCode
+if(StringUtils.isNotBlank(model.getResponseText()))%0A    %09   entity.setResponseText(model.getResponseText());%0A       if(StringUtils.isNotBlank(model.getAppId()))%0A    %09   entity.setAppId(model.getAppId());%0A       if(StringUtils.isNotBlank
 (mod
@@ -982,22 +982,23 @@
 eCode())
-;
+)
 %0A    
+%09
    entit
@@ -1006,85 +1006,105 @@
 .set
-AppId(model.getAppId());%0A       entity.setHmisLink(model.getHmisLink());
+PickListValueCode(model.getPickListValueCode());%0A       if(model.getEffectiveDate() !=null)
 %0A    
+%09
    e
@@ -1167,63 +1167,111 @@
  if(
-model.getDedupClientId()!=null) entity.setDedupClientId
+StringUtils.isNotBlank(model.getHmisLink()))%0A    %09   entity.setHmisLink(model.getHmisLink());%0A       if
 (mod
@@ -1295,17 +1295,22 @@
 Id()
-);%0A//
+!=null) %0A
     
+%09
    e
@@ -1314,24 +1314,29 @@
   entity.set
+Dedup
 ClientId(mod
@@ -1337,24 +1337,29 @@
 Id(model.get
+Dedup
 ClientId());

Fix the same thing for v3 apis