//Auto generated file
//Do not edit this file
package org.sisiya.ui.standart;
import org.sql2o.Connection;
import org.sql2o.Query;
import java.util.List;
import org.sisiya.ui.Application;

									import java.util.Date;
	
public class System_service_alert extends Model{

	//Fields  
  protected int id;
  protected int user_id;
  protected int system_id;
  protected int service_id;
  protected int alert_type_id;
  protected int status_id;
  protected int frequency;
  protected boolean enabled;
  protected Date last_alert_time;

	private	ModelHooks mh;
	private Boolean isNew;
 //Constructer
  public System_service_alert(){
  	isNew = true;
  	//Default Constructer
    fields.add(new Field("id","int","system_service_alert-id",true,true));
   fields.add(new Field("user_id","int","system_service_alert-user_id",false,false));
   fields.add(new Field("system_id","int","system_service_alert-system_id",false,false));
   fields.add(new Field("service_id","int","system_service_alert-service_id",false,false));
   fields.add(new Field("alert_type_id","int","system_service_alert-alert_type_id",false,false));
   fields.add(new Field("status_id","int","system_service_alert-status_id",false,false));
   fields.add(new Field("frequency","int","system_service_alert-frequency",false,false));
   fields.add(new Field("enabled","boolean","system_service_alert-enabled",false,false));
   fields.add(new Field("last_alert_time","Date","system_service_alert-last_alert_time",false,false));
  }
  public void registerHooks(ModelHooks _mh){
	  mh = _mh;
  }
 //Setters

  public void setId(int _id){
    id = _id;
  }
  public void setUser_id(int _user_id){
    user_id = _user_id;
  }
  public void setSystem_id(int _system_id){
    system_id = _system_id;
  }
  public void setService_id(int _service_id){
    service_id = _service_id;
  }
  public void setAlert_type_id(int _alert_type_id){
    alert_type_id = _alert_type_id;
  }
  public void setStatus_id(int _status_id){
    status_id = _status_id;
  }
  public void setFrequency(int _frequency){
    frequency = _frequency;
  }
  public void setEnabled(boolean _enabled){
    enabled = _enabled;
  }
  public void setLast_alert_time(Date _last_alert_time){
    last_alert_time = _last_alert_time;
  }
  public void setIsNew(boolean b){
	isNew = b;    
  }
  //Getters

  public int getId(){
    return id;
  }
  public int getUser_id(){
    return user_id;
  }
  public int getSystem_id(){
    return system_id;
  }
  public int getService_id(){
    return service_id;
  }
  public int getAlert_type_id(){
    return alert_type_id;
  }
  public int getStatus_id(){
    return status_id;
  }
  public int getFrequency(){
    return frequency;
  }
  public boolean getEnabled(){
    return enabled;
  }
  public Date getLast_alert_time(){
    return last_alert_time;
  }
	public Boolean isNew() {
		return isNew;
  }
	//Data Access Methods
	//Data Access Methods
	public boolean save(Connection _connection){
		return(save(_connection,true,true));
	}
	public boolean save(Connection _connection,boolean doValidate,boolean executeHooks){
		if(doValidate){
			try {
				if(!mh.validate(_connection)){
					return(false);
				}
			} catch (Exception e) {
				errors.add(new Error(e.getClass().getName(), e.getMessage()));
				return(false);
			}
			if(!isReadyToSave()){
				return(false);
			}
		}
		if(executeHooks){
			try {
				if(isNew()){
					if(!mh.beforeInsert(_connection)){return(false);}
				}else{
					if(!mh.beforeUpdate(_connection)){return(false);}
				}
				if(!mh.beforeSave(_connection)){return(false);}
			} catch (Exception e) {
				errors.add(new Error(e.getClass().getName(), e.getMessage()));
				return(false);
			}
			//Actual db update
			if(isNew()){
				try {
					if(!insert(_connection)){return(false);}
				  	if(!mh.afterInsert(_connection)){return(false);}
				  	if(!mh.afterSave(_connection)){return(false);}
				} catch (Exception e) {
					errors.add(new Error(e.getClass().getName(), e.getMessage()));
					return(false);				
				}
			}else{
				try {
					if(!update(_connection)){return(false);}
					if(!mh.afterUpdate(_connection)){return(false);}
					if(!mh.afterSave(_connection)){return(false);}
				} catch (Exception e) {
					errors.add(new Error(e.getClass().getName(), e.getMessage()));
					return(false);				
				}			
			}
		}else{
			//Actual db operation without hooks
			if(isNew()){
				try {
					if(!insert(_connection)){return(false);}
				} catch (Exception e) {
					errors.add(new Error(e.getClass().getName(), e.getMessage()));
					return(false);				
				}
			}else{
				try {
					if(!update(_connection)){return(false);}
				} catch (Exception e) {
					errors.add(new Error(e.getClass().getName(), e.getMessage()));
					return(false);				
				}			
			}
		}
		return true;
	}
	public boolean destroy(Connection _connection,boolean executeHooks){
		if(executeHooks){
			if(!mh.beforeDestroy(_connection)){return(false);}
		}
		if(!delete(_connection)){return(false);}
		if(executeHooks){
			if(!mh.afterDestroy(_connection)){return(false);}
		}
		return(true);
	}
	public boolean destroy(Connection _connection){
		return(destroy(_connection,true));
	}

