Package com.tubeonfire.model

Source Code of com.tubeonfire.model.ChannelModel

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

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

  private static Objectify ofy;

  private static Cache cache = null;

  private static boolean isRegisted = false;

  private static ObjectifyOpts opts = null;

  private int limit = 12;

  private static String cacheSide = "frontEnd_";

  private static String cachePrefix = "channelModel_";

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

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

  public int getLimit() {
    return limit;
  }

  public void setLimit(int limit) {
    this.limit = limit;
  }

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

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

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

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

  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.ChannelModel

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.