package com.chamabem.util;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.ws.rs.ext.Provider;

import org.hibernate.validator.InvalidValue;
import org.jboss.resteasy.spi.HttpResponse;

import com.chamabem.core.exception.ValidationException;

@Provider
public class ResponseUtils {

	@Inject
	private static Logger log;

	public static void setResponseHeaders(HttpResponse response) throws IOException {
//		log.info("Entrou: setResponseHeaders()");
//		response.getOutputHeaders().putSingle("totvs-page-number", search.getPageNumber());
//		response.getOutputHeaders().putSingle("totvs-page-size", search.getPageSize());
//		response.getOutputHeaders().putSingle("totvs-total-records", search.getTotalRecords());
//		response.getOutputHeaders().putSingle("totvs-total-pages", search.getTotalPages());
//		log.info("Setou HTTP Headers");
//		// se request tentou buscar uma pagina maior que o que realmente existe,
//		// setar HTTP CODE 204: "No content"
//		/*
//		 * if (search.getPageNumber() > search.getTotalPages()) {
//		 * log.debug("Tentou buscar uma pagina inexistente. Setando HTTP Code 204"
//		 * ); response.sendError(204); }
//		 */
//		log.info("Saiu: setResponseHeaders()");
	}

//	public static <E> List<E> build(TBNativeQuery<E> query, org.jboss.resteasy.spi.HttpResponse response) {
//		if (query.getResultCount() != null && query.getResultCount() > 0) {
//			response.getOutputHeaders().putSingle("totvs-total-count", query.getResultCount());
//			response.getOutputHeaders().putSingle("totvs-current-page", query.getPage());
//			response.getOutputHeaders().putSingle("totvs-total-pages", query.getPageCount());
//			response.getOutputHeaders().putSingle("totvs-page-size", query.getMaxResults());
//		}
//		return query.getResultList();
//	}
//
//	public static <E> List<E> build(TBEntityQuery<E> query, org.jboss.resteasy.spi.HttpResponse response) {
//		if (query.getResultCount() != null && query.getResultCount() > 0) {
//			response.getOutputHeaders().putSingle("totvs-total-count", query.getResultCount());
//			response.getOutputHeaders().putSingle("totvs-current-page", query.getPage());
//			response.getOutputHeaders().putSingle("totvs-total-pages", query.getPageCount());
//			response.getOutputHeaders().putSingle("totvs-page-size", query.getMaxResults());
//			;
//		}
//		return query.getResultList();
//	}

	public static void validate(InvalidValue[] invalidValues) throws ValidationException {

		if (invalidValues.length == 0) {
			return;
		}

		ValidationResponse validationResponse = new ValidationResponse();

		for (InvalidValue invalidValue : invalidValues) {
			validationResponse.add(new Validation(invalidValue.getPropertyPath(), invalidValue.getMessage()));
		}

		throw new ValidationException(validationResponse);
	}

	public static void validate(List<Validation> validationItems) throws ValidationException {

		if (validationItems.isEmpty()) {
			return;
		}

		ValidationResponse validationResponse = new ValidationResponse();
		validationResponse.setValidationItems(validationItems);

		throw new ValidationException(validationResponse);
	}

	public static void validate(InvalidValue[] invalidValues, List<Validation> validationItems) throws ValidationException {

		if (validationItems.isEmpty() && invalidValues.length == 0) {
			return;
		}

		for (InvalidValue invalidValue : invalidValues) {
			validationItems.add(new Validation(invalidValue.getPropertyPath(), invalidValue.getMessage()));
		}

		ValidationResponse validationResponse = new ValidationResponse();
		validationResponse.setValidationItems(validationItems);

		throw new ValidationException(validationResponse);
	}
}
@@ -561,30 +561,24 @@
 .putSingle(%22
-totvs-
 page-number%22
@@ -639,38 +639,32 @@
 rs().putSingle(%22
-totvs-
 page-size%22, sear
@@ -721,30 +721,24 @@
 .putSingle(%22
-totvs-
 total-record
@@ -803,38 +803,32 @@
 rs().putSingle(%22
-totvs-
 total-pages%22, se
@@ -1460,38 +1460,32 @@
 rs().putSingle(%22
-totvs-
 total-count%22, qu
@@ -1543,38 +1543,32 @@
 rs().putSingle(%22
-totvs-
 current-page%22, q
@@ -1620,38 +1620,32 @@
 rs().putSingle(%22
-totvs-
 total-pages%22, qu
@@ -1701,38 +1701,32 @@
 rs().putSingle(%22
-totvs-
 page-size%22, quer
@@ -2012,30 +2012,24 @@
 .putSingle(%22
-totvs-
 total-count%22
@@ -2099,22 +2099,16 @@
 Single(%22
-totvs-
 current-
@@ -2176,22 +2176,16 @@
 Single(%22
-totvs-
 total-pa
@@ -2261,14 +2261,8 @@
 le(%22
-totvs-
 page
@@ -3642,8 +3642,9 @@
 e);%0A%09%7D%0A%7D
+%0A

Update ResponseUtils.java