Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Migrate dicomextractor module settings to database
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed Dec 5, 2014
1 parent 0077c8e commit 411438a
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 231 deletions.
24 changes: 8 additions & 16 deletions modules/dicomextractor/configs/module.ini
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
[global]
; version of the module
version = 1.0.0
; full name
fullname = DICOM Extractor
; description
description = Extract metadata from DICOM files
; category
category = DICOM
; dependencies
dependencies = scheduler,api,thumbnailcreator
; MIDAS Server. Copyright Kitware SAS. Licensed under the Apache License 2.0.

; hachoir-metadata command
dcm2xml = "dcm2xml"
dcmj2pnm = "dcmj2pnm --write-jpeg --min-max-window"
dcmftest = "dcmftest"
dcmdictpath = ""
[global]
fullname = "DICOM Extractor"
description = "Extract metadata from DICOM files"
category = "DICOM"
dependencies = api,scheduler,thumbnailcreator
uuid = "53ef7737-29d8-4873-97f5-d35ffb2c2200"
version = "1.1.0"
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,11 @@
limitations under the License.
=========================================================================*/

/** Configuration form for the dicom extractor */
class Dicomextractor_ConfigForm extends AppForm
{
/** create form */
public function createConfigForm()
{
$form = new Zend_Form();

$form->setAction($this->webroot.'/dicomextractor/config/index')->setMethod('post');

$dcm2xml = new Zend_Form_Element_Text('dcm2xml');
$dcmj2pnm = new Zend_Form_Element_Text('dcmj2pnm');
$dcmftest = new Zend_Form_Element_Text('dcmftest');
$dcmdictpath = new Zend_Form_Element_Text('dcmdictpath');

$submit = new Zend_Form_Element_Submit('submitConfig');
$submit->setLabel('Save configuration');

$form->addElements(array($dcm2xml, $dcmj2pnm, $dcmftest, $dcmdictpath, $submit));

return $form;
}
}
define('DICOMEXTRACTOR_DCM2XML_COMMAND_KEY', 'dcm2xml_command');
define('DICOMEXTRACTOR_DCM2XML_COMMAND_DEFAULT_VALUE', 'dcm2xml');
define('DICOMEXTRACTOR_DCMJ2PNM_COMMAND_KEY', 'dcmj2pnm_command');
define('DICOMEXTRACTOR_DCMJ2PNM_COMMAND_DEFAULT_VALUE', 'dcmj2pnm --write-jpeg --min-max-window');
define('DICOMEXTRACTOR_DCMFTEST_COMMAND_KEY', 'dcmftest_command');
define('DICOMEXTRACTOR_DCMFTEST_COMMAND_DEFAULT_VALUE', 'dcmftest');
define('DICOMEXTRACTOR_DCMDICTPATH_KEY', 'dcmdictpath');
define('DICOMEXTRACTOR_DCMDICTPATH_DEFAULT_VALUE', '');
68 changes: 68 additions & 0 deletions modules/dicomextractor/controllers/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

/** Admin controller for the dicomextractor module. */
class Dicomextractor_AdminController extends Dicomextractor_AppController
{
/** @var array */
public $_models = array('Setting');

/** Index action */
public function indexAction()
{
$this->requireAdminPrivileges();

$this->view->pageTitle = 'DICOM Extractor Module Configuration';
$form = new Dicomextractor_Form_Admin();

if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost();

if ($form->isValid($data)) {
$values = $form->getValues();

foreach ($values as $key => $value) {
if ($value !== null) {
$this->Setting->setConfig($key, $value, $this->moduleName);
}
}
}

$form->populate($data);
} else {
$elements = $form->getElements();

foreach ($elements as $element) {
$name = $element->getName();

if ($name !== 'csrf' && $name !== 'submit') {
$value = $this->Setting->getValueByName($name, $this->moduleName);

if (!is_null($value)) {
$form->setDefault($name, $value);
}
}
}
}

$this->view->form = $form;
session_start();
}
}
70 changes: 0 additions & 70 deletions modules/dicomextractor/controllers/ConfigController.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
/** Extract dicom metadata */
class Dicomextractor_ExtractorComponent extends AppComponent
{
public $moduleName = 'dicomextractor';

/**
* Check whether a given application is configured properly.
*
* @param command the command to test with
* @param appName the human-readable application name
* @appendVersion whether we need the --version flag
* @param appendVersion whether we need the --version flag
* @return an array indicating whether the app is valid or not
*/
public function getApplicationStatus($preparedCommand, $appName, $appendVersion = true)
Expand Down Expand Up @@ -76,8 +78,10 @@ private function _getExecutableArg($commandWithParams)
*/
private function _prependDataDict(&$command)
{
$modulesConfig = Zend_Registry::get('configsModules');
$dictPath = $modulesConfig['dicomextractor']->dcmdictpath;
/** @var SettingModel $settingModel */
$settingModel = MidasLoader::loadModel('Setting');
$dictPath = $settingModel->getValueByName(DICOMEXTRACTOR_DCMDICTPATH_KEY, $this->moduleName);

if ($dictPath != "") {
$command = 'DCMDICTPATH="'.$dictPath.'" '.$command;
}
Expand All @@ -88,18 +92,24 @@ private function _prependDataDict(&$command)
*/
public function isDCMTKWorking()
{
$ret = array();
$modulesConfig = Zend_Registry::get('configsModules');
$dcm2xmlCommand = $modulesConfig['dicomextractor']->dcm2xml;
$dcmftestCommand = $modulesConfig['dicomextractor']->dcmftest;
/** @var SettingModel $settingModel */
$settingModel = MidasLoader::loadModel('Setting');
$dcm2xmlCommand = $settingModel->getValueByName(DICOMEXTRACTOR_DCM2XML_COMMAND_KEY, $this->moduleName);
$dcmftestCommand = $settingModel->getValueByName(DICOMEXTRACTOR_DCMFTEST_COMMAND_KEY, $this->moduleName);

// dcmj2pnmCommand may have some params that will cause it to throw
// an error when no input is given, hence for existence and configuration
// testing just get the command itself, without params
$dcmj2pnmCommand = $this->_getExecutableArg($modulesConfig['dicomextractor']->dcmj2pnm);
$dcmj2pnmCommand = $this->_getExecutableArg($settingModel->getValueByName(DICOMEXTRACTOR_DCMJ2PNM_COMMAND_KEY, $this->moduleName));

$ret = array();
$ret['dcm2xml'] = $this->getApplicationStatus($dcm2xmlCommand, 'dcm2xml');
$ret['dcmftest'] = $this->getApplicationStatus($dcmftestCommand, 'dcmftest', false);
$ret['dcmj2pnm'] = $this->getApplicationStatus($dcmj2pnmCommand, 'dcmj2pnm');
if ($modulesConfig['dicomextractor']->dcmdictpath == "") {

$dataDictVar = $settingModel->getValueByName(DICOMEXTRACTOR_DCMDICTPATH_KEY, $this->moduleName);

if ($dataDictVar == "") {
if (is_readable('/usr/local/share/dcmtk/dicom.dic') || // default on OS X
is_readable('/usr/share/dcmtk/dicom.dic')
) { // default on Ubuntu
Expand All @@ -111,7 +121,6 @@ public function isDCMTKWorking()
);
}
} else {
$dataDictVar = $modulesConfig['dicomextractor']->dcmdictpath;
$dictPaths = explode(":", $dataDictVar);
$errorInDictVar = false;
foreach ($dictPaths as $path) {
Expand Down Expand Up @@ -154,10 +163,12 @@ public function thumbnail($item)
$bitstream = $bitstreams[$numBitstreams / 2];

// Turn the DICOM into a JPEG
$modulesConfig = Zend_Registry::get('configsModules');
$tempDirectory = $utilityComponent->getTempDirectory();
$tmpSlice = $tempDirectory.'/'.$bitstream->getName().'.jpg';
$command = $modulesConfig['dicomextractor']->dcmj2pnm;

/** @var SettingModel $settingModel */
$settingModel = MidasLoader::loadModel('Setting');
$command = $settingModel->getValueByName(DICOMEXTRACTOR_DCMJ2PNM_COMMAND_KEY, $this->moduleName);
$preparedCommand = str_replace("'", '"', $command);
$preparedCommand .= ' "'.$bitstream->getFullPath().'" "'.$tmpSlice.'"';
$this->_prependDataDict($preparedCommand);
Expand All @@ -183,8 +194,10 @@ public function extract($revision)
return;
}
$bitstream = $bitstreams[0];
$modulesConfig = Zend_Registry::get('configsModules');
$command = $modulesConfig['dicomextractor']->dcm2xml;

/** @var SettingModel $settingModel */
$settingModel = MidasLoader::loadModel('Setting');
$command = $settingModel->getValueByName(DICOMEXTRACTOR_DCM2XML_COMMAND_KEY, $this->moduleName);
$preparedCommand = str_replace("'", '"', $command);
$preparedCommand .= ' "'.$bitstream->getFullPath().'"';
$this->_prependDataDict($preparedCommand);
Expand Down
39 changes: 39 additions & 0 deletions modules/dicomextractor/database/InstallScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

require_once BASE_PATH.'/modules/dicomextractor/constant/module.php';

/** Install the dicomextractor module. */
class Dicomextractor_InstallScript extends MIDASModuleInstallScript
{
/** @var string */
public $moduleName = 'dicomextractor';

/** Post database install. */
public function postInstall()
{
/** @var SettingModel $settingModel */
$settingModel = MidasLoader::loadModel('Setting');
$settingModel->setConfig(DICOMEXTRACTOR_DCM2XML_COMMAND_KEY, DICOMEXTRACTOR_DCM2XML_COMMAND_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(DICOMEXTRACTOR_DCMJ2PNM_COMMAND_KEY, DICOMEXTRACTOR_DCMJ2PNM_COMMAND_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(DICOMEXTRACTOR_DCMFTEST_COMMAND_KEY, DICOMEXTRACTOR_DCMFTEST_COMMAND_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(DICOMEXTRACTOR_DCMDICTPATH_KEY, DICOMEXTRACTOR_DCMDICTPATH_DEFAULT_VALUE, $this->moduleName);
}
}
58 changes: 58 additions & 0 deletions modules/dicomextractor/database/upgrade/1.1.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

/** Upgrade the dicomextractor module to version 1.1.0. */
class Dicomextractor_Upgrade_1_1_0 extends MIDASUpgrade
{
/** @var string */
public $moduleName = 'dicomextractor';

/** Post database upgrade. */
public function postUpgrade()
{
/** @var SettingModel $settingModel */
$settingModel = MidasLoader::loadModel('Setting');
$configPath = LOCAL_CONFIGS_PATH.DIRECTORY_SEPARATOR.$this->moduleName.'.local.ini';

if (file_exists($configPath)) {
$config = new Zend_Config_Ini($configPath, 'global');
$settingModel->setConfig(DICOMEXTRACTOR_DCM2XML_COMMAND_KEY, $config->get('dcm2xml', DICOMEXTRACTOR_DCM2XML_COMMAND_DEFAULT_VALUE), $this->moduleName);
$settingModel->setConfig(DICOMEXTRACTOR_DCMJ2PNM_COMMAND_KEY, $config->get('dcmj2pnm', DICOMEXTRACTOR_DCMJ2PNM_COMMAND_DEFAULT_VALUE), $this->moduleName);
$settingModel->setConfig(DICOMEXTRACTOR_DCMFTEST_COMMAND_KEY, $config->get('dcmftest', DICOMEXTRACTOR_DCMFTEST_COMMAND_DEFAULT_VALUE), $this->moduleName);
$settingModel->setConfig(DICOMEXTRACTOR_DCMDICTPATH_KEY, $config->get('dcmdictpath', DICOMEXTRACTOR_DCMDICTPATH_DEFAULT_VALUE), $this->moduleName);

$config = new Zend_Config_Ini($configPath, null, true);
unset($config->global->dcm2xml);
unset($config->global->dcmj2pnm);
unset($config->global->dcmftest);
unset($config->global->dcmdictpath);

$writer = new Zend_Config_Writer_Ini();
$writer->setConfig($config);
$writer->setFilename($configPath);
$writer->write();
} else {
$settingModel->setConfig(DICOMEXTRACTOR_DCM2XML_COMMAND_KEY, DICOMEXTRACTOR_DCM2XML_COMMAND_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(DICOMEXTRACTOR_DCMJ2PNM_COMMAND_KEY, DICOMEXTRACTOR_DCMJ2PNM_COMMAND_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(DICOMEXTRACTOR_DCMFTEST_COMMAND_KEY, DICOMEXTRACTOR_DCMFTEST_COMMAND_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(DICOMEXTRACTOR_DCMDICTPATH_KEY, DICOMEXTRACTOR_DCMDICTPATH_DEFAULT_VALUE, $this->moduleName);
}
}
}
Loading

0 comments on commit 411438a

Please sign in to comment.