Package com.tubeonfire.model.admin

Source Code of com.tubeonfire.model.admin.FeedbackModel

package com.tubeonfire.model.admin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;
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.tubeonfire.entity.Feedback;

public class FeedbackModel {
  private static final Logger log = Logger.getLogger(FeedbackModel.class
      .getName());

  private static Objectify ofy;

  private static Cache cache = null;

  private static boolean isRegisted = false;

  private static ObjectifyOpts opts = null;

  private int limit = 10;

  private int page = 1;

  private int totalResult = 0;

  private int totalPage = 1;

  private static String cacheSide = "backEnd_";

  private static String cachePrefix = "feedbackModel_";

  private static TreeMap<String, String> mapCacheKey = new TreeMap<String, String>();

  private List<Feedback> listResult = new ArrayList<Feedback>();

  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<Feedback> getListResult() {
    return listResult;
  }

  public void setListResult(List<Feedback> listResult) {
    this.listResult = listResult;
  }

  public static void init() {
    if (!isRegisted) {
      isRegisted = true;
      try {
        ObjectifyService.register(Feedback.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 FeedbackModel() {
    init();
  }

  @SuppressWarnings("unchecked")
  public static void insert(Feedback obj) {
    init();
    if (cache != null) {
      String prefix = cachePrefix + "id_" + obj.getId();
      cache.put(prefix, obj);
      clearModelCache();
    }
    ofy.put(obj);
  }

  public static void delete(Feedback obj) {
    init();
    String prefix = cachePrefix + "id_" + obj.getId();
    if (cache != null && cache.containsKey(prefix)) {
      cache.remove(prefix);
      clearModelCache();
    }
    ofy.delete(obj);
  }

  @SuppressWarnings("unchecked")
  public static Feedback getById(String id) {
    try {
      init();
      Feedback obj = new Feedback();
      String prefix = cachePrefix + "id_" + id;
      if (cache != null && cache.containsKey(prefix)) {
        obj = (Feedback) cache.get(prefix);
      } else {
        try {
          obj = ofy.get(new Key<Feedback>(Feedback.class, id));
          cache.put(prefix, obj);
        } catch (Exception e) {
          obj = null;
        }
      }
      return obj;
    } catch (Exception e) {
      log.warning(e.toString());
      e.printStackTrace();
      return null;
    }
  }

  @SuppressWarnings("unchecked")
  public void prepareList(int status) {
    init();
    listResult = new ArrayList<Feedback>();
    String prefix = cachePrefix + "page_" + page + "_limit_" + limit
        + "_status_" + status;
    mapCacheKey.put(prefix, prefix);
    if (cache != null && cache.containsKey(prefix)) {
      listResult = (List<Feedback>) cache.get(prefix);
      totalResult = (Integer) cache.get(cacheSide
          + "allFeedbackTotalResult" + status);
      limit = (Integer) cache
          .get(cacheSide + "allFeedbackLimit" + status);
    } else {
      int start = (page - 1) * limit;
      Query<Feedback> q = null;
      if (status == 1) {
        q = ofy.query(Feedback.class).filter("status", 1);
      } else {
        q = ofy.query(Feedback.class);
      }
      totalResult = q.count();
      q = q.limit(limit).offset(start);
      for (Feedback obj : q) {
        listResult.add(obj);
      }
      if (listResult.size() > 0) {
        cache.put(prefix, listResult);
        cache.put(cacheSide + "allFeedbackTotalResult" + status,
            totalResult);
        cache.put(cacheSide + "allFeedbackLimit" + status, limit);
      }
    }
  }

  public static int countNew() {
    init();
    int result = 0;
    String prefix = cachePrefix + "count_new_feedback";
    mapCacheKey.put(prefix, prefix);
    if (cache != null && cache.containsKey(prefix)) {
      result = (Integer) cache.get(prefix);
    } else {
      Query<Feedback> q = ofy.query(Feedback.class).filter("status", 1);
      result = q.count();
    }
    return result;
  }

  public static void clearModelCache() {
    init();
    try {
      if (mapCacheKey != null && mapCacheKey.size() > 0) {
        for (String key : mapCacheKey.keySet()) {
          cache.remove(key);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of com.tubeonfire.model.admin.FeedbackModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.