Package com.tubeonfire.model

Source Code of com.tubeonfire.model.TagModel

package com.tubeonfire.model;

import java.util.List;
import com.tubeonfire.entity.Tag;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

public class TagModel {

  PersistenceManager pm = PMF.get().getPersistenceManager();

  public boolean add(Tag tag) {
    try {
      pm.makePersistent(tag);

    } catch (Exception e) {
      System.out.println("Error when add tag object.");
      e.printStackTrace();
      return false;
    }
    return true;
  }

  public boolean addAll(List<Tag> listTag) {
    try {
      pm.makePersistentAll(listTag);
      System.out.println("Add all tube success.");
    } catch (Exception e) {
      System.out.println("Error when add many tag object.");
      e.printStackTrace();
      return false;
    }
    return true;
  }

  public boolean update(Tag tag) {
    pm.makePersistent(tag);
    return true;
  }

  @SuppressWarnings("unchecked")
  public Tag getByAlias(String alias) {
    Tag tag = null;
    Query query = pm.newQuery(Tag.class);
    query.setFilter("alias==tagAlias");
    query.declareParameters("java.lang.String tagAlias");
    query.setRange(0, 1);
    List<Tag> listResult = (List<Tag>) query.execute(alias);
    if (listResult.size() > 0) {
      tag = listResult.get(0);
    }
    return tag;
  }

  @SuppressWarnings("unchecked")
  public List<Tag> getRandom() {
    int min = 1;
    int max = 80;
    int randomMin = min + (int) (Math.random() * (max - min));
    int randomMax = randomMin + 10;
    if ((max - randomMin) < 10) {
      randomMax = randomMin;
      randomMin = randomMax - 10;
    }
    System.out.println("Min : " + randomMin);
    System.out.println("Max : " + randomMax);
    Query query = pm.newQuery(Tag.class);
    query.setRange(randomMin, randomMax);
    List<Tag> listResult = (List<Tag>) query.execute();
    if (listResult.size() > 0) {
      return listResult;
    } else {
      return null;
    }
  }

  public void closePM() {
    pm.close();   
  }

}
TOP

Related Classes of com.tubeonfire.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.