File size: 13,269 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php

/**
 * Class used as interface to have access to the search index services
 */
class BpmnEngineServicesSearchIndex
{
    private $_solrIsEnabled = false;
    private $_solrHost = "";

    public function __construct($solrIsEnabled = false, $solrHost = "")
    {
        $this->_solrIsEnabled = $solrIsEnabled;
        $this->_solrHost = $solrHost;
    }

    /**
     * Verify if the Solr service is available
     *
     * @gearman = false
     * @rest = false
     * @background = false no input parameters @param[in]
     * @param [out] bool true if index service is enabled false in other case
     */
    public function isEnabled($workspace)
    {
        $solr = new BpmnEngineSearchIndexAccessSolr($this->_solrIsEnabled, $this->_solrHost);
        return $solr->isEnabled($workspace);
    }

    /**
     * Get the list of facets in base to the specified query and filter
     *
     * @gearman = true
     * @rest = false
     * @background = false
     * @param [in] Entity_FacetRequest facetRequestEntity Facet request entity
     * @param [out] array FacetGroup
     */
    public function getFacetsList($facetRequestEntity)
    {
        // get array of selected facet groups
        $facetRequestEntity->selectedFacetsString = str_replace(',,', ',', $facetRequestEntity->selectedFacetsString);
        // remove descriptions of selected facet groups

        $aGroups = explode(',', $facetRequestEntity->selectedFacetsString);

        $aGroups = array_filter($aGroups); // remove empty items

        $aSelectedFacetGroups = array();
        foreach ($aGroups as $i => $value) {
            $gi = explode(':::', $value);
            $gr = explode('::', $gi [0]);
            $it = explode('::', $gi [1]);

            // create string for remove condition
            $count = 0;
            $removeCondition = str_replace($value . ',', '', $facetRequestEntity->selectedFacetsString, $count);
            if ($count == 0) {
                $removeCondition = str_replace($value, '', $facetRequestEntity->selectedFacetsString, $count);
            }
            $selectedFacetGroupData = array(
                'selectedFacetGroupName' => $gr [0],
                'selectedFacetGroupPrintName' => $gr [1],
                'selectedFacetItemName' => $it [0],
                'selectedFacetItemPrintName' => $it [1],
                'selectedFacetRemoveCondition' => $removeCondition
            );

            $aSelectedFacetGroups [] = EntitySelectedFacetGroupItem::createForRequest($selectedFacetGroupData);
        }

        // convert request to index request
        // create filters
        $filters = array();
        if (!empty($aSelectedFacetGroups)) {
            // exclude facetFields and facetDates included in filter from the next
            // list of facets
            foreach ($aSelectedFacetGroups as $value) {
                $facetRequestEntity->facetFields = array_diff($facetRequestEntity->facetFields, array(
                    $value->selectedFacetGroupName
                ));
                $facetRequestEntity->facetDates = array_diff($facetRequestEntity->facetDates, array(
                    $value->selectedFacetGroupName
                ));
            }

            foreach ($aSelectedFacetGroups as $group) {
                $filters [] = $group->selectedFacetGroupName . ':' . urlencode($group->selectedFacetItemName);
            }
        }
        $facetRequestEntity->filters = $filters;

        $solr = new BpmnEngineSearchIndexAccessSolr($this->_solrIsEnabled, $this->_solrHost);

        // create list of facets
        $facetsList = $solr->getFacetsList($facetRequestEntity);

        $numFound = $facetsList->response->numFound;

        $facetCounts = $facetsList->facet_counts;

        $facetGroups = array();

        // convert facet fields result to objects
        // include facet field results
        $facetFieldsResult = $facetsList->facet_counts->facet_fields;
        if (!empty($facetFieldsResult)) {
            foreach ($facetFieldsResult as $facetGroup => $facetvalues) {
                if (count($facetvalues) > 0) {         // if the group have facets included
                    $data = array(
                        'facetGroupName' => $facetGroup
                    );
                    $data ['facetGroupPrintName'] = $facetGroup;
                    $data ['facetGroupType'] = 'field';
                    $facetItems = array();
                    for ($i = 0; $i < count($facetvalues); $i += 2) {
                        $dataItem = array();
                        $dataItem ['facetName'] = $facetvalues [$i];
                        $dataItem ['facetPrintName'] = $facetvalues [$i];
                        $dataItem ['facetCount'] = $facetvalues [$i + 1];
                        $dataItem ['facetSelectCondition'] = $facetRequestEntity->selectedFacetsString . (empty($facetRequestEntity->selectedFacetsString) ? '' : ',') . $data ['facetGroupName'] . '::' . $data ['facetGroupPrintName'] . ':::' . $dataItem ['facetName'] . '::' . $dataItem ['facetPrintName'];
                        $newFacetItem = EntityFacetItem::createForInsert($dataItem);
                        $facetItems [] = $newFacetItem;
                    }
                    $data ['facetItems'] = $facetItems;
                    $newFacetGroup = EntityFacetGroup::createForInsert($data);

                    $facetGroups [] = $newFacetGroup;
                }
            }
        }

        // include facet date ranges results
        $facetDatesResult = $facetsList->facet_counts->facet_dates;
        if (!empty($facetDatesResult)) {
            foreach ($facetDatesResult as $facetGroup => $facetvalues) {
                if (count((array) $facetvalues) > 3) {         // if the group have any facets included
                    // besides start, end and gap
                    $data = array(
                        'facetGroupName' => $facetGroup
                    );
                    $data ['facetGroupPrintName'] = $facetGroup;
                    $data ['facetGroupType'] = 'daterange';
                    $facetItems = array();
                    $facetvalueskeys = array_keys((array) $facetvalues);
                    foreach ($facetvalueskeys as $i => $k) {
                        if ($k != 'gap' && $k != 'start' && $k != 'end') {
                            $dataItem = array();
                            if ($i < count($facetvalueskeys) - 4) {
                                $dataItem ['facetName'] = '[' . $k . '%20TO%20' . $facetvalueskeys [$i + 1] . ']';
                                $dataItem ['facetPrintName'] = '[' . $k . '%20TO%20' . $facetvalueskeys [$i + 1] . ']';
                            } else {
                                // the last group
                                $dataItem ['facetName'] = '[' . $k . '%20TO%20' . $facetvalues->end . ']';
                                $dataItem ['facetPrintName'] = '[' . $k . '%20TO%20' . $facetvalues->end . ']';
                            }

                            $dataItem ['facetCount'] = $facetvalues->$k;
                            $dataItem ['facetSelectCondition'] = $facetRequestEntity->selectedFacetsString . (empty($facetRequestEntity->selectedFacetsString) ? '' : ',') . $data ['facetGroupName'] . '::' . $data ['facetGroupPrintName'] . ':::' . $dataItem ['facetName'] . '::' . $dataItem ['facetPrintName'];
                            $newFacetItem = EntityFacetItem::createForInsert($dataItem);
                            $facetItems [] = $newFacetItem;
                        }
                    }

                    $data ['facetItems'] = $facetItems;
                    $newFacetGroup = EntityFacetGroup::createForInsert($data);

                    $facetGroups [] = $newFacetGroup;
                }
            }
        }
        // TODO:convert facet queries
        // Create a filter string used in the filter of results of a datatable
        $filterText = ''; // the list of selected filters used for filtering result,
        // send in ajax
        foreach ($aSelectedFacetGroups as $selectedFacetGroup) {
            $filterText .= $selectedFacetGroup->selectedFacetGroupName . ':' . urlencode($selectedFacetGroup->selectedFacetItemName) . ',';
        }
        $filterText = substr_replace($filterText, '', - 1);

        // Create result
        $dataFacetResult = array(
            'aFacetGroups' => $facetGroups,
            'aSelectedFacetGroups' => $aSelectedFacetGroups,
            'sFilterText' => $filterText
        );
        $facetResult = EntityFacetResult::createForRequest($dataFacetResult);

        return $facetResult;
    }

