package com.k_int.discover.service; import javax.xml.xpath.XPathExpressionException; import com.k_int.aggregator.core.*; import org.springframework.stereotype.Service; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.*; import com.k_int.aggregator.plugin.XMLProcessingResult; import com.k_int.aggregator.repository.RepositoryService; import com.k_int.aggregator.repository.RepositoryStoreResult; import com.k_int.aggregator.util.SOLRHelper; import com.k_int.commons.util.XMLUtil; @Service("XMLHandler-CopyOfPNDSDCAP") // TODO - change this! public class CopyOfPNDSDCAPHandler extends RecordHandler { private static Log log = LogFactory.getLog(CopyOfPNDSDCAPHandler.class); protected void setupMappings() { // xpathMappings.put("audience", new NameXPathMapping("dcterms.audience","/pn:description/dcterms:audience", false)); // xpathMappings.put("contributor", new NameXPathMapping("dc.contributor","/pn:description/dc:contributor", false)); xpathMappings.put("description", new NameXPathMapping("dc.description","/pn:description/dc:description", false)); // xpathMappings.put("format", new NameXPathMapping("dc.format","/pn:description/dc:format", false)); xpathMappings.put("identifier", new NameXPathMapping("dc.identifier","/pn:description/dc:identifier", true)); // xpathMappings.put("language", new NameXPathMapping("dc.language","/pn:description/dc:language", false)); // xpathMappings.put("license", new NameXPathMapping("dcterms.license","/pn:description/dcterms:license/@valueURI", false)); // xpathMappings.put("location", new NameXPathMapping("dc.location","/pn:description/dc:location", false)); // xpathMappings.put("place", new NameXPathMapping("dc.place","/pn:description/dc:place", false)); // xpathMappings.put("publisher", new NameXPathMapping("dc.publisher","/pn:description/dc:publisher", false)); // xpathMappings.put("rightsHolder", new NameXPathMapping("dcterms.rightsHolder","/pn:description/dcterms:rightsHolder", false)); xpathMappings.put("spatial", new NameXPathMapping("dcterms.spatial", "/pn:description/dcterms.spatial", false)); xpathMappings.put("subject", new NameXPathMapping("dc.subject","/pn:description/dc:subject", false)); xpathMappings.put("temporal", new NameXPathMapping("dcterms.temporal", "/pn:description/dcterms:temporal", false)); xpathMappings.put("thumbnail", new NameXPathMapping("pndsterms.thumbnail","/pn:description/pndsterms:thumbnail/@valueURI", false)); xpathMappings.put("title", new NameXPathMapping("dc.title","/pn:description/dc:title", false)); xpathMappings.put("type", new NameXPathMapping("dcmi.type","/pn:description/dc:type", false)); // xpathMappings.put("materials", new NameXPathMapping("e20cl.materials", "/pn:description/e20cl:materials", false)); // xpathMappings.put("size", new NameXPathMapping("e20cl.size", "/pn:description/e20cl:size", false)); // xpathMappings.put("creditLine", new NameXPathMapping("e20cl.creditLine", "/pn:description/e20cl:creditLine", false)); // xpathMappings.put("relatedSubject", new NameXPathMapping("e20cl.relatedSubject", "/pn:description/e20cl:relatedSubject", false)); xpathMappings.put("subject.person", new NameXPathMapping("e20cl.relatedPerson", "/pn:description/e20cl:relatedPerson", false)); xpathMappings.put("subject.organisation", new NameXPathMapping("e20cl.relatedOrganisation", "/pn:description/e20cl:relatedOrganisation", false)); xpathMappings.put("subject.event", new NameXPathMapping("e20cl.relatedEvent", "/pn:description/e20cl:relatedEvent", false)); // xpathMappings.put("relation", new NameXPathMapping("dc.relation", "/pn:description/dc:relation", false)); } private static String[] SUPPORTED_SCHEMAS = new String[] { HandlerNamespaces.XMLNS_PNDS_DC, HandlerNamespaces.XMLNS_PNDS_DC_E20CL }; // TODO - how should this be set really? public CopyOfPNDSDCAPHandler() { super(); log.debug("new CopyOfPNDSDCAPHandler"); } public XMLProcessingResult process(DepositResult deposit_result, byte[] content, Document d, // The xml fragment being deposited String depositor_id, // The user / system making the deposit String owner_id, // The actual owner this deposit is on behalf of String root_namespace, // Namespace of the root element Boolean authoritative) { // Set up the mappings information that we need.. this.setupMappings(); // Set up the return value XMLProcessingResult returnValue = new XMLProcessingResult(); try { log.debug("Processing CopyOfPNDSDCAPHandler Document"); com.k_int.aggregator.util.SOLRHelper solr_helper = (com.k_int.aggregator.util.SOLRHelper) ctx.getBean("SolrHelper"); // Set up namespace elements Element new_namespace_node = d.createElement("NSNode"); // Set up our local namespace definitions new_namespace_node.setAttribute("xmlns:dc", HandlerNamespaces.XMLNS_DC); new_namespace_node.setAttribute("xmlns:dcterms", HandlerNamespaces.XMLNS_DCTERMS); new_namespace_node.setAttribute("xmlns:xsi", HandlerNamespaces.XMLNS_XSI); new_namespace_node.setAttribute("xmlns:pn", HandlerNamespaces.XMLNS_PNDS_DC); new_namespace_node.setAttribute("xmlns:pndsterms", HandlerNamespaces.XMLNS_PNDSTERMS); new_namespace_node.setAttribute("xmlns:e20cl", HandlerNamespaces.XMLNS_PNDS_DC_E20CL); // Loop through the list of mappings and get the values from the document if possible, remembering them // in the mappings for(String key: xpathMappings.keySet()) { NameXPathMapping thisOne = xpathMappings.get(key); String xpath = thisOne.getXpathExpression(); String[] newValues = getValues(d, xpath, new_namespace_node); log.debug("Just mapped xpath " + xpath + " to " + newValues.length + " values"); if (newValues != null && newValues.length != 0 ) { thisOne.setValues(newValues); log.debug("stored the new found value.. in a local object"); xpathMappings.put(key, thisOne); log.debug("stored the value in the mappings"); } } // Check that we have all of the 'required' information as specified by the mappings // - will throw out to the catch below if not this.checkForRequiredData(); // Check that we have an identifier as without this we can't do anything String doc_identifier = xpathMappings.get("identifier").getValues()[0]; log.debug("doc_identifier: " + doc_identifier); if ( doc_identifier != null && !"".equals(doc_identifier) ) { log.debug("About to store the content..."); // 2. Store in content store RepositoryService content_store = (RepositoryService) ctx.getBean("ContentStore"); RepositoryStoreResult store_result = content_store.store(content,owner_id,depositor_id,doc_identifier,"pnds_dcap","application/xml",SUPPORTED_SCHEMAS[0],authoritative,1); // Set up the return value with the information that we care about returnValue.setDocId(store_result.getDocId()); // 4. Extract indexing document // Submit indexing fields to SOLR java.util.List index_properties = new java.util.ArrayList(); for(String key: xpathMappings.keySet()) { NameXPathMapping thisEntry = xpathMappings.get(key); String[] values = thisEntry.getValues(); if ( values != null ) { if ( thisEntry.collectAll ) { // Store several values for(String aValue: values) { if ( aValue != null ) { index_properties.add(new SOLRHelper.NVPair(thisEntry.getSolrName(), aValue)); } } } else { // Store just one (if there is one) if ( values[0] != null) { index_properties.add(new SOLRHelper.NVPair(thisEntry.getSolrName(), values[0])); } } } } //TODO - need to handle these properly... NameXPathMapping thumbnailEntry = xpathMappings.get("thumbnail"); String haveThumbnail = "false"; if ( thumbnailEntry != null && thumbnailEntry.getValues() != null && thumbnailEntry.getValues()[0] != null ) { haveThumbnail = "true"; } index_properties.add(new SOLRHelper.NVPair("have_thumbnail", haveThumbnail)); String combinedSubject = ""; String[] separateSubjects = {"subject.person", "subject.organisation", "subject.event"}; for(String subject: separateSubjects) { NameXPathMapping subjectEntry = xpathMappings.get(subject); if ( subjectEntry != null && subjectEntry.getValues() != null && subjectEntry.getValues()[0] != null ) { combinedSubject += " " + subjectEntry.getValues()[0]; } } index_properties.add(new SOLRHelper.NVPair("subject.combined", combinedSubject.trim())); index_properties.add(new SOLRHelper.NVPair("authority", owner_id)); index_properties.add(new SOLRHelper.NVPair("record_type", "item")); index_properties.add(new SOLRHelper.NVPair("depositor_id", depositor_id)); index_properties.add(new SOLRHelper.NVPair("restp","ServiceProvider")); index_properties.add(new SOLRHelper.NVPair("aggregator.internal.id",store_result.getDocId()+"")); // 5. Submit to indexing service Document solr_doc = solr_helper.postIndexEntries(index_properties); if (solr_doc != null) { content_store.store(XMLUtil.serializeDocument(solr_doc), owner_id, depositor_id, doc_identifier, "solr", "application/xml", "", authoritative, 2); } } else { log.error("No identifier found in ECD document"); returnValue.setMessage("No identifier found in the ECD document"); } } catch ( javax.xml.transform.TransformerException te ) { log.error("Problem",te); returnValue.setMessage("TransformerException thrown when parsing the ECD document."); } catch ( org.hibernate.HibernateException he ) { returnValue.setMessage("HibernateException thrown when parsing the ECD document."); log.error("Problem with user search", he); } catch ( XPathExpressionException xe ) { returnValue.setMessage(xe.getMessage()); log.error("Problem when parsing the ECD document. " + xe.getMessage()); } return returnValue; } }