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.Calendar;
35
import java.util.HashMap;
36
import java.util.Iterator;
37
import java.util.List;
38
import java.util.Map;
39
import java.util.Set;
40

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

    
48
import org.apache.commons.codec.EncoderException;
49
import org.apache.commons.io.output.ByteArrayOutputStream;
50
import org.apache.commons.lang.StringUtils;
51
import org.apache.commons.logging.Log;
52
import org.apache.commons.logging.LogFactory;
53
import org.apache.solr.client.solrj.SolrQuery;
54
import org.apache.solr.client.solrj.SolrServer;
55
import org.apache.solr.client.solrj.SolrServerException;
56
import org.apache.solr.client.solrj.response.QueryResponse;
57
import org.apache.solr.client.solrj.response.UpdateResponse;
58
import org.apache.solr.common.SolrDocument;
59
import org.apache.solr.common.SolrDocumentList;
60
import org.apache.solr.common.SolrInputDocument;
61
import org.dataone.cn.indexer.XMLNamespaceConfig;
62
import org.dataone.cn.indexer.parser.IDocumentSubprocessor;
63
import org.dataone.cn.indexer.parser.SolrField;
64
import org.dataone.cn.indexer.resourcemap.ResourceEntry;
65
import org.dataone.cn.indexer.resourcemap.ResourceMap;
66
import org.dataone.cn.indexer.solrhttp.SolrDoc;
67
import org.dataone.cn.indexer.solrhttp.SolrElementField;
68
import org.dataone.service.exceptions.NotFound;
69
import org.dataone.service.exceptions.NotImplemented;
70
import org.dataone.service.exceptions.ServiceFailure;
71
import org.dataone.service.exceptions.UnsupportedType;
72
import org.dataone.service.types.v1.Event;
73
import org.dataone.service.types.v1.Identifier;
74
import org.dataone.service.types.v1.SystemMetadata;
75
import org.dataone.service.util.TypeMarshaller;
76
import org.dspace.foresite.OREParserException;
77
import org.jibx.runtime.JiBXException;
78
import org.w3c.dom.Document;
79
import org.xml.sax.SAXException;
80

    
81
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
82
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
83
import edu.ucsb.nceas.metacat.index.resourcemap.ResourceMapSubprocessor;
84

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
544
    /*
545
     * Remove a resource map pid
546
     */
547
    private void removeDataPackage(String pid) throws ServiceFailure, SAXException, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, OREParserException  {
548
        Document resourceMapDoc = generateXmlDocument(DistributedMapsFactory.getDataObject(pid));
549
        //ResourceMap resourceMap = new ResourceMap(resourceMapDoc);
550
        ResourceMap resourceMap = new ResourceMap(resourceMapDoc);
551
        List<String> documentIds = resourceMap.getAllDocumentIDs();
552
        List<SolrDoc> indexDocuments =ResourceMapSubprocessor.getSolrDocs(documentIds);
553
        removeFromIndex(pid);
554
        //List<SolrDoc> docsToUpdate = new ArrayList<SolrDoc>();
555
        // for each document in data package:
556
        for (SolrDoc indexDoc : indexDocuments) {
557

    
558
            if (indexDoc.getIdentifier().equals(pid)) {
559
                continue; // skipping the resource map, no need update
560
                          // it.
561
                          // will
562
                          // be removed.
563
            }
564

    
565
            // Remove resourceMap reference
566
            indexDoc.removeFieldsWithValue(SolrElementField.FIELD_RESOURCEMAP,
567
                    resourceMap.getIdentifier());
568

    
569
            // // Remove documents/documentedby values for this resource
570
            // map
571
            for (ResourceEntry entry : resourceMap.getMappedReferences()) {
572
                if (indexDoc.getIdentifier().equals(entry.getIdentifier())) {
573
                    for (String documentedBy : entry.getDocumentedBy()) {
574
                        // Using removeOneFieldWithValue in-case same
575
                        // documents
576
                        // are in more than one data package. just
577
                        // remove
578
                        // one
579
                        // instance of data package info.
580
                        indexDoc.removeOneFieldWithValue(SolrElementField.FIELD_ISDOCUMENTEDBY,
581
                                documentedBy);
582
                    }
583
                    for (String documents : entry.getDocuments()) {
584
                        indexDoc.removeOneFieldWithValue(SolrElementField.FIELD_DOCUMENTS,
585
                                documents);
586
                    }
587
                    break;
588
                }
589
            }
590
            removeFromIndex(indexDoc.getIdentifier());
591
            insertToIndex(indexDoc);
592
            //docsToUpdate.add(indexDoc);
593
        }
594
        //SolrElementAdd addCommand = new SolrElementAdd(docsToUpdate);
595
        //httpService.sendUpdate(solrIndexUri, addCommand);
596
    }
