Example usage for org.w3c.dom Document getTextContent

List of usage examples for org.w3c.dom Document getTextContent

Introduction

In this page you can find the example usage for org.w3c.dom Document getTextContent.

Prototype

public String getTextContent() throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:Main.java

public static Document getdoc(String uri) throws Exception {
    uri = uri.replace(" ", "%20");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;/* w  w  w  .ja  v a 2  s  .c  om*/
    db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    doc = db.parse(new URL(uri).openStream());
    if (doc == null || doc.getTextContent() == "") {
        throw new Exception();
    } else
        return doc;
}

From source file:org.kitodo.production.services.data.ImportService.java

/**
 * Convert given Document 'doc' to String and return it.
 *
 * @param doc the Document to be converted
 * @return the String content of the given Document
 *///  w w w  . jav  a2s . c  o m
private static String convertDocumentToString(Document doc) {
    try {
        StringWriter writer = new StringWriter();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        throw new ConfigException(
                "This document '" + doc.getTextContent() + "' can not be converted to String");
    }
}

From source file:org.sakaiproject.contentreview.compilatio.CompilatioReviewServiceImpl.java

@SuppressWarnings({ "deprecation" })
@Override/*w  ww. j a  v a 2s . c  o  m*/
public void checkForReports() {
    SimpleDateFormat dform = ((SimpleDateFormat) DateFormat.getDateInstance());
    dform.applyPattern(COMPILATIO_DATETIME_FORMAT);

    log.info("Fetching reports from Compilatio");

    // get the list of all items that are waiting for reports
    List<ContentReviewItem> awaitingReport = crqs.getAwaitingReports(getProviderId());

    Iterator<ContentReviewItem> listIterator = awaitingReport.iterator();
    HashMap<String, Integer> reportTable = new HashMap<>();

    log.debug("There are " + awaitingReport.size() + " submissions awaiting reports");

    int errors = 0;
    int success = 0;
    int inprogress = 0;
    ContentReviewItem currentItem;
    while (listIterator.hasNext()) {
        currentItem = (ContentReviewItem) listIterator.next();

        // has the item reached its next retry time?
        if (currentItem.getNextRetryTime() == null) {
            currentItem.setNextRetryTime(new Date());
        }

        if (currentItem.getNextRetryTime().after(new Date())) {
            // we haven't reached the next retry time
            log.info("checkForReports :: next retry time not yet reached for item: " + currentItem.getId());
            crqs.update(currentItem);
            continue;
        }

        if (currentItem.getRetryCount() == null) {
            currentItem.setRetryCount(Long.valueOf(0));
            currentItem.setNextRetryTime(this.getNextRetryTime(0));
        } else if (currentItem.getRetryCount().intValue() > maxRetry) {
            processError(currentItem,
                    ContentReviewConstants.CONTENT_REVIEW_SUBMISSION_ERROR_RETRY_EXCEEDED_CODE, null, null);
            errors++;
            continue;
        } else {
            long l = currentItem.getRetryCount().longValue();
            log.debug("Still have retries left (" + l + " <= " + maxRetry + "), continuing. ItemID: "
                    + currentItem.getId());
            l++;
            currentItem.setRetryCount(Long.valueOf(l));
            currentItem.setNextRetryTime(this.getNextRetryTime(Long.valueOf(l)));
            crqs.update(currentItem);
        }

        //back to analysis (this should not happen)
        if (StringUtils.isBlank(currentItem.getExternalId())) {
            currentItem
                    .setStatus(Long.valueOf(ContentReviewConstants.CONTENT_REVIEW_SUBMISSION_ERROR_RETRY_CODE));
            crqs.update(currentItem);
            errors++;
            continue;
        }

        if (!reportTable.containsKey(currentItem.getExternalId())) {
            // get the list from compilatio and see if the review is
            // available

            log.debug("Attempting to update hashtable with reports for site " + currentItem.getSiteId());

            Map<String, String> params = CompilatioAPIUtil.packMap("action", "getDocument", "idDocument",
                    currentItem.getExternalId());

            Document document = null;
            try {
                document = compilatioConn.callCompilatioReturnDocument(params);
            } catch (TransientSubmissionException | SubmissionException e) {
                log.warn("Update failed : " + e.toString(), e);
                processError(currentItem, ContentReviewConstants.CONTENT_REVIEW_REPORT_ERROR_RETRY_CODE,
                        e.getMessage(), null);
                errors++;
                continue;
            }

            Element root = document.getDocumentElement();
            if (root.getElementsByTagName("documentStatus").item(0) != null) {
                log.debug("Report list returned successfully");

                NodeList objects = root.getElementsByTagName("documentStatus");
                log.debug(objects.getLength() + " objects in the returned list");

                String status = getNodeValue("status", root);

                if ("ANALYSE_NOT_STARTED".equals(status)) {
                    //send back to the process queue, we need no analyze it again
                    processError(currentItem, ContentReviewConstants.CONTENT_REVIEW_SUBMISSION_ERROR_RETRY_CODE,
                            "ANALYSE_NOT_STARTED", null);
                    errors++;
                    continue;
                } else if ("ANALYSE_COMPLETE".equals(status)) {
                    String reportVal = getNodeValue("indice", root);
                    currentItem.setReviewScore((int) Math.round(Double.parseDouble(reportVal)));
                    currentItem
                            .setStatus(ContentReviewConstants.CONTENT_REVIEW_SUBMITTED_REPORT_AVAILABLE_CODE);
                    success++;
                } else {
                    String progression = getNodeValue("progression", root);
                    if (StringUtils.isNotBlank(progression)) {
                        currentItem.setReviewScore((int) Double.parseDouble(progression));
                        inprogress++;
                    }
                }
                currentItem.setDateReportReceived(new Date());
                crqs.update(currentItem);
                log.debug("new report received: " + currentItem.getExternalId() + " -> "
                        + currentItem.getReviewScore());

            } else {
                log.debug("Report list request not successful");
                log.debug(document.getTextContent());
                errors++;
            }
        }
    }

    log.info("Finished fetching reports from Compilatio : " + success + " success items, " + inprogress
            + " in progress, " + errors + " errors");
}

