package net.sf.cannagrower.data;
import java.util.Vector;
import java.io.IOException;
import de.schlichtherle.io.File;
import net.sf.cannagrower.CannaGrower;
import net.sf.cannagrower.data.hardware.Extractor;
import net.sf.cannagrower.data.hardware.Intractor;
import net.sf.cannagrower.data.hardware.Lamp;
import net.sf.cannagrower.data.hardware.MediumSoil;
import net.sf.cannagrower.data.hardware.Room;
import net.sf.cannagrower.i18n.Messages;
import net.sf.orexio.lopf.DataSettings;
import net.sf.orexio.lopf.DataSource;
import net.sf.orexio.lopf.container.ContainerVector;
import net.sf.orexio.lopf.repository.TrueZipRepository;
import net.sf.orexio.lopf.serializer.XmlSerializer;
public class Culture extends DataSource<File>{
private static final long serialVersionUID = 1L;
public static final int typeEmpty = 0;
public static final int typeDemo = 1;
private ContainerVector hardwares;
private ContainerVector plantations;
private Culture(TrueZipRepository repository)throws IOException,ClassNotFoundException{
super(repository);
}
/**
* This method initialize this
*
* @param type
*/
public static Culture create(int type)throws IOException,ClassNotFoundException{
Culture culture;
TrueZipRepository repository;
switch (type) {
case typeDemo:
java.util.Date date=new java.util.Date();
Plantation plantation;
net.sf.cannagrower.data.Event event;
repository=new TrueZipRepository(new File(Messages.getMessage(Messages.cultureDemoName)+CannaGrower.extension));
repository.open();
culture=new Culture(repository);
culture.getHardwares().store(new Room());
culture.getHardwares().store(new Lamp());
culture.getHardwares().store(new Intractor());
culture.getHardwares().store(new Extractor());
culture.getHardwares().store(new MediumSoil());
plantation=new Plantation(culture);
event=new net.sf.cannagrower.data.event.PlantsAdd();
event.setDate(date);
plantation.getEvents().store(event);
date=new java.util.Date(date.getTime()+((1000*60)*60)*24);
event=new net.sf.cannagrower.data.event.EnvironmentAirState();
event.setDate(date);
plantation.getEvents().store(event);
event=new net.sf.cannagrower.data.event.EnvironmentMediumState();
event.setDate(date);
plantation.getEvents().store(event);
date=new java.util.Date(date.getTime()+((1000*60)*60)*24);
event=new net.sf.cannagrower.data.event.PlantsCut();
event.setDate(date);
plantation.getEvents().store(event);
date=new java.util.Date(date.getTime()+((1000*60)*60)*24);
event=new net.sf.cannagrower.data.event.EnvironmentLightPeriod();
event.setDate(date);
plantation.getEvents().store(event);
date=new java.util.Date(date.getTime()+((1000*60)*60)*24);
event=new net.sf.cannagrower.data.event.PlantsCut();
event.setDate(date);
plantation.getEvents().store(event);
date=new java.util.Date(date.getTime()+((1000*60)*60)*24);
event=new net.sf.cannagrower.data.event.EnvironmentAirState();
event.setDate(date);
plantation.getEvents().store(event);
event=new net.sf.cannagrower.data.event.EnvironmentMediumState();
event.setDate(date);
plantation.getEvents().store(event);
date=new java.util.Date(date.getTime()+((1000*60)*60)*24);
event=new net.sf.cannagrower.data.event.PlantsCut();
event.setDate(date);
plantation.getEvents().store(event);
date=new java.util.Date(date.getTime()+((1000*60)*60)*24);
event=new net.sf.cannagrower.data.event.PlantsHarvest();
event.setDate(date);
plantation.getEvents().store(event);
culture.getPlantations().store(plantation);
culture.setModified(false);
culture.setNew(false);
break;
default:
repository=new TrueZipRepository(new File(Messages.getMessage(Messages.cultureNewName)+CannaGrower.extension));
repository.setSerializer(XmlSerializer.getSerializer());
repository.open();
culture=new Culture(repository);
culture.repository.read();
culture.setNew(true);
culture.setModified(false);
break;
}
return culture;
}
public static Culture read(java.io.File file)throws IOException,ClassNotFoundException{
TrueZipRepository repository=new TrueZipRepository(new File(file));
Culture culture;
repository.open();
repository.read();
culture=new Culture (repository);
culture.setNew(false);
culture.setModified(false);
return culture;
}
public static void write(Culture culture, java.io.File file) throws IOException {
culture.repository.setId(new File(file));
culture.setModified(true);
culture.save();
culture.setNew(false);
culture.setModified(false);
culture.repository.write();
}
public String getName(){
File file=(File)repository.getId();
return file.getName();
}
public DataSettings getSettings(){return new DataSettings(Culture.class,4);}
public ContainerVector getHardwares() {
return hardwares;
}
public void setHardwares(ContainerVector hardwares) {
this.hardwares = hardwares;
}
public ContainerVector getPlantations() {
return plantations;
}
public void setPlantations(ContainerVector plantations) {
this.plantations = plantations;
}
protected Vector<ContainerVector> addContainerVectors() throws IOException,ClassNotFoundException{
Vector<ContainerVector> ghostVectors=new Vector<ContainerVector>();
hardwares=new ContainerVector(this,repository,null,Hardware.class);
plantations=new ContainerVector(this,repository,null,Plantation.class);
ghostVectors.add(hardwares);
ghostVectors.add(plantations);
return ghostVectors;
}
public String toString(){return getName();}
}