	//Private Data Acess utility Methods
	private boolean insert(Connection _connection){
    Query query;
    query = _connection.createQuery(insertSql(),true);
	
	
			query = query.addParameter("user_idP",user_id);
	query = query.addParameter("system_idP",system_id);
	query = query.addParameter("service_idP",service_id);
	query = query.addParameter("alert_type_idP",alert_type_id);
	query = query.addParameter("status_idP",status_id);
	query = query.addParameter("frequencyP",frequency);
	query = query.addParameter("enabledP",enabled);
	query = query.addParameter("last_alert_timeP",last_alert_time);

		
    id = (int) query.executeUpdate().getKey();
    
    return(true);
	}
	private boolean update(Connection _connection){
    Query query;
    query = _connection.createQuery(updateSql());
	
	query = query.addParameter("idP",id);
	
	query = query.addParameter("user_idP",user_id);
	
	query = query.addParameter("system_idP",system_id);
	
	query = query.addParameter("service_idP",service_id);
	
	query = query.addParameter("alert_type_idP",alert_type_id);
	
	query = query.addParameter("status_idP",status_id);
	
	query = query.addParameter("frequencyP",frequency);
	
	query = query.addParameter("enabledP",enabled);
	
	query = query.addParameter("last_alert_timeP",last_alert_time);

    query.executeUpdate();
    return(true);
	}
	private boolean delete(Connection _connection){
		Query query;
		query = _connection.createQuery(deleteSql());
    query.addParameter("idP",id);
    query.executeUpdate();
    return(true);
	}
  private String insertSql(){
    String  querySql = "insert into system_service_alerts( ";
      querySql += "user_id,";
      querySql += "system_id,";
      querySql += "service_id,";
      querySql += "alert_type_id,";
      querySql += "status_id,";
      querySql += "frequency,";
      querySql += "enabled,";
      querySql += "last_alert_time)";
  	
    querySql += "values (";
      querySql += ":user_idP,";
      querySql += ":system_idP,";
      querySql += ":service_idP,";
      querySql += ":alert_type_idP,";
      querySql += ":status_idP,";
      querySql += ":frequencyP,";
      querySql += ":enabledP,";
      querySql += ":last_alert_timeP)"; 
  	
    return querySql;
  }
  private String updateSql(){
    String  querySql = "update system_service_alerts set ";
      querySql += "user_id = :user_idP, " ;
      querySql += "system_id = :system_idP, " ;
      querySql += "service_id = :service_idP, " ;
      querySql += "alert_type_id = :alert_type_idP, " ;
      querySql += "status_id = :status_idP, " ;
      querySql += "frequency = :frequencyP, " ;
      querySql += "enabled = :enabledP, " ;
      querySql += "last_alert_time = :last_alert_timeP ";
  	
			querySql += " where id = :idP";
	
	
	
	
	
	
	
	
	
	
    
   return querySql;
  }
  private String deleteSql(){
			String querySql = "delete from system_service_alerts where id = :idP";
	
	
	
	
	
	
	
	
	
	
  	
  	return querySql;
  }

}
