package p2.application.datadeleteconfighead;

import baseclass.PaoCust;
import p2.pao.PaoSet;
import p2.pao.PaoSetRemote;
import p2.util.P2AppException;
import p2.util.P2Exception;

public class datadeleteconfighead extends PaoCust {

	public datadeleteconfighead(PaoSet arg0) {
		super(arg0);
	}

	@Override
	protected void save() throws P2Exception {
		super.save();
		String OBJECTNAME = getString("OBJECTNAME");
		String TRIGGERNAME = OBJECTNAME + "_DETELE_SUB";
		StringBuilder deletesql = new StringBuilder();

		PaoSetRemote detail = getPaoSet("DATADELETECONFIG");
		int i = 0;
		while (detail.getPao(i) != null) {
			String CHILDOBJECTNAME = detail.getPao(i).getString(
					"CHILDOBJECTNAME");
			String FWHERE = detail.getPao(i).getString("FWHERE");
			deletesql.append(" DELETE H FROM ").append(CHILDOBJECTNAME).append(" H INNER JOIN deleted D ON ").append(FWHERE);
			i++;
		}
		if (!"".equals(deletesql.toString())) {
			String type = runQuerySQL(
					"select name from sysobjects  where xtype='TR'and name='"
							+ TRIGGERNAME + "'").size() > 0 ? "ALTER"
					: "CREATE";
			String sql = type + " TRIGGER [dbo].[" + TRIGGERNAME + "] "
					+ "ON  [dbo].[" + OBJECTNAME + "] " + "AFTER  delete "
					+ "AS " + "BEGIN " + "SET NOCOUNT ON; " + deletesql
					+ " END";
			runUpdateSQL(sql);
		}
	}

	@Override
	public void canDelete() throws P2Exception {
		super.canDelete();
		if ("DATADELETECONFIGHEAD".equals(getString("objectname"))) {
			throw new P2AppException("", "该条数据无法删除");
		}
	}
}
