Package com.multysite.model

Source Code of com.multysite.model.TagModel

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

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

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

  private int limit = 30;

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

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

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

  @SuppressWarnings("unchecked")
  public static void insert(Tag obj) {
    init();
    if (cache != null) {
      cache.clear();
      String prefix = cachePrefix + "alias_" + obj.getAlias();
      cache.put(prefix, obj);
    }
    ofy.put(obj);
  }

  public static void delete(Tag obj) {
    init();
    String prefix = cachePrefix + "alias_" + obj.getAlias();
    if (cache != null && cache.containsKey(prefix)) {
      cache.clear();
      cache.remove(prefix);
    }
    ofy.delete(obj);
  }

  @SuppressWarnings("unchecked")
  public static Tag getById(String alias) {
    try {
      init();
      boolean cached = false;
      Tag obj = new Tag();
      String prefix = cachePrefix + "alias_" + alias;
      try {
        obj = (Tag) cache.get(prefix);
        if (obj != null) {
          cached = true;
        }
      } catch (Exception e) {
        cached = false;
      }
      if (!cached) {
        try {
          obj = ofy.get(new Key<Tag>(Tag.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<Tag>();
      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<Tag>) cache.get(prefix);
        totalResult = (Integer) cache.get(prefixTotal);
        limit = (Integer) cache.get(prefixLimit);
      } else {
        int start = (page - 1) * limit;
        Query<Tag> q = ofy.query(Tag.class);
        totalResult = q.count();
        q = q.limit(limit).offset(start);
        for (Tag 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<Tag>();
    }
  }
}
TOP

Related Classes of com.multysite.model.TagModel

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.