|
| 1 | +/** |
| 2 | + * This file is part of the Meeds project (https://meeds.io/). |
| 3 | + * |
| 4 | + * Copyright (C) 2020 - 2025 Meeds Association contact@meeds.io |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package io.meeds.layout.plugin.renderer; |
| 21 | + |
| 22 | +import io.meeds.layout.model.PortletInstanceContext; |
| 23 | +import io.meeds.layout.model.PortletInstancePreference; |
| 24 | +import io.meeds.layout.plugin.PortletInstancePreferencePlugin; |
| 25 | +import io.meeds.social.translation.model.TranslationField; |
| 26 | +import io.meeds.social.translation.service.TranslationService; |
| 27 | +import lombok.SneakyThrows; |
| 28 | +import org.apache.commons.codec.binary.Base64; |
| 29 | +import org.apache.commons.lang3.StringUtils; |
| 30 | +import org.exoplatform.commons.exception.ObjectNotFoundException; |
| 31 | +import org.exoplatform.commons.file.model.FileItem; |
| 32 | +import org.exoplatform.commons.file.services.FileService; |
| 33 | +import org.exoplatform.commons.file.services.FileStorageException; |
| 34 | +import org.exoplatform.portal.config.model.Application; |
| 35 | +import org.exoplatform.portal.pom.spi.portlet.Portlet; |
| 36 | +import org.exoplatform.portal.pom.spi.portlet.Preference; |
| 37 | +import org.exoplatform.services.log.ExoLogger; |
| 38 | +import org.exoplatform.services.log.Log; |
| 39 | +import org.exoplatform.social.attachment.AttachmentService; |
| 40 | +import org.exoplatform.social.attachment.model.ObjectAttachmentDetail; |
| 41 | +import org.exoplatform.social.attachment.model.ObjectAttachmentList; |
| 42 | +import org.json.JSONObject; |
| 43 | +import org.springframework.beans.factory.annotation.Autowired; |
| 44 | + |
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Map; |
| 48 | + |
| 49 | +public class CMSPortletInstancePreferencePlugin implements PortletInstancePreferencePlugin { |
| 50 | + private static final Log LOG = ExoLogger.getLogger(SidebarLoginPortletInstancePreferencePlugin.class); |
| 51 | + |
| 52 | + private static final String DATA_INIT_PREFERENCE_NAME = "data.init"; |
| 53 | + |
| 54 | + private static final String EXPORT_DATA_INIT_PREFERENCE_NAME = "export.data.init"; |
| 55 | + |
| 56 | + private static final String EXPORT_DATA_ALT_INIT_PREFERENCE_NAME = "export.data.alt.init"; |
| 57 | + |
| 58 | + private static final String EXPORT_DATA_TRANSLATION_INIT_PREFERENCE_NAME = "export.data.translation.init"; |
| 59 | + |
| 60 | + private static final String CMS_SETTING_PREFERENCE_NAME = "name"; |
| 61 | + |
| 62 | + @Autowired |
| 63 | + private AttachmentService attachmentService; |
| 64 | + |
| 65 | + @Autowired |
| 66 | + private FileService fileService; |
| 67 | + |
| 68 | + @Autowired |
| 69 | + private TranslationService translationService; |
| 70 | + |
| 71 | + public static final String OBJECT_TYPE = "cmsPortlet"; |
| 72 | + |
| 73 | + @Override |
| 74 | + public String getPortletName() { |
| 75 | + return "CMSPortlet"; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + @SneakyThrows |
| 80 | + public List<PortletInstancePreference> generatePreferences(Application application, |
| 81 | + Portlet preferences, |
| 82 | + PortletInstanceContext portletInstanceContext) { |
| 83 | + String settingName = getCmsSettingName(preferences); |
| 84 | + List<PortletInstancePreference> result = new ArrayList<>(); |
| 85 | + preferences.forEach(preference -> { |
| 86 | + if (!preference.getName().equals("name") && |
| 87 | + !preference.getName().equals(DATA_INIT_PREFERENCE_NAME)) { |
| 88 | + result.add(new PortletInstancePreference(preference.getName(), preference.getValue())); |
| 89 | + } |
| 90 | + }); |
| 91 | + if (!StringUtils.isBlank(settingName)) { |
| 92 | + if (portletInstanceContext.isExport()) { |
| 93 | + //portlet is in export process |
| 94 | + //we need to put background images and translations in preferences |
| 95 | + result.addAll(computePreferencesToExport(settingName)); |
| 96 | + } else { |
| 97 | + result.add(new PortletInstancePreference(DATA_INIT_PREFERENCE_NAME, settingName)); |
| 98 | + } |
| 99 | + } else { |
| 100 | + |
| 101 | + if (portletInstanceContext.isExport() && preferences.getPreference(DATA_INIT_PREFERENCE_NAME) != null) { |
| 102 | + //portlet is in export process |
| 103 | + //we need to put background images and translations in preferences |
| 104 | + settingName = preferences.getPreference(DATA_INIT_PREFERENCE_NAME).getValue(); |
| 105 | + result.addAll(computePreferencesToExport(settingName)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + return result; |
| 111 | + } |
| 112 | + |
| 113 | + private String getFileContent(String id) { |
| 114 | + try { |
| 115 | + FileItem |
| 116 | + file = |
| 117 | + fileService.getFile(Long.parseLong(id)); |
| 118 | + if (file == null) { |
| 119 | + return null; |
| 120 | + } else { |
| 121 | + return Base64.encodeBase64String(file.getAsByte()); |
| 122 | + } |
| 123 | + } catch (FileStorageException e) { |
| 124 | + LOG.warn("Unable to read file with id=", id, e); |
| 125 | + return null; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private String getCmsSettingName(Portlet preferences) { |
| 130 | + if (preferences == null) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + Preference settingNamePreference = preferences.getPreference(CMS_SETTING_PREFERENCE_NAME); |
| 134 | + return settingNamePreference == null ? null : settingNamePreference.getValue(); |
| 135 | + } |
| 136 | + |
| 137 | + private List<PortletInstancePreference> computePreferencesToExport(String settingName) { |
| 138 | + List<PortletInstancePreference> preferencesToExport = new ArrayList<>(); |
| 139 | + |
| 140 | + try { |
| 141 | + JSONObject translationsJson = new JSONObject(); |
| 142 | + Map<String, TranslationField> |
| 143 | + translations = translationService.getAllTranslationFields(OBJECT_TYPE, settingName); |
| 144 | + translations.entrySet().forEach(entry -> { |
| 145 | + String translationKey = entry.getKey(); |
| 146 | + TranslationField translationField = entry.getValue(); |
| 147 | + if (!translationField.getLabels().isEmpty()) { |
| 148 | + translationsJson.put(translationKey, translationField.getLabels()); |
| 149 | + } |
| 150 | + }); |
| 151 | + if (!translationsJson.isEmpty()) { |
| 152 | + preferencesToExport.add(new PortletInstancePreference(EXPORT_DATA_TRANSLATION_INIT_PREFERENCE_NAME, |
| 153 | + translationsJson.toString())); |
| 154 | + } |
| 155 | + } catch (ObjectNotFoundException o) { |
| 156 | + //nothing to do, no translations to copy |
| 157 | + } |
| 158 | + |
| 159 | + ObjectAttachmentList attachmentList = attachmentService.getAttachments(OBJECT_TYPE, settingName); |
| 160 | + ObjectAttachmentDetail file = attachmentList == null || attachmentList.getAttachments() == null || attachmentList.getAttachments().size() == 0 ? |
| 161 | + null : |
| 162 | + attachmentList.getAttachments().getFirst(); |
| 163 | + if (file != null) { |
| 164 | + String fileContent = getFileContent(file.getId()); |
| 165 | + preferencesToExport.add(new PortletInstancePreference(EXPORT_DATA_INIT_PREFERENCE_NAME, fileContent)); |
| 166 | + if (file.getAltText() != null) { |
| 167 | + preferencesToExport.add(new PortletInstancePreference(EXPORT_DATA_ALT_INIT_PREFERENCE_NAME, file.getAltText())); |
| 168 | + } |
| 169 | + } |
| 170 | + return preferencesToExport; |
| 171 | + } |
| 172 | +} |
0 commit comments