protected JSONObject getGadgetJson(Gadget gadget, GadgetSpec spec)
throws JSONException {
JSONObject gadgetJson = new JSONObject();
ModulePrefs prefs = spec.getModulePrefs();
// TODO: modularize response fields based on requested items.
JSONObject views = new JSONObject();
for (View view : spec.getViews().values()) {
JSONObject jv = new JSONObject()
// .put("content", view.getContent())
.put("type", view.getType().toString())
.put("quirks", view.getQuirks())
.put("preferredHeight", view.getPreferredHeight())
.put("preferredWidth", view.getPreferredWidth());
Map<String, String> vattrs = view.getAttributes();
if (!vattrs.isEmpty()){
JSONObject ja = new JSONObject(vattrs);
jv.put("attributes", ja);
}
views.put(view.getName(), jv);
}
// Features.
Set<String> feats = prefs.getFeatures().keySet();
String[] features = feats.toArray(new String[feats.size()]);
// Feature details
// The following renders an object containing feature details, of the form
// { <featureName>*: { "required": <boolean>, "parameters": { <paramName>*: <string> } } }
JSONObject featureDetailList = new JSONObject();
for (Feature featureSpec : prefs.getFeatures().values()) {
JSONObject featureDetail = new JSONObject();
featureDetail.put("required", featureSpec.getRequired());
JSONObject featureParameters = new JSONObject();
featureDetail.put("parameters", featureParameters);
Multimap<String, String> featureParams = featureSpec.getParams();
for (String paramName : featureParams.keySet()) {
featureParameters.put(paramName, featureParams.get(paramName));
}
featureDetailList.put(featureSpec.getName(), featureDetail);
}
// Links
JSONObject links = new JSONObject();
for (LinkSpec link : prefs.getLinks().values()) {
links.put(link.getRel(), link.getHref());
}
JSONObject userPrefs = new JSONObject();
// User pref specs
for (UserPref pref : spec.getUserPrefs().values()) {
JSONObject up = new JSONObject()
.put("displayName", pref.getDisplayName())
.put("type", pref.getDataType().toString().toLowerCase())
.put("default", pref.getDefaultValue())
.put("enumValues", pref.getEnumValues())
.put("orderedEnumValues", getOrderedEnums(pref));
userPrefs.put(pref.getName(), up);
}
// TODO: This should probably just copy all data from
// ModulePrefs.getAttributes(), but names have to be converted to
// camel case.
gadgetJson.put("iframeUrl", iframeUriManager.makeRenderingUri(gadget).toString())
.put("url",context.getUrl().toString())
.put("moduleId", context.getModuleId())
.put("title", prefs.getTitle())
.put("titleUrl", prefs.getTitleUrl().toString())
.put("views", views)
.put("features", features)
.put("featureDetails", featureDetailList)
.put("userPrefs", userPrefs)
.put("links", links)
// extended meta data
.put("directoryTitle", prefs.getDirectoryTitle())
.put("thumbnail", prefs.getThumbnail().toString())
.put("screenshot", prefs.getScreenshot().toString())
.put("author", prefs.getAuthor())
.put("authorEmail", prefs.getAuthorEmail())
.put("authorAffiliation", prefs.getAuthorAffiliation())
.put("authorLocation", prefs.getAuthorLocation())
.put("authorPhoto", prefs.getAuthorPhoto())
.put("authorAboutme", prefs.getAuthorAboutme())
.put("authorQuote", prefs.getAuthorQuote())
.put("authorLink", prefs.getAuthorLink())
.put("categories", prefs.getCategories())
.put("screenshot", prefs.getScreenshot().toString())
.put("height", prefs.getHeight())
.put("width", prefs.getWidth())
.put("showStats", prefs.getShowStats())
.put("showInDirectory", prefs.getShowInDirectory())
.put("singleton", prefs.getSingleton())
.put("scaling", prefs.getScaling())
.put("scrolling", prefs.getScrolling());
return gadgetJson;
}