Package com.tubeonfire.model

Source Code of com.tubeonfire.model.PageModel

package com.tubeonfire.model;

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.Page;

public class PageModel {
  private static final Logger log = Logger.getLogger(PageModel.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 = "frontEndPageModel_";

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

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

  public List<Page> getListResult() {
    return listResult;
  }

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

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

  @SuppressWarnings("unchecked")
  public static Page getById(String id) {
    try {
      init();
      Page obj = new Page();
      String prefix = cachePrefix + "id_" + id;
      if (cache != null && cache.containsKey(prefix)) {
        obj = (Page) cache.get(prefix);
      } else {
        try {
          obj = ofy.get(new Key<Page>(Page.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() {
    try {
      String prefix = cachePrefix + "listpage";
      if (cache != null && cache.containsKey(prefix)) {
        listResult = (ArrayList<Page>) cache.get(prefix);
      } else {
        mapCacheKey.put(prefix, prefix);
        listResult = new ArrayList<Page>();
        Query<Page> q = ofy.query(Page.class).filter("status", 1)
            .order("-updated");
        for (Page obj : q) {
          listResult.add(obj);
        }
        cache.put(prefix, listResult);
      }
    } catch (Exception e) {
      log.warning(e.toString());
      e.printStackTrace();
      listResult = new ArrayList<Page>();
    }
  }

  public static void initCache() {
    if (!isRegisted) {
      isRegisted = true;
      try {
        cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());
      } catch (CacheException e) {
        log.warning(e.toString());
        e.printStackTrace();
        isRegisted = false;
      }
    }
  }

  public static void clearModelCache() {
    initCache();
    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.PageModel

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.