Example usage for org.dom4j Node selectSingleNode

List of usage examples for org.dom4j Node selectSingleNode

Introduction

In this page you can find the example usage for org.dom4j Node selectSingleNode.

Prototype

Node selectSingleNode(String xpathExpression);

Source Link

Document

selectSingleNode evaluates an XPath expression and returns the result as a single Node instance.

Usage

From source file:edu.scripps.fl.pubchem.PubChemXMLParserFactory.java

License:Apache License

protected void handleTargetXRefs(PCAssay assay, PCAssayPanel panel, List<Node> nodes) {
    for (Node n : nodes) {
        String name = n.selectSingleNode("PC-AssayTargetInfo_name").getText();
        String id = n.selectSingleNode("PC-AssayTargetInfo_mol-id").getText();
        String type = n.selectSingleNode("PC-AssayTargetInfo_molecule-type").valueOf("@value");
        String database = targetType.containsKey(type) ? targetType.get(type) : type;
        Node taxonNode = n.selectSingleNode("PC-AssayTargetInfo_organism/BioSource/BioSource_org/Org-ref");
        String taxonName = "", taxonCommon = "", taxon = "";
        if (taxonNode != null) {
            taxonName = taxonNode.selectSingleNode("Org-ref_taxname").getText();
            taxonCommon = nullSafeGet(taxonNode, "Org-ref_common", "text()");
            String db = nullSafeGet(taxonNode, "Org-ref_db/Dbtag/Dbtag_db", "text()");
            if (!"taxon".equals(db))
                throw new RuntimeException("Non taxon BioSource Org-ref_db (was " + db + ")");
            taxonNode = taxonNode.selectSingleNode("Org-ref_db/Dbtag/Dbtag_tag/Object-id/Object-id_id");
            taxon = taxonNode.getText();
        }//w  w w.  j a  v a2 s.c o  m

        XRef xref = new XRef();
        xref.setXRefId(id);
        xref.setDatabase(database);
        xref.setType(type);
        xref.setName(name);

        PCAssayXRef aXref = new PCAssayXRef();
        aXref.setTarget(true);
        aXref.setPanel(panel);
        aXref.setAssay(assay);
        aXref.setXRef(xref);
        if (!taxon.equals("")) {
            aXref.setTaxon(Long.parseLong(taxon));
            aXref.setTaxonName(taxonName);
            aXref.setTaxonCommon(taxonCommon);
        }
        assay.getAssayXRefs().add(aXref);
    }
}

From source file:edu.scripps.fl.pubchem.web.pug.PUGRequest.java

License:Apache License

public static void setResponseIds(Document document, Type type, Collection<Object> ids) throws Exception {
    String idTypePath = "//PCT-QueryAssayData_";
    idTypePath += Type.AID.equals(type) ? "aids" : "scids";
    Node localRoot = document.selectSingleNode(idTypePath);
    Node node = localRoot.selectSingleNode(".//PCT-ID-List_db");
    node.setText(type.getDatabase());/*from w  w  w  .  ja va2  s.c o  m*/
    node = localRoot.selectSingleNode(".//PCT-ID-List_uids");
    //      if (node == null)
    //         throw new Exception("Cannot find PCT-ID-List_uids node");
    for (Node child : (List<Node>) node.selectNodes("*"))
        child.detach();
    for (Object id : ids) {
        Element aidElem = DocumentHelper.createElement("PCT-ID-List_uids_E");
        aidElem.setText(id.toString());
        ((Element) node).add(aidElem);
    }
}

From source file:edu.scripps.fl.pubchem.xml.extract.AssayExtractor.java

License:Apache License

