Example usage for java.lang StringBuilder replace

List of usage examples for java.lang StringBuilder replace

Introduction

In this page you can find the example usage for java.lang StringBuilder replace.

Prototype

@Override
public StringBuilder replace(int start, int end, String str) 

Source Link

Usage

From source file:org.sakaiproject.assignment.tool.AssignmentAction.java

/**
 * Apply the fix to pre 1.1.05 assignments submissions feedback.
 *///from   www  .  j a  v a 2s  . com
private static String fixAssignmentFeedback(String value) {
    if (value == null || value.length() == 0)
        return value;

    StringBuilder buf = new StringBuilder(value);
    int pos = -1;

    // <br/> -> \n
    while ((pos = buf.indexOf("<br/>")) != -1) {
        buf.replace(pos, pos + "<br/>".length(), "\n");
    }

    // <span class='chefAlert'>( -> {{
    while ((pos = buf.indexOf("<span class='chefAlert'>(")) != -1) {
        buf.replace(pos, pos + "<span class='chefAlert'>(".length(), "{{");
    }

    // )</span> -> }}
    while ((pos = buf.indexOf(")</span>")) != -1) {
        buf.replace(pos, pos + ")</span>".length(), "}}");
    }

    while ((pos = buf.indexOf("<ins>")) != -1) {
        buf.replace(pos, pos + "<ins>".length(), "{{");
    }

    while ((pos = buf.indexOf("</ins>")) != -1) {
        buf.replace(pos, pos + "</ins>".length(), "}}");
    }

    return buf.toString();

}

From source file:org.sakaiproject.assignment.tool.AssignmentAction.java

/**
 * Processes the given assignmnent feedback text, as returned from the user's browser. Makes sure that the Chef-style markup {{like this}} is properly balanced.
 *//*from  ww w.j av  a2  s  .c  o m*/
private String processAssignmentFeedbackFromBrowser(SessionState state, String strFromBrowser) {
    if (strFromBrowser == null || strFromBrowser.length() == 0)
        return strFromBrowser;

    StringBuilder buf = new StringBuilder(strFromBrowser);
    int pos = -1;
    int numopentags = 0;

    while ((pos = buf.indexOf("{{")) != -1) {
        buf.replace(pos, pos + "{{".length(), "<ins>");
        numopentags++;
    }

    while ((pos = buf.indexOf("}}")) != -1) {
        buf.replace(pos, pos + "}}".length(), "</ins>");
        numopentags--;
    }

    while (numopentags > 0) {
        buf.append("</ins>");
        numopentags--;
    }

    boolean checkForFormattingErrors = true; // so that grading isn't held up by formatting errors
    buf = new StringBuilder(processFormattedTextFromBrowser(state, buf.toString(), checkForFormattingErrors));

    while ((pos = buf.indexOf("<ins>")) != -1) {
        buf.replace(pos, pos + "<ins>".length(), "{{");
    }

    while ((pos = buf.indexOf("</ins>")) != -1) {
        buf.replace(pos, pos + "</ins>".length(), "}}");
    }

    return buf.toString();
}

From source file:tds.dll.mysql.StudentDLL.java

