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
@@ -0,0 +1,88 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2025 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.ide.plugin.renderer;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.StreamSupport;

import io.meeds.ide.model.Widget;
import io.meeds.ide.service.WidgetService;
import io.meeds.layout.model.PortletInstanceContext;
import io.meeds.social.util.JsonUtils;
import org.springframework.stereotype.Service;

import org.exoplatform.portal.config.model.Application;
import org.exoplatform.portal.pom.spi.portlet.Portlet;

import io.meeds.layout.model.PortletInstancePreference;
import io.meeds.layout.plugin.PortletInstancePreferencePlugin;

import lombok.SneakyThrows;

@Service
public class WidgetPortletInstancePreferencePlugin implements PortletInstancePreferencePlugin {

private static final String WIDGET_ID_PARAM = "widgetId";

private static final String DATA_INIT_PREFERENCE_NAME = "data.init";

private final WidgetService widgetService;

public WidgetPortletInstancePreferencePlugin(WidgetService widgetService) {
this.widgetService = widgetService;
}

@Override
public String getPortletName() {
return "WidgetPortlet";
}

@Override
@SneakyThrows
public List<PortletInstancePreference> generatePreferences(Application application,
Portlet preferences,
PortletInstanceContext portletInstanceContext) {
if (portletInstanceContext.isExport()) {
if (preferences != null && preferences.getPreference(DATA_INIT_PREFERENCE_NAME) != null) {
return Collections.singletonList(new PortletInstancePreference(DATA_INIT_PREFERENCE_NAME,
preferences.getPreference(DATA_INIT_PREFERENCE_NAME)
.getValue()));
} else {
long widgetId = 0L;
if (preferences != null) {
widgetId = Long.parseLong(preferences.getPreference(WIDGET_ID_PARAM).getValue());
}
Widget widget = widgetService.getWidget(widgetId);
Map<String, String> content = new HashMap<>();
content.put("html", widget.getHtml());
content.put("js", widget.getJs());
content.put("css", widget.getCss());
return List.of(new PortletInstancePreference(DATA_INIT_PREFERENCE_NAME, JsonUtils.toJsonString(content)),
new PortletInstancePreference(WIDGET_ID_PARAM, String.valueOf(widget.getId())));
}
} else {
return StreamSupport.stream(preferences.spliterator(), false)
.map(p -> new PortletInstancePreference(p.getName(), p.getValue()))
.toList();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2025 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.ide.plugin.renderer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.meeds.ide.model.Widget;
import io.meeds.ide.service.WidgetService;
import io.meeds.layout.model.PortletInstanceContext;
import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import org.exoplatform.portal.pom.spi.portlet.Portlet;
import org.exoplatform.portal.pom.spi.portlet.Preference;

import io.meeds.layout.model.PortletInstancePreference;
import org.springframework.boot.test.mock.mockito.MockBean;

@SpringBootTest(classes = { WidgetPortletInstancePreferencePlugin.class, })
@ExtendWith(MockitoExtension.class)
class WidgetPortletInstancePreferencePluginTest {

private static final String DATA_PREF_NAME = "data.init";

private static final String WIDGET_ID_PREF_NAME = "widgetId";

private static final String SETTING_NAME = "name";

@MockBean
private WidgetService widgetService;

@Autowired
private WidgetPortletInstancePreferencePlugin widgetPortletInstancePreferencePlugin;

@Test
void getPortletName() {
assertEquals("WidgetPortlet", widgetPortletInstancePreferencePlugin.getPortletName());
}

@Test
void generatePreferences() throws ObjectNotFoundException {
Map<String, Preference> map = new HashMap<>();
map.put(DATA_PREF_NAME, new Preference(SETTING_NAME, "value", false));
map.remove(DATA_PREF_NAME);
map.put(WIDGET_ID_PREF_NAME, new Preference(SETTING_NAME, "1", false));
Widget widget = new Widget();
widget.setJs("Js");
widget.setHtml("Html");
widget.setCss("Css");
when(widgetService.getWidget(1L)).thenReturn(widget);
Portlet preferences = new Portlet(map);
List<PortletInstancePreference> generatedPreferences =
widgetPortletInstancePreferencePlugin.generatePreferences(null,
preferences,
new PortletInstanceContext(false,
new HashMap<>()));
assertNotNull(generatedPreferences);
assertEquals(1, generatedPreferences.size());
assertEquals("name", generatedPreferences.getFirst().getName());

// Export
generatedPreferences = widgetPortletInstancePreferencePlugin.generatePreferences(null,
preferences,
new PortletInstanceContext(true,
new HashMap<>()));
assertNotNull(generatedPreferences);
assertEquals(2, generatedPreferences.size());
assertEquals(DATA_PREF_NAME, generatedPreferences.getFirst().getName());
}
}
13 changes: 12 additions & 1 deletion ide-webapp/src/main/java/io/meeds/ide/portlet/WidgetPortlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import io.meeds.ide.model.Widget;
import io.meeds.ide.service.WidgetService;

import static io.meeds.layout.util.JsonUtils.fromJsonString;
import static io.meeds.social.portlet.CMSPortlet.DATA_INIT_PREFERENCE_NAME;

public class WidgetPortlet extends GenericDispatchedViewPortlet {

private static final String WIDGET_ID_PARAM = "widgetId";
Expand Down Expand Up @@ -82,14 +85,21 @@ 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) {
if ((preferences.getValue(PORTLET_INSTANCE_ID_PARAM, null) != null && preferences.getValue(WIDGET_ID_PARAM, null) == null)
|| preferences.getValue(DATA_INIT_PREFERENCE_NAME, 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 @@ -99,6 +109,7 @@ 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