Package com.multysite.model.admin

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

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.multysite.entity.admin.ApplicationConfig;

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

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

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

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

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

  @SuppressWarnings("unchecked")
  public static void insert(String ns, ApplicationConfig obj) {
    String currentNs = NamespaceManager.get();
    if (!currentNs.equals(ns)) {
      NamespaceManager.set(ns);
    }
    init();
    if (cache != null) {
      String prefix = cachePrefix + "id_" + obj.getApplicationId();
      cache.put(prefix, obj);
    }
    ofy.put(obj);
    NamespaceManager.set(currentNs);
  }

  public static void delete(String ns, ApplicationConfig obj) {
    String currentNs = NamespaceManager.get();
    if (!currentNs.equals(ns)) {
      NamespaceManager.set(ns);
    }
    init();
    String prefix = cachePrefix + "id_" + obj.getApplicationId();
    if (cache != null && cache.containsKey(prefix)) {
      cache.remove(prefix);
    }
    ofy.delete(obj);
    NamespaceManager.set(currentNs);
  }

  @SuppressWarnings("unchecked")
  public static ApplicationConfig getById(String ns, String id) {
    String currentNs = NamespaceManager.get();
    if (!currentNs.equals(ns)) {
      NamespaceManager.set(ns);
    }
    try {
      init();
      boolean cached = false;
      ApplicationConfig obj = new ApplicationConfig();
      String prefix = cachePrefix + "id_" + id;
      try {
        obj = (ApplicationConfig) cache.get(prefix);
        if (obj != null) {
          cached = true;
        }
      } catch (Exception e) {
        cached = false;
      }
      if (!cached) {
        try {
          obj = ofy.get(new Key<ApplicationConfig>(
              ApplicationConfig.class, id));
          cache.put(prefix, obj);
        } catch (Exception e) {
          e.printStackTrace();
          obj = null;
        }
      }
      NamespaceManager.set(currentNs);
      return obj;
    } catch (Exception e) {
      log.warning(e.toString());
      e.printStackTrace();
      NamespaceManager.set(currentNs);
      return null;
    }
  }
}
TOP

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

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.