Package com.tubeonfire.model.admin

Source Code of com.tubeonfire.model.admin.MissionModel

package com.tubeonfire.model.admin;

import java.util.ArrayList;
import java.util.List;

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

public class MissionModel {
  private static Objectify ofy;

  private static boolean isRegisted = false;

  private static ObjectifyOpts opts = null;

  private int limit = 12;

  private int page = 1;

  private int totalResult = 0;

  private int totalPage = 1;

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

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

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

  public static void init() {
    if (!isRegisted) {
      isRegisted = true;
      try {
        ObjectifyService.register(Mission.class);
      } catch (Exception e) {
        isRegisted = false;
      }     
      opts = new ObjectifyOpts().setSessionCache(true);
    }
    ofy = ObjectifyService.begin(opts);
  }

  public MissionModel() {
    init();
  }
 
  public static void insert(Mission obj) {
    init();   
    ofy.put(obj);
  }

  public static void delete(Mission obj) {
    init();   
    ofy.delete(obj);
  }

  public void prepareList() {
    init();
    listResult = new ArrayList<Mission>();
    int start = (page - 1) * limit;
    Query<Mission> q = ofy.query(Mission.class);
    totalResult = q.count();
    q = q.limit(limit).offset(start);
    for (Mission obj : q) {
      listResult.add(obj);
    }
  }

  public void search(int status, int type) {
    init();
    listResult = new ArrayList<Mission>();
    int start = (page - 1) * limit;
    Query<Mission> q = ofy.query(Mission.class);
    if (status != 0) {
      q.filter("status", status);
    }
    if (type != 0) {
      q.filter("type", type);
    }
    totalResult = q.count();
    q = q.limit(limit).offset(start).order("updated");
    for (Mission obj : q) {
      listResult.add(obj);
    }
  }

  public static Mission getById(String alias) {
    init();
    Mission obj = new Mission();
    try {
      obj = ofy.get(new Key<Mission>(Mission.class, alias));
    } catch (Exception e) {
      obj = null;
    }
    return obj;
  }

  public void getByStatus(int status) {
    listResult = new ArrayList<Mission>();
    int start = (page - 1) * limit;
    Query<Mission> q = ofy.query(Mission.class).filter("type", status);
    totalResult = q.count();
    q = q.limit(limit).offset(start);
    for (Mission obj : q) {
      listResult.add(obj);
    }
  }

  public static Mission getNotComplete() {
    init();
    Query<Mission> q = ofy.query(Mission.class).filter("status", 1)
        .order("updated");
    return q.get();
  }

  public static Mission getNotCompleteByType(int type) {
    init();
    Query<Mission> q = ofy.query(Mission.class).filter("status", 1)
        .filter("type", type).order("updated");
    return q.get();
  }

  public void getListNotCompleteByType(int type) {
    init();
    Query<Mission> q = ofy.query(Mission.class).filter("status", 1)
        .filter("type", type).order("updated");
    ;
    totalResult = q.count();
    for (Mission obj : q) {
      listResult.add(obj);
    }
  }
}
TOP

Related Classes of com.tubeonfire.model.admin.MissionModel

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.