Skip to content

Commit

Permalink
CLDR-14386 Modernize List Users; fix JSONWriter conflict between PR 1…
Browse files Browse the repository at this point in the history
…000 and 1002 (#1007)

-Change remaining SurveyAjax.JSONWriter to SurveyJSONWrapper

-Change some org.json.JSONWriter to simply JSONWriter
  • Loading branch information
btangmu authored Feb 3, 2021
1 parent 86fde68 commit ef4a0fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
44 changes: 21 additions & 23 deletions tools/cldr-apps/src/main/java/org/unicode/cldr/web/SurveyAjax.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.unicode.cldr.icu.LDMLConstants;
import org.unicode.cldr.test.CheckCLDR;
import org.unicode.cldr.test.CheckCLDR.CheckStatus;
Expand Down Expand Up @@ -851,7 +852,7 @@ public CheckCLDR handleCheck(String path, String fullPath, String value, Options
break;
case WHAT_USER_LIST: {
if (!SurveyTool.useDojo(request)) {
final JSONWriter r = newJSONStatusQuick(sm);
final SurveyJSONWrapper r = newJSONStatusQuick(sm);
new UserList().getJson(r, request, response, mySession, sm);
send(r, out);
} else if (mySession.user.isAdminForOrg(mySession.user.org)) { // for now- only admin can do these
Expand Down Expand Up @@ -1398,11 +1399,11 @@ private void setupStatus(HttpServletRequest request, SurveyMain sm, SurveyJSONWr
}

/**
* Create a new JSONWriter and setup its status with the given SurveyMain.
* Create a new SurveyJSONWrapper and setup its status with the given SurveyMain.
* This is used to initialize JSON responses so that callers know
* whether the ST is actually running or not.
* @param sm the SurveyMain
* @return the JSONWriter
* @return the SurveyJSONWrapper
*/
private SurveyJSONWrapper newJSONStatus(HttpServletRequest request, SurveyMain sm) {
SurveyJSONWrapper r = newJSON();
Expand Down Expand Up @@ -1486,7 +1487,7 @@ private static void send(SurveyJSONWrapper r, PrintWriter out) throws IOExceptio
/**
* Import old votes.
*
* @param r the JSONWriter in which to write
* @param r the SurveyJSONWrapper in which to write
* @param user the User (see UserRegistry.java)
* @param sm the SurveyMain instance
* @param isSubmit false when just showing options to user, true when user clicks "Vote for selected Winning/Losing Votes"
Expand All @@ -1512,7 +1513,7 @@ private void importOldVotes(SurveyJSONWrapper r, User user, SurveyMain sm, boole
/**
* Import old votes, having confirmed user has permission.
*
* @param r the JSONWriter in which to write
* @param r the SurveyJSONWrapper in which to write
* @param user the User
* @param sm the SurveyMain instance
* @param isSubmit false when just showing options to user, true when user clicks "Vote for selected Winning/Losing Votes"
Expand Down Expand Up @@ -2043,7 +2044,7 @@ private static Map<String, Object>[] getOldVotesRows(final String newVotesTable,
* as though the user had chosen to select all their old winning votes in viewOldVotes, and
* submit them as in submitOldVotes.
*
* @param r the JSONWriter in which to write
* @param r the SurveyJSONWrapper in which to write
* @param user the User
* @param sm the SurveyMain instance
* @param oldVotesTable the String for the table name like "cldr_vote_value_33"
Expand Down Expand Up @@ -2249,7 +2250,7 @@ private boolean valueCanBeAutoImported(String value, String curValue, XMLSource
* @param request the HttpServletRequest, for parameters from_user_id, to_user_id, from_locale, to_locale
* @param sm the SurveyMain, for sm.reg and newJSONStatusQuick
* @param user the current User, who needs admin rights
* @param r the JSONWriter to be written to
* @param r the SurveyJSONWrapper to be written to
* @throws SurveyException
* @throws SQLException
* @throws JSONException
Expand Down Expand Up @@ -2282,7 +2283,7 @@ private void transferOldVotes(SurveyJSONWrapper r, HttpServletRequest request, S
*
* Loop thru multiple old votes tables in reverse chronological order.
*
* @param r the JSONWriter to be written to
* @param r the SurveyJSONWrapper to be written to
* @param from_user_id the id of the user from which to transfer
* @param to_user_id the id of the user to which to transfer
* @param from_locale the locale from which to transfer
Expand Down Expand Up @@ -2348,7 +2349,7 @@ private void transferOldVotesGivenUsersAndLocales(SurveyJSONWrapper r, Integer f
* @param request the HttpServletRequest, for parameter old_user_id
* @param sm the SurveyMain, for sm.reg and newJSONStatusQuick
* @param user the current User, who needs admin rights
* @param r the JSONWriter to be written to
* @param r the SurveyJSONWrapper to be written to
* @throws SQLException
* @throws JSONException
* @throws SurveyException
Expand All @@ -2374,7 +2375,7 @@ private void viewOldVoteStats(SurveyJSONWrapper r, HttpServletRequest request, S
*
* Check multiple old votes tables.
*
* @param r the JSONWriter to be written to
* @param r the SurveyJSONWrapper to be written to
* @throws SQLException
* @throws JSONException
* @throws IOException
Expand Down Expand Up @@ -2406,7 +2407,7 @@ private void viewOldVoteStatsForOldUserId(SurveyJSONWrapper r, Integer old_user_
/**
* Handle the user's submission of a vote, or an abstention if val is null.
*
* @param r the JSONWriter to be written to
* @param r the SurveyJSONWrapper to be written to
* @param val the string value voted for, or null for abstention
* @param mySession the CookieSession
* @param locale the CLDRLocale
Expand Down Expand Up @@ -2585,7 +2586,7 @@ public static void getRow(HttpServletRequest request, HttpServletResponse respon
curThread.setName(request.getServletPath() + ":" + loc + ":" + xpath);

if (mySession == null) {
new org.json.JSONWriter(out).object().key("err")
new JSONWriter(out).object().key("err")
.value("Your session has timed out or the SurveyTool has restarted.").endObject();
return;
}
Expand Down Expand Up @@ -2622,7 +2623,7 @@ public static void getRow(HttpServletRequest request, HttpServletResponse respon
try {
xp = sm.xpt.getByStringID(strid);
} catch (Throwable t) {
new org.json.JSONWriter(out).object().key("err").value("Exception getting stringid " + strid)
new JSONWriter(out).object().key("err").value("Exception getting stringid " + strid)
.key("err_code").value("E_BAD_SECTION").endObject();
return;
}
Expand All @@ -2643,7 +2644,7 @@ public static void getRow(HttpServletRequest request, HttpServletResponse respon
SupplementalDataInfo sdi = ctx.sm.getSupplementalDataInfo();
CLDRLocale dcParent = sdi.getBaseFromDefaultContent(locale);
if (dcParent != null) {
new org.json.JSONWriter(out).object().key("section")
new JSONWriter(out).object().key("section")
.value(new JSONObject().put("nocontent", "Default Content, see " + dcParent.getBaseName())).endObject();
return; // short circuit.
}
Expand Down Expand Up @@ -2677,14 +2678,14 @@ public static void getRow(HttpServletRequest request, HttpServletResponse respon
baseXp = XPathTable.xpathToBaseXpath(xp);
section = ctx.getDataSection(baseXp /* prefix */, matcher, null /* pageId */);
} else {
new org.json.JSONWriter(out).object().key("err")
new JSONWriter(out).object().key("err")
.value("Could not understand that section, xpath, or ID. Bad URL?")
.key("err_code").value("E_BAD_SECTION").endObject();
return;
}
} catch (Throwable t) {
SurveyLog.logException(t, "on loading " + locale + ":" + baseXp);
new org.json.JSONWriter(out).object().key("err").value("Exception on getSection:" + t.toString())
new JSONWriter(out).object().key("err").value("Exception on getSection:" + t.toString())
.key("err_code").value("E_BAD_SECTION").endObject();
return;
}
Expand All @@ -2704,18 +2705,15 @@ public static void getRow(HttpServletRequest request, HttpServletResponse respon

if (pageId != null) {
if (pageId.getSectionId() == org.unicode.cldr.util.PathHeader.SectionId.Special) {
new org.json.JSONWriter(out).object().key("err")
new JSONWriter(out).object().key("err")
.value("Items not visible - page " + pageId + " section " + pageId.getSectionId()).key("err_code").value("E_SPECIAL_SECTION")
.endObject();
return;
}
}

try {
/*
* TODO: why use org.json.JSONWriter here but SurveyAjax.JSONWriter elsewhere?
*/
org.json.JSONWriter r = new org.json.JSONWriter(out).object()
JSONWriter r = new JSONWriter(out).object()
.key("stro").value(STFactory.isReadOnlyLocale(locale))
.key("baseXpath").value(baseXp)
.key("pageId").value((pageId != null) ? pageId.name() : null)
Expand All @@ -2733,7 +2731,7 @@ public static void getRow(HttpServletRequest request, HttpServletResponse respon
r.endObject();
} catch (Throwable t) {
SurveyLog.logException(t, "RefreshRow write");
new org.json.JSONWriter(out).object().key("err").value("Exception on writeSection:" + t.toString()).endObject();
new JSONWriter(out).object().key("err").value("Exception on writeSection:" + t.toString()).endObject();
}
}
} finally {
Expand Down Expand Up @@ -3117,7 +3115,7 @@ private static void generateDashboard(Writer out, CookieSession cs, CLDRLocale l
*/
SurveyLog.logException(t, "when loading the Dashboard", ctx);
try {
new org.json.JSONWriter(out).object().key("err").value("Exception: " + t.getMessage()
new JSONWriter(out).object().key("err").value("Exception: " + t.getMessage()
+ " while loading the Dashboard").key("err_code").value("E_INTERNAL").endObject();
} catch (JSONException e) {
SurveyLog.logException(e, "when loading the Dashboard", ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.json.JSONString;
import org.unicode.cldr.util.Organization;
import org.unicode.cldr.util.VoteResolver;
import org.unicode.cldr.web.SurveyAjax.JSONWriter;
import org.unicode.cldr.web.UserRegistry.InfoType;
import org.unicode.cldr.web.UserRegistry.User;

Expand All @@ -44,7 +43,7 @@ public class UserList {

private static final String GET_ORGS = "get_orgs";

public void getJson(JSONWriter r, HttpServletRequest request,
public void getJson(SurveyJSONWrapper r, HttpServletRequest request,
HttpServletResponse response, CookieSession mySession, SurveyMain sm) throws JSONException, SurveyException, IOException {
if (!mySession.user.isAdminForOrg(mySession.user.org)) {
r.put("err", "You do not have permission to list users.");
Expand Down Expand Up @@ -107,7 +106,7 @@ private static JSONObject getUserPerms(User me) throws JSONException {
return userPerms;
}

private void doList(JSONWriter r, WebContext ctx, SurveyMain sm, String justOrg) throws JSONException {
private void doList(SurveyJSONWrapper r, WebContext ctx, SurveyMain sm, String justOrg) throws JSONException {
boolean justme = false; // "my account" mode
String just = ctx.field(LIST_JUST);
if (just != null) {
Expand Down

0 comments on commit ef4a0fa

Please sign in to comment.