} else {
gadgetName = uiForm.getUIStringInput(UIGadgetEditor.FIELD_NAME).getValue();
}
//
Gadget gadget = service.getGadget(gadgetName);
if (isEdit) {
if (gadget == null) {
UIApplication uiApp = event.getRequestContext().getUIApplication();
uiApp.addMessage(new ApplicationMessage("gadget.msg.changeNotExist", null, ApplicationMessage.WARNING));
uiManagement.reload();
return;
}
} else {
// If gadget is null we need to create it first
if (gadget == null) {
gadget = new Gadget();
gadget.setName(gadgetName);
// Those data will be taken from the gadget XML anyway
gadget.setDescription("");
gadget.setThumbnail("");
gadget.setLocal(true);
gadget.setTitle("");
gadget.setReferenceUrl("");
// Save gadget with empty data first
service.saveGadget(gadget);
} else {
UIApplication uiApp = event.getRequestContext().getUIApplication();
uiApp.addMessage(new ApplicationMessage("UIGadgetEditor.gadget.msg.gadgetIsExist", null,
ApplicationMessage.WARNING));
return;
}
}
//
Source source = new Source(gadgetName, "application/xml");
source.setTextContent(text);
source.setLastModified(Calendar.getInstance());
try {
sourceStorage.saveSource(gadget, source);
uiManagement.removeChild(UIGadgetEditor.class);
// This will update the source and also update the gadget related
// cached meta data
// from the source
uiManagement.initData();
uiManagement.setSelectedGadget(gadget.getName());
event.getRequestContext().addUIComponentToUpdateByAjax(uiManagement);
// Send request to invalidate the cache to Shindig
String gadgetServerUrl = GadgetUtil.getGadgetServerUrl();
String gadgetUrl = GadgetUtil.reproduceUrl(gadget.getUrl(), gadget.isLocal());
String metadataUrl = gadgetServerUrl + (gadgetServerUrl.endsWith("/") ? "" : "/") + "metadata";
String queryString = "{\"context\":{\"ignoreCache\":\"true\"},\"gadgets\":[" + "{\"url\":\"" + gadgetUrl
+ "\"}]}";
event.getRequestContext().getJavascriptManager().require("SHARED/base")
.addScripts("ajaxRequest('POST', '" + metadataUrl + "', true, '" + queryString + "');");