<commit_before>/*************************************************************************
 * Copyright 2014 Infield Design
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author <NAME> <<EMAIL>>
 * 
 *************************************************************************/

package com.infield.googlesearch;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Dictionary;
import java.util.LinkedList;
import java.util.List;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.osgi.framework.Constants;

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.customsearch.Customsearch;
import com.google.api.services.customsearch.CustomsearchRequestInitializer;
import com.google.api.services.customsearch.model.Result;
import com.google.api.services.customsearch.model.Search;
import com.google.api.services.customsearch.model.Search.SearchInformation;
import com.infield.googlesearch.model.ResultItem;
import com.infield.googlesearch.model.ResultList;


@Component(
		label = "Google Custom Search Service", 
		description = "Service used to call Google Custom Services.",
		immediate = true, 
		metatype = true)
@Service(GoogleSearchService.class)
@Properties({
	@Property(name = Constants.SERVICE_VENDOR, value = "Infield"),
	@Property(name = Constants.SERVICE_DESCRIPTION, value = "Google Custom Search service")
	 , 
	 @Property(
	 label = "Application Name", 
	 name = "cse.application.name", 
	 value = "",
	 description = "Be sure to specify the name of your application. "
	 + "If the application name is null or blank, the application "
	 + "will log a warning. Suggested format is "
	 + "'MyCompany-ProductName/1.0'.")
	 ,
	 @Property(
	 label = "API Key",
	 name = "cse.api.key", 
	 value = "", 
	 description = "API Key for the registered developer project for your application.")
	 ,
	 @Property(
	 label = "Context",
	 name = "cse.cx",
	 value = "", 
	 description = "The main search engine ID to scope the search query")
})
public class GoogleSearchService  {

	private final Logger log = LoggerFactory.getLogger(getClass());

	public static String apikey = "";
	public static String applicationname = "";
	public static String cx = "";
	public static Long num = 7l;
	public static Long pagesToShow = 10l;
	private Long pagesRight;
	private Long pagesLeft;
	private Customsearch customsearch;
	private LinkedList<ResultItem> resultItems;

	static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
	private static final long serialVersionUID = 1L;

	
	@Reference
	private SlingRepository repository;

	@Activate
	protected void activate(ComponentContext componentContext){
		configure(componentContext.getProperties());
	}
	
	
	protected void configure(Dictionary<?, ?> properties) {
		GoogleSearchService.apikey = PropertiesUtil.toString(properties.get("cse.api.key"), apikey);
		GoogleSearchService.applicationname = PropertiesUtil.toString(properties.get("cse.application.name"), applicationname);
		GoogleSearchService.cx = PropertiesUtil.toString(properties.get("cse.cx"),cx);
	 }