public Assay getAssayValuesFromXML(Document doc)
        throws ParseException, IllegalAccessException, InvocationTargetException {
    Assay assay = new Assay();
    PubChemXMLUtils utils = new PubChemXMLUtils();

    utils.getElementText(assay, "//PC-DBTracking_source-id/Object-id/Object-id_str", doc, "externalRegId");
    utils.getElementText(assay, "//PC-AssayDescription_name", doc, "name");
    utils.getListofElementsText(assay, "//PC-AssayDescription_description_E", doc, "description");
    utils.getListofElementsText(assay, "//PC-AssayDescription_protocol_E", doc, "protocol");
    utils.getListofElementsText(assay, "//PC-AssayDescription_comment_E", doc, "comment");
    utils.getElementText(assay, "//PC-ID_id", doc, "aid");
    utils.getElementText(assay, "//PC-AssayDescription_grant-number_E", doc, "grantNumber");
    utils.getElementText(assay, "//PC-DBTracking_name", doc, "source");

    String attribute = utils.getElementAttribute("//PC-AssayDescription_activity-outcome-method", doc);
    if (!attribute.equals(""))
        assay.setActivityOutcomeMethod(attribute);
    attribute = utils.getElementAttribute("//PC-AssayDescription_project-category", doc);
    if (!attribute.equals(""))
        assay.setProjectCategory(attribute);
    Node node = doc.selectSingleNode("//PC-DBTracking_date/Date/Date_std/Date-std");
    if (node != null)
        assay.setHoldUntilDate(/*  www .  j  ava 2 s .  c  om*/
                new SimpleDateFormat("MM/dd/yyyy").parse(node.selectSingleNode("Date-std_month").getText() + "/"
                        + node.selectSingleNode("Date-std_day").getText() + "/"
                        + node.selectSingleNode("Date-std_year").getText()));

    return assay;
}

From source file:edu.scripps.fl.pubchem.xml.extract.CategorizedCommentExtractor.java

License:Apache License

public List<CategorizedComment> getCategorizedCommentsFromXML(Document doc) throws Exception {
    List<CategorizedComment> comments = new ArrayList<CategorizedComment>();
    List<Node> nodes = doc.selectNodes("//PC-AssayDescription_categorized-comment/PC-CategorizedComment");
    PubChemXMLUtils utils = new PubChemXMLUtils();
    for (Node nn : nodes) {
        CategorizedComment comment = new CategorizedComment();
        comment.setCommentTag(nn.selectSingleNode("PC-CategorizedComment_title").getText());
        List<Node> valueNodes = nn.selectNodes("PC-CategorizedComment_comment/PC-CategorizedComment_comment_E");
        String text = "";
        for (Node valeuNode : valueNodes) {
            if (null != valeuNode.getText())
                text = text + valeuNode.getText() + "\n";
        }/* w  ww. j  a va 2 s.co  m*/
        comment.setCommentValue(text);
        comments.add(comment);
    }
    return comments;
}

From source file:edu.scripps.fl.pubchem.xml.extract.PanelExtractor.java

License:Apache License

public List<Panel> getPanelValuesFromXML(Document doc) throws Exception {
    List<Panel> panelValues = new ArrayList<Panel>();
    List<Node> nodes = doc.selectNodes(
            "//PC-AssayDescription_panel-info/PC-AssayPanel/PC-AssayPanel_member/PC-AssayPanelMember");
    for (Node nn : nodes) {
        Panel panelValue = new Panel();
        panelValue.setPanelName(nn.selectSingleNode("PC-AssayPanelMember_name").getText());
        List<Node> targetNodes = nn.selectNodes("PC-AssayPanelMember_target/PC-AssayTargetInfo");
        if (targetNodes != null && targetNodes.size() > 0) {
            List<PanelTarget> targets = new ArrayList<PanelTarget>();
            for (Node targetNode : targetNodes) {
                PanelTarget target = new PanelTarget();
                target.setPanelTargetName(targetNode.selectSingleNode("PC-AssayTargetInfo_name").getText());
                BeanUtils.setProperty(target, "panelTargetGi",
                        targetNode.selectSingleNode("PC-AssayTargetInfo_mol-id").getText());
                target.setPanelTargetType(
                        targetNode.selectSingleNode("PC-AssayTargetInfo_molecule-type").valueOf("@value"));
                targets.add(target);/*from  ww  w . ja v  a2s . com*/
            }
            panelValue.setPanelTarget(targets);
        }

        Node p = nn.selectSingleNode("PC-AssayPanelMember_xref");
        if (p != null) {
            List<Integer> taxonomies = getXrefValues(
                    "PC-AnnotatedXRef/PC-AnnotatedXRef_xref/PC-XRefData/PC-XRefData_taxonomy", p);
            panelValue.setPanelTaxonomy(taxonomies);
            List<Integer> genes = getXrefValues(
                    "PC-AnnotatedXRef/PC-AnnotatedXRef_xref/PC-XRefData/PC-XRefData_gene", p);
            panelValue.setPanelGene(genes);
        }
        panelValues.add(panelValue);
    }
    return panelValues;
}

