Skip to content

Commit

Permalink
Merge Page Templates - Meeds-io/MIPs#133 (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker authored May 28, 2024
2 parents 4374109 + 4c08fdd commit 8bc6926
Show file tree
Hide file tree
Showing 51 changed files with 1,989 additions and 104 deletions.
18 changes: 18 additions & 0 deletions crowdin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,22 @@ files: [
"escape_special_characters": 0,
"escape_quotes" : 0,
},
{
"source" : "/layout-webapp/src/main/resources/locale/portlet/Analytics_en.properties",

"translation" : "%original_path%/%file_name%!_%locale_with_underscore%.%file_extension%",
"translation_replace" : {
"_en!": "","ar_SA": "ar","ar_OM": "aro","az_AZ": "az","ca_ES": "ca","ceb_PH": "ceb",
"co_FR": "co","cs_CZ": "cs","de_DE": "de","el_GR": "el","en_US": "en","es_ES": "es_ES","eu_ES": "eu","fa_IR": "fa",
"fi_FI": "fi","fil_PH": "fil","fr_FR": "fr","hi_IN": "hi","hu_HU": "hu","id_ID": "id","it_IT": "it","ja_JP": "ja",
"kab_KAB": "kab","ko_KR": "ko","lt_LT": "lt","ms_MY": "ms","nl_NL": "nl","no_NO": "no","pcm_NG": "pcm","pl_PL": "pl",
"pt_BR": "pt_BR","pt_PT": "pt_PT","ro_RO": "ro","ru_RU": "ru","sk_SK": "sk","sl_SI": "sl","sq_AL": "sq",
"sv_SE": "sv_SE","th_TH": "th","tl_PH": "tl","tr_TR": "tr","uk_UA": "uk","ur_IN": "ur_IN","vi_VN": "vi",
"zh_CN": "zh_CN","zh_TW": "zh_TW",
},
"dest" : "add__ons/layout/portlet/Analytics.properties",
"update_option" : "update_as_unapproved",
"escape_special_characters": 0,
"escape_quotes" : 0,
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ public class PageTemplateEntity {
@SequenceGenerator(name = "SEQ_PAGE_TEMPLATE_ID", sequenceName = "SEQ_PAGE_TEMPLATE_ID", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_PAGE_TEMPLATE_ID")
@Column(name = "ID")
protected Long id;
protected Long id;

@Column(name = "DISABLED")
private boolean disabled;

@Column(name = "SYSTEM_PAGE_TEMPLATE")
private boolean system;

@Column(name = "CATEGORY")
private String category;

@Column(name = "CONTENT")
private String content;
private String content;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,32 @@
@AllArgsConstructor
public class PageTemplate {

private long id;
private long id;

private String content;
private boolean disabled;

private String name;
private boolean system;

private String description;
private String category;

private long illustrationId;
private String content;

public PageTemplate(long id, String content) {
private String name;

private String description;

private long illustrationId;

public PageTemplate(long id,
boolean disabled,
boolean system,
String category,
String content) {
this.id = id;
this.content = content;
this.disabled = disabled;
this.system = system;
this.category = category;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class PageTemplateDescriptor {

private Map<String, String> descriptions;

private String category;

private boolean system;

private String illustrationPath;

private String layoutPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ public List<PageTemplate> getPageTemplates(HttpServletRequest request) {
return pageTemplateService.getPageTemplates(request.getLocale(), true);
}

@GetMapping("{id}")
@Secured("users")
@Operation(summary = "Retrieve a page template designated by its id", method = "GET",
description = "This will retrieve a page template designated by its id")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Request fulfilled"), })
public PageTemplate getPageTemplate(
HttpServletRequest request,
@Parameter(description = "Page template identifier")
@PathVariable("id")
long id) {
return pageTemplateService.getPageTemplate(id, request.getLocale(), true);
}

@PostMapping
@Secured("users")
@Operation(summary = "Create a page template", method = "POST", description = "This creates a new page template")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public NodeLabel getNodeLabels(long nodeId, String username) throws ObjectNotFou
String label = nodeData.getState().getLabel();
if (ExpressionUtil.isResourceBindingExpression(label)) {
SiteKey siteKey = nodeData.getSiteKey();
ResourceBundle nodeLabelResourceBundle = resourceBundleManager.getNavigationResourceBundle(locale.toLanguageTag(),
ResourceBundle nodeLabelResourceBundle = resourceBundleManager.getNavigationResourceBundle(getLocaleName(locale),
siteKey.getTypeName(),
siteKey.getName());
if (nodeLabelResourceBundle != null) {
Expand Down Expand Up @@ -448,4 +448,8 @@ public NodeLabel toNodeLabel(Map<Locale, State> nodeLabels) {
return nodeLabel;
}

private String getLocaleName(Locale locale) {
return locale.toLanguageTag().replace("-", "_"); // Use same name as localeConfigService
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ private Page createPageInstance(String siteType,
if (pageTemplate == null) {
throw new ObjectNotFoundException("pageTemplate not found");
}
if (pageTemplate.isDisabled()) {
throw new IllegalArgumentException("pageTemplate with designated Id is disabled");
}
page = userPortalConfigService.createPageTemplate(EMPTY_PAGE_TEMPLATE,
siteType,
siteName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;

import org.apache.commons.io.IOUtils;
Expand All @@ -49,6 +50,7 @@
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.resources.LocaleConfigService;
import org.exoplatform.services.resources.ResourceBundleService;
import org.exoplatform.services.security.ConversationState;
import org.exoplatform.social.attachment.AttachmentService;
import org.exoplatform.social.attachment.model.UploadedAttachmentDetail;
Expand All @@ -71,13 +73,17 @@
@Component
public class PageTemplateImportService {

private static final Scope PAGE_TEMPLATE_IMPORT_SCOPE = Scope.APPLICATION.id("PAGE_TEMPLATE_IMPORT");
private static final Scope PAGE_TEMPLATE_IMPORT_SCOPE = Scope.APPLICATION.id("PAGE_TEMPLATE_IMPORT");

private static final Context PAGE_TEMPLATE_CONTEXT = Context.GLOBAL.id("PAGE_TEMPLATE");
private static final Context PAGE_TEMPLATE_CONTEXT = Context.GLOBAL.id("PAGE_TEMPLATE");

private static final Log LOG = ExoLogger.getLogger(PageTemplateImportService.class);
private static final String PAGE_TEMPLATE_VERSION = "version";

private static final Random RANDOM = new Random();
private static final long PAGE_TEMPLATE_IMPORT_VERSION = 1;

private static final Log LOG = ExoLogger.getLogger(PageTemplateImportService.class);

private static final Random RANDOM = new Random();

@Autowired
private LayoutAclService layoutAclService;
Expand All @@ -97,6 +103,9 @@ public class PageTemplateImportService {
@Autowired
private SettingService settingService;

@Autowired
private ResourceBundleService resourceBundleService;

@Autowired
private ConfigurationManager configurationManager;

Expand All @@ -121,6 +130,13 @@ public void init() {
@ContainerTransactional
public void importPageTemplates() {
LOG.info("Importing Page Templates");
if (!forceReimportTemplates
&& getSettingValue(PAGE_TEMPLATE_VERSION) != PAGE_TEMPLATE_IMPORT_VERSION) {
forceReimportTemplates = true;
}

ConversationState currentConversationState = ConversationState.getCurrent();
ConversationState.setCurrent(layoutAclService.getSuperUserConversationState());
try {
Enumeration<URL> templateFiles = PortalContainer.getInstance()
.getPortalClassLoader()
Expand All @@ -139,7 +155,11 @@ public void importPageTemplates() {
.forEach(this::importDescriptor);
} catch (Exception e) {
LOG.warn("An error occurred while importing page templates", e);
} finally {
ConversationState.setCurrent(currentConversationState);
}
setSettingValue(PAGE_TEMPLATE_VERSION, PAGE_TEMPLATE_IMPORT_VERSION);
LOG.info("Importing Page Templates finished successfully");
}

protected List<PageTemplateDescriptor> parseDescriptors(URL url) {
Expand All @@ -155,42 +175,42 @@ protected List<PageTemplateDescriptor> parseDescriptors(URL url) {

protected void importDescriptor(PageTemplateDescriptor descriptor) {
String descriptorId = descriptor.getId();
long oldTemplateId = getAlreadyImportedTemplateId(descriptorId);
if (forceReimportTemplates || oldTemplateId == 0) {
importPageTemplate(descriptor, oldTemplateId);
long existingTemplateId = getSettingValue(descriptorId);
if (forceReimportTemplates || existingTemplateId == 0) {
importPageTemplate(descriptor, existingTemplateId);
} else {
LOG.info("Ignore re-importing Page Template {}", descriptorId);
}
}

protected void importPageTemplate(PageTemplateDescriptor d, long oldTemplateId) {
ConversationState currentConversationState = ConversationState.getCurrent();
ConversationState.setCurrent(layoutAclService.getSuperUserConversationState());
LOG.info("Importing Page Template {}", d.getId());
try {
LOG.info("Importing Page Template {}", d.getId());
PageTemplate pageTemplate = createPageTemplate(d, oldTemplateId);
if (oldTemplateId == 0 || pageTemplate.getId() != oldTemplateId) {
if (forceReimportTemplates || oldTemplateId == 0 || pageTemplate.getId() != oldTemplateId) {
LOG.info("Importing Page Template {} title translations", d.getId());
saveTemplateNames(d, pageTemplate);
LOG.info("Importing Page Template {} description translations", d.getId());
saveTemplateDescriptions(d, pageTemplate);
LOG.info("Importing Page Template {} illustration", d.getId());
saveTemplateIllustration(pageTemplate.getId(), d.getIllustrationPath());
// Mark as imported
settingService.set(PAGE_TEMPLATE_CONTEXT,
PAGE_TEMPLATE_IMPORT_SCOPE,
d.getId(),
SettingValue.create(String.valueOf(pageTemplate.getId())));
setSettingValue(d.getId(), pageTemplate.getId());
}
LOG.info("Importing Page Template {} finished successfully", d.getId());
} catch (Exception e) {
LOG.warn("An error occurred while importing page template {}", d.getId(), e);
} finally {
ConversationState.setCurrent(currentConversationState);
}
}

protected void saveTemplateNames(PageTemplateDescriptor d, PageTemplate pageTemplate) {
try {
translationService.deleteTranslationLabels(PageTemplateTranslationPlugin.OBJECT_TYPE,
pageTemplate.getId(),
PageTemplateTranslationPlugin.TITLE_FIELD_NAME);
} catch (Exception e) { // NOSONAR
// Normal, when not exists
}
d.getNames()
.forEach((k, v) -> saveTranslationLabel(PageTemplateTranslationPlugin.OBJECT_TYPE,
pageTemplate.getId(),
Expand All @@ -209,6 +229,13 @@ protected void saveTemplateNames(PageTemplateDescriptor d, PageTemplate pageTemp
}

protected void saveTemplateDescriptions(PageTemplateDescriptor d, PageTemplate pageTemplate) {
try {
translationService.deleteTranslationLabels(PageTemplateTranslationPlugin.OBJECT_TYPE,
pageTemplate.getId(),
PageTemplateTranslationPlugin.DESCRIPTION_FIELD_NAME);
} catch (Exception e) { // NOSONAR
// Normal, when not exists
}
d.getDescriptions()
.forEach((k, v) -> saveTranslationLabel(PageTemplateTranslationPlugin.OBJECT_TYPE,
pageTemplate.getId(),
Expand All @@ -232,11 +259,12 @@ protected PageTemplate createPageTemplate(PageTemplateDescriptor d, long oldTemp
if (oldTemplateId > 0) {
pageTemplate = pageTemplateService.getPageTemplate(oldTemplateId);
}
boolean isNew = false;
if (pageTemplate == null) {
boolean isNew = pageTemplate == null;
if (isNew) {
pageTemplate = new PageTemplate();
isNew = true;
}
pageTemplate.setCategory(d.getCategory());
pageTemplate.setSystem(d.isSystem());
try (InputStream is = configurationManager.getInputStream(d.getLayoutPath())) {
String xml = IOUtil.getStreamContentAsString(is);
Container layout = fromXML(xml);
Expand All @@ -251,6 +279,12 @@ protected PageTemplate createPageTemplate(PageTemplateDescriptor d, long oldTemp

@SneakyThrows
protected void saveTranslationLabel(String objectType, long id, String fieldName, Locale locale, String label) {
if (PortalContainer.getInstanceIfPresent() != null) {
String i18nLabel = getI18NLabel(label, locale);
if (i18nLabel != null) {
label = i18nLabel;
}
}
translationService.saveTranslationLabel(objectType,
id,
fieldName,
Expand Down Expand Up @@ -287,9 +321,32 @@ protected Container fromXML(String xml) {
return obj.getObject();
}

protected long getAlreadyImportedTemplateId(String descriptorId) {
protected String getI18NLabel(String label, Locale locale) {
try {
ResourceBundle resourceBundle =
resourceBundleService.getResourceBundle("locale.portlet.Portlets",
locale,
PortalContainer.getInstance()
.getPortalClassLoader());
if (resourceBundle != null && resourceBundle.containsKey(label)) {
return resourceBundle.getString(label);
}
} catch (Exception e) {
LOG.debug("Resource Bundle not found with locale {}", locale, e);
}
return null;
}

protected void setSettingValue(String name, long value) {
settingService.set(PAGE_TEMPLATE_CONTEXT,
PAGE_TEMPLATE_IMPORT_SCOPE,
name,
SettingValue.create(String.valueOf(value)));
}

protected long getSettingValue(String name) {
try {
SettingValue<?> settingValue = settingService.get(PAGE_TEMPLATE_CONTEXT, PAGE_TEMPLATE_IMPORT_SCOPE, descriptorId);
SettingValue<?> settingValue = settingService.get(PAGE_TEMPLATE_CONTEXT, PAGE_TEMPLATE_IMPORT_SCOPE, name);
return settingValue == null || settingValue.getValue() == null ? 0l : Long.parseLong(settingValue.getValue().toString());
} catch (NumberFormatException e) {
return 0l;
Expand Down
Loading

0 comments on commit 8bc6926

Please sign in to comment.