Skip to content

Commit b372e53

Browse files
committed
fix: fix creating articles with new json format and simplify the code
1 parent 5e74b4b commit b372e53

File tree

4 files changed

+78
-93
lines changed

4 files changed

+78
-93
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: 67 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,26 @@
2222
import com.liferay.asset.link.service.AssetLinkLocalServiceUtil;
2323
import com.liferay.counter.kernel.service.CounterLocalServiceUtil;
2424
import com.liferay.data.engine.rest.dto.v2_0.DataDefinition;
25+
import com.liferay.data.engine.rest.dto.v2_0.util.DataDefinitionDDMFormUtil;
2526
import com.liferay.data.engine.rest.resource.v2_0.DataDefinitionResource;
2627
import com.liferay.dynamic.data.lists.model.DDLRecordSet;
2728
import com.liferay.dynamic.data.lists.service.DDLRecordSetLocalServiceUtil;
29+
import com.liferay.dynamic.data.mapping.constants.DDMStructureConstants;
2830
import com.liferay.dynamic.data.mapping.constants.DDMTemplateConstants;
2931
import com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException;
32+
import com.liferay.dynamic.data.mapping.io.DDMFormDeserializer;
33+
import com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeRequest;
34+
import com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeResponse;
35+
import com.liferay.dynamic.data.mapping.model.DDMForm;
36+
import com.liferay.dynamic.data.mapping.model.DDMFormLayout;
3037
import com.liferay.dynamic.data.mapping.model.DDMStructure;
3138
import com.liferay.dynamic.data.mapping.model.DDMTemplate;
39+
import com.liferay.dynamic.data.mapping.service.DDMStructureLayoutLocalServiceUtil;
3240
import com.liferay.dynamic.data.mapping.service.DDMStructureLocalServiceUtil;
3341
import com.liferay.dynamic.data.mapping.service.DDMTemplateLocalServiceUtil;
42+
import com.liferay.dynamic.data.mapping.storage.StorageType;
43+
import com.liferay.dynamic.data.mapping.util.DDMUtil;
44+
import com.liferay.dynamic.data.mapping.util.DefaultDDMStructureHelper;
3445
import com.liferay.journal.constants.JournalArticleConstants;
3546
import com.liferay.journal.model.JournalArticle;
3647
import com.liferay.journal.model.JournalFolder;
@@ -49,6 +60,7 @@
4960
import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
5061
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
5162
import com.liferay.portal.kernel.template.TemplateConstants;
63+
import com.liferay.portal.kernel.util.HashMapBuilder;
5264
import com.liferay.portal.kernel.util.LocaleUtil;
5365
import com.liferay.portal.kernel.util.LocalizationUtil;
5466
import com.liferay.portal.kernel.util.PortalUtil;
@@ -64,8 +76,10 @@
6476
import java.util.List;
6577
import java.util.Locale;
6678
import java.util.Map;
79+
import javax.validation.groups.Default;
6780
import org.dom4j.tree.DefaultText;
6881
import org.dom4j.util.IndexedElement;
82+
import org.osgi.framework.wiring.BundleWiring;
6983
import org.slf4j.Logger;
7084
import org.slf4j.LoggerFactory;
7185

@@ -80,6 +94,8 @@ public final class SetupArticles {
8094
private static final int ARTICLE_PUBLISH_YEAR = 2008;
8195
private static final int MIN_DISPLAY_ROWS = 10;
8296
private static ServiceTrackerBuilder<DataDefinitionResource.Factory> dataDefinitionResourceFactoryTracker;
97+
private static ServiceTrackerBuilder<DDMFormDeserializer> ddmFormDeserializerTracker;
98+
private static ServiceTrackerBuilder<DefaultDDMStructureHelper> defaultDDMStructureHelper;
8399

84100
static {
85101
DEFAULT_PERMISSIONS = new HashMap<>();
@@ -187,106 +203,60 @@ public static void setupSiteArticles(
187203

188204
public static void addDDMStructure(final StructureType structure, final long groupId, final long classNameId)
189205
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<>();
206+
final Locale siteDefaultLocale = PortalUtil.getSiteDefaultLocale(groupId);
203207

204208
String content = null;
205-
DataDefinition dataDefinition = null;
206209
try {
207210
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-
);
211+
DataDefinition dataDefinition = DataDefinition.toDTO(content);
226212

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())
213+
dataDefinition.setName(() ->
214+
HashMapBuilder.<String, Object>put(String.valueOf(siteDefaultLocale), structure.getName()).build()
239215
);
240-
ddmStructure.setGroupId(groupId);
241-
ddmStructure.setCompanyId(companyId);
242-
ddmStructure.setClassNameId(classNameId);
243-
ddmStructure.setStructureKey(structure.getKey());
244-
ddmStructure = DDMStructureLocalServiceUtil.addDDMStructure(ddmStructure);
245-
}
246216

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

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

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(
223+
DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
258224
groupId,
259225
classNameId,
260-
structure.getParent(),
261-
true
226+
structure.getKey()
262227
);
263-
if (parentStructure != null) {
264-
ddmStructure.setParentStructureId(parentStructure.getStructureId());
228+
229+
if (ddmStructure == null) {
230+
LOG.info("Adding article structure {}", structure.getName());
231+
DataDefinitionUtil.updateDataDefinitionFields(dataDefinition, null);
232+
dataDefinition = dataDefinitionResource.postSiteDataDefinitionByContentType(
233+
groupId,
234+
"journal",
235+
dataDefinition
236+
);
265237
} else {
266-
LOG.info("Parent structure not found: {}", structure.getName());
238+
LOG.info("Updating article structure {}", structure.getName());
239+
DataDefinitionUtil.updateDataDefinitionFields(dataDefinition, ddmStructure);
240+
dataDefinition.setId(ddmStructure.getStructureId());
241+
dataDefinition = dataDefinitionResource.putDataDefinition(dataDefinition.getId(), dataDefinition);
267242
}
268-
}
269-
270-
try {
271-
DataDefinitionUtil.updateDataDefinitionFields(dataDefinition, ddmStructure);
272-
273-
dataDefinitionResource.putDataDefinition(dataDefinitionId, dataDefinition);
243+
SetupPermissions.updatePermission(
244+
String.format("Structure %s", structure.getKey()),
245+
SetupConfigurationThreadLocal.getRunInCompanyId(),
246+
dataDefinition.getId(),
247+
DDMStructure.class.getName() + "-" + JournalArticle.class.getName(),
248+
structure.getRolePermissions(),
249+
DEFAULT_DDM_PERMISSIONS
250+
);
251+
} catch (IOException e) {
252+
LOG.error("The structure can not be added: {}", structure.getName(), e);
274253
} catch (Exception e) {
275-
throw new PortalException(e);
254+
LOG.error(
255+
"Other error while trying to get content of the structure file. Possibly wrong filesystem path ({})?",
256+
structure.getPath(),
257+
e
258+
);
276259
}
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;
290260
}
291261

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

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

415-
final DDMTemplate ddmTemplate = getDdmTemplate(template.getName(), template.getTemplateKey(), groupId, classNameId);
385+
final DDMTemplate ddmTemplate = getDdmTemplate(
386+
template.getName(),
387+
template.getTemplateKey(),
388+
groupId,
389+
classNameId
390+
);
416391

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

@@ -681,13 +656,15 @@ public static JournalArticle getJournalArticle(
681656
if (articleId.equalsIgnoreCase(art.getArticleId())) {
682657
// liferay inside: uses 'ignore-case'
683658
if (art.getStatus() == WorkflowConstants.STATUS_APPROVED) {
684-
LOG.info("Found article with ID: {} and directory: {} ({})", articleId, folderPathForTheLog, folderId);
685-
withSameArticleId.add(art);
686-
} else {
687659
LOG.info(
688-
"Found article which is not 'approved' [{}], leave-alone",
689-
articleId
660+
"Found article with ID: {} and directory: {} ({})",
661+
articleId,
662+
folderPathForTheLog,
663+
folderId
690664
);
665+
withSameArticleId.add(art);
666+
} else {
667+
LOG.info("Found article which is not 'approved' [], leave-alone", articleId);
691668
}
692669
}
693670
}

0 commit comments

Comments
 (0)