From source file:org.sakaiproject.contentreview.impl.compilatio.CompilatioReviewServiceImpl.java

@SuppressWarnings({ "deprecation" })
@Override/*from w w  w .j a v a2s  .  c o  m*/
public void checkForReports() {
    SimpleDateFormat dform = ((SimpleDateFormat) DateFormat.getDateInstance());
    dform.applyPattern(COMPILATIO_DATETIME_FORMAT);

    log.info("Fetching reports from Compilatio");

    // get the list of all items that are waiting for reports
    Search search = new Search();
    search.setConjunction(false); //OR clauses
    search.addRestriction(new Restriction("status", ContentReviewItem.SUBMITTED_AWAITING_REPORT_CODE));
    search.addRestriction(new Restriction("status", ContentReviewItem.REPORT_ERROR_RETRY_CODE));
    List<ContentReviewItem> awaitingReport = dao.findBySearch(ContentReviewItem.class, search);

    Iterator<ContentReviewItem> listIterator = awaitingReport.iterator();

    log.debug("There are " + awaitingReport.size() + " submissions awaiting reports");

    int errors = 0;
    int success = 0;
    int inprogress = 0;
    ContentReviewItem currentItem;
    while (listIterator.hasNext()) {
        currentItem = (ContentReviewItem) listIterator.next();

        // has the item reached its next retry time?
        if (currentItem.getNextRetryTime() == null) {
            currentItem.setNextRetryTime(new Date());
        }

        if (currentItem.getNextRetryTime().after(new Date())) {
            // we haven't reached the next retry time
            log.info("checkForReports :: next retry time not yet reached for item: " + currentItem.getId());
            dao.update(currentItem);
            continue;
        }

        if (!processItem(currentItem)) {
            errors++;
            continue;
        }

        //back to analysis (this should not happen)
        if (StringUtils.isBlank(currentItem.getExternalId())) {
            currentItem.setStatus(Long.valueOf(ContentReviewItem.SUBMISSION_ERROR_RETRY_CODE));
            dao.update(currentItem);
            errors++;
            continue;
        }

        // get the list from compilatio and see if the review is
        // available

        log.debug("Attempting to update hashtable with reports for site " + currentItem.getSiteId());

        Map<String, String> params = CompilatioAPIUtil.packMap("action", "getDocument", "idDocument",
                currentItem.getExternalId());

        Document document = null;
        try {
            document = compilatioConn.callCompilatioReturnDocument(params);
        } catch (TransientSubmissionException | SubmissionException e) {
            log.warn("Update failed : " + e.toString(), e);
            processError(currentItem, ContentReviewItem.REPORT_ERROR_RETRY_CODE, e.getMessage(), null);
            errors++;
            continue;
        }

        Element root = document.getDocumentElement();
        if (root.getElementsByTagName("documentStatus").item(0) != null) {
            log.debug("Report list returned successfully");

            NodeList objects = root.getElementsByTagName("documentStatus");
            log.debug(objects.getLength() + " objects in the returned list");

            String status = getNodeValue("status", root);

            if ("ANALYSE_NOT_STARTED".equals(status)) {
                //send back to the process queue, we need no analyze it again
                processError(currentItem, ContentReviewItem.SUBMISSION_ERROR_RETRY_CODE, "ANALYSE_NOT_STARTED",
                        null);
                errors++;
                continue;
            } else if ("ANALYSE_COMPLETE".equals(status)) {
                String reportVal = getNodeValue("indice", root);
                currentItem.setReviewScore((int) Math.round(Double.parseDouble(reportVal)));
                currentItem.setStatus(ContentReviewItem.SUBMITTED_REPORT_AVAILABLE_CODE);
                success++;
            } else {
                String progression = getNodeValue("progression", root);
                if (StringUtils.isNotBlank(progression)) {
                    currentItem.setReviewScore((int) Double.parseDouble(progression));
                    inprogress++;
                }
            }
            currentItem.setDateReportReceived(new Date());
            dao.update(currentItem);
            log.debug("new report received: " + currentItem.getExternalId() + " -> "
                    + currentItem.getReviewScore());

        } else {
            log.debug("Report list request not successful");
            log.debug(document.getTextContent());

        }
    }

    log.info("Finished fetching reports from Compilatio : " + success + " success items, " + inprogress
            + " in progress, " + errors + " errors");
}

From source file:org.sakaiproject.contentreview.impl.turnitin.TurnitinMigrateSRC.java

/**
 * Call Turnitin Migrate function//from  w  w w.jav a 2s .  c  om
 * @throws SubmissionException
 * @throws TransientSubmissionException
 */
