Commit 9973512e authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

Sitemap update

parent 0ff8fee4
......@@ -116,8 +116,8 @@ public class CMSController {
* @return the page
*/
@RequestMapping(value = "/all-news", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public Page<ActivityPost> getAllNews(@RequestParam(name = "p", required = false, defaultValue = "1") final int page) {
return contentService.allNews(page);
public Page<ActivityPost> getAllNews(final Pagination page) {
return contentService.allNews(page.toPageRequest(50, Sort.Direction.DESC, "postDate"));
}
/**
......
/**
* Copyright 2015 Global Crop Diversity Trust
/*
* Copyright 2019 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.
......@@ -12,7 +12,7 @@
* 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.mvc;
......@@ -27,6 +27,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
......@@ -51,8 +53,7 @@ public class NewsController extends BaseController {
@GetMapping(value = "/content/news")
public String getAllNews(ModelMap model, @RequestParam(value = "page", defaultValue = "1") int page) {
Page<ActivityPost> allNews = contentService.allNews(page);
Page<ActivityPost> allNews = contentService.allNews(new PageRequest(page - 1, 50, Sort.Direction.DESC, "postDate"));
model.addAttribute("pagedData", allNews);
......
......@@ -20,27 +20,43 @@ import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.google.common.collect.Sets;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.catalog.model.filters.DescriptorListFilter;
import org.genesys.catalog.model.filters.PartnerFilter;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.catalog.service.DatasetService;
import org.genesys.catalog.service.DescriptorListService;
import org.genesys.catalog.service.PartnerService;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.ActivityPost;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.service.ContentService;
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.filter.AccessionFilter;
import org.genesys2.server.service.filter.SubsetFilter;
import org.genesys2.server.service.worker.AccessionProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -73,6 +89,24 @@ public class SitemapXMLController {
@Autowired
private AccessionProcessor accessionProcessor;
@Autowired
private ContentService contentService;
@Autowired
private PartnerService partnerService;
@Autowired
private SubsetService subsetService;
@Autowired
private DescriptorListService descriptorListService;
@Autowired
private DatasetService datasetService;
@Autowired
private JspHelper jspHelper;
private static class SitemapPage {
String url;
String freq = "always";
......@@ -95,16 +129,29 @@ public class SitemapXMLController {
}
private final static SitemapPage[] sitemaps = new SitemapPage[] { new SitemapPage("/sitemap-content.xml"), new SitemapPage("/sitemap-geo.xml"),
new SitemapPage("/sitemap-wiews.xml"), new SitemapPage("/sitemap-crop.xml") };
private final SitemapPage[] sitemapPages = new SitemapPage[] { new SitemapPage("/welcome", 1.0), new SitemapPage("/content/about", "monthly", 0.2),
new SitemapPage("/explore/overview", 1.0), new SitemapPage("/wiews/active", 1.0), new SitemapPage("/content/terms", 1.0),
new SitemapPage("/explore", 1.0), new SitemapPage("/org/") };
private final static SitemapPage[] sitemaps = new SitemapPage[] {
new SitemapPage("/sitemap-content.xml"),
new SitemapPage("/sitemap-iso3166.xml"),
new SitemapPage("/sitemap-wiews.xml"),
new SitemapPage("/sitemap-crop.xml"),
new SitemapPage("/sitemap-partner.xml"),
new SitemapPage("/sitemap-subset.xml"),
new SitemapPage("/sitemap-dataset.xml"),
new SitemapPage("/sitemap-descriptorlist.xml")
};
private final SitemapPage[] sitemapContentPages = new SitemapPage[] {
new SitemapPage("/", 1.0),
new SitemapPage("/content/about/about", "monthly", 0.2),
new SitemapPage("/content/legal/terms", 1.0),
new SitemapPage("/content/legal/disclaimer", 1.0),
new SitemapPage("/documentation/apis", 1.0),
new SitemapPage("/documentation/brapi", 1.0)
};
@RequestMapping(value = "/sitemap.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public @ResponseBody
String sitemapsXml(HttpServletResponse response) {
@ResponseBody
public String sitemapsXml(HttpServletResponse response) {
response.setContentType("text/xml");
final StringBuffer sb = new StringBuffer();
......@@ -121,29 +168,110 @@ public class SitemapXMLController {
}
@RequestMapping(value = "/sitemap-content.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public @ResponseBody
String sitemapContentXml(HttpServletResponse response) {
return writeSitemap(response, sitemapPages);
@ResponseBody
public String sitemapContentXml(HttpServletResponse response) {
final List<SitemapPage> list = new ArrayList<>(Arrays.asList(sitemapContentPages));
List<ActivityPost> activityPosts = contentService.allNews(new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "postDate")).getContent();
for (ActivityPost ap : activityPosts) {
String title = jspHelper.suggestUrlForText(ap.getTitle());
SitemapPage sm = new SitemapPage("/content/news/" + ap.getId() + "/" + title);
list.add(sm);
}
return writeSitemap(response, list.toArray(new SitemapPage[list.size()]));
}
@RequestMapping(value = "/sitemap-geo.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public @ResponseBody
String sitemapGeoXml(HttpServletResponse response) {
@RequestMapping(value = "/sitemap-iso3166.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
@ResponseBody
public String sitemapGeoXml(HttpServletResponse response) {
response.setContentType("text/xml");
final StringBuffer sb = new StringBuffer();
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\">");
List<Country> activeCountries = geoService.listActive(LocaleContextHolder.getLocale());
for (Country country : activeCountries) {
writePage(response, sb, "/iso3166/" + country.getCode3().toUpperCase(), null, null);
List<VocabularyTerm> vt = geoService.list3166Alpha3Terms(new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.ASC, "code3")).getContent();
for (VocabularyTerm term : vt) {
writePage(response, sb, "/iso3166/" + term.getCode().toUpperCase(), null, null);
}
sb.append("</urlset>");
return sb.toString();
}
@RequestMapping(value = "/sitemap-partner.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public void sitemapPartnerXml(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\">");
List<Partner> partners = partnerService.listPartners(new PartnerFilter(), new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.ASC, "id")).getContent();
for (Partner partner : partners) {
writePage(response, sb, "/partners/" + partner.getUuid(), null, null);
}
sb.append("</urlset>");
sb.flush();
response.flushBuffer();
}
@RequestMapping(value = "/sitemap-subset.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public void sitemapSubsetXml(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\">");
List<Subset> subsets = subsetService.list(new SubsetFilter(), new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.ASC, "id")).getContent();
for (Subset subset : subsets) {
writePage(response, sb, "/subsets/" + subset.getUuid(), null, null);
}
sb.append("</urlset>");
sb.flush();
response.flushBuffer();
}
@RequestMapping(value = "/sitemap-dataset.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public void sitemapDatasetXml(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\">");
List<Dataset> datasets = datasetService.listDatasets(new DatasetFilter(), new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.ASC, "id")).getContent();
for (Dataset dataset : datasets) {
writePage(response, sb, "/datasets/" + dataset.getUuid(), null, null);
}
sb.append("</urlset>");
sb.flush();
response.flushBuffer();
}
@RequestMapping(value = "/sitemap-descriptorlist.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public void sitemapDescriptorListXml(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\">");
List<DescriptorList> descriptorLists = descriptorListService.listDescriptorLists(new DescriptorListFilter(), new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.ASC, "id")).getContent();
for (DescriptorList descriptorList : descriptorLists) {
writePage(response, sb, "/descriptorlists/" + descriptorList.getUuid(), null, null);
}
sb.append("</urlset>");
sb.flush();
response.flushBuffer();
}
@RequestMapping(value = "/sitemap-wiews.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public void sitemapWiewsXml(HttpServletResponse response) throws IOException {
response.setContentType("text/xml");
......@@ -212,7 +340,7 @@ public class SitemapXMLController {
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\">");
for (Crop crop : cropService.listCrops()) {
writePage(response, sb, "/c/" + crop.getShortName() + "/data", null, null);
writePage(response, sb, "/c/" + crop.getShortName(), null, null);
}
sb.append("</urlset>");
......
......@@ -72,7 +72,7 @@ public interface ContentService {
List<ActivityPost> lastNews();
Page<ActivityPost> allNews(int page);
Page<ActivityPost> allNews(Pageable page);
ClassPK ensureClassPK(Class<?> clazz);
......
......@@ -102,9 +102,8 @@ public class ContentServiceImpl implements ContentService {
@Override
@Cacheable(value = "contentcache")
public Page<ActivityPost> allNews(int page) {
final PageRequest pageRequest = new PageRequest(page - 1, 50, Direction.DESC, "postDate");
return postRepository.findAll(pageRequest);
public Page<ActivityPost> allNews(Pageable page) {
return postRepository.findAll(page);
}
@Override
......
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