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.jibx.runtime.JiBXException;
77
import org.w3c.dom.Document;
78
import org.xml.sax.SAXException;
79

    
80
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
81
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
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
                IndexEvent event = new IndexEvent();
394
                event.setDate(Calendar.getInstance().getTime());
395
                Identifier pid = new Identifier();
396
                pid.setValue(doc.getIdentifier());
397
                event.setIdentifier(pid);
398
                try {
399
                    UpdateResponse response = solrServer.add(solrDoc);
400
                    solrServer.commit();
401
                    /*event.setType(IndexEvent.SUCCESSINSERT);
402
                    event.setDescription("Successfully insert the solr index for the id "+pid.getValue());
403
                    try {
404
                        EventlogFactory.createIndexEventLog().write(event);
405
                    } catch (Exception e) {
406
                        log.error("SolrIndex.insertToIndex - IndexEventLog can't log the index inserting event :"+e.getMessage());
407
                    }*/
408
                } catch (SolrServerException e) {
409
                    event.setAction(Event.CREATE);
410
                    event.setDescription("Failed to insert the solr index for the id "+pid.getValue()+" since "+e.getMessage());
411
                    try {
412
                        EventlogFactory.createIndexEventLog().write(event);
413
                    } catch (Exception ee) {
414
                        log.error("SolrIndex.insertToIndex - IndexEventLog can't log the index inserting event :"+ee.getMessage());
415
                    }
416
                    throw e;
417
                } catch (IOException e) {
418
                    event.setAction(Event.CREATE);
419
                    event.setDescription("Failed to insert the solr index for the id "+pid.getValue()+" since "+e.getMessage());
420
                    try {
421
                        EventlogFactory.createIndexEventLog().write(event);
422
                    } catch (Exception ee) {
423
                        log.error("SolrIndex.insertToIndex - IndexEventLog can't log the index inserting event :"+ee.getMessage());
424
                    }
425
                    throw e;
426
                    
427
                }
428
                //System.out.println("=================the response is:\n"+response.toString());
429
            }
430
        }
431
    }
432
    
433
    /**
434
     * Update the solr index. This method handles the three scenarios:
435
     * 1. Archive (or delete) - if the the system metadata shows the value of the archive is true,
436
     *    remove the index for the document and its previous versions if it has.
437
     * 2. Update an existing doc - if the the system metadata shows the value of the archive is false and it has an obsoletes,
438
     *    remove the index for the previous version(s) and generate new index for the doc.
439
     * 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
440
     *    index for the doc.
441
     * @param pid  the id of the document
442
     * @param obsoleteIds  the chain of the obsoletes by this id
443
     * @param systemMetadata  the system metadata associated with the data object
444
     * @param data  the data object itself
445
     * @throws SolrServerException 
446
     * @throws JiBXException 
447
     * @throws EncoderException 
448
     * @throws UnsupportedType 
449
     * @throws NotFound 
450
     * @throws NotImplemented 
451
     * @throws ServiceFailure 
452
     */
453
    public void update(String pid, List<String> obsoleteIds, SystemMetadata systemMetadata, InputStream data) 
454
                    throws IOException, SAXException, ParserConfigurationException,
455
                    XPathExpressionException, SolrServerException, JiBXException, EncoderException, NotImplemented, NotFound, UnsupportedType, ServiceFailure {
456
        checkParams(pid, systemMetadata, data);
457
        boolean isArchive = systemMetadata.getArchived();
458
        if(isArchive) {
459
            //archive(delete)
460
            Identifier obsolete = systemMetadata.getObsoletes();
461
            if(obsolete != null) {
462
                removeObsoletesChain(obsolete.getValue(), obsoleteIds);
463
            }
464
            remove(pid);
465
            log.info("============================= archive the idex for the identifier "+pid);
466
        } else {
467
            Identifier obsolete = systemMetadata.getObsoletes();
468
            if(obsolete != null) {
469
                removeObsoletesChain(obsolete.getValue(), obsoleteIds);
470
            }
471
            //generate index for either add or update.
472
            insert(pid, systemMetadata, data);
473
            log.info("============================= insert index for the identifier "+pid);
474
        }
475
    }
476
    
477
    
478
    private void removeObsoletesChain(String obsoleteId, List<String> obsoleteIdChain) throws SolrServerException, IOException, ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SAXException, ParserConfigurationException {
479
        if(obsoleteId != null && !obsoleteId.trim().equals("")) {
480
            if(obsoleteIdChain == null || obsoleteIdChain.isEmpty()) {
481
                throw new SolrServerException("SolrIndex.removeObsoletesChain - The obsoletes chain can't be null or empty since the system metadata already has the obsoletes element."); 
482
            }
483
            if(!obsoleteIdChain.contains(obsoleteId)) {
484
                throw new SolrServerException("SolrIndex.removeObsoletesChain - The obsoletes elment in the system metadata is not in the obsoleteId chain"); 
485
            }
486
            remove(obsoleteIdChain);
487
        } else {
488
            throw new SolrServerException("SolrIndex.removeObsoletesChain - The obsolete id should be null."); 
489
        }  
490
    }