public void migrateSRC() throws SubmissionException, TransientSubmissionException {
    log.info("Starting SRC Migration");
    Map params = new HashMap();
    params.putAll(turnitinConn.getBaseTIIOptions());
    params.put("fid", "99");
    params.put("fcmd", "2");
    params.put("utp", "2");
    params.put("uem", "fid99@turnitin.com");
    params.put("ufn", "FID99");
    params.put("uln", "Turnitin");

    Document document = null;
    try {
        document = turnitinConn.callTurnitinReturnDocument(params);
    } catch (TransientSubmissionException e) {
        log.debug("Update failed due to TransientSubmissionException error: " + e.toString());
    } catch (SubmissionException e) {
        log.debug("Update failed due to SubmissionException error: " + e.toString());
    } catch (Exception e) {
        log.debug(e.toString());
    }

    Element root = document.getDocumentElement();
    if (((CharacterData) (root.getElementsByTagName("rcode").item(0).getFirstChild())).getData().trim()
            .compareTo("99") == 0) {
        log.info("SRC Migration Successful");
    } else {
        log.debug("SRC Migration Not Successful");
        log.debug(document.getTextContent());
    }
}

From source file:org.sakaiproject.contentreview.impl.turnitin.TurnitinReviewServiceImpl.java

/**
* Check Turnitin for grades and write them to the associated gradebook
* @param data Map containing relevant IDs (site ID, Assignment ID, Title)
*//*w  w w  .  ja  v  a  2  s  .  c o m*/
public void syncGrades(Map<String, Object> data) {
    //Get session and check if gardes have already been synced
    Session sess = sessionManager.getCurrentSession();
    boolean runOnce = gradesChecked(sess, data.get("taskId").toString());
    boolean isStudent = isUserStudent(data.get("siteId").toString(), sess.getUserId());

    if (turnitinConn.getUseGradeMark() && runOnce == false && isStudent == false) {
        log.info("Syncing Grades with Turnitin");

        String siteId = data.get("siteId").toString();
        String taskId = data.get("taskId").toString();

        HashMap<String, Integer> reportTable = new HashMap<>();
        HashMap<String, String> additionalData = new HashMap<>();
        String tiiUserId;

        String assign = taskId;
        if (data.containsKey("assignment1")) {
            //Assignments 1 uses the actual title whereas Assignments 2 uses the ID
            assign = getAssignmentTitle(taskId);
        }

        //Run once
        sess.setAttribute("sync", taskId);

        //Get students enrolled on class in Turnitin
        Map<String, Object> enrollmentInfo = getAllEnrollmentInfo(siteId);

        //Get Associated GB item
        Assignment assignment = getAssociatedGbItem(data);

        //List submissions call
        Map params = TurnitinAPIUtil.packMap(turnitinConn.getBaseTIIOptions(), "fid", "10", "fcmd", "2", "tem",
                getTEM(siteId), "assign", assign, "assignid", taskId, "cid", siteId, "ctl", siteId, "utp", "2");
        params.putAll(getInstructorInfo(siteId));

        Document document = null;
        try {
            document = turnitinConn.callTurnitinReturnDocument(params);
        } catch (TransientSubmissionException e) {
            log.error(e);
        } catch (SubmissionException e) {
            log.warn("SubmissionException error. " + e);
        }
        Element root = document.getDocumentElement();
        if (((CharacterData) (root.getElementsByTagName("rcode").item(0).getFirstChild())).getData().trim()
                .compareTo("72") == 0) {
            NodeList objects = root.getElementsByTagName("object");
            String grade;
            log.debug(objects.getLength() + " objects in the returned list");

            for (int i = 0; i < objects.getLength(); i++) {
                tiiUserId = ((CharacterData) (((Element) (objects.item(i))).getElementsByTagName("userid")
                        .item(0).getFirstChild())).getData().trim();
                additionalData.put("tiiUserId", tiiUserId);
                //Get GradeMark Grade
                try {
                    grade = ((CharacterData) (((Element) (objects.item(i))).getElementsByTagName("score")
                            .item(0).getFirstChild())).getData().trim();
                    reportTable.put("grade" + tiiUserId, Integer.valueOf(grade));
                } catch (DOMException | NumberFormatException e) {
                    //No score returned
                    grade = "";
                } catch (Exception e) {
                    grade = "";
                    log.error("Unexpected exception getting grade", e);
                }

                if (!grade.equals("")) {
                    //Update Grade ----------------
                    if (gradebookService.isGradebookDefined(siteId)) {
                        writeGrade(assignment, data, reportTable, additionalData, enrollmentInfo);
                    }
                }
            }
        } else {
            log.debug("Report list request not successful");
            log.debug(document.getTextContent());
        }
    }
}

From source file:org.sakaiproject.contentreview.impl.turnitin.TurnitinReviewServiceImpl.java