From source file:edu.scripps.fl.pubchem.xml.extract.ResultTidExtractor.java

License:Apache License

public List<ResultTid> getTidValuesFromXML(Document doc)
        throws IllegalAccessException, InvocationTargetException {

    List<ResultTid> tidValues = new ArrayList<ResultTid>();
    List<Node> nodes = doc.selectNodes("//PC-AssayDescription_results/PC-ResultType");
    for (Node nn : nodes) {
        ResultTid tidValue = new ResultTid();
        tidValue.setTidName(nn.selectSingleNode("PC-ResultType_name").getText());
        tidValue.setTidType(nn.selectSingleNode("PC-ResultType_type").valueOf("@value"));
        Node node = nn.selectSingleNode("PC-ResultType_unit");
        if (node != null)
            tidValue.setTidUnit(node.valueOf("@value"));
        node = nn.selectSingleNode("PC-ResultType_description/PC-ResultType_description_E");
        if (node != null)
            tidValue.setTidDescription(node.getText());
        node = nn.selectSingleNode("PC-ResultType_ac");
        if (node != null)
            tidValue.setIsActiveConcentration(true);
        node = nn.selectSingleNode("PC-ResultType_tc");
        if (node != null) {
            Node node2 = node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_dr-id");
            if (node2 != null) {
                BeanUtils.setProperty(tidValue, "tidPlot", node2.getText());
            }/*from w ww .ja  v a  2s. com*/
            BeanUtils.setProperty(tidValue, "tidConcentration",
                    node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_concentration").getText());
        }
        node = nn.selectSingleNode("PC-ResultType_panel-info/PC-AssayPanelTestResult");
        if (node != null) {
            BeanUtils.setProperty(tidValue, "tidPanelNum",
                    node.selectSingleNode("PC-AssayPanelTestResult_mid").getText());
            tidValue.setTidPanelReadout(
                    node.selectSingleNode("PC-AssayPanelTestResult_readout-annot").valueOf("@value"));
        }
        tidValues.add(tidValue);
    }
    return tidValues;
}

From source file:edu.scripps.fl.pubchem.xml.extract.XrefExtractor.java

License:Apache License

public List<Xref> getXrefValuesFromXML(Document doc, List<Panel> panel) throws Exception {
    List<Xref> xrefs = new ArrayList<Xref>();
    List<Node> nodes = doc.selectNodes("//PC-AssayDescription_xref/PC-AnnotatedXRef");
    for (Node nn : nodes) {
        Xref xref = new Xref();
        Node node = nn.selectSingleNode("PC-AnnotatedXRef_xref/PC-XRefData");
        selectXref(xref, node, "PC-XRefData_aid", "AID");
        selectXref(xref, node, "PC-XRefData_protein-gi", "Protein");
        selectXref(xref, node, "PC-XRefData_gene", "Gene");
        selectXref(xref, node, "PC-XRefData_taxonomy", "Taxonomy");
        selectXref(xref, node, "PC-XRefData_mim", "OMIM");
        selectXref(xref, node, "PC-XRefData_dburl", "Source Database Homepage");
        selectXref(xref, node, "PC-XRefData_mesh", "MESH");
        selectXref(xref, node, "PC-XRefData_sburl", "Substance Homepage");
        selectXref(xref, node, "PC-XRefData_asurl", "Assay Homepage");
        selectXref(xref, node, "PC-XRefData_rn", "Substance Registry #");
        selectXref(xref, node, "PC-XRefData_pmid", "PMID");
        selectXref(xref, node, "PC-XRefData_sid", "SID");
        selectXref(xref, node, "PC-XRefData_cid", "CID");
        selectXref(xref, node, "PC-XRefData_nucleotide-gi", "Nucleotide");
        selectXref(xref, node, "PC-XRefData_gi", "GenBank");
        selectXref(xref, node, "PC-XRefData_mmdb", "MMDB");
        selectXref(xref, node, "PC-XRefData_biosystem", "Biosystems Id");
        if (nn.selectSingleNode("PC-AnnotatedXRef_comment") != null)
            xref.setXrefComment(nn.selectSingleNode("PC-AnnotatedXRef_comment").getText());
        Node citationNode = nn.selectSingleNode("PC-AnnotatedXRef_type");
        if (citationNode != null) {
            if (citationNode.getText().equals("1"))
                xref.setIsPrimaryCitation(true);
        }/*from   w w  w.  j  a v a2s .co m*/
        xrefs.add(xref);
    }
    nodes = doc.selectNodes("//PC-AssayDescription_target/PC-AssayTargetInfo");
    for (Node nn : nodes) {
        Xref xref = new Xref();
        BeanUtils.setProperty(xref, "xrefValue", nn.selectSingleNode("PC-AssayTargetInfo_mol-id").getText());
        BeanUtils.setProperty(xref, "xrefTargetValue",
                nn.selectSingleNode("PC-AssayTargetInfo_molecule-type").getText());
        BeanUtils.setProperty(xref, "isTarget", true);
        xrefs.add(xref);
    }
    if (panel != null)
        checkXrefAgainstPanel(xrefs, panel);
    return xrefs;
}

From source file:edu.scripps.fl.pubchem.xml.extract.XrefExtractor.java

License:Apache License

public void selectXref(Xref xref, Node nn, String xrefNode, String xrefType)
        throws IllegalAccessException, InvocationTargetException {
    if (nn.selectSingleNode(xrefNode) != null) {
        BeanUtils.setProperty(xref, "xrefValue", nn.selectSingleNode(xrefNode).getText());
        xref.setXrefType(xrefType);//from ww w  .j av  a  2  s.c  o  m
    }
}

From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportHandler.java

/**
 * Procedure to populate the RDF metadata and ingest the files
 *///from  w  ww . j av a  2s  .  c  om
public boolean execute() throws Exception {
    if (filesPaths != null) {
        File file = null;
        // List the source files
        for (int i = 0; i < filesPaths.length; i++) {
            file = new File(filesPaths[i]);
            if (file.exists()) {
                listFile(filesMap, file);
            }
        }
    }

    String message = "";
    Document doc = null;
    DamsURI damsURI = null;

    String oid = null;
    int fLen = rdfFiles.length;
    String currFile = null;
    SAXReader saxReader = new SAXReader();
    for (int i = 0; i < fLen && !interrupted; i++) {
        currFile = rdfFiles[i].getName();
        setStatus(
                "Processing external import for file " + currFile + " (" + (i + 1) + " of " + fLen + ") ... ");
        try {
            doc = saxReader.read(rdfFiles[i]);
            List<Node> nodes = doc.selectNodes("//@rdf:about");
            for (int j = 0; j < nodes.size(); j++) {
                Node nUri = nodes.get(j);
                String iUri = nUri.getStringValue();
                Node parentNode = nUri.getParent();
                String nName = parentNode.getName();
                if (iUri.endsWith("/COL") || !(iUri.startsWith("http") && iUri.indexOf("/ark:/") > 0)) {
                    // Assign ARK
                    if (nName.endsWith("Object") || nName.endsWith("Component") || nName.endsWith("File")
                            || (((Element) parentNode).isRootElement()
                                    || (parentNode.getParent().isRootElement()
                                            && parentNode.getParent().getName().equals("RDF")))) {
                        String objId = iUri;

                        if (nName.endsWith("Component") || nName.endsWith("File")) {
                            damsURI = DamsURI.toParts(iUri, null);
                            objId = damsURI.getObject();
                        }
                        String srcObjKey = objId + "::" + rdfFiles[i].getAbsolutePath();
                        oid = idsMap.get(srcObjKey);

                        // Assign new ARK
                        if (oid == null) {
                            oid = getNewId();
                            idsMap.put(srcObjKey, oid);
                        }

                        if (nName.endsWith("Object")) {
                            objId = oid;
                            objRecords.put(objId, currFile);
                        } else if (nName.endsWith("Component") || nName.endsWith("File")) {
                            damsURI.setObject(oid);
                            // XXX
                            // Assign cid and fid for Component and FIle if required
                            objId = damsURI.toString();
                        } else
                            objId = oid;
                        nUri.setText(objId);
                        updateReference(doc, iUri, objId);
                    } else {
                        String field = null;
                        Node tNode = null;
                        String xPath = null;
                        Map<String, String> props = new TreeMap<String, String>();
                        String elemXPath = parentNode.getPath();
                        if (nName.endsWith("Collection") || nName.endsWith("CollectionPart")) {
                            // Retrieve the Collection record
                            field = "title_tesim";
                            xPath = "dams:title/mads:Title/mads:authoritativeLabel";
                            tNode = parentNode.selectSingleNode(xPath);
                            if (tNode == null) {
                                // Loop through to locate the rdfs:label if not selected by xPath.
                                Node n = parentNode.selectSingleNode("dams:title");
                                for (Iterator<Element> it = ((Element) n).elementIterator(); it.hasNext();) {
                                    Element elem = it.next();
                                    if (elem.getNamespacePrefix().equals("mads")
                                            && elem.getName().equals("Title"))
                                        tNode = elem.selectSingleNode("mads:authoritativeLabel");
                                }
                            }
                        } /* else if (nName.endsWith("Language") || nName.endsWith("Authority") || nName.endsWith("Subject") || nName.endsWith("Name") || nName.endsWith("Topic") || nName.endsWith("GenreForm") || nName.endsWith("Temporal") || nName.endsWith("Geographic")){
                            // Subject, Authority records use mads:authoritativeLabel
                            field = "name_tesim";
                            xPath = "mads:authoritativeLabel";
                            tNode = parentNode.selectSingleNode(xPath);
                          } */else if (nName.endsWith(COPYRIGHT)) {
                            // Copyright records use dams:copyrightStatus, plus other properties in the next step.
                            field = "status_tesim";
                            xPath = "dams:copyrightStatus";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = copyrightProperties(parentNode);
                        } else if (nName.endsWith(LICENSE)) {
                            // License records use dams:LicenseNote, plus other properties in the next step.
                            field = "note_tesim";
                            xPath = "dams:licenseNote";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = licenseProperties(parentNode);
                        } else if (nName.endsWith(OTHERRIGHTS)) {
                            // Copyright records use dams:copyrightStatus, plus other properties in the next step.
                            field = "otherRightsBasis_tesim";
                            xPath = "dams:otherRightsBasis";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = licenseProperties(parentNode);
                        } else if (nName.endsWith(RELATEDRESOURCE)) {
                            // RelatedResource records use dams:description, plus other properties in the next step.
                            field = "description_tesim";
                            xPath = "dams:description";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = relatedResourceProperties(parentNode);
                        } else if (nName.endsWith(SOURCECAPTURE)) {
                            // SourceCapture records use dams:sourceType, plus other properties in the next step.
                            field = "sourceType_tesim";
                            xPath = "dams:sourceType";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = sourceCaptureProperties(parentNode);
                        } else if (elemXPath.indexOf("mads", elemXPath.lastIndexOf('/') + 1) >= 0) {
                            // MADSScheme and Language
                            if (nName.endsWith(MADSSCHEME) || nName.equals(LANGUAGE)) {
                                field = "code_tesim";
                                xPath = "mads:code";
                                tNode = parentNode.selectSingleNode(xPath);
                                if (tNode == null) {
                                    field = "name_tesim";
                                    xPath = "rdfs:label";
                                    tNode = parentNode.selectSingleNode(xPath);
                                    if (tNode == null) {
                                        // Loop through to locate the rdfs:label if not selected by xPath.
                                        for (Iterator<Element> it = ((Element) parentNode).elementIterator(); it
                                                .hasNext();) {
                                            Element elem = it.next();
                                            if (elem.getNamespacePrefix().equals("rdfs")
                                                    && elem.getName().equals("label"))
                                                tNode = elem;
                                        }
                                    }
                                }
                            } else {
                                // Subject, Authority records use mads:authoritativeLabel
                                field = "name_tesim";
                                xPath = "mads:authoritativeLabel";
                                tNode = parentNode.selectSingleNode(xPath);
                                if (tNode == null) {
                                    // Try to use the mads:code for mapping when mads:authoritativeLabel is not available
                                    field = "code_tesim";
                                    xPath = "mads:code";
                                    tNode = parentNode.selectSingleNode(xPath);
                                }
                                // Mapping for mads:isMemberOfMADSScheme
                                String madsScheme = null;
                                Node madsSchemeNode = parentNode.selectSingleNode("mads:isMemberOfMADSScheme");
                                if (madsSchemeNode != null) {
                                    Node msValueNode = madsSchemeNode.selectSingleNode("@rdf:resource");
                                    if (msValueNode != null) {
                                        madsScheme = madsSchemeNode.getStringValue();
                                        props.put("scheme_tesim", madsScheme);
                                    } else if ((madsSchemeNode = madsSchemeNode
                                            .selectSingleNode("mads:MADSScheme")) != null
                                            && madsSchemeNode.hasContent()) {
                                        if ((msValueNode = madsSchemeNode
                                                .selectSingleNode("mads:code")) != null) {
                                            madsScheme = msValueNode.getText();
                                            props.put("scheme_code_tesim", madsScheme);
                                        } else if ((msValueNode = madsSchemeNode
                                                .selectSingleNode("rdfs:label")) != null) {
                                            madsScheme = msValueNode.getText();
                                            props.put("scheme_name_tesim", madsScheme);
                                        }
                                    } else {
                                        props.put("scheme_tesim", "");
                                    }
                                } else {
                                    props.put("scheme_tesim", null);
                                }
                            }

                        } else {
                            // XXX Other Rights records like Statute, License, Other Rights etc. 
                            field = "value_tesim";
                            xPath = "rdf:value";
                            tNode = parentNode.selectSingleNode(xPath);
                            field = "code_tesim";
                            if (tNode == null) {
                                xPath = "dams:code";
                                tNode = parentNode.selectSingleNode(xPath);
                            }
                        }
                        if (tNode == null) {
                            throw new Exception("Element " + xPath + " is missing from the " + nName
                                    + " record " + iUri + " in file " + currFile + ".");
                        }
                        updateDocument(doc, parentNode, field, tNode.getText(), props);
                    }
                } else if (nName.endsWith("Object")) {
                    objRecords.put(iUri, currFile);
                }
            }

            String dams4Rdf = doc.asXML();
            logData("dams4_" + rdfFiles[i].getName(), dams4Rdf);

            // Ingest the records
            String subjectId = null;
            DamsURI objURI = null;
            List<DamsURI> objURIs = null;
            RDFStore graph = null;

            rdfStore = new RDFStore();
            Model rdf = rdfStore.loadRDFXML(dams4Rdf);
            initHandler();

            Model iRdf = null;
            int jLen = items.size();

            for (int j = 0; j < jLen && !interrupted; j++) {
                graph = new RDFStore();
                recordsCount++;
                // Add subject
                subjectId = items.get(j);
                try {
                    setStatus("Processing metadata for record " + subjectId + " (" + (j + 1) + " of " + jLen
                            + ") in file " + currFile + " ... ");
                    boolean succeeded = false;
                    objURIs = objects.get(subjectId);

                    for (int k = 0; k < objURIs.size(); k++) {
                        objURI = objURIs.get(k);
                        iRdf = rdfStore.querySubject(objURI.toString());
                        graph.merge(iRdf);
                    }

                    // Update object
                    //log.info(j + " ingesting record " + subjectId + ":\n" + graph.export(RDFStore.RDFXML_ABBREV_FORMAT) + "\n\n");

                    succeeded = damsClient.updateObject(subjectId, graph.export(RDFStore.RDFXML_ABBREV_FORMAT),
                            Constants.IMPORT_MODE_ADD);

                    if (!succeeded) {
                        if (metadataFailed.indexOf(currFile) < 0)
                            failedCount++;
                        metadataFailed.append(subjectId + " (" + currFile + "), \n");
                        message = "Metadata import for record " + subjectId + " failed (" + (j + 1) + " of "
                                + jLen + ") in file " + currFile + ".";
                        setStatus(message);
                        logError(message + "\n Error RDF: \n" + graph.export(RDFStore.RDFXML_ABBREV_FORMAT));
                    } else {
                        recordsIngested.add(subjectId);
                        message = "Metadata import for record " + subjectId + " succeeded (" + (j + 1) + " of "
                                + jLen + ") in file " + currFile + ". ";
                        setStatus(message);
                        logMessage(message);
                        log.info(message);

                        // Update SOLR fre records ingested.
                        updateSOLR(subjectId);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    if (metadataFailed.indexOf(currFile) < 0)
                        failedCount++;
                    metadataFailed.append(subjectId + " (" + currFile + "), \n");
                    message = "Metadata import failed: " + e.getMessage();
                    setStatus(message + " (" + (j + 1) + " of " + jLen + ") in file " + currFile + ".");
                    logError(message);
                }

                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    interrupted = true;
                    failedCount++;
                    metadataFailed.append(subjectId + " (" + currFile + ") \n");
                    message = "Metadata import interrupted for subject " + subjectId + ". \n Error: "
                            + e.getMessage() + "\n";
                    setStatus("Canceled");
                    clearSession();
                    logError(message);
                }
            }

            // Ingest the source file
            if (importOption.equalsIgnoreCase("metadataAndFiles")) {
                uploadFiles(rdf, currFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
            failedCount++;
            message = "Import failed for " + currFile + ": " + e.getMessage();
            setStatus(message + " (" + (i + 1) + " of " + fLen + ").");
            logError(message);
        } finally {
            // Update SOLR for files uploaded
            int iLen = objWithFiles.size();
            for (int j = 0; j < iLen && !interrupted; j++) {
                updateSOLR(objWithFiles.get(j));
            }
        }

        setProgressPercentage(((i + 1) * 100) / fLen);

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
            interrupted = true;
            failedCount++;
            message = "Import interrupted for oject in " + currFile + ". \n Error: " + e.getMessage() + "\n";
            setStatus("Canceled");
            clearSession();
            logError(message);
        }
    }
    return exeResult;
}

From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportHandler.java

/**
 * Update the document for DAMS ARK ID/*from  w w w .  ja va2s.  co m*/
 * @param doc
 * @param record
 * @param title
 * @throws Exception
 */
private void updateDocument(Document doc, Node record, String field, String title, Map<String, String> props)
        throws Exception {
    // Skip if the record detached
    if (record.getDocument() == null)
        return;

    // Subject, Authority records use mads:authoritativeLabel
    Node aboutAttr = record.selectSingleNode("@rdf:about");
    String srcUri = aboutAttr.getStringValue();
    //String nName = record.getName();
    String xPath = record.getPath();
    String elemName = xPath.substring(xPath.lastIndexOf("/") + 1);

    // MADSScheme model: MadsScheme
    if (elemName.endsWith("MADSScheme"))
        elemName = elemName.replace("MADSScheme", "Scheme");
    String modelName = (elemName.substring(0, 1).toUpperCase() + elemName.substring(1)).replace(":", "");
    String nKey = INFO_MODEL_PREFIX + modelName + "::" + title;
    if (props != null) {
        for (Iterator<String> it = props.keySet().iterator(); it.hasNext();) {
            String iKey = it.next();
            nKey += "_" + iKey + "::" + props.get(iKey);
        }
    }
    String oid = idsMap.get(nKey);
    // Retrieve the record
    if (oid == null) {
        /*Map<String, String> props = null;
        if(nName.endsWith(COPYRIGHT)){
           props = copyrightProperties(record);
        }*/
        oid = lookupRecord(damsClient, field, title, modelName, props);

        if (oid == null) {
            // Create the record
            oid = getNewId();
            aboutAttr.setText(oid);
            idsMap.put(nKey, oid);
            log.info("Found new redord " + srcUri + " (" + oid + ": " + field + " -- " + title);

        } else {
            // Record found. Add linking, remove it.
            toResourceLinking(oid, record);
        }
    } else {
        // Record added. Add linking, remove it.
        toResourceLinking(oid, record);
    }
    updateReference(doc, srcUri, oid);
}