Package net.sf.cannagrower.data

Source Code of net.sf.cannagrower.data.Plantation

package net.sf.cannagrower.data;

import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;

import net.sf.cannagrower.i18n.Messages;

import net.sf.orexio.lopf.Data;
import net.sf.orexio.lopf.DataSettings;
import net.sf.orexio.lopf.container.ContainerVector;

/**
* This class store a culture plantation
*
* @author alois_cochard@users.sf.net
*
*/
public class Plantation extends Data implements Serializable{
  static final long serialVersionUID = 1L;
 
  private String  id;

  private Date   started = new Date();

  private Date   finished = null;

  transient private ContainerVector events;
 
  public Plantation(Culture culture){
    this.setParent(culture);
    id=String.valueOf(this.hashCode());
  }

  public void clean() throws IOException{
    Culture culture=(Culture)getParent();
    if(culture!=null){culture.getContainerVectors().remove(events);}
  }
 
  public DataSettings getSettings(){return new DataSettings(Plantation.class,2);}
 
  public int compare(Data data){
    if(data instanceof Plantation){
      Plantation plantation=(Plantation)data;
      return this.started.compareTo(plantation.started);
    }
    return super.compare(data);
  }

  public String toString() {
    String suffix = Messages.getMessage(Messages.plantationActive);
    if (finished != null) {
      suffix = DateFormat.getDateInstance(DateFormat.SHORT).format(finished);
    }
    return DateFormat.getDateInstance(DateFormat.SHORT).format(started) + " - " + suffix;
  }
  public long getRank(){return getStarted().getTime();}
 
  /**
   * This method return started date
   *
   * @return Plantation started date
   */
  public Date getStarted() {
    return started;
  }

  /**
   * This method define started date
   *
   * @param started
   *            Plantation started date
   */
  public void setStarted(Date started) {
    if (started == null) {
      return;
    }
    if (this.started != null) {
      if (DateFormat.getDateInstance().format(started).equals(DateFormat.getDateInstance().format(this.started))) {
        return;
      }
    }
    if(finished!=null){
      if (started.before(finished)) {
        this.started = finished;
        return;
      }
    }
    this.started = started;
    setModified(true);
  }

  /**
   * This method return the finished date
   *
   * @return Plantation finished date
   */
  public Date getFinished() {
    return finished;
  }

  /**
   * This method define started date
   *
   * @param finished
   *            Plantation finished date
   */
  public void setFinished(Date finished) {
    if (finished != null) {
      if (this.finished != null) {
        if (DateFormat.getDateInstance().format(finished).equals(DateFormat.getDateInstance().format(this.finished))) {
          return;
        }
        if (finished.before(started)) {
          finished = started;
        }
      }
    } else {
      if (this.finished == null) {
        return;
      }
    }
    this.finished = finished;
    setModified(true);
  }

  /**
   * This method return the active state
   *
   * @return True if plantation is active (not finished)
   */
  public boolean isActive() {
    if (started.after(new Date())) {
      return false;
    }
    if (finished == null) {
      return true;
    }
    if (finished.before(new Date())) {
      return false;
    }
    return true;
  }

  public ContainerVector getEvents() {
    if(events==null){
      Culture culture=(Culture)getParent();
      try{
        events=new ContainerVector(this,culture.getRepository(),id,Event.class);
      }catch(ClassNotFoundException e){
        Messages.showException(e);
      }catch(java.io.IOException e){
        Messages.showException(e);
      }
      culture.getContainerVectors().add(events);
    }
    return events;
  }

}
TOP

Related Classes of net.sf.cannagrower.data.Plantation

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.