List of usage examples for org.json JSONObject has
public boolean has(String key)
From source file:org.collectionspace.chain.csp.webui.record.RecordSearchList.java
private boolean morePermissions(JSONObject permissionResults, String key) throws JSONException { boolean result = false; if (permissionResults.has("pagination")) { JSONObject pagination = permissionResults.getJSONObject("pagination"); JSONArray separatelists = pagination.getJSONArray("separatelists"); String[] permissionsList = (String[]) separatelists.get(0); ///* w w w . j a v a2s .c o m*/ // If the permissionsList is not empty then there still might // be another page of permissions, so we'll return true // if (permissionsList != null && permissionsList.length > 0) { result = true; } } // // Just to be safe, we'll also return true if we found items in the result list. // if (permissionResults.has(key) && permissionResults.getJSONArray(key).length() > 0) { result = true; } return result; }
From source file:org.collectionspace.chain.csp.webui.record.RecordSearchList.java
/** * This function is the general function that calls the correct funtions to * get all the data that the UI requested and get it in the correct format * for the UI./* w w w . ja v a2s .c o m*/ * * @param {Storage} storage The type of storage requested (e.g. * RecordStorage, AuthorizationStorage,...) * @param {UIRequest} ui The request from the ui to which we send a * response. * @param {String} param If a querystring has been added to the * URL(e.g.'?query='), it will be in this param * @param {String} pageSize The amount of results per page requested. * @param {String} pageNum The amount of pages requested. * @throws UIException * @throws UnderlyingStorageException * @throws UnimplementedException * @throws ExistException * @throws JSONException */ protected JSONObject getResults(UIRequest request, Storage storage, JSONObject restriction, String key, String path) throws UIException, JSONException, ExistException, UnimplementedException, UnderlyingStorageException { JSONObject results = new JSONObject(); if (this.r.getID().equals("permission")) { // // Pagination information is not implemented in permissions. See CSPACE-4785 // Until CSPACE-4785 gets resolved, we'll tread "permission" requests // as a special case. // restriction.put("pageSize", MAX_PERMISSIONS_PAGESIZE); results = getJSON(storage, restriction, key, base); log.warn( String.format("Due to issue CSPACE-4785, we can retrieve only the first %d permission records.", MAX_PERMISSIONS_PAGESIZE)); log.debug(results.toString()); } else if (r.getID().equals("reports")) { String type = ""; if (path != null && !path.equals("")) { restriction.put("queryTerm", "doctype"); restriction.put("queryString", spec.getRecordByWebUrl(path).getServicesTenantDoctype(false)); } if (restriction.has("queryTerm") && restriction.getString("queryTerm").equals("doctype")) { type = restriction.getString("queryString"); results = getJSON(storage, restriction, key, base); results = showReports(results, type, key); if (request != null) { int cacheMaxAgeSeconds = spec.getAdminData().getReportListCacheAge(); if (cacheMaxAgeSeconds > 0) { request.setCacheMaxAgeSeconds(cacheMaxAgeSeconds); } } } else { JSONObject reporting = new JSONObject(); for (Record r2 : spec.getAllRecords()) { if (r2.isInRecordList()) { type = r2.getServicesTenantSg(); restriction.put("queryTerm", "doctype"); restriction.put("queryString", type); JSONObject rdata = getJSON(storage, restriction, key, base); JSONObject procedurereports = showReports(rdata, type, key); reporting.put(r2.getWebURL(), procedurereports); } } results.put("reporting", reporting); } } else if (r.getID().equals("batch")) { String type = ""; if (path != null && !path.equals("")) { restriction.put("queryTerm", "doctype"); restriction.put("queryString", spec.getRecordByWebUrl(path).getServicesTenantDoctype(false)); } if (restriction.has("queryTerm") && restriction.getString("queryTerm").equals("doctype")) { type = restriction.getString("queryString"); results = getJSON(storage, restriction, key, base); results = showBatches(results, type, key); // TODO: Add caching stuff, as with reports } else { JSONObject batch = new JSONObject(); for (Record r2 : spec.getAllRecords()) { if (r2.isInRecordList()) { type = r2.getServicesTenantSg(); restriction.put("queryTerm", "doctype"); restriction.put("queryString", type); JSONObject rdata = getJSON(storage, restriction, key, base); JSONObject procedurebatches = showBatches(rdata, type, key); batch.put(r2.getWebURL(), procedurebatches); } } results.put("batch", batch); } } else { if ((mode == MODE_SEARCH_RELATED) && !path.isEmpty()) { // This is a related to case restriction.put(GenericSearch.SEARCH_RELATED_TO_CSID_AS_SUBJECT, path); } if ((searchAllGroup != null) && r.isType("searchall")) { // Add a new service group name to restriction.put(GenericSearch.SEARCH_ALL_GROUP, searchAllGroup); } results = getJSON(storage, restriction, key, base); } return results; }
From source file:org.collectionspace.chain.csp.webui.record.RecordSearchList.java
private JSONObject showReports(JSONObject data, String type, String key) throws JSONException { JSONObject results = new JSONObject(); JSONArray list = new JSONArray(); JSONArray names = new JSONArray(); if (data.has(key)) { JSONArray ja = data.getJSONArray(key); for (int j = 0; j < ja.length(); j++) { list.put(ja.getJSONObject(j).getString("csid")); names.put(ja.getJSONObject(j).getString("number")); }/* ww w . j av a2 s . c o m*/ results.put("reportlist", list); results.put("reportnames", names); } return results; }
From source file:org.collectionspace.chain.csp.webui.record.RecordSearchList.java
private JSONObject showBatches(JSONObject data, String type, String key) throws JSONException { JSONObject results = new JSONObject(); JSONArray list = new JSONArray(); JSONArray names = new JSONArray(); JSONArray newFocuses = new JSONArray(); if (data.has(key)) { JSONArray ja = data.getJSONArray(key); for (int j = 0; j < ja.length(); j++) { list.put(ja.getJSONObject(j).getString("csid")); names.put(ja.getJSONObject(j).getString("number")); JSONObject summarylist = ja.getJSONObject(j).getJSONObject("summarylist"); newFocuses.put(summarylist.getBoolean("createsNewFocus")); }// ww w. j a v a 2 s.co m results.put("batchlist", list); results.put("batchnames", names); results.put("batchnewfocuses", newFocuses); } return results; }
From source file:org.collectionspace.chain.csp.webui.record.RecordSearchList.java
private void advancedSearch(Storage storage, UIRequest ui, String path, JSONObject params) throws UIException { try {//w w w . ja v a 2 s. co m JSONObject results = new JSONObject(); JSONObject restrictedkey = GenericSearch.setRestricted(ui, null, null, null, true, this.r); JSONObject restriction = restrictedkey.getJSONObject("restriction"); String key = restrictedkey.getString("key"); GenericSearch.buildQuery(this.r, params, restriction); key = "results"; results = getJSON(storage, restriction, key, base); //cache for record traverser if (results.has("pagination") && results.getJSONObject("pagination").has("separatelists")) { GenericSearch.createTraverser(ui, this.r.getID(), "", results, restriction, key, 1); } ui.sendJSONResponse(results); } catch (JSONException e) { throw new UIException("JSONException during advancedSearch " + e.getMessage(), e); } catch (ExistException e) { throw new UIException("ExistException during search_or_list", e); } catch (UnimplementedException e) { throw new UIException("UnimplementedException during search_or_list", e); } catch (UnderlyingStorageException x) { UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x); ui.sendJSONResponse(uiexception.getJSON()); } }
From source file:org.collectionspace.chain.csp.webui.record.RecordSearchList.java
public JSONObject getJSON(Storage storage, JSONObject restriction, String key, String mybase) throws JSONException, UIException, ExistException, UnimplementedException, UnderlyingStorageException { JSONObject out = new JSONObject(); JSONObject data = storage.getPathsJSON(mybase, restriction); String[] paths = (String[]) data.get("listItems"); JSONObject pagination = new JSONObject(); if (data.has("pagination")) { pagination = data.getJSONObject("pagination"); }/*from w w w. j a v a 2s. c o m*/ for (int i = 0; i < paths.length; i++) { if (paths[i].startsWith(mybase + "/")) { paths[i] = paths[i].substring((mybase + "/").length()); } } out = pathsToJSON(storage, mybase, paths, key, pagination); return out; }
From source file:com.leanengine.JsonEncode.java
static JSONObject queryToJson(LeanQuery query) throws LeanException { JSONObject json = new JSONObject(); try {/*from w w w . jav a 2 s. c o m*/ json.put("kind", query.getKind()); JSONObject jsonFilters = new JSONObject(); for (QueryFilter filter : query.getFilters()) { JSONObject jsonFilter; if (jsonFilters.has(filter.getProperty())) { jsonFilter = jsonFilters.getJSONObject(filter.getProperty()); } else { jsonFilter = new JSONObject(); } addTypedNode(jsonFilter, filter.getOperator().toJSON(), filter.getValue()); jsonFilters.put(filter.getProperty(), jsonFilter); } json.put("filter", jsonFilters); JSONObject jsonSorts = new JSONObject(); for (QuerySort sort : query.getSorts()) { jsonSorts.put(sort.getProperty(), sort.getDirection().toJSON()); } json.put("sort", jsonSorts); json.put("limit", query.getLimit()); if (query.getOffset() != 0) json.put("offset", query.getOffset()); if (query.getCursor() != null) json.put("cursor", query.getCursor()); return json; } catch (JSONException e) { // this should not happen under normal circumstances throw new IllegalStateException(e); } }
From source file:pe.chalk.telegram.type.file.photo.Sticker.java
private Sticker(final JSONObject json) { super(json);//from www. j a v a 2s . c o m this.thumb = json.has("thumb") ? PhotoSize.create(json.getJSONObject("thumb")) : null; }
From source file:org.seadpdt.impl.RepoServicesImpl.java
@POST @Path("/") @Consumes(MediaType.APPLICATION_JSON)/* ww w.jav a 2s. c om*/ @Produces(MediaType.APPLICATION_JSON) public Response registerRepository(String profileString) { JSONObject profile = new JSONObject(profileString); if (!profile.has("orgidentifier")) { return Response.status(Status.BAD_REQUEST) .entity(new BasicDBObject("Failure", "Invalid request format: " + "Request must contain the field \"orgidentifier\"")) .build(); } String newID = (String) profile.get("orgidentifier"); FindIterable<Document> iter = repositoriesCollection.find(new Document("orgidentifier", newID)); if (iter.iterator().hasNext()) { return Response.status(Status.CONFLICT) .entity(new BasicDBObject("Failure", "Repository with Identifier " + newID + " already exists")) .build(); } else { repositoriesCollection.insertOne(Document.parse(profile.toString())); URI resource = null; try { resource = new URI("./" + newID); } catch (URISyntaxException e) { // Should not happen given simple ids e.printStackTrace(); } return Response.created(resource).entity(new Document("orgidentifier", newID)).build(); } }
From source file:com.cmackay.plugins.googleanalytics.GoogleAnalyticsPlugin.java
private static Map<String, String> objectToMap(JSONObject o) throws JSONException { if (o.length() == 0) { return Collections.<String, String>emptyMap(); }/*from ww w . ja va 2s. com*/ Map<String, String> map = new HashMap<String, String>(o.length()); Iterator it = o.keys(); String key, value; while (it.hasNext()) { key = it.next().toString(); value = o.has(key) ? o.get(key).toString() : null; map.put(key, value); } return map; }