Counter.java
资源名称:cindyblog.rar [点击查看]
上传用户:cdpainuo
上传日期:2022-07-12
资源大小:5257k
文件大小:4k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.appspot.cindyblog.util;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- import javax.persistence.EntityManager;
- import com.appspot.cindyblog.common.EMF;
- import com.appspot.cindyblog.pojo.ArticleBean;
- import com.appspot.cindyblog.pojo.DiscussBean;
- import com.appspot.cindyblog.pojo.MessageBean;
- import com.appspot.cindyblog.service.BlogService;
- public class Counter {
- public static int articleCount = -1;
- public static int disscussCount = -1;
- public static int messageCount = -1;
- private static Counter counter = null;
- private static Map<String,Integer> articleMap = null;
- private static Map<Integer,Integer> disscussMap = new HashMap<Integer,Integer>();
- private Counter(){
- EntityManager em = null;
- BlogService bs = null;
- try{
- if(articleCount == -1){
- em = EMF.get().createEntityManager();
- bs = (BlogService) Singleton.getInstance(BlogService.class);
- articleCount = bs.getObjectCount(ArticleBean.class, em);
- disscussCount = bs.getObjectCount(DiscussBean.class, em);
- messageCount = bs.getObjectCount(MessageBean.class, em);
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- bs = null;
- if(em.isOpen()){
- em.close();
- }
- }
- }
- public static synchronized int getArticleCount(String value) {
- int count = 0;
- if(articleMap != null && articleMap.size() != 0){
- count = articleMap.get(value);
- }
- return count;
- }
- public static synchronized int getDisscussCount() {
- EntityManager em = null;
- BlogService bs = null;
- try{
- if(disscussCount == -1){
- em = EMF.get().createEntityManager();
- bs = (BlogService) Singleton.getInstance(BlogService.class);
- disscussCount = bs.getObjectCount(DiscussBean.class, em);
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- if(em != null && em.isOpen()){
- em.close();
- }
- em = null;
- bs = null;
- return disscussCount;
- }
- }
- public static synchronized int getMessageCount() {
- EntityManager em = null;
- BlogService bs = null;
- try{
- if(messageCount == -1){
- em = EMF.get().createEntityManager();
- bs = (BlogService) Singleton.getInstance(BlogService.class);
- messageCount = bs.getObjectCount(MessageBean.class, em);
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- if(em != null && em.isOpen()){
- em.close();
- }
- em = null;
- bs = null;
- return messageCount;
- }
- }
- public static synchronized Counter getCounter() {
- if(counter == null){
- counter = new Counter();
- }
- return counter;
- }
- public static synchronized void setArticleCount(Map<String,Integer> map){
- if(articleMap == null){
- articleMap = map;
- }
- }
- public static synchronized void addSingleArticleCount(String value){
- if(value != null && !"".equals(value)){
- int allcount = articleMap.get("All");
- if(articleMap.get(value) == null){
- articleMap.put(value,1);
- articleMap.put("All", ++allcount);
- }else{
- int count = articleMap.get(value);
- articleMap.put(value, ++count);
- articleMap.put("All", ++allcount);
- }
- }
- }
- public static synchronized void addSingleDisscussCount(int value){
- EntityManager em = null;
- BlogService bs = null;
- try{
- int count = 0;
- if(disscussMap.get(value) == null){
- em = EMF.get().createEntityManager();
- bs = (BlogService) Singleton.getInstance(BlogService.class);
- count = bs.getDisscussCount(new Long(value), em);
- disscussMap.put(value, ++count);
- }else{
- count = disscussMap.get(value);
- disscussMap.put(value, ++count);
- disscussCount++;
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- if(em != null && em.isOpen()){
- em.close();
- }
- em = null;
- bs = null;
- }
- }
- public static synchronized int getSingleDisscussCount(int value){
- int count = 0;
- EntityManager em = null;
- BlogService bs = null;
- try{
- if(disscussMap.get(value) == null){
- em = EMF.get().createEntityManager();
- bs = (BlogService) Singleton.getInstance(BlogService.class);
- count = bs.getDisscussCount(new Long(value), em);
- disscussMap.put(value, count);
- }else{
- count = disscussMap.get(value);
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- if(em != null && em.isOpen()){
- em.close();
- }
- em = null;
- bs = null;
- return count;
- }
- }
- }