Project

General

Profile

« Previous | Next » 

Revision 8893

Added by Jing Tao almost 10 years ago

when we remove a slor index of a resource map, we don't need to know the content of the resource map. Instead, we will search the solr index to get information.

View differences:

metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/resourcemap/ResourceMapSubprocessor.java
16 16
package edu.ucsb.nceas.metacat.index.resourcemap;
17 17

  
18 18
import java.io.IOException;
19
import java.net.MalformedURLException;
19 20
import java.util.ArrayList;
20 21
import java.util.Date;
21 22
import java.util.HashMap;
......
63 64
public class ResourceMapSubprocessor extends AbstractDocumentSubprocessor implements IDocumentSubprocessor {
64 65

  
65 66
    private static final String QUERY ="q=id:";
67
    private static final String QUERY2="q="+SolrElementField.FIELD_RESOURCEMAP+":";
66 68
    private static Log log = LogFactory.getLog(SolrIndex.class);
67 69
    private static SolrServer solrServer =  null;
68 70
    static {
......
144 146
	public static SolrDoc getSolrDoc(String id) throws SolrServerException,
145 147
			IOException, ParserConfigurationException, SAXException,
146 148
			XPathExpressionException, NotImplemented, NotFound, UnsupportedType {
149
	    int targetIndex = 0;
147 150
		SolrDoc doc = null;
148

  
149
		if (solrServer != null) {
150
			String query = QUERY + "\"" + id + "\"";
151
			SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
152
			QueryResponse qr = solrServer.query(solrParams);
153
			if (qr.getResults().size() > 0) {
154
				SolrDocument orig = qr.getResults().get(0);
155
				doc = new SolrDoc();
156
				IndexSchema indexSchema = SolrQueryServiceController.getInstance().getSchema();
157
				for (String fieldName : orig.getFieldNames()) {
158
					// don't transfer the copyTo fields, otherwise there are errors
159
					if (indexSchema.isCopyFieldTarget(indexSchema.getField(fieldName))) {
160
						continue;
161
					}
162
					for (Object value : orig.getFieldValues(fieldName)) {
163
						String stringValue = value.toString();
164
						// special handling for dates in ISO 8601
165
						if (value instanceof Date) {
166
							stringValue = DateTimeMarshaller.serializeDateToUTC((Date) value);
167
							SolrDateConverter converter = new SolrDateConverter();
168
							stringValue = converter.convert(stringValue);
169
						}
170
						SolrElementField field = new SolrElementField(fieldName, stringValue);
171
						log.debug("Adding field: " + fieldName);
172
						doc.addField(field);
173
					}
174
				}
175
			}
176

  
177
		}
151
		String query = QUERY + "\"" + id + "\"";
152
	    List<SolrDoc> list = getDocumentsByQuery(query);
153
	    if(list != null && !list.isEmpty()) {
154
	        doc = list.get(targetIndex);
155
	    }
178 156
		return doc;
179 157
	}
158
	
159
	/**
160
	 * Get a list of solr documents which's resourcemap field matches the given value.
161
	 * @param resourceMapId - the target resource map id
162
	 * @return the list of solr document 
163
	 * @throws MalformedURLException
164
	 * @throws UnsupportedType
165
	 * @throws NotFound
166
	 * @throws SolrServerException
167
	 * @throws ParserConfigurationException
168
	 * @throws IOException
169
	 * @throws SAXException
170
	 */
171
	public static List<SolrDoc> getDocumentsByResourceMap(String resourceMapId) throws MalformedURLException, 
172
	            UnsupportedType, NotFound, SolrServerException, ParserConfigurationException, IOException, SAXException {
173
	    String query = QUERY2 + "\"" + resourceMapId + "\"";
174
	    return getDocumentsByQuery(query);
175
	}
176
	
177
	/**
178
	 * Get a list of slor docs which match the query.
179
	 * @param query - a string of a query
180
	 * @return
181
	 * @throws SolrServerException
182
	 * @throws MalformedURLException
183
	 * @throws UnsupportedType
184
	 * @throws NotFound
185
	 * @throws ParserConfigurationException
186
	 * @throws IOException
187
	 * @throws SAXException
188
	 */
189
	public static List<SolrDoc> getDocumentsByQuery(String query) throws SolrServerException, MalformedURLException, UnsupportedType, 
190
	                                                                NotFound, ParserConfigurationException, IOException, SAXException {
191
	    List<SolrDoc> docs = new ArrayList<SolrDoc>();
192
	    if (solrServer != null && query != null && !query.trim().equals("")) {
193
            SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
194
            QueryResponse qr = solrServer.query(solrParams);
195
            if (qr != null && qr.getResults() != null) {
196
                for(int i=0; i<qr.getResults().size(); i++) {
197
                    SolrDocument orig = qr.getResults().get(i);
198
                    SolrDoc doc = new SolrDoc();
199
                    IndexSchema indexSchema = SolrQueryServiceController.getInstance().getSchema();
200
                    for (String fieldName : orig.getFieldNames()) {
201
                        // don't transfer the copyTo fields, otherwise there are errors
202
                        if (indexSchema.isCopyFieldTarget(indexSchema.getField(fieldName))) {
203
                            continue;
204
                        }
205
                        for (Object value : orig.getFieldValues(fieldName)) {
206
                            String stringValue = value.toString();
207
                            // special handling for dates in ISO 8601
208
                            if (value instanceof Date) {
209
                                stringValue = DateTimeMarshaller.serializeDateToUTC((Date) value);
210
                                SolrDateConverter converter = new SolrDateConverter();
211
                                stringValue = converter.convert(stringValue);
212
                            }
213
                            SolrElementField field = new SolrElementField(fieldName, stringValue);
214
                            log.debug("Adding field: " + fieldName);
215
                            doc.addField(field);
216
                        }
217
                    }
218
                    docs.add(doc);
219
                }
220
                
221
            }
222
	    }
223
	    return docs;
224
	}
180 225

  
181 226

  
182 227
}
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SolrIndex.java
23 23
import java.io.FileNotFoundException;
24 24
import java.io.IOException;
25 25
import java.io.InputStream;
26
import java.net.MalformedURLException;
26 27
import java.util.ArrayList;
27 28
import java.util.Calendar;
29
import java.util.Collection;
28 30
import java.util.Date;
29 31
import java.util.HashMap;
30 32
import java.util.Iterator;
......
40 42
import javax.xml.xpath.XPathFactory;
41 43

  
42 44
import org.apache.commons.codec.EncoderException;
45
import org.apache.commons.collections.CollectionUtils;
43 46
import org.apache.commons.io.output.ByteArrayOutputStream;
44 47
import org.apache.commons.lang.StringUtils;
45 48
import org.apache.commons.logging.Log;
......
513 516
     *    index for the doc.
514 517
     */
515 518
    public void update(Identifier pid, SystemMetadata systemMetadata) {
519
        if(systemMetadata==null || pid==null) {
520
            log.error("SolrIndex.update - the systemMetadata or pid is null. So nothing will be indexed.");
521
            return;
522
        }
516 523
        String objectPath = null;
517 524
        try {
518
            objectPath = DistributedMapsFactory.getObjectPathMap().get(pid);
525
            if(!systemMetadata.getArchived()) {
526
                objectPath = DistributedMapsFactory.getObjectPathMap().get(pid);
527
            }
519 528
            update(pid, systemMetadata, objectPath);
520 529
            EventlogFactory.createIndexEventLog().remove(pid);
521 530
        } catch (Exception e) {
......
524 533
            log.error(error, e);
525 534
        }
526 535
    }
527
    /**
528
     * This method is used to delete solr index for a resourceMap. The file of resource map
529
     * probably was deleted. But the byte array is the content.
530
     * @param pid
531
     * @param systemMetadata
532
     * @param resourceMapData
533
     */
534
    public void updateResourceMap(Identifier pid, SystemMetadata systemMetadata, byte[] resourceMapData) {
535
    	if(systemMetadata != null && pid !=null && pid.getValue()!= null) {
536
    		try {
537
    			boolean archived = systemMetadata.getArchived() != null && systemMetadata.getArchived();
538
        		if(archived && isDataPackage(pid.getValue(), systemMetadata)) {
539
        			removeDataPackage(pid.getValue(), resourceMapData);
540
        		}
541
    		}catch (Exception e) {
542
                String error = "SolrIndex.updateResourceMap - could not update the solr index since " + e.getMessage();
543
                writeEventLog(systemMetadata, pid, error);
544
                log.error(error, e);
545
            }
546
    		
547
    	}
548
    }
536
   
549 537
    
550 538
    /**
551 539
     * Update the solr index. This method handles the three scenarios:
......
572 560
    void update(Identifier pid, SystemMetadata systemMetadata, String objectPath) throws SolrServerException, 
573 561
                                ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, 
574 562
                                IOException, SAXException, ParserConfigurationException, OREParserException, JiBXException, EncoderException {
575
        checkParams(pid, systemMetadata, objectPath);
563
        //checkParams(pid, systemMetadata, objectPath);
564
        if(systemMetadata==null || pid==null) {
565
            log.error("SolrIndex.update - the systemMetadata or pid is null. So nothing will be indexed.");
566
            return;
567
        }
576 568
        boolean isArchive = systemMetadata.getArchived() != null && systemMetadata.getArchived();
577 569
        if(isArchive ) {
578 570
            //delete the index for the archived objects
......
634 626
    }
635 627
    
636 628
    /*
637
     * Remove a data package for given pid. The content of package will be obtained from a file.
629
     * Remove the resource map from the solr index. It doesn't only remove the index for itself and also
630
     * remove the relationship for the related metadata and data objects.
638 631
     */
639
    private void removeDataPackage(String pid) throws ServiceFailure, SAXException, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, OREParserException {
640
    	Document resourceMapDoc = generateXmlDocument(DistributedMapsFactory.getDataObject(pid));
641
    	removeDataPackage(pid, resourceMapDoc);
632
    private void removeDataPackage(String pid) throws  XPathExpressionException, IOException, 
633
            SolrServerException, UnsupportedType, NotFound, ParserConfigurationException, SAXException {
634
        removeFromIndex(pid);
635
        List<SolrDoc> docsToUpdate = getUpdatedSolrDocsByRemovingResourceMap(pid);
636
        if (docsToUpdate != null && !docsToUpdate.isEmpty()) {
637
            //SolrElementAdd addCommand = new SolrElementAdd(docsToUpdate);
638
            //httpService.sendUpdate(solrIndexUri, addCommand);
639
            for(SolrDoc doc : docsToUpdate) {
640
                removeFromIndex(doc.getIdentifier());
641
                insertToIndex(doc);
642
            }
643
        }
644

  
642 645
    }
643
    
646

  
644 647
    /*
645
     * Remove a data package for given pid. The content of package will be the byte array.
648
     * Get the list of the solr doc which need to be updated because the removal of the resource map
646 649
     */
647
    private void removeDataPackage(String pid, byte[] resourceMap) throws ServiceFailure, SAXException, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, OREParserException{
648
    	Document resourceMapDoc = generateXmlDocument(new ByteArrayInputStream(resourceMap));
649
    	removeDataPackage(pid, resourceMapDoc);
650
    private List<SolrDoc> getUpdatedSolrDocsByRemovingResourceMap(String resourceMapId)
651
            throws UnsupportedType, NotFound, SolrServerException, ParserConfigurationException, SAXException, MalformedURLException, IOException, XPathExpressionException {
652
        List<SolrDoc> updatedSolrDocs = null;
653
        if (resourceMapId != null && !resourceMapId.trim().equals("")) {
654
            /*List<SolrDoc> docsContainResourceMap = httpService.getDocumentsByResourceMap(
655
                    solrQueryUri, resourceMapId);*/
656
            List<SolrDoc> docsContainResourceMap = ResourceMapSubprocessor.getDocumentsByResourceMap(resourceMapId);
657
            updatedSolrDocs = removeResourceMapRelationship(docsContainResourceMap,
658
                    resourceMapId);
659
        }
660
        return updatedSolrDocs;
650 661
    }
662

  
663
    /*
664
     * Get the list of the solr doc which need to be updated because the removal of the resource map
665
     */
666
    private List<SolrDoc> removeResourceMapRelationship(List<SolrDoc> docsContainResourceMap,
667
            String resourceMapId) throws XPathExpressionException, IOException {
668
        List<SolrDoc> totalUpdatedSolrDocs = new ArrayList<SolrDoc>();
669
        if (docsContainResourceMap != null && !docsContainResourceMap.isEmpty()) {
670
            for (SolrDoc doc : docsContainResourceMap) {
671
                List<SolrDoc> updatedSolrDocs = new ArrayList<SolrDoc>();
672
                List<String> resourceMapIdStrs = doc
673
                        .getAllFieldValues(SolrElementField.FIELD_RESOURCEMAP);
674
                List<String> dataIdStrs = doc
675
                        .getAllFieldValues(SolrElementField.FIELD_DOCUMENTS);
676
                List<String> metadataIdStrs = doc
677
                        .getAllFieldValues(SolrElementField.FIELD_ISDOCUMENTEDBY);
678
                if ((dataIdStrs == null || dataIdStrs.isEmpty())
679
                        && (metadataIdStrs == null || metadataIdStrs.isEmpty())) {
680
                    // only has resourceMap field, doesn't have either documentBy or documents fields.
681
                    // so we only remove the resource map field.
682
                    doc.removeFieldsWithValue(SolrElementField.FIELD_RESOURCEMAP, resourceMapId);
683
                    updatedSolrDocs.add(doc);
684
                } else if ((dataIdStrs != null && !dataIdStrs.isEmpty())
685
                        && (metadataIdStrs == null || metadataIdStrs.isEmpty())) {
686
                    //The solr doc is for a metadata object since the solr doc documents data files
687
                    updatedSolrDocs = removeAggregatedItems(resourceMapId, doc, resourceMapIdStrs,
688
                            dataIdStrs, SolrElementField.FIELD_DOCUMENTS);
689
                } else if ((dataIdStrs == null || dataIdStrs.isEmpty())
690
                        && (metadataIdStrs != null && !metadataIdStrs.isEmpty())) {
691
                    //The solr doc is for a data object since it documentedBy elements.
692
                    updatedSolrDocs = removeAggregatedItems(resourceMapId, doc, resourceMapIdStrs,
693
                            metadataIdStrs, SolrElementField.FIELD_ISDOCUMENTEDBY);
694
                } else if ((dataIdStrs != null && !dataIdStrs.isEmpty())
695
                        && (metadataIdStrs != null && !metadataIdStrs.isEmpty())){
696
                    // both metadata and data for one object
697
                    List<SolrDoc> solrDocsRemovedDocuments = removeAggregatedItems(resourceMapId, doc, resourceMapIdStrs,
698
                            dataIdStrs, SolrElementField.FIELD_DOCUMENTS);
699
                    List<SolrDoc> solrDocsRemovedDocumentBy = removeAggregatedItems(resourceMapId, doc, resourceMapIdStrs,
700
                            metadataIdStrs, SolrElementField.FIELD_ISDOCUMENTEDBY);
701
                    updatedSolrDocs = mergeUpdatedSolrDocs(solrDocsRemovedDocumentBy, solrDocsRemovedDocuments);
702
                }
703
                //move them to the final result
704
                if(updatedSolrDocs != null) {
705
                    for(SolrDoc updatedDoc: updatedSolrDocs) {
706
                        totalUpdatedSolrDocs.add(updatedDoc);
707
                    }
708
                }
709
                
710
            }
711

  
712
        }
713
        return totalUpdatedSolrDocs;
714
    }
651 715
    
652 716
    /*
653
     * Remove a resource map pid
717
     * Process the list of ids of the documentBy/documents in a slor doc.
654 718
     */
655
    private void removeDataPackage(String pid,Document resourceMapDoc) throws ServiceFailure, SAXException, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, OREParserException  {
656
       
657
        //ResourceMap resourceMap = new ResourceMap(resourceMapDoc);
658
        ResourceMap resourceMap = ResourceMapFactory.buildResourceMap(resourceMapDoc);
659
        List<String> documentIds = resourceMap.getAllDocumentIDs();
660
        List<SolrDoc> indexDocuments =ResourceMapSubprocessor.getSolrDocs(documentIds);
661
        removeFromIndex(pid);
662
        //List<SolrDoc> docsToUpdate = new ArrayList<SolrDoc>();
663
        // for each document in data package:
664
        for (SolrDoc indexDoc : indexDocuments) {
719
    private List<SolrDoc> removeAggregatedItems(String targetResourceMapId, SolrDoc doc,
720
            List<String> resourceMapIdsInDoc, List<String> aggregatedItemsInDoc, String fieldNameRemoved) {
721
        List<SolrDoc> updatedSolrDocs = new ArrayList<SolrDoc>();
722
        if (doc != null && resourceMapIdsInDoc != null && aggregatedItemsInDoc != null
723
                && fieldNameRemoved != null) {
724
            if (resourceMapIdsInDoc.size() == 1) {
725
                //only has one resource map. remove the resource map. also remove the documentBy
726
                doc.removeFieldsWithValue(SolrElementField.FIELD_RESOURCEMAP, targetResourceMapId);
727
                doc.removeAllFields(fieldNameRemoved);
728
                updatedSolrDocs.add(doc);
729
            } else if (resourceMapIdsInDoc.size() > 1) {
730
                //we have multiple resource maps. We should match them.                     
731
                Map<String, String> ids = matchResourceMapsAndItems(doc.getIdentifier(),
732
                        targetResourceMapId, resourceMapIdsInDoc, aggregatedItemsInDoc, fieldNameRemoved);
733
                if (ids != null) {
734
                    for (String id : ids.keySet()) {
735
                        doc.removeFieldsWithValue(fieldNameRemoved, id);
736
                    }
737
                }
738
                doc.removeFieldsWithValue(SolrElementField.FIELD_RESOURCEMAP,
739
                        targetResourceMapId);
740
                updatedSolrDocs.add(doc);
741
                /*if (aggregatedItemsInDoc.size() > 1) {
742
                    
665 743

  
666
            if (indexDoc.getIdentifier().equals(pid)) {
667
                continue; // skipping the resource map, no need update
668
                          // it.
669
                          // will
670
                          // be removed.
744
                } else {
745
                    //multiple resource map aggregate same metadata and data. Just remove the resource map
746
                    doc.removeFieldsWithValue(SolrElementField.FIELD_RESOURCEMAP,
747
                            targetResourceMapId);
748
                    updatedSolrDocs.add(doc);
749
                }*/
671 750
            }
751
        }
752
        return updatedSolrDocs;
753
    }
672 754

  
673
            // Remove resourceMap reference
674
            indexDoc.removeFieldsWithValue(SolrElementField.FIELD_RESOURCEMAP,
675
                    resourceMap.getIdentifier());
755
    /*
756
     * Return a map of mapping aggregation id map the target resourceMapId.
757
     * This will look the aggregation information in another side - If the targetId
758
     * is a metadata object, we will look the data objects which it describes; If 
759
     * the targetId is a data object, we will look the metadata object which documents it.
760
     */
761
    private Map<String, String> matchResourceMapsAndItems(String targetId,
762
            String targetResourceMapId, List<String> originalResourceMaps, List<String> aggregatedItems, String fieldName) {
763
        Map<String, String> map = new HashMap<String, String>();
764
        if (targetId != null && targetResourceMapId != null && aggregatedItems != null
765
                && fieldName != null) {
766
            String newFieldName = null;
767
            if (fieldName.equals(SolrElementField.FIELD_ISDOCUMENTEDBY)) {
768
                newFieldName = SolrElementField.FIELD_DOCUMENTS;
769
            } else if (fieldName.equals(SolrElementField.FIELD_DOCUMENTS)) {
770
                newFieldName = SolrElementField.FIELD_ISDOCUMENTEDBY;
771
            }
772
            if (newFieldName != null) {
773
                for (String item : aggregatedItems) {
774
                    SolrDoc doc = null;
775
                    try {
776
                        doc = getDocumentById(item);
777
                        List<String> fieldValues = doc.getAllFieldValues(newFieldName);
778
                        List<String> resourceMapIds = doc
779
                                .getAllFieldValues(SolrElementField.FIELD_RESOURCEMAP);
780
                        if ((fieldValues != null && fieldValues.contains(targetId))
781
                                && (resourceMapIds != null && resourceMapIds
782
                                        .contains(targetResourceMapId))) {
783
                            //okay, we found the target aggregation item id and the resource map id
784
                            //in this solr doc. However, we need check if another resource map with different
785
                            //id but specify the same relationship. If we have the id(s), we should not
786
                            // remove the documents( or documentBy) element since we need to preserve the 
787
                            // relationship for the remain resource map. 
788
                            boolean hasDuplicateIds = false;
789
                            if(originalResourceMaps != null) {
790
                               for(String id :resourceMapIds) {
791
                                    if (originalResourceMaps.contains(id) && !id.equals(targetResourceMapId)) {
792
                                        hasDuplicateIds = true;
793
                                        break;
794
                                    }
795
                                }
796
                            }
797
                            if(!hasDuplicateIds) {
798
                                map.put(item, targetResourceMapId);
799
                            }
800
                            
801
                        }
802
                    } catch (Exception e) {
803
                        log.warn("SolrIndex.matchResourceMapsAndItems - can't get the solrdoc for the id "
804
                                + item + " since " + e.getMessage());
805
                    }
806
                }
807
            }
808
        }
809
        return map;
810
    }
676 811

  
677
            // // Remove documents/documentedby values for this resource
678
            // map
679
            for (ResourceEntry entry : resourceMap.getMappedReferences()) {
680
                if (indexDoc.getIdentifier().equals(entry.getIdentifier())) {
681
                    for (String documentedBy : entry.getDocumentedBy()) {
682
                        // Using removeOneFieldWithValue in-case same
683
                        // documents
684
                        // are in more than one data package. just
685
                        // remove
686
                        // one
687
                        // instance of data package info.
688
                        indexDoc.removeOneFieldWithValue(SolrElementField.FIELD_ISDOCUMENTEDBY,
689
                                documentedBy);
812
    /*
813
     * Get the solr index doc from the index server for the given id.
814
     */
815
    private SolrDoc getDocumentById(String id) throws NotImplemented, NotFound, UnsupportedType, 
816
                SolrServerException, ParserConfigurationException, SAXException, XPathExpressionException, IOException {
817
        SolrDoc doc = ResourceMapSubprocessor.getSolrDoc(id);
818
        return doc;
819
    }
820
    
821
    /*
822
     * Merge two list of updated solr docs. removedDocumentBy has the correct information about documentBy element.
823
     * removedDocuments has the correct information about the documents element.
824
     * So we go through the two list and found the two docs having the same identifier.
825
     * Get the list of the documents value from the one in the removedDoucments (1). 
826
     * Remove all values of documents from the one in the removedDocumentBy. 
827
     * Then copy the list of documents value from (1) to to the one in the removedDocumentBy.
828
     */
829
    private List<SolrDoc> mergeUpdatedSolrDocs(List<SolrDoc>removedDocumentBy, List<SolrDoc>removedDocuments) {
830
        List<SolrDoc> mergedDocuments = new ArrayList<SolrDoc>();
831
        if(removedDocumentBy == null || removedDocumentBy.isEmpty()) {
832
            mergedDocuments = removedDocuments;
833
        } else if (removedDocuments == null || removedDocuments.isEmpty()) {
834
            mergedDocuments = removedDocumentBy;
835
        } else {
836
            int sizeOfDocBy = removedDocumentBy.size();
837
            int sizeOfDocs = removedDocuments.size();
838
            for(int i=sizeOfDocBy-1; i>= 0; i--) {
839
                SolrDoc docInRemovedDocBy = removedDocumentBy.get(i);
840
                for(int j= sizeOfDocs-1; j>=0; j--) {
841
                    SolrDoc docInRemovedDocs = removedDocuments.get(j);
842
                    if(docInRemovedDocBy.getIdentifier().equals(docInRemovedDocs.getIdentifier())) {
843
                        //find the same doc in both list. let's merge them.
844
                        //first get all the documents element from the docWithDocs(it has the correct information about the documents element)
845
                        List<String> idsInDocuments = docInRemovedDocs.getAllFieldValues(SolrElementField.FIELD_DOCUMENTS);
846
                        docInRemovedDocBy.removeAllFields(SolrElementField.FIELD_DOCUMENTS);//clear out any documents element in docInRemovedDocBy
847
                        //add the Documents element from the docInRemovedDocs if it has any.
848
                        // The docInRemovedDocs has the correct information about the documentBy. Now it copied the correct information of the documents element.
849
                        // So docInRemovedDocs has both correct information about the documentBy and documents elements.
850
                        if(idsInDocuments != null) {
851
                            for(String id : idsInDocuments) {
852
                                if(id != null && !id.trim().equals("")) {
853
                                    docInRemovedDocBy.addField(new SolrElementField(SolrElementField.FIELD_DOCUMENTS, id));
854
                                }
855
                                
856
                            }
857
                        }
858
                        //intersect the resource map ids.
859
                        List<String> resourceMapIdsInWithDocs = docInRemovedDocs.getAllFieldValues(SolrElementField.FIELD_RESOURCEMAP);
860
                        List<String> resourceMapIdsInWithDocBy = docInRemovedDocBy.getAllFieldValues(SolrElementField.FIELD_RESOURCEMAP);
861
                        docInRemovedDocBy.removeAllFields(SolrElementField.FIELD_RESOURCEMAP);
862
                        Collection resourceMapIds = CollectionUtils.union(resourceMapIdsInWithDocs, resourceMapIdsInWithDocBy);
863
                        if(resourceMapIds != null) {
864
                            for(Object idObj : resourceMapIds) {
865
                                String id = (String)idObj;
866
                                docInRemovedDocBy.addField(new SolrElementField(SolrElementField.FIELD_RESOURCEMAP, id));
867
                            }
868
                        }
869
                        //we don't need do anything about the documentBy elements since the docInRemovedDocBy has the correct information.
870
                        mergedDocuments.add(docInRemovedDocBy);
871
                        //delete the two documents from the list
872
                        removedDocumentBy.remove(i);
873
                        removedDocuments.remove(j);
874
                        break;
690 875
                    }
691
                    for (String documents : entry.getDocuments()) {
692
                        indexDoc.removeOneFieldWithValue(SolrElementField.FIELD_DOCUMENTS,
693
                                documents);
694
                    }
695
                    break;
876
                    
696 877
                }
697 878
            }
698
            removeFromIndex(indexDoc.getIdentifier());
699
            insertToIndex(indexDoc);
700
            //docsToUpdate.add(indexDoc);
879
            // when we get there, if the two lists are empty, this will be a perfect merge. However, if something are left. we 
880
            //just put them in.
881
            for(SolrDoc doc: removedDocumentBy) {
882
                mergedDocuments.add(doc);
883
            }
884
            for(SolrDoc doc: removedDocuments) {
885
                mergedDocuments.add(doc);
886
            }
701 887
        }
702
        //SolrElementAdd addCommand = new SolrElementAdd(docsToUpdate);
703
        //httpService.sendUpdate(solrIndexUri, addCommand);
888
        return mergedDocuments;
704 889
    }
890
    
705 891

  
706 892
    /*
707 893
     * Remove a pid which is part of resource map.
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SystemMetadataEventListener.java
110 110
    }
111 111

  
112 112
    public void entryRemoved(EntryEvent<Identifier, IndexTask> entryEvent) {
113
    	// do nothing
113
        // do nothing
114 114
    }
115 115
    public void entryEvicted(EntryEvent<Identifier, IndexTask> entryEvent) {
116 116
    	// do nothing
......
131 131
		IndexTask task = entryEvent.getValue();
132 132
		SystemMetadata systemMetadata = task.getSystemMetadata();
133 133
		Map<String, List<Object>> fields = task.getFields();
134
		byte[] resourceMapData = task.getResourceMapData();
135 134
		
136 135
		/*if(systemMetadata == null) {
137 136
		    writeEventLog(systemMetadata, pid, "SystemMetadataEventListener.itemAdded -could not get the SystemMetadata");
......
144 143
		}
145 144
		
146 145
		if (systemMetadata != null) {
147
			if(resourceMapData == null) {
148
				solrIndex.update(pid, systemMetadata);
149
			} else {
150
				solrIndex.updateResourceMap(pid, systemMetadata, resourceMapData);
151
			}
146
		    solrIndex.update(pid, systemMetadata);
152 147
			
153 148
		}
154 149
		if (fields != null) {

Also available in: Unified diff