package com.multysite.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.multysite.entity.News;
import com.multysite.search.NewsSearchEngine;
public class NewsModel {
private static final Logger log = Logger.getLogger(NewsModel.class
.getName());
private static Objectify ofy;
private static Cache cache = null;
private static boolean isRegisted = false;
private static ObjectifyOpts opts = null;
private static String cachePrefix = "newsModel_";
private List<News> listResult = new ArrayList<News>();
private int limit = 10;
private int page = 1;
private int totalResult = 0;
private int totalPage = 1;
public int getTotalPage() {
totalPage = totalResult / limit;
if ((totalResult % limit) > 0) {
totalPage += 1;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotalResult() {
return totalResult;
}
public void setTotalResult(int totalResult) {
this.totalResult = totalResult;
}
public List<News> getListResult() {
return listResult;
}
public void setListResult(List<News> listResult) {
this.listResult = listResult;
}
public static void init() {
if (!isRegisted) {
isRegisted = true;
try {
ObjectifyService.register(News.class);
} catch (Exception e) {
isRegisted = false;
}
try {
cache = CacheManager.getInstance().getCacheFactory()
.createCache(Collections.emptyMap());
} catch (CacheException e) {
isRegisted = false;
}
opts = new ObjectifyOpts().setSessionCache(true);
}
ofy = ObjectifyService.begin(opts);
}
public NewsModel() {
init();
}
@SuppressWarnings("unchecked")
public static void insert(News obj) {
init();
if (cache != null) {
cache.clear();
String prefix = cachePrefix + "alias_" + obj.getAlias();
cache.put(prefix, obj);
}
ofy.put(obj);
NewsSearchEngine.remove(obj.getAlias());
NewsSearchEngine.index(obj);
}
public static void delete(News obj) {
init();
String prefix = cachePrefix + "alias_" + obj.getAlias();
if (cache != null && cache.containsKey(prefix)) {
cache.clear();
cache.remove(prefix);
}
ofy.delete(obj);
NewsSearchEngine.remove(obj.getAlias());
}
@SuppressWarnings("unchecked")
public static News getById(String alias) {
try {
init();
boolean cached = false;
News obj = new News();
String prefix = cachePrefix + "alias_" + alias;
try {
obj = (News) cache.get(prefix);
if (obj != null) {
cached = true;
}
} catch (Exception e) {
cached = false;
}
if (!cached) {
try {
obj = ofy.get(new Key<News>(News.class, alias));
cache.put(prefix, obj);
} catch (Exception e) {
e.printStackTrace();
obj = null;
}
}
return obj;
} catch (Exception e) {
log.warning(e.toString());
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
public void prepareList() {
try {
listResult = new ArrayList<News>();
String prefix = cachePrefix + "list_" + page;
String prefixTotal = cachePrefix + "list_total";
String prefixLimit = cachePrefix + "list_limit";
if (cache != null && cache.containsKey(prefix)
&& cache.containsKey(prefixTotal)
&& cache.containsKey(prefixLimit)) {
listResult = (ArrayList<News>) cache.get(prefix);
totalResult = (Integer) cache.get(prefixTotal);
limit = (Integer) cache.get(prefixLimit);
} else {
int start = (page - 1) * limit;
Query<News> q = ofy.query(News.class).order("-date");
totalResult = q.count();
q = q.limit(limit).offset(start);
for (News obj : q) {
listResult.add(obj);
}
if (listResult.size() > 0) {
cache.put(prefix, listResult);
cache.put(prefixTotal, totalResult);
cache.put(prefixLimit, limit);
}
}
} catch (Exception e) {
log.warning(e.toString());
e.printStackTrace();
listResult = new ArrayList<News>();
}
}
@SuppressWarnings("unchecked")
public void prepareListByCategory(String categoryAlias) {
try {
listResult = new ArrayList<News>();
String prefix = cachePrefix + "list_" + categoryAlias + page;
String prefixTotal = cachePrefix + "list_total" + categoryAlias;
String prefixLimit = cachePrefix + "list_limit" + categoryAlias;
if (cache != null && cache.containsKey(prefix)
&& cache.containsKey(prefixTotal)
&& cache.containsKey(prefixLimit)) {
listResult = (ArrayList<News>) cache.get(prefix);
totalResult = (Integer) cache.get(prefixTotal);
limit = (Integer) cache.get(prefixLimit);
} else {
int start = (page - 1) * limit;
Query<News> q = ofy.query(News.class).filter("categoryAlias",
categoryAlias);
totalResult = q.count();
q = q.limit(limit).offset(start);
for (News obj : q) {
listResult.add(obj);
}
if (listResult.size() > 0) {
cache.put(prefix, listResult);
cache.put(prefixTotal, totalResult);
cache.put(prefixLimit, limit);
}
}
} catch (Exception e) {
log.warning(e.toString());
e.printStackTrace();
listResult = new ArrayList<News>();
}
}
}