public static void EnsureNivelUpperExists(PersistencyHelper.PreConcArguments args) { PersistencyHelper.VerifyIfRHNivelUpperExistsPreConcArguments pcArgs = null; pcArgs = (PersistencyHelper.VerifyIfRHNivelUpperExistsPreConcArguments)args; GISADataset.NivelRow nRow = (GISADataset.NivelRow)(GisaDataSetHelper.GetInstance().Nivel.Select("ID=" + pcArgs.nRowID.ToString())[0]); long rhRowIDUpper = nRow.GetRelacaoHierarquicaRowsByNivelRelacaoHierarquica()[0].IDUpper; if (rhRowIDUpper < 0) { pcArgs.RHNivelUpperExists = true; } else { // antes de obter o código verificar se adição de um novo nível ainda é possível (por razões de concorrência) pcArgs.RHNivelUpperExists = NivelRule.Current.isNivelDeleted(rhRowIDUpper, args.tran); } if (!pcArgs.RHNivelUpperExists) { GISADataset.RelacaoHierarquicaRow rhRow = (GISADataset.RelacaoHierarquicaRow)(GisaDataSetHelper.GetInstance().RelacaoHierarquica.Select(string.Format("ID={0} AND IDUpper={1}", pcArgs.rhRowID, pcArgs.rhRowIDUpper))[0]); GISADataset.NivelDesignadoRow ndRow = (GISADataset.NivelDesignadoRow)(GisaDataSetHelper.GetInstance().NivelDesignado.Select("ID=" + pcArgs.ndRowID.ToString())[0]); GISADataset.FRDBaseRow frdBaseRow = (GISADataset.FRDBaseRow)(GisaDataSetHelper.GetInstance().FRDBase.Select("ID=" + pcArgs.frdBaseID.ToString())[0]); System.Data.DataSet tempgisaBackup1 = pcArgs.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup1, rhRow); pcArgs.gisaBackup = tempgisaBackup1; rhRow.RejectChanges(); System.Data.DataSet tempgisaBackup2 = pcArgs.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup2, ndRow); pcArgs.gisaBackup = tempgisaBackup2; ndRow.RejectChanges(); System.Data.DataSet tempgisaBackup3 = pcArgs.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup3, nRow); pcArgs.gisaBackup = tempgisaBackup3; nRow.RejectChanges(); System.Data.DataSet tempgisaBackup4 = pcArgs.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup4, frdBaseRow); pcArgs.gisaBackup = tempgisaBackup4; frdBaseRow.RejectChanges(); pcArgs.message = "Não foi possível criar a unidade informacional uma" + Environment.NewLine + "vez que a unidade superior foi apagada por outro utilizador."; } }
public static void HandleUF(PersistencyHelper.PreConcArguments args) { PersistencyHelper.AddEditUFPreConcArguments aeufpca = null; PersistencyHelper.IsCodigoUFBeingUsedPreSaveArguments psa = null; aeufpca = (PersistencyHelper.AddEditUFPreConcArguments)args; psa = (PersistencyHelper.IsCodigoUFBeingUsedPreSaveArguments)aeufpca.psa; psa.cancelSetNewCodigo = false; aeufpca.message = string.Empty; GISADataset.RelacaoHierarquicaRow rhufRow = null; if (GisaDataSetHelper.GetInstance().RelacaoHierarquica.Select(string.Format("ID={0} AND IDUpper={1}", aeufpca.rhufRowID, aeufpca.rhufRowIDUpper)).Length > 0) { rhufRow = (GISADataset.RelacaoHierarquicaRow)(GisaDataSetHelper.GetInstance().RelacaoHierarquica.Select(string.Format("ID={0} AND IDUpper={1}", aeufpca.rhufRowID, aeufpca.rhufRowIDUpper))[0]); } else if (GisaDataSetHelper.GetInstance().RelacaoHierarquica.Select(string.Format("ID={0} AND IDUpper={1}", aeufpca.rhufRowID, aeufpca.rhufRowIDUpper), "", DataViewRowState.Deleted).Length > 0) { rhufRow = (GISADataset.RelacaoHierarquicaRow)(GisaDataSetHelper.GetInstance().RelacaoHierarquica.Select(string.Format("ID={0} AND IDUpper={1}", aeufpca.rhufRowID, aeufpca.rhufRowIDUpper), "", DataViewRowState.Deleted)[0]); } GISADataset.NivelDesignadoRow ndufRow = (GISADataset.NivelDesignadoRow)(GisaDataSetHelper.GetInstance().NivelDesignado.Select("ID=" + aeufpca.ndufRowID.ToString())[0]); GISADataset.NivelRow nivelUFRow = (GISADataset.NivelRow)(GisaDataSetHelper.GetInstance().Nivel.Select("ID=" + aeufpca.nivelUFRowID.ToString())[0]); if (aeufpca.Operation == PersistencyHelper.AddEditUFPreConcArguments.Operations.Create || aeufpca.Operation == PersistencyHelper.AddEditUFPreConcArguments.Operations.CreateLike) { GISADataset.NivelUnidadeFisicaRow nufufRow = (GISADataset.NivelUnidadeFisicaRow)(GisaDataSetHelper.GetInstance().NivelUnidadeFisica.Select("ID=" + aeufpca.nufufRowID.ToString())[0]); // validar criação de UF if (DBAbstractDataLayer.DataAccessRules.UFRule.Current.isNivelRowDeleted(rhufRow.NivelRowByNivelRelacaoHierarquicaUpper.ID, aeufpca.tran)) { var tempgisaBackup = aeufpca.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup, nufufRow); aeufpca.gisaBackup = tempgisaBackup; nufufRow.RejectChanges(); PersistencyHelper.BackupRow(ref tempgisaBackup, rhufRow); aeufpca.gisaBackup = tempgisaBackup; rhufRow.RejectChanges(); PersistencyHelper.BackupRow(ref tempgisaBackup, ndufRow); aeufpca.gisaBackup = tempgisaBackup; ndufRow.RejectChanges(); PersistencyHelper.BackupRow(ref tempgisaBackup, nivelUFRow); aeufpca.gisaBackup = tempgisaBackup; nivelUFRow.RejectChanges(); List <DataRow> lstRows = new List <DataRow>(); // recolher rows para fazer backup caso a operação seja "CREATE LIKE" if (aeufpca.Operation == PersistencyHelper.AddEditUFPreConcArguments.Operations.CreateLike) { lstRows.AddRange(GisaDataSetHelper.GetInstance().SFRDDatasProducao.Select("IDFRDBase=" + aeufpca.frdufRowID.ToString())); lstRows.AddRange(GisaDataSetHelper.GetInstance().SFRDUFCota.Select("IDFRDBase=" + aeufpca.frdufRowID.ToString())); lstRows.AddRange(GisaDataSetHelper.GetInstance().SFRDConteudoEEstrutura.Select("IDFRDBase=" + aeufpca.frdufRowID.ToString())); lstRows.AddRange(GisaDataSetHelper.GetInstance().SFRDUFDescricaoFisica.Select("IDFRDBase=" + aeufpca.frdufRowID.ToString())); foreach (long uaAssociadaID in aeufpca.uaAssociadas) { lstRows.AddRange(GisaDataSetHelper.GetInstance().SFRDUnidadeFisica.Select(string.Format("IDFRDBase={0} AND IDNivel={1}", uaAssociadaID, aeufpca.nivelUFRowID))); } } lstRows.AddRange(GisaDataSetHelper.GetInstance().FRDBase.Select("ID=" + aeufpca.frdufRowID.ToString())); PersistencyHelper.BackupRows(ref tempgisaBackup, lstRows); aeufpca.gisaBackup = tempgisaBackup; aeufpca.OperationError = PersistencyHelper.AddEditUFPreConcArguments.OperationErrors.NewUF; aeufpca.message = "A entidade produtora que pretende associar à unidade física foi eliminada por outro utilizador. Esta operação não poderá, por isso, ser concluída."; psa.cancelSetNewCodigo = true; aeufpca.continueSave = false; } } else { // validar edição de UF bool isUFDeleted = DBAbstractDataLayer.DataAccessRules.UFRule.Current.isNivelRowDeleted(nivelUFRow.ID, aeufpca.tran); // verificar se a UF a editar não foi apagada por outro utilizador if (isUFDeleted) { System.Data.DataSet tempgisaBackup6 = aeufpca.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup6, ndufRow); aeufpca.gisaBackup = tempgisaBackup6; ndufRow.RejectChanges(); System.Data.DataSet tempgisaBackup7 = aeufpca.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup7, nivelUFRow); aeufpca.gisaBackup = tempgisaBackup7; nivelUFRow.RejectChanges(); aeufpca.OperationError = PersistencyHelper.AddEditUFPreConcArguments.OperationErrors.EditEDAndDesignacao; aeufpca.message = "A unidade física em edição foi eliminada por outro utilizador. " + Environment.NewLine + "Esta operação não poderá, por isso, ser concluída."; psa.cancelSetNewCodigo = true; aeufpca.continueSave = false; return; } // Verificar se se pretende alterar a entidade detentora associada bool isRelacaoHierarquicaDeleted = false; if (rhufRow.RowState == DataRowState.Deleted) { isRelacaoHierarquicaDeleted = DBAbstractDataLayer.DataAccessRules.UFRule.Current.isRelacaoHierarquicaDeleted(aeufpca.rhufRowID, aeufpca.rhufRowIDUpper, aeufpca.tran); } else { // a operação pretendida é editar a designação da UF e por esse motivo não é necessário // atribuir um novo código (a operação não é create e a entidade detentora não foi alterada) psa.cancelSetNewCodigo = true; aeufpca.continueSave = false; } GISADataset.RelacaoHierarquicaRow[] newRhufRow = (GISADataset.RelacaoHierarquicaRow[])(GisaDataSetHelper.GetInstance().RelacaoHierarquica.Select(string.Format("ID={0} AND IDUpper={1}", aeufpca.newRhufRowID, aeufpca.newRhufRowIDUpper))); // Verificar se outro utilizador também alterou (concorrentemente) a entidade detentora associada if (isRelacaoHierarquicaDeleted) { System.Data.DataSet tempgisaBackup8 = aeufpca.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup8, newRhufRow[0]); aeufpca.gisaBackup = tempgisaBackup8; newRhufRow[0].RejectChanges(); System.Data.DataSet tempgisaBackup9 = aeufpca.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup9, rhufRow); aeufpca.gisaBackup = tempgisaBackup9; rhufRow.RejectChanges(); System.Data.DataSet tempgisaBackup10 = aeufpca.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup10, nivelUFRow); aeufpca.gisaBackup = tempgisaBackup10; nivelUFRow.RejectChanges(); aeufpca.OperationError = PersistencyHelper.AddEditUFPreConcArguments.OperationErrors.EditOriginalEd; aeufpca.message = "A entidade detentora da unidade física em edição foi alterada por outro utilizador." + Environment.NewLine + "Esta operação não poderá, por isso, ser concluída."; psa.cancelSetNewCodigo = true; aeufpca.continueSave = false; } else if (newRhufRow.Length > 0) // se tivermos alterado a entidade detentora da uf { // verificar se a ED a associar à UF não foi apagada por outro utilizador if (DBAbstractDataLayer.DataAccessRules.UFRule.Current.isNivelRowDeleted(newRhufRow[0].NivelRowByNivelRelacaoHierarquicaUpper.ID, aeufpca.tran)) { System.Data.DataSet tempgisaBackup11 = aeufpca.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup11, newRhufRow[0]); aeufpca.gisaBackup = tempgisaBackup11; newRhufRow[0].RejectChanges(); System.Data.DataSet tempgisaBackup12 = aeufpca.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup12, rhufRow); aeufpca.gisaBackup = tempgisaBackup12; rhufRow.RejectChanges(); System.Data.DataSet tempgisaBackup13 = aeufpca.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup13, nivelUFRow); aeufpca.gisaBackup = tempgisaBackup13; nivelUFRow.RejectChanges(); if (!isUFDeleted) { // UF existe mas a ED que se pretende adicionar não aeufpca.OperationError = PersistencyHelper.AddEditUFPreConcArguments.OperationErrors.EditNewEd; aeufpca.message = "A entidade produtora que pretende associar à unidade física foi eliminada por outro utilizador. Esta operação não poderá, por isso, ser concluída."; } psa.cancelSetNewCodigo = true; aeufpca.continueSave = false; } //else if (!isUFDeleted) //{ // // Tanto a UF como a ED existem; // NivelRule.Current.DeleteSFRDUnidadeFisica(nivelUFRow.ID, aeufpca.tran); // foreach (GISADataset.SFRDUnidadeFisicaRow relacaoRow in nivelUFRow.GetSFRDUnidadeFisicaRows()) // { // System.Data.DataSet tempgisaBackup14 = aeufpca.gisaBackup; // PersistencyHelper.BackupRow(ref tempgisaBackup14, relacaoRow); // aeufpca.gisaBackup = tempgisaBackup14; // relacaoRow.Delete(); // } //} } } }
public static void ensureUniqueCodigo(PersistencyHelper.PreConcArguments args) { PersistencyHelper.EnsureUniqueCodigoNivelPreConcArguments pcArgs = null; pcArgs = (PersistencyHelper.EnsureUniqueCodigoNivelPreConcArguments)args; GISADataset.NivelRow nRow = (GISADataset.NivelRow)(GisaDataSetHelper.GetInstance().Nivel.Select("ID=" + pcArgs.nRowID.ToString())[0]); GISADataset.NivelDesignadoRow ndRow = (GISADataset.NivelDesignadoRow)(GisaDataSetHelper.GetInstance().NivelDesignado.Select("ID=" + pcArgs.ndRowID.ToString())[0]); GISADataset.RelacaoHierarquicaRow rhRow = null; if (GisaDataSetHelper.GetInstance().RelacaoHierarquica.Select(string.Format("ID={0} AND IDUpper={1}", pcArgs.rhRowID, pcArgs.rhRowIDUpper)).Length > 0) { rhRow = (GISADataset.RelacaoHierarquicaRow)(GisaDataSetHelper.GetInstance().RelacaoHierarquica.Select(string.Format("ID={0} AND IDUpper={1}", pcArgs.rhRowID, pcArgs.rhRowIDUpper))[0]); } // Se o nível em questão for uma entidade detentora if (rhRow == null && nRow.TipoNivelRow.ID == TipoNivel.LOGICO) { if (DBAbstractDataLayer.DataAccessRules.NivelRule.Current.isUniqueCodigo(nRow.Codigo, nRow.ID, pcArgs.tran, pcArgs.testOnlyWithinNivel)) { pcArgs.successful = true; } else { var cod = nRow.Codigo; System.Data.DataSet tempgisaBackup1 = pcArgs.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup1, ndRow); pcArgs.gisaBackup = tempgisaBackup1; System.Data.DataSet tempgisaBackup2 = pcArgs.gisaBackup; PersistencyHelper.BackupRow(ref tempgisaBackup2, nRow); pcArgs.gisaBackup = tempgisaBackup2; ndRow.RejectChanges(); PermissoesHelper.UndoAddNivelGrantPermissions(nRow); nRow.RejectChanges(); pcArgs.message = string.Format( "Não é possível completar a operação porque não é permitido " + Environment.NewLine + "que duas unidades de informação tenham o mesmo código " + Environment.NewLine + "parcial ({0}) no mesmo nivel de descrição.", cod); } } else { // antes de obter o código verificar se adição de um novo nível ainda é possível (por razões // de concorrência é necessário garantir que tanto o nível acima mantém-se na base de dados // até o save estar terminado) bool upperRelationExists; if (rhRow.IDUpper < 0) { upperRelationExists = true; } else { upperRelationExists = NivelRule.Current.isNivelDeleted(rhRow.IDUpper, args.tran); } if (!upperRelationExists) { var frdRow = GisaDataSetHelper.GetInstance().FRDBase.Cast <GISADataset.FRDBaseRow>().Single(r => r.ID == pcArgs.frdBaseID); Nivel.DeleteInDataSet(frdRow, false, pcArgs.gisaBackup); Nivel.DeleteInDataSet(nRow, false, pcArgs.gisaBackup); pcArgs.message = "Não foi possível criar/editar a unidade informacional uma" + Environment.NewLine + "vez que a unidade superior foi apagada por outro utilizador."; } else { if (DBAbstractDataLayer.DataAccessRules.NivelRule.Current.isUniqueCodigo(nRow.Codigo, nRow.ID, pcArgs.tran, pcArgs.testOnlyWithinNivel, rhRow.NivelRowByNivelRelacaoHierarquicaUpper.ID)) { pcArgs.successful = true; } else { var cod = nRow.Codigo; if (nRow.RowState == DataRowState.Modified) { nRow.RejectChanges(); GisaDataSetHelper.GetInstance().IndexFRDCA.Cast <GISADataset.IndexFRDCARow>() .Where(r => r.RowState != DataRowState.Unchanged).ToList().ForEach(r => r.RejectChanges()); } else if (nRow.RowState == DataRowState.Added) { var frdRow = GisaDataSetHelper.GetInstance().FRDBase.Cast <GISADataset.FRDBaseRow>().Single(r => r.ID == pcArgs.frdBaseID); Nivel.DeleteInDataSet(frdRow, false, pcArgs.gisaBackup); Nivel.DeleteInDataSet(nRow, false, pcArgs.gisaBackup); } pcArgs.message = string.Format( "Não é possível completar a operação porque não é permitido " + Environment.NewLine + "que duas unidades de informação tenham o mesmo código " + Environment.NewLine + "parcial ({0}) no mesmo nivel de descrição.", cod); } } } }