Skip to content

Commit

Permalink
feat: instance model, and store instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed Aug 1, 2023
1 parent d16c095 commit 19dce9e
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 4 deletions.
10 changes: 6 additions & 4 deletions models/sql/dicom-json-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { DicomJsonModel } = require("@models/DICOM/dicom-json-model");
const { PatientPersistentObject } = require("./po/patient.po");
const { StudyPersistentObject } = require("./po/study.po");
const { SeriesPersistentObject } = require("./po/series.po");
const { InstancePersistentObject } = require("./po/instance.po");


class SqlDicomJsonModel extends DicomJsonModel {
Expand All @@ -28,8 +29,8 @@ class SqlDicomJsonModel extends DicomJsonModel {

let storedPatient = await this.storePatientCollection(dicomJsonClone);
let storedStudy = await this.storeStudyCollection(dicomJsonClone, storedPatient);
await this.storeSeriesCollection(dicomJsonClone, storedStudy);
this.storeInstanceCollection(dicomJsonClone);
let storedSeries = await this.storeSeriesCollection(dicomJsonClone, storedStudy);
await this.storeInstanceCollection(dicomJsonClone, storedSeries);
} catch(e) {
throw e;
}
Expand All @@ -53,8 +54,9 @@ class SqlDicomJsonModel extends DicomJsonModel {
return series;
}

async storeInstanceCollection(dicomJson) {
// TODO
async storeInstanceCollection(dicomJson, series) {
let instancePo = new InstancePersistentObject(dicomJson, series);
return await instancePo.createInstance();
}
}

Expand Down
11 changes: 11 additions & 0 deletions models/sql/init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { PersonNameModel } = require("./models/personName.model");
const { PatientModel } = require("./models/patient.model");
const { StudyModel } = require("./models/study.model");
const { SeriesModel } = require("./models/series.model");
const { InstanceModel } = require("./models/instance.mode");

const sequelizeInstance = require("./instance");

async function init() {
Expand All @@ -14,6 +17,14 @@ async function init() {
foreignKey: "x00100020",
targetKey: "x00100020"
});
SeriesModel.belongsTo(StudyModel, {
foreignKey: "x0020000D",
targetKey: "x0020000D"
});
InstanceModel.belongsTo(SeriesModel, {
foreignKey: "x0020000E",
targetKey: "x0020000E"
});

//TODO: 設計完畢後要將 force 刪除
await sequelizeInstance.sync({force: true});
Expand Down
58 changes: 58 additions & 0 deletions models/sql/models/instance.mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { Sequelize, DataTypes, Model } = require("sequelize");
const sequelizeInstance = require("@models/sql/instance");
const { vrTypeMapping } = require("../vrTypeMapping");

class InstanceModel extends Model { };

InstanceModel.init({
"x0020000D": {
type: vrTypeMapping.UI,
allowNull: false
},
"x0020000E": {
type: vrTypeMapping.UI,
allowNull: false
},
"x00080018": {
type: vrTypeMapping.UI,
allowNull: false,
unique: true
},
"x00080016": {
type: vrTypeMapping.UI
},
"x00080023": {
type: vrTypeMapping.DA
},
"x00080033": {
type: vrTypeMapping.TM
},
"x00200013": {
type: vrTypeMapping.IS
},
"x0040A043": { //SQ
type: DataTypes.JSON
},
"x0040A073": {
type: DataTypes.JSON
},
"x0040A491": {
type: vrTypeMapping.CS
},
"x0040A493": {
type: vrTypeMapping.CS
},
"x0040A730": {
type: DataTypes.JSON
},
"json": {
type: DataTypes.JSON
}
}, {
sequelize: sequelizeInstance,
modelName: "Instance",
tableName: "Instance",
freezeTableName: true
});

module.exports.InstanceModel = InstanceModel;
100 changes: 100 additions & 0 deletions models/sql/po/instance.po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const moment = require("moment");
const _ = require("lodash");
const { PersonNameModel } = require("../models/personName.model");
const { InstanceModel } = require("../models/instance.mode");

const { tagsNeedStore } = require("@models/DICOM/dicom-tags-mapping");

const INSTANCE_STORE_TAGS = {
"00080016": true,
"00080018": true,
"00080023": true,
"00080033": true,
"00200013": true,
"0040A043": true,
"0040A073": true,
"0040A491": true,
"0040A493": true,
"0040A730": true,
"00080008": true,
"0040A032": true,
"00081115": true,
"00280008": true,
"00280010": true,
"00280011": true,
"00280100": true,
"0040A370": true,
"0040A375": true,
"0040A504": true,
"0040A525": true,
"00420010": true,
"00420012": true,
"00700080": true,
"00700081": true,
"00700082": true,
"00700083": true,
"00700084": true,
"00080005": true,
"00081190": true,
"00080054": true,
"00080056": true,
...tagsNeedStore.Patient,
...tagsNeedStore.Study,
...tagsNeedStore.Series
};

class InstancePersistentObject {
constructor(dicomJson, series) {
this.json = {};
Object.keys(INSTANCE_STORE_TAGS).forEach(key => {
let value = _.get(dicomJson, key);
value ? _.set(this.json, key, value) : undefined;
});
this.series = series;

this.x0020000D = this.series.x0020000D;
this.x0020000E = this.series.x0020000E;
this.x00080018 = _.get(dicomJson, "00080018.Value.0", undefined);
this.x00080016 = _.get(dicomJson, "00080016.Value.0", undefined);
this.x00080023 = _.get(dicomJson, "00080023.Value.0", undefined);
this.x00080033 = _.get(dicomJson, "00080033.Value.0", undefined);
this.x00200013 = _.get(dicomJson, "00200013.Value.0", undefined);
this.x0040A043 = _.get(dicomJson, "0040A043.Value", undefined);
this.x0040A073 = _.get(dicomJson, "0040A073.Value", undefined);
this.x0040A491 = _.get(dicomJson, "0040A491.Value.0", undefined);
this.x0040A493 = _.get(dicomJson, "0040A493.Value.0", undefined);
this.x0040A730 = _.get(dicomJson, "0040A730.Value", undefined);
}


async createInstance() {
let [instance, created] = await InstanceModel.findOrCreate({
where: {
x0020000D: this.x0020000D,
x0020000E: this.x0020000E,
x00080018: this.x00080018
},
defaults: {
json: this.json,
x0020000D: this.x0020000D,
x0020000E: this.x0020000E,
x00080018: this.x00080018,
x00080016: this.x00080016,
x00080023: this.x00080023,
x00080033: this.x00080033 ? Number(this.x00080033) : undefined,
x00200013: this.x00200013,
x0040A043: this.x0040A043,
x0040A073: this.x0040A073,
x0040A491: this.x0040A491,
x0040A493: this.x0040A493,
x0040A730: this.x0040A730
}
});


return instance;
}

}

module.exports.InstancePersistentObject = InstancePersistentObject;

0 comments on commit 19dce9e

Please sign in to comment.