	public ResultList getResults(String q, String currentTab, long num, long pagesToShow) {
		//TODO: do XSS protection on Q
		ResultList resultList = new ResultList();

		try {
			
			pagesLeft = new BigDecimal(pagesToShow).divide(new BigDecimal(2), RoundingMode.UP).longValue();
			pagesRight = new BigDecimal(pagesToShow).divide(new BigDecimal(2), RoundingMode.DOWN).longValue();
			
			customsearch = new Customsearch.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, null)
			.setApplicationName(applicationname)
			.setGoogleClientRequestInitializer(new CustomsearchRequestInitializer(apikey))
			.build();
			Long actualPage = Long.valueOf(currentTab);
			com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list(q);
			
			list.setNum(num);
			list.setStart((num * (actualPage - 1)) + 1);
			list.setCx(cx);
			Search results = list.execute();
			SearchInformation searchInformation = results.getSearchInformation();

			resultItems = new LinkedList<ResultItem>();

			if (searchInformation != null){
				Long totalResults = searchInformation.getTotalResults();
				Long totalPages = new BigDecimal(totalResults).divide(new BigDecimal(num), RoundingMode.UP).longValue();
				resultList.setTotalPages(totalPages);
				resultList.setTotalResults(searchInformation.getTotalResults());
				resultList.setFormattedSearchTime(searchInformation.getFormattedSearchTime());
				resultList.setFormattedTotalResults(searchInformation.getFormattedTotalResults());
				resultList.setSearchTime(searchInformation.getSearchTime());
				resultList.setStartPage(getStartPage(actualPage));
				resultList.setEndPage(getEndPage(actualPage,totalPages));
				resultList.setCurrentTab(actualPage);


				List <Result> searchResults = results.getItems();
				for (Result searchResult: searchResults){
					ResultItem resultItem = new ResultItem();
					resultItem.setCacheId(searchResult.getCacheId());
					resultItem.setDisplayLink(searchResult.getDisplayLink());
					resultItem.setFileFormat(searchResult.getFileFormat());
					resultItem.setFormattedUrl(searchResult.getFormattedUrl());
					resultItem.setHtmlFormattedUrl(searchResult.getHtmlFormattedUrl());
					resultItem.setHtmlSnippet(searchResult.getHtmlSnippet());
					resultItem.setHtmlTitle(searchResult.getHtmlTitle());
					resultItem.setKind(searchResult.getKind());
					//resultItem.setLabels(searchResult.getLabels());
					resultItem.setLink(searchResult.getLink());
					resultItem.setMime(searchResult.getMime());
					resultItem.setSnippet(searchResult.getSnippet());
					resultItem.setTitle(searchResult.getTitle());
					resultItems.add(resultItem);
				}


				resultList.setResultItems(resultItems);

			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return resultList;
	}

	private Long getEndPage(Long actualPage, Long totalPages) {
		if (totalPages <= 10l || (actualPage + (pagesRight - 1)) > totalPages) {
			return totalPages;
		}
		if (actualPage + (pagesRight - 1) <= 10l) {
			return 10l;
		}
		return actualPage + (pagesRight - 1);
	}

	private Long getStartPage(Long actualPage) {
		if ((actualPage - pagesLeft) <= 0l) return 1l;
		return actualPage-pagesLeft;
	}



}<commit_msg>Working on fixing totalPage issue for navigation<commit_after>/*************************************************************************
 * Copyright 2014 Infield Design
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author <NAME> <<EMAIL>>
 * 
 *************************************************************************/

package com.infield.googlesearch;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Dictionary;
import java.util.LinkedList;
import java.util.List;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.osgi.framework.Constants;

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.customsearch.Customsearch;
import com.google.api.services.customsearch.CustomsearchRequestInitializer;
import com.google.api.services.customsearch.model.Result;
import com.google.api.services.customsearch.model.Search;
import com.google.api.services.customsearch.model.Search.SearchInformation;
import com.infield.googlesearch.model.ResultItem;
import com.infield.googlesearch.model.ResultList;


@Component(
		label = "Google Custom Search Service", 
		description = "Service used to call Google Custom Services.",
		immediate = true, 
		metatype = true)
@Service(GoogleSearchService.class)
@Properties({
	@Property(name = Constants.SERVICE_VENDOR, value = "Infield"),
	@Property(name = Constants.SERVICE_DESCRIPTION, value = "Google Custom Search service")
	 , 
	 @Property(
	 label = "Application Name", 
	 name = "cse.application.name", 
	 value = "",
	 description = "Be sure to specify the name of your application. "
	 + "If the application name is null or blank, the application "
	 + "will log a warning. Suggested format is "
	 + "'MyCompany-ProductName/1.0'.")
	 ,
	 @Property(
	 label = "API Key",
	 name = "cse.api.key", 
	 value = "", 
	 description = "API Key for the registered developer project for your application.")
	 ,
	 @Property(
	 label = "Context",
	 name = "cse.cx",
	 value = "", 
	 description = "The main search engine ID to scope the search query")
})
public class GoogleSearchService  {

	private final Logger log = LoggerFactory.getLogger(getClass());

	public static String apikey = "";
	public static String applicationname = "";
	public static String cx = "";
	public static Long num = 7l;
	public static Long pagesToShow = 10l;
	private Long pagesRight;
	private Long pagesLeft;
	private Customsearch customsearch;
	private LinkedList<ResultItem> resultItems;

	static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
	private static final long serialVersionUID = 1L;

	
	@Reference
	private SlingRepository repository;

