package pl.srw.billcalculator.persistence;

import android.database.sqlite.SQLiteDatabase;

import pl.srw.billcalculator.db.PgeG12Bill;
import pl.srw.billcalculator.db.dao.HistoryDao;
import pl.srw.billcalculator.db.dao.PgeBillDao;
import pl.srw.billcalculator.db.dao.PgeG12BillDao;
import pl.srw.billcalculator.db.dao.PgnigBillDao;
import pl.srw.billcalculator.persistence.type.BillType;

/**
 * Created by Kamil Seweryn.
 */
public final class Triggers {

    public static final String[] BILL_INSERT_TRIGGERS =
            {insertTrigger("bill_pge_insert_trigger", PgeBillDao.TABLENAME, BillType.PGE.toString()),
             insertTrigger("bill_pgeG12_insert_trigger", PgeG12BillDao.TABLENAME, BillType.PGE_G12.toString()),
             insertTrigger("bill_pgnig_insert_trigger", PgnigBillDao.TABLENAME, BillType.PGNIG.toString())};

    private static String insertTrigger(final String triggerName, final String tableName, final String billType) {
        return "CREATE TRIGGER " + triggerName + " AFTER INSERT ON " + tableName + " BEGIN " +
                getInnerInsert(billType) + " END;";
    }

    private static String getInnerInsert(final String billType) {
        return "INSERT INTO " + HistoryDao.TABLENAME +
                "(" +
                HistoryDao.Properties.DateFrom.columnName + "," + HistoryDao.Properties.BillType.columnName + "," + HistoryDao.Properties.BillId.columnName +
                ") VALUES(" +
                "new." + PgeBillDao.Properties.DateFrom.columnName + ", '" + billType + "', new." + PgeBillDao.Properties.Id.columnName +
                ");";
    }

    public static void create(SQLiteDatabase db) {
        for (String sql : Triggers.BILL_INSERT_TRIGGERS)
            db.execSQL(sql);
//                    for (String sql : Triggers.BILL_DELETE_TRIGGERS)
//                        db.execSQL(sql);//TODO
        
    }
}

@@ -506,16 +506,18 @@
 IGGERS =
+ %7B
 %0A       
@@ -521,17 +521,16 @@
         
-%7B
 insertTr
@@ -602,33 +602,32 @@
 GE.toString()),%0A
- 
             inse
@@ -713,33 +713,32 @@
 12.toString()),%0A
- 
             inse
@@ -826,16 +826,318 @@
 tring())
+%0A    %7D;%0A    private static String%5B%5D BILL_DELETE_TRIGGERS = %7B%0A            deleteTrigger(%22bill_pge_delete_trigger%22, PgeBillDao.TABLENAME),%0A            deleteTrigger(%22bill_pgeG12_delete_trigger%22, PgeG12BillDao.TABLENAME),%0A            deleteTrigger(%22bill_pgnig_delete_trigger%22, PgnigBillDao.TABLENAME)%0A    
 %7D;%0A%0A    
@@ -1895,24 +1895,483 @@
 );%22;%0A    %7D%0A%0A
+    private static String deleteTrigger(String triggerName, String tableName) %7B%0A        return %22CREATE TRIGGER %22 + triggerName + %22 AFTER DELETE ON %22 + tableName + %22 BEGIN %22 +%0A                getInnerDelete() + %22 END;%22;%0A    %7D%0A%0A    private static String getInnerDelete() %7B%0A        return %22DELETE FROM %22 + HistoryDao.TABLENAME + %0A                %22 WHERE %22 + HistoryDao.Properties.BillId.columnName + %22 = old.%22 + PgeBillDao.Properties.Id.columnName + %22;%22;%0A    %7D%0A%0A
     public s
@@ -2495,30 +2495,16 @@
 L(sql);%0A
-//            
         
@@ -2556,11 +2556,8 @@
 RS)%0A
-// 
     
@@ -2564,27 +2564,16 @@
         
-           
 db.execS
@@ -2584,14 +2584,8 @@
 ql);
-//TODO
 %0A   

delete triggers