<commit_before>package org.opencps.api.controller.impl;

import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Company;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.Validator;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

import org.apache.commons.httpclient.util.HttpURLConnection;
import org.opencps.api.controller.FaqManagement;
import org.opencps.api.controller.util.CaptchaServiceSingleton;
import org.opencps.api.faq.model.AnswerDetailModel;
import org.opencps.api.faq.model.AnswerInputModel;
import org.opencps.api.faq.model.AnswerModel;
import org.opencps.api.faq.model.AnswerResultsModel;
import org.opencps.api.faq.model.QuestionDetailModel;
import org.opencps.api.faq.model.QuestionInputModel;
import org.opencps.api.faq.model.QuestionModel;
import org.opencps.api.faq.model.QuestionResultsModel;
import org.opencps.auth.utils.APIDateTimeUtils;
import org.opencps.communication.model.ServerConfig;
import org.opencps.communication.service.ServerConfigLocalServiceUtil;
import org.opencps.dossiermgt.rest.utils.SyncServerTerm;
import org.opencps.usermgt.model.Answer;
import org.opencps.usermgt.model.Question;
import org.opencps.usermgt.service.AnswerLocalServiceUtil;
import org.opencps.usermgt.service.QuestionLocalServiceUtil;

import backend.auth.api.exception.BusinessExceptionImpl;
import backend.auth.api.exception.ErrorMsgModel;

public class FaqManagementImpl implements FaqManagement {

	private static final Log _log = LogFactoryUtil.getLog(FaqManagementImpl.class);
	@Override
	public Response addQuestion(HttpServletRequest request, HttpHeaders header, Company company, Locale locale,
			User user, ServiceContext serviceContext, QuestionInputModel input, String jCaptchaResponse) {
		long groupId = GetterUtil.getLong(header.getHeaderString("groupId"));
		try {
			ImageCaptchaService instance = CaptchaServiceSingleton.getInstance();
			String captchaId = request.getSession().getId();
	        try {
	        	boolean isResponseCorrect = instance.validateResponseForID(captchaId,
	        			jCaptchaResponse);
	        	if (!isResponseCorrect) {
	        		ErrorMsgModel error = new ErrorMsgModel();
	        		error.setMessage("Captcha incorrect");
	    			error.setCode(HttpURLConnection.HTTP_NOT_AUTHORITATIVE);
	    			error.setDescription("Captcha incorrect");

	    			return Response.status(HttpURLConnection.HTTP_NOT_AUTHORITATIVE).entity(error).build();
	        	}
	        } catch (CaptchaServiceException e) {
	        	_log.debug(e);
        		ErrorMsgModel error = new ErrorMsgModel();
        		error.setMessage("Captcha incorrect");
    			error.setCode(HttpURLConnection.HTTP_NOT_AUTHORITATIVE);
    			error.setDescription("Captcha incorrect");

    			return Response.status(HttpURLConnection.HTTP_NOT_AUTHORITATIVE).entity(error).build();
	        }

			Question question = QuestionLocalServiceUtil.updateQuestion(serviceContext.getCompanyId(), groupId, 0l,
					input.getFullname(), input.getEmail(), input.getContent(), input.getPublish(),
					input.getGovAgencyCode(), input.getGovAgencyName(), input.getQuestionType());
			if (question != null) {
				QuestionDetailModel result = new QuestionDetailModel();
				result.setContent(question.getContent());
				result.setCreateDate(APIDateTimeUtils.convertDateToString(question.getCreateDate()));
				result.setEmail(question.getEmail());
				result.setFullname(question.getFullname());
				result.setPublish(question.getPublish());
				result.setQuestionId(question.getQuestionId());
				result.setGovAgencyCode(question.getGovAgencyCode());
				result.setGovAgencyName(question.getGovAgencyName());
				
				return Response.status(200).entity(result)
						.header("Access-Control-Allow-Origin", request.getHeader("Origin"))
						.header("Access-Control-Allow-Credentials", "true")
						.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization, groupid, token")
						.header("Access-Control-Allow-Methods", "POST")
						.build();
			}
			else {
				throw new Exception("Error process database");
			}
		}
		catch (Exception e) {
			return BusinessExceptionImpl.processException(e);
		}
	}

