package com.oceaneconsulting.tanaguru.dao.impl;


import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.opens.tanaguru.entity.statistics.CriterionStatistics;
import org.opens.tanaguru.entity.statistics.CriterionStatisticsImpl;
import org.opens.tanaguru.entity.statistics.TestStatistics;
import org.opens.tanaguru.entity.statistics.TestStatisticsImpl;
import org.opens.tanaguru.entity.statistics.ThemeStatistics;
import org.opens.tanaguru.entity.statistics.ThemeStatisticsImpl;
import org.opens.tanaguru.entity.statistics.WebResourceStatistics;
import org.opens.tanaguru.entity.statistics.WebResourceStatisticsImpl;
import org.opens.tanaguru.sdk.entity.dao.jpa.AbstractJPADAO;
import org.springframework.stereotype.Repository;

import com.oceaneconsulting.tanaguru.dao.StatisticsDAO;
import com.oceaneconsulting.tanaguru.ws.types.AuditResult;


@Repository("statisticsDAO")
public class StatisticsDAOImpl extends AbstractJPADAO<WebResourceStatistics, Long> implements StatisticsDAO {

    private static final String SELECT_CLAUSE_Q1 = "SELECT WEB_RESOURCE_STATISTICS.Mark, AUDIT.STATUS ";
    private static final String FROM_CLAUSE_Q1  = "FROM AUDIT left outer join WEB_RESOURCE on WEB_RESOURCE.ID_AUDIT = AUDIT.ID_AUDIT left outer join WEB_RESOURCE_STATISTICS on WEB_RESOURCE_STATISTICS.Id_Web_Resource=WEB_RESOURCE.Id_Web_Resource ";
    private static final String WHERE_CLAUSE_Q1  = "WHERE  AUDIT.ID_AUDIT = :idAudit";
	
   
    @Override
    public AuditResult findWeightedMarkAndStatusByAuditId(Long idAudit) {
    	AuditResult auditResult = new AuditResult();
        StringBuilder queryString = new StringBuilder();
        queryString.append(SELECT_CLAUSE_Q1); 
        queryString.append(FROM_CLAUSE_Q1); 
        queryString.append(WHERE_CLAUSE_Q1); 
        Query query = entityManager.createNativeQuery(queryString.toString());
        query.setParameter("idAudit", idAudit);
        try {
        	Object[] result = (Object[])query.getSingleResult();
            
            if (result[0] instanceof Float) {
            	auditResult.setScore((Float)result[0]);
            } else if (result[0] instanceof Double) {
            	auditResult.setScore(((Double)result[0]).floatValue());
            } else {
            	auditResult.setScore(Float.valueOf(0));
            }
            
            auditResult.setStatus((String)result[1]);
            
        } catch (NoResultException e) {
        	return null;
        }
        
        return auditResult;
    }

    @Override
    protected Class<? extends WebResourceStatistics> getEntityClass() {
        return WebResourceStatisticsImpl.class;
    }
    
    protected Class<? extends CriterionStatistics> getCriterionStatisticsEntityClass() {
        return CriterionStatisticsImpl.class;
    }
    
    protected Class<? extends TestStatistics> getTestStatisticsEntityClass() {
        return TestStatisticsImpl.class;
    }
    
    protected Class<? extends ThemeStatistics> getThemeStatisticsEntityClass() {
        return ThemeStatisticsImpl.class;
    }
}

@@ -42,24 +42,56 @@
 .impl;%0D%0A%0D%0A%0D%0A
+import java.math.BigInteger;%0D%0A%0D%0A
 import javax
@@ -123,16 +123,16 @@
 ption;%0D%0A
-
 import j
@@ -952,16 +952,18 @@
 icsDAO%22)
+  
 %0D%0Apublic
@@ -1170,16 +1170,57 @@
 T.STATUS
+, WEB_RESOURCE_STATISTICS.ID_WEB_RESOURCE
  %22;%0D%0A   
@@ -1556,16 +1556,135 @@
 t%22;%0D%0A%09%0D%0A
+    private static final String RESULT_URL = %22http://localhost:8080/tgol-web-app/home/contract/page-result.html?wr=%22;%0D%0A
    %0D%0A   
@@ -2564,32 +2564,32 @@
 %0D%0A            %0D%0A
-
             audi
@@ -2633,32 +2633,181 @@
 %0D%0A            %0D%0A
+            if(result%5B2%5D != null)%7B%0D%0A            %09auditResult.setUrl(RESULT_URL + (BigInteger)result%5B2%5D);%0D%0A            %7D%0D%0A            %0D%0A            %0D%0A
         %7D catch 

number of invocations limited to 5