Skip to content

fix: fix creating articles with new json format and simplify the code #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
We didn't publish binary yet so you'll need to build the jar yourself. Here are the steps to do it:

. Download sources.
. Install JDK 11
. Install JDK 21
. Install Maven 3.8.1 or higher.
. Run: `mvn clean install`

Expand Down
8 changes: 6 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Liferay Portal DB setup core
:liferay-version: 7.4.3.125
:current-db-setup-core-version: 7.4.3125.1
:current-db-setup-core-version: 7.4.3125.2
:TOC:

image:https://maven-badges.herokuapp.com/maven-central/com.ableneo.liferay/com.ableneo.liferay.db.setup.core/badge.svg?color=blue[Maven Central,link=https://search.maven.org/search?q=g:com.ableneo.liferay%20AND%20a:com.ableneo.liferay.db.setup.core]
Expand All @@ -15,7 +15,7 @@ Library that allows to automate https://github.com/liferay[Liferay] data setup.
|Liferay Portal version|DB setup core version

|{liferay-version} |{current-db-setup-core-version} link:https://github.com/ableneo/liferay-db-setup-core/tree/master[source]
|7.4.3.125 | 7.4.3125.1 link:https://central.sonatype.com/artifact/com.ableneo.liferay/com.ableneo.liferay.db.setup.core/7.4.3125.1[jar] link:https://github.com/ableneo/liferay-db-setup-core/tree/7.4.3125.1[source]
|7.4.3.125 | 7.4.3125.2 link:https://central.sonatype.com/artifact/com.ableneo.liferay/com.ableneo.liferay.db.setup.core/7.4.3125.2[jar] link:https://github.com/ableneo/liferay-db-setup-core/tree/7.4.3125.2[source]
|7.4.3.86 | 7.4.3861 link:https://central.sonatype.com/artifact/com.ableneo.liferay/com.ableneo.liferay.db.setup.core/7.4.3861[jar] link:https://github.com/ableneo/liferay-db-setup-core/tree/7.4.3861[source]
|7.3.6 | 7.3.605 link:https://central.sonatype.com/artifact/com.ableneo.liferay/com.ableneo.liferay.db.setup.core/7.3.605[jar] link:https://github.com/ableneo/liferay-db-setup-core/tree/7.3.6[source]
|7.3.6 | 7.3.605 link:https://central.sonatype.com/artifact/com.ableneo.liferay/com.ableneo.liferay.db.setup.core/7.3.605[jar] link:https://github.com/ableneo/liferay-db-setup-core/tree/7.3.6[source]
Expand Down Expand Up @@ -358,6 +358,10 @@ They are probably not perfect, please let me know if anything feels wrong or inc

== Changelog

=== Version 7.4.3125.2
==== Bug fixe
* fixed SetupArticles bug so it works creating new article structures, simplified the code

=== Version 7.4.3125.1
==== Features & bug fixes
* fixed SetupArticles bug so it works with data definition json export
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@
<version>1.2.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.dynamic.data.mapping.api</artifactId>
</dependency>
<!-- test deps stop -->
</dependencies>