    /**
     * Get the total number of documents in search server
     * @param string $workspace
     * @return integer number of documents
     *
     */
    public function getNumberDocuments($workspace)
    {
        require_once('class.solr.php');
        $solr = new BpmnEngineSearchIndexAccessSolr($this->_solrIsEnabled, $this->_solrHost);

        // create list of facets
        $numberDocuments = $solr->getNumberDocuments($workspace);

        return $numberDocuments;
    }

    /**
     * Update document Index
     * @param SolrUpdateDocumentEntity $solrUpdateDocumentEntity
     */
    public function updateIndexDocument($solrUpdateDocumentEntity)
    {
        $solr = new BpmnEngineSearchIndexAccessSolr($this->_solrIsEnabled, $this->_solrHost);

        // create list of facets
        $solr->updateDocument($solrUpdateDocumentEntity);
    }

    /**
     * Delete document from index
     * @param string $workspace
     * @param string $idQuery
     */
    public function deleteDocumentFromIndex($workspace, $idQuery)
    {
        $solr = new BpmnEngineSearchIndexAccessSolr($this->_solrIsEnabled, $this->_solrHost);

        // create list of facets
        $solr->deleteDocument($workspace, $idQuery);
    }

    /**
     * Commit index changes
     * @param string $workspace
     */
    public function commitIndexChanges($workspace)
    {
        $solr = new BpmnEngineSearchIndexAccessSolr($this->_solrIsEnabled, $this->_solrHost);

        // commit
        $solr->commitChanges($workspace);
    }

    /**
     * Optimize index changes
     * @param string $workspace
     */
    public function optimizeIndexChanges($workspace)
    {
        $solr = new BpmnEngineSearchIndexAccessSolr($this->_solrIsEnabled, $this->_solrHost);

        // commit
        $solr->optimizeChanges($workspace);
    }

    /**
     * Call Solr server to return the list of paginated pages.
     * @param FacetRequest $solrRequestData
     * @return EntitySolrQueryResult
     */
    public function getDataTablePaginatedList($solrRequestData)
    {
        // execute query
        $solr = new BpmnEngineSearchIndexAccessSolr($this->_solrIsEnabled, $this->_solrHost);
        $solrPaginatedResult = $solr->executeQuery($solrRequestData);

        // get total number of documents in index
        $numTotalDocs = $solr->getNumberDocuments($solrRequestData->workspace);

        // create the Datatable response of the query
        $numFound = $solrPaginatedResult->response->numFound;

        $docs = $solrPaginatedResult->response->docs;

        // insert list of names in docs result
        $data = array(
            "sEcho" => '', // must be completed in response
            "iTotalRecords" => intval($numTotalDocs), // we must get the
            // total number of documents
            "iTotalDisplayRecords" => $numFound,
            "aaData" => array()
        );
        // copy result document or add placeholders to result
        foreach ($docs as $i => $doc) {
            $data ['aaData'] [$i] = array();
            foreach ($solrRequestData->includeCols as $columnName) {
                if ($columnName == '') {
                    $data ['aaData'] [$i] [] = ''; // placeholder
                } else {
                    if (isset($doc->$columnName)) {
                        $data ['aaData'] [$i] [$columnName] = $doc->$columnName;
                    } else {
                        $data ['aaData'] [$i] [$columnName] = '';
                    }
                }
            }
        }

        $solrQueryResponse = EntitySolrQueryResult::createForRequest($data);

        return $solrQueryResponse;
    }

    /**
     * Return the list of stored fields in the index.
     * @param string $workspace
     * @return array of index fields
     */
    public function getIndexFields($workspace)
    {
        $solr = new BpmnEngineSearchIndexAccessSolr($this->_solrIsEnabled, $this->_solrHost);

        $solrFieldsData = $solr->getListIndexedStoredFields($workspace);
        // copy list of arrays
        $listFields = array();
        foreach ($solrFieldsData->fields as $key => $fieldData) {
            if (array_key_exists('dynamicBase', $fieldData)) {
                $originalFieldName = substr($key, 0, - strlen($fieldData->dynamicBase) + 1);

                // Maintain case sensitive variable names
                $listFields [$originalFieldName] = $key;
            } else {
                // Maintain case sensitive variable names
                $listFields [$key] = $key;
            }
        }

        return $listFields;
    }
}