Skip to content

Commit e6b3598

Browse files
committed
Improve behavior of MCC MarkShippedWindow
1 parent 95c14a3 commit e6b3598

File tree

1 file changed

+214
-129
lines changed

1 file changed

+214
-129
lines changed

mcc/resources/web/mcc/window/MarkShippedWindow.js

Lines changed: 214 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ Ext4.define('MCC.window.MarkShippedWindow', {
208208
return;
209209
}
210210

211-
var targetFolderId = win.down('#targetFolder').store.findRecord('Path', targetFolder).get('EntityId');
212211
Ext4.Msg.wait('Saving...');
213212
LABKEY.Query.selectRows({
214213
schemaName: 'study',
@@ -224,143 +223,229 @@ Ext4.define('MCC.window.MarkShippedWindow', {
224223
return false;
225224
}
226225

227-
var commands = [];
228-
Ext4.Array.forEach(results.rows, function(row){
229-
var effectiveId = win.down('#usePreviousId-' + row.Id).getValue() ? row.Id : win.down('#newId-' + row.Id).getValue();
230-
var requestId = win.down('#requestId-' + row.Id).getValue();
231-
// This should be checked above, although perhaps case sensitivity could get involved:
232-
LDK.Assert.assertNotEmpty('Missing effective ID after query', effectiveId);
233-
234-
var shouldAddDeparture = !row['Id/MostRecentDeparture/MostRecentDeparture'] ||
235-
row['Id/MostRecentDeparture/MostRecentDeparture'] !== Ext4.Date.format(row.effectiveDate, 'Y-m-d') ||
236-
row['Id/MostRecentDeparture/mccRequestId'] !== requestId ||
237-
row.Id !== effectiveId;
238-
if (shouldAddDeparture) {
239-
commands.push({
240-
command: 'insert',
241-
schemaName: 'study',
242-
queryName: 'Departure',
243-
rows: [{
244-
Id: row.Id,
245-
date: effectiveDate,
246-
source: row.colony,
247-
destination: centerName,
248-
mccRequestId: requestId,
249-
description: row.colony ? 'Original center: ' + row.colony : null,
250-
qcstate: null,
251-
objectId: null,
252-
QCStateLabel: 'Completed'
253-
}]
254-
});
255-
}
226+
var uniqueIds = [];
227+
Ext4.Array.forEach(results.rows, function(row) {
228+
uniqueIds.push(win.down('#usePreviousId-' + row.Id).getValue() ? row.Id : win.down('#newId-' + row.Id).getValue());
229+
}, this);
256230

257-
// If going to a new LK folder, we're creating a whole new record:
258-
if (targetFolderId.toUpperCase() !== LABKEY.Security.currentContainer.id.toUpperCase() || effectiveId !== row.Id) {
259-
commands.push({
260-
command: 'insert',
261-
containerPath: targetFolder,
262-
schemaName: 'study',
263-
queryName: 'Demographics',
264-
rows: [{
265-
Id: effectiveId,
266-
date: effectiveDate,
267-
alternateIds: row.Id !== effectiveId ? row.Id : null,
268-
gender: row.gender,
269-
species: row.species,
270-
birth: row.birth,
271-
death: row.death,
272-
dam: row.dam,
273-
sire: row.sire,
274-
damMccAlias: row['damMccAlias/externalAlias'],
275-
sireMccAlias: row['sireMccAlias/externalAlias'],
276-
colony: centerName,
277-
source: row.colony,
278-
calculated_status: 'Alive',
279-
mccAlias: row['Id/mccAlias/externalAlias'],
280-
QCState: null,
281-
QCStateLabel: 'Completed',
282-
objectId: null
283-
}]
284-
});
285-
286-
commands.push({
287-
command: 'update',
288-
containerPath: null, //Use current folder
289-
schemaName: 'study',
290-
queryName: 'Demographics',
291-
rows: [{
292-
Id: row.Id, // NOTE: always change the original record
293-
excludeFromCensus: true
294-
}]
295-
});
296-
}
297-
else {
298-
// Otherwise update the existing:
299-
commands.push({
300-
command: 'update',
301-
containerPath: targetFolder,
302-
schemaName: 'study',
303-
queryName: 'Demographics',
304-
rows: [{
305-
Id: row.Id,
306-
date: effectiveDate,
307-
alternateIds: null,
308-
gender: row.gender,
309-
species: row.species,
310-
birth: row.birth,
311-
death: row.death,
312-
dam: row.dam,
313-
sire: row.sire,
314-
colony: centerName,
315-
source: row.colony,
316-
calculated_status: 'Alive',
317-
QCState: null,
318-
QCStateLabel: 'Completed',
319-
objectId: null
320-
}]
321-
});
231+
LABKEY.Query.SelectRows({
232+
schemaName: 'study',
233+
queryName: 'Demographics',
234+
containerPath: targetFolder,
235+
filterArray: [LABKEY.Filter.create('Id', uniqueIds.join(';'), LABKEY.Filter.Types.IN)],
236+
columns: 'Id,gender,species,birth,death,dam,sire,damMccAlias/externalAlias,sireMccAlias/externalAlias,calculated_status,Id/mccAlias/externalAlias,colony,source,lsid,objectid',
237+
scope: this,
238+
failure: LDK.Utils.getErrorCallback(),
239+
success: function(existingIdResults) {
240+
var preexistingIdsInTargetFolder = {};
241+
Ext4.Array.forEach(existingIdResults.rows, function(r){
242+
preexistingIdsInTargetFolder[r.Id] = r;
243+
}, this);
244+
245+
this.doSave(win, results, preexistingIdsInTargetFolder);
322246
}
247+
});
248+
}
249+
});
250+
},
251+
252+
doSave: function(win, results, preexistingIdsInTargetFolder){
253+
var effectiveDate = win.down('#effectiveDate').getValue();
254+
var centerName = win.down('#centerName').getValue();
255+
var targetFolder = win.down('#targetFolder').getValue();
256+
var targetFolderId = win.down('#targetFolder').store.findRecord('Path', targetFolder).get('EntityId');
257+
258+
var commands = [];
259+
var hadError = false;
260+
Ext4.Array.forEach(results.rows, function(row){
261+
var effectiveId = win.down('#usePreviousId-' + row.Id).getValue() ? row.Id : win.down('#newId-' + row.Id).getValue();
262+
var requestId = win.down('#requestId-' + row.Id).getValue();
263+
// This should be checked above, although perhaps case sensitivity could get involved:
264+
LDK.Assert.assertNotEmpty('Missing effective ID after query', effectiveId);
265+
266+
var shouldAddDeparture = !row['Id/MostRecentDeparture/MostRecentDeparture'] ||
267+
row['Id/MostRecentDeparture/MostRecentDeparture'] !== Ext4.Date.format(row.effectiveDate, 'Y-m-d') ||
268+
row['Id/MostRecentDeparture/mccRequestId'] !== requestId ||
269+
row.Id !== effectiveId;
270+
if (shouldAddDeparture) {
271+
commands.push({
272+
command: 'insert',
273+
schemaName: 'study',
274+
queryName: 'Departure',
275+
rows: [{
276+
Id: row.Id,
277+
date: effectiveDate,
278+
source: row.colony,
279+
destination: centerName,
280+
mccRequestId: requestId,
281+
description: row.colony ? 'Original center: ' + row.colony : null,
282+
qcstate: null,
283+
objectId: null,
284+
QCStateLabel: 'Completed'
285+
}]
286+
});
287+
}
323288

324-
var shouldAddArrival = !row['Id/MostRecentArrival/MostRecentArrival'] ||
325-
row['Id/MostRecentArrival/MostRecentArrival'] !== Ext4.Date.format(row.effectiveDate, 'Y-m-d') ||
326-
row['Id/MostRecentArrival/mccRequestId'] !== requestId ||
327-
row.Id !== effectiveId;
328-
if (shouldAddArrival) {
329-
// And also add an arrival record. NOTE: set the date after the departure to get status to update properly
330-
var arrivalDate = new Date(effectiveDate).setMinutes(effectiveDate.getMinutes() + 1);
331-
commands.push({
332-
command: 'insert',
333-
containerPath: targetFolder,
334-
schemaName: 'study',
335-
queryName: 'Arrival',
336-
rows: [{
337-
Id: effectiveId,
338-
date: arrivalDate,
339-
source: centerName,
340-
mccRequestId: requestId,
341-
QCState: null,
342-
QCStateLabel: 'Completed',
343-
objectId: null
344-
}]
345-
});
289+
// If going to a new LK folder, we're creating a whole new record:
290+
if (targetFolderId.toUpperCase() !== LABKEY.Security.currentContainer.id.toUpperCase() || effectiveId !== row.Id) {
291+
if (Ext4.Object.getKeys(preexistingIdsInTargetFolder).indexOf(effectiveId) === -1) {
292+
// No existing record for this ID, make new record:
293+
commands.push({
294+
command: 'insert',
295+
containerPath: targetFolder,
296+
schemaName: 'study',
297+
queryName: 'Demographics',
298+
rows: [{
299+
Id: effectiveId,
300+
date: effectiveDate,
301+
alternateIds: row.Id !== effectiveId ? row.Id : null,
302+
gender: row.gender,
303+
species: row.species,
304+
birth: row.birth,
305+
death: row.death,
306+
dam: row.dam,
307+
sire: row.sire,
308+
damMccAlias: row['damMccAlias/externalAlias'],
309+
sireMccAlias: row['sireMccAlias/externalAlias'],
310+
colony: centerName,
311+
source: row.colony,
312+
calculated_status: 'Alive',
313+
mccAlias: row['Id/mccAlias/externalAlias'],
314+
QCState: null,
315+
QCStateLabel: 'Completed',
316+
objectId: null
317+
}]
318+
});
319+
}
320+
else {
321+
// There is an existing record for this ID, so merge/validate:
322+
console.log('Existing record found for: ' + effectiveId)
323+
var toUpdate = preexistingIdsInTargetFolder[effectiveId]
324+
325+
var errors = []
326+
Ext4.Array.forEach(['gender', 'species', 'birth', 'death', 'dam', 'sire'], function(fieldName) {
327+
this.doFieldCheck(row, fieldName, toUpdate, fieldName, errors, effectiveId)
328+
}, this);
329+
330+
toUpdate.colony = centerName
331+
toUpdate.source = toUpdate.source || row.colony
332+
toUpdate.calculated_status = toUpdate.calculated_status || 'Alive';
333+
334+
if (row.Id !== effectiveId) {
335+
toUpdate.alternateIds = toUpdate.alternateIds ? toUpdate.alternateIds + ',' + row.Id : row.Id;
346336
}
347-
}, this);
348337

349-
LABKEY.Query.saveRows({
350-
commands: commands,
351-
scope: this,
352-
failure: LDK.Utils.getErrorCallback(),
353-
success: function() {
354-
Ext4.Msg.hide();
355-
Ext4.Msg.alert('Success', 'Transfer Added', function () {
356-
var dataRegion = LABKEY.DataRegions[this.dataRegionName];
357-
this.destroy();
338+
this.doFieldCheck(row, 'damMccAlias/externalAlias', toUpdate, 'damMccAlias', errors, effectiveId)
339+
this.doFieldCheck(row, 'sireMccAlias/externalAlias', toUpdate, 'sireMccAlias', errors, effectiveId)
340+
this.doFieldCheck(row, 'Id/mccAlias/externalAlias', toUpdate, 'mccAlias', errors, effectiveId)
358341

359-
dataRegion.refresh();
360-
}, this);
342+
if (errors.length) {
343+
Ext4.Msg.hide();
344+
Ext4.Msg.alert('Error', 'Inconsistent data between source and destination demographics for: ' + effectiveId + + '<br>' + errors.join('<br>'));
345+
hadError = true;
346+
return false;
361347
}
348+
349+
commands.push({
350+
command: 'update',
351+
containerPath: targetFolder,
352+
schemaName: 'study',
353+
queryName: 'Demographics',
354+
rows: [toUpdate]
355+
});
356+
}
357+
358+
commands.push({
359+
command: 'update',
360+
containerPath: null, //Use current folder
361+
schemaName: 'study',
362+
queryName: 'Demographics',
363+
rows: [{
364+
Id: row.Id, // NOTE: always change the original record
365+
excludeFromCensus: true
366+
}]
362367
});
363368
}
369+
else {
370+
// Otherwise update the existing:
371+
commands.push({
372+
command: 'update',
373+
containerPath: targetFolder,
374+
schemaName: 'study',
375+
queryName: 'Demographics',
376+
rows: [{
377+
Id: row.Id,
378+
date: effectiveDate,
379+
alternateIds: null,
380+
gender: row.gender,
381+
species: row.species,
382+
birth: row.birth,
383+
death: row.death,
384+
dam: row.dam,
385+
sire: row.sire,
386+
colony: centerName,
387+
source: row.colony,
388+
calculated_status: 'Alive',
389+
QCState: null,
390+
QCStateLabel: 'Completed',
391+
objectId: null
392+
}]
393+
});
394+
}
395+
396+
var shouldAddArrival = !row['Id/MostRecentArrival/MostRecentArrival'] ||
397+
row['Id/MostRecentArrival/MostRecentArrival'] !== Ext4.Date.format(row.effectiveDate, 'Y-m-d') ||
398+
row['Id/MostRecentArrival/mccRequestId'] !== requestId ||
399+
row.Id !== effectiveId;
400+
if (shouldAddArrival) {
401+
// And also add an arrival record. NOTE: set the date after the departure to get status to update properly
402+
var arrivalDate = new Date(effectiveDate).setMinutes(effectiveDate.getMinutes() + 1);
403+
commands.push({
404+
command: 'insert',
405+
containerPath: targetFolder,
406+
schemaName: 'study',
407+
queryName: 'Arrival',
408+
rows: [{
409+
Id: effectiveId,
410+
date: arrivalDate,
411+
source: centerName,
412+
mccRequestId: requestId,
413+
QCState: null,
414+
QCStateLabel: 'Completed',
415+
objectId: null
416+
}]
417+
});
418+
}
419+
}, this);
420+
421+
if (hadError) {
422+
return;
423+
}
424+
425+
LABKEY.Query.saveRows({
426+
commands: commands,
427+
scope: this,
428+
failure: LDK.Utils.getErrorCallback(),
429+
success: function() {
430+
Ext4.Msg.hide();
431+
Ext4.Msg.alert('Success', 'Transfer Added', function () {
432+
var dataRegion = LABKEY.DataRegions[this.dataRegionName];
433+
this.destroy();
434+
435+
dataRegion.refresh();
436+
}, this);
437+
}
364438
});
439+
},
440+
441+
doFieldCheck: function(row, fieldName1, toUpdate, fieldName2, errors, effectiveId) {
442+
if (row[fieldName1]) {
443+
if (toUpdate[fieldName2] && toUpdate[fieldName2] !== row[fieldName1]) {
444+
errors.push('Pre-existing record for ' + effectiveId + ', but ' + fieldName2 + ' was inconsistent between old/new (' + toUpdate[fieldName2] + '/' + row[fieldName1] + ')')
445+
}
446+
else {
447+
toUpdate[fieldName2] = row[fieldName1];
448+
}
449+
}
365450
}
366451
});

0 commit comments

Comments
 (0)