597

    
598
    private void removeFromDataPackage(String pid) throws XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, SAXException  {
599
        SolrDoc indexedDoc = ResourceMapSubprocessor.getSolrDoc(pid);
600
        removeFromIndex(pid);
601
        List<SolrDoc> docsToUpdate = new ArrayList<SolrDoc>();
602

    
603
        List<String> documents = indexedDoc.getAllFieldValues(SolrElementField.FIELD_DOCUMENTS);
604
        for (String documentsValue : documents) {
605
            SolrDoc solrDoc = ResourceMapSubprocessor.getSolrDoc(documentsValue);
606
            solrDoc.removeFieldsWithValue(SolrElementField.FIELD_ISDOCUMENTEDBY, pid);
607
            removeFromIndex(documentsValue);
608
            insertToIndex(solrDoc);
609
        }
610

    
611
        List<String> documentedBy = indexedDoc
612
                .getAllFieldValues(SolrElementField.FIELD_ISDOCUMENTEDBY);
613
        for (String documentedByValue : documentedBy) {
614
            SolrDoc solrDoc = ResourceMapSubprocessor.getSolrDoc(documentedByValue);
615
            solrDoc.removeFieldsWithValue(SolrElementField.FIELD_DOCUMENTS, documentedByValue);
616
            //docsToUpdate.add(solrDoc);
617
            removeFromIndex(documentedByValue);
618
            insertToIndex(solrDoc);
619
        }
620

    
621
        //SolrElementAdd addCommand = new SolrElementAdd(docsToUpdate);
622
        //httpService.sendUpdate(solrIndexUri, addCommand);
623
    }
624

    
625
    /*
626
     * Remove a pid from the solr index
627
     */
628
    private void removeFromIndex(String pid) throws SolrServerException, IOException {
629
        if(pid != null && !pid.trim().equals("")) {
630
            /*IndexEvent event = new IndexEvent();
631
            event.setDate(Calendar.getInstance().getTime());
632
            Identifier identifier = new Identifier();
633
            identifier.setValue(pid);
634
            event.setIdentifier(identifier);*/
635
            try {
636
                solrServer.deleteById(pid);
637
                solrServer.commit();
638
                /*event.setType(IndexEvent.SUCCESSDELETE);
639
                event.setDescription("Successfully remove the solr index for the id "+identifier.getValue());
640
                try {
641
                    EventlogFactory.createIndexEventLog().write(event);
642
                } catch (Exception e) {
643
                    log.error("SolrIndex.removeFromIndex - IndexEventLog can't log the index deleting event :"+e.getMessage());
644
                }*/
645
            } catch (SolrServerException e) {
646
                /*event.setAction(Event.DELETE);
647
                event.setDescription("Failurely remove the solr index for the id "+identifier.getValue()+" since "+e.getMessage());
648
                try {
649
                    EventlogFactory.createIndexEventLog().write(event);
650
                } catch (Exception ee) {
651
                    log.error("SolrIndex.removeFromIndex - IndexEventLog can't log the index deleting event :"+ee.getMessage());
652
                }*/
653
                throw e;
654
                
655
            } catch (IOException e) {
656
                /*event.setAction(Event.DELETE);
657
                event.setDescription("Failurely remove the solr index for the id "+identifier.getValue()+" since "+e.getMessage());
658
                try {
659
                    EventlogFactory.createIndexEventLog().write(event);
660
                } catch (Exception ee) {
661
                    log.error("SolrIndex.removeFromIndex - IndexEventLog can't log the index deleting event :"+ee.getMessage());
662
                }*/
663
                throw e;
664
            }
665
            
666
        }
667
    }
668

    
669
    /*
670
     * Is the pid a resource map
671
     */
672
    private boolean isDataPackage(String pid) throws FileNotFoundException, ServiceFailure {
673
        boolean isDataPackage = false;
674
        SystemMetadata sysmeta = DistributedMapsFactory.getSystemMetadata(pid);
675
        if(sysmeta != null) {
676
            isDataPackage = IndexGenerator.isResourceMap(sysmeta.getFormatId());
677
        }
678
        return isDataPackage;
679
    }
680

    
681
    private boolean isPartOfDataPackage(String pid) throws XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, SAXException {
682
        SolrDoc dataPackageIndexDoc = ResourceMapSubprocessor.getSolrDoc(pid);
683
        if (dataPackageIndexDoc != null) {
684
            String resourceMapId = dataPackageIndexDoc
685
                    .getFirstFieldValue(SolrElementField.FIELD_RESOURCEMAP);
686
            return StringUtils.isNotEmpty(resourceMapId);
687
        } else {
688
            return false;
689
        }
690
    }
691

    
692
    /**
693
     * Get the solrServer
694
     * @return
695
     */
696
    public SolrServer getSolrServer() {
697
        return solrServer;
698
    }
699

    
700
    /**
701
     * Set the solrServer. 
702
     * @param solrServer
703
     */
704
    public void setSolrServer(SolrServer solrServer) {
705
        this.solrServer = solrServer;
706
    }
707
    
708
    /**
709
     * Get all indexed ids in the solr server. 
710
     * @return an empty list if there is no index.
711
     * @throws SolrServerException
712
     */
713
    public List<String> getSolrIds() throws SolrServerException {
714
        List<String> list = new ArrayList<String>();
715
        SolrQuery query = new SolrQuery(IDQUERY); 
716
        query.setRows(Integer.MAX_VALUE); 
717
        query.setFields(ID); 
718
        QueryResponse response = solrServer.query(query); 
719
        SolrDocumentList docs = response.getResults();
720
        if(docs != null) {
721
            for(SolrDocument doc :docs) {
722
                String identifier = (String)doc.getFieldValue(ID);
723
                //System.out.println("======================== "+identifier);
724
                list.add(identifier);
725
            }
726
        }
727
        return list;
728
    }
729
}
(5-5/6)