	@Override
	public Response getQuestions(HttpServletRequest request, HttpHeaders header, Company company, Locale locale,
			User user, Integer start, Integer end, Integer publish, String govAgencyCode, String keyword, String questionType,
			String answer,
			ServiceContext serviceContext) {
		long groupId = GetterUtil.getLong(header.getHeaderString("groupId"));
		_log.debug("IN GET QUESTION: " + groupId);
		try {
			if (Validator.isNull(start)) {
				start = QueryUtil.ALL_POS;
				end = QueryUtil.ALL_POS;
			}
			//List<Question> lstQuestions = null;
//			if (publish == null) {
//				lstQuestions = QuestionLocalServiceUtil.findByG_PL(groupId, new int[] { 0, 1 }, start, end);
//			}
//			else {
//				lstQuestions = QuestionLocalServiceUtil.findByG_PL(groupId, new int[] { publish }, start, end);
//			}
			
			Boolean filterAnswer = null;
			if (Validator.isNotNull(answer)) {
				filterAnswer = Boolean.parseBoolean(answer);
			}
			List<Question> lstQuestions = QuestionLocalServiceUtil.findByQuerySearch(groupId, keyword, govAgencyCode,
					publish, questionType, filterAnswer, start, end);
			
			QuestionResultsModel result = new QuestionResultsModel();
			result.setTotal(QuestionLocalServiceUtil.countByQuerySearch(groupId, keyword, govAgencyCode, publish, questionType, filterAnswer));
			
			List<QuestionModel> lstModels = new ArrayList<>();
			if (lstQuestions != null && lstQuestions.size() > 0) {
				for (Question q : lstQuestions) {
					QuestionModel model = new QuestionModel();
					model.setContent(q.getContent());
					model.setCreateDate(APIDateTimeUtils.convertDateToString(q.getCreateDate()));
					model.setEmail(q.getEmail());
					model.setFullname(q.getFullname());
					model.setPublish(q.getPublish());
					model.setQuestionId(q.getQuestionId());
					model.setGovAgencyCode(q.getGovAgencyCode());
					model.setGovAgencyName(q.getGovAgencyName());
					
					int count = AnswerLocalServiceUtil.countByG_Q_PL(groupId, q.getQuestionId(), new int[] { 0, 1 } );
					model.setAnswered(false);
					
					if (count > 0) {
						model.setAnswered(true);
					}
					lstModels.add(model);
				}
				result.getData().addAll(lstModels);
			}

			return Response.status(200).entity(result)
					.header("Access-Control-Allow-Origin", request.getHeader("Origin"))
					.header("Access-Control-Allow-Credentials", "true")
					.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization, groupid, token")
					.header("Access-Control-Allow-Methods", "GET")
					.build();
		}
		catch (Exception e) {
			return BusinessExceptionImpl.processException(e);
		}
	}

