Package com.multysite.model.admin

Source Code of com.multysite.model.admin.ApplicationModel

package com.multysite.model.admin;

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.google.appengine.api.NamespaceManager;
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.admin.Application;
import com.multysite.util.Setting;

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

  private static Objectify ofy;

  private static Cache cache = null;

  private static ObjectifyOpts opts = null;

  private static String cachePrefix = "siteModel_";

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

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

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

  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;
  }

  static {
    try {
      ObjectifyService.register(Application.class);
    } catch (Exception e) {
    }
    try {
      cache = CacheManager.getInstance().getCacheFactory()
          .createCache(Collections.emptyMap());
    } catch (CacheException e) {     
    }
    opts = new ObjectifyOpts().setSessionCache(true);
    ofy = ObjectifyService.begin(opts);
  }

  public ApplicationModel() {
   
  }

  @SuppressWarnings("unchecked")
  public static void insert(Application obj) {
    NamespaceManager.set(Setting.getGeneralNamespace());
 
    cache.clear();
    if (cache != null) {
      String prefix = cachePrefix + "id_" + obj.getId();
      cache.put(prefix, obj);
    }
    ofy.put(obj);
  }

  public static void delete(Application obj) {
    NamespaceManager.set(Setting.getGeneralNamespace());
   
    cache.clear();
    String prefix = cachePrefix + "id_" + obj.getId();
    if (cache != null && cache.containsKey(prefix)) {
      cache.remove(prefix);
    }
    ofy.delete(obj);
  }

  @SuppressWarnings("unchecked")
  public static Application getById(String id) {
    NamespaceManager.set(Setting.getGeneralNamespace());
    try {     
      boolean cached = false;
      Application obj = new Application();
      String prefix = cachePrefix + "id_" + id;
      try {
        obj = (Application) cache.get(prefix);
        if (obj != null) {
          cached = true;
        }
      } catch (Exception e) {
        cached = false;
      }
      if (!cached) {
        try {
          obj = ofy.get(new Key<Application>(Application.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 prepareApplicationByUser(String userEmail) {
    NamespaceManager.set(Setting.getGeneralNamespace());
    try {
      listResult = new ArrayList<Application>();
      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<Application>) cache.get(prefix);
        totalResult = (Integer) cache.get(prefixTotal);
        limit = (Integer) cache.get(prefixLimit);
      } else {
        int start = (page - 1) * limit;
        Query<Application> q = ofy.query(Application.class).filter(
            "userEmail", userEmail);
        totalResult = q.count();
        q = q.limit(limit).offset(start);
        for (Application obj : q) {
          listResult.add(obj);
        }
        if (listResult.size() > 0) {
          cache.put(prefix, listResult);
          cache.put("tubesTotalResult", totalResult);
          cache.put("tubesLimit", limit);
        }
      }
    } catch (Exception e) {
      log.warning(e.toString());
      e.printStackTrace();
      listResult = new ArrayList<Application>();
    }
  }
}
TOP

Related Classes of com.multysite.model.admin.ApplicationModel

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.