2222import com .liferay .asset .link .service .AssetLinkLocalServiceUtil ;
2323import com .liferay .counter .kernel .service .CounterLocalServiceUtil ;
2424import com .liferay .data .engine .rest .dto .v2_0 .DataDefinition ;
25+ import com .liferay .data .engine .rest .dto .v2_0 .util .DataDefinitionDDMFormUtil ;
2526import com .liferay .data .engine .rest .resource .v2_0 .DataDefinitionResource ;
2627import com .liferay .dynamic .data .lists .model .DDLRecordSet ;
2728import com .liferay .dynamic .data .lists .service .DDLRecordSetLocalServiceUtil ;
29+ import com .liferay .dynamic .data .mapping .constants .DDMStructureConstants ;
2830import com .liferay .dynamic .data .mapping .constants .DDMTemplateConstants ;
2931import 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 ;
3037import com .liferay .dynamic .data .mapping .model .DDMStructure ;
3138import com .liferay .dynamic .data .mapping .model .DDMTemplate ;
39+ import com .liferay .dynamic .data .mapping .service .DDMStructureLayoutLocalServiceUtil ;
3240import com .liferay .dynamic .data .mapping .service .DDMStructureLocalServiceUtil ;
3341import 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 ;
3445import com .liferay .journal .constants .JournalArticleConstants ;
3546import com .liferay .journal .model .JournalArticle ;
3647import com .liferay .journal .model .JournalFolder ;
4960import com .liferay .portal .kernel .service .ServiceContextThreadLocal ;
5061import com .liferay .portal .kernel .service .UserLocalServiceUtil ;
5162import com .liferay .portal .kernel .template .TemplateConstants ;
63+ import com .liferay .portal .kernel .util .HashMapBuilder ;
5264import com .liferay .portal .kernel .util .LocaleUtil ;
5365import com .liferay .portal .kernel .util .LocalizationUtil ;
5466import com .liferay .portal .kernel .util .PortalUtil ;
6476import java .util .List ;
6577import java .util .Locale ;
6678import java .util .Map ;
79+ import javax .validation .groups .Default ;
6780import org .dom4j .tree .DefaultText ;
6881import org .dom4j .util .IndexedElement ;
82+ import org .osgi .framework .wiring .BundleWiring ;
6983import org .slf4j .Logger ;
7084import 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