diff --git a/CONTRIBUTING.adoc b/CONTRIBUTING.adoc index 5ca3799..cdf29d7 100644 --- a/CONTRIBUTING.adoc +++ b/CONTRIBUTING.adoc @@ -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` diff --git a/README.adoc b/README.adoc index 222a8dc..309e97b 100644 --- a/README.adoc +++ b/README.adoc @@ -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] @@ -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] @@ -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 diff --git a/pom.xml b/pom.xml index 8fc94b1..deeeed8 100644 --- a/pom.xml +++ b/pom.xml @@ -489,6 +489,10 @@ 1.2.10 test + + com.liferay + com.liferay.dynamic.data.mapping.api + diff --git a/src/main/java/com/ableneo/liferay/portal/setup/core/SetupArticles.java b/src/main/java/com/ableneo/liferay/portal/setup/core/SetupArticles.java index 16cdad2..1f35b6c 100644 --- a/src/main/java/com/ableneo/liferay/portal/setup/core/SetupArticles.java +++ b/src/main/java/com/ableneo/liferay/portal/setup/core/SetupArticles.java @@ -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; @@ -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; @@ -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 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 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.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() { @@ -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()); @@ -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); } } }