public SingleDataResultSet T_GetEligibleTests_SP(SQLConnection connection, Long testee, UUID sessionKey,
        String grade) throws ReturnStatusException {

    SingleDataResultSet returnResultSet = new SingleDataResultSet();
    Date startTime = _dateUtil.getDateWRetStatus(connection);
    String clientName = null;//from   ww w  .  j  a va  2s. c  o  m
    Integer sessionType = null;
    List<EligibleTest> returnTestList = new ArrayList<EligibleTest>();

    //Getting sessiontype and clientName
    final String SQL_QUERY1 = "select sessionType, clientname from session where _Key = ${sessionKey}";
    SqlParametersMaps parms1 = (new SqlParametersMaps()).put("sessionKey", sessionKey);
    SingleDataResultSet result = executeStatement(connection, SQL_QUERY1, parms1, false).getResultSets().next();
    DbResultRecord record = (result.getCount() > 0 ? result.getRecords().next() : null);
    if (record != null) {
        sessionType = record.<Integer>get("sessionType");
        clientName = record.<String>get("clientname");
    }

    //Getting Tests from ART
    Set<String> rtsTests = new HashSet<String>();
    if (testee >= 0) {
        _Ref<String> refRTSTests = new _Ref<String>();
        _rtsDll._GetRTSAttribute_SP(connection, clientName, testee, "--ELIGIBLETESTS--", refRTSTests);

        if (refRTSTests.get() != null) {
            String rows[] = refRTSTests.get().split(";");
            for (String row : rows) {
                rtsTests.add(row.trim());
            }
        }
    }

    //Get EligibleTests for the currentSession
    result = _GetCurrentTests(connection, sessionKey);
    Iterator<DbResultRecord> recordsIter = result.getRecords();
    List<EligibleTest> eligibleTests = new ArrayList<EligibleTest>();
    Set<String> eligibleTestKeys = new HashSet<String>();
    StringBuilder sbTestIds = new StringBuilder();
    while (recordsIter.hasNext()) {
        record = recordsIter.next();
        String testKey = record.<String>get("testkey").trim();
        //If guest session and testid do not match with the one in the ART than do not show that test
        if (testee >= 0) {
            if (!rtsTests.contains(testKey) || eligibleTestKeys.contains(testKey)) {
                recordsIter.remove();
                continue;
            }
        }

        //Eliminate those that are not in a compatible grade.
        if (testee < 0 && __IsGradeEquiv_FN(record.<String>get("grade"), grade) == false) {
            recordsIter.remove();
            continue;
        }

        //Creating EligibleTest object
        EligibleTest test = new EligibleTest();
        test.setTestKey(testKey);
        test.setTestId(record.<String>get("testid"));
        test.setGrade(grade);
        test.setSubject(record.<String>get("subject"));
        test.setEnroll(record.<String>get("enroll"));
        test.setMaxOpps(record.<Integer>get("maxopps"));
        test.setRtsEquiv(record.<Long>get("rtsequiv"));
        test.setMode(record.<String>get("mode"));
        test.setSession(record.<UUID>get("session"));
        test.setWindowMax(record.<Integer>get("windowmax"));
        test.setStartDate(record.<Date>get("startdate"));
        test.setEndDate(record.<Date>get("enddate"));
        test.setWindowid(record.<String>get("windowid"));
        test.setModeMax(record.<Integer>get("modemax"));
        test.setWindowSession(record.<Integer>get("windowsession"));
        test.setModeSession(record.<Integer>get("modesession"));
        test.setSessionType(sessionType);
        test.setDisplayName(record.<String>get("displayName"));
        test.setSortOrder(record.<Integer>get("sortOrder"));
        test.setClientName(clientName);
        eligibleTests.add(test);
        eligibleTestKeys.add(testKey);
        //If sessionType is zero and session is null for the test than update the test with Denied status
        if (DbComparator.isEqual(test.getSessionType(), 0) && sessionKey != null && test.getSession() == null) {
            test.setOpportunity(0);
            test.setStatus("denied");

            _Ref<String> notAvailRef = new _Ref<>();
            _commonDll._FormatMessage_SP(connection, clientName, "ENU", "_CanOpenTestOpportunity",
                    "Test not available for this session.", notAvailRef);

            test.setReason(notAvailRef.get());
            returnTestList.add(test);
        } else {
            //Created a string for semicolon seperated testIds
            sbTestIds.append(test.getTestId()).append(";");
        }
    } //End while loop - Iterating through all the tests.
      //Remove the last semicolon from the testIds string
    if (sbTestIds.length() > 1) {
        sbTestIds.replace(sbTestIds.length() - 1, sbTestIds.length(), "");
    }

    //Getting All the Opportunity info for all the eligible tests
    SingleDataResultSet resultSet = _GetOpportunityInfo(connection, clientName, sessionKey, testee,
            sbTestIds.toString());
    recordsIter = resultSet.getRecords();
    //Store oppInfo inside Map for matching it with the opp
    Map<String, DbResultRecord> oppInfoMap = new HashMap<String, DbResultRecord>();
    while (recordsIter.hasNext()) {
        DbResultRecord record1 = recordsIter.next();
        oppInfoMap.put(record1.<String>get("_efk_testid"), record1);
    }

    // Get Enviornment Details
    String environment = _commonDll.getExternsColumnByClientName(connection, clientName, "environment");
    _Ref<String> reasonBlockedRef = new _Ref<String>();
    _Ref<String> testeeSubjectsRef = new _Ref<String>();
    //If It is not SIMULATION enviornment and not a Guest student than check if _IsOpportunityBlocked
    if (DbComparator.notEqual(environment, "SIMULATION") && DbComparator.greaterThan(testee, 0)) {
        _rtsDll._GetRTSAttribute_SP(connection, clientName, testee, "BLOCKEDSUBJECT", testeeSubjectsRef);
    }
    String subject = null;
    for (EligibleTest test : eligibleTests) {
        Date today = _dateUtil.getDateWRetStatus(connection);
        //Get Opportunity details for the testId
        DbResultRecord oppRecord = oppInfoMap.get(test.getTestId());
        if (DbComparator.notEqual(environment, "SIMULATION") && DbComparator.greaterThan(testee, 0)) {
            subject = test.getSubject();
            if (subject != null)
                subject = String.format(";%s;", subject.trim());
            else
                subject = ";;";

            //if the subject is explicitly blocked by the rts attribute BLOCKED, block the opportunity 
            String testeeSubjects = testeeSubjectsRef.get();
            if (testeeSubjects != null && testeeSubjects.length() > 0) {

                testeeSubjects = String.format(";%s;", testeeSubjects.trim());
                if (testeeSubjects.indexOf(subject) > -1) {

                    _commonDll._FormatMessage_SP(connection, clientName, "ENU", "_CanOpenTestOpportunity",
                            "This test is administratively blocked. Please check with your test administrator.",
                            reasonBlockedRef);
                    test.setReason(reasonBlockedRef.get());
                    continue;
                }
            }

            //Check if there is an existing opportunity  _CanOpenExistingOpportunity
            _CanOpenExistingOpportunity_New(connection, oppRecord, test, clientName, reasonBlockedRef,
                    sessionKey, today, testee);
            // If OpenexistingOpportunity is executed and teststatus is paused than continue with the next test
            if (DbComparator.greaterThan(test.getNumRef(), 0) || test.getReason() != null) {
                continue;
            }
        } //If not Guest or Simulation - IsOpportunity
          //_Can Open New Opportunity 
        _CanOpenNewOpportunity_New(connection, oppRecord, test, clientName, reasonBlockedRef, today,
                environment);
        if (DbComparator.greaterThan(test.getNumRef(), 0)) {
            test.setNewoppRef(true);
        }
    } //End for loop for existingTests

    if (DbComparator.isEqual(sessionType, 1)) {

        Iterator<EligibleTest> iTests = eligibleTests.iterator();
        while (iTests.hasNext()) {
            EligibleTest test = iTests.next();
            if (test.getStatus() != null && test.getStatus().equals("NA")) {
                iTests.remove();
            }
            final String SQL_QUERY_INSERT1 = "insert into sessiontests (_fk_Session, _efk_AdminSubject, _efk_TestID) "
                    + " values(${sessionKey},${testKey},${testId})";
            SqlParametersMaps parms2 = (new SqlParametersMaps());
            parms2.put("sessionKey", sessionKey);
            parms2.put("testKey", test.getTestKey());
            parms2.put("testId", test.getTestId());

            executeStatement(connection, SQL_QUERY_INSERT1, parms2, false).getUpdateCount();
        }
    }
    Collections.sort(eligibleTests);
    Iterator<EligibleTest> iTests = eligibleTests.iterator();
    List<CaseInsensitiveMap<Object>> resultList = new ArrayList<CaseInsensitiveMap<Object>>();
    while (iTests.hasNext()) {
        EligibleTest test = iTests.next();
        CaseInsensitiveMap<Object> rcd = new CaseInsensitiveMap<Object>();
        rcd.put("displayName", test.getDisplayName());
        rcd.put("testkey", test.getTestKey());
        rcd.put("test", test.getTestId());
        rcd.put("opportunity", test.getNumRef());
        rcd.put("mode", test.getMode());
        rcd.put("maxopps", test.getMaxOpps());
        rcd.put("subject", test.getSubject());
        rcd.put("grade", grade);

        String status = null;
        if (DbComparator.isEqual(test.getNumRef(), 0))
            status = "denied";
        else if (DbComparator.isEqual(test.getNumRef(), -1))
            status = "N/A";
        else if (DbComparator.isEqual(test.getNewoppRef(), false))
            status = "suspended";
        else
            status = "pending";

        rcd.put("status", status);
        rcd.put("reason", test.getReason());
        resultList.add(rcd);
    }
    returnResultSet.addColumn("displayName", SQL_TYPE_To_JAVA_TYPE.VARCHAR);
    returnResultSet.addColumn("testkey", SQL_TYPE_To_JAVA_TYPE.VARCHAR);
    returnResultSet.addColumn("test", SQL_TYPE_To_JAVA_TYPE.VARCHAR);
    returnResultSet.addColumn("opportunity", SQL_TYPE_To_JAVA_TYPE.INT);
    returnResultSet.addColumn("mode", SQL_TYPE_To_JAVA_TYPE.VARCHAR);
    returnResultSet.addColumn("maxopps", SQL_TYPE_To_JAVA_TYPE.INT);
    returnResultSet.addColumn("sortOrder", SQL_TYPE_To_JAVA_TYPE.INT);
    returnResultSet.addColumn("subject", SQL_TYPE_To_JAVA_TYPE.VARCHAR);
    returnResultSet.addColumn("grade", SQL_TYPE_To_JAVA_TYPE.VARCHAR);
    returnResultSet.addColumn("status", SQL_TYPE_To_JAVA_TYPE.VARCHAR);
    returnResultSet.addColumn("reason", SQL_TYPE_To_JAVA_TYPE.VARCHAR);

    returnResultSet.addRecords(resultList);
    _commonDll._LogDBLatency_SP(connection, "T_GetEligibleTests", startTime, testee, true, null, null, null,
            clientName, null);

    return returnResultSet;
}