UIGadgetEditor uiForm = event.getSource();
UIGadgetManagement uiManagement = uiForm.getParent();
String gadgetName;
String text = uiForm.getUIFormTextAreaInput(UIGadgetEditor.FIELD_SOURCE).getValue();
GadgetRegistryService service = uiForm.getApplicationComponent(GadgetRegistryService.class);
SourceStorage sourceStorage = uiForm.getApplicationComponent(SourceStorage.class);
boolean isEdit = uiForm.isEdit();
if (isEdit) {
gadgetName = uiForm.getSourceFullName();
} 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();