Expand Down
143 changes: 52 additions & 91 deletions src/main/java/com/ableneo/liferay/portal/setup/core/SetupArticles.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil;
import com.liferay.asset.link.constants.AssetLinkConstants;
import com.liferay.asset.link.service.AssetLinkLocalServiceUtil;
import com.liferay.counter.kernel.service.CounterLocalServiceUtil;
import com.liferay.data.engine.rest.dto.v2_0.DataDefinition;
import com.liferay.data.engine.rest.resource.v2_0.DataDefinitionResource;
import com.liferay.dynamic.data.lists.model.DDLRecordSet;
Expand Down Expand Up @@ -49,6 +48,7 @@
import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import com.liferay.portal.kernel.template.TemplateConstants;
import com.liferay.portal.kernel.util.HashMapBuilder;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.LocalizationUtil;
import com.liferay.portal.kernel.util.PortalUtil;
Expand Down Expand Up @@ -187,106 +187,60 @@ public static void setupSiteArticles(

public static void addDDMStructure(final StructureType structure, final long groupId, final long classNameId)
throws PortalException {
LOG.info("Adding Article structure {}", structure.getName());
Map<Locale, String> nameMap = new HashMap<>();
Locale siteDefaultLocale = null;
try {
siteDefaultLocale = PortalUtil.getSiteDefaultLocale(groupId);
} catch (PortalException e) {
LOG.error("Failed to get site default locale for groupId {}", groupId, e);
}
String name = getStructureNameOrKey(structure);
// when default site locale is not 'en_us', then LocaleUtil.getSiteDefault still returns en_us.. we are not IN the site yet..
// so an exception follows: Name is null (for en_us locale). so:
nameMap.put(siteDefaultLocale, name);
Map<Locale, String> descMap = new HashMap<>();
final Locale siteDefaultLocale = PortalUtil.getSiteDefaultLocale(groupId);

String content = null;
DataDefinition dataDefinition = null;
try {
content = ResourcesUtil.getFileContent(structure.getPath());
dataDefinition = DataDefinition.toDTO(content);
} catch (IOException e) {
LOG.error("The structure can not be added: {}", structure.getName(), e);
return;
} catch (Exception e) {
LOG.error(
"Other error while trying to get content of the structure file. Possibly wrong filesystem path ({})?",
structure.getPath(),
e
);
return;
}

DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
groupId,
classNameId,
structure.getKey()
);
DataDefinition dataDefinition = DataDefinition.toDTO(content);

long runAsUserId = SetupConfigurationThreadLocal.getRunAsUserId();
long companyId = SetupConfigurationThreadLocal.getRunInCompanyId();

DataDefinitionResource.Builder dataDefinitionResourcedBuilder = getDataDefinitionResourceFactory().create();

DataDefinitionResource dataDefinitionResource = dataDefinitionResourcedBuilder
.user(UserLocalServiceUtil.getUser(runAsUserId))
.build();

if (ddmStructure == null) {
ddmStructure = DDMStructureLocalServiceUtil.createDDMStructure(
CounterLocalServiceUtil.increment(DDMStructure.class.getName())
dataDefinition.setName(() ->
HashMapBuilder.<String, Object>put(String.valueOf(siteDefaultLocale), structure.getName()).build()
);
ddmStructure.setGroupId(groupId);
ddmStructure.setCompanyId(companyId);
ddmStructure.setClassNameId(classNameId);
ddmStructure.setStructureKey(structure.getKey());
ddmStructure = DDMStructureLocalServiceUtil.addDDMStructure(ddmStructure);
}

final long dataDefinitionId = ddmStructure.getStructureId();
DataDefinitionResource.Builder dataDefinitionResourcedBuilder = getDataDefinitionResourceFactory().create();

Locale contentDefaultLocale = ddmStructure.getDDMForm().getDefaultLocale();
if (!contentDefaultLocale.equals(siteDefaultLocale)) {
nameMap.put(contentDefaultLocale, name);
}
DataDefinitionResource dataDefinitionResource = dataDefinitionResourcedBuilder
.user(UserLocalServiceUtil.getUser(SetupConfigurationThreadLocal.getRunAsUserId()))
.build();

LOG.info("Structure already exists and will be overwritten.");
if (structure.getParent() != null && !structure.getParent().isEmpty()) {
LOG.info("Setting up parent structure: {}", structure.getName());
DDMStructure parentStructure = DDMStructureLocalServiceUtil.fetchStructure(
DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
groupId,
classNameId,
structure.getParent(),
true
structure.getKey()
);
if (parentStructure != null) {
ddmStructure.setParentStructureId(parentStructure.getStructureId());

if (ddmStructure == null) {
LOG.info("Adding article structure {}", structure.getName());
DataDefinitionUtil.updateDataDefinitionFields(dataDefinition, null);
dataDefinition = dataDefinitionResource.postSiteDataDefinitionByContentType(
groupId,
"journal",
dataDefinition
);
} else {
LOG.info("Parent structure not found: {}", structure.getName());
LOG.info("Updating article structure {}", structure.getName());
DataDefinitionUtil.updateDataDefinitionFields(dataDefinition, ddmStructure);
dataDefinition.setId(ddmStructure.getStructureId());
dataDefinition = dataDefinitionResource.putDataDefinition(dataDefinition.getId(), dataDefinition);
}
}

try {
DataDefinitionUtil.updateDataDefinitionFields(dataDefinition, ddmStructure);

dataDefinitionResource.putDataDefinition(dataDefinitionId, dataDefinition);
SetupPermissions.updatePermission(
String.format("Structure %s", structure.getKey()),
SetupConfigurationThreadLocal.getRunInCompanyId(),
dataDefinition.getId(),
DDMStructure.class.getName() + "-" + JournalArticle.class.getName(),
structure.getRolePermissions(),
DEFAULT_DDM_PERMISSIONS
);
} catch (IOException e) {
LOG.error("The structure can not be added: {}", structure.getName(), e);
} catch (Exception e) {
throw new PortalException(e);
LOG.error(
"Other error while trying to get content of the structure file. Possibly wrong filesystem path ({})?",
structure.getPath(),
e
);
}

LOG.info("Template successfully updated: {}", structure.getName());

SetupPermissions.updatePermission(
String.format("Structure %1$s", structure.getKey()),
companyId,
dataDefinitionId,
DDMStructure.class.getName() + "-" + JournalArticle.class.getName(),
structure.getRolePermissions(),
DEFAULT_DDM_PERMISSIONS
);

return;
}

private static DataDefinitionResource.Factory getDataDefinitionResourceFactory() {
Expand Down Expand Up @@ -412,7 +366,12 @@ public static void addDDMTemplate(final Adt template, final long groupId) throws

String language = template.getLanguage() == null ? TemplateConstants.LANG_TYPE_FTL : template.getLanguage();

final DDMTemplate ddmTemplate = getDdmTemplate(template.getName(), template.getTemplateKey(), groupId, classNameId);
final DDMTemplate ddmTemplate = getDdmTemplate(
template.getName(),
template.getTemplateKey(),
groupId,
classNameId
);

String script = ResourcesUtil.getFileContent(template.getPath());

Expand Down Expand Up @@ -681,13 +640,15 @@ public static JournalArticle getJournalArticle(
if (articleId.equalsIgnoreCase(art.getArticleId())) {
// liferay inside: uses 'ignore-case'
if (art.getStatus() == WorkflowConstants.STATUS_APPROVED) {
LOG.info("Found article with ID: {} and directory: {} ({})", articleId, folderPathForTheLog, folderId);
withSameArticleId.add(art);
} else {
LOG.info(
"Found article which is not 'approved' [{}], leave-alone",
articleId
"Found article with ID: {} and directory: {} ({})",
articleId,
folderPathForTheLog,
folderId
);
withSameArticleId.add(art);
} else {
LOG.info("Found article which is not 'approved' [], leave-alone", articleId);
}
}
}
Expand Down