Changeset 4501


Ignore:
Timestamp:
02/12/14 13:19:02 (10 years ago)
Author:
twagoo
Message:

Created facet fields provider that uses a facet fields service to get all field objects given the current selection.
Made an implementation of this service that uses the search result dao to get the fields from the SOLR

Location:
vlo/branches/vlo-3.0/vlo-web-app
Files:
5 added
3 deleted
7 edited
1 copied
2 moved

Legend:

Unmodified
Added
Removed
  • vlo/branches/vlo-3.0/vlo-web-app/pom.xml

    r4498 r4501  
    5050            <artifactId>spring-context</artifactId>
    5151            <version>${spring.version}</version>
    52             <type>jar</type>
    5352            <exclusions>
    5453                <!--
     
    6665            <artifactId>spring-web</artifactId>
    6766            <version>${spring.version}</version>
    68             <type>jar</type>
     67        </dependency>
     68        <dependency>
     69            <groupId>org.springframework</groupId>
     70            <artifactId>spring-test</artifactId>
     71            <version>${spring.version}</version>
     72            <scope>test</scope>
    6973        </dependency>
    7074       
     
    9498            <version>${solr.version}</version>
    9599            <type>jar</type>
     100        </dependency>
     101        <!-- JMOCK for mocking -->
     102        <dependency>
     103            <groupId>org.jmock</groupId>
     104            <artifactId>jmock-junit4</artifactId>
     105            <version>2.6.0</version>
     106            <scope>test</scope>
     107            <exclusions>
     108                <exclusion>
     109                    <artifactId>junit-dep</artifactId>
     110                    <groupId>junit</groupId>
     111                </exclusion>
     112            </exclusions>
    96113        </dependency>
    97114    </dependencies>
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/VloWicketApplication.java

    r4469 r4501  
    44import org.apache.wicket.markup.html.WebPage;
    55import org.apache.wicket.protocol.http.WebApplication;
     6import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
     7import org.springframework.beans.BeansException;
     8import org.springframework.context.ApplicationContext;
     9import org.springframework.context.ApplicationContextAware;
    610
    711/**
    8  * Application object for your web application.
    9  * If you want to run this application without deploying, run the Start class.
    10  * 
     12 * Application object for your web application. If you want to run this
     13 * application without deploying, run the Start class.
     14 *
    1115 * @see eu.clarin.cmdi.Start#main(String[])
    1216 */
    13 public class VloWicketApplication extends WebApplication
    14 {
    15         /**
    16          * @see org.apache.wicket.Application#getHomePage()
    17          */
    18         @Override
    19         public Class<? extends WebPage> getHomePage()
    20         {
    21                 return FacetedSearchPage.class;
    22         }
     17public class VloWicketApplication extends WebApplication implements ApplicationContextAware {
    2318
    24         /**
    25          * @see org.apache.wicket.Application#init()
    26          */
    27         @Override
    28         public void init()
    29         {
    30                 super.init();
     19    private ApplicationContext applicationContext;
    3120
    32                 // add your configuration here
    33         }
     21    /**
     22     * @return the home page of this application
     23     * @see org.apache.wicket.Application#getHomePage()
     24     */
     25    @Override
     26    public Class<? extends WebPage> getHomePage() {
     27        return FacetedSearchPage.class;
     28    }
     29
     30    /**
     31     * @see org.apache.wicket.Application#init()
     32     */
     33    @Override
     34    public void init() {
     35        super.init();
     36        // this listener will inject any spring beans that need to be autowired
     37        getComponentInstantiationListeners().add(new SpringComponentInjector(this, applicationContext));
     38    }
     39
     40    /**
     41     * Method needed for dynamic injection of application context (as happens in
     42     * unit tests)
     43     *
     44     * @param applicationContext
     45     * @throws BeansException
     46     */
     47    @Override
     48    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
     49        this.applicationContext = applicationContext;
     50    }
    3451}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/components/FacetsPanel.java

    r4500 r4501  
    1717package eu.clarin.cmdi.vlo.components;
    1818
     19import eu.clarin.cmdi.vlo.pojo.FacetSelection;
     20import eu.clarin.cmdi.vlo.service.FacetFieldsService;
     21import eu.clarin.cmdi.vlo.service.impl.FacetFieldsDataProvider;
    1922import org.apache.solr.client.solrj.response.FacetField;
    20 import org.apache.wicket.markup.html.list.ListItem;
    21 import org.apache.wicket.markup.html.list.ListView;
    2223import org.apache.wicket.markup.html.panel.Panel;
     24import org.apache.wicket.markup.repeater.Item;
     25import org.apache.wicket.markup.repeater.data.DataView;
     26import org.apache.wicket.markup.repeater.data.IDataProvider;
    2327import org.apache.wicket.model.util.ListModel;
     28import org.apache.wicket.spring.injection.annot.SpringBean;
    2429
    2530/**
     
    2934public class FacetsPanel extends Panel {
    3035
    31     public FacetsPanel(String id, ListModel<FacetField> model) {
     36    @SpringBean
     37    private FacetFieldsService facetFieldsService;
     38   
     39    public FacetsPanel(String id, ListModel<FacetSelection> model) {
    3240        super(id, model);
    33         add(new ListView<FacetField>("facets", model) {
     41
     42        final IDataProvider<FacetField> provider = new FacetFieldsDataProvider(facetFieldsService, model);
     43        add(new DataView<FacetField>("facets", provider) {
    3444
    3545            @Override
    36             protected void populateItem(ListItem<FacetField> item) {
    37                 //TODO: Check whether a value has been selected or not
     46            protected void populateItem(Item<FacetField> item) {
    3847                item.add(new FacetPanel("facet", item.getModel()));
    3948            }
    4049        });
    4150    }
    42 
    4351}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/config/VloSpringConfig.java

    r4498 r4501  
    1818
    1919import eu.clarin.cmdi.vlo.VloWicketApplication;
     20import eu.clarin.cmdi.vlo.service.FacetFieldsService;
     21import eu.clarin.cmdi.vlo.service.SearchResultsDao;
     22import eu.clarin.cmdi.vlo.service.SolrQueryFactory;
     23import eu.clarin.cmdi.vlo.service.impl.SearchResultsDaoImpl;
     24import eu.clarin.cmdi.vlo.service.impl.SolrFacetFieldsService;
     25import eu.clarin.cmdi.vlo.service.impl.SolrQueryFactoryImpl;
     26import org.springframework.beans.factory.annotation.Value;
    2027import org.springframework.context.annotation.Bean;
    2128import org.springframework.context.annotation.Configuration;
     
    2431 * Annotation based Spring configuration for the VLO web application.
    2532 *
    26  * Note: this works because {@link org.apache.wicket.spring.SpringWebApplicationFactory}
    27  * is used in place of the standard Wicket application factory and annotation
    28  * driven configuration is enabled in WEB-INF/applicationContext.xml
     33 * Note: this works because
     34 * {@link org.apache.wicket.spring.SpringWebApplicationFactory} is used in place
     35 * of the standard Wicket application factory and annotation driven
     36 * configuration is enabled in WEB-INF/applicationContext.xml
    2937 *
    3038 * @author twagoo
     
    3846     */
    3947    @Bean
    40     VloWicketApplication webApplication() {
     48    public VloWicketApplication webApplication() {
    4149        return new VloWicketApplication();
    4250    }
     51
     52    @Bean
     53    public VloConfig vloConfig() {
     54        VloConfig.readPackagedConfig();
     55        return VloConfig.config;
     56    }
     57
     58    @Bean
     59    public FacetFieldsService facetFieldsService() {
     60        return new SolrFacetFieldsService(searchResultsDao(), queryFactory());
     61    }
     62
     63    @Bean
     64    public SearchResultsDao searchResultsDao() {
     65        return new SearchResultsDaoImpl(vloConfig().getSolrUrl());
     66    }
     67
     68    public SolrQueryFactory queryFactory() {
     69        return new SolrQueryFactoryImpl();
     70    }
    4371}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/pages/FacetedSearchPage.java

    r4500 r4501  
    22
    33import eu.clarin.cmdi.vlo.components.FacetsPanel;
     4import eu.clarin.cmdi.vlo.pojo.Facet;
     5import eu.clarin.cmdi.vlo.pojo.FacetSelection;
    46import java.util.Arrays;
    5 import org.apache.solr.client.solrj.response.FacetField;
     7import java.util.Collections;
    68import org.apache.wicket.request.mapper.parameter.PageParameters;
    79import org.apache.wicket.markup.html.WebPage;
     
    1416    public FacetedSearchPage(final PageParameters parameters) {
    1517        super(parameters);
    16        
    17         add(new FacetsPanel("facets", new ListModel<FacetField>(Arrays.asList(
    18                 new FacetField("Language"),
    19                 new FacetField("Resource type")))));
     18        final FacetSelection languageSelection = new FacetSelection(new Facet("Language"), Collections.singleton("Dutch"));
     19        final FacetSelection typeSelection = new FacetSelection(new Facet("Resource type"), Collections.<String>emptySet());
     20
     21        add(new FacetsPanel("facets",
     22                new ListModel<FacetSelection>(
     23                        Arrays.asList(languageSelection, typeSelection)
     24                )));
    2025    }
    2126}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/SearchResultsDao.java

    r4480 r4501  
    1 package eu.clarin.cmdi.vlo.service.impl;
     1/*
     2 * Copyright (C) 2014 CLARIN
     3 *
     4 * This program is free software: you can redistribute it and/or modify
     5 * it under the terms of the GNU General Public License as published by
     6 * the Free Software Foundation, either version 3 of the License, or
     7 * (at your option) any later version.
     8 *
     9 * This program is distributed in the hope that it will be useful,
     10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 * GNU General Public License for more details.
     13 *
     14 * You should have received a copy of the GNU General Public License
     15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 */
    217
    3 import eu.clarin.cmdi.vlo.FacetConstants;
     18package eu.clarin.cmdi.vlo.service;
     19
    420import java.util.List;
    521import org.apache.solr.client.solrj.SolrQuery;
    622import org.apache.solr.client.solrj.response.FacetField;
    7 import org.apache.solr.client.solrj.response.QueryResponse;
    823import org.apache.solr.common.SolrDocumentList;
    924
    10 public class SearchResultsDao extends SolrDao {
     25/**
     26 *
     27 * @author twagoo
     28 */
     29public interface SearchResultsDao {
    1130
    12     //private final static Logger LOG = LoggerFactory.getLogger(SearchResultsDao.class);
     31    SolrDocumentList getDocIdList(SolrQuery query);
    1332
    14     private QueryResponse response;
     33    List<FacetField> getFacets(SolrQuery query);
    1534
    16     public SearchResultsDao() {
    17         super();
    18     }
    19 
    20     public SolrDocumentList getResults(SolrQuery query) {
    21         setDefaultSortField(query);
    22         response = fireQuery(query);
    23         SolrDocumentList results = response.getResults();
    24         return results;
    25     }
    26 
    27     public List<FacetField> getFacets(SolrQuery query) {
    28         response = fireQuery(query);
    29         return response.getFacetFields();
    30     }
    31 
    32     public SolrDocumentList getDocIdList(SolrQuery query) {
    33         setDefaultSortField(query);
    34         query.setFields(FacetConstants.FIELD_ID);
    35         query.setFacet(false);
    36         query.setStart(0);
    37         query.setRows(Integer.MAX_VALUE);
    38         QueryResponse queryResponse = fireQuery(query);
    39         return queryResponse.getResults();
    40     }
    41 
    42     private void setDefaultSortField(SolrQuery query) {
    43         if (query.getSortField() == null) {
    44             query.setSortField(FacetConstants.FIELD_NAME, SolrQuery.ORDER.asc);
    45         }
    46     }
    47 
     35    SolrDocumentList getResults(SolrQuery query);
     36   
    4837}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/AutoCompleteDao.java

    r4480 r4501  
    1515 *
    1616 */
    17 public class AutoCompleteDao extends SolrDao {
     17public class AutoCompleteDao extends SolrDaoImpl {
    1818
     19    public AutoCompleteDao(String solrUrl) {
     20        super(solrUrl);
     21    }
     22   
    1923    /**
    2024     * Returns list of suggestions for incomplete input (used for autocomplete
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/SearchResultsDaoImpl.java

    r4480 r4501  
    11package eu.clarin.cmdi.vlo.service.impl;
    22
     3import eu.clarin.cmdi.vlo.service.SearchResultsDao;
    34import eu.clarin.cmdi.vlo.FacetConstants;
    45import java.util.List;
     
    89import org.apache.solr.common.SolrDocumentList;
    910
    10 public class SearchResultsDao extends SolrDao {
     11public class SearchResultsDaoImpl extends SolrDaoImpl implements SearchResultsDao {
    1112
    12     //private final static Logger LOG = LoggerFactory.getLogger(SearchResultsDao.class);
    13 
    14     private QueryResponse response;
    15 
    16     public SearchResultsDao() {
    17         super();
     13    public SearchResultsDaoImpl(String solrUrl) {
     14        super(solrUrl);
    1815    }
    1916
     17    @Override
    2018    public SolrDocumentList getResults(SolrQuery query) {
    21         setDefaultSortField(query);
    22         response = fireQuery(query);
    23         SolrDocumentList results = response.getResults();
    24         return results;
     19        setDefaultSortField(query);
     20        QueryResponse response = fireQuery(query);
     21        SolrDocumentList results = response.getResults();
     22        return results;
    2523    }
    2624
     25    @Override
    2726    public List<FacetField> getFacets(SolrQuery query) {
    28         response = fireQuery(query);
    29         return response.getFacetFields();
     27        QueryResponse response = fireQuery(query);
     28        return response.getFacetFields();
    3029    }
    3130
     31    @Override
    3232    public SolrDocumentList getDocIdList(SolrQuery query) {
    33         setDefaultSortField(query);
    34         query.setFields(FacetConstants.FIELD_ID);
    35         query.setFacet(false);
    36         query.setStart(0);
    37         query.setRows(Integer.MAX_VALUE);
    38         QueryResponse queryResponse = fireQuery(query);
    39         return queryResponse.getResults();
     33        query = query.getCopy();
     34        setDefaultSortField(query);
     35        query.setFields(FacetConstants.FIELD_ID);
     36        query.setFacet(false);
     37        query.setStart(0);
     38        query.setRows(Integer.MAX_VALUE);
     39        QueryResponse queryResponse = fireQuery(query);
     40        return queryResponse.getResults();
    4041    }
    4142
    4243    private void setDefaultSortField(SolrQuery query) {
    43         if (query.getSortField() == null) {
    44             query.setSortField(FacetConstants.FIELD_NAME, SolrQuery.ORDER.asc);
    45         }
     44        if (query.getSortField() == null) {
     45            query.setSort(SolrQuery.SortClause.asc(FacetConstants.FIELD_NAME));
     46        }
    4647    }
    4748
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/SolrDaoImpl.java

    r4480 r4501  
    1616import org.slf4j.LoggerFactory;
    1717
    18 public class SolrDao {
     18public class SolrDaoImpl {
    1919
    20     private final static Logger LOG = LoggerFactory.getLogger(SolrDao.class);
     20    private final static Logger LOG = LoggerFactory.getLogger(SolrDaoImpl.class);
    2121    private final HttpSolrServer solrServer;
    2222
    23     public SolrDao() {
    24         String solrUrl;
    25         solrUrl = VloConfig.getSolrUrl();
     23    public SolrDaoImpl(String solrUrl) {
    2624        solrServer = new HttpSolrServer(solrUrl);
    2725    }
  • vlo/branches/vlo-3.0/vlo-web-app/src/test/java/eu/clarin/cmdi/vlo/pages/TestFacetedSearchPage.java

    r4469 r4501  
    22
    33import eu.clarin.cmdi.vlo.VloWicketApplication;
     4import eu.clarin.cmdi.vlo.config.VloSpringConfig;
     5import eu.clarin.cmdi.vlo.service.FacetFieldsService;
     6import java.util.Arrays;
     7import java.util.List;
     8import org.apache.solr.client.solrj.response.FacetField;
    49import org.apache.wicket.util.tester.WicketTester;
     10import org.jmock.Expectations;
     11import org.jmock.Mockery;
     12import org.jmock.integration.junit4.JUnit4Mockery;
    513import org.junit.Before;
    614import org.junit.Test;
     15import org.junit.runner.RunWith;
     16import org.springframework.beans.factory.annotation.Autowired;
     17import org.springframework.context.annotation.Bean;
     18import org.springframework.context.annotation.ComponentScan;
     19import org.springframework.context.annotation.Configuration;
     20import org.springframework.test.context.ContextConfiguration;
     21import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     22import org.springframework.test.context.support.AnnotationConfigContextLoader;
    723
    824/**
    9  * Simple test using the WicketTester
     25 * Abstract base class for tests that require dependency injection of (mock)
     26 * objects and services. Based on blog post by Petri Kainulainen found at
     27 * {@link http://www.petrikainulainen.net/programming/tips-and-tricks/mocking-spring-beans-with-apache-wicket-and-mockito/}
     28 *
    1029 */
    11 public class TestFacetedSearchPage
    12 {
    13         private WicketTester tester;
     30@RunWith(SpringJUnit4ClassRunner.class)
     31@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
     32public class TestFacetedSearchPage {
    1433
    15         @Before
    16         public void setUp()
    17         {
    18                 tester = new WicketTester(new VloWicketApplication());
    19         }
     34    @Configuration
     35    @ComponentScan
     36    static class ContextConfiguration extends VloSpringConfig {
    2037
    21         @Test
    22         public void homepageRendersSuccessfully()
    23         {
    24                 //start and render the test page
    25                 tester.startPage(FacetedSearchPage.class);
     38        @Bean
     39        public Mockery mockery() {
     40            return new JUnit4Mockery();
     41        }
    2642
    27                 //assert rendered page class
    28                 tester.assertRenderedPage(FacetedSearchPage.class);
    29         }
     43        @Override
     44        public FacetFieldsService facetFieldsService() {
     45            return mockery().mock(FacetFieldsService.class);
     46        }
     47
     48    }
     49
     50    private WicketTester tester;
     51    @Autowired
     52    private VloWicketApplication application;
     53    @Autowired
     54    private Mockery mockery;
     55    @Autowired
     56    private FacetFieldsService facetFieldsService;
     57
     58    @Before
     59    public void setUp() {
     60        tester = new WicketTester(application);
     61    }
     62
     63    @Test
     64    public void homepageRendersSuccessfully() {
     65        // mock behaviour of facet fields service
     66        mockery.checking(new Expectations() {
     67            {
     68                oneOf(facetFieldsService).getFacetFieldCount();
     69                will(returnValue(2L));
     70                oneOf(facetFieldsService).getFacetFields(with(any(List.class)));
     71                will(returnValue(Arrays.asList(new FacetField("language"), new FacetField("resource class"))));
     72            }
     73        });
     74
     75        //start and render the test page
     76        tester.startPage(FacetedSearchPage.class);
     77
     78        //assert rendered page class
     79        tester.assertRenderedPage(FacetedSearchPage.class);
     80    }
    3081}
Note: See TracChangeset for help on using the changeset viewer.