<commit_before>package it.makeit.zefiro.ws.resources;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Map.Entry;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Item;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.ObjectType;
import org.apache.chemistry.opencmis.client.api.Property;
import org.apache.chemistry.opencmis.client.api.QueryResult;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.runtime.util.CollectionIterator;
import org.apache.chemistry.opencmis.commons.data.PropertyData;
import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
import org.apache.commons.lang.StringUtils;

import it.makeit.alfresco.AlfrescoHelper;
import it.makeit.alfresco.CmisQueryBuilder;
import it.makeit.alfresco.CmisQueryPredicate;
import it.makeit.alfresco.CmisQueryPredicate.Operator;
import it.makeit.alfresco.addon.DocumentListenener;
import it.makeit.jbrick.JBrickConfigManager;
import it.makeit.jbrick.Log;
import it.makeit.jbrick.print.PrintFormat;
import it.makeit.jbrick.print.PrintUtil;
import it.makeit.jbrick.web.LocaleUtil;
import it.makeit.zefiro.Util;

@Path("/Search")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public class SearchEndpoint {
	private static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
	
	@Context
	private HttpServletRequest httpRequest;
	
	
	@Inject DocumentListenener documentListenener;
	
	private static final String CONTAINS_FIELD = "contains";
	private static final String EQ = "eq";
	private static final String FROM = "ge";
	private static final String TO = "le";
	private static final String LIKE = "lk";

	private static final String mAlfrescoBaseTypeId = JBrickConfigManager.getInstance().getMandatoryProperty("alfresco/@baseTypeId");
	private static final String mAlfrescoBaseTypeItemId = JBrickConfigManager.getInstance().getProperty("alfresco/@baseTypeItemId");
	private static Log mLog = Log.getInstance(SearchEndpoint.class);
	
	private static final Integer ALFRESCORESULTSLIMIT = 100;
	
	@GET
	@Path("/")
	public Response getDocuments() throws ParseException {
		Session session = Util.getUserAlfrescoSession(httpRequest);
		String rootFolderId = (String) httpRequest.getSession().getAttribute("rootFolderId");

		Map<String, String[]> mapParams = new HashMap<String, String[]>(httpRequest.getParameterMap());
		
		if(mAlfrescoBaseTypeItemId != null) {
			String typeId = mapParams.get("type")==null? mAlfrescoBaseTypeId:mapParams.get("type")[0];
			String parentType = session.getTypeDefinition(typeId).getParentTypeId();
			
			if (parentType.equals(mAlfrescoBaseTypeItemId)) {
				return Response.ok(searchItems(typeId, rootFolderId, session)).build();
			}
			
		}
		
		return Response.ok(searchDocuments(mapParams, rootFolderId, session)).build();
	}
	

	@GET
	@Path("/print/xls/")
	public Response printXls(@Context HttpServletRequest pRequest, @Context ServletContext pServletContext)
			throws IOException {
		String rootFolderId = (String) httpRequest.getSession().getAttribute("rootFolderId");
		StreamingOutput lStreamingOutput = print(pRequest, pServletContext, PrintFormat.XLS.toString(), rootFolderId);

		return Response.ok(lStreamingOutput).header("Content-Disposition", "attachment; filename=Document.xls").build();
	}

	
	private static class QueryFilter {

		private String propertyId;
		private Operator operator;<commit_msg>fix controllo search sul tipo primitivo<commit_after>package it.makeit.zefiro.ws.resources;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Map.Entry;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Item;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.ObjectType;
import org.apache.chemistry.opencmis.client.api.Property;
import org.apache.chemistry.opencmis.client.api.QueryResult;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.runtime.util.CollectionIterator;
import org.apache.chemistry.opencmis.commons.data.PropertyData;
import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
import org.apache.commons.lang.StringUtils;

import it.makeit.alfresco.AlfrescoHelper;
import it.makeit.alfresco.CmisQueryBuilder;
import it.makeit.alfresco.CmisQueryPredicate;
import it.makeit.alfresco.CmisQueryPredicate.Operator;
import it.makeit.alfresco.addon.DocumentListenener;
import it.makeit.jbrick.JBrickConfigManager;
import it.makeit.jbrick.Log;
import it.makeit.jbrick.print.PrintFormat;
import it.makeit.jbrick.print.PrintUtil;
import it.makeit.jbrick.web.LocaleUtil;
import it.makeit.zefiro.Util;

@Path("/Search")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public class SearchEndpoint {
	private static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
	
	@Context
	private HttpServletRequest httpRequest;
	
	
	@Inject DocumentListenener documentListenener;
	
	private static final String CONTAINS_FIELD = "contains";
	private static final String EQ = "eq";
	private static final String FROM = "ge";
	private static final String TO = "le";
	private static final String LIKE = "lk";

	private static final String mAlfrescoBaseTypeId = JBrickConfigManager.getInstance().getMandatoryProperty("alfresco/@baseTypeId");
	private static final String mAlfrescoBaseTypeItemId = JBrickConfigManager.getInstance().getProperty("alfresco/@baseTypeItemId");
	private static Log mLog = Log.getInstance(SearchEndpoint.class);
	
	private static final Integer ALFRESCORESULTSLIMIT = 100;
	
	@GET
	@Path("/")
	public Response getDocuments() throws ParseException {
		Session session = Util.getUserAlfrescoSession(httpRequest);
		String rootFolderId = (String) httpRequest.getSession().getAttribute("rootFolderId");

		Map<String, String[]> mapParams = new HashMap<String, String[]>(httpRequest.getParameterMap());
		
		if(mAlfrescoBaseTypeItemId != null) {
			String typeId = mapParams.get("type")==null? mAlfrescoBaseTypeId:mapParams.get("type")[0];

			while (typeId != null) {
				typeId = session.getTypeDefinition(typeId).getParentTypeId();
				if (typeId.equals(mAlfrescoBaseTypeItemId)){
					return Response.ok(searchItems(typeId, rootFolderId, session)).build();
				};
			};			
		};
		return Response.ok(searchDocuments(mapParams, rootFolderId, session)).build();
	}
	

	@GET
	@Path("/print/xls/")
	public Response printXls(@Context HttpServletRequest pRequest, @Context ServletContext pServletContext)
			throws IOException {
		String rootFolderId = (String) httpRequest.getSession().getAttribute("rootFolderId");
		StreamingOutput lStreamingOutput = print(pRequest, pServletContext, PrintFormat.XLS.toString(), rootFolderId);

		return Response.ok(lStreamingOutput).header("Content-Disposition", "attachment; filename=Document.xls").build();
	}

	
	private static class QueryFilter {

		private String propertyId;
		private Operator operator;
{'fraction_non_alphanumeric': 0.11216919499501717, 'fraction_numerical': 0.0011072970878086591, 'mean_word_length': 6.196812749003984, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6542466', 'n_tokens_mistral': 2972, 'n_tokens_neox': 2799, 'n_words': 516}