Skip to content

Instantly share code, notes, and snippets.

View marcosblandim's full-sized avatar

marcosblandim

View GitHub Profile
@marcosblandim
marcosblandim / createDLFileFromString.groovy
Created September 14, 2023 03:13
Create DL File from text in Liferay Groovy
import com.liferay.document.library.kernel.service.DLAppLocalServiceUtil
import com.liferay.petra.string.StringPool
import com.liferay.portal.kernel.service.ServiceContextThreadLocal
import java.nio.charset.StandardCharsets
void createDLFile(String filnameWithExtension, long folderId, String mimeType, String title, String description, String text) {
def serviceContext = ServiceContextThreadLocal.getServiceContext()
def inputStream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))
@marcosblandim
marcosblandim / getDisplayPageAssetAndJournalInsidePortlet.java
Created August 15, 2023 22:01
Get the Display Page Asset and Journal inside a Portlet
import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil;
import com.liferay.info.constants.InfoDisplayWebKeys;
import com.liferay.info.item.InfoItemDetails;
import com.liferay.journal.model.JournalArticle;
import com.liferay.journal.service.JournalArticleLocalService;
import com.liferay.journal.service.JournalArticleLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.util.PortalUtil;
@marcosblandim
marcosblandim / getOSGiService.WithServiceTracker.groovy
Created August 2, 2023 23:29
Liferay: get OSGi Service with ServiceTracker in Groovy
import org.osgi.framework.Bundle
import org.osgi.framework.BundleContext
import org.osgi.framework.FrameworkUtil
import org.osgi.util.tracker.ServiceTracker
static <T> T getService(Class<T> clazz) {
Bundle bundle = FrameworkUtil.getBundle(clazz)
BundleContext bundleContext = bundle.getBundleContext()
ServiceTracker serviceTracker = new ServiceTracker(bundleContext, clazz, null)
@marcosblandim
marcosblandim / removeStringAccents.java
Last active July 10, 2023 15:32
Remove accent from string in Java
import java.text.Normalizer;
public String unaccent(String str) {
return Normalizer.normalize(src, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
}
@marcosblandim
marcosblandim / disableComponentOnactivate.java
Created June 27, 2023 16:57
Disable component on activate
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
* <p>
* This library 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 2.1 of the License, or (at your option)
* any later version.
* <p>
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
@marcosblandim
marcosblandim / getDDMFieldValue.groovy
Last active June 13, 2023 17:01
Liferay: Get Journal Article's DDM Field value
import com.liferay.dynamic.data.mapping.storage.DDMFormFieldValue;
import com.liferay.journal.model.JournalArticle;
import com.liferay.journal.service.JournalArticleLocalServiceUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
String fieldName = "field name";
Locale locale = LocaleUtil.getDefault();
boolean includeNestedDDMFormFieldValues = true;
private SchedulerEngine getSchedulerEngine() {
SchedulerEngine schedulerEngine = (SchedulerEngine) IdentifiableOSGiServiceUtil.getIdentifiableOSGiService(OSGI_SERVICE_IDENTIFIER);
}
private final String OSGI_SERVICE_IDENTIFIER = "com.liferay.portal.scheduler.multiple.internal.ClusterSchedulerEngine";
SchedulerEngine schedulerEngine = getSchedulerEngine();
@marcosblandim
marcosblandim / getOSGiService.groovy
Last active September 6, 2023 14:41
Liferay: Get OSGi Service in Groovy
import com.liferay.registry.Registry
import com.liferay.registry.RegistryUtil
import com.liferay.registry.ServiceReference
def getService(Class clazz) {
Registry registry = RegistryUtil.getRegistry();
ServiceReference serviceReference = registry.getServiceReference(clazz);
return registry.getService(serviceReference);
@marcosblandim
marcosblandim / fragment-number-configuration.json
Created January 30, 2023 16:32
Liferay: Fragment configuration of an integer number with boundaries
{
"fieldSets": [
{
"fields": [
{
"name": "number",
"label": "Number",
"type": "text",
"dataType": "int",
"defaultValue": "5",
@marcosblandim
marcosblandim / UseOSGIServicesGroovy.groovy
Last active April 6, 2023 14:39
How to use Liferay OSGI Services inside Server Administration Groovy
import com.liferay.journal.service.JournalArticleLocalService
import com.liferay.portal.template.ServiceLocator
serviceLocator = ServiceLocator.newInstance()
journalArticleLocalService = serviceLocator.findService(JournalArticleLocalService.class.getName())
println journalArticleLocalService.getJournalArticles(1,3)