Package com.monkygames.wox.health.io

Source Code of com.monkygames.wox.health.io.HealthLoader

/*
* See COPYING in top-level directory.
*/
package com.monkygames.wox.health.io;

// === java imports === //
import java.io.File;
// === db4o imports === //
import com.db4o.*;
import com.db4o.query.*;
// === wo imports === //
import com.monkygames.wo.avatar.Info;
// === wox imports === //
import com.monkygames.wox.health.data.*;

/**
* Handles reading and writing health data to the database.
* Note, this is a local database.
* @version 1.0
*/
public class HealthLoader{

    /**
     * The database that contains the health information.
     **/
    private ObjectContainer db;

// ============= Constructors ============== //
    public HealthLoader(String dbFile){
  initDatabase(dbFile);
    }
// ============= Public Methods ============== //
    /**
     * Returns the health data specified by the avatar's info.
     * @param info the avatar's info which is associated with health.
     * @return the health data and null if it doesn't exist in the database.
     **/
    public HealthData getHealthData(final Info info){
  ObjectSet <HealthData> result = db.query(new Predicate <HealthData> (){
      public boolean match(HealthData data){
    if(data.info.equals(info)){
        return true;
    }
    return false;
      }
  });
  if(result.hasNext()){
      return result.next();
  }
  return null;
    }
    /**
     * Creates an health data entry in the database.
     * @param healthData the data to put in the database.
     * @return true on success and false if data already exists for the info associated with the HealthData.
     **/
    public boolean createHealthData(final HealthData healthData){
  HealthData next = getHealthData(healthData.info);
  if(next != null){
      return false;
  }
  db.set(healthData);
  db.commit();
  return true;
    }
    /**
     * Updates the health data in the database.
     * If the health data doesn't exist, then its created and added to the db.
     **/
    public boolean updateHealthData(final HealthData healthData){
  HealthData oldHealthData = getHealthData(healthData.info);
  if(oldHealthData == null){
      return createHealthData(healthData);
  }
  // update health data
  oldHealthData.updateData(healthData);
  db.set(oldHealthData);
  db.commit();
  return true;
    }

    public void shutdown(){
  db.close();
    }
// ============= Protected Methods ============== //
// ============= Private Methods ============== //
    private void initDatabase(String dbFile){
  try{
      Db4o.configure().objectClass(com.monkygames.wox.health.data.HealthData.class).cascadeOnUpdate(true);
      File file = new File(dbFile);
      db = Db4o.openFile(file.getAbsolutePath());
  }catch(Exception e){e.printStackTrace();}
    }
// ============= Implemented Methods ============== //
// ============= Extended Methods ============== //
// ============= Internal Classes ============== //
// ============= Static Methods ============== //

    public static void main(String []args){
  HealthLoader hl = new HealthLoader("test.db");
  Info info = new Info();
  info.name = "TEST";
  HealthData hd = new HealthData();
  hd.info = info;
  System.out.println("Creating health data = "+hl.createHealthData(hd));
  System.out.println("Updating health data = "+hl.updateHealthData(hd));
  hl.shutdown();
    }

}
/*
* Local variables:
*  c-indent-level: 4
*  c-basic-offset: 4
* End:
*
* vim: ts=8 sts=4 sw=4 noexpandtab
*/ 
TOP

Related Classes of com.monkygames.wox.health.io.HealthLoader

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.