001package org.kuali.ole.docstore.indexer.solr; 002 003import org.apache.commons.io.FileUtils; 004import org.apache.commons.lang.StringUtils; 005import org.apache.commons.lang.time.StopWatch; 006import org.apache.solr.client.solrj.SolrQuery; 007import org.apache.solr.client.solrj.SolrServer; 008import org.apache.solr.client.solrj.SolrServerException; 009import org.apache.solr.client.solrj.impl.HttpSolrServer; 010import org.apache.solr.client.solrj.response.QueryResponse; 011import org.apache.solr.client.solrj.response.UpdateResponse; 012import org.apache.solr.client.solrj.util.ClientUtils; 013import org.apache.solr.common.SolrDocument; 014import org.apache.solr.common.SolrDocumentList; 015import org.apache.solr.common.SolrInputDocument; 016import org.apache.solr.common.SolrInputField; 017import org.kuali.ole.docstore.common.document.content.instance.Instance; 018import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection; 019import org.kuali.ole.docstore.discovery.service.SolrServerManager; 020import org.kuali.ole.docstore.discovery.solr.work.bib.WorkBibCommonFields; 021import org.kuali.ole.docstore.discovery.solr.work.bib.dublin.WorkBibDublinDocBuilder; 022import org.kuali.ole.docstore.discovery.solr.work.bib.dublin.unqualified.WorkBibDublinUnQualifiedDocBuilder; 023import org.kuali.ole.docstore.discovery.solr.work.bib.marc.WorkBibMarcDocBuilder; 024import org.kuali.ole.docstore.discovery.solr.work.instance.oleml.WorkInstanceOlemlDocBuilder; 025import org.kuali.ole.docstore.model.enums.DocCategory; 026import org.kuali.ole.docstore.model.enums.DocFormat; 027import org.kuali.ole.docstore.model.enums.DocType; 028import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument; 029import org.kuali.ole.docstore.model.xmlpojo.work.bib.dublin.WorkBibDublinRecord; 030import org.kuali.ole.docstore.model.xstream.work.bib.dublin.WorkBibDublinRecordProcessor; 031import org.kuali.ole.docstore.model.xstream.work.bib.dublin.unqualified.WorkBibDublinUnQualifiedRecordProcessor; 032import org.kuali.ole.docstore.model.xstream.work.bib.marc.WorkBibMarcRecordProcessor; 033import org.kuali.ole.docstore.utility.BatchIngestStatistics; 034import org.kuali.ole.docstore.utility.BulkIngestStatistics; 035import org.kuali.rice.core.api.config.property.ConfigContext; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039import javax.xml.stream.XMLInputFactory; 040import javax.xml.stream.XMLStreamConstants; 041import javax.xml.stream.XMLStreamReader; 042import javax.xml.transform.OutputKeys; 043import javax.xml.transform.Transformer; 044import javax.xml.transform.TransformerFactory; 045import javax.xml.transform.stax.StAXSource; 046import javax.xml.transform.stream.StreamResult; 047import java.io.*; 048import java.net.HttpURLConnection; 049import java.net.MalformedURLException; 050import java.net.URL; 051import java.util.*; 052 053/** 054 * Created with IntelliJ IDEA. 055 * User: mjagan 056 * Date: 7/2/13 057 * Time: 4:18 PM 058 * To change this template use File | Settings | File Templates. 059 */ 060public abstract class AbstractDocumentIndexer implements IndexerService { 061 private static final Logger LOG = LoggerFactory.getLogger(AbstractDocumentIndexer.class); 062 public WorkBibMarcDocBuilder workBibMarcDocBuilder1 = new WorkBibMarcDocBuilder(); 063 public long commitRecCount = 0; 064 065 066 public AbstractDocumentIndexer() { 067 init(); 068 } 069 070 protected void init() { 071 LOG.debug("IndexerServiceImpl init "); 072 // docSearchUrl = PropertyUtil.getPropertyUtil().getProperty("docSearchURL"); 073 // if ((null != docSearchUrl) && !docSearchUrl.endsWith("/")) { 074 // docSearchUrl = docSearchUrl + "/"; 075 // } 076 } 077 078 public String deleteDocuments(String docCategory, List<String> uuidList) 079 throws MalformedURLException, SolrServerException { 080 String result = deleteDocumentsByUUIDList(uuidList, docCategory); 081 return result; 082 } 083 084 public String deleteDocument(String docCategory, String uuid) { 085 String result = deleteDocumentByUUID(uuid, docCategory); 086 return result; 087 } 088 089 public String indexSolrDocuments(List<SolrInputDocument> solrDocs) { 090 return indexSolrDocuments(solrDocs, true); 091 } 092 093 public String indexSolrDocuments(List<SolrInputDocument> solrDocs, boolean commit) { 094 String result = null; 095 StopWatch timer = new StopWatch(); 096 timer.start(); 097 try { 098 result = indexSolrDocuments(solrDocs, commit, false); 099 timer.stop(); 100 LOG.info("Time taken for indexing " + solrDocs.size() + " Solr docs:" + timer.toString()); 101 } catch (Exception e) { 102 result = buildFailureMsg(null, "Indexing failed. " + e.getMessage()); 103 LOG.error(result, e); 104 } 105 return result; 106 } 107 108 @Override 109 public String indexDocumentsFromDirBySolrDoc(String docCategory, String docType, String docFormat, String dataDir) { 110 String result = null; 111 String xmlContent = ""; 112 // get the files from the dir. 113 File srcDir = new File(dataDir); 114 if ((null == srcDir) || !srcDir.isDirectory()) { 115 result = buildFailureMsg(null, "Invalid data directory:" + dataDir); 116 return result; 117 } 118 FilenameFilter filter = new FilenameFilter() { 119 public boolean accept(File dir, String name) { 120 return (!name.startsWith(".") && (name.endsWith(".xml"))); 121 } 122 }; 123 124 String[] srcFileNames = srcDir.list(filter); 125 if ((null == srcFileNames) || (srcFileNames.length == 0)) { 126 result = buildFailureMsg(null, "No data files found in data dir:" + dataDir); 127 return result; 128 } 129 List<File> fileList = new ArrayList<File>(srcFileNames.length); 130 for (int i = 0; i < srcFileNames.length; i++) { 131 File srcFile = new File(dataDir + File.separator + srcFileNames[i]); 132 fileList.add(srcFile); 133 } 134 return indexDocumentsFromFiles(docCategory, docType, docFormat, fileList); 135 } 136 137 @Override 138 public String indexDocumentsFromStringBySolrDoc(String docCategory, String docType, String docFormat, String data) 139 throws IOException { 140 141 File file = File.createTempFile("marc.xml", ".tmp"); 142 FileUtils.writeStringToFile(file, data, "UTF-8"); 143 String filePath = file.getAbsolutePath(); 144 return indexDocumentsFromFileBySolrDoc(docCategory, docType, docFormat, 145 filePath); //To change body of implemented methods use File | Settings | File Templates. 146 } 147 148 @Override 149 public String indexDocumentsFromFileBySolrDoc(String docCategory, String docType, String docFormat, 150 String filePath) { 151 List<File> fileList = new ArrayList<File>(0); 152 fileList.add(new File(filePath)); 153 return indexDocumentsFromFiles(docCategory, docType, docFormat, fileList); 154 } 155 156 157 /** 158 * Indexes the records (of the given docCategory, docType and docFormat) from the files in the given data directory. 159 * <p> 160 * This is a utility method to use Discovery separately from DocStore. 161 * </p> 162 * 163 * @param docCategory category of the documents expected in the input files 164 * @param docType type of the documents expected in the input files 165 * @param docFormat format of the documents expected in the input files 166 * @param fileList list of files to be indexed 167 * @return SUCCESS or FAILURE 168 */ 169 @Override 170 public String indexDocumentsFromFiles(String docCategory, String docType, String docFormat, List<File> fileList) { 171 // TODO: Modify this method so that if dataDir is a file, it should be indexed. 172 String result = null; 173 String xmlContent = ""; 174 try { 175 StopWatch indexingTimer = new StopWatch(); 176 StopWatch conversionTimer = new StopWatch(); 177 StopWatch fileIOTimer = new StopWatch(); 178 StopWatch totalTimer = new StopWatch(); 179 totalTimer.start(); 180 fileIOTimer.start(); 181 fileIOTimer.suspend(); 182 183 if ((null == fileList) || (fileList.size() == 0)) { 184 result = buildFailureMsg(null, "No files found in data dir:" + fileList); 185 return result; 186 } 187 int numFiles = fileList.size(); 188 int numDocs = 0; 189 SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 190 TransformerFactory tf = new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl(); 191 Transformer t = tf.newTransformer(); 192 t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 193 t.setOutputProperty(OutputKeys.INDENT, "yes"); 194 conversionTimer.start(); 195 conversionTimer.suspend(); 196 indexingTimer.start(); 197 indexingTimer.suspend(); 198 for (int i = 0; i < fileList.size(); i++) { 199 File srcFile = fileList.get(i); 200 LOG.info("Processing File: " + srcFile.getAbsolutePath()); 201 String srcFileName = srcFile.getName(); 202 203 // Get the id of the doc from the file name if Exists. 204 String idFromFileName = null; 205 List<String> idFromFileNameList = null; 206 int suffixIndex = srcFileName.indexOf(UUID_FILE_NAME_SUFFIX); 207 if (suffixIndex > 0) { 208 idFromFileName = srcFileName.substring(0, suffixIndex); 209 idFromFileNameList = new ArrayList<String>(1); 210 idFromFileNameList.add(idFromFileName); 211 } 212 213 int recordsProcessedInFile = 0; 214 try { 215 XMLInputFactory xif = XMLInputFactory.newInstance(); 216 XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader(srcFile)); 217 xsr.nextTag(); 218 recordsProcessedInFile = 0; 219 List<SolrInputDocument> solrDocsToAdd = new ArrayList<SolrInputDocument>(); 220 List<SolrInputDocument> solrDocs = null; 221 while (xsr.hasNext()) { 222 int eventType = xsr.next(); 223 if (eventType == XMLStreamConstants.START_ELEMENT) { 224 if (DocFormat.MARC.isEqualTo(docFormat)) { 225 recordsProcessedInFile++; 226 LOG.debug("Processing Record(" + recordsProcessedInFile + ") of File: " + srcFileName); 227 fileIOTimer.resume(); 228 StringWriter str = new StringWriter(); 229 str.append("<collection>"); 230 t.transform(new StAXSource(xsr), new StreamResult(str)); 231 str.append("</collection>"); 232 xmlContent = str.getBuffer().toString(); 233 fileIOTimer.suspend(); 234 conversionTimer.resume(); 235 solrDocs = convertToSolrDocs(docCategory, docType, docFormat, xmlContent); 236 if ((null == solrDocs) || (solrDocs.size() == 0)) { 237 continue; 238 } 239 if (idFromFileName == null) { 240 assignUUIDs(solrDocs, null); 241 } else { 242 assignUUIDs(solrDocs.subList(0, 1), idFromFileNameList); 243 } 244 conversionTimer.suspend(); 245 numDocs += solrDocs.size(); 246 } else if (DocFormat.DUBLIN_CORE.isEqualTo(docFormat)) { 247 // TODO: May be moved out of while loop? 248 conversionTimer.resume(); 249 solrDocs = convertToSolrDocs(docCategory, docType, docFormat, 250 FileUtils.readFileToString(srcFile, "UTF-8")); 251 assignUUIDs(solrDocs, null); 252 conversionTimer.suspend(); 253 solrDocsToAdd.addAll(solrDocs); 254 numDocs += solrDocs.size(); 255 break; 256 } else if (DocFormat.DUBLIN_UNQUALIFIED.isEqualTo(docFormat)) { 257 if (xsr.getName().getLocalPart().equalsIgnoreCase("record")) { 258 conversionTimer.resume(); 259 solrDocs = new ArrayList<SolrInputDocument>(); 260 StringWriter str = new StringWriter(); 261 str.append("<OAI-PMH><ListRecords>"); 262 t.transform(new StAXSource(xsr), new StreamResult(str)); 263 str.append("</ListRecords></OAI-PMH>"); 264 str.close(); 265 xmlContent = str.getBuffer().toString(); 266 solrDocs = convertToSolrDocs(docCategory, docType, docFormat, xmlContent); 267 str.flush(); 268 assignUUIDs(solrDocs, null); 269 conversionTimer.suspend(); 270 numDocs += solrDocs.size(); 271 } 272 } else { 273 throw new Exception("Unsupported Document Format: " + docFormat); 274 } 275 } else { 276 continue; 277 } 278 279 if (solrDocs != null) { 280 solrDocsToAdd.addAll(solrDocs); 281 } 282 if (solrDocsToAdd.size() < 500) { 283 // TODO: Handle the case when the size of the batch is too high. Do a check on the size. 284 continue; 285 } 286 indexingTimer.resume(); 287 solr.add(solrDocsToAdd); 288 indexingTimer.suspend(); 289 solrDocsToAdd.clear(); 290 if (recordsProcessedInFile % 10000 == 0) { 291 totalTimer.split(); 292 LOG.info("Records processed in file " + srcFileName + ":" + recordsProcessedInFile 293 + "; Time elapsed:" + totalTimer.toSplitString()); 294 } 295 if (idFromFileName != null || DocFormat.DUBLIN_CORE.isEqualTo(docFormat)) { 296 break; 297 } 298 } 299 if (solrDocsToAdd.size() > 0) { 300 indexingTimer.resume(); 301 solr.add(solrDocsToAdd); 302 indexingTimer.suspend(); 303 solrDocsToAdd.clear(); 304 } 305 } catch (Exception ex) { 306 String message = "Failure while processing file '" + srcFile.getAbsolutePath() + "' \nat Record: " 307 + recordsProcessedInFile + "\n" + xmlContent; 308 LOG.error(message , ex); 309 solr.rollback(); 310 throw ex; 311 } 312 totalTimer.split(); 313 if (recordsProcessedInFile > 0) { 314 // Do not log this message if a file has only one record. 315 LOG.info("Records processed in file " + srcFileName + ":" + recordsProcessedInFile 316 + "; Time elapsed:" + totalTimer.toSplitString()); 317 } 318 } 319 // commit after all docs are added. 320 if (numDocs > 0) { 321 indexingTimer.resume(); 322 solr.commit(); 323 indexingTimer.suspend(); 324 } 325 326 conversionTimer.stop(); 327 fileIOTimer.stop(); 328 indexingTimer.stop(); 329 totalTimer.stop(); 330 LOG.info("Num of files processed:" + numFiles + "; Num of documents processed:" + numDocs); 331 LOG.info("Time taken for reading files:" + fileIOTimer.toString() 332 + "; Time taken for parsing and converting to Solr Docs:" + conversionTimer.toString()); 333 LOG.info( 334 "Time taken for indexing Solr docs:" + indexingTimer.toString() + "; Total time taken:" + totalTimer 335 .toString()); 336 result = SUCCESS + "-" + numDocs; 337 } catch (Exception e) { 338 result = buildFailureMsg(null, "Indexing failed. " + e.getMessage()); 339 LOG.error(result, e); 340 } 341 return result; 342 } 343 344 // public String indexDocuments(List<RequestDocument> requestDocuments) { 345 // for (RequestDocument requestDocument : requestDocuments) { 346 // indexDocument(requestDocument); 347 // } 348 // return null; 349 // } 350 351 public String indexDocument(RequestDocument requestDocument) { 352 return indexDocument(requestDocument, true); 353 } 354 355 public String indexDocument(RequestDocument requestDocument, boolean commit) { 356 List<RequestDocument> requestDocuments = null; 357 if (requestDocument != null) { 358 requestDocuments = new ArrayList<RequestDocument>(1); 359 requestDocuments.add(requestDocument); 360 } 361 return indexDocuments(requestDocuments, commit); 362 } 363 364 @Override 365 public String indexDocuments(List<RequestDocument> requestDocuments) { 366 return indexDocuments(requestDocuments, true); 367 } 368 369 public String bulkIndexDocuments(List<RequestDocument> requestDocuments, boolean isCommit) { 370 String result = "success"; 371 Map<String, SolrInputDocument> bibIdToDocMap = new HashMap<String, SolrInputDocument>(); 372 BatchIngestStatistics batchStatistics = BulkIngestStatistics.getInstance().getCurrentBatch(); 373 if (requestDocuments != null && requestDocuments.size() > 0) { 374 StopWatch timer = new StopWatch(); 375 StopWatch buildSolrInputDocTimer = new StopWatch(); 376 StopWatch xmlToPojoTimer = new StopWatch(); 377 timer.start(); 378 buildSolrInputDocTimer.start(); 379 buildSolrInputDocTimer.suspend(); 380 xmlToPojoTimer.start(); 381 xmlToPojoTimer.suspend(); 382 383 List<SolrInputDocument> solrInputDocuments = new ArrayList<SolrInputDocument>(); 384 try { 385 if (DocCategory.WORK.isEqualTo(requestDocuments.get(0).getCategory())) { 386 if (DocType.BIB.isEqualTo(requestDocuments.get(0).getType())) { 387 if (DocFormat.MARC.isEqualTo(requestDocuments.get(0).getFormat())) { 388 WorkBibMarcDocBuilder marcBuilder = new WorkBibMarcDocBuilder(); 389 for (RequestDocument requestDocument : requestDocuments) { 390 marcBuilder.buildSolrInputDocument(requestDocument, solrInputDocuments, 391 buildSolrInputDocTimer, xmlToPojoTimer); 392 } 393 } else if (DocFormat.DUBLIN_CORE.isEqualTo(requestDocuments.get(0).getFormat())) { 394 WorkBibDublinDocBuilder dublinBuilder = new WorkBibDublinDocBuilder(); 395 for (RequestDocument requestDocument : requestDocuments) { 396 dublinBuilder.buildSolrInputDocument(requestDocument, solrInputDocuments); 397 } 398 } else if (DocFormat.DUBLIN_UNQUALIFIED.isEqualTo(requestDocuments.get(0).getFormat())) { 399 WorkBibDublinUnQualifiedDocBuilder dublinUnqBuilder 400 = new WorkBibDublinUnQualifiedDocBuilder(); 401 for (RequestDocument requestDocument : requestDocuments) { 402 dublinUnqBuilder.buildSolrInputDocument(requestDocument, solrInputDocuments); 403 } 404 } 405 } else if (DocType.INSTANCE.isEqualTo(requestDocuments.get(0).getType())) { 406 WorkInstanceOlemlDocBuilder oleMlDocBuilder = new WorkInstanceOlemlDocBuilder(); 407 for (RequestDocument requestDocument : requestDocuments) { 408 if(requestDocument != null && requestDocument.getContent() != null && requestDocument.getContent().getContentObject() != null) { 409 InstanceCollection instanceCollection = (InstanceCollection) requestDocument.getContent().getContentObject(); 410 if(instanceCollection.getInstance() != null && instanceCollection.getInstance().size() > 0) { 411 Instance instance = instanceCollection.getInstance().get(0); 412 for (String rId : instance.getResourceIdentifier()) { 413 List<SolrDocument> docs = getSolrDocumentBySolrId(rId); 414 for (SolrDocument solrDoc : docs) { 415 SolrInputDocument bibSolrIDoc = ClientUtils.toSolrInputDocument(solrDoc); 416 String bibId = bibSolrIDoc.getFieldValue(WorkBibCommonFields.UNIQUE_ID).toString(); 417 if (bibIdToDocMap.get(bibId) == null) { 418 bibIdToDocMap.put(bibId, bibSolrIDoc); 419 } 420 bibIdToDocMap.get(bibId) 421 .addField("instanceIdentifier", instance.getInstanceIdentifier()); 422 } 423 } 424 oleMlDocBuilder.buildSolrInputDocuments(requestDocument, solrInputDocuments); 425 } 426 } 427 } 428 } 429 } 430 // if (DocCategory.SECURITY.isEqualTo(requestDocuments.get(0).getCategory())) { 431 // if (DocType.PATRON.isEqualTo(requestDocuments.get(0).getType())) { 432 // if (DocFormat.OLEML.isEqualTo(requestDocuments.get(0).getFormat())) { 433 // SecurityPatronOlemlDocBuilder patronBuilder = new SecurityPatronOlemlDocBuilder(); 434 // for (RequestDocument requestDocument : requestDocuments) { 435 // patronBuilder.buildSolrInputDocument(requestDocument, solrInputDocuments); 436 // } 437 // } 438 // } 439 // } 440 assignUUIDs(solrInputDocuments, null); 441 solrInputDocuments.addAll(bibIdToDocMap.values()); 442 } catch (Exception e1) { 443 result = buildFailureMsg(null, "Bulk Indexing failed. " + e1.getMessage()); 444 LOG.error(result, e1); 445 return result; 446 } 447 timer.stop(); 448 if (solrInputDocuments.isEmpty()) { 449 result = buildFailureMsg(null, "No valid documents found in input."); 450 return result; 451 } 452 int numDocs = solrInputDocuments.size(); 453 batchStatistics.setTimeToConvertXmlToPojo(xmlToPojoTimer.getTime()); 454 batchStatistics.setTimeToConvertToSolrInputDocs(buildSolrInputDocTimer.getTime()); 455 StopWatch indexingTimer = new StopWatch(); 456 indexingTimer.start(); 457 try { 458 result = indexSolrDocuments(solrInputDocuments, isCommit, false, false, false); 459 indexingTimer.stop(); 460 // batchStatistics.setTimeToIndexSolrInputDocs(indexingTimer.toString()); 461 } catch (Exception e) { 462 result = buildFailureMsg(null, "Indexing failed. " + e.getMessage()); 463 LOG.error(result, e); 464 } 465 LOG.debug("Time Consumptions...:\txmlToObj(" + numDocs + "):" + xmlToPojoTimer + "\tbuildSolrInputDoc(" 466 + numDocs + "):" + buildSolrInputDocTimer + "\tTotal(" + numDocs + "):" + timer.toString() 467 + "\t indexingTime(" + solrInputDocuments.size() + "):" + indexingTimer.toString()); 468 } 469 return result; 470 } 471 472 public List<SolrDocument> getSolrDocumentBySolrId(String uniqueId) { 473 QueryResponse response = null; 474 String result = null; 475 try { 476 String args = "(" + WorkBibCommonFields.UNIQUE_ID + ":" + uniqueId + ")"; 477 SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 478 SolrQuery query = new SolrQuery(); 479 query.setQuery(args); 480 response = solr.query(query); 481 } catch (Exception e) { 482 result = buildFailureMsg(); 483 LOG.error(result, e); 484 } 485 return response.getResults(); 486 } 487 488// public List<SolrDocument> getSolrDocumentBySolrId(String uniqueId,String docType) { 489// QueryResponse response = null; 490// String result = null; 491// try { 492// String args = "(" + WorkBibCommonFields.ID + ":" + uniqueId +" AND DocType:"+docType+" )"; 493// SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 494// SolrQuery query = new SolrQuery(); 495// query.setQuery(args); 496// response = solr.query(query); 497// } 498// catch (Exception e) { 499// result = buildFailureMsg(); 500// LOG.error(result, e); 501// } 502// return response.getResults(); 503// } 504 505 506 public List<SolrDocument> getSolrDocument(String fieldName, String fieldValue) { 507 QueryResponse response = null; 508 String result = null; 509 try { 510 String args = "(" + fieldName + ":" + fieldValue + ")"; 511 SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 512 SolrQuery query = new SolrQuery(); 513 query.setQuery(args); 514 response = solr.query(query); 515 } catch (Exception e) { 516 result = buildFailureMsg(); 517 LOG.error(result, e); 518 } 519 return response.getResults(); 520 } 521 522 /** 523 * Builds a UUID value with an identifiable prefix. 524 * Used when a document does not have a UUID assigned by docstore. 525 * Typical scenario is when discovery layer is used without docstore. 526 * 527 * @return 528 */ 529 public String buildUuid() { 530 String uuid = UUID.randomUUID().toString(); 531 uuid = ID_FIELD_PREFIX + uuid; // identifies the uuid generated by discovery module. 532 return uuid; 533 } 534 535 /** 536 * Assigns UUIDs for each document (that does not have an "id" field) in the given list. 537 * Also makes sure "uniqueId" field is present. The UUIDs generated by this method start 538 * with ID_FIELD_PREFIX for easy identification. Optionally takes a list 539 * of UUIDs to be used to set/override the "id" field values of the documents. 540 * 541 * @param solrDocs 542 * @param ids List of id values (optional) to be used for the given documents. 543 */ 544 protected void assignUUIDs(List<SolrInputDocument> solrDocs, List<String> ids) throws Exception { 545 if ((null == solrDocs) || (solrDocs.size() == 0)) { 546 return; 547 } 548 if ((null != ids) && (ids.size() < solrDocs.size())) { 549 throw new Exception( 550 "Insufficient UUIDs(" + ids.size() + ") specified for documents(" + solrDocs.size() + "."); 551 } 552 for (int i = 0; i < solrDocs.size(); i++) { 553 SolrInputDocument solrInputDocument = solrDocs.get(i); 554 SolrInputField idField = solrInputDocument.getField("id"); 555 String uuid = null; 556 if (null != ids) { 557 // Get the supplied UUID. 558 uuid = ids.get(i); 559 } 560 if (null == idField) { 561 if (null == uuid) { 562 // Generate UUID. 563 uuid = UUID.randomUUID().toString(); 564 uuid = ID_FIELD_PREFIX + uuid; // identifies the uuid generated by discovery module. 565 } 566 solrInputDocument.addField(WorkBibCommonFields.ID, uuid); 567 solrInputDocument.addField(WorkBibCommonFields.UNIQUE_ID, uuid); 568 } else { 569 if (null != uuid) { 570 // Use the supplied UUID. 571 solrInputDocument.setField(WorkBibCommonFields.ID, uuid); 572 solrInputDocument.setField(WorkBibCommonFields.UNIQUE_ID, uuid); 573 } else { 574 // Leave the existing id value and make sure uniqueId is set. 575 // uuid = (String) idField.getValue(); 576 if (idField.getValue() instanceof List) { 577 List<String> uuidList = (List<String>) idField.getValue(); 578 uuid = uuidList.get(0); 579 } else if (idField.getValue() instanceof String) { 580 uuid = (String) idField.getValue(); 581 } 582 if (null == uuid) { 583 // Generate UUID. 584 uuid = UUID.randomUUID().toString(); 585 uuid = ID_FIELD_PREFIX + uuid; // identifies the uuid generated by discovery module. 586 idField.setValue(uuid, 1.0f); 587 } 588 SolrInputField uniqueIdField = solrInputDocument.getField(WorkBibCommonFields.UNIQUE_ID); 589 if (null == uniqueIdField) { 590 solrInputDocument.addField(WorkBibCommonFields.UNIQUE_ID, uuid); 591 } else { 592 if (uniqueIdField.getValue() == null) { 593 solrInputDocument.setField(WorkBibCommonFields.UNIQUE_ID, uuid); 594 } 595 } 596 } 597 } 598 } 599 } 600 601 @Override 602 public void commit() throws Exception { 603 boolean waitFlush = false; 604 boolean waitSearcher = false; 605 SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 606 solr.commit(waitFlush, waitSearcher); 607 } 608 609 @Override 610 public void rollback() throws Exception { 611 SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 612 solr.rollback(); 613 } 614 615 protected String indexSolrDocuments(List<SolrInputDocument> solrDocs, boolean isCommit, boolean optimize, 616 boolean waitFlush, boolean waitSearcher) throws Exception { 617 BulkIngestStatistics bulkLoadStatistics = BulkIngestStatistics.getInstance(); 618 BatchIngestStatistics batchStatistics = bulkLoadStatistics.getCurrentBatch(); 619 long commitSize = batchStatistics.getCommitSize(); 620 long recCount = batchStatistics.getRecCount(); 621 StopWatch indexSolrDocsTime = new StopWatch(); 622 StopWatch solrCommitTime = new StopWatch(); 623 indexSolrDocsTime.start(); 624 SolrServer solr = null; 625 if ((null == solrDocs) || (solrDocs.isEmpty())) { 626 return SUCCESS + "-0"; 627 } 628 solr = SolrServerManager.getInstance().getSolrServer(); 629 if (solrDocs.size() > BATCH_SIZE) { 630 int numSolrDocs = solrDocs.size(); 631 for (int fromIndex = 0; fromIndex < numSolrDocs; fromIndex += BATCH_SIZE) { 632 int toIndex = fromIndex + BATCH_SIZE; 633 if (toIndex > numSolrDocs) { 634 toIndex = numSolrDocs; 635 } 636 List batchSolrDocs = solrDocs.subList(fromIndex, toIndex); 637 if ((null != batchSolrDocs) && (!batchSolrDocs.isEmpty())) { 638 LOG.info("Indexing records. fromIndex=" + fromIndex + ", toIndex=" + toIndex); 639 UpdateResponse response = solr.add(solrDocs); 640 } 641 } 642 } else { 643 LOG.debug("Indexing records. size=" + solrDocs.size()); 644 UpdateResponse response = solr.add(solrDocs); 645 } 646 indexSolrDocsTime.stop(); 647 commitRecCount = commitRecCount + recCount; 648 if (commitRecCount == commitSize || bulkLoadStatistics.isLastBatch()) { 649 // isCommit = true; 650 } 651 if (isCommit) { 652 solrCommitTime.start(); 653 LOG.info("Bulk ingest: Index commit started. Number of records being committed: " + bulkLoadStatistics 654 .getCommitRecCount()); 655 solr.commit(waitFlush, waitSearcher); 656 commitRecCount = 0; 657 solrCommitTime.stop(); 658 } 659 if (optimize) { 660 solr.optimize(waitFlush, waitSearcher); 661 } 662 663 664 LOG.debug("Time Consumptions...: Solr input docs of size ..." + solrDocs.size() 665 + "\t time taken to index solr Input Docs" + indexSolrDocsTime + "solrcommit & Optimize" 666 + solrCommitTime); 667 batchStatistics.setTimeToIndexSolrInputDocs(indexSolrDocsTime.getTime()); 668 batchStatistics.setTimeToSolrCommit(solrCommitTime.getTime()); 669 return SUCCESS + "-" + solrDocs.size(); 670 } 671 672 protected String indexSolrDocuments(List<SolrInputDocument> solrDocs, boolean commit, boolean optimize) 673 throws Exception { 674 String result = indexSolrDocuments(solrDocs, commit, optimize, true, true); 675 return result; 676 } 677 678 protected List<SolrInputDocument> convertToSolrDocs(String docCategory, String docType, String docFormat, 679 String docContent) throws Exception { 680 List<SolrInputDocument> solrDocs = null; 681 if (DocCategory.WORK.isEqualTo(docCategory) && DocType.BIB.isEqualTo(docType) && DocFormat.MARC.isEqualTo( 682 docFormat)) { 683 try { 684 WorkBibMarcRecordProcessor recordProcessor = new WorkBibMarcRecordProcessor(); 685 solrDocs = new WorkBibMarcDocBuilder() 686 .buildSolrInputDocuments(recordProcessor.fromXML(docContent).getRecords()); 687 } catch (Exception e) { 688 LOG.error(e.getMessage(), e); 689 throw new Exception("Exception while converting given XML Document: ", e); 690 } 691 } else if (DocCategory.WORK.isEqualTo(docCategory) && DocType.BIB.isEqualTo(docType) && DocFormat.DUBLIN_CORE 692 .isEqualTo( 693 docFormat)) { 694 WorkBibDublinRecordProcessor processor = new WorkBibDublinRecordProcessor(); 695 WorkBibDublinRecord record = processor.fromXML(docContent); 696 solrDocs = new ArrayList<SolrInputDocument>(); 697 solrDocs.add(new WorkBibDublinDocBuilder().buildSolrInputDocument(record)); 698 } else if (DocCategory.WORK.isEqualTo(docCategory) && DocType.BIB.isEqualTo(docType) && DocFormat 699 .DUBLIN_UNQUALIFIED.isEqualTo(docFormat)) { 700 solrDocs = new WorkBibDublinUnQualifiedDocBuilder() 701 .buildSolrInputDocuments(new WorkBibDublinUnQualifiedRecordProcessor().fromXML(docContent)); 702 } else { 703 throw new Exception("UnSupported Document Format: " + docCategory + ", " + docType + ", " + docFormat); 704 } 705 return solrDocs; 706 } 707 708 protected String deleteDocumentByUUID(String uuid, String category, boolean commit) { 709 String result = SUCCESS; 710 try { 711 SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 712 solr.deleteById(uuid); 713 if (commit) { 714 solr.commit(); 715 } 716 } catch (Exception e) { 717 result = buildFailureMsg(); 718 LOG.error(result, e); 719 } 720 return result; 721 } 722 723 protected String deleteDocumentByUUID(String uuid, String category) { 724 return deleteDocumentByUUID(uuid, category, true); 725 } 726 727 protected String deleteDocumentsByUUIDList(List<String> uuidList, String category, boolean commit) { 728 String result = SUCCESS; 729 try { 730 SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 731 List<String> uuidList1 = new ArrayList<String>(); 732 if (uuidList.size() > 0) { 733 for (String id : uuidList) { 734 if (id != null) { 735 uuidList1.add(id); 736 } 737 } 738 } 739 if (uuidList1.size() > 0) { 740 solr.deleteById(uuidList1); 741 } 742 if (commit) { 743 solr.commit(); 744 } 745 } catch (Exception e) { 746 result = buildFailureMsg(); 747 LOG.error(result, e); 748 } 749 return result; 750 } 751 752 protected String deleteDocumentsByUUIDList(List<String> uuidsList, String category) 753 throws SolrServerException, MalformedURLException { 754 List<String> deleteUuidsList = new ArrayList<String>(); 755 List<String> holdingsIdentifierList = new ArrayList<String>(); 756 List<String> itemIdentifierList = new ArrayList<String>(); 757 SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 758 SolrQuery query = new SolrQuery(); 759 deleteUuidsList.addAll(uuidsList); 760 for (int i = 0; i < uuidsList.size(); i++) { 761 query.setQuery("id:" + uuidsList.get(i)); 762 QueryResponse response = solr.query(query); 763 LOG.debug("query-->" + query); 764 for (SolrDocument doc : response.getResults()) { 765 LOG.debug("doc" + doc.toString()); 766 String docFormat = (String) doc.getFieldValue(DOC_FORMAT); 767 String docType = (String) doc.getFieldValue(DOC_TYPE); 768 if (docType.equalsIgnoreCase(BIBLIOGRAPHIC)) { 769 } else if (docType.equalsIgnoreCase(INSTANCE)) { 770 if (doc.getFieldValue(ITEM_IDENTIFIER) instanceof List) { 771 itemIdentifierList = (List<String>) doc.getFieldValue(ITEM_IDENTIFIER); 772 } else { 773 itemIdentifierList.add((String) doc.getFieldValue(ITEM_IDENTIFIER)); 774 } 775 if (doc.getFieldValue(HOLDINGS_IDENTIFIER) instanceof String) { 776 holdingsIdentifierList.add((String) doc.getFieldValue(HOLDINGS_IDENTIFIER)); 777 } else { 778 holdingsIdentifierList = (List<String>) doc.getFieldValue(HOLDINGS_IDENTIFIER); 779 } 780 if (holdingsIdentifierList != null && holdingsIdentifierList.size() > 0) { 781 deleteUuidsList.addAll(holdingsIdentifierList); 782 } 783 if (itemIdentifierList != null && itemIdentifierList.size() > 0) { 784 deleteUuidsList.addAll(itemIdentifierList); 785 786 } 787 } 788 } 789 } 790 return deleteDocumentsByUUIDList(deleteUuidsList, category, true); 791 } 792 793 @Deprecated 794 protected String buildDeleteQueryParamsForDeleteUrl(List<String> uuidList, boolean commit) { 795 StringBuffer deleteQueryBuffer = new StringBuffer(""); 796 deleteQueryBuffer.append("stream.body="); 797 deleteQueryBuffer.append("<delete>"); 798 for (int i = 0; i < uuidList.size(); i++) { 799 deleteQueryBuffer.append("<query>"); 800 deleteQueryBuffer.append("id:"); 801 deleteQueryBuffer.append(uuidList.get(i)); 802 deleteQueryBuffer.append("</query>"); 803 } 804 deleteQueryBuffer.append("</delete>"); 805 if (commit) { 806 deleteQueryBuffer.append("&stream.body=<commit/>"); 807 } 808 return deleteQueryBuffer.toString(); 809 810 } 811 812 @Deprecated 813 protected String buildDeleteQuery(String uuid, String category, boolean commit) { 814 StringBuffer deleteQueryUrl = new StringBuffer(""); 815 if (commit) { 816 deleteQueryUrl.append(SolrServerManager.getInstance().getSolrCoreURL()); 817 deleteQueryUrl.append("/update?stream.body=<delete><query>id:" + uuid 818 + "</query></delete>&stream.body=<commit/>"); 819 } else { 820 deleteQueryUrl.append(SolrServerManager.getInstance().getSolrCoreURL()); 821 deleteQueryUrl.append("/update?stream.body=<delete><query>id:" + uuid + "</query></delete>"); 822 } 823 return deleteQueryUrl.toString(); 824 } 825 826 /** 827 * @param inputURL 828 * @throws Exception 829 */ 830 @Deprecated 831 protected void openConnection(URL inputURL) throws Exception { 832 HttpURLConnection urlConnection = (HttpURLConnection) inputURL.openConnection(); 833 urlConnection.setDoOutput(true); 834 urlConnection.connect(); 835 OutputStreamWriter streamWriter = new OutputStreamWriter(urlConnection.getOutputStream()); 836 streamWriter.flush(); 837 // Get the response from inputURL 838 BufferedReader bufferReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 839 String xmlResponse; 840 while ((xmlResponse = bufferReader.readLine()) != null) { 841 if (LOG.isDebugEnabled()) { 842 LOG.debug("XmlResponse->" + xmlResponse); 843 } 844 } 845 } 846 847 protected String getErrorID() { 848 return String.valueOf(new Date().getTime()); 849 } 850 851 protected String buildFailureMsg(String id, String msg) { 852 StringBuilder sb = new StringBuilder(); 853 sb.append(FAILURE).append("-ErrorID:"); 854 if (null != id) { 855 sb.append(id); 856 } else { 857 sb.append(getErrorID()); 858 } 859 if (null != msg) { 860 sb.append("-ErrorMsg:").append(msg); 861 } 862 return sb.toString(); 863 } 864 865 protected String buildFailureMsg() { 866 return FAILURE + "-ErrorID:" + getErrorID(); 867 } 868 869 public QueryResponse searchBibRecord(String docCat, String docType, String docFormat, String fieldName, 870 String fieldValue, String fieldList) { 871 QueryResponse response = null; 872 String result = null; 873 try { 874 String identifier_args = "(" + fieldName + ":" + fieldValue + ")"; 875 String docCategory_args = "(DocCategory" + ":" + docCat + ")"; 876 String docType_args = "(DocType" + ":" + docType + ")"; 877 String docFormat_args = "(DocFormat" + ":" + docFormat + ")"; 878 String args = identifier_args + "AND" + docCategory_args + "AND" + docType_args + "AND" + docFormat_args; 879 SolrServer solr = new HttpSolrServer( 880 ConfigContext.getCurrentContextConfig().getProperty("docSearchURL") + "bib"); 881 SolrQuery query = new SolrQuery(); 882 query.addField(fieldList); 883 query.setQuery(args); 884 response = solr.query(query); 885 } catch (Exception e) { 886 result = buildFailureMsg(); 887 LOG.error(result, e); 888 } 889 return response; 890 } 891 892 @Override 893 public void cleanupDiscoveryData() throws IOException, SolrServerException { 894 SolrServer server = null; 895 try { 896 server = SolrServerManager.getInstance().getSolrServer(); 897 } catch (SolrServerException e) { 898 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 899 } 900 server.deleteByQuery("*:*"); 901 server.commit(); 902 903 } 904 905 @Override 906 public String bind(List<RequestDocument> requestDocuments) throws Exception { 907 String result = null; 908 for (RequestDocument requestDocument : requestDocuments) { 909 result = bind(requestDocument); 910 } 911 return result; 912 } 913 914 @Override 915 public String bind(RequestDocument requestDocument) throws Exception { 916 List<SolrInputDocument> solrInputDocumentList = new ArrayList<SolrInputDocument>(); 917 updateInstanceDocument(requestDocument, solrInputDocumentList); 918 updateBibDocument(requestDocument, solrInputDocumentList); 919 LOG.info("solrInputDocumentList-->" + solrInputDocumentList); 920 SolrQuery solrQuery = new SolrQuery(); 921 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 922 UpdateResponse updateResponse = server.add(solrInputDocumentList); 923 server.commit(); 924 return "success"; 925 } 926 927 private void updateBibDocument(RequestDocument requestDocument, List<SolrInputDocument> solrInputDocumentList) throws Exception { 928 List<RequestDocument> linkedRequestDocuments = requestDocument.getLinkedRequestDocuments(); 929 for (RequestDocument linkedRequestDocument : linkedRequestDocuments) { 930 SolrDocument bibSolrDocument = getSolrDocumentByUUID(linkedRequestDocument.getUuid(), 931 linkedRequestDocument.getType()); 932 List<String> instanceIdentifierList = new ArrayList<String>(); 933 Object instanceIdentifier = bibSolrDocument.getFieldValue("instanceIdentifier"); 934 if (instanceIdentifier instanceof List) { 935 instanceIdentifierList = (List<String>) bibSolrDocument.getFieldValue("instanceIdentifier"); 936 937 } else if (instanceIdentifier instanceof String) { 938 instanceIdentifierList.add((String) instanceIdentifier); 939 } 940 941 instanceIdentifierList.add(requestDocument.getUuid()); 942 bibSolrDocument.setField("instanceIdentifier", instanceIdentifierList); 943 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(bibSolrDocument)); 944 945 } 946 } 947 948 /** 949 * @param requestDocument 950 * @param solrInputDocumentList 951 * @throws Exception 952 * @throws java.io.IOException 953 */ 954 private void updateInstanceDocument(RequestDocument requestDocument, List<SolrInputDocument> solrInputDocumentList) throws Exception { 955 SolrQuery solrQuery = new SolrQuery(); 956 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 957 solrQuery.setQuery(("id:" + requestDocument.getUuid() + " AND DocType:" + requestDocument.getType())); 958 QueryResponse response = server.query(solrQuery); 959 List<SolrDocument> solrDocumentList = response.getResults(); 960 LOG.debug("response.getResults()-->" + response.getResults()); 961 for (SolrDocument solrDocument : solrDocumentList) { 962 List<String> bibIdentifierList = new ArrayList<String>(); 963 964 //Get the BibIdentifier list and update the bibIdentifier field with the linked doc uuid's. 965 Object bibIdentifier = solrDocument.getFieldValue("bibIdentifier"); 966 if (bibIdentifier instanceof List) { 967 bibIdentifierList = (List<String>) solrDocument.getFieldValue("bibIdentifier"); 968 969 } else if (bibIdentifier instanceof String) { 970 bibIdentifierList.add((String) bibIdentifier); 971 } 972 LOG.info("bibIdentifierList-->" + bibIdentifierList); 973 List<RequestDocument> linkedRequestDocuments = requestDocument.getLinkedRequestDocuments(); 974 for (RequestDocument linkedRequestDocument : linkedRequestDocuments) { 975 bibIdentifierList.add(linkedRequestDocument.getUuid()); 976 } 977 solrDocument.setField("bibIdentifier", bibIdentifierList); 978 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(solrDocument)); 979 980 //Get the holdings identifier from the solrdoc and update bibIdentifier field in holding doc with the linked doc uuid's. 981 Object holdingsIdentifier = solrDocument.getFieldValue("holdingsIdentifier"); 982 List<String> holdingIdentifierList = new ArrayList<String>(); 983 if (holdingsIdentifier instanceof List) { 984 holdingIdentifierList = (List<String>) solrDocument.getFieldValue("holdingsIdentifier"); 985 986 } else if (holdingsIdentifier instanceof String) { 987 holdingIdentifierList.add((String) holdingsIdentifier); 988 989 } 990 SolrDocument holdingSolrDocument = getSolrDocumentByUUID(holdingIdentifierList.get(0), 991 DocType.HOLDINGS.getCode()); 992 holdingSolrDocument.setField("bibIdentifier", bibIdentifierList); 993 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(holdingSolrDocument)); 994 995 //Get the item identifier from the solrdoc and update bibIdentifier field in item doc with the linked doc uuid's. 996 Object itemIdentifier = solrDocument.getFieldValue("itemIdentifier"); 997 List<String> itemIdentifierList = new ArrayList<String>(); 998 if (itemIdentifier instanceof List) { 999 itemIdentifierList = (List<String>) solrDocument.getFieldValue("itemIdentifier"); 1000 1001 } else if (itemIdentifier instanceof String) { 1002 itemIdentifierList.add((String) itemIdentifier); 1003 } 1004 1005 for (String itemId : itemIdentifierList) { 1006 SolrDocument itemSolrDocument = getSolrDocumentByUUID(itemId, DocType.ITEM.getCode()); 1007 itemSolrDocument.setField("bibIdentifier", bibIdentifierList); 1008 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(itemSolrDocument)); 1009 1010 } 1011 1012 1013 } 1014 } 1015 1016 1017 @Override 1018 public String unbind(List<RequestDocument> requestDocuments) throws Exception { 1019 for (RequestDocument requestDocument : requestDocuments) { 1020 List<SolrInputDocument> solrInputDocumentList = new ArrayList<SolrInputDocument>(); 1021 unbindFromInstanceDocument(requestDocument, solrInputDocumentList); 1022 unbindFromBibDocument(requestDocument, solrInputDocumentList); 1023 LOG.info("solrInputDocumentList-->" + solrInputDocumentList); 1024 SolrQuery solrQuery = new SolrQuery(); 1025 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 1026 UpdateResponse updateResponse = server.add(solrInputDocumentList); 1027 server.commit(); 1028 1029 } 1030 return "success"; 1031 1032 1033 } 1034 1035 @Override 1036 public void transferInstances(List<RequestDocument> requestDocuments) throws Exception { 1037 String destBibIdentifier = requestDocuments.get(requestDocuments.size() - 1).getUuid(); 1038 List<SolrInputDocument> solrInputDocumentList = new ArrayList<SolrInputDocument>(); 1039 // List<String> intanceIdentifierList=new ArrayList<String>(); 1040 List<String> sourceBibUUIDList = new ArrayList<String>(); 1041 List<String> instanceIdentifiersList = new ArrayList<String>(); 1042 for (RequestDocument requestDocument : requestDocuments) { 1043 if (requestDocument.getType().equalsIgnoreCase("instance") && requestDocument.getFormat() 1044 .equalsIgnoreCase("oleml")) { 1045 instanceIdentifiersList.add(requestDocument.getUuid()); 1046 } 1047 } 1048 modifyInstanceSolrDocumentForTransferInstances(instanceIdentifiersList, sourceBibUUIDList, destBibIdentifier, 1049 solrInputDocumentList); 1050 LOG.debug("sourceBibUUIDList " + sourceBibUUIDList); 1051 LOG.debug("instanceIdentifiersList " + instanceIdentifiersList); 1052 LOG.debug("destBibIdentifier " + destBibIdentifier); 1053 1054 modifySolrDocForSourceBibForTransferInstances(sourceBibUUIDList, instanceIdentifiersList, 1055 solrInputDocumentList); 1056 modifySolrDocForDestBibForTransferInstances(destBibIdentifier, instanceIdentifiersList, solrInputDocumentList); 1057 SolrQuery query = new SolrQuery(); 1058 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 1059 UpdateResponse updateResponse = server.add(solrInputDocumentList); 1060 //server.commit(); 1061 LOG.debug("updateResponse " + updateResponse); 1062 } 1063 1064 private void modifyInstanceSolrDocumentForTransferInstances(List<String> instanceIdentifiersList, 1065 List<String> sourceBibUUIDList, 1066 String destBibIdentifier, 1067 List<SolrInputDocument> solrInputDocumentList) 1068 throws Exception { 1069 String sourceBibIdentifier = ""; 1070 String holdingsIdentifier = ""; 1071 SolrDocumentList solrDocumentList = getSolrDocumentByUUIDs(instanceIdentifiersList); 1072 for (SolrDocument instanceSolrDocument : solrDocumentList) { 1073 // String instanceIdentifier = requestDocument.getUuid(); 1074 // intanceIdentifierList.add(instanceIdentifier); 1075 // SolrDocument instanceSolrDocument = getSolrDocumentByUUID(instanceIdentifier); 1076 sourceBibIdentifier = (String) instanceSolrDocument.getFieldValue("bibIdentifier"); 1077 sourceBibUUIDList.add(sourceBibIdentifier); 1078 holdingsIdentifier = (String) instanceSolrDocument.getFieldValue("holdingsIdentifier"); 1079 1080 SolrDocument holdingSolrDocument = getSolrDocumentByUUID(holdingsIdentifier); 1081 1082 holdingSolrDocument.setField("bibIdentifier", destBibIdentifier); 1083 solrInputDocumentList.add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(holdingSolrDocument)); 1084 1085 modifyItemSolrDocumentForTransferInstances(instanceSolrDocument, destBibIdentifier, solrInputDocumentList); 1086 instanceSolrDocument.setField("bibIdentifier", destBibIdentifier); 1087 solrInputDocumentList.add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(instanceSolrDocument)); 1088 } 1089 } 1090 1091 private void modifyItemSolrDocumentForTransferInstances(SolrDocument instanceSolrDocument, String destBibIdentifier, 1092 List<SolrInputDocument> solrInputDocumentList) 1093 throws Exception { 1094 Object object = instanceSolrDocument.getFieldValue("itemIdentifier"); 1095 //Bib can have more than one item identifier. If there is one item identifier solr will return item identifier string. If there are more than one item identifier solr returns 1096 //list of item identifiers 1097 if (object instanceof String) { 1098 String itemIdentifier = (String) object; 1099 SolrDocument itemSolrDocument = getSolrDocumentByUUID(itemIdentifier); 1100 itemSolrDocument.setField("bibIdentifier", destBibIdentifier); 1101 solrInputDocumentList.add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(itemSolrDocument)); 1102 } else if (object instanceof List) { 1103 List<String> itemIdentifierList = (List<String>) object; 1104 SolrDocumentList solrDocumentList = getSolrDocumentByUUIDs(itemIdentifierList); 1105 for (SolrDocument itemSolrDocument : solrDocumentList) { 1106 itemSolrDocument.setField("bibIdentifier", destBibIdentifier); 1107 solrInputDocumentList.add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(itemSolrDocument)); 1108 } 1109 } 1110 1111 } 1112 1113 private SolrDocumentList getSolrDocumentByUUIDs(List<String> uuids) throws Exception { 1114 String operand = "OR"; 1115 StringBuffer sb = new StringBuffer(); 1116 sb.append("("); 1117 for (String uuid : uuids) { 1118 sb.append("("); 1119 sb.append(uuid); 1120 sb.append(")"); 1121 sb.append(operand); 1122 } 1123 String queryString = sb.substring(0, sb.length() - operand.length()) + ")"; 1124 LOG.debug("queryString " + queryString); 1125 SolrQuery query = new SolrQuery(); 1126 SolrDocument solrDocument = null; 1127 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 1128 QueryResponse response = null; 1129 query.setQuery("id:" + queryString); 1130 response = server.query(query); 1131 LOG.debug("size " + response.getResults().size()); 1132 SolrDocumentList solrDocumentList = response.getResults(); 1133 return solrDocumentList; 1134 1135 } 1136 1137 private void modifySolrDocForDestBibForTransferInstances(String destBibIdentifier, 1138 List<String> intanceIdentifierList, 1139 List<SolrInputDocument> solrInputDocumentList) 1140 throws Exception { 1141 1142 SolrDocument destBibSolrDocument = getSolrDocumentByUUID(destBibIdentifier); 1143 destBibSolrDocument.addField("instanceIdentifier", intanceIdentifierList); 1144 solrInputDocumentList.add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(destBibSolrDocument)); 1145 } 1146 1147 private void modifySolrDocForSourceBibForTransferInstances(List<String> sourceBibUUIDList, 1148 List<String> instanceUUIDList, 1149 List<SolrInputDocument> solrInputDocumentList) 1150 throws Exception { 1151 List bibIdentifierList; 1152 SolrDocumentList solrDocumentList = getSolrDocumentByUUIDs(sourceBibUUIDList); 1153 for (SolrDocument solrDocumentForSourceBib : solrDocumentList) { 1154 Object field = solrDocumentForSourceBib.getFieldValue("instanceIdentifier"); 1155 if (field instanceof String) { 1156 String instanceIdentifier = (String) solrDocumentForSourceBib.getFieldValue("instanceIdentifier"); 1157 solrDocumentForSourceBib.setField("instanceIdentifier", ""); 1158 } else if (field instanceof List) { 1159 bibIdentifierList = (List) solrDocumentForSourceBib.getFieldValue("instanceIdentifier"); 1160 for (String instanceIdenfier : instanceUUIDList) { 1161 if (bibIdentifierList.contains(instanceIdenfier)) { 1162 bibIdentifierList.remove(instanceIdenfier); 1163 } 1164 } 1165 solrDocumentForSourceBib.setField("instanceIdentifier", bibIdentifierList); 1166 1167 } 1168 solrInputDocumentList.add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(solrDocumentForSourceBib)); 1169 1170 } 1171 1172 } 1173 1174 @Override 1175 public void transferItems(List<RequestDocument> requestDocuments) throws Exception { 1176 LOG.debug("IndexerServiceImpl transferItems"); 1177 List<SolrInputDocument> solrInputDocumentListFinal = new ArrayList<SolrInputDocument>(); 1178 String destinationInstanceIdentifier = requestDocuments.get(requestDocuments.size() - 1).getUuid(); 1179 List<String> itemUUIDList = new ArrayList<String>(); 1180 List<String> sourceInstanceUUIDList = new ArrayList<String>(); 1181 modifySolrDocForItem(requestDocuments, sourceInstanceUUIDList, itemUUIDList, destinationInstanceIdentifier, 1182 solrInputDocumentListFinal); 1183 LOG.debug("sourceInstanceUUIDList " + sourceInstanceUUIDList); 1184 LOG.debug("itemUUIDList " + itemUUIDList); 1185 modifySolrDocForDestInstance(destinationInstanceIdentifier, itemUUIDList, solrInputDocumentListFinal); 1186 modifySolrDocForSourceInstance(sourceInstanceUUIDList, itemUUIDList, solrInputDocumentListFinal); 1187 LOG.debug("solrInputDocumentListFinal size " + solrInputDocumentListFinal.size()); 1188 SolrQuery query = new SolrQuery(); 1189 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 1190 UpdateResponse updateResponse = server.add(solrInputDocumentListFinal); 1191 // server.commit(); 1192 LOG.debug("updateResponse " + updateResponse); 1193 } 1194 1195 @Override 1196 public String delete(List<RequestDocument> requestDocuments) throws Exception { 1197 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 1198 List<SolrInputDocument> solrInputDocumentList = new ArrayList<SolrInputDocument>(); 1199 SolrQuery solrQuery = new SolrQuery(); 1200 for (RequestDocument requestDocument : requestDocuments) { 1201 if (requestDocument.getCategory().equalsIgnoreCase("work") && requestDocument.getType().equalsIgnoreCase("bibliographic") 1202 && requestDocument.getFormat().equalsIgnoreCase("marc")) { 1203 String query = "bibIdentifier:" + requestDocument.getUuid() + " OR " + "id:" + requestDocument.getUuid(); 1204 UpdateResponse updateResponse = server.deleteByQuery(query); 1205 LOG.debug("updateResponse " + updateResponse); 1206 } 1207 if (requestDocument.getCategory().equalsIgnoreCase("work") && requestDocument.getType().equalsIgnoreCase("instance") 1208 && requestDocument.getFormat().equalsIgnoreCase("oleml")) { 1209 String query = "id:" + requestDocument.getUuid(); 1210 solrQuery.setQuery(query); 1211 QueryResponse response = server.query(solrQuery); 1212 List<SolrDocument> solrDocumentList = response.getResults(); 1213 List<String> deleteIdList = new ArrayList<String>(); 1214 for (SolrDocument solrDocument : solrDocumentList) { 1215 Object holId = solrDocument.getFieldValue("holdingsIdentifier"); 1216 if (holId != null) { 1217 if (holId instanceof List) { 1218 List<String> holIdList = (List<String>) solrDocument.getFieldValue("holdingsIdentifier"); 1219 deleteIdList.addAll(holIdList); 1220 } else if (holId instanceof String) { 1221 String holdId = (String) holId; 1222 deleteIdList.add(holdId); 1223 } 1224 } 1225 Object itemId = solrDocument.getFieldValue("itemIdentifier"); 1226 if (itemId != null) { 1227 if (itemId instanceof List) { 1228 List<String> itemList = (List<String>) solrDocument.getFieldValue("itemIdentifier"); 1229 deleteIdList.addAll(itemList); 1230 } else if (itemId instanceof String) { 1231 String itmId = (String) itemId; 1232 deleteIdList.add(itmId); 1233 } 1234 } 1235 } 1236 deleteIdList.add(requestDocument.getUuid()); 1237 deleteDocumentsByUUIDList(deleteIdList, "instance", true); 1238 query = "instanceIdentifier:" + requestDocument.getUuid() + "AND" + "(DocType:bibliographic)"; 1239 solrQuery.setQuery(query); 1240 response = server.query(solrQuery); 1241 solrDocumentList = response.getResults(); 1242 for (SolrDocument bibSolrDocument : solrDocumentList) { 1243 List<String> instanceIdentifierList = new ArrayList<String>(); 1244 Object instanceIdentifier = bibSolrDocument.getFieldValue("instanceIdentifier"); 1245 if (instanceIdentifier instanceof List) { 1246 instanceIdentifierList = (List<String>) bibSolrDocument.getFieldValue("instanceIdentifier"); 1247 if (instanceIdentifierList.contains(requestDocument.getUuid())) { 1248 instanceIdentifierList.remove(requestDocument.getUuid()); 1249 bibSolrDocument.setField("instanceIdentifier", instanceIdentifierList); 1250 } 1251 } else if (instanceIdentifier instanceof String) { 1252 String instId = (String) instanceIdentifier; 1253 if (instId.equalsIgnoreCase(requestDocument.getUuid())) { 1254 instanceIdentifier = ""; 1255 bibSolrDocument.setField("instanceIdentifier", instanceIdentifier); 1256 } 1257 } 1258 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(bibSolrDocument)); 1259 } 1260 String result = indexSolrDocuments(solrInputDocumentList, true); 1261 if (!result.contains(SUCCESS)) { 1262 return result; 1263 } 1264// return result; 1265 } 1266 if (requestDocument.getCategory().equalsIgnoreCase("work") && requestDocument.getType().equalsIgnoreCase("item") 1267 && requestDocument.getFormat().equalsIgnoreCase("oleml")) { 1268 String query = "id:" + requestDocument.getUuid(); 1269 UpdateResponse updateResponse = server.deleteByQuery(query); 1270 query = "itemIdentifier:" + requestDocument.getUuid(); 1271 solrQuery.setQuery(query); 1272 QueryResponse response = server.query(solrQuery); 1273 List<SolrDocument> solrDocumentList = response.getResults(); 1274 for (SolrDocument instanceSolrDocument : solrDocumentList) { 1275 List<String> itemIdentifierList = new ArrayList<String>(); 1276 Object itemIdentifier = instanceSolrDocument.getFieldValue("itemIdentifier"); 1277 if (itemIdentifier instanceof List) { 1278 itemIdentifierList = (List<String>) instanceSolrDocument.getFieldValue("itemIdentifier"); 1279 if (itemIdentifierList.contains(requestDocument.getUuid())) { 1280 itemIdentifierList.remove(requestDocument.getUuid()); 1281 instanceSolrDocument.setField("itemIdentifier", itemIdentifierList); 1282 } 1283 } else if (itemIdentifier instanceof String) { 1284 String itemId = (String) itemIdentifier; 1285 if (itemId.equalsIgnoreCase(requestDocument.getUuid())) { 1286 itemIdentifier = ""; 1287 instanceSolrDocument.setField("itemIdentifier", itemIdentifier); 1288 } 1289 } 1290 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(instanceSolrDocument)); 1291 } 1292 String result = indexSolrDocuments(solrInputDocumentList, true); 1293 if (!result.contains(SUCCESS)) { 1294 return result; 1295 } 1296// return result; 1297 } 1298 } 1299// server.commit(); 1300 return "success"; 1301 } 1302 1303 1304 @Override 1305 public String delete(RequestDocument requestDocument) throws Exception { 1306 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 1307 List<SolrInputDocument> solrInputDocumentList = new ArrayList<SolrInputDocument>(); 1308 SolrQuery solrQuery = new SolrQuery(); 1309 Date date = new Date(); 1310 if (requestDocument.getCategory().equalsIgnoreCase("work") && requestDocument.getType() 1311 .equalsIgnoreCase("bibliographic") 1312 && requestDocument.getFormat().equalsIgnoreCase("marc")) { 1313 String query = "(bibIdentifier:" + requestDocument.getUuid() + ") OR (DocType:" + requestDocument.getType() 1314 + " AND id:" + requestDocument.getUuid() + ")"; 1315 LOG.info("query-->" + query); 1316 UpdateResponse updateResponse = server.deleteByQuery(query); 1317 LOG.info("updateResponse " + updateResponse); 1318 // save deleted Bibliographic with Bibliographic_delete 1319 String newId = requestDocument.getUuid() + "_d"; 1320 SolrInputDocument solrInputDocument = new SolrInputDocument(); 1321 solrInputDocument.setField("DocType", "bibliographic_delete"); 1322 solrInputDocument.setField("dateUpdated", date); 1323 solrInputDocument.setField("uniqueId", newId); 1324 solrInputDocument.setField("id", newId); 1325 solrInputDocument.setField("LocalId_display", DocumentLocalId.getDocumentIdDisplay(requestDocument.getUuid())); 1326 UpdateResponse updateResponseForBib = server.add(solrInputDocument); 1327 LOG.debug("updateResponse " + updateResponseForBib); 1328 } 1329 if (requestDocument.getCategory().equalsIgnoreCase("work") && requestDocument.getType() 1330 .equalsIgnoreCase("bibliographic") 1331 && requestDocument.getFormat().equalsIgnoreCase("dublinunq")) { 1332 String query = "(bibIdentifier:" + requestDocument.getUuid() + ") OR (DocType:" + requestDocument.getType() 1333 + " AND id:" + requestDocument.getUuid() + ")"; 1334 LOG.info("query-->" + query); 1335 UpdateResponse updateResponse = server.deleteByQuery(query); 1336 LOG.info("updateResponse " + updateResponse); 1337 } 1338 1339 if (requestDocument.getCategory().equalsIgnoreCase("work") && requestDocument.getType() 1340 .equalsIgnoreCase("instance") 1341 && requestDocument.getFormat().equalsIgnoreCase("oleml")) { 1342 String query = "(instanceIdentifier:" + requestDocument.getUuid() + " AND DocFormat:" + requestDocument 1343 .getFormat() + ")"; 1344 LOG.info("query-->" + query); 1345 UpdateResponse updateResponse = server.deleteByQuery(query); 1346 query = "(id:" + requestDocument.getUuid() + " AND DocType:" + requestDocument.getType() + ")"; 1347 server.deleteByQuery(query); 1348 query = "instanceIdentifier:" + requestDocument.getUuid() + " AND " + "(DocType:bibliographic)"; 1349 solrQuery.setQuery(query); 1350 QueryResponse response = server.query(solrQuery); 1351 List<SolrDocument> solrDocumentList = response.getResults(); 1352 for (SolrDocument bibSolrDocument : solrDocumentList) { 1353 List<String> instanceIdentifierList = new ArrayList<String>(); 1354 Object instanceIdentifier = bibSolrDocument.getFieldValue("instanceIdentifier"); 1355 if (instanceIdentifier instanceof List) { 1356 instanceIdentifierList = (List<String>) bibSolrDocument.getFieldValue("instanceIdentifier"); 1357 if (instanceIdentifierList.contains(requestDocument.getUuid())) { 1358 instanceIdentifierList.remove(requestDocument.getUuid()); 1359 bibSolrDocument.setField("instanceIdentifier", instanceIdentifierList); 1360 } 1361 } else if (instanceIdentifier instanceof String) { 1362 String instId = (String) instanceIdentifier; 1363 if (instId.equalsIgnoreCase(requestDocument.getUuid())) { 1364 bibSolrDocument.removeFields("instanceIdentifier"); 1365 } 1366 } 1367 bibSolrDocument.setField("dateUpdated", date); 1368 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(bibSolrDocument)); 1369 } 1370 String result = indexSolrDocuments(solrInputDocumentList, true); 1371 if (!result.contains(SUCCESS)) { 1372 return result; 1373 } 1374 } 1375 if (requestDocument.getCategory().equalsIgnoreCase("work") && requestDocument.getType().equalsIgnoreCase("item") 1376 && requestDocument.getFormat().equalsIgnoreCase("oleml")) { 1377 String query = "id:" + requestDocument.getUuid(); 1378 UpdateResponse updateResponse = server.deleteByQuery(query); 1379 query = "itemIdentifier:" + requestDocument.getUuid(); 1380 solrQuery.setQuery(query); 1381 QueryResponse response = server.query(solrQuery); 1382 List<SolrDocument> solrDocumentList = response.getResults(); 1383 for (SolrDocument instanceSolrDocument : solrDocumentList) { 1384 List<String> itemIdentifierList = new ArrayList<String>(); 1385 Object itemIdentifier = instanceSolrDocument.getFieldValue("itemIdentifier"); 1386 if (itemIdentifier instanceof List) { 1387 itemIdentifierList = (List<String>) instanceSolrDocument.getFieldValue("itemIdentifier"); 1388 if (itemIdentifierList.contains(requestDocument.getUuid())) { 1389 itemIdentifierList.remove(requestDocument.getUuid()); 1390 instanceSolrDocument.setField("itemIdentifier", itemIdentifierList); 1391 } 1392 } else if (itemIdentifier instanceof String) { 1393 String itemId = (String) itemIdentifier; 1394 if (itemId.equalsIgnoreCase(requestDocument.getUuid())) { 1395 itemIdentifier = ""; 1396 instanceSolrDocument.setField("itemIdentifier", itemIdentifier); 1397 } 1398 } 1399 solrInputDocumentList 1400 .add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(instanceSolrDocument)); 1401 } 1402 query = "id:" + requestDocument.getUuid(); 1403 solrQuery.setQuery(query); 1404 response = server.query(solrQuery); 1405 List<SolrDocument> solrDocuments = response.getResults(); 1406 String bibId = ""; 1407 if(solrDocuments != null && solrDocuments.size() > 0) { 1408 SolrDocument itemDoc = solrDocuments.get(0); 1409 Object bibIdentifier = itemDoc.getFieldValue("bibIdentifier"); 1410 if (bibIdentifier instanceof List) { 1411 List<String> bibIds = (List) bibIdentifier; 1412 bibId = bibIds.get(0); 1413 } 1414 else { 1415 bibId = (String) bibIdentifier; 1416 } 1417 } 1418 if(StringUtils.isNotEmpty(bibId)) { 1419 query = "id:" + bibId; 1420 solrQuery.setQuery(query); 1421 response = server.query(solrQuery); 1422 List<SolrDocument> bibSolrDocumentList = response.getResults(); 1423 for(SolrDocument bibSolrDocument : bibSolrDocumentList) { 1424 bibSolrDocument.setField("dateUpdated", date); 1425 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(bibSolrDocument)); 1426 } 1427 } 1428 String result = indexSolrDocuments(solrInputDocumentList, true); 1429 if (!result.contains(SUCCESS)) { 1430 return result; 1431 } 1432 // return result; 1433 1434 } 1435 return "success"; 1436 1437 } 1438 1439 private void modifySolrDocForSourceInstance(List<String> sourceInstanceUUIDList, List<String> itemUUIDList, 1440 List<SolrInputDocument> solrInputDocumentListFinal) throws Exception { 1441 1442 List itemIdentifierList = new ArrayList(); 1443 for (String sourceInstanceIdentifier : sourceInstanceUUIDList) { 1444 SolrDocument solrDocumentForSourceInstance = getSolrDocumentByUUID(sourceInstanceIdentifier); 1445 Object field = solrDocumentForSourceInstance.getFieldValue("itemIdentifier"); 1446 if (field instanceof String) { 1447 String instanceIdentifier = (String) solrDocumentForSourceInstance.getFieldValue("itemIdentifier"); 1448 itemIdentifierList.add(instanceIdentifier); 1449 } else if (field instanceof List) { 1450 itemIdentifierList = (List) solrDocumentForSourceInstance.getFieldValue("itemIdentifier"); 1451 } 1452 for (String itemIdenfier : itemUUIDList) { 1453 if (itemIdentifierList.contains(itemIdenfier)) { 1454 itemIdentifierList.remove(itemIdenfier); 1455 } 1456 } 1457 solrDocumentForSourceInstance.setField("itemIdentifier", itemIdentifierList); 1458 String holdingsIdentifier = (String) solrDocumentForSourceInstance.getFieldValue("holdingsIdentifier"); 1459 SolrDocument solrDocumentForHoldingsIdentifier = getSolrDocumentByUUID(holdingsIdentifier); 1460 solrDocumentForHoldingsIdentifier.setField("itemIdentifier", itemIdentifierList); 1461 solrInputDocumentListFinal 1462 .add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(solrDocumentForSourceInstance)); 1463 solrInputDocumentListFinal 1464 .add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(solrDocumentForHoldingsIdentifier)); 1465 } 1466 1467 } 1468 1469 private void modifySolrDocForDestInstance(String destInstanceIdentifier, List<String> itemUUIDList, 1470 List<SolrInputDocument> solrInputDocumentListFinal) throws Exception { 1471 1472 SolrDocument solrDocumentForDestinationInstance = getSolrDocumentByUUID(destInstanceIdentifier); 1473 solrDocumentForDestinationInstance.addField("itemIdentifier", itemUUIDList); 1474 String holdingsIdentifier = (String) solrDocumentForDestinationInstance.getFieldValue("holdingsIdentifier"); 1475 SolrDocument solrDocumentForHoldingsIdentifier = getSolrDocumentByUUID(holdingsIdentifier); 1476 solrDocumentForHoldingsIdentifier.addField("itemIdentifier", itemUUIDList); 1477 solrInputDocumentListFinal 1478 .add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(solrDocumentForDestinationInstance)); 1479 solrInputDocumentListFinal 1480 .add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(solrDocumentForHoldingsIdentifier)); 1481 1482 } 1483 1484 private void modifySolrDocForItem(List<RequestDocument> requestDocuments, List<String> sourceInstanceUUIDList, 1485 List<String> itemUUIDList, String destInstanceIdentifier, 1486 List<SolrInputDocument> solrInputDocumentListFinal) throws Exception { 1487 // List<SolrInputDocument> solrInputDocumentList = new ArrayList<SolrInputDocument>(); 1488 SolrDocument solrDocumentForDestinationInstance = getSolrDocumentByUUID(destInstanceIdentifier); 1489 1490 for (RequestDocument requestDocument : requestDocuments) { 1491 if (requestDocument.getType().equalsIgnoreCase("item") && requestDocument.getFormat() 1492 .equalsIgnoreCase("oleml")) { 1493 String itemUUID = requestDocument.getUuid(); 1494 SolrDocument solrDocumentForItem = getSolrDocumentByUUID(itemUUID); 1495 String sourceInstanceUUID = (String) solrDocumentForItem.getFieldValue("instanceIdentifier"); 1496 sourceInstanceUUIDList.add(sourceInstanceUUID); 1497 solrDocumentForItem.setField("holdingsIdentifier", 1498 solrDocumentForDestinationInstance.getFieldValue("holdingsIdentifier")); 1499 solrDocumentForItem 1500 .setField("bibIdentifier", solrDocumentForDestinationInstance.getFieldValue("bibIdentifier")); 1501 solrDocumentForItem 1502 .setField("instanceIdentifier", solrDocumentForDestinationInstance.getFieldValue("id")); 1503 itemUUIDList.add(itemUUID); 1504 solrInputDocumentListFinal 1505 .add(workBibMarcDocBuilder1.buildSolrInputDocFromSolrDoc(solrDocumentForItem)); 1506 } 1507 } 1508 } 1509 1510 private void unbindFromInstanceDocument(RequestDocument requestDocument, List<SolrInputDocument> solrInputDocumentList) throws Exception { 1511 1512 SolrQuery solrQuery = new SolrQuery(); 1513 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 1514 solrQuery.setQuery("id:" + requestDocument.getUuid()); 1515 QueryResponse response = server.query(solrQuery); 1516 List<SolrDocument> solrDocumentList = response.getResults(); 1517 LOG.debug("response.getResults()-->" + response.getResults()); 1518 for (SolrDocument solrDocument : solrDocumentList) { 1519 List<String> bibIdentifierList = new ArrayList<String>(); 1520 1521 //Get the BibIdentifier list and update the bibIdentifier field with the linked doc uuid's. 1522 Object bibIdentifier = solrDocument.getFieldValue("bibIdentifier"); 1523 if (bibIdentifier instanceof List) { 1524 bibIdentifierList = (List<String>) solrDocument.getFieldValue("bibIdentifier"); 1525 1526 } else if (bibIdentifier instanceof String) { 1527 bibIdentifierList.add((String) bibIdentifier); 1528 } 1529 LOG.info("bibIdentifierList before remove-->" + bibIdentifierList); 1530 List<RequestDocument> linkedRequestDocuments = requestDocument.getLinkedRequestDocuments(); 1531 for (RequestDocument linkedRequestDocument : linkedRequestDocuments) { 1532 if (bibIdentifierList.contains(linkedRequestDocument.getUuid())) { 1533 bibIdentifierList.remove(linkedRequestDocument.getUuid()); 1534 } 1535 1536 } 1537 LOG.info("bibIdentifierList after remove-->" + bibIdentifierList); 1538 solrDocument.setField("bibIdentifier", bibIdentifierList); 1539 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(solrDocument)); 1540 1541 //Get the holdings identifier from the solrdoc and update bibIdentifier field in holding doc with the linked doc uuid's. 1542 Object holdingsIdentifier = solrDocument.getFieldValue("holdingsIdentifier"); 1543 List<String> holdingIdentifierList = new ArrayList<String>(); 1544 if (holdingsIdentifier instanceof List) { 1545 holdingIdentifierList = (List<String>) solrDocument.getFieldValue("holdingsIdentifier"); 1546 1547 } else if (holdingsIdentifier instanceof String) { 1548 holdingIdentifierList.add((String) holdingsIdentifier); 1549 1550 } 1551 1552 SolrDocument holdingSolrDocument = getSolrDocumentByUUID(holdingIdentifierList.get(0)); 1553 holdingSolrDocument.setField("bibIdentifier", bibIdentifierList); 1554 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(holdingSolrDocument)); 1555 1556 //Get the item identifier from the solrdoc and update bibIdentifier field in item doc with the linked doc uuid's. 1557 Object itemIdentifier = solrDocument.getFieldValue("itemIdentifier"); 1558 List<String> itemIdentifierList = new ArrayList<String>(); 1559 if (itemIdentifier instanceof List) { 1560 itemIdentifierList = (List<String>) solrDocument.getFieldValue("itemIdentifier"); 1561 1562 } else if (itemIdentifier instanceof String) { 1563 itemIdentifierList.add((String) itemIdentifier); 1564 } 1565 1566 for (String itemId : itemIdentifierList) { 1567 SolrDocument itemSolrDocument = getSolrDocumentByUUID(itemId); 1568 itemSolrDocument.setField("bibIdentifier", bibIdentifierList); 1569 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(itemSolrDocument)); 1570 1571 } 1572 1573 1574 } 1575 1576 1577 } 1578 1579 private void unbindFromBibDocument(RequestDocument requestDocument, List<SolrInputDocument> solrInputDocumentList) throws Exception { 1580 List<RequestDocument> linkedRequestDocuments = requestDocument.getLinkedRequestDocuments(); 1581 for (RequestDocument linkedRequestDocument : linkedRequestDocuments) { 1582 SolrDocument bibSolrDocument = getSolrDocumentByUUID(linkedRequestDocument.getUuid()); 1583 List<String> instanceIdentifierList = new ArrayList<String>(); 1584 Object instanceIdentifier = bibSolrDocument.getFieldValue("instanceIdentifier"); 1585 if (instanceIdentifier instanceof List) { 1586 instanceIdentifierList = (List<String>) bibSolrDocument.getFieldValue("instanceIdentifier"); 1587 1588 } else if (instanceIdentifier instanceof String) { 1589 instanceIdentifierList.add((String) instanceIdentifier); 1590 } 1591 LOG.info("instanceIdentifierList before remove-->" + instanceIdentifierList); 1592 if (instanceIdentifierList.contains(requestDocument.getUuid())) { 1593 instanceIdentifierList.remove(requestDocument.getUuid()); 1594 } 1595 LOG.info("instanceIdentifierList after remove-->" + instanceIdentifierList); 1596 bibSolrDocument.setField("instanceIdentifier", instanceIdentifierList); 1597 solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(bibSolrDocument)); 1598 1599 } 1600 1601 } 1602 1603 1604 private SolrDocument getSolrDocumentByUUID(String identifier) throws Exception { 1605 SolrQuery query = new SolrQuery(); 1606 SolrDocument solrDocument = null; 1607 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 1608 QueryResponse response = null; 1609 query.setQuery("id:" + identifier); 1610 response = server.query(query); 1611 solrDocument = response.getResults().get(0); 1612 return solrDocument; 1613 } 1614 1615 private SolrDocument getSolrDocumentByUUID(String identifier, String docType) throws Exception { 1616 SolrQuery query = new SolrQuery(); 1617 SolrDocument solrDocument = null; 1618 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 1619 QueryResponse response = null; 1620 query.setQuery(("id:" + identifier + " AND " + "DocType:" + docType)); 1621 response = server.query(query); 1622 solrDocument = response.getResults().get(0); 1623 return solrDocument; 1624 } 1625}