|
20 | 20 | import com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil; |
21 | 21 | import com.liferay.asset.link.constants.AssetLinkConstants; |
22 | 22 | import com.liferay.asset.link.service.AssetLinkLocalServiceUtil; |
23 | | -import com.liferay.counter.kernel.service.CounterLocalServiceUtil; |
24 | 23 | import com.liferay.data.engine.rest.dto.v2_0.DataDefinition; |
25 | 24 | import com.liferay.data.engine.rest.resource.v2_0.DataDefinitionResource; |
26 | 25 | import com.liferay.dynamic.data.lists.model.DDLRecordSet; |
|
49 | 48 | import com.liferay.portal.kernel.service.ServiceContextThreadLocal; |
50 | 49 | import com.liferay.portal.kernel.service.UserLocalServiceUtil; |
51 | 50 | import com.liferay.portal.kernel.template.TemplateConstants; |
| 51 | +import com.liferay.portal.kernel.util.HashMapBuilder; |
52 | 52 | import com.liferay.portal.kernel.util.LocaleUtil; |
53 | 53 | import com.liferay.portal.kernel.util.LocalizationUtil; |
54 | 54 | import com.liferay.portal.kernel.util.PortalUtil; |
@@ -187,106 +187,60 @@ public static void setupSiteArticles( |
187 | 187 |
|
188 | 188 | public static void addDDMStructure(final StructureType structure, final long groupId, final long classNameId) |
189 | 189 | 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); |
203 | 191 |
|
204 | 192 | String content = null; |
205 | | - DataDefinition dataDefinition = null; |
206 | 193 | try { |
207 | 194 | 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); |
226 | 196 |
|
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() |
239 | 199 | ); |
240 | | - ddmStructure.setGroupId(groupId); |
241 | | - ddmStructure.setCompanyId(companyId); |
242 | | - ddmStructure.setClassNameId(classNameId); |
243 | | - ddmStructure.setStructureKey(structure.getKey()); |
244 | | - ddmStructure = DDMStructureLocalServiceUtil.addDDMStructure(ddmStructure); |
245 | | - } |
246 | 200 |
|
247 | | - final long dataDefinitionId = ddmStructure.getStructureId(); |
| 201 | + DataDefinitionResource.Builder dataDefinitionResourcedBuilder = getDataDefinitionResourceFactory().create(); |
248 | 202 |
|
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(); |
253 | 206 |
|
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( |
258 | 208 | groupId, |
259 | 209 | classNameId, |
260 | | - structure.getParent(), |
261 | | - true |
| 210 | + structure.getKey() |
262 | 211 | ); |
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 | + ); |
265 | 221 | } 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); |
267 | 226 | } |
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); |
274 | 237 | } 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 | + ); |
276 | 243 | } |
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; |
290 | 244 | } |
291 | 245 |
|
292 | 246 | private static DataDefinitionResource.Factory getDataDefinitionResourceFactory() { |
@@ -412,7 +366,12 @@ public static void addDDMTemplate(final Adt template, final long groupId) throws |
412 | 366 |
|
413 | 367 | String language = template.getLanguage() == null ? TemplateConstants.LANG_TYPE_FTL : template.getLanguage(); |
414 | 368 |
|
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 | + ); |
416 | 375 |
|
417 | 376 | String script = ResourcesUtil.getFileContent(template.getPath()); |
418 | 377 |
|
@@ -681,13 +640,15 @@ public static JournalArticle getJournalArticle( |
681 | 640 | if (articleId.equalsIgnoreCase(art.getArticleId())) { |
682 | 641 | // liferay inside: uses 'ignore-case' |
683 | 642 | if (art.getStatus() == WorkflowConstants.STATUS_APPROVED) { |
684 | | - LOG.info("Found article with ID: {} and directory: {} ({})", articleId, folderPathForTheLog, folderId); |
685 | | - withSameArticleId.add(art); |
686 | | - } else { |
687 | 643 | 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 |
690 | 648 | ); |
| 649 | + withSameArticleId.add(art); |
| 650 | + } else { |
| 651 | + LOG.info("Found article which is not 'approved' [], leave-alone", articleId); |
691 | 652 | } |
692 | 653 | } |
693 | 654 | } |
|
0 commit comments