@SuppressWarnings({ "deprecation", "unchecked" })
public void checkForReportsBulk() {

    SimpleDateFormat dform = ((SimpleDateFormat) DateFormat.getDateInstance());
    dform.applyPattern(TURNITIN_DATETIME_FORMAT);

    log.info("Fetching reports from Turnitin");

    // get the list of all items that are waiting for reports
    // but skip items with externalId = null, this happens when the LTI integration's callback fails. In this case, they'll be resubmitted by the queue job.
    // For the Sakai API integration, we should never enter the report state with externalId = null
    List<ContentReviewItem> awaitingReport = dao.findByProperties(ContentReviewItem.class,
            new String[] { "status", "externalId" },
            new Object[] { ContentReviewItem.SUBMITTED_AWAITING_REPORT_CODE, "" },
            new int[] { dao.EQUALS, dao.NOT_NULL });

    awaitingReport.addAll(dao.findByProperties(ContentReviewItem.class, new String[] { "status", "externalId" },
            new Object[] { ContentReviewItem.REPORT_ERROR_RETRY_CODE, "" },
            new int[] { dao.EQUALS, dao.NOT_NULL }));

    Iterator<ContentReviewItem> listIterator = awaitingReport.iterator();
    HashMap<String, Integer> reportTable = new HashMap<>();

    log.debug("There are " + awaitingReport.size() + " submissions awaiting reports");

    ContentReviewItem currentItem;// w  w w. j ava 2 s.  c om
    while (listIterator.hasNext()) {
        currentItem = (ContentReviewItem) listIterator.next();

        try {
            // has the item reached its next retry time?
            if (currentItem.getNextRetryTime() == null)
                currentItem.setNextRetryTime(new Date());

            else if (currentItem.getNextRetryTime().after(new Date())) {
                //we haven't reached the next retry time
                log.info("next retry time not yet reached for item: " + currentItem.getId());
                dao.update(currentItem);
                continue;
            }

            if (currentItem.getRetryCount() == null) {
                currentItem.setRetryCount(Long.valueOf(0));
                currentItem.setNextRetryTime(this.getNextRetryTime(0));
            } else if (currentItem.getRetryCount().intValue() > maxRetry) {
                processError(currentItem, ContentReviewItem.SUBMISSION_ERROR_RETRY_EXCEEDED, null, null);
                continue;
            } else {
                log.debug("Still have retries left, continuing. ItemID: " + currentItem.getId());
                // Moving down to check for report generate speed.
                //long l = currentItem.getRetryCount().longValue();
                //l++;
                //currentItem.setRetryCount(Long.valueOf(l));
                //currentItem.setNextRetryTime(this.getNextRetryTime(Long.valueOf(l)));
                //dao.update(currentItem);
            }

            Site s;
            try {
                s = siteService.getSite(currentItem.getSiteId());
            } catch (IdUnusedException iue) {
                log.warn("checkForReportsBulk: Site " + currentItem.getSiteId() + " not found!"
                        + iue.getMessage());
                long l = currentItem.getRetryCount();
                l++;
                currentItem.setRetryCount(l);
                currentItem.setNextRetryTime(this.getNextRetryTime(l));
                currentItem.setLastError("Site not found");
                dao.update(currentItem);
                continue;
            }
            //////////////////////////////  NEW LTI INTEGRATION  ///////////////////////////////
            Optional<Date> dateOpt = getAssignmentCreationDate(currentItem.getTaskId());
            if (dateOpt.isPresent() && siteAdvisor.siteCanUseLTIReviewServiceForAssignment(s, dateOpt.get())) {
                log.debug("getReviewScore using the LTI integration");

                Map<String, String> ltiProps = new HashMap<>();
                ltiProps = putInstructorInfo(ltiProps, currentItem.getSiteId());

                String paperId = currentItem.getExternalId();

                if (paperId == null) {
                    log.warn("Could not find TII paper id for the content " + currentItem.getContentId());
                    long l = currentItem.getRetryCount();
                    l++;
                    currentItem.setRetryCount(l);
                    currentItem.setNextRetryTime(this.getNextRetryTime(l));
                    currentItem.setLastError("Could not find TII paper id for the submission");
                    dao.update(currentItem);
                    continue;
                }

                TurnitinReturnValue result = tiiUtil.makeLTIcall(TurnitinLTIUtil.INFO_SUBMISSION, paperId,
                        ltiProps);
                if (result.getResult() >= 0) {
                    currentItem.setReviewScore(result.getResult());
                    currentItem.setStatus(ContentReviewItem.SUBMITTED_REPORT_AVAILABLE_CODE);
                    currentItem.setDateReportReceived(new Date());
                    currentItem.setLastError(null);
                    currentItem.setErrorCode(null);
                    dao.update(currentItem);

                    try {
                        ContentResource resource = contentHostingService
                                .getResource(currentItem.getContentId());
                        boolean itemUpdated = updateItemAccess(resource.getId());
                        if (!itemUpdated) {
                            log.error("Could not update cr item access status");
                        }
                    } catch (PermissionException | IdUnusedException | TypeException ex) {
                        log.error("Could not update cr item access status", ex);
                    }

                    //log.debug("new report received: " + currentItem.getExternalId() + " -> " + currentItem.getReviewScore());
                    log.debug("new report received: " + paperId + " -> " + currentItem.getReviewScore());
                } else {
                    if (result.getResult() == -7) {
                        log.debug("report is still pending for paper " + paperId);
                        currentItem.setStatus(ContentReviewItem.SUBMITTED_AWAITING_REPORT_CODE);
                        currentItem.setLastError(result.getErrorMessage());
                        currentItem.setErrorCode(result.getResult());
                    } else {
                        log.error("Error making LTI call");
                        long l = currentItem.getRetryCount();
                        l++;
                        currentItem.setRetryCount(l);
                        currentItem.setNextRetryTime(this.getNextRetryTime(l));
                        currentItem.setStatus(ContentReviewItem.REPORT_ERROR_RETRY_CODE);
                        currentItem.setLastError("Report Data Error: " + result.getResult());
                    }
                    dao.update(currentItem);
                }

                continue;
            }
            //////////////////////////////  OLD API INTEGRATION  ///////////////////////////////

            if (currentItem.getExternalId() == null || currentItem.getExternalId().equals("")) {
                currentItem.setStatus(Long.valueOf(4));
                dao.update(currentItem);
                continue;
            }

            if (!reportTable.containsKey(currentItem.getExternalId())) {
                // get the list from turnitin and see if the review is available

                log.debug("Attempting to update hashtable with reports for site " + currentItem.getSiteId());

                String fcmd = "2";
                String fid = "10";

                try {
                    User user = userDirectoryService.getUser(currentItem.getUserId());
                } catch (Exception e) {
                    log.error("Unable to look up user: " + currentItem.getUserId() + " for contentItem: "
                            + currentItem.getId(), e);
                }

                String cid = currentItem.getSiteId();
                String tem = getTEM(cid);

                String utp = "2";

                String assignid = currentItem.getTaskId();

                String assign = currentItem.getTaskId();
                String ctl = currentItem.getSiteId();

                // TODO FIXME Current sgithens
                // Move the update setRetryAttempts to here, and first call and
                // check the assignment from TII to see if the generate until
                // due is enabled. In that case we don't want to waste retry
                // attempts and should just continue.
                try {
                    // TODO FIXME This is broken at the moment because we need
                    // to have a userid, but this is assuming it's coming from
                    // the thread, but we're in a quartz job.
                    //Map curasnn = getAssignment(currentItem.getSiteId(), currentItem.getTaskId());
                    // TODO FIXME Parameterize getAssignment method to take user information
                    Map getAsnnParams = TurnitinAPIUtil.packMap(turnitinConn.getBaseTIIOptions(), "assign",
                            getAssignmentTitle(currentItem.getTaskId()), "assignid", currentItem.getTaskId(),
                            "cid", currentItem.getSiteId(), "ctl", currentItem.getSiteId(), "fcmd", "7", "fid",
                            "4", "utp", "2");

                    getAsnnParams.putAll(getInstructorInfo(currentItem.getSiteId()));

                    Map curasnn = turnitinConn.callTurnitinReturnMap(getAsnnParams);

                    if (curasnn.containsKey("object")) {
                        Map curasnnobj = (Map) curasnn.get("object");
                        String reportGenSpeed = (String) curasnnobj.get("generate");
                        String duedate = (String) curasnnobj.get("dtdue");
                        SimpleDateFormat retform = ((SimpleDateFormat) DateFormat.getDateInstance());
                        retform.applyPattern(TURNITIN_DATETIME_FORMAT);
                        Date duedateObj = null;
                        try {
                            if (duedate != null) {
                                duedateObj = retform.parse(duedate);
                            }
                        } catch (ParseException pe) {
                            log.warn("Unable to parse turnitin dtdue: " + duedate, pe);
                        }
                        if (reportGenSpeed != null && duedateObj != null && reportGenSpeed.equals("2")
                                && duedateObj.after(new Date())) {
                            log.info("Report generate speed is 2, skipping for now. ItemID: "
                                    + currentItem.getId());
                            // If there was previously a transient error for this item, reset the status
                            if (ContentReviewItem.REPORT_ERROR_RETRY_CODE.equals(currentItem.getStatus())) {
                                currentItem.setStatus(ContentReviewItem.SUBMITTED_AWAITING_REPORT_CODE);
                                currentItem.setLastError(null);
                                currentItem.setErrorCode(null);
                                dao.update(currentItem);
                            }
                            continue;
                        } else {
                            log.debug("Incrementing retry count for currentItem: " + currentItem.getId());
                            long l = currentItem.getRetryCount();
                            l++;
                            currentItem.setRetryCount(l);
                            currentItem.setNextRetryTime(this.getNextRetryTime(l));
                            dao.update(currentItem);
                        }
                    }
                } catch (SubmissionException | TransientSubmissionException e) {
                    log.error(
                            "Unable to check the report gen speed of the asnn for item: " + currentItem.getId(),
                            e);
                }

                Map params = TurnitinAPIUtil.packMap(turnitinConn.getBaseTIIOptions(), "fid", fid, "fcmd", fcmd,
                        "tem", tem, "assign", assign, "assignid", assignid, "cid", cid, "ctl", ctl, "utp", utp);
                params.putAll(getInstructorInfo(currentItem.getSiteId()));

                Document document;

                try {
                    document = turnitinConn.callTurnitinReturnDocument(params);
                } catch (TransientSubmissionException e) {
                    log.warn("Update failed due to TransientSubmissionException error: " + e.toString(), e);
                    currentItem.setStatus(ContentReviewItem.REPORT_ERROR_RETRY_CODE);
                    currentItem.setLastError(e.getMessage());
                    dao.update(currentItem);
                    break;
                } catch (SubmissionException e) {
                    log.warn("Update failed due to SubmissionException error: " + e.toString(), e);
                    currentItem.setStatus(ContentReviewItem.REPORT_ERROR_RETRY_CODE);
                    currentItem.setLastError(e.getMessage());
                    dao.update(currentItem);
                    break;
                }

                Element root = document.getDocumentElement();
                if (((CharacterData) (root.getElementsByTagName("rcode").item(0).getFirstChild())).getData()
                        .trim().compareTo("72") == 0) {
                    log.debug("Report list returned successfully");

                    NodeList objects = root.getElementsByTagName("object");
                    String objectId;
                    String similarityScore;
                    String overlap = "";
                    log.debug(objects.getLength() + " objects in the returned list");
                    for (int i = 0; i < objects.getLength(); i++) {
                        similarityScore = ((CharacterData) (((Element) (objects.item(i)))
                                .getElementsByTagName("similarityScore").item(0).getFirstChild())).getData()
                                        .trim();
                        objectId = ((CharacterData) (((Element) (objects.item(i)))
                                .getElementsByTagName("objectID").item(0).getFirstChild())).getData().trim();
                        if (similarityScore.compareTo("-1") != 0) {
                            overlap = ((CharacterData) (((Element) (objects.item(i)))
                                    .getElementsByTagName("overlap").item(0).getFirstChild())).getData().trim();
                            reportTable.put(objectId, Integer.valueOf(overlap));
                        } else {
                            reportTable.put(objectId, -1);
                        }

                        log.debug("objectId: " + objectId + " similarity: " + similarityScore + " overlap: "
                                + overlap);
                    }
                } else {
                    log.debug("Report list request not successful");
                    log.debug(document.getTextContent());

                }
            }

            int reportVal;
            // check if the report value is now there (there may have been a
            // failure to get the list above)
            if (reportTable.containsKey(currentItem.getExternalId())) {
                reportVal = ((reportTable.get(currentItem.getExternalId())));
                log.debug("reportVal for " + currentItem.getExternalId() + ": " + reportVal);
                if (reportVal != -1) {
                    currentItem.setReviewScore(reportVal);
                    currentItem.setStatus(ContentReviewItem.SUBMITTED_REPORT_AVAILABLE_CODE);
                    currentItem.setDateReportReceived(new Date());
                    currentItem.setLastError(null);
                    currentItem.setErrorCode(null);
                    dao.update(currentItem);

                    try {
                        ContentResource resource = contentHostingService
                                .getResource(currentItem.getContentId());
                        boolean itemUpdated = updateItemAccess(resource.getId());
                        if (!itemUpdated) {
                            log.error("Could not update cr item access status");
                        }
                    } catch (PermissionException | IdUnusedException | TypeException ex) {
                        log.error("Could not update cr item access status", ex);
                    }

                    log.debug("new report received: " + currentItem.getExternalId() + " -> "
                            + currentItem.getReviewScore());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage() + "\n" + e.getStackTrace());
        }
    }

    log.info("Finished fetching reports from Turnitin");
}

From source file:org.sakaiproject.contentreview.turnitin.TurnitinReviewServiceImpl.java

/**
 * Check Turnitin for grades and write them to the associated gradebook
 * /*  w ww  .  j a va  2  s . c  o m*/
 * @param data
 *            Map containing relevant IDs (site ID, Assignment ID, Title)
 */
public void syncGrades(Map<String, Object> data) {
    // Get session and check if gardes have already been synced
    Session sess = sessionManager.getCurrentSession();
    boolean runOnce = gradesChecked(sess, data.get("taskId").toString());
    boolean isStudent = isUserStudent(data.get("siteId").toString(), sess.getUserId());

    if (turnitinConn.getUseGradeMark() && runOnce == false && isStudent == false) {
        log.info("Syncing Grades with Turnitin");

        String siteId = data.get("siteId").toString();
        String taskId = data.get("taskId").toString();

        HashMap<String, Integer> reportTable = new HashMap<String, Integer>();
        HashMap<String, String> additionalData = new HashMap<String, String>();
        String tiiUserId = "";

        String assign = taskId;
        if (data.containsKey("assignment1")) {
            // Assignments 1 uses the actual title whereas Assignments 2
            // uses the ID
            assign = getAssignmentTitle(taskId);
        }

        // Run once
        sess.setAttribute("sync", taskId);

        // Get students enrolled on class in Turnitin
        Map<String, Object> enrollmentInfo = getAllEnrollmentInfo(siteId);

        // Get Associated GB item
        Assignment assignment = getAssociatedGbItem(data);

        // List submissions call
        Map params = new HashMap();
        params = TurnitinAPIUtil.packMap(turnitinConn.getBaseTIIOptions(), "fid", "10", "fcmd", "2", "tem",
                getTEM(siteId), "assign", assign, "assignid", taskId, "cid", siteId, "ctl", siteId, "utp", "2");
        params.putAll(getInstructorInfo(siteId));

        Document document = null;
        try {
            document = turnitinConn.callTurnitinReturnDocument(params);
        } catch (TransientSubmissionException e) {
            log.error(e.getMessage());
        } catch (SubmissionException e) {
            log.warn("SubmissionException error. " + e.getMessage());
        }
        Element root = document.getDocumentElement();
        if (((CharacterData) (root.getElementsByTagName("rcode").item(0).getFirstChild())).getData().trim()
                .compareTo("72") == 0) {
            NodeList objects = root.getElementsByTagName("object");
            String grade = "";
            log.debug(objects.getLength() + " objects in the returned list");

            for (int i = 0; i < objects.getLength(); i++) {
                tiiUserId = ((CharacterData) (((Element) (objects.item(i))).getElementsByTagName("userid")
                        .item(0).getFirstChild())).getData().trim();
                additionalData.put("tiiUserId", tiiUserId);
                // Get GradeMark Grade
                try {
                    grade = ((CharacterData) (((Element) (objects.item(i))).getElementsByTagName("score")
                            .item(0).getFirstChild())).getData().trim();
                    reportTable.put("grade" + tiiUserId, Integer.valueOf(grade));
                } catch (Exception e) {
                    // No score returned
                    grade = "";
                }

                if (!grade.equals("")) {
                    // Update Grade ----------------
                    if (gradebookService.isGradebookDefined(siteId)) {
                        writeGrade(assignment, data, reportTable, additionalData, enrollmentInfo);
                    }
                }
            }
        } else {
            log.debug("Report list request not successful");
            log.debug(document.getTextContent());
        }
    }
}

From source file:org.sakaiproject.contentreview.turnitin.TurnitinReviewServiceImpl.java

@SuppressWarnings({ "deprecation", "unchecked" })
public void checkForReportsBulk() {

    SimpleDateFormat dform = ((SimpleDateFormat) DateFormat.getDateInstance());
    dform.applyPattern(TURNITIN_DATETIME_FORMAT);

    log.info("Fetching reports from Turnitin");

    // get the list of all items that are waiting for reports
    List<ContentReviewItem> awaitingReport = crqs.getAwaitingReports(getProviderId());

    Iterator<ContentReviewItem> listIterator = awaitingReport.iterator();
    HashMap<String, Integer> reportTable = new HashMap<String, Integer>();

    log.debug("There are " + awaitingReport.size() + " submissions awaiting reports");

    ContentReviewItem currentItem;//from  ww  w  .j a v  a  2 s. c  om
    while (listIterator.hasNext()) {
        currentItem = (ContentReviewItem) listIterator.next();

        // has the item reached its next retry time?
        if (currentItem.getNextRetryTime() == null)
            currentItem.setNextRetryTime(new Date());

        if (currentItem.getNextRetryTime().after(new Date())) {
            // we haven't reached the next retry time
            log.info("next retry time not yet reached for item: " + currentItem.getId());
            crqs.update(currentItem);
            continue;
        }

        if (currentItem.getRetryCount() == null) {
            currentItem.setRetryCount(Long.valueOf(0));
            currentItem.setNextRetryTime(this.getNextRetryTime(0));
        } else if (currentItem.getRetryCount().intValue() > maxRetry) {
            currentItem.setStatus(ContentReviewConstants.CONTENT_REVIEW_SUBMISSION_ERROR_RETRY_EXCEEDED_CODE);
            crqs.update(currentItem);
            continue;
        } else {
            log.debug("Still have retries left, continuing. ItemID: " + currentItem.getId());
            // Moving down to check for report generate speed.
            // long l = currentItem.getRetryCount().longValue();
            // l++;
            // currentItem.setRetryCount(Long.valueOf(l));
            // currentItem.setNextRetryTime(this.getNextRetryTime(Long.valueOf(l)));
            // dao.update(currentItem);
        }

        if (currentItem.getExternalId() == null || currentItem.getExternalId().equals("")) {
            currentItem.setStatus(Long.valueOf(4));
            crqs.update(currentItem);
            continue;
        }

        if (!reportTable.containsKey(currentItem.getExternalId())) {
            // get the list from turnitin and see if the review is available

            log.debug("Attempting to update hashtable with reports for site " + currentItem.getSiteId());

            String fcmd = "2";
            String fid = "10";

            try {
                User user = userDirectoryService.getUser(currentItem.getUserId());
            } catch (Exception e) {
                log.error("Unable to look up user: " + currentItem.getUserId() + " for contentItem: "
                        + currentItem.getId(), e);
            }

            String cid = currentItem.getSiteId();
            String tem = getTEM(cid);

            String utp = "2";

            String assignid = currentItem.getTaskId();

            String assign = currentItem.getTaskId();
            String ctl = currentItem.getSiteId();

            // TODO FIXME Current sgithens
            // Move the update setRetryAttempts to here, and first call and
            // check the assignment from TII to see if the generate until
            // due is enabled. In that case we don't want to waste retry
            // attempts and should just continue.
            try {
                // TODO FIXME This is broken at the moment because we need
                // to have a userid, but this is assuming it's coming from
                // the thread, but we're in a quartz job.
                // Map curasnn = getAssignment(currentItem.getSiteId(),
                // currentItem.getTaskId());
                // TODO FIXME Parameterize getAssignment method to take user
                // information
                Map getAsnnParams = TurnitinAPIUtil.packMap(turnitinConn.getBaseTIIOptions(), "assign",
                        getAssignmentTitle(currentItem.getTaskId()), "assignid", currentItem.getTaskId(), "cid",
                        currentItem.getSiteId(), "ctl", currentItem.getSiteId(), "fcmd", "7", "fid", "4", "utp",
                        "2");

                getAsnnParams.putAll(getInstructorInfo(currentItem.getSiteId()));

                Map curasnn = turnitinConn.callTurnitinReturnMap(getAsnnParams);

                if (curasnn.containsKey("object")) {
                    Map curasnnobj = (Map) curasnn.get("object");
                    String reportGenSpeed = (String) curasnnobj.get("generate");
                    String duedate = (String) curasnnobj.get("dtdue");
                    SimpleDateFormat retform = ((SimpleDateFormat) DateFormat.getDateInstance());
                    retform.applyPattern(TURNITIN_DATETIME_FORMAT);
                    Date duedateObj = null;
                    try {
                        if (duedate != null) {
                            duedateObj = retform.parse(duedate);
                        }
                    } catch (ParseException pe) {
                        log.warn("Unable to parse turnitin dtdue: " + duedate, pe);
                    }
                    if (reportGenSpeed != null && duedateObj != null && reportGenSpeed.equals("2")
                            && duedateObj.after(new Date())) {
                        log.info(
                                "Report generate speed is 2, skipping for now. ItemID: " + currentItem.getId());
                        // If there was previously a transient error for
                        // this item, reset the status
                        if (ContentReviewConstants.CONTENT_REVIEW_REPORT_ERROR_RETRY_CODE
                                .equals(currentItem.getStatus())) {
                            currentItem.setStatus(
                                    ContentReviewConstants.CONTENT_REVIEW_SUBMITTED_AWAITING_REPORT_CODE);
                            currentItem.setLastError(null);
                            currentItem.setErrorCode(null);
                            crqs.update(currentItem);
                        }
                        continue;
                    } else {
                        log.debug("Incrementing retry count for currentItem: " + currentItem.getId());
                        long l = currentItem.getRetryCount().longValue();
                        l++;
                        currentItem.setRetryCount(Long.valueOf(l));
                        currentItem.setNextRetryTime(this.getNextRetryTime(Long.valueOf(l)));
                        crqs.update(currentItem);
                    }
                }
            } catch (SubmissionException e) {
                log.error("Unable to check the report gen speed of the asnn for item: " + currentItem.getId(),
                        e);
            } catch (TransientSubmissionException e) {
                log.error("Unable to check the report gen speed of the asnn for item: " + currentItem.getId(),
                        e);
            }

            Map params = new HashMap();
            // try {
            params = TurnitinAPIUtil.packMap(turnitinConn.getBaseTIIOptions(), "fid", fid, "fcmd", fcmd, "tem",
                    tem, "assign", assign, "assignid", assignid, "cid", cid, "ctl", ctl, "utp", utp);
            params.putAll(getInstructorInfo(currentItem.getSiteId()));

            Document document = null;

            try {
                document = turnitinConn.callTurnitinReturnDocument(params);
            } catch (TransientSubmissionException e) {
                log.warn("Update failed due to TransientSubmissionException error: " + e.toString(), e);
                currentItem.setStatus(ContentReviewConstants.CONTENT_REVIEW_REPORT_ERROR_RETRY_CODE);
                currentItem.setLastError(e.getMessage());
                crqs.update(currentItem);
                break;
            } catch (SubmissionException e) {
                log.warn("Update failed due to SubmissionException error: " + e.toString(), e);
                currentItem.setStatus(ContentReviewConstants.CONTENT_REVIEW_REPORT_ERROR_RETRY_CODE);
                currentItem.setLastError(e.getMessage());
                crqs.update(currentItem);
                break;
            }

            Element root = document.getDocumentElement();
            if (((CharacterData) (root.getElementsByTagName("rcode").item(0).getFirstChild())).getData().trim()
                    .compareTo("72") == 0) {
                log.debug("Report list returned successfully");

                NodeList objects = root.getElementsByTagName("object");
                String objectId;
                String similarityScore;
                String overlap = "";
                log.debug(objects.getLength() + " objects in the returned list");
                for (int i = 0; i < objects.getLength(); i++) {
                    similarityScore = ((CharacterData) (((Element) (objects.item(i)))
                            .getElementsByTagName("similarityScore").item(0).getFirstChild())).getData().trim();
                    objectId = ((CharacterData) (((Element) (objects.item(i))).getElementsByTagName("objectID")
                            .item(0).getFirstChild())).getData().trim();
                    if (similarityScore.compareTo("-1") != 0) {
                        overlap = ((CharacterData) (((Element) (objects.item(i)))
                                .getElementsByTagName("overlap").item(0).getFirstChild())).getData().trim();
                        reportTable.put(objectId, Integer.valueOf(overlap));
                    } else {
                        reportTable.put(objectId, Integer.valueOf(-1));
                    }

                    log.debug("objectId: " + objectId + " similarity: " + similarityScore + " overlap: "
                            + overlap);
                }
            } else {
                log.debug("Report list request not successful");
                log.debug(document.getTextContent());

            }
        }

        int reportVal;
        // check if the report value is now there (there may have been a
        // failure to get the list above)
        if (reportTable.containsKey(currentItem.getExternalId())) {
            reportVal = ((Integer) (reportTable.get(currentItem.getExternalId()))).intValue();
            log.debug("reportVal for " + currentItem.getExternalId() + ": " + reportVal);
            if (reportVal != -1) {
                currentItem.setReviewScore(reportVal);
                currentItem.setStatus(ContentReviewConstants.CONTENT_REVIEW_SUBMITTED_REPORT_AVAILABLE_CODE);
                currentItem.setDateReportReceived(new Date());
                crqs.update(currentItem);
                log.debug("new report received: " + currentItem.getExternalId() + " -> "
                        + currentItem.getReviewScore());
            }
        }
    }

    log.info("Finished fetching reports from Turnitin");
}