<commit_before>/*
 * Copyright 2004-2006 the original author or authors.
 * 
 * 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.
 */

package org.compass.spring.web.mvc;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.compass.core.CompassCallback;
import org.compass.core.CompassDetachedHits;
import org.compass.core.CompassException;
import org.compass.core.CompassHits;
import org.compass.core.CompassQuery;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

/**
 * A general Spring's MVC Controller that perform the search operation of
 * <code>Compass</code>.
 * <p/>
 * Will perform the search operation on the
 * <code>Compass</code> instance using the query supplied by the
 * {@link CompassSearchCommand}.
 * <p/> If you wish to enable the pagination
 * feature, you must set the <code>pageSize</code> property on the controller,
 * as well as providing the <code>page</code> number property on the
 * {@link CompassSearchCommand}.
 * <p/> The controller has two views to be set,
 * the <code>searchView</code>, which is the view that holds the screen which
 * the user will initiate the search operation, and the
 * <code>searchResultsView</code>, which will show the results of the search
 * operation (they can be the same page).
 * <p/> The results of the search
 * operation will be saved under the
 * <code>searchResultsName<code>, which defaults to "searchResults".
 *
 * @author kimchy
 */
public class CompassSearchController extends AbstractCompassCommandController {

    private String searchView;

    private String searchResultsView;

    private String searchResultsName = "searchResults";

    private Integer pageSize;