	@Override
	public Response addAnswer(HttpServletRequest request, HttpHeaders header, Company company, Locale locale, User user,
			ServiceContext serviceContext, long questionId, AnswerInputModel input) {
		long groupId = GetterUtil.getLong(header.getHeaderString("groupId"));
		try {
			Answer answer = AnswerLocalServiceUtil.updateAnswer(user.getUserId(), groupId, 0l, questionId, input.getContent(), input.getPublish());<commit_msg>Update faq<commit_after>package org.opencps.api.controller.impl;

import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Company;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.Validator;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

import org.apache.commons.httpclient.util.HttpURLConnection;
import org.opencps.api.controller.FaqManagement;
import org.opencps.api.controller.util.CaptchaServiceSingleton;
import org.opencps.api.faq.model.AnswerDetailModel;
import org.opencps.api.faq.model.AnswerInputModel;
import org.opencps.api.faq.model.AnswerModel;
import org.opencps.api.faq.model.AnswerResultsModel;
import org.opencps.api.faq.model.QuestionDetailModel;
import org.opencps.api.faq.model.QuestionInputModel;
import org.opencps.api.faq.model.QuestionModel;
import org.opencps.api.faq.model.QuestionResultsModel;
import org.opencps.auth.utils.APIDateTimeUtils;
import org.opencps.communication.model.ServerConfig;
import org.opencps.communication.service.ServerConfigLocalServiceUtil;
import org.opencps.dossiermgt.rest.utils.SyncServerTerm;
import org.opencps.usermgt.model.Answer;
import org.opencps.usermgt.model.Question;
import org.opencps.usermgt.service.AnswerLocalServiceUtil;
import org.opencps.usermgt.service.QuestionLocalServiceUtil;

import backend.auth.api.exception.BusinessExceptionImpl;
import backend.auth.api.exception.ErrorMsgModel;

public class FaqManagementImpl implements FaqManagement {

	private static final Log _log = LogFactoryUtil.getLog(FaqManagementImpl.class);
	@Override
	public Response addQuestion(HttpServletRequest request, HttpHeaders header, Company company, Locale locale,
			User user, ServiceContext serviceContext, QuestionInputModel input, String jCaptchaResponse) {
		long groupId = GetterUtil.getLong(header.getHeaderString("groupId"));
		try {
			ImageCaptchaService instance = CaptchaServiceSingleton.getInstance();
			String captchaId = request.getSession().getId();
	        try {
	        	boolean isResponseCorrect = instance.validateResponseForID(captchaId,
	        			jCaptchaResponse);
	        	if (!isResponseCorrect) {
	        		ErrorMsgModel error = new ErrorMsgModel();
	        		error.setMessage("Captcha incorrect");
	    			error.setCode(HttpURLConnection.HTTP_NOT_AUTHORITATIVE);
	    			error.setDescription("Captcha incorrect");

	    			return Response.status(HttpURLConnection.HTTP_NOT_AUTHORITATIVE).entity(error).build();
	        	}
	        } catch (CaptchaServiceException e) {
	        	_log.debug(e);
        		ErrorMsgModel error = new ErrorMsgModel();
        		error.setMessage("Captcha incorrect");
    			error.setCode(HttpURLConnection.HTTP_NOT_AUTHORITATIVE);
    			error.setDescription("Captcha incorrect");

    			return Response.status(HttpURLConnection.HTTP_NOT_AUTHORITATIVE).entity(error).build();
	        }

			Question question = QuestionLocalServiceUtil.updateQuestion(serviceContext.getCompanyId(), groupId, 0l,
					input.getFullname(), input.getEmail(), input.getContent(), input.getPublish(),
					input.getGovAgencyCode(), input.getGovAgencyName(), input.getQuestionType());
			if (question != null) {
				QuestionDetailModel result = new QuestionDetailModel();
				result.setContent(question.getContent());
				result.setCreateDate(APIDateTimeUtils.convertDateToString(question.getCreateDate()));
				result.setEmail(question.getEmail());
				result.setFullname(question.getFullname());
				result.setPublish(question.getPublish());
				result.setQuestionId(question.getQuestionId());
				result.setGovAgencyCode(question.getGovAgencyCode());
				result.setGovAgencyName(question.getGovAgencyName());
				
				return Response.status(200).entity(result)
						.header("Access-Control-Allow-Origin", request.getHeader("Origin"))
						.header("Access-Control-Allow-Credentials", "true")
						.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization, groupid, token")
						.header("Access-Control-Allow-Methods", "POST")
						.build();
			}
			else {
				throw new Exception("Error process database");
			}
		}
		catch (Exception e) {
			return BusinessExceptionImpl.processException(e);
		}
	}

	@Override
	public Response getQuestions(HttpServletRequest request, HttpHeaders header, Company company, Locale locale,
			User user, Integer start, Integer end, Integer publish, String govAgencyCode, String keyword, String questionType,
			String answer,
			ServiceContext serviceContext) {
		long groupId = GetterUtil.getLong(header.getHeaderString("groupId"));
		_log.debug("IN GET QUESTION: " + groupId);
		try {
			if (Validator.isNull(start)) {
				start = QueryUtil.ALL_POS;
				end = QueryUtil.ALL_POS;
			}
			//List<Question> lstQuestions = null;
//			if (publish == null) {
//				lstQuestions = QuestionLocalServiceUtil.findByG_PL(groupId, new int[] { 0, 1 }, start, end);
//			}
//			else {
//				lstQuestions = QuestionLocalServiceUtil.findByG_PL(groupId, new int[] { publish }, start, end);
//			}
			
			Boolean filterAnswer = null;
			if (Validator.isNotNull(answer)) {
				filterAnswer = Boolean.parseBoolean(answer);
			}
			List<Question> lstQuestions = QuestionLocalServiceUtil.findByQuerySearch(groupId, keyword, govAgencyCode,
					publish, questionType, filterAnswer, start, end);
			
			QuestionResultsModel result = new QuestionResultsModel();
			result.setTotal(QuestionLocalServiceUtil.countByQuerySearch(groupId, keyword, govAgencyCode, publish, questionType, filterAnswer));
			
			List<QuestionModel> lstModels = new ArrayList<>();
			if (lstQuestions != null && lstQuestions.size() > 0) {
				for (Question q : lstQuestions) {
					QuestionModel model = new QuestionModel();
					model.setContent(q.getContent());
					model.setCreateDate(APIDateTimeUtils.convertDateToString(q.getCreateDate()));
					model.setEmail(q.getEmail());
					model.setFullname(q.getFullname());
					model.setPublish(q.getPublish());
					model.setQuestionId(q.getQuestionId());
					model.setGovAgencyCode(q.getGovAgencyCode());
					model.setGovAgencyName(q.getGovAgencyName());
					model.setQuestionType(q.getQuestionType());
					
					int count = AnswerLocalServiceUtil.countByG_Q_PL(groupId, q.getQuestionId(), new int[] { 0, 1 } );
					model.setAnswered(false);
					
					if (count > 0) {
						model.setAnswered(true);
					}
					lstModels.add(model);
				}
				result.getData().addAll(lstModels);
			}

			return Response.status(200).entity(result)
					.header("Access-Control-Allow-Origin", request.getHeader("Origin"))
					.header("Access-Control-Allow-Credentials", "true")
					.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization, groupid, token")
					.header("Access-Control-Allow-Methods", "GET")
					.build();
		}
		catch (Exception e) {
			return BusinessExceptionImpl.processException(e);
		}
	}

	@Override
	public Response addAnswer(HttpServletRequest request, HttpHeaders header, Company company, Locale locale, User user,
			ServiceContext serviceContext, long questionId, AnswerInputModel input) {
		long groupId = GetterUtil.getLong(header.getHeaderString("groupId"));
		try {
			Answer answer = AnswerLocalServiceUtil.updateAnswer(user.getUserId(), groupId, 0l, questionId, input.getContent(), input.getPublish());
{'fraction_non_alphanumeric': 0.11692855389148048, 'fraction_numerical': 0.001964154186103609, 'mean_word_length': 4.7982206405693955, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1378633', 'n_tokens_mistral': 5529, 'n_tokens_neox': 4916, 'n_words': 819}