org.spincast.website.repositories.TemplateFilesRepository Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spincast-website Show documentation
Show all versions of spincast-website Show documentation
Source code for the https://www.spincast.org website.
package org.spincast.website.repositories;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.spincast.core.json.JsonArray;
import org.spincast.core.json.JsonManager;
import org.spincast.core.json.JsonObject;
import org.spincast.core.templating.TemplatingEngine;
import org.spincast.core.utils.SpincastStatics;
import org.spincast.core.utils.SpincastUtils;
import org.spincast.website.AppConfig;
import org.spincast.website.models.NewsEntriesAndTotalNbr;
import org.spincast.website.models.NewsEntry;
import org.spincast.website.models.NewsEntryDefault;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
/**
* Implementation of the News repository that takes the
* news entries from template files.
*/
public class TemplateFilesRepository implements NewsRepository {
private final AppConfig appConfig;
private final TemplatingEngine templatingEngine;
private final SpincastUtils spincastUtils;
private final JsonManager jsonManager;
private final Object getNewsEntriesLocalLock = new Object();
private List newsEntries;
private List newsEntriesAsc;
private List newsEntriesDesc;
private Map newsEntriesById;
/**
* Constructor
*/
@Inject
public TemplateFilesRepository(AppConfig appConfig,
TemplatingEngine templatingEngine,
SpincastUtils spincastUtils,
JsonManager jsonManager) {
this.appConfig = appConfig;
this.templatingEngine = templatingEngine;
this.spincastUtils = spincastUtils;
this.jsonManager = jsonManager;
}
/**
* Init
*/
@Inject
public void init() {
//==========================================
// Load news entries
//==========================================
getNewsEntriesLocal();
}
protected AppConfig getAppConfig() {
return this.appConfig;
}
protected TemplatingEngine getTemplatingEngine() {
return this.templatingEngine;
}
protected SpincastUtils getSpincastUtils() {
return this.spincastUtils;
}
protected JsonManager getJsonManager() {
return this.jsonManager;
}
@Override
public int getNewsEntriesTotalNumber() {
return getNewsEntriesLocal().size();
}
@Override
public List getNewsEntries(boolean ascOrder) {
if(ascOrder) {
if(this.newsEntriesAsc == null || getAppConfig().isDevelopmentMode()) {
List entries = getNewsEntriesLocal();
Collections.sort(entries, new Comparator() {
@Override
public int compare(NewsEntry entry1, NewsEntry entry2) {
return entry1.getPublishedDate().compareTo(entry2.getPublishedDate());
}
});
this.newsEntriesAsc = entries;
}
return this.newsEntriesAsc;
} else {
if(this.newsEntriesDesc == null || getAppConfig().isDevelopmentMode()) {
List newsEntriesAsc = getNewsEntries(true);
this.newsEntriesDesc = Lists.reverse(newsEntriesAsc);
}
return this.newsEntriesDesc;
}
}
@Override
public List getNewsEntries(int startPos, int endPos, boolean ascOrder) {
return getNewsEntriesAndTotalNbr(startPos, endPos, ascOrder).getNewsEntries();
}
@Override
public NewsEntriesAndTotalNbr getNewsEntriesAndTotalNbr(int startPos, int endPos, boolean ascOrder) {
if(startPos < 1) {
startPos = 1;
}
if(endPos < 1) {
endPos = 1;
}
if(endPos < startPos) {
endPos = startPos;
}
final List entries = getNewsEntries(ascOrder);
if(entries.size() == 0 || startPos > entries.size()) {
return new NewsEntriesAndTotalNbr() {
@Override
public List getNewsEntries() {
return new ArrayList();
}
@Override
public int getNbrNewsEntriesTotal() {
return entries.size();
}
};
}
if(endPos > entries.size()) {
endPos = entries.size();
}
final int startPosFinal = startPos;
final int endPosFinal = endPos;
return new NewsEntriesAndTotalNbr() {
@Override
public List getNewsEntries() {
return Collections.unmodifiableList(entries.subList(startPosFinal - 1, endPosFinal)); // endPos is exclusive here
}
@Override
public int getNbrNewsEntriesTotal() {
return entries.size();
}
};
}
@Override
public NewsEntry getNewsEntry(long newsId) {
return getNewsEntriesById().get(newsId);
}
protected Map getNewsEntriesById() {
if(this.newsEntriesById == null || getAppConfig().isDevelopmentMode()) {
this.newsEntriesById = new HashMap();
List newsEntries = getNewsEntriesLocal();
for(NewsEntry newsEntry : newsEntries) {
this.newsEntriesById.put(newsEntry.getId(), newsEntry);
}
}
return this.newsEntriesById;
}
protected List getNewsEntriesLocal() {
if(this.newsEntries == null || getAppConfig().isDevelopmentMode()) {
synchronized(this.getNewsEntriesLocalLock) {
if(this.newsEntries == null || getAppConfig().isDevelopmentMode()) {
try {
List newsEntries = new ArrayList();
JsonObject newsJsonObj = getJsonManager().fromClasspathFile("/news/news-index.json");
Map params = new HashMap();
params.put("appUrlPrefix", getAppConfig().getPublicUrlBase());
JsonArray newsArray = newsJsonObj.getJsonArray("news");
for(int i = 0; i < newsArray.size(); i++) {
JsonObject newsObj = newsArray.getJsonObject(i);
String path = newsObj.getString("path");
String newsEntryContent = getSpincastUtils().readClasspathFile(path);
if(newsEntryContent == null) {
throw new RuntimeException("News not found : " + path);
}
//==========================================
// Replace placeholders
//==========================================
newsEntryContent = getTemplatingEngine().evaluate(newsEntryContent, params);
long id = newsObj.getLong("id");
Date date = newsObj.getDate("date");
String title = newsObj.getString("title");
NewsEntry entry = new NewsEntryDefault(id, date, title, newsEntryContent);
newsEntries.add(entry);
}
this.newsEntries = newsEntries;
} catch(Exception ex) {
throw SpincastStatics.runtimize(ex);
}
}
}
}
return this.newsEntries;
}
}