491
    
492
    /**
493
     * Remove all the indexes associated with the pids in the list.
494
     * @param pidList
495
     * @throws IOException
496
     * @throws SolrServerException
497
     * @throws ParserConfigurationException 
498
     * @throws SAXException 
499
     * @throws UnsupportedType 
500
     * @throws NotFound 
501
     * @throws NotImplemented 
502
     * @throws XPathExpressionException 
503
     * @throws ServiceFailure 
504
     */
505
    private void remove(List<String> pidList) throws IOException, SolrServerException, ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SAXException, ParserConfigurationException {
506
        if(pidList != null) {
507
            for(String id : pidList) {
508
                remove(id);
509
            }
510
        }
511
    }
512
 
513
    /**
514
     * Remove the indexed associated with specified pid.
515
     * @param pid  the pid which the indexes are associated with
516
     * @throws IOException
517
     * @throws SolrServerException
518
     * @throws ParserConfigurationException 
519
     * @throws SAXException 
520
     * @throws UnsupportedType 
521
     * @throws NotFound 
522
     * @throws NotImplemented 
523
     * @throws XPathExpressionException 
524
     * @throws ServiceFailure 
525
     */
526
    public void remove(String pid) throws IOException, SolrServerException, ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SAXException, ParserConfigurationException {
527
        if (isDataPackage(pid)) {
528
            removeDataPackage(pid);
529
        } else if (isPartOfDataPackage(pid)) {
530
            removeFromDataPackage(pid);
531
        } else {
532
            removeFromIndex(pid);
533
        }
534
    }
535
    
536
   
537
   
538

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

    
552
            if (indexDoc.getIdentifier().equals(pid)) {
553
                continue; // skipping the resource map, no need update
554
                          // it.
555
                          // will
556
                          // be removed.
557
            }
558

    
559
            // Remove resourceMap reference
560
            indexDoc.removeFieldsWithValue(SolrElementField.FIELD_RESOURCEMAP,
561
                    resourceMap.getIdentifier());
562

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

    
592
    private void removeFromDataPackage(String pid) throws XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, SAXException  {
593
        SolrDoc indexedDoc = ResourceMapSubprocessor.getSolrDoc(pid);
594
        removeFromIndex(pid);
595
        List<SolrDoc> docsToUpdate = new ArrayList<SolrDoc>();
596

    
597
        List<String> documents = indexedDoc.getAllFieldValues(SolrElementField.FIELD_DOCUMENTS);
598
        for (String documentsValue : documents) {
599
            SolrDoc solrDoc = ResourceMapSubprocessor.getSolrDoc(documentsValue);
600
            solrDoc.removeFieldsWithValue(SolrElementField.FIELD_ISDOCUMENTEDBY, pid);
601
            removeFromIndex(documentsValue);
602
            insertToIndex(solrDoc);
603
        }
604

    
605
        List<String> documentedBy = indexedDoc
606
                .getAllFieldValues(SolrElementField.FIELD_ISDOCUMENTEDBY);
607
        for (String documentedByValue : documentedBy) {
608
            SolrDoc solrDoc = ResourceMapSubprocessor.getSolrDoc(documentedByValue);
609
            solrDoc.removeFieldsWithValue(SolrElementField.FIELD_DOCUMENTS, documentedByValue);
610
            //docsToUpdate.add(solrDoc);
611
            removeFromIndex(documentedByValue);
612
            insertToIndex(solrDoc);
613
        }
614

    
615
        //SolrElementAdd addCommand = new SolrElementAdd(docsToUpdate);
616
        //httpService.sendUpdate(solrIndexUri, addCommand);
617
    }
618

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

    
663
    /*
664
     * Is the pid a resource map
665
     */
666
    private boolean isDataPackage(String pid) throws FileNotFoundException, ServiceFailure {
667
        boolean isDataPackage = false;
668
        SystemMetadata sysmeta = DistributedMapsFactory.getSystemMetadata(pid);
669
        if(sysmeta != null) {
670
            isDataPackage = IndexGenerator.isResourceMap(sysmeta.getFormatId());
671
        }
672
        return isDataPackage;
673
    }
674

    
675
    private boolean isPartOfDataPackage(String pid) throws XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, SAXException {
676
        SolrDoc dataPackageIndexDoc = ResourceMapSubprocessor.getSolrDoc(pid);
677
        if (dataPackageIndexDoc != null) {
678
            String resourceMapId = dataPackageIndexDoc
679
                    .getFirstFieldValue(SolrElementField.FIELD_RESOURCEMAP);
680
            return StringUtils.isNotEmpty(resourceMapId);
681
        } else {
682
            return false;
683
        }
684
    }
685

    
686
    /**
687
     * Get the solrServer
688
     * @return
689
     */
690
    public SolrServer getSolrServer() {
691
        return solrServer;
692
    }
693

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