    public CompassSearchController() {
        setCommandClass(CompassSearchCommand.class);
    }

    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
        if (searchView == null) {
            throw new IllegalArgumentException("Must set the searchView property");
        }
        if (searchResultsView == null) {
            throw new IllegalArgumentException("Must set the serachResultsView property");
        }
    }

    protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command,
                                  BindException errors) throws Exception {
        final CompassSearchCommand searchCommand = (CompassSearchCommand) command;
        if (!StringUtils.hasText(searchCommand.getQuery())) {
            return new ModelAndView(getSearchView(), getCommandName(), searchCommand);
        }
        CompassSearchResults searchResults;
        searchResults = (CompassSearchResults) getCompassTemplate().execute(
                CompassTransaction.TransactionIsolation.READ_ONLY_READ_COMMITTED, new CompassCallback() {
            public Object doInCompass(CompassSession session) throws CompassException {
                return performSearch(searchCommand, session);
            }
        });
        HashMap data = new HashMap();
        data.put(getCommandName(), searchCommand);
        data.put(getSearchResultsName(), searchResults);
        return new ModelAndView(getSearchResultsView(), data);
    }

    protected CompassSearchResults performSearch(CompassSearchCommand searchCommand, CompassSession session) {
        long time = System.currentTimeMillis();
        CompassQuery query = buildQuery(searchCommand, session);
        CompassHits hits = query.hits();
        CompassDetachedHits detachedHits;
        CompassSearchResults.Page[] pages = null;
        if (pageSize == null) {
            doProcessBeforeDetach(searchCommand, session, hits, -1, -1);
            detachedHits = hits.detach();
        } else {
            int iPageSize = pageSize.intValue();
            int page = 0;
            int hitsLength = hits.getLength();
            if (searchCommand.getPage() != null) {
                page = searchCommand.getPage().intValue();
            }
            int from = page * iPageSize;
            if (from > hits.getLength()) {
                from = hits.getLength() - iPageSize;
                doProcessBeforeDetach(searchCommand, session, hits, from, hitsLength);
                detachedHits = hits.detach(from, hitsLength);
            } else if ((from + iPageSize) > hitsLength) {
                doProcessBeforeDetach(searchCommand, session, hits, from, hitsLength);
                detachedHits = hits.detach(from, hitsLength);<commit_msg>CMP-296 in CompassSearchController doProcessAfterDetach is never called if page number not supplied<commit_after>/*
 * Copyright 2004-2006 the original author or authors.
 * 
 * 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.
 */

package org.compass.spring.web.mvc;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.compass.core.CompassCallback;
import org.compass.core.CompassDetachedHits;
import org.compass.core.CompassException;
import org.compass.core.CompassHits;
import org.compass.core.CompassQuery;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

/**
 * A general Spring's MVC Controller that perform the search operation of
 * <code>Compass</code>.
 * <p/>
 * Will perform the search operation on the
 * <code>Compass</code> instance using the query supplied by the
 * {@link CompassSearchCommand}.
 * <p/> If you wish to enable the pagination
 * feature, you must set the <code>pageSize</code> property on the controller,
 * as well as providing the <code>page</code> number property on the
 * {@link CompassSearchCommand}.
 * <p/> The controller has two views to be set,
 * the <code>searchView</code>, which is the view that holds the screen which
 * the user will initiate the search operation, and the
 * <code>searchResultsView</code>, which will show the results of the search
 * operation (they can be the same page).
 * <p/> The results of the search
 * operation will be saved under the
 * <code>searchResultsName<code>, which defaults to "searchResults".
 *
 * @author kimchy
 */
public class CompassSearchController extends AbstractCompassCommandController {

    private String searchView;

    private String searchResultsView;

    private String searchResultsName = "searchResults";

    private Integer pageSize;

    public CompassSearchController() {
        setCommandClass(CompassSearchCommand.class);
    }

    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
        if (searchView == null) {
            throw new IllegalArgumentException("Must set the searchView property");
        }
        if (searchResultsView == null) {
            throw new IllegalArgumentException("Must set the serachResultsView property");
        }
    }

    protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command,
                                  BindException errors) throws Exception {
        final CompassSearchCommand searchCommand = (CompassSearchCommand) command;
        if (!StringUtils.hasText(searchCommand.getQuery())) {
            return new ModelAndView(getSearchView(), getCommandName(), searchCommand);
        }
        CompassSearchResults searchResults;
        searchResults = (CompassSearchResults) getCompassTemplate().execute(
                CompassTransaction.TransactionIsolation.READ_ONLY_READ_COMMITTED, new CompassCallback() {
            public Object doInCompass(CompassSession session) throws CompassException {
                return performSearch(searchCommand, session);
            }
        });
        HashMap data = new HashMap();
        data.put(getCommandName(), searchCommand);
        data.put(getSearchResultsName(), searchResults);
        return new ModelAndView(getSearchResultsView(), data);
    }

    protected CompassSearchResults performSearch(CompassSearchCommand searchCommand, CompassSession session) {
        long time = System.currentTimeMillis();
        CompassQuery query = buildQuery(searchCommand, session);
        CompassHits hits = query.hits();
        CompassDetachedHits detachedHits;
        CompassSearchResults.Page[] pages = null;
        if (pageSize == null) {
            doProcessBeforeDetach(searchCommand, session, hits, -1, -1);
            detachedHits = hits.detach();
            doProcessAfterDetach(searchCommand, session, detachedHits);
        } else {
            int iPageSize = pageSize.intValue();
            int page = 0;
            int hitsLength = hits.getLength();
            if (searchCommand.getPage() != null) {
                page = searchCommand.getPage().intValue();
            }
            int from = page * iPageSize;
            if (from > hits.getLength()) {
                from = hits.getLength() - iPageSize;
                doProcessBeforeDetach(searchCommand, session, hits, from, hitsLength);
                detachedHits = hits.detach(from, hitsLength);
            } else if ((from + iPageSize) > hitsLength) {
                doProcessBeforeDetach(searchCommand, session, hits, from, hitsLength);
                detachedHits = hits.detach(from, hitsLength);
{'fraction_non_alphanumeric': 0.08555744841945864, 'fraction_numerical': 0.0031232254400908573, 'mean_word_length': 3.25402576489533, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '259021', 'n_tokens_mistral': 2674, 'n_tokens_neox': 2610, 'n_words': 843}