	@Activate
	protected void activate(ComponentContext componentContext){
		configure(componentContext.getProperties());
	}
	
	
	protected void configure(Dictionary<?, ?> properties) {
		GoogleSearchService.apikey = PropertiesUtil.toString(properties.get("cse.api.key"), apikey);
		GoogleSearchService.applicationname = PropertiesUtil.toString(properties.get("cse.application.name"), applicationname);
		GoogleSearchService.cx = PropertiesUtil.toString(properties.get("cse.cx"),cx);
	 }


	public ResultList getResults(String q, String currentTab, long num, long pagesToShow) {
		//TODO: do XSS protection on Q
		ResultList resultList = new ResultList();

		try {
			
			pagesLeft = new BigDecimal(pagesToShow).divide(new BigDecimal(2), RoundingMode.UP).longValue();
			pagesRight = new BigDecimal(pagesToShow).divide(new BigDecimal(2), RoundingMode.DOWN).longValue();
			
			customsearch = new Customsearch.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, null)
			.setApplicationName(applicationname)
			.setGoogleClientRequestInitializer(new CustomsearchRequestInitializer(apikey))
			.build();
			Long actualPage = Long.valueOf(currentTab);
			com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list(q);
			
			list.setNum(num);
			list.setStart((num * (actualPage - 1)) + 1);
			list.setCx(cx);
			Search results = list.execute();
			SearchInformation searchInformation = results.getSearchInformation();

			resultItems = new LinkedList<ResultItem>();

			if (searchInformation != null){
				Long totalResults = searchInformation.getTotalResults();
				Long totalPages = new BigDecimal(totalResults).divide(new BigDecimal(num), RoundingMode.UP).longValue();
				resultList.setTotalPages(totalPages);
				resultList.setPagesToShow(pagesToShow);
				resultList.setTotalResults(searchInformation.getTotalResults());
				resultList.setFormattedSearchTime(searchInformation.getFormattedSearchTime());
				resultList.setFormattedTotalResults(searchInformation.getFormattedTotalResults());
				resultList.setSearchTime(searchInformation.getSearchTime());
				resultList.setStartPage(getStartPage(actualPage));
				resultList.setEndPage(getEndPage(actualPage,totalPages));
				resultList.setCurrentTab(actualPage);


				List <Result> searchResults = results.getItems();
				for (Result searchResult: searchResults){
					ResultItem resultItem = new ResultItem();
					resultItem.setCacheId(searchResult.getCacheId());
					resultItem.setDisplayLink(searchResult.getDisplayLink());
					resultItem.setFileFormat(searchResult.getFileFormat());
					resultItem.setFormattedUrl(searchResult.getFormattedUrl());
					resultItem.setHtmlFormattedUrl(searchResult.getHtmlFormattedUrl());
					resultItem.setHtmlSnippet(searchResult.getHtmlSnippet());
					resultItem.setHtmlTitle(searchResult.getHtmlTitle());
					resultItem.setKind(searchResult.getKind());
					//resultItem.setLabels(searchResult.getLabels());
					resultItem.setLink(searchResult.getLink());
					resultItem.setMime(searchResult.getMime());
					resultItem.setSnippet(searchResult.getSnippet());
					resultItem.setTitle(searchResult.getTitle());
					resultItems.add(resultItem);
				}


				resultList.setResultItems(resultItems);

			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return resultList;
	}

	private Long getEndPage(Long actualPage, Long totalPages) {
		if (totalPages <= pagesToShow || (actualPage + (pagesRight - 1)) > totalPages) {
			return totalPages;
		}
		if (actualPage + (pagesRight - 1) <= pagesToShow) {
			return pagesToShow;
		}
		return actualPage + (pagesRight - 1);
	}

	private Long getStartPage(Long actualPage) {
		if ((actualPage - pagesLeft) <= 0l) return 1l;
		return actualPage-pagesLeft;
	}



}
{'fraction_non_alphanumeric': 0.12656532511886426, 'fraction_numerical': 0.003884015268197951, 'mean_word_length': 5.178733967728589, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6817441', 'n_tokens_mistral': 4629, 'n_tokens_neox': 4123, 'n_words': 949}