Skip to content

Commit 40d8923

Browse files
authored
Merge pull request #46 from ktor/master
fix: fix creating articles with new json format and simplify the code
2 parents 5e74b4b + 7269b1c commit 40d8923

File tree

4 files changed

+63
-94
lines changed

4 files changed

+63
-94
lines changed

CONTRIBUTING.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
We didn't publish binary yet so you'll need to build the jar yourself. Here are the steps to do it:
1313

1414
. Download sources.
15-
. Install JDK 11
15+
. Install JDK 21
1616
. Install Maven 3.8.1 or higher.
1717
. Run: `mvn clean install`
1818

README.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= Liferay Portal DB setup core
22
:liferay-version: 7.4.3.125
3-
:current-db-setup-core-version: 7.4.3125.1
3+
:current-db-setup-core-version: 7.4.3125.2
44
:TOC:
55

66
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]
@@ -15,7 +15,7 @@ Library that allows to automate https://github.com/liferay[Liferay] data setup.
1515
|Liferay Portal version|DB setup core version
1616

1717
|{liferay-version} |{current-db-setup-core-version} link:https://github.com/ableneo/liferay-db-setup-core/tree/master[source]
18-
|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]
18+
|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]
1919
|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]
2020
|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]
2121
|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]
@@ -358,6 +358,10 @@ They are probably not perfect, please let me know if anything feels wrong or inc
358358

359359
== Changelog
360360

361+
=== Version 7.4.3125.2
362+
==== Bug fixe
363+
* fixed SetupArticles bug so it works creating new article structures, simplified the code
364+
361365
=== Version 7.4.3125.1
362366
==== Features & bug fixes
363367
* fixed SetupArticles bug so it works with data definition json export

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@
489489
<version>1.2.10</version>
490490
<scope>test</scope>
491491
</dependency>
492+
<dependency>
493+
<groupId>com.liferay</groupId>
494+
<artifactId>com.liferay.dynamic.data.mapping.api</artifactId>
495+
</dependency>
492496
<!-- test deps stop -->
493497
</dependencies>
494498

src/main/java/com/ableneo/liferay/portal/setup/core/SetupArticles.java

Lines changed: 52 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil;
2121
import com.liferay.asset.link.constants.AssetLinkConstants;
2222
import com.liferay.asset.link.service.AssetLinkLocalServiceUtil;
23-
import com.liferay.counter.kernel.service.CounterLocalServiceUtil;
2423
import com.liferay.data.engine.rest.dto.v2_0.DataDefinition;
2524
import com.liferay.data.engine.rest.resource.v2_0.DataDefinitionResource;
2625
import com.liferay.dynamic.data.lists.model.DDLRecordSet;
@@ -49,6 +48,7 @@
4948
import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
5049
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
5150
import com.liferay.portal.kernel.template.TemplateConstants;
51+
import com.liferay.portal.kernel.util.HashMapBuilder;
5252
import com.liferay.portal.kernel.util.LocaleUtil;
5353
import com.liferay.portal.kernel.util.LocalizationUtil;
5454
import com.liferay.portal.kernel.util.PortalUtil;
@@ -187,106 +187,60 @@ public static void setupSiteArticles(
187187

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

204192
String content = null;
205-
DataDefinition dataDefinition = null;
206193
try {
207194
content = ResourcesUtil.getFileContent(structure.getPath());
208-
dataDefinition = DataDefinition.toDTO(content);
209-
} catch (IOException e) {
210-
LOG.error("The structure can not be added: {}", structure.getName(), e);
211-
return;
212-
} catch (Exception e) {
213-
LOG.error(
214-
"Other error while trying to get content of the structure file. Possibly wrong filesystem path ({})?",
215-
structure.getPath(),
216-
e
217-
);
218-
return;
219-
}
220-
221-
DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
222-
groupId,
223-
classNameId,
224-
structure.getKey()
225-
);
195+
DataDefinition dataDefinition = DataDefinition.toDTO(content);
226196

227-
long runAsUserId = SetupConfigurationThreadLocal.getRunAsUserId();
228-
long companyId = SetupConfigurationThreadLocal.getRunInCompanyId();
229-
230-
DataDefinitionResource.Builder dataDefinitionResourcedBuilder = getDataDefinitionResourceFactory().create();
231-
232-
DataDefinitionResource dataDefinitionResource = dataDefinitionResourcedBuilder
233-
.user(UserLocalServiceUtil.getUser(runAsUserId))
234-
.build();
235-
236-
if (ddmStructure == null) {
237-
ddmStructure = DDMStructureLocalServiceUtil.createDDMStructure(
238-
CounterLocalServiceUtil.increment(DDMStructure.class.getName())
197+
dataDefinition.setName(() ->
198+
HashMapBuilder.<String, Object>put(String.valueOf(siteDefaultLocale), structure.getName()).build()
239199
);
240-
ddmStructure.setGroupId(groupId);
241-
ddmStructure.setCompanyId(companyId);
242-
ddmStructure.setClassNameId(classNameId);
243-
ddmStructure.setStructureKey(structure.getKey());
244-
ddmStructure = DDMStructureLocalServiceUtil.addDDMStructure(ddmStructure);
245-
}
246200

247-
final long dataDefinitionId = ddmStructure.getStructureId();
201+
DataDefinitionResource.Builder dataDefinitionResourcedBuilder = getDataDefinitionResourceFactory().create();
248202

249-
Locale contentDefaultLocale = ddmStructure.getDDMForm().getDefaultLocale();
250-
if (!contentDefaultLocale.equals(siteDefaultLocale)) {
251-
nameMap.put(contentDefaultLocale, name);
252-
}
203+
DataDefinitionResource dataDefinitionResource = dataDefinitionResourcedBuilder
204+
.user(UserLocalServiceUtil.getUser(SetupConfigurationThreadLocal.getRunAsUserId()))
205+
.build();
253206

254-
LOG.info("Structure already exists and will be overwritten.");
255-
if (structure.getParent() != null && !structure.getParent().isEmpty()) {
256-
LOG.info("Setting up parent structure: {}", structure.getName());
257-
DDMStructure parentStructure = DDMStructureLocalServiceUtil.fetchStructure(
207+
DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
258208
groupId,
259209
classNameId,
260-
structure.getParent(),
261-
true
210+
structure.getKey()
262211
);
263-
if (parentStructure != null) {
264-
ddmStructure.setParentStructureId(parentStructure.getStructureId());
212+
213+
if (ddmStructure == null) {
214+
LOG.info("Adding article structure {}", structure.getName());
215+
DataDefinitionUtil.updateDataDefinitionFields(dataDefinition, null);
216+
dataDefinition = dataDefinitionResource.postSiteDataDefinitionByContentType(
217+
groupId,
218+
"journal",
219+
dataDefinition
220+
);
265221
} else {
266-
LOG.info("Parent structure not found: {}", structure.getName());
222+
LOG.info("Updating article structure {}", structure.getName());
223+
DataDefinitionUtil.updateDataDefinitionFields(dataDefinition, ddmStructure);
224+
dataDefinition.setId(ddmStructure.getStructureId());
225+
dataDefinition = dataDefinitionResource.putDataDefinition(dataDefinition.getId(), dataDefinition);
267226
}
268-
}
269-
270-
try {
271-
DataDefinitionUtil.updateDataDefinitionFields(dataDefinition, ddmStructure);
272-
273-
dataDefinitionResource.putDataDefinition(dataDefinitionId, dataDefinition);
227+
SetupPermissions.updatePermission(
228+
String.format("Structure %s", structure.getKey()),
229+
SetupConfigurationThreadLocal.getRunInCompanyId(),
230+
dataDefinition.getId(),
231+
DDMStructure.class.getName() + "-" + JournalArticle.class.getName(),
232+
structure.getRolePermissions(),
233+
DEFAULT_DDM_PERMISSIONS
234+
);
235+
} catch (IOException e) {
236+
LOG.error("The structure can not be added: {}", structure.getName(), e);
274237
} catch (Exception e) {
275-
throw new PortalException(e);
238+
LOG.error(
239+
"Other error while trying to get content of the structure file. Possibly wrong filesystem path ({})?",
240+
structure.getPath(),
241+
e
242+
);
276243
}
277-
278-
LOG.info("Template successfully updated: {}", structure.getName());
279-
280-
SetupPermissions.updatePermission(
281-
String.format("Structure %1$s", structure.getKey()),
282-
companyId,
283-
dataDefinitionId,
284-
DDMStructure.class.getName() + "-" + JournalArticle.class.getName(),
285-
structure.getRolePermissions(),
286-
DEFAULT_DDM_PERMISSIONS
287-
);
288-
289-
return;
290244
}
291245

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

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

415-
final DDMTemplate ddmTemplate = getDdmTemplate(template.getName(), template.getTemplateKey(), groupId, classNameId);
369+
final DDMTemplate ddmTemplate = getDdmTemplate(
370+
template.getName(),
371+
template.getTemplateKey(),
372+
groupId,
373+
classNameId
374+
);
416375

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

@@ -681,13 +640,15 @@ public static JournalArticle getJournalArticle(
681640
if (articleId.equalsIgnoreCase(art.getArticleId())) {
682641
// liferay inside: uses 'ignore-case'
683642
if (art.getStatus() == WorkflowConstants.STATUS_APPROVED) {
684-
LOG.info("Found article with ID: {} and directory: {} ({})", articleId, folderPathForTheLog, folderId);
685-
withSameArticleId.add(art);
686-
} else {
687643
LOG.info(
688-
"Found article which is not 'approved' [{}], leave-alone",
689-
articleId
644+
"Found article with ID: {} and directory: {} ({})",
645+
articleId,
646+
folderPathForTheLog,
647+
folderId
690648
);
649+
withSameArticleId.add(art);
650+
} else {
651+
LOG.info("Found article which is not 'approved' [], leave-alone", articleId);
691652
}
692653
}
693654
}

0 commit comments

Comments
 (0)