Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public String getPortletName() {
public List<PortletInstancePreference> generatePreferences(Application application,
Portlet preferences,
PortletInstanceContext portletInstanceContext) {
if (portletInstanceContext.isExport()) {
if (preferences != null && preferences.getPreference(DATA_INIT_PREFERENCE_NAME) != null) {
if (portletInstanceContext != null && portletInstanceContext.isExport()) {
if (preferences != null && preferences.getPreference(DATA_INIT_PREFERENCE_NAME) != null
&& preferences.getPreference(DATA_INIT_PREFERENCE_NAME).getValue() != null) {
return Collections.singletonList(new PortletInstancePreference(DATA_INIT_PREFERENCE_NAME,
preferences.getPreference(DATA_INIT_PREFERENCE_NAME)
.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ public Widget createWidget(Widget widget, String username) throws ObjectAlreadyE
if (!layoutAclService.isAdministrator(username)) {
throw new IllegalAccessException(NOT_ADMINISTRATOR_USER);
}
if (widget.getPortletId() == null || widget.getPortletId() == 0) {
throw new IllegalArgumentException("Widget portlet instance id is mandatory");
}
if (widgetStorage.existsByPortletInstanceId(widget.getPortletId())) {
if (widget.getPortletId() != null && widgetStorage.existsByPortletInstanceId(widget.getPortletId())) {
throw new ObjectAlreadyExistsException(String.format("Widget for portlet with id %s already exists",
widget.getPortletId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,15 @@ void generatePreferences() throws ObjectNotFoundException {
widgetPortletInstancePreferencePlugin.generatePreferences(null,
preferences,
new PortletInstanceContext(false,
new HashMap<>()));
null));
assertNotNull(generatedPreferences);
assertEquals(1, generatedPreferences.size());
assertEquals("name", generatedPreferences.getFirst().getName());

// Export
generatedPreferences = widgetPortletInstancePreferencePlugin.generatePreferences(null,
preferences,
new PortletInstanceContext(true,
new HashMap<>()));
new PortletInstanceContext(true, null));
assertNotNull(generatedPreferences);
assertEquals(2, generatedPreferences.size());
assertEquals(DATA_PREF_NAME, generatedPreferences.getFirst().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ public void getWidget() throws ObjectNotFoundException {
public void createWidget() throws ObjectAlreadyExistsException, IllegalAccessException {
assertThrows(IllegalAccessException.class, () -> widgetService.createWidget(widget, USERNAME));
when(layoutAclService.isAdministrator(USERNAME)).thenReturn(true);

assertThrows(IllegalArgumentException.class, () -> widgetService.createWidget(widget, USERNAME));
when(widget.getPortletId()).thenReturn(3l);

when(widgetStorage.existsByPortletInstanceId(3l)).thenReturn(true);
Expand Down
38 changes: 28 additions & 10 deletions ide-webapp/src/main/java/io/meeds/ide/portlet/WidgetPortlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import io.meeds.social.cms.service.CMSService;
import org.exoplatform.commons.ObjectAlreadyExistsException;
import org.exoplatform.commons.api.portlet.GenericDispatchedViewPortlet;
import org.exoplatform.container.ExoContainerContext;
Expand Down Expand Up @@ -85,21 +84,41 @@ protected void doEdit(RenderRequest request, RenderResponse response) throws Por

private void checkPreferences(RenderRequest request) throws PortletException {
PortletPreferences preferences = request.getPreferences();
if ((preferences.getValue(PORTLET_INSTANCE_ID_PARAM, null) != null && preferences.getValue(WIDGET_ID_PARAM, null) == null)
|| preferences.getValue(DATA_INIT_PREFERENCE_NAME, null) != null) {
if (preferences.getValue(DATA_INIT_PREFERENCE_NAME, null) != null) {
try {
Widget widget = new Widget();
Widget imported = fromJsonString(preferences.getValue(DATA_INIT_PREFERENCE_NAME, null), Widget.class);
if (imported != null) {
widget.setJs(imported.getJs());
widget.setHtml(imported.getHtml());
widget.setCss(imported.getCss());
}
WidgetService widgetService = ExoContainerContext.getService(WidgetService.class);
Identity identity = Utils.getViewerIdentity();
widget = widgetService.createWidget(widget, identity.getRemoteId());
String storageId = UIPortlet.getCurrentUIPortlet().getStorageId();
Application applicationModel = getLayoutService().getApplicationModel(storageId);
ApplicationState state = applicationModel.getState();
Portlet prefs = getLayoutService().load(state);
prefs.setValue(WIDGET_ID_PARAM, String.valueOf(widget.getId()));
prefs.setValue(DATA_INIT_PREFERENCE_NAME, null);
layoutService.save(state, prefs);
preferences.setValue(WIDGET_ID_PARAM, String.valueOf(widget.getId()));
preferences.setValue(DATA_INIT_PREFERENCE_NAME, null);
} catch (ObjectAlreadyExistsException e) {
throw new PortletException("Widget already exists", e);
} catch (IllegalAccessException e) {
throw new PortletException("User not allowed to change Widget settings", e);
}
}
if (preferences.getValue(PORTLET_INSTANCE_ID_PARAM, null) != null && preferences.getValue(WIDGET_ID_PARAM, null) == null) {
long portletInstanceId = Long.parseLong(preferences.getValue(PORTLET_INSTANCE_ID_PARAM, null));
Identity identity = Utils.getViewerIdentity();
try {
WidgetService widgetService = ExoContainerContext.getService(WidgetService.class);
Widget widget = widgetService.getWidgetByPortletId(portletInstanceId);
if (widget == null) {
widget = new Widget();
Widget imported = fromJsonString(preferences.getValue(DATA_INIT_PREFERENCE_NAME, null), Widget.class);
if (imported != null) {
widget.setJs(imported.getJs());
widget.setHtml(imported.getHtml());
widget.setCss(imported.getCss());
}
widget.setPortletId(portletInstanceId);
widget = widgetService.createWidget(widget, identity.getRemoteId());
}
Expand All @@ -109,7 +128,6 @@ private void checkPreferences(RenderRequest request) throws PortletException {
ApplicationState state = applicationModel.getState();
Portlet prefs = getLayoutService().load(state);
prefs.setValue(WIDGET_ID_PARAM, String.valueOf(widget.getId()));
prefs.setValue(DATA_INIT_PREFERENCE_NAME, null);
layoutService.save(state, prefs);
} catch (IllegalAccessException e) {
throw new PortletException("User not allowed to change Widget settings", e);
Expand Down