Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that gets Accession Number, check for uniqueness
4
 *             and register it into db
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *
9
 *   '$Author: leinfelder $'
10
 *     '$Date: 2011-11-02 20:40:12 -0700 (Wed, 02 Nov 2011) $'
11
 * '$Revision: 6595 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
package edu.ucsb.nceas.metacat.index;
28

    
29
import java.io.ByteArrayInputStream;
30
import java.io.FileNotFoundException;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.util.ArrayList;
34
import java.util.HashMap;
35
import java.util.Iterator;
36
import java.util.List;
37
import java.util.Map;
38
import java.util.Set;
39

    
40
import javax.xml.parsers.DocumentBuilder;
41
import javax.xml.parsers.DocumentBuilderFactory;
42
import javax.xml.parsers.ParserConfigurationException;
43
import javax.xml.xpath.XPath;
44
import javax.xml.xpath.XPathExpressionException;
45
import javax.xml.xpath.XPathFactory;
46

    
47
import org.apache.commons.codec.EncoderException;
48
import org.apache.commons.io.output.ByteArrayOutputStream;
49
import org.apache.commons.lang.StringUtils;
50
import org.apache.commons.logging.Log;
51
import org.apache.commons.logging.LogFactory;
52
import org.apache.solr.client.solrj.SolrQuery;
53
import org.apache.solr.client.solrj.SolrServer;
54
import org.apache.solr.client.solrj.SolrServerException;
55
import org.apache.solr.client.solrj.response.QueryResponse;
56
import org.apache.solr.client.solrj.response.UpdateResponse;
57
import org.apache.solr.common.SolrDocument;
58
import org.apache.solr.common.SolrDocumentList;
59
import org.apache.solr.common.SolrInputDocument;
60
import org.apache.solr.common.util.NamedList;
61

    
62
import org.dataone.cn.indexer.XMLNamespaceConfig;
63
import org.dataone.cn.indexer.parser.IDocumentSubprocessor;
64
import org.dataone.cn.indexer.parser.SolrField;
65
import org.dataone.cn.indexer.resourcemap.ResourceEntry;
66
import org.dataone.cn.indexer.resourcemap.ResourceMap;
67
import org.dataone.cn.indexer.solrhttp.SolrDoc;
68
import org.dataone.cn.indexer.solrhttp.SolrElementAdd;
69
import org.dataone.cn.indexer.solrhttp.SolrElementField;
70
import org.dataone.service.exceptions.NotFound;
71
import org.dataone.service.exceptions.NotImplemented;
72
import org.dataone.service.exceptions.ServiceFailure;
73
import org.dataone.service.exceptions.UnsupportedType;
74
import org.dataone.service.types.v1.Identifier;
75
import org.dataone.service.types.v1.SystemMetadata;
76
import org.dataone.service.util.TypeMarshaller;
77
import org.jibx.runtime.JiBXException;
78
import org.w3c.dom.Document;
79
import org.w3c.dom.NameList;
80
import org.xml.sax.SAXException;
81

    
82
import edu.ucsb.nceas.metacat.index.resourcemap.ResourceMapSubprocessor;
83

    
84
/**
85
 * A class does insert, update and remove indexes to a SOLR server
86
 * @author tao
87
 *
88
 */
89
public class SolrIndex {
90
            
91
    public static final String ID = "id";
92
    private static final String IDQUERY = ID+":*";
93
    private List<IDocumentSubprocessor> subprocessors = null;
94
    private SolrServer solrServer = null;
95
    private XMLNamespaceConfig xmlNamespaceConfig = null;
96
    private List<SolrField> sysmetaSolrFields = null;
97

    
98
    private static DocumentBuilderFactory documentBuilderFactory = null;
99
    private static DocumentBuilder builder = null;
100

    
101
    private static XPathFactory xpathFactory = null;
102
    private static XPath xpath = null;
103
    Log log = LogFactory.getLog(SolrIndex.class);
104
    
105
    static {
106
        documentBuilderFactory = DocumentBuilderFactory.newInstance();
107
        documentBuilderFactory.setNamespaceAware(true);
108
        try {
109
            builder = documentBuilderFactory.newDocumentBuilder();
110
        } catch (ParserConfigurationException e) {
111
            e.printStackTrace();
112
        }
113
        xpathFactory = XPathFactory.newInstance();
114
        xpath = xpathFactory.newXPath();
115
    }
116
    
117
    /**
118
     * Constructor
119
     * @throws SAXException 
120
     * @throws IOException 
121
     */
122
    public SolrIndex(List<SolrField> sysmetaSolrFields, XMLNamespaceConfig xmlNamespaceConfig)
123
                    throws XPathExpressionException, ParserConfigurationException, IOException, SAXException {
124
         this.xmlNamespaceConfig = xmlNamespaceConfig;
125
         this.sysmetaSolrFields = sysmetaSolrFields;
126
         init();
127
    }
128
    
129
    private void init() throws ParserConfigurationException, XPathExpressionException {
130
        xpath.setNamespaceContext(xmlNamespaceConfig);
131
        initExpressions();
132
    }
133

    
134
    private void initExpressions() throws XPathExpressionException {
135
        for (SolrField field : sysmetaSolrFields) {
136
            field.initExpression(xpath);
137
        }
138

    
139
    }
140
    
141
    
142
    /**
143
     * Get the list of the Subprocessors in this index.
144
     * @return the list of the Subprocessors.
145
     */
146
    public List<IDocumentSubprocessor> getSubprocessors() {
147
        return subprocessors;
148
    }
149

    
150
    /**
151
     * Set the list of Subprocessors.
152
     * @param subprocessorList  the list will be set.
153
     */
154
    public void setSubprocessors(List<IDocumentSubprocessor> subprocessorList) {
155
        for (IDocumentSubprocessor subprocessor : subprocessorList) {
156
            subprocessor.initExpression(xpath);
157
        }
158
        this.subprocessors = subprocessorList;
159
    }
160
    
161
    /**
162
     * Generate the index for the given information
163
     * @param id
164
     * @param systemMetadata
165
     * @param dataStream
166
     * @return
167
     * @throws IOException
168
     * @throws SAXException
169
     * @throws ParserConfigurationException
170
     * @throws XPathExpressionException
171
     * @throws JiBXException 
172
     * @throws SolrServerException 
173
     * @throws EncoderException
174
     * @throws UnsupportedType 
175
     * @throws NotFound 
176
     * @throws NotImplemented 
177
     */
178
    private Map<String, SolrDoc> process(String id, SystemMetadata systemMetadata, InputStream dataStream)
179
                    throws IOException, SAXException, ParserConfigurationException,
180
                    XPathExpressionException, JiBXException, EncoderException, SolrServerException, NotImplemented, NotFound, UnsupportedType{
181

    
182
        // Load the System Metadata document
183
        ByteArrayOutputStream systemMetadataOutputStream = new ByteArrayOutputStream();
184
        TypeMarshaller.marshalTypeToOutputStream(systemMetadata, systemMetadataOutputStream);
185
        ByteArrayInputStream systemMetadataStream = new ByteArrayInputStream(systemMetadataOutputStream.toByteArray());
186
        Document sysMetaDoc = generateXmlDocument(systemMetadataStream);
187
        if (sysMetaDoc == null) {
188
            log.error("Could not load System metadata for ID: " + id);
189
            return null;
190
        }
191

    
192
        // Extract the field values from the System Metadata
193
        List<SolrElementField> sysSolrFields = processSysmetaFields(sysMetaDoc, id);
194
        SolrDoc indexDocument = new SolrDoc(sysSolrFields);
195
        Map<String, SolrDoc> docs = new HashMap<String, SolrDoc>();
196
        docs.put(id, indexDocument);
197

    
198
        // Determine if subprocessors are available for this ID
199
        if (subprocessors != null) {
200
                    // for each subprocessor loaded from the spring config
201
                    for (IDocumentSubprocessor subprocessor : subprocessors) {
202
                        // Does this subprocessor apply?
203
                        if (subprocessor.canProcess(sysMetaDoc)) {
204
                            // if so, then extract the additional information from the
205
                            // document.
206
                            try {
207
                                // docObject = the resource map document or science
208
                                // metadata document.
209
                                // note that resource map processing touches all objects
210
                                // referenced by the resource map.
211
                                Document docObject = generateXmlDocument(dataStream);
212
                                if (docObject == null) {
213
                                    log.error("Could not load OBJECT for ID " + id );
214
                                } else {
215
                                    docs = subprocessor.processDocument(id, docs, docObject);
216
                                }
217
                            } catch (Exception e) {
218
                                log.error(e.getStackTrace().toString());
219
                            }
220
                        }
221
                    }
222
       }
223

    
224
       // TODO: in the XPathDocumentParser class in d1_cn_index_process module,
225
       // merge is only for resource map. We need more work here.
226
       for (SolrDoc mergeDoc : docs.values()) {
227
           if (!mergeDoc.isMerged()) {
228
                 mergeWithIndexedDocument(mergeDoc);
229
           }
230
       }
231

    
232
       //SolrElementAdd addCommand = getAddCommand(new ArrayList<SolrDoc>(docs.values()));
233
               
234
       return docs;
235
    }
236
    
237
    /**
238
     * Merge updates with existing solr documents
239
     * 
240
     * This method appears to re-set the data package field data into the
241
     * document about to be updated in the solr index. Since packaging
242
     * information is derived from the package document (resource map), this
243
     * information is not present when processing a document contained in a data
244
     * package. This method replaces those values from the existing solr index
245
     * record for the document being processed. -- sroseboo, 1-18-12
246
     * 
247
     * @param indexDocument
248
     * @return
249
     * @throws IOException
250
     * @throws EncoderException
251
     * @throws XPathExpressionException
252
     * @throws SAXException 
253
     * @throws ParserConfigurationException 
254
     * @throws SolrServerException 
255
     * @throws UnsupportedType 
256
     * @throws NotFound 
257
     * @throws NotImplemented 
258
     */
259
    // TODO:combine merge function with resourcemap merge function
260

    
261
    private SolrDoc mergeWithIndexedDocument(SolrDoc indexDocument) throws IOException,
262
            EncoderException, XPathExpressionException, SolrServerException, ParserConfigurationException, SAXException, NotImplemented, NotFound, UnsupportedType {
263
        List<String> ids = new ArrayList<String>();
264
        ids.add(indexDocument.getIdentifier());
265
        List<SolrDoc> indexedDocuments = ResourceMapSubprocessor.getSolrDocs(ids);
266
        SolrDoc indexedDocument = indexedDocuments == null || indexedDocuments.size() <= 0 ? null
267
                : indexedDocuments.get(0);
268
        if (indexedDocument == null || indexedDocument.getFieldList().size() <= 0) {
269
            return indexDocument;
270
        } else {
271
            for (SolrElementField field : indexedDocument.getFieldList()) {
272
                if ((field.getName().equals(SolrElementField.FIELD_ISDOCUMENTEDBY)
273
                        || field.getName().equals(SolrElementField.FIELD_DOCUMENTS) || field
274
                        .getName().equals(SolrElementField.FIELD_RESOURCEMAP))
275
                        && !indexDocument.hasFieldWithValue(field.getName(), field.getValue())) {
276
                    indexDocument.addField(field);
277
                }
278
            }
279

    
280
            indexDocument.setMerged(true);
281
            return indexDocument;
282
        }
283
    }
284
    
285
    /*
286
     * Generate a Document from the InputStream
287
     */
288
    private Document generateXmlDocument(InputStream smdStream) throws SAXException {
289
        Document doc = null;
290

    
291
        try {
292
            doc = builder.parse(smdStream);
293
        } catch (IOException e) {
294
            log.error(e.getMessage(), e);
295
        }
296

    
297
        return doc;
298
    }
299
    
300
    /*
301
     * Index the fields of the system metadata
302
     */
303
    private List<SolrElementField> processSysmetaFields(Document doc, String identifier) {
304

    
305
        List<SolrElementField> fieldList = new ArrayList<SolrElementField>();
306
        // solrFields is the list of fields defined in the application context
307
       
308
        for (SolrField field : sysmetaSolrFields) {
309
            try {
310
                // the field.getFields method can return a single value or
311
                // multiple values for multi-valued fields
312
                // or can return multiple SOLR document fields.
313
                fieldList.addAll(field.getFields(doc, identifier));
314
            } catch (Exception e) {
315
                e.printStackTrace();
316
            }
317
        }
318
        return fieldList;
319

    
320
    }
321
    
322
    /**
323
     * Check the parameters of the insert or update methods.
324
     * @param pid
325
     * @param systemMetadata
326
     * @param data
327
     * @throws SolrServerException
328
     */
329
    private void checkParams(String pid, SystemMetadata systemMetadata, InputStream data) throws SolrServerException {
330
        if(pid == null || pid.trim().equals("")) {
331
            throw new SolrServerException("The identifier of the indexed document should not be null or blank.");
332
        }
333
        if(systemMetadata == null) {
334
            throw new SolrServerException("The system metadata of the indexed document should not be null.");
335
        }
336
        if(data == null) {
337
            throw new SolrServerException("The indexed document itself should not be null.");
338
        }
339
    }
340
    
341
    /**
342
     * Insert the indexes for a document.
343
     * @param pid  the id of this document
344
     * @param systemMetadata  the system metadata associated with the data object
345
     * @param data  the data object itself
346
     * @throws SolrServerException 
347
     * @throws JiBXException 
348
     * @throws EncoderException 
349
     * @throws UnsupportedType 
350
     * @throws NotFound 
351
     * @throws NotImplemented 
352
     */
353
    private synchronized void insert(String pid, SystemMetadata systemMetadata, InputStream data) 
354
                    throws IOException, SAXException, ParserConfigurationException,
355
                    XPathExpressionException, SolrServerException, JiBXException, EncoderException, NotImplemented, NotFound, UnsupportedType {
356
        checkParams(pid, systemMetadata, data);
357
        Map<String, SolrDoc> docs = process(pid, systemMetadata, data);
358
        
359
        //transform the Map to the SolrInputDocument which can be used by the solr server
360
        if(docs != null) {
361
            Set<String> ids = docs.keySet();
362
            for(String id : ids) {
363
                if(id != null) {
364
                    SolrDoc doc = docs.get(id);
365
                    insertToIndex(doc);
366
                }
367
                
368
            }
369
        }
370
    }
371
    
372
    /*
373
     * Insert a SolrDoc to the solr server.
374
     */
375
    private synchronized void insertToIndex(SolrDoc doc) throws SolrServerException, IOException {
376
        if(doc != null ) {
377
            SolrInputDocument solrDoc = new SolrInputDocument();
378
            List<SolrElementField> list = doc.getFieldList();
379
            if(list != null) {
380
                //solrDoc.addField(METACATPIDFIELD, pid);
381
                Iterator<SolrElementField> iterator = list.iterator();
382
                while (iterator.hasNext()) {
383
                    SolrElementField field = iterator.next();
384
                    if(field != null) {
385
                        String value = field.getValue();
386
                        String name = field.getName();
387
                        //System.out.println("add name/value pair - "+name+"/"+value);
388
                        solrDoc.addField(name, value);
389
                    }
390
                }
391
            }
392
            if(!solrDoc.isEmpty()) {
393
                UpdateResponse response = solrServer.add(solrDoc);
394
                solrServer.commit();
395
                //System.out.println("=================the response is:\n"+response.toString());
396
            }
397
        }
398
    }
399
    
400
    /**
401
     * Update the solr index. This method handles the three scenarios:
402
     * 1. Archive (or delete) - if the the system metadata shows the value of the archive is true,
403
     *    remove the index for the document and its previous versions if it has.
404
     * 2. Update an existing doc - if the the system metadata shows the value of the archive is false and it has an obsoletes,
405
     *    remove the index for the previous version(s) and generate new index for the doc.
406
     * 3. Add a new doc - if the system metadata shows the value of the archive is false and it hasn't an obsoletes, generate the
407
     *    index for the doc.
408
     * @param pid  the id of the document
409
     * @param obsoleteIds  the chain of the obsoletes by this id
410
     * @param systemMetadata  the system metadata associated with the data object
411
     * @param data  the data object itself
412
     * @throws SolrServerException 
413
     * @throws JiBXException 
414
     * @throws EncoderException 
415
     * @throws UnsupportedType 
416
     * @throws NotFound 
417
     * @throws NotImplemented 
418
     * @throws ServiceFailure 
419
     */
420
    public void update(String pid, List<String> obsoleteIds, SystemMetadata systemMetadata, InputStream data) 
421
                    throws IOException, SAXException, ParserConfigurationException,
422
                    XPathExpressionException, SolrServerException, JiBXException, EncoderException, NotImplemented, NotFound, UnsupportedType, ServiceFailure {
423
        checkParams(pid, systemMetadata, data);
424
        boolean isArchive = systemMetadata.getArchived();
425
        if(isArchive) {
426
            //archive(delete)
427
            Identifier obsolete = systemMetadata.getObsoletes();
428
            if(obsolete != null) {
429
                removeObsoletesChain(obsolete.getValue(), obsoleteIds);
430
            }
431
            remove(pid);
432
            log.info("============================= archive the idex for the identifier "+pid);
433
        } else {
434
            Identifier obsolete = systemMetadata.getObsoletes();
435
            if(obsolete != null) {
436
                removeObsoletesChain(obsolete.getValue(), obsoleteIds);
437
            }
438
            //generate index for either add or update.
439
            insert(pid, systemMetadata, data);
440
            log.info("============================= insert index for the identifier "+pid);
441
        }
442
    }
443
    
444
    
445
    private void removeObsoletesChain(String obsoleteId, List<String> obsoleteIdChain) throws SolrServerException, IOException, ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SAXException, ParserConfigurationException {
446
        if(obsoleteId != null && !obsoleteId.trim().equals("")) {
447
            if(obsoleteIdChain == null || obsoleteIdChain.isEmpty()) {
448
                throw new SolrServerException("SolrIndex.removeObsoletesChain - The obsoletes chain can't be null or empty since the system metadata already has the obsoletes element."); 
449
            }
450
            if(!obsoleteIdChain.contains(obsoleteId)) {
451
                throw new SolrServerException("SolrIndex.removeObsoletesChain - The obsoletes elment in the system metadata is not in the obsoleteId chain"); 
452
            }
453
            remove(obsoleteIdChain);
454
        } else {
455
            throw new SolrServerException("SolrIndex.removeObsoletesChain - The obsolete id should be null."); 
456
        }  
457
    }
458
    
459
    /**
460
     * Remove all the indexes associated with the pids in the list.
461
     * @param pidList
462
     * @throws IOException
463
     * @throws SolrServerException
464
     * @throws ParserConfigurationException 
465
     * @throws SAXException 
466
     * @throws UnsupportedType 
467
     * @throws NotFound 
468
     * @throws NotImplemented 
469
     * @throws XPathExpressionException 
470
     * @throws ServiceFailure 
471
     */
472
    private void remove(List<String> pidList) throws IOException, SolrServerException, ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SAXException, ParserConfigurationException {
473
        if(pidList != null) {
474
            for(String id : pidList) {
475
                remove(id);
476
            }
477
        }
478
    }
479
 
480
    /**
481
     * Remove the indexed associated with specified pid.
482
     * @param pid  the pid which the indexes are associated with
483
     * @throws IOException
484
     * @throws SolrServerException
485
     * @throws ParserConfigurationException 
486
     * @throws SAXException 
487
     * @throws UnsupportedType 
488
     * @throws NotFound 
489
     * @throws NotImplemented 
490
     * @throws XPathExpressionException 
491
     * @throws ServiceFailure 
492
     */
493
    public void remove(String pid) throws IOException, SolrServerException, ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SAXException, ParserConfigurationException {
494
        if (isDataPackage(pid)) {
495
            removeDataPackage(pid);
496
        } else if (isPartOfDataPackage(pid)) {
497
            removeFromDataPackage(pid);
498
        } else {
499
            removeFromIndex(pid);
500
        }
501
    }
502
    
503
   
504
   
505

    
506
    /*
507
     * Remove a resource map pid
508
     */
509
    private void removeDataPackage(String pid) throws ServiceFailure, SAXException, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException  {
510
        Document resourceMapDoc = generateXmlDocument(DistributedMapsFactory.getDataObject(pid));
511
        ResourceMap resourceMap = new ResourceMap(resourceMapDoc);
512
        List<String> documentIds = resourceMap.getAllDocumentIDs();
513
        List<SolrDoc> indexDocuments =ResourceMapSubprocessor.getSolrDocs(documentIds);
514
        removeFromIndex(pid);
515
        //List<SolrDoc> docsToUpdate = new ArrayList<SolrDoc>();
516
        // for each document in data package:
517
        for (SolrDoc indexDoc : indexDocuments) {
518

    
519
            if (indexDoc.getIdentifier().equals(pid)) {
520
                continue; // skipping the resource map, no need update
521
                          // it.
522
                          // will
523
                          // be removed.
524
            }
525

    
526
            // Remove resourceMap reference
527
            indexDoc.removeFieldsWithValue(SolrElementField.FIELD_RESOURCEMAP,
528
                    resourceMap.getIdentifier());
529

    
530
            // // Remove documents/documentedby values for this resource
531
            // map
532
            for (ResourceEntry entry : resourceMap.getMappedReferences()) {
533
                if (indexDoc.getIdentifier().equals(entry.getIdentifier())) {
534
                    for (String documentedBy : entry.getDocumentedBy()) {
535
                        // Using removeOneFieldWithValue in-case same
536
                        // documents
537
                        // are in more than one data package. just
538
                        // remove
539
                        // one
540
                        // instance of data package info.
541
                        indexDoc.removeOneFieldWithValue(SolrElementField.FIELD_ISDOCUMENTEDBY,
542
                                documentedBy);
543
                    }
544
                    for (String documents : entry.getDocuments()) {
545
                        indexDoc.removeOneFieldWithValue(SolrElementField.FIELD_DOCUMENTS,
546
                                documents);
547
                    }
548
                    break;
549
                }
550
            }
551
            removeFromIndex(indexDoc.getIdentifier());
552
            insertToIndex(indexDoc);
553
            //docsToUpdate.add(indexDoc);
554
        }
555
        //SolrElementAdd addCommand = new SolrElementAdd(docsToUpdate);
556
        //httpService.sendUpdate(solrIndexUri, addCommand);
557
    }
558

    
559
    private void removeFromDataPackage(String pid) throws XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, SAXException  {
560
        SolrDoc indexedDoc = ResourceMapSubprocessor.getSolrDoc(pid);
561
        removeFromIndex(pid);
562
        List<SolrDoc> docsToUpdate = new ArrayList<SolrDoc>();
563

    
564
        List<String> documents = indexedDoc.getAllFieldValues(SolrElementField.FIELD_DOCUMENTS);
565
        for (String documentsValue : documents) {
566
            SolrDoc solrDoc = ResourceMapSubprocessor.getSolrDoc(documentsValue);
567
            solrDoc.removeFieldsWithValue(SolrElementField.FIELD_ISDOCUMENTEDBY, pid);
568
            removeFromIndex(documentsValue);
569
            insertToIndex(solrDoc);
570
        }
571

    
572
        List<String> documentedBy = indexedDoc
573
                .getAllFieldValues(SolrElementField.FIELD_ISDOCUMENTEDBY);
574
        for (String documentedByValue : documentedBy) {
575
            SolrDoc solrDoc = ResourceMapSubprocessor.getSolrDoc(documentedByValue);
576
            solrDoc.removeFieldsWithValue(SolrElementField.FIELD_DOCUMENTS, documentedByValue);
577
            //docsToUpdate.add(solrDoc);
578
            removeFromIndex(documentedByValue);
579
            insertToIndex(solrDoc);
580
        }
581

    
582
        //SolrElementAdd addCommand = new SolrElementAdd(docsToUpdate);
583
        //httpService.sendUpdate(solrIndexUri, addCommand);
584
    }
585

    
586
    /*
587
     * Remove a pid from the solr index
588
     */
589
    private void removeFromIndex(String pid) throws SolrServerException, IOException {
590
        if(pid != null && !pid.trim().equals("")) {
591
            solrServer.deleteById(pid);
592
            solrServer.commit();
593
        }
594
    }
595

    
596
    /*
597
     * Is the pid a resource map
598
     */
599
    private boolean isDataPackage(String pid) throws FileNotFoundException, ServiceFailure {
600
        boolean isDataPackage = false;
601
        SystemMetadata sysmeta = DistributedMapsFactory.getSystemMetadata(pid);
602
        if(sysmeta != null) {
603
            isDataPackage = IndexGenerator.isResourceMap(sysmeta.getFormatId());
604
        }
605
        return isDataPackage;
606
    }
607

    
608
    private boolean isPartOfDataPackage(String pid) throws XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, SAXException {
609
        SolrDoc dataPackageIndexDoc = ResourceMapSubprocessor.getSolrDoc(pid);
610
        if (dataPackageIndexDoc != null) {
611
            String resourceMapId = dataPackageIndexDoc
612
                    .getFirstFieldValue(SolrElementField.FIELD_RESOURCEMAP);
613
            return StringUtils.isNotEmpty(resourceMapId);
614
        } else {
615
            return false;
616
        }
617
    }
618

    
619
    /**
620
     * Get the solrServer
621
     * @return
622
     */
623
    public SolrServer getSolrServer() {
624
        return solrServer;
625
    }
626

    
627
    /**
628
     * Set the solrServer. 
629
     * @param solrServer
630
     */
631
    public void setSolrServer(SolrServer solrServer) {
632
        this.solrServer = solrServer;
633
    }
634
    
635
    /**
636
     * Get all indexed ids in the solr server. 
637
     * @return an empty list if there is no index.
638
     * @throws SolrServerException
639
     */
640
    public List<String> getSolrIds() throws SolrServerException {
641
        List<String> list = new ArrayList<String>();
642
        SolrQuery query = new SolrQuery(IDQUERY); 
643
        query.setRows(Integer.MAX_VALUE); 
644
        query.setFields(ID); 
645
        QueryResponse response = solrServer.query(query); 
646
        SolrDocumentList docs = response.getResults();
647
        if(docs != null) {
648
            for(SolrDocument doc :docs) {
649
                String identifier = (String)doc.getFieldValue(ID);
650
                //System.out.println("======================== "+identifier);
651
                list.add(identifier);
652
            }
653
        }
654
        return list;
655
    }
656
}
(5-5/6)