<commit_before>package com.web.javsterisk.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.exception.ConstraintViolationException;

import com.web.javsterisk.dao.ExtensionsDAO;
import com.web.javsterisk.dao.ExtensionsWizzardDAO;
import com.web.javsterisk.dao.ParameterDAO;
import com.web.javsterisk.entity.Extensions;
import com.web.javsterisk.entity.ExtensionsId;
import com.web.javsterisk.entity.ExtensionsWizzard;
import com.web.javsterisk.entity.Parameter;

/**
 * 
 * @author <NAME>
 * @version 1.0
 */
@ManagedBean
@ViewScoped
public class ExtensionsController extends BaseController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final Logger log = LogManager.getLogger(ExtensionsController.class);
	
	private final String EXT_PREFIX = "_";
	
	private final String EXT_CHAR = "X";
	
	private final String APP_ANSWER = "Answer";
	
	private final String APP_SET = "Set";
	
	private final String APP_MIX_MONITOR = "MixMonitor";
	
	private final String APP_DIAL = "Dial";
	
	private final String APP_HANGUP = "Hangup";
	
	private ExtensionsDAO extensionsDAO;
	
	private ExtensionsWizzardDAO extensionsWizzardDAO;
	
	private ParameterDAO parameterDAO;
	
	private Parameter param_record_path;
	
	private String context;
	
	@ManagedProperty("#{securityController}")
	private SecurityController securityController;
	
	private List<Extensions> extensionesFiltered;

	private Extensions newExtensions;
	
	private ExtensionsWizzard newExtensionsWizzard;
				
	private Extensions[] selectedExtensiones;
	
	private Extensions selectedExtension;
	
	private int selectedExtensionesSize;
	
	private List<Extensions> extensiones;
	
	@PostConstruct
	public void initNewExtensions() {
		parameterDAO = new ParameterDAO();		
		extensionsDAO = new ExtensionsDAO();
		extensionsWizzardDAO = new ExtensionsWizzardDAO();
		log.info("@PostConstruct Extensions");
		if(securityController.isAuthenticated()){
			context = null;
			param_record_path = parameterDAO.findByName("asterisk.recorder.path");			
			newExtensionsWizzard = new ExtensionsWizzard();			
			extensiones = extensionsDAO.findAllOrderedById();
		}
	}
	
	public void register() { 		
		log.info("Start register()");
//		String permission = formatPermission(new StringBuilder(this.getClass().getSimpleName()), Thread.currentThread().getStackTrace(), 2);	
//		log.info("Permission module is in list ToDo : {}", permission);
		if ( securityController.isAdministrator() ) {		
			try {
				
				makeExtensions();

				extensionsWizzardDAO.register(newExtensionsWizzard);
				
				log.info("Registration successful");
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Registered!", "Registration successful"));			
				initNewExtensions();
			} catch (Exception e) {				
				if(e.getCause().getCause().getCause() instanceof ConstraintViolationException){
					log.error("ConstraintViolationException", e);
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "El Dial Plan ya existe"));
				} else {
					log.error("Exception", e);
				}
			}
		} else {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Authorization!", "No tiene privilegios para esta accion"));
		}		
	}

	private void makeExtensions() {
		
		newExtensionsWizzard.setExtensions(new ArrayList<Extensions>(0));
		
		int id = extensionsDAO.findAllOrderedById().size() + 1;
		
		String extension = StringUtils.rightPad(EXT_PREFIX + newExtensionsWizzard.getDigito(), newExtensionsWizzard.getLongitud() + 1, EXT_CHAR);
		
		log.info("-> context : {}", context);
		log.info("-> extesnion generated : {}", extension);		
		
		Extensions answer = new Extensions();
		answer.setApp(APP_ANSWER);		
		
		Extensions set = new Extensions();
		set.setApp(APP_SET);
		set.setAppdata("MONITOR_FILENAME=${STRFTIME($,,%Y%m%-%H%M%S)}-${CALLERID(num)}");
		
//		char c  = (char) 43;
		
		Extensions monitor = new Extensions();
		monitor.setApp(APP_MIX_MONITOR);
		monitor.setAppdata(param_record_path.getValue() + " + ​${MONITOR_FILENAME}.wav,b");		
		
		Extensions dial = new Extensions();
		dial.setApp(APP_DIAL);
		
		Extensions hangup = new Extensions();
		hangup.setApp(APP_HANGUP);
		
		String limit = "";
		
		if(newExtensionsWizzard.isLimit()) {
			
			limit = ",L(" + newExtensionsWizzard.getTimeLimit() + "::)";
			
			if(!"".equals(newExtensionsWizzard.getFirstAlert().trim()) 
					&& "".equals(newExtensionsWizzard.getSecondAlert().trim())) {
				limit = ",L(" + newExtensionsWizzard.getTimeLimit() + ":" 
					+ newExtensionsWizzard.getFirstAlert() + ":)";
			}
			
			if(!"".equals(newExtensionsWizzard.getFirstAlert().trim()) 
					&& !"".equals(newExtensionsWizzard.getSecondAlert().trim())) {<commit_msg>remove special char in appdata, issue #21<commit_after>package com.web.javsterisk.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.exception.ConstraintViolationException;

import com.web.javsterisk.dao.ExtensionsDAO;
import com.web.javsterisk.dao.ExtensionsWizzardDAO;
import com.web.javsterisk.dao.ParameterDAO;
import com.web.javsterisk.entity.Extensions;
import com.web.javsterisk.entity.ExtensionsId;
import com.web.javsterisk.entity.ExtensionsWizzard;
import com.web.javsterisk.entity.Parameter;

/**
 * 
 * @author <NAME>
 * @version 1.0
 */
@ManagedBean
@ViewScoped
public class ExtensionsController extends BaseController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final Logger log = LogManager.getLogger(ExtensionsController.class);
	
	private final String EXT_PREFIX = "_";
	
	private final String EXT_CHAR = "X";
	
	private final String APP_ANSWER = "Answer";
	
	private final String APP_SET = "Set";
	
	private final String APP_MIX_MONITOR = "MixMonitor";
	
	private final String APP_DIAL = "Dial";
	
	private final String APP_HANGUP = "Hangup";
	
	private ExtensionsDAO extensionsDAO;
	
	private ExtensionsWizzardDAO extensionsWizzardDAO;
	
	private ParameterDAO parameterDAO;
	
	private Parameter param_record_path;
	
	private String context;
	
	@ManagedProperty("#{securityController}")
	private SecurityController securityController;
	
	private List<Extensions> extensionesFiltered;

	private Extensions newExtensions;
	
	private ExtensionsWizzard newExtensionsWizzard;
				
	private Extensions[] selectedExtensiones;
	
	private Extensions selectedExtension;
	
	private int selectedExtensionesSize;
	
	private List<Extensions> extensiones;
	
	@PostConstruct
	public void initNewExtensions() {
		parameterDAO = new ParameterDAO();		
		extensionsDAO = new ExtensionsDAO();
		extensionsWizzardDAO = new ExtensionsWizzardDAO();
		log.info("@PostConstruct Extensions");
		if(securityController.isAuthenticated()){
			context = null;
			param_record_path = parameterDAO.findByName("asterisk.recorder.path");			
			newExtensionsWizzard = new ExtensionsWizzard();			
			extensiones = extensionsDAO.findAllOrderedById();
		}
	}
	
	public void register() { 		
		log.info("Start register()");
//		String permission = formatPermission(new StringBuilder(this.getClass().getSimpleName()), Thread.currentThread().getStackTrace(), 2);	
//		log.info("Permission module is in list ToDo : {}", permission);
		if ( securityController.isAdministrator() ) {		
			try {
				
				makeExtensions();

				extensionsWizzardDAO.register(newExtensionsWizzard);
				
				log.info("Registration successful");
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Registered!", "Registration successful"));			
				initNewExtensions();
			} catch (Exception e) {				
				if(e.getCause().getCause().getCause() instanceof ConstraintViolationException){
					log.error("ConstraintViolationException", e);
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "El Dial Plan ya existe"));
				} else {
					log.error("Exception", e);
				}
			}
		} else {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Authorization!", "No tiene privilegios para esta accion"));
		}		
	}

	private void makeExtensions() {
		
		newExtensionsWizzard.setExtensions(new ArrayList<Extensions>(0));
		
		int id = extensionsDAO.findAllOrderedById().size() + 1;
		
		String extension = StringUtils.rightPad(EXT_PREFIX + newExtensionsWizzard.getDigito(), newExtensionsWizzard.getLongitud() + 1, EXT_CHAR);
		
		log.info("-> context : {}", context);
		log.info("-> extesnion generated : {}", extension);		
		
		Extensions answer = new Extensions();
		answer.setApp(APP_ANSWER);		
		
		Extensions set = new Extensions();
		set.setApp(APP_SET);
		set.setAppdata("MONITOR_FILENAME=${STRFTIME($,,%Y%m%-%H%M%S)}-${CALLERID(num)}");
		
//		char c  = (char) 43;
		
		Extensions monitor = new Extensions();
		monitor.setApp(APP_MIX_MONITOR);
		monitor.setAppdata(param_record_path.getValue() + " + ${MONITOR_FILENAME}.wav,b");		
		
		Extensions dial = new Extensions();
		dial.setApp(APP_DIAL);
		
		Extensions hangup = new Extensions();
		hangup.setApp(APP_HANGUP);
		
		String limit = "";
		
		if(newExtensionsWizzard.isLimit()) {
			
			limit = ",L(" + newExtensionsWizzard.getTimeLimit() + "::)";
			
			if(!"".equals(newExtensionsWizzard.getFirstAlert().trim()) 
					&& "".equals(newExtensionsWizzard.getSecondAlert().trim())) {
				limit = ",L(" + newExtensionsWizzard.getTimeLimit() + ":" 
					+ newExtensionsWizzard.getFirstAlert() + ":)";
			}
			
			if(!"".equals(newExtensionsWizzard.getFirstAlert().trim()) 
					&& !"".equals(newExtensionsWizzard.getSecondAlert().trim())) {
{'fraction_non_alphanumeric': 0.12245477791457525, 'fraction_numerical': 0.0024623543896202292, 'mean_word_length': 4.962732919254658, 'pattern_counts': {'":': 6, '<': 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': '255008', 'n_tokens_mistral': 3650, 'n_tokens_neox': 3136, 'n_words': 602}