diff --git a/Back/database/Pipe/DB_Mother/090_Correction_protocol_Entomo.txt b/Back/database/Pipe/DB_Mother/090_Correction_protocol_Entomo.txt new file mode 100644 index 000000000..e393e39c9 --- /dev/null +++ b/Back/database/Pipe/DB_Mother/090_Correction_protocol_Entomo.txt @@ -0,0 +1,76 @@ + +DELETE v + FROM ObservationDynPropValue v + JOIN ObservationDynProp dp ON dp.ID = v.FK_ObservationDynProp + JOIN Observation o on o.ID = v.FK_Observation and o.FK_ProtocoleType = 231 + WHERE dp.Name = 'Identificateur' + + +UPDATE ObservationDynProp SET Name = 'Identified_on' +WHERE Name = 'Identification_on' + + -- SELECT * + -- FROM ProtocoleType_ObservationDynProp l + -- JOIN ObservationDynProp dp ON dp.ID = l.FK_ObservationDynProp ---and dp.Name = 'validator' + -- JOIN ProtocoleType pt on pt.ID = l.FK_ProtocoleType + --WHERE FK_ProtocoleType = 231 + +/**** SET DynProp Conf ****/ +INSERT INTO ObservationDynProp (Name,TypeProp) +VALUES ('Identified_by','String') + +INSERT INTO ProtocoleType_ObservationDynProp (Required,FK_ObservationDynProp,FK_ProtocoleType) +VALUES (0,(SELECT ID FROM ObservationDynProp WHERE Name = 'Identified_by'),231) + + +UPDATE V SET FK_ObservationDynProp = (SELECT ID FROM ObservationDynProp WHERE Name = 'Identified_by') +FROM ObservationDynPropValue v +JOIN ObservationDynProp dp ON dp.ID = v.FK_ObservationDynProp and dp.Name = 'validator' +JOIN Observation o on o.ID = v.FK_Observation and o.FK_ProtocoleType = 231 + +DELETE l +FROM ProtocoleType_ObservationDynProp l + JOIN ObservationDynProp dp ON dp.ID = l.FK_ObservationDynProp and dp.Name in ( 'validator','Identificateur') + JOIN ProtocoleType pt on pt.ID = l.FK_ProtocoleType + WHERE FK_ProtocoleType = 231 + + + /******* SET Form Conf ******/ +UPDATE ModuleForms SET Name = 'Identified_by', Label = 'Identified by' +WHERE TypeObj = 231 and module_id = 1 and Name = 'validator' + +DELETE ModuleForms +WHERE TypeObj = 231 and module_id = 1 and Name = 'Identificateur' + +INSERT INTO ModuleForms +SELECT [module_id] + ,[TypeObj] + ,'Identified_on' + ,'Identified on' + ,[Required] + ,5 + ,5 + ,'Text' + ,[editorClass] + ,[FormRender] + ,10 + ,[Legend] + ,NULL + ,[Validators] + ,[displayClass] + ,[EditClass] + ,[Status] + ,[Locked] + ,[DefaultValue] + ,[Rules] +FROM ModuleForms +WHERE TypeObj = 231 and module_id = 1 and Name = 'Identified_by' + +GO + + + +INSERT INTO [dbo].[TVersion] (TVer_FileName,TVer_Date,TVer_DbName) VALUES ('90_Correction_protocol_Entomo',GETDATE(),(SELECT db_name())) + + +GO diff --git a/Back/database/Pipe/DB_Mother/091_ERD_SP_SendData.txt b/Back/database/Pipe/DB_Mother/091_ERD_SP_SendData.txt new file mode 100644 index 000000000..3c6b3d1f9 --- /dev/null +++ b/Back/database/Pipe/DB_Mother/091_ERD_SP_SendData.txt @@ -0,0 +1,151 @@ + +SET ANSI_NULLS ON +GO + +SET QUOTED_IDENTIFIER ON +GO + + + +ALTER PROCEDURE [dbo].[MessageSendDataToDest] +AS +BEGIN + DECLARE @DestDB VARCHAR(250) + ,@MessageToSendConf BIGINT + ,@SQLText NVARCHAR(4000) + + DECLARE c_destination CURSOR FOR + SELECT DISTINCT C.DestDatabase FROM TMessageSend M JOIN TMessageToSendConf C ON M.ObjectType = C.ObjectType AND M.Operation = C.Operation + WHERE NOT EXISTS (SELECT * FROM TMessageTransfertLog L WHERE L.fk_ConfDest = C.pk_MessageToSendConf and L.fk_MessageSend = pk_MessageSend) + + IF OBJECT_ID('tempdb..#MessageToSend') IS NOT NULL + DROP TABLE #MessageToSend + + IF OBJECT_ID('tempdb..#MessageDetailToSend') IS NOT NULL + DROP TABLE #MessageDetailToSend + + -- GET ALL MESSAGE FOR ALL DEST + + SELECT M.*,C.DestDatabase,C.pk_MessageToSendConf into #MessageToSend + FROM TMessageSend M JOIN TMessageToSendConf C ON M.ObjectType = C.ObjectType AND M.Operation = C.Operation + WHERE M.SendDate IS NULL AND NOT EXISTS (SELECT * FROM TMessageTransfertLog L WHERE L.fk_ConfDest = C.pk_MessageToSendConf and L.fk_MessageSend = pk_MessageSend) + + -- GET ALL ASSOCIATED DETAILS + + SELECT D.*,M.DestDatabase INTO #MessageDetailToSend + FROM dbo.TMessageSendDetail D JOIN #MessageToSend M ON M.pk_MessageSend = D.fk_MessageSend + + + print 'BEGIN TRAN' + OPEN c_destination + + BEGIN TRY + + + FETCH NEXT FROM c_destination + INTO @DestDB + + + + WHILE @@FETCH_STATUS = 0 + BEGIN + + + SET @SQLText = ' DELETE D FROM ' + @DestDB + '.TMessageReceivedDetail D WHERE exists (select * from ' + @DestDB + '.TMessageReceived M where M.isMessageComplete = 0 and M.Provenance =''' + dbo.GetProvenance() + ''' and D.fk_MessageReceived = M.pk_MessageReceived and d.Provenance = m.Provenance)' + print @SQLText + + EXEC(@SQLText) + + SET @SQLText = ' DELETE M FROM ' + @DestDB + '.TMessageReceived M WHERE M.isMessageComplete = 0 and Provenance =''' + dbo.GetProvenance() + '''' + print @SQLText + + EXEC(@SQLText) + + + + -- INSERTING DATE IN DEST TMessageReceived + + SET @SQLText = ' INSERT INTO ' + @DestDB + '.TMessageReceived (pk_MessageReceived,Provenance,ObjectType,ObjectId,ObjectOriginalID,Operation,CreationDate,ImportDate,Comment,isMessageComplete)' + SET @SQLText = @SQLText + ' SELECT pk_MessageSend,''' + dbo.GetProvenance() + ''', M.ObjectType,M.ObjectId,M.ObjectOriginalID,M.Operation,GetDate(),NULL,NULL,0 ' + SET @SQLText = @SQLText + ' FROM #MessageToSend M WHERE M.DestDatabase = ''' + @DestDB + '''' + print @SQLText + + EXEC(@SQLText) + + -- INSERTING DATE IN DEST TMessageReceivedDetail + + SET @SQLText = ' INSERT INTO ' + @DestDB + '.TMessageReceivedDetail (pk_MessageReceivedDetail,fk_MessageReceived,Provenance,PropName,PropValue,Parametre)' + SET @SQLText = @SQLText + ' SELECT D.pk_MessageSendDetail,D.fk_MessageSend,''' + dbo.GetProvenance() + ''', D.PropName,D.PropValue,D.Parametre ' + SET @SQLText = @SQLText + ' FROM #MessageDetailToSend D WHERE D.DestDatabase = ''' + @DestDB + '''' + + print @SQLText + EXEC(@SQLText) + + + + + SET @SQLText = ' UPDATE R SET isMessageComplete=1 FROM ' + @DestDB + '.TMessageReceived R' + SET @SQLText += ' JOIN #MessageToSend S ON R.pk_MessageReceived = S.pk_MessageSend AND R.Provenance = ''' + dbo.GetProvenance() + ''' AND S.DestDatabase = ''' + @DestDB + '''' + + print @SQLText + EXEC(@SQLText) + + + INSERT INTO [TMessageTransfertLog] + ([fk_MessageSend] + ,[fk_ConfDest] + ,[Statut] + ,[SendMessage]) + SELECT pk_MessageSend,pk_MessageToSendConf,0,NULL + FROM #MessageToSend M WHERE M.DestDatabase = @DestDB + + FETCH NEXT FROM c_destination + INTO @DestDB + + END + CLOSE c_destination; + DEALLOCATE c_destination; + + UPDATE TMessageSend SET SendDate=GETDATE() + WHERE pk_MessageSend in (select pk_MessageSend from #MessageToSend) + + + + print 'COMMIT TRAN' + END TRY + BEGIN CATCH + print 'CATCH' + CLOSE c_destination; + DEALLOCATE c_destination; + + + DECLARE @ErrorMessage NVARCHAR(4000); + DECLARE @ErrorSeverity INT; + DECLARE @ErrorState INT; + + SELECT + @ErrorMessage = ERROR_MESSAGE(), + @ErrorSeverity = ERROR_SEVERITY(), + @ErrorState = ERROR_STATE(); + + + DELETE FROM [TMessageTransfertLog] WHERE [fk_MessageSend] IN (select pk_MessageSend from #MessageToSend) + + RAISERROR (@ErrorMessage, -- Message text. + @ErrorSeverity, -- Severity. + @ErrorState -- State. + ); + END CATCH + + END + +GO + + + + + +INSERT INTO [dbo].[TVersion] (TVer_FileName,TVer_Date,TVer_DbName) VALUES ('91_ERD_SP_SendData',GETDATE(),(SELECT db_name())) + + +GO diff --git a/Back/database/Pipe/DB_Mother/092_ERD_CreateMessage_for_eColl.txt b/Back/database/Pipe/DB_Mother/092_ERD_CreateMessage_for_eColl.txt new file mode 100644 index 000000000..2a88380e0 --- /dev/null +++ b/Back/database/Pipe/DB_Mother/092_ERD_CreateMessage_for_eColl.txt @@ -0,0 +1,200 @@ + +SET ANSI_NULLS ON +GO + +SET QUOTED_IDENTIFIER ON +GO + + + + + + +ALTER PROCEDURE [dbo].[pr_MessageExportProtoEntomo] +( +@ProtocoleID INT = NULL +) +AS +BEGIN + + BEGIN TRY + BEGIN TRAN + + + IF OBJECT_ID('tempdb..#ProtocoleID') IS NOT NULL + DROP TABLE #ProtocoleID + + CREATE TABLE #ProtocoleID + (ID INT NOT NULL) + + IF @ProtocoleID IS NULL + BEGIN + + IF OBJECT_ID('tempdb..#ExistsingID') IS NOT NULL + DROP TABLE #ExistsingID + + SELECT ID into #ExistsingID + FROM ExistingSampleEntomoECol + + CREATE CLUSTERED INDEX PK_TmpExistsingID on #ExistsingID(ID) + + + INSERT INTO #ProtocoleID + SELECT O.ID + FROM Observation O JOIN ProtocoleType PT ON o.FK_ProtocoleType = PT.ID AND PT.Name ='Entomo_Pop_Census' + WHERE NOT EXISTS (select * FROM #ExistsingID E where e.ID = o.ID) + END + ELSE + BEGIN + INSERT INTO #ProtocoleID VALUES (@ProtocoleID) + END + + IF OBJECT_ID('tempdb..#StationID') IS NOT NULL + DROP TABLE #StationID + + CREATE TABLE #StationID + (ID INT NOT NULL) + + INSERT INTO #StationID + SELECT DISTINCT S.ID + FROM Station S + JOIN Observation O on o.FK_Station = s.ID + JOIN ProtocoleType PT ON o.FK_ProtocoleType = PT.ID AND PT.Name ='Entomo_Pop_Census' + JOIN ObservationDynPropValuesNow v ON v.FK_Observation = o.ID and v.Name = 'sampled' and v.ValueInt=1 + + + + IF OBJECT_ID('tempdb..#MessageIDs') IS NOT NULL + DROP TABLE #MessageIDs + + + CREATE TABLE #MessageIDs( + ID INT + ) + + + + /*********************************** Exporting Stations */ + + + INSERT INTO [dbo].[TMessageSend] + ([ObjectType] + ,[ObjectId] + ,[ObjectOriginalID] + ,[Operation] + ,[CreationDate] + ,[SendDate] + ,[Comment] + ) + OUTPUT INSERTED.pk_MessageSend INTO #MessageIDs + SELECT DISTINCT 'Station',S.id,dbo.GetProvenance() + '_' + convert(varchar,S.id),'Create',getdate(),NULL,NULL + FROM Station S + WHERE s.id IN (select ID FROM #StationID) + + + + INSERT INTO [dbo].[TMessageSendDetail] + ([fk_MessageSend] + ,[PropName] + ,[PropValue] + ,[Parametre]) + select m.pk_MessageSend,C.PropName,PropValue,NULL + from Station S + JOIN [TMessageSend] M on m.[ObjectId] = S.ID + LEFT JOIN fieldActivity F ON S.fieldActivityId = F.ID LEFT JOIN Region R ON S.FK_Region = R.ID + CROSS APPLY + ( + values + ('StationDate',convert(varchar,S.StationDate,120)) + ,('StationName',convert(varchar,S.Name)) + ,('LAT',convert(varchar,S.LAT)) + ,('LON',convert(varchar,S.LON)) + ,('ELE',convert(varchar,S.ELE)) + ,('precision',convert(varchar,S.precision)) + ,('fieldActivity',F.Name) + ,('Place',S.place) + ,('Region',R.Region) + ,('Comments',s.Comments) + ) C (PropName,PropValue) + WHERE M.pk_MessageSend IN (SELECT ID FROM #MessageIDs) + + TRUNCATE TABLE #MessageIDs + + /**************** Exporting Protocole ********************/ + + INSERT INTO [dbo].[TMessageSend] + ([ObjectType] + ,[ObjectId] + ,[ObjectOriginalID] + ,[Operation] + ,[CreationDate] + ,[SendDate] + ,[Comment] + ) + OUTPUT INSERTED.pk_MessageSend INTO #MessageIDs + SELECT Pt.Name,o.id,dbo.GetProvenance() + '_' + convert(varchar,o.ID),'Create',getdate(),NULL,NULL + FROM Observation O JOIN ProtocoleType PT ON o.FK_ProtocoleType = PT.ID AND PT.Name ='Entomo_Pop_Census' + WHERE o.ID IN (select ID FROM #ProtocoleID) + + + + INSERT INTO [dbo].[TMessageSendDetail] + ([fk_MessageSend] + ,[PropName] + ,[PropValue] + ,[Parametre]) + select m.pk_MessageSend,C.PropName,PropValue,NULL + from Observation O JOIN [TMessageSend] M on m.[ObjectId] = O.ID + CROSS APPLY + ( + values + ('ECol_id',replace(convert(varchar,O.original_id),'Fichier_Alex_ss_Prot_','')) + ,('Comments',O.Comments) + ,('FK_Station',convert(varchar,O.FK_Station)) + + ) C (PropName,PropValue) + WHERE M.pk_MessageSend IN (SELECT ID FROM #MessageIDs) + + + + INSERT INTO [dbo].[TMessageSendDetail] + ([fk_MessageSend] + ,[PropName] + ,[PropValue] + ,[Parametre]) + SELECT m.pk_MessageSend,v.Name,CASE WHEN TYPEPROP ='STring' THEN ValueString WHEN Typeprop ='Integer' THEN convert(varchar,valueint) ELSE ValueString END,NULL + FROM ObservationDynPropValuesNow V JOIN [TMessageSend] M on convert(int,m.[ObjectId]) = V.FK_Observation + WHERE M.pk_MessageSend in (SELECT ID FROM #MessageIDs) + + print 'COMMIT TRAN' + IF @@TRANCOUNT > 0 COMMIT TRAN + + END TRY + BEGIN CATCH + print 'CATCH' + print @@TRANCOUNT + IF @@TRANCOUNT >0 ROLLBACK TRAN; + print @@TRANCOUNT + + DECLARE @ErrorMessage NVARCHAR(4000); + DECLARE @ErrorSeverity INT; + DECLARE @ErrorState INT; + + SELECT + @ErrorMessage = ERROR_MESSAGE(), + @ErrorSeverity = ERROR_SEVERITY(), + @ErrorState = ERROR_STATE(); + + RAISERROR (@ErrorMessage, -- Message text. + @ErrorSeverity, -- Severity. + @ErrorState -- State. + ); + END CATCH + +END + + + +GO + + diff --git a/Back/database/Pipe/DB_Mother/093_SP_IN_eColl_ImportMessageFromERD.txt b/Back/database/Pipe/DB_Mother/093_SP_IN_eColl_ImportMessageFromERD.txt new file mode 100644 index 000000000..38dc906a9 --- /dev/null +++ b/Back/database/Pipe/DB_Mother/093_SP_IN_eColl_ImportMessageFromERD.txt @@ -0,0 +1,294 @@ + +SET ANSI_NULLS ON +GO + +SET QUOTED_IDENTIFIER ON +GO + + + +ALTER PROCEDURE [dbo].[MessageEcolImportFromERD] +AS +BEGIN + print 'MessageEcolImportFromERD' + + BEGIN TRY + BEGIN TRAN + + ------------------------- GESTION DES SUJETS ------------------------- + print 'SUbject insertion' + -- Insertion des nouveaux sujets + INSERT INTO [Subjects] + ([Name] + ,[Original_Id] + ,[Status] + ,[Comment] + ,[TypeObj_ID]) + SELECT DISTINCT D.PropValue,m.Provenance + '_' + convert(varchar,m.ObjectId),4,'',1 + FROM TMessageReceived M JOIN TMessageReceivedDetail D ON M.pk_MessageReceived=D.fk_MessageReceived and D.PropName = 'StationName' + WHERE Importdate IS NULL AND M.ObjectType ='Station' AND m.Provenance like 'EcoReleve%' and m.isMessageComplete = 1 + AND NOT EXISTS (SELECT * FROM [Subjects] S WHERE [TypeObj_ID] =1 and S.Original_Id = m.ObjectOriginalID) + + + ---- MAJ des sujets existants + print 'SUbject Updating' + UPDATE S + SET Name = D.PropValue + FROM [Subjects] S + JOIN TMessageReceived M ON S.Original_Id = m.ObjectOriginalID and m.isMessageComplete = 1 + JOIN TMessageReceivedDetail D ON M.pk_MessageReceived=D.fk_MessageReceived and D.PropName = 'StationName' + WHERE M.ImportDate IS NULL and M.isMessageComplete = 1 + + + print 'Inserting DYnPropValues' + -- MAJ des propriétées dynamiques on prends la valeur récupérée et on conmpare avec les valeurs existantes, on insére si valeurs existante + INSERT INTO [SubjectDynPropValues] + ([StartDate] + ,[Parameter] + ,[ValueInt] + ,[ValueString] + ,[ValueDate] + ,[ValueFloat] + ,[Subject_ID] + ,[SubjectDynProp_ID]) + + SELECT DISTINCT GETDATE(),NULL + ,CASE WHEN DP.TypeProp = 'entier' THEN CONVERT(int,D.PropValue) ELSE NULL END + ,CASE WHEN DP.TypeProp = 'string' THEN D.PropValue ELSE NULL END + ,CASE WHEN DP.TypeProp = 'date' THEN CONVERT(DATETIME,D.PropValue,120) ELSE NULL END + ,CASE WHEN DP.TypeProp = 'float' THEN CONVERT(float,D.PropValue) ELSE NULL END + ,S.ID + ,DP.ID + FROM TMessageReceived M + JOIN subjects S on S.Original_Id = m.ObjectOriginalID + JOIN TMessageReceivedDetail D ON M.pk_MessageReceived=D.fk_MessageReceived + JOIN TMessageDynPropvsERD CDP ON CDP.ERDName = D.PropName + JOIN SubjectDynProps DP ON DP.Name = CDP.EColName + LEFT JOIN SubjectDynPropValuesNow V ON v.Subject_ID = S.ID and V.name = DP.Name + WHERE Importdate IS NULL AND M.ObjectType ='Station' AND m.Provenance like 'EcoReleve%' + AND (CASE WHEN DP.TypeProp = 'entier' THEN + CASE WHEN V.ValueInt = CONVERT(int,D.PropValue) OR (V.ValueInt IS NULL AND D.PropValue IS NULL) THEN 0 + ELSE 1 END + ELSE 1 END) = 1 + AND (CASE WHEN DP.TypeProp = 'string' THEN CASE WHEN V.ValueString = D.PropValue OR (V.ValueString IS NULL AND D.PropValue IS NULL) THEN 0 + ELSE 1 END + ELSE 1 END ) = 1 + AND (CASE WHEN DP.TypeProp = 'date' THEN CASE WHEN V.ValueDate = CONVERT(DATETIME,D.PropValue,120) OR (V.ValueDate IS NULL AND D.PropValue IS NULL) THEN 0 + ELSE 1 END + ELSE 1 END) = 1 + AND (CASE WHEN DP.TypeProp = 'float' THEN CASE WHEN V.ValueFloat = CONVERT(float,D.PropValue) OR (V.ValueFloat IS NULL AND D.PropValue IS NULL) THEN 0 + ELSE 1 END + ELSE 1 END) = 1 + and m.isMessageComplete = 1 + + + UPDATE TMessageReceived + SET ImportDate=GETDATE() + WHERE ObjectType ='Station' + AND ImportDate IS NULL AND Provenance like 'EcoReleve%' and isMessageComplete = 1 + + IF @@TRANCOUNT >0 BEGIN + COMMIT TRAN + BEGIN TRAN + END + + ------------------------- GESTION DES SAMPLES ------------------------- + + -- CREATION DES SAMPLES AVEC ID + + --SET IDENTITY_INSERT [Samples] ON + + + INSERT INTO [Samples] + (--ID, + [Original_Id] + ,[Status] + ,[Name] + ,[Tampon] + ,[CreationDate] + ,[SampleDate] + ,[Container] + ,[Holder] + ,[Position] + ,[Comment] + ,[ParentSample_ID] + ,[Subject_ID] + ,[TypeObj_ID] + ,Qte) + + SELECT DISTINCT /*convert(bigint,DID.PropValue) ID, */m.Provenance + '_' + convert(varchar,m.ObjectId),2,'','',getdate(),SV.ValueDate,NULL,NULL,NULL,DCo.PropValue,NULL,S.ID,ST.ID,convert(INT,DNb.PropValue) + FROM TMessageReceived M + JOIN TMessageReceivedDetail DID ON M.pk_MessageReceived=DID.fk_MessageReceived and DID.PropName = 'ECol_id' and DID.Provenance = m.Provenance and DID.PropValue IS NOT NULL + JOIN TMessageReceivedDetail DSta ON M.pk_MessageReceived=DSta.fk_MessageReceived and DSta.PropName = 'FK_Station' and DSta.Provenance = m.Provenance + JOIN TMessageReceivedDetail DCo ON M.pk_MessageReceived=DCo.fk_MessageReceived and DCo.PropName = 'Comments' and DCo.Provenance = m.Provenance + JOIN TMessageReceivedDetail DNb ON M.pk_MessageReceived=DNb.fk_MessageReceived and DNb.PropName = 'number' and DNb.Provenance = m.Provenance + JOIN Subjects S ON s.Original_Id = M.Provenance + '_' + DSta.PropValue + JOIN SampleTypes ST ON ST.Name = CASE WHEN convert(INT,DNb.PropValue) >1 THEN 'Group' ELSE 'Individual' END + LEFT JOIN SubjectDynPropValuesNow SV ON SV.Subject_ID = s.id and SV.Name ='StationDate' + WHERE M.ObjectType ='Entomo_Pop_Census' AND m.Provenance like 'EcoReleve_%' + AND Importdate IS NULL + AND NOT EXISTS (SELECT * FROM Samples ES WHERE ES.original_ID = m.Provenance + '_' + convert(varchar,m.ObjectId)) + + + --SET IDENTITY_INSERT [Samples] OFF + + + -- CREATION DES SAMPLES SANS ID + + INSERT INTO [Samples] + ([Original_Id] + ,[Status] + ,[Name] + ,[Tampon] + ,[CreationDate] + ,[SampleDate] + ,[Container] + ,[Holder] + ,[Position] + ,[Comment] + ,[ParentSample_ID] + ,[Subject_ID] + ,[TypeObj_ID] + ,Qte) + SELECT DISTINCT m.Provenance + '_' + convert(varchar,m.ObjectId),2,'','',getdate(),SV.ValueDate,NULL,NULL,NULL,DCo.PropValue,NULL,S.ID,ST.ID,convert(INT,DNb.PropValue) + FROM TMessageReceived M + JOIN TMessageReceivedDetail DSta ON M.pk_MessageReceived=DSta.fk_MessageReceived and DSta.PropName = 'FK_Station' and DSta.Provenance = m.Provenance + JOIN TMessageReceivedDetail DCo ON M.pk_MessageReceived=DCo.fk_MessageReceived and DCo.PropName = 'Comments' and DCo.Provenance = m.Provenance + JOIN TMessageReceivedDetail DNb ON M.pk_MessageReceived=DNb.fk_MessageReceived and DNb.PropName = 'number' and DNb.Provenance = m.Provenance --and DNb.PropValue is not null + JOIN Subjects S ON s.Original_Id = M.Provenance + '_' + DSta.PropValue + JOIN SampleTypes ST ON ST.Name = CASE WHEN convert(INT,DNb.PropValue) >1 THEN 'Group' ELSE 'Individual' END + LEFT JOIN SubjectDynPropValuesNow SV ON SV.Subject_ID = s.id and SV.Name ='StationDate' + WHERE M.ObjectType ='Entomo_Pop_Census' AND m.Provenance like 'EcoReleve_%' + AND Importdate IS NULL + AND NOT EXISTS (SELECT * FROM Samples ES WHERE ES.original_ID = m.Provenance + '_' + convert(varchar,m.ObjectId)) + + + + ---- Get originalspecies + --originalSpecies + INSERT INTO [SampleDynPropValues] + ([StartDate] + ,[Parameter] + ,[ValueInt] + ,[ValueString] + ,[ValueDate] + ,[ValueFloat] + ,[Sample_ID] + ,[SampleDynProp_ID]) + SELECT DISTINCT GETDATE(),NULL,NULL,D.PropValue,NULL,NULL,S.ID,DP.ID + FROM TMessageReceived M + JOIN Samples S on S.Original_Id = m.Provenance + '_' + convert(varchar,m.ObjectId) + JOIN TMessageReceivedDetail D ON M.pk_MessageReceived=D.fk_MessageReceived and d.Provenance = m.Provenance + JOIN SampleDynProps DP ON DP.Name in ('determinatedSpecies') + WHERE Importdate IS NULL AND M.ObjectType ='Entomo_Pop_Census' AND m.Provenance like 'EcoReleve_%' + and d.PropName = 'taxon' + AND m.isMessageComplete = 1 + order by s.ID,DP.ID + + INSERT INTO [SampleDynPropValues] + ([StartDate] + ,[Parameter] + ,[ValueInt] + ,[ValueString] + ,[ValueDate] + ,[ValueFloat] + ,[Sample_ID] + ,[SampleDynProp_ID]) + SELECT GETDATE(),NULL,NULL,D.PropValue,NULL,NULL,S.ID,Case WHEN DP.ID is NULL THEN (SELECT ID FROM SampleDynProps WHERE Name = 'Age') ELSE DP.ID END + FROM TMessageReceived M + JOIN Samples S on S.Original_Id = m.Provenance + '_' + convert(varchar,m.ObjectId) + JOIN TMessageReceivedDetail D ON M.pk_MessageReceived=D.fk_MessageReceived and d.Provenance = m.Provenance + LEFT JOIN SampleDynProps DP ON dp.name = D.PropName + WHERE --Importdate IS NULL AND + M.ObjectType ='Entomo_Pop_Census' AND m.Provenance like 'EcoReleve_%' + and d.PropName in ('Stade','Sex') + AND m.isMessageComplete = 1 + order by s.ID,DP.ID + + -- TODO Créer un Event + IF OBJECT_ID('tempdb..#EventCreated') IS NOT NULL + DROP TABLE #EventCreated + + CREATE TABLE #EventCreated + ( + Event_ID BIGINT + ,Sample_ID BIGINT + ) + + + INSERT INTO [dbo].[EcolEvents] + ([Status] + ,[Name] + ,[EventDate] + ,[InputOperator] + ,[User] + ,[Comment] + ,[Sample_ID] + ,[TypeObj_ID] + ,[SamplePositionHisto_ID]) + OUTPUT inserted.ID,inserted.sample_id into #EventCreated + select 2,'',GETDATE(),CASE WHEN D.PropValue IS NULL THEN '' ELSE D.PropValue END,NULL,NULL,S.id,T.ID ,NULL + FROM TMessageReceived M + JOIN Samples S on S.Original_Id =m.Provenance + '_' + convert(varchar,m.ObjectId) + JOIN EcolEventtypes T ON T.Name ='Imported' + JOIN TMessageReceivedDetail D ON D.fk_MessageReceived = m.pk_MessageReceived and D.PropName='Identified_by' + + INSERT INTO [dbo].[EcolEventDynPropValues] + ([StartDate] + ,[Parameter] + ,[ValueInt] + ,[ValueString] + ,[ValueDate] + ,[ValueFloat] + ,[EcolEvent_ID] + ,[EcolEventDynProp_ID]) + + select getdate(),null,null,D.PropValue,NULL,NULL,C.Event_ID, DP.ID + from #EventCreated C JOIN Samples S on C.Sample_ID = S.ID + JOIN TMessageReceived M ON S.Original_Id = m.Provenance + '_' + convert(varchar,m.ObjectId) + JOIN TMessageReceivedDetail D ON D.fk_MessageReceived = m.pk_MessageReceived and D.PropName='taxon' + JOIN EcolEventDynProps DP ON DP.Name in ('species') + + --UPDATE S + --SET Original_Id = m.ObjectOriginalID + --FROM Samples S JOIN TMessageReceived M ON S.Original_Id = CONVERT(varchar,m.ObjectID) + + + + UPDATE TMessageReceived SET ImportDate=GETDATE() + WHERE (ObjectType ='Entomo_Pop_Census' or ObjectType ='Station') AND Provenance like 'EcoReleve_%' + + IF @@TRANCOUNT >0 COMMIT TRAN; + + END TRY + BEGIN CATCH + print 'CATCH' + print @@TRANCOUNT + IF @@TRANCOUNT >0 ROLLBACK TRAN; + print @@TRANCOUNT + + DECLARE @ErrorMessage NVARCHAR(4000); + DECLARE @ErrorSeverity INT; + DECLARE @ErrorState INT; + + SELECT + @ErrorMessage = ERROR_MESSAGE(), + @ErrorSeverity = ERROR_SEVERITY(), + @ErrorState = ERROR_STATE(); + + RAISERROR (@ErrorMessage, -- Message text. + @ErrorSeverity, -- Severity. + @ErrorState -- State. + ); + END CATCH +END +GO + + + + + +INSERT INTO [dbo].[TVersion] (TVer_FileName,TVer_Date,TVer_DbName) VALUES ('93_SP_IN_eColl_ImportMessageFromERD',GETDATE(),(SELECT db_name())) + + +GO diff --git a/Back/database/Pipe/DB_Mother/094_Add_Disable_Rule_EntomoPopCensus_inForm.txt b/Back/database/Pipe/DB_Mother/094_Add_Disable_Rule_EntomoPopCensus_inForm.txt new file mode 100644 index 000000000..0993fd039 --- /dev/null +++ b/Back/database/Pipe/DB_Mother/094_Add_Disable_Rule_EntomoPopCensus_inForm.txt @@ -0,0 +1,11 @@ +update f SET Rules = '{"operator":"disable","target":"'+Name+'","source":"sampled","value":1}' +FROM ModuleForms f +where typeobj = (SELECT ID FROM ProtocoleType WHERE Name = 'Entomo_pop_Census') + +GO + + +INSERT INTO [dbo].[TVersion] (TVer_FileName,TVer_Date,TVer_DbName) VALUES ('94_Add_Disable_Rule_EntomoPopCensus_inForm',GETDATE(),(SELECT db_name())) + + +GO diff --git a/Back/ecoreleve_server/GenericObjets/FrontModules.py b/Back/ecoreleve_server/GenericObjets/FrontModules.py index dbaea9217..2d012b09f 100644 --- a/Back/ecoreleve_server/GenericObjets/FrontModules.py +++ b/Back/ecoreleve_server/GenericObjets/FrontModules.py @@ -184,10 +184,25 @@ def InputLNM(self) : subschema.update(gridRanged) else : subschema[conf.Name] = conf.GetDTOFromConf(self.Editable) - + + subschema['ID'] = { + 'name': 'ID', + 'type': 'Number', + 'title' : 'ID', + 'editable' : True, + 'editorClass' : 'form-control' , + 'validators': [], + 'options': None, + 'fieldClass':'hide col-md-1', + 'defaultValue' : None, + 'editorAttrs' : {'disabled': True}, + 'fullPath':False, + 'size':3 + } fields = [] resultat = [] Legends = sorted ([(obj.Legend,obj.FormOrder,obj.Name) for obj in result if obj.FormOrder is not None and obj.InputType != 'GridRanged'], key = lambda x : x[1]) + Legends.append((None,0,'ID')) # Legend2s = sorted ([(obj.Legend)for obj in result if obj.FormOrder is not None ], key = lambda x : x[1]) withOutLegends = sorted ([(obj.Legend,obj.FormOrder,obj.Name)for obj in result if obj.FormOrder is not None and obj.Legend is None and obj.InputType != 'GridRanged'], key = lambda x : x[1]) diff --git a/Back/ecoreleve_server/Models/Protocoles.py b/Back/ecoreleve_server/Models/Protocoles.py index 320558568..9623c110a 100644 --- a/Back/ecoreleve_server/Models/Protocoles.py +++ b/Back/ecoreleve_server/Models/Protocoles.py @@ -103,7 +103,7 @@ def Observation_childrens(self,listOfSubProtocols): subDictList.append(subDict) curData['listOfSubObs'] = subDictList - if 'ID' in curData : + if 'ID' in curData and curData['ID'] is not None: subObs = list(filter(lambda x : x.ID==curData['ID'],self.Observation_children))[0] subObs.LoadNowValues() else : diff --git a/Front/app/ns_modules/ns_bbfe/bbfe-gridForm.js b/Front/app/ns_modules/ns_bbfe/bbfe-gridForm.js index 2d169e7b9..8df9bacb2 100644 --- a/Front/app/ns_modules/ns_bbfe/bbfe-gridForm.js +++ b/Front/app/ns_modules/ns_bbfe/bbfe-gridForm.js @@ -3,9 +3,10 @@ define([ 'underscore', 'backbone', 'marionette', + 'ns_ruler/ruler', 'backbone-forms', - ], function ($, _, Backbone, Marionette, Form, List, tpl) { + ], function ($, _, Backbone, Marionette,Ruler, Form, List, tpl) { 'use strict'; return Form.editors.GridFormEditor = Form.editors.Base.extend({ @@ -106,7 +107,8 @@ define([ form.$el.find('textarea').on("change", function(e) { window.formChange = true; }); - + + this.initRules(form); this.forms.push(form); if(!this.defaultRequired){ @@ -345,9 +347,39 @@ define([ }; return values; } + }, + initRules:function(form) { + var _this = this; + this.ruler = new Ruler({ + form: form + }); + var globalError = {}; + var errorMsg = 'error on field(s): \n'; + + _.each(form.schema,function(curSchema){ + if (curSchema.rule){ + var curRule = curSchema.rule; + var target = curSchema.name; + var curResult = _this.ruler.addRule(target,curRule.operator,curRule.source,curRule.value); + if (curResult) { + globalError[target] = curResult; + errorMsg += curResult.object + ': '+curResult.message+'\n' ; + } + } + }); + + if (!$.isEmptyObject(globalError) && this.displayMode == 'edit'){ + this.swal({ + title : 'Rule error', + text : errorMsg, + type:'error', + showCancelButton: false, + confirmButtonColor:'#DD6B55', + confirmButtonText:'Ok'}); + } + }, - }, }, { //STATICS template: _.template('\ diff --git a/Front/app/ns_modules/ns_form/NsFormsModuleGit.js b/Front/app/ns_modules/ns_form/NsFormsModuleGit.js index b7249df38..add62b360 100644 --- a/Front/app/ns_modules/ns_form/NsFormsModuleGit.js +++ b/Front/app/ns_modules/ns_form/NsFormsModuleGit.js @@ -260,7 +260,7 @@ define([ if (curSchema.rule){ var curRule = curSchema.rule; var target = curSchema.name; - var curResult = _this.ruler.addRule(target,curRule.operator,curRule.source); + var curResult = _this.ruler.addRule(target,curRule.operator,curRule.source,curRule.value); if (curResult) { globalError[target] = curResult; errorMsg += curResult.object + ': '+curResult.message+'\n' ; diff --git a/Front/app/ns_modules/ns_ruler/ruler.js b/Front/app/ns_modules/ns_ruler/ruler.js index 294fe44c3..fe021583e 100644 --- a/Front/app/ns_modules/ns_ruler/ruler.js +++ b/Front/app/ns_modules/ns_ruler/ruler.js @@ -63,11 +63,12 @@ 'sum': this.operationListRule, 'minus': this.operationListRule, 'times': this.operationListRule, + 'disable': this.disable, 'context': this }; }, - addRule: function (target, operator, source) { + addRule: function (target, operator, source,value) { var _this = this; var realSource = source ; this.sourceFields = {}; @@ -80,17 +81,22 @@ try { _.each(source,function(curSource){ if (_this.sourceFields[curSource] == null) { - _this.sourceFields[curSource] = [{source:source, target: target, operator: operator }]; + _this.sourceFields[curSource] = [{source:source, target: target, operator: operator, value:value}]; } else { - _this.sourceFields[curSource].push({source:source, target: target, operator: operator }); + _this.sourceFields[curSource].push({source:source, target: target, operator: operator, value:value}); + } + + if (operator == 'disable'){ + _this.ApplyRules(null,{source:curSource,value:value}); + } + else { + _this.form.$el.find(('#' + _this.getEditor(curSource).id)).on('change keyup paste', function (evt) { + _this.ApplyRules(evt,null); + }); } - _this.form.$el.find(('#' + _this.getEditor(curSource).id)).on('change keyup paste', function (evt) { - _this.ApplyRules(evt); - }); }); return null; } catch (e) { - console.log('totot',e); return {message: 'invalid configuration', error:e, object: target}; } }, @@ -99,12 +105,17 @@ return this.form.fields[name].editor; }, - ApplyRules: function (evt) { - var sourceName = $(evt.currentTarget).attr('name'); + ApplyRules: function (evt,ruleVal) { + if (evt) { + var sourceName = $(evt.currentTarget).attr('name'); + } else { + var sourceName = ruleVal.source; + } var ruleList = this.sourceFields[sourceName]; for (var i = 0; i < ruleList.length; i++) { var targetName = ruleList[i].target; var operator = ruleList[i].operator; + this.dictRule[operator](ruleList[i]); //this.form.$el.find(('#' + this.getEditor(ruleList[i].target).id)).val(this.getEditor(sourceName).getValue()); } @@ -153,10 +164,15 @@ _this.form.$el.find('#' + _this.getEditor(curRule.target).id).val(result); return result; }, + disable: function(curRule){ + var _this = this.context; + var result = _this.getEditor(curRule.source).getValue(); + if (result==curRule.value) { + _this.form.$el.find('#' + _this.getEditor(curRule.target).id).prop("disabled", true); + } + } }); - - return (NsRuler); })); \ No newline at end of file