Commit 844e7a96 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '500-sitemap-add-sitemap-genus-xml' into 'master'

Resolve "Sitemap: Add sitemap-genus.xml"

Closes #500

See merge request genesys-pgr/genesys-server!524
parents baf97745 cea7791b
/*
* Copyright 2020 Global Crop Diversity Trust
*
* 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.genesys2.server.api.v1;
import java.util.List;
import io.swagger.annotations.Api;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Maxym Borodenko
*/
@RestController("taxonomyApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = TaxonomyController.CONTROLLER_URL)
@Api(tags = {"taxonomy"})
public class TaxonomyController extends ApiBaseController {
/** The Constant CONTROLLER_URL. */
public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/taxonomy";
/** The taxonomy service **/
@Autowired
private TaxonomyService taxonomyService;
@PostMapping(value = "/", produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Taxonomy2> listTaxonomies(@RequestBody TaxonomyFilter taxonomyFilter) {
LOG.info("Listing taxonomies");
return taxonomyService.list(taxonomyFilter);
}
}
/*
* Copyright 2020 Global Crop Diversity Trust
*
* 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.genesys2.server.api.v2;
import java.util.List;
import io.swagger.annotations.Api;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Maxym Borodenko
*/
@RestController("taxonomyApi2")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = TaxonomyController.CONTROLLER_URL)
@Api(tags = {"taxonomy"})
public class TaxonomyController extends ApiBaseController {
/** The Constant CONTROLLER_URL. */
public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/taxonomy";
/** The taxonomy service **/
@Autowired
private TaxonomyService taxonomyService;
@PostMapping(value = "/", produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Taxonomy2> listTaxonomies(@RequestBody TaxonomyFilter taxonomyFilter) {
LOG.info("Listing taxonomies");
return taxonomyService.list(taxonomyFilter);
}
}
......@@ -47,6 +47,7 @@ import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.SubsetFilter;
import org.genesys2.server.service.worker.AccessionProcessor;
......@@ -87,6 +88,9 @@ public class SitemapXMLController {
@Value("${frontend.url}")
private String frontendUrl;
@Autowired
private TaxonomyService taxonomyService;
@Autowired
private GeoService geoService;
......@@ -147,7 +151,8 @@ public class SitemapXMLController {
new SitemapPage("/sitemap-partner.xml"),
new SitemapPage("/sitemap-subset.xml"),
new SitemapPage("/sitemap-dataset.xml"),
new SitemapPage("/sitemap-descriptorlist.xml")
new SitemapPage("/sitemap-descriptorlist.xml"),
new SitemapPage("/sitemap-genus.xml")
};
private final SitemapPage[] sitemapContentPages = new SitemapPage[] {
......@@ -177,6 +182,28 @@ public class SitemapXMLController {
}
@RequestMapping(value = "/sitemap-genus.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public void sitemapGenusXml(HttpServletResponse response) throws IOException {
response.setContentType("text/xml");
final BufferedWriter sb = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()), 2048);
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
taxonomyService.getAllGenera().stream().filter(g -> g.matches("^[a-zA-Z ]+$")).forEach(genus -> {
try {
writePage(response, sb, "/t/" + genus, Frequency.MONTHLY, 0.4);
sb.flush();
} catch (IOException e) {
LOG.warn(e.getMessage());
}
});
sb.append("</urlset>");
sb.flush();
response.flushBuffer();
}
@RequestMapping(value = "/sitemap-content.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
@ResponseBody
public String sitemapContentXml(HttpServletResponse response) {
......
......@@ -26,8 +26,9 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> {
public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long>, QueryDslPredicateExecutor<Taxonomy2> {
@Query("select distinct t.genus from Taxonomy2 t where t.genus like ?1")
List<String> autocompleteGenus(String term, Pageable page);
......@@ -92,4 +93,7 @@ public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> {
@Query("select t.id from Taxonomy2 t where t.id = t.taxSpecies and t.taxonName in (?1)")
List<Long> findTaxSpeciesId(List<String> speciesNames);
@Query("select distinct t.genus from Taxonomy2 t")
List<String> getAllGenera();
}
......@@ -20,6 +20,7 @@ import java.util.List;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.service.filter.TaxonomyFilter;
public interface TaxonomyService {
......@@ -39,6 +40,10 @@ public interface TaxonomyService {
Taxonomy2 get(String genus);
List<Taxonomy2> list(TaxonomyFilter filter);
List<String> getAllGenera();
Taxonomy2 get(String genus, String species);
Taxonomy2 find(String genus, String species, String spAuthor, String subtaxa, String subtAuthor);
......
......@@ -15,6 +15,12 @@
*/
package org.genesys2.server.service.filter;
import static org.genesys2.server.model.genesys.QTaxonomy2.taxonomy2;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import com.querydsl.core.types.Predicate;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.AuditedVersionedModelFilter;
......@@ -24,23 +30,11 @@ import org.genesys2.server.model.genesys.QTaxonomy2;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.persistence.Taxonomy2Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static org.genesys2.server.model.genesys.QTaxonomy2.taxonomy2;
/**
* The Class TaxonomyFilter.
*/
public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter, Taxonomy2> {
private static Taxonomy2Repository taxonomyRepository;
static {
taxonomyRepository = CurrentApplicationContext.getContext().getBean(Taxonomy2Repository.class);
}
/** The genus. */
public Set<String> genus;
......@@ -73,7 +67,8 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
*/
public List<Predicate> collectPredicates(final QTaxonomy2 taxonomy) {
final List<Predicate> predicates = super.collectPredicates(taxonomy2, taxonomy2._super._super);
final Taxonomy2Repository taxonomyRepository = CurrentApplicationContext.getContext().getBean(Taxonomy2Repository.class);
if (CollectionUtils.isNotEmpty(genus) && taxonomyRepository != null) {
// We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box
List<Long> taxGenus = taxonomyRepository.findTaxGenusId(new ArrayList<>(genus));
......@@ -89,7 +84,6 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
predicates.add(taxonName.buildQuery(taxonomy.taxonName));
}
if (CollectionUtils.isNotEmpty(genusSpecies) && taxonomyRepository != null) {
// We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box
List<Long> taxSpecies = taxonomyRepository.findTaxSpeciesId(new ArrayList<>(genusSpecies));
......
......@@ -20,12 +20,15 @@ import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import com.google.api.client.util.Lists;
import com.querydsl.core.BooleanBuilder;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.persistence.Taxonomy2Repository;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -203,6 +206,20 @@ public class TaxonomyServiceImpl implements TaxonomyService {
return find(genus, "sp.", "", "", "");
}
@Override
public List<Taxonomy2> list(TaxonomyFilter filter) {
final BooleanBuilder predicate = new BooleanBuilder();
if (filter != null) {
predicate.and(filter.buildPredicate());
}
return Lists.newArrayList(taxonomy2Repository.findAll(predicate));
}
@Override
public List<String> getAllGenera() {
return taxonomy2Repository.getAllGenera();
}
@Override
@Transactional
public void cleanupTaxonomies() {
......
/*
* Copyright 2020 Global Crop Diversity Trust
*
* 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.genesys.test.server.api;
import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.persistence.Taxonomy2Repository;
import org.genesys2.server.service.TaxonomyService;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Maxym Borodenko
*/
public abstract class AbstractTaxonomyControllerTest extends AbstractApiTest {
@Autowired
protected TaxonomyService taxonomyService;
@Autowired
protected Taxonomy2Repository taxonomyRepository;
protected static final String GENUS_1 = "Abelia";
protected static final String GENUS_2 = "Hordeum";
@Before
@Override
@Transactional
public void beforeTest() throws Exception {
super.beforeTest();
assertNotNull(ensureTaxonomy(GENUS_1));
assertNotNull(ensureTaxonomy(GENUS_2));
assertEquals(2, taxonomyRepository.count());
}
@After
@Override
@Transactional
public void cleanup() throws Exception {
super.cleanup();
taxonomyRepository.deleteAll();
}
private Taxonomy2 ensureTaxonomy(String genus) {
Taxonomy2 taxon = new Taxonomy2();
taxon.setGenus(genus);
return taxonomyService.ensureTaxonomy(taxon);
}
}
/*
* Copyright 2020 Global Crop Diversity Trust
*
* 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.genesys.test.server.api.v1;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.google.common.collect.Sets;
import org.genesys.test.server.api.AbstractTaxonomyControllerTest;
import org.genesys2.server.api.v1.TaxonomyController;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.junit.Test;
import org.springframework.http.MediaType;
/**
* @author Maxym Borodenko
*/
public class TaxonomyControllerTest extends AbstractTaxonomyControllerTest {
@Test
public void listTaxonomiesTest() throws Exception {
assertEquals(2, taxonomyRepository.count());
TaxonomyFilter filter = new TaxonomyFilter();
filter.genus = Sets.newHashSet(GENUS_1);
/*@formatter:off*/
mockMvc
.perform(post(TaxonomyController.CONTROLLER_URL.concat("/"))
.contentType(MediaType.APPLICATION_JSON)
.content(verboseMapper.writeValueAsString(filter)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$.[0].genus", is(GENUS_1)));
/*@formatter:on*/
}
}
/*
* Copyright 2020 Global Crop Diversity Trust
*
* 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.genesys.test.server.api.v2;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.google.common.collect.Sets;
import org.genesys.test.server.api.AbstractTaxonomyControllerTest;
import org.genesys2.server.api.v1.TaxonomyController;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.junit.Test;
import org.springframework.http.MediaType;
/**
* @author Maxym Borodenko
*/
public class TaxonomyControllerTest extends AbstractTaxonomyControllerTest {
@Test
public void listTaxonomiesTest() throws Exception {
assertEquals(2, taxonomyRepository.count());
TaxonomyFilter filter = new TaxonomyFilter();
filter.genus = Sets.newHashSet(GENUS_1);
/*@formatter:off*/
mockMvc
.perform(post(TaxonomyController.CONTROLLER_URL.concat("/"))
.contentType(MediaType.APPLICATION_JSON)
.content(verboseMapper.writeValueAsString(filter)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$.[0].genus", is(GENUS_1)));
/*@formatter:on*/
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment