-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConceptSearchSourceFactory.inc.php
297 lines (262 loc) · 9.17 KB
/
ConceptSearchSourceFactory.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
require_once(MCL_ROOT . 'ConceptSearchSourceCollection.inc.php');
require_once(MCL_ROOT . 'ConceptSearchSource.inc.php');
class ConceptSearchSourceFactory
{
/**
* Whether to display debug info.
*/
public $debug = false;
/**
* Whether to display verbose info.
*/
public $verbose = false;
/**
* Loads all source definitions for OpenMRS only mode, which includes the
* default OpenMRS dictionary source and all of its map sources.
* @return ConceptSearchSourceCollection
*/
public function loadOpenmrsOnlySourceDefinitions($cxn_mcl,
$mcl_default_concept_dict_db, $mcl_default_concept_dict_name,
$db_host, $db_uid, $db_pwd)
{
// $cxn_mcl points to the actual openmrs database. only map sources exist
// Add definition for the default dictionary
$cssc = new ConceptSearchSourceCollection();
$css_dict = new ConceptSearchSource();
$css_dict->setSourceDictionary(MCL_SOURCE_DEFAULT_DICT_ID,
$mcl_default_concept_dict_db, $mcl_default_concept_dict_name);
$css_dict->setConnectionParameters($db_host, $db_uid, $db_pwd);
$cssc->add($css_dict);
// Add definitions for map sources
$cssc->add($this->loadMapSourceDefinitions($css_dict));
return $cssc;
}
/**
* Loads all source definitions for MCL Enhanced mode, which includes
* concept dictionaries as defined in the MCL concept_dict table, all
* of their map sources, and all MCL Concept Lists.
* @return ConceptSearchSourceCollection
*/
public function loadEnhancedSourceDefinitions($cxn_mcl)
{
// $cxn_mcl points to the MCL db, so load dictionary definitions from there
// Load dictionary definitions
$cssc = new ConceptSearchSourceCollection();
$arr_dict = $this->loadDictionaryDefinitions($cxn_mcl);
$cssc->add($arr_dict);
// Load concept lists from MCL db
$cssc->add($this->loadConceptListDefinitions($cxn_mcl, $cssc));
// Load map sources for each dictionary
foreach ($arr_dict as $css_dict) {
$cssc->add($this->loadMapSourceDefinitions($css_dict));
}
return $cssc;
}
/**
* Load dictionary source definitions from the MCL database in
* enhanced mode.
* @return Array of ConceptSearchSource objects
*/
public function loadDictionaryDefinitions($cxn_mcl)
{
// load dictionary sources
$arr_dict = array();
$sql_dict = 'select * from concept_dict where active = 1 order by sort_order, dict_name';
if (!($rsc_dict = mysql_query($sql_dict))) {
trigger_error('Could not load dictionary sources: ' . mysql_error($cxn_mcl), E_USER_ERROR);
}
if ( $this->debug ) {
echo '<p><b>Loading dictionary sources:</b><br> ' . $sql_dict . '</p>';
}
while ($row = mysql_fetch_assoc($rsc_dict))
{
$css = new ConceptSearchSource();
$css->setSourceDictionary(
$row[ 'dict_id' ],
$row[ 'db_name' ],
$row[ 'dict_name' ],
$row[ 'fulltext_mode' ],
$row[ 'last_updated' ]
);
$css->setConnectionParameters(
$row[ 'host' ],
$row[ 'uid' ],
$row[ 'pwd' ]
);
$arr_dict[] = $css;
}
return $arr_dict;
}
/**
* Load concept list definitions from the MCL database in enhanced mode.
* @return Array of ConceptSearchSource objects
*/
public function loadConceptListDefinitions($cxn_mcl, ConceptSearchSourceCollection $cssc)
{
// Load concept list definitions
$arr_list = array();
$sql_list = 'select concept_list_id, list_name from concept_list where active = 1 order by list_name';
if ( !($rsc_list = mysql_query($sql_list, $cxn_mcl)) ) {
trigger_error('Could not load concept list definitions: ' . mysql_error($cxn_mcl), E_USER_ERROR);
}
if ( $this->debug ) {
echo '<p><b>Loading concept list definitions:</b><br> ' . $sql_list . '</p>';
}
while ( $row = mysql_fetch_assoc($rsc_list) )
{
// Create the object
$css = new ConceptSearchSource();
$css->setSourceList( $row['concept_list_id'] , $row['list_name'] );
$arr_list[ $row['concept_list_id'] ] = $css;
}
// Load dictionary sources for the each list
$sql_list_dict_id = 'select distinct concept_list_id, dict_id from concept_list_map';
if ( !($rsc_list_dict_id = mysql_query($sql_list_dict_id, $cxn_mcl)) ) {
trigger_error('Could not load concept list definitions: ' . mysql_error($cxn_mcl), E_USER_ERROR);
}
while ( $row_dict_id = mysql_fetch_assoc($rsc_list_dict_id) )
{
$css_dict = $cssc->getDictionary( $row_dict_id['dict_id'] );
if ($css_dict && isset($arr_list[$row_dict_id['concept_list_id']])) {
$arr_list[$row_dict_id['concept_list_id']]->addDictionarySource( $css_dict );
}
}
return $arr_list;
}
/**
* Load the Map Source definitions from the passed concept dictionary
* definition.
* @param ConceptSearchSource $css_dict ConceptSearchSource of type MCL_SOURCE_TYPE_DICTIONARY
* @return Array of ConceptSearchSource objects
*/
public function loadMapSourceDefinitions(ConceptSearchSource $css_dict)
{
// Make sure the passed source is a dictionary
if ( $css_dict->type != MCL_SOURCE_TYPE_DICTIONARY ) {
trigger_error('Parameter $css_dict must be of type MCL_SOURCE_TYPE_DICTIONARY', E_USER_ERROR);
}
// Connect to the database
$cxn = $css_dict->getConnection();
mysql_select_db( $css_dict->dict_db , $cxn );
// Build the sql
$sql_cs =
'select cs.concept_source_id, cs.name, cs.description, cs.retired ' .
'from concept_source cs ' .
'order by cs.name';
if ( $this->debug ) {
echo '<p><b>Loading map source definitions for ' . $css_dict->dict_db . ':</b><br> ' . $sql_cs . '</p>';
}
// Execute the query
$rsc_cs = mysql_query( $sql_cs , $cxn );
if ( !$rsc_cs ) {
trigger_error('Could not load map source definitions: ' . mysql_error(), E_USER_ERROR);
}
// put the data into an array
$arr_source = array();
while ( $row = mysql_fetch_assoc($rsc_cs) )
{
$css = new ConceptSearchSource();
$css->setSourceMap(
$css_dict->dict_id ,
$css_dict->dict_db ,
$css_dict->dict_name ,
$row[ 'concept_source_id' ] ,
$row[ 'name' ]
);
$css->addDictionarySource( $css_dict );
$arr_source[] = $css;
}
return $arr_source;
}
/**
* Load concept classes for all dictionaries in the passed source collection.
* @return ConceptClassCollection
*/
public function loadAllConceptClasses(ConceptSearchSourceCollection $coll_source)
{
$ccc = new ConceptClassCollection();
foreach ($coll_source->getDictionaries() as $css_dict) {
$ccc->merge($this->loadConceptClasses($css_dict));
}
return $ccc;
}
/**
* Load concept datatypes for all dictionaries in the passed source collection.
* @return ConceptDatatypeCollection
*/
public function loadAllConceptDatatypes(ConceptSearchSourceCollection $coll_source)
{
$cdc = new ConceptDatatypeCollection();
foreach ($coll_source->getDictionaries() as $css_dict) {
$cdc->merge($this->loadConceptDatatypes($css_dict));
}
return $cdc;
}
/**
* Loads the concept datatypes from the specified dictionary.
* @return ConceptDatatypeCollection
*/
public function loadConceptDatatypes(ConceptSearchSource $css_dict)
{
// get the data
$sql_datatypes =
'select concept_datatype_id, name, description, hl7_abbreviation, uuid ' .
'from concept_datatype ' .
'where retired != 1 ' .
'order by name';
if ($this->debug) {
echo '<p><b>Loading concept datatypes for ' . $css_dict->dict_db . ':</b><br> ' . $sql_datatypes . '</p>';
}
$rsc_datatypes = mysql_query($sql_datatypes, $css_dict->getConnection());
if (!$rsc_datatypes) {
echo "Could not query db in ConceptSearchFactory::_loadConceptDatatypes: " . mysql_error();
}
// Create the datatype collection
$cdc = new ConceptDatatypeCollection();
while ($row = mysql_fetch_assoc($rsc_datatypes))
{
$cd = new ConceptDatatype($row['concept_datatype_id'], $row['name'],
$row['description'], $row['hl7_abbreviation'], $row['uuid']);
$cd->setSourceDictionary($css_dict);
// $key = '<database_name>:datatype(<concept_datatype_id>)
$key = $css_dict->dict_db . ':datatype(' . $row['concept_datatype_id'] . ')';
$cdc->Add($key, $cd);
}
return $cdc;
}
/**
* Loads the concept classes from the specified dictionary.
* @return ConceptClassCollection
*/
public function loadConceptClasses(ConceptSearchSource $css_dict)
{
// get the data
$sql_classes =
'select concept_class_id, name, description, uuid ' .
'from concept_class ' .
'where retired != 1 ' .
'order by name';
if ($this->debug) {
echo '<p><b>Loading concept classes for <strong>' . $css_dict->dict_db .
'</strong>:</b><br> ' . $sql_classes . '</p>';
}
$rsc_classes = mysql_query($sql_classes, $css_dict->getConnection());
if (!$rsc_classes) {
echo "could not query db: " . mysql_error();
}
// Create the class collection
$ccc = new ConceptClassCollection();
while ($row = mysql_fetch_assoc($rsc_classes))
{
$cd = new ConceptClass($row['concept_class_id'], $row['name'],
$row['description'], $row['uuid']);
$cd->setSourceDictionary($css_dict);
// $key = '<database_name>:class(<concept_class_id>)
$key = $css_dict->dict_db . ':class(' . $row['concept_class_id'] . ')';
$ccc->Add($key, $cd);
}
return $ccc;
}
}
?>