Search in sources :

Example 1 with NextProtNews

use of org.nextprot.api.web.domain.NextProtNews in project nextprot-api by calipho-sib.

the class GitHubServiceUnitTest method shouldGetANextProtNews.

@Test
public void shouldGetANextProtNews() throws Exception {
    NextProtNews n = GitHubServiceImpl.parseGitHubNewsFilePath("2014/08/25/Google OAuth Support.md");
    assertEquals(n.getUrl(), "google-oauth-support");
    assertEquals(n.getTitle(), "Google OAuth Support");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date date = df.parse("2014-08-25");
    assertEquals(n.getPublicationDate(), date);
    assertEquals(n.getPublicationDateFormatted(), "Aug 25, 2014");
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) NextProtNews(org.nextprot.api.web.domain.NextProtNews) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) WebUnitBaseTest(org.nextprot.api.web.dbunit.base.mvc.WebUnitBaseTest) Test(org.junit.Test)

Example 2 with NextProtNews

use of org.nextprot.api.web.domain.NextProtNews in project nextprot-api by calipho-sib.

the class GitHubServiceImpl method getNews.

@Override
@Cacheable(value = "github-news")
public List<NextProtNews> getNews() {
    List<NextProtNews> news = new ArrayList<>();
    try {
        GitHub github = getGitHubConnection();
        GHRepository repo = github.getRepository("calipho-sib/nextprot-docs");
        GHTree tree = repo.getTreeRecursive(githubDocBranch, 1);
        newsFileNames.clear();
        for (GHTreeEntry te : tree.getTree()) {
            if (te.getPath().startsWith("news")) {
                // Add only file on news
                if (te.getType().equalsIgnoreCase("blob")) {
                    // file
                    String fileName = te.getPath().replaceAll("news/", "");
                    NextProtNews n = parseGitHubNewsFilePath(fileName);
                    if (n != null) {
                        news.add(n);
                        String fileEncoded = URLEncoder.encode(fileName.replace(".md", ""), "UTF-8").replace("+", "%20");
                        newsFileNames.put(n.getUrl(), fileEncoded);
                    }
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new NextProtException("News not available, sorry for the inconvenience");
    }
    Collections.sort(news);
    return news;
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) GHRepository(org.kohsuke.github.GHRepository) GitHub(org.kohsuke.github.GitHub) ArrayList(java.util.ArrayList) GHTree(org.kohsuke.github.GHTree) IOException(java.io.IOException) NextProtNews(org.nextprot.api.web.domain.NextProtNews) GHTreeEntry(org.kohsuke.github.GHTree.GHTreeEntry) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 3 with NextProtNews

use of org.nextprot.api.web.domain.NextProtNews in project nextprot-api by calipho-sib.

the class GitHubServiceImpl method parseGitHubNewsFilePath.

static NextProtNews parseGitHubNewsFilePath(String filePath) {
    NextProtNews result = new NextProtNews();
    if (filePath == null || filePath.equals("")) {
        return null;
    }
    String[] elements = filePath.split("\\/");
    if (elements.length != 4) {
        LOGGER.warn("Number of elements is different than 4 != " + elements.length + " " + elements);
        return null;
    }
    // Check for elements not null or empty
    for (String elem : elements) {
        if (elem == null || elem.isEmpty()) {
            LOGGER.warn("Found a null or empty element on " + filePath);
            return null;
        }
    }
    try {
        // Parse the date
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        Date date = df.parse(elements[0] + "/" + elements[1] + "/" + elements[2]);
        result.setPublicationDate(date);
    } catch (ParseException e) {
        LOGGER.warn("Failed to parse the date for file" + filePath + " " + e.getMessage());
        // e.printStackTrace();
        return null;
    }
    // Gets url and title
    String title = elements[3].replace(".md", "").trim();
    result.setTitle(title);
    result.setUrl(normalizeTitleToUrl(title));
    return result;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) NextProtNews(org.nextprot.api.web.domain.NextProtNews) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with NextProtNews

use of org.nextprot.api.web.domain.NextProtNews in project nextprot-api by calipho-sib.

the class SeoTagsServiceImpl method getNewsSeoTags.

@Override
@Cacheable(value = "seo-github-news")
public SeoTags getNewsSeoTags(String url) {
    String[] urlElements = RelativeUrlUtils.getPathElements(url);
    List<NextProtNews> allNews = gitHubService.getNews();
    if (urlElements.length > 1) {
        String pageUrl = urlElements[1];
        for (NextProtNews oneNews : allNews) {
            if (pageUrl.equals(oneNews.getUrl())) {
                return getOneNewsSeoTags(oneNews);
            }
        }
    } else {
        NextProtNews mostRecentNews = allNews.get(allNews.size() - 1);
        return getOneNewsSeoTags(mostRecentNews);
    }
    return null;
}
Also used : NextProtNews(org.nextprot.api.web.domain.NextProtNews) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 5 with NextProtNews

use of org.nextprot.api.web.domain.NextProtNews in project nextprot-api by calipho-sib.

the class GitHubServiceUnitTest method shouldReturnNullIfThereIsLessThan2Slashes.

@Test
public void shouldReturnNullIfThereIsLessThan2Slashes() throws Exception {
    NextProtNews n = GitHubServiceImpl.parseGitHubNewsFilePath("2014/08/Google OAuth Support.md");
    assertTrue(n == null);
}
Also used : NextProtNews(org.nextprot.api.web.domain.NextProtNews) WebUnitBaseTest(org.nextprot.api.web.dbunit.base.mvc.WebUnitBaseTest) Test(org.junit.Test)

Aggregations

NextProtNews (org.nextprot.api.web.domain.NextProtNews)7 Test (org.junit.Test)3 WebUnitBaseTest (org.nextprot.api.web.dbunit.base.mvc.WebUnitBaseTest)3 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Cacheable (org.springframework.cache.annotation.Cacheable)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 GHRepository (org.kohsuke.github.GHRepository)1 GHTree (org.kohsuke.github.GHTree)1 GHTreeEntry (org.kohsuke.github.GHTree.GHTreeEntry)1 GitHub (org.kohsuke.github.GitHub)1 NextProtException (org.nextprot.api.commons.exception.NextProtException)1 SeoTagsAndUrl (org.nextprot.api.web.seo.domain.SeoTagsAndUrl)1 SitemapUrl (org.nextprot.api.web.seo.domain.SitemapUrl)1 SitemapUrlSet (org.nextprot.api.web.seo.domain.SitemapUrlSet)1