001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.util.test;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.search.Hits;
019    import com.liferay.portal.kernel.search.Indexer;
020    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
021    import com.liferay.portal.kernel.search.QueryConfig;
022    import com.liferay.portal.kernel.search.SearchContext;
023    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
024    import com.liferay.portal.kernel.util.Constants;
025    import com.liferay.portal.kernel.util.LocaleUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.workflow.WorkflowConstants;
029    import com.liferay.portal.kernel.xml.Attribute;
030    import com.liferay.portal.kernel.xml.Document;
031    import com.liferay.portal.kernel.xml.Element;
032    import com.liferay.portal.kernel.xml.Node;
033    import com.liferay.portal.kernel.xml.SAXReaderUtil;
034    import com.liferay.portal.kernel.xml.XPath;
035    import com.liferay.portal.model.Group;
036    import com.liferay.portal.model.Layout;
037    import com.liferay.portal.service.GroupLocalServiceUtil;
038    import com.liferay.portal.service.LayoutLocalServiceUtil;
039    import com.liferay.portal.service.ServiceContext;
040    import com.liferay.portal.util.PortalUtil;
041    import com.liferay.portal.util.test.RandomTestUtil;
042    import com.liferay.portal.util.test.ServiceContextTestUtil;
043    import com.liferay.portal.util.test.TestPropsValues;
044    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
045    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
046    import com.liferay.portlet.dynamicdatamapping.util.test.DDMStructureTestUtil;
047    import com.liferay.portlet.dynamicdatamapping.util.test.DDMTemplateTestUtil;
048    import com.liferay.portlet.journal.model.JournalArticle;
049    import com.liferay.portlet.journal.model.JournalArticleConstants;
050    import com.liferay.portlet.journal.model.JournalFeed;
051    import com.liferay.portlet.journal.model.JournalFeedConstants;
052    import com.liferay.portlet.journal.model.JournalFolder;
053    import com.liferay.portlet.journal.model.JournalFolderConstants;
054    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
055    import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
056    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
057    import com.liferay.util.RSSUtil;
058    
059    import java.util.Calendar;
060    import java.util.Date;
061    import java.util.HashMap;
062    import java.util.List;
063    import java.util.Locale;
064    import java.util.Map;
065    
066    /**
067     * @author Juan Fern??ndez
068     * @author Marcellus Tavares
069     * @author Manuel de la Pe??a
070     */
071    public class JournalTestUtil {
072    
073            public static JournalArticle addArticle(long groupId, long folderId)
074                    throws Exception {
075    
076                    ServiceContext serviceContext =
077                            ServiceContextTestUtil.getServiceContext(groupId);
078    
079                    serviceContext.setCommand(Constants.ADD);
080                    serviceContext.setLayoutFullURL("http://localhost");
081    
082                    return addArticle(
083                            groupId, folderId, JournalArticleConstants.CLASSNAME_ID_DEFAULT,
084                            RandomTestUtil.randomString(), RandomTestUtil.randomString(),
085                            RandomTestUtil.randomString(), LocaleUtil.getSiteDefault(), false,
086                            false, serviceContext);
087            }
088    
089            public static JournalArticle addArticle(
090                            long groupId, long folderId, long classNameId,
091                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
092                            Map<Locale, String> contentMap, Locale defaultLocale,
093                            boolean workflowEnabled, boolean approved,
094                            ServiceContext serviceContext)
095                    throws Exception {
096    
097                    return addArticle(
098                            groupId, folderId, classNameId, titleMap, descriptionMap,
099                            contentMap, defaultLocale, null, workflowEnabled, approved,
100                            serviceContext);
101            }
102    
103            public static JournalArticle addArticle(
104                            long groupId, long folderId, long classNameId,
105                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
106                            Map<Locale, String> contentMap, Locale defaultLocale,
107                            Date expirationDate, boolean workflowEnabled, boolean approved,
108                            ServiceContext serviceContext)
109                    throws Exception {
110    
111                    return addArticle(
112                            groupId, folderId, classNameId, titleMap, descriptionMap,
113                            contentMap, null, defaultLocale, expirationDate, workflowEnabled,
114                            approved, serviceContext);
115            }
116    
117            public static JournalArticle addArticle(
118                            long groupId, long folderId, long classNameId,
119                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
120                            Map<Locale, String> contentMap, String layoutUuid,
121                            Locale defaultLocale, Date expirationDate, boolean workflowEnabled,
122                            boolean approved, ServiceContext serviceContext)
123                    throws Exception {
124    
125                    String content = DDMStructureTestUtil.getSampleStructuredContent(
126                            contentMap, LocaleUtil.toLanguageId(defaultLocale));
127    
128                    String definition = DDMStructureTestUtil.getSampleStructureDefinition(
129                            _locales, defaultLocale);
130    
131                    DDMStructure ddmStructure = DDMStructureTestUtil.addStructure(
132                            groupId, JournalArticle.class.getName(), definition, defaultLocale);
133    
134                    DDMTemplate ddmTemplate = DDMTemplateTestUtil.addTemplate(
135                            groupId, ddmStructure.getStructureId());
136    
137                    boolean neverExpire = true;
138    
139                    int expirationDateDay = 0;
140                    int expirationDateMonth = 0;
141                    int expirationDateYear = 0;
142                    int expirationDateHour = 0;
143                    int expirationDateMinute = 0;
144    
145                    if (expirationDate != null) {
146                            neverExpire = false;
147    
148                            Calendar expirationCal = CalendarFactoryUtil.getCalendar(
149                                    TestPropsValues.getUser().getTimeZone());
150    
151                            expirationCal.setTime(expirationDate);
152    
153                            expirationDateMonth = expirationCal.get(Calendar.MONTH);
154                            expirationDateDay = expirationCal.get(Calendar.DATE);
155                            expirationDateYear = expirationCal.get(Calendar.YEAR);
156                            expirationDateHour = expirationCal.get(Calendar.HOUR_OF_DAY);
157                            expirationDateMinute = expirationCal.get(Calendar.MINUTE);
158                    }
159    
160                    Calendar displayCal = CalendarFactoryUtil.getCalendar(
161                            TestPropsValues.getUser().getTimeZone());
162    
163                    int displayDateDay = displayCal.get(Calendar.DATE);
164                    int displayDateMonth = displayCal.get(Calendar.MONTH);
165                    int displayDateYear = displayCal.get(Calendar.YEAR);
166                    int displayDateHour = displayCal.get(Calendar.HOUR_OF_DAY);
167                    int displayDateMinute = displayCal.get(Calendar.MINUTE);
168    
169                    if (workflowEnabled) {
170                            serviceContext = (ServiceContext)serviceContext.clone();
171    
172                            if (approved) {
173                                    serviceContext.setWorkflowAction(
174                                            WorkflowConstants.ACTION_PUBLISH);
175                            }
176                            else {
177                                    serviceContext.setWorkflowAction(
178                                            WorkflowConstants.ACTION_SAVE_DRAFT);
179                            }
180                    }
181    
182                    return JournalArticleLocalServiceUtil.addArticle(
183                            serviceContext.getUserId(), groupId, folderId, classNameId, 0,
184                            StringPool.BLANK, true, JournalArticleConstants.VERSION_DEFAULT,
185                            titleMap, descriptionMap, content, ddmStructure.getStructureKey(),
186                            ddmTemplate.getTemplateKey(), layoutUuid, displayDateMonth,
187                            displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
188                            expirationDateMonth, expirationDateDay, expirationDateYear,
189                            expirationDateHour, expirationDateMinute, neverExpire, 0, 0, 0, 0,
190                            0, true, true, false, null, null, null, null, serviceContext);
191            }
192    
193            public static JournalArticle addArticle(
194                            long groupId, long folderId, long classNameId, String title,
195                            String description, String content, Locale defaultLocale,
196                            boolean workflowEnabled, boolean approved,
197                            ServiceContext serviceContext)
198                    throws Exception {
199    
200                    return addArticle(
201                            groupId, folderId, classNameId, title, description, content,
202                            defaultLocale, null, workflowEnabled, approved, serviceContext);
203            }
204    
205            public static JournalArticle addArticle(
206                            long groupId, long folderId, long classNameId, String title,
207                            String description, String content, Locale defaultLocale,
208                            Date expirationDate, boolean workflowEnabled, boolean approved,
209                            ServiceContext serviceContext)
210                    throws Exception {
211    
212                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
213    
214                    for (Locale locale : _locales) {
215                            titleMap.put(locale, title);
216                    }
217    
218                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
219    
220                    for (Locale locale : _locales) {
221                            descriptionMap.put(locale, description);
222                    }
223    
224                    Map<Locale, String> contentMap = new HashMap<Locale, String>();
225    
226                    for (Locale locale : _locales) {
227                            contentMap.put(locale, content);
228                    }
229    
230                    return addArticle(
231                            groupId, folderId, classNameId, titleMap, descriptionMap,
232                            contentMap, defaultLocale, expirationDate, workflowEnabled,
233                            approved, serviceContext);
234            }
235    
236            public static JournalArticle addArticle(
237                            long groupId, long folderId, String title, String content)
238                    throws Exception {
239    
240                    return addArticle(
241                            groupId, folderId, title, title, content,
242                            LocaleUtil.getSiteDefault(), false, false);
243            }
244    
245            public static JournalArticle addArticle(
246                            long groupId, long folderId, String title, String content,
247                            Locale defaultLocale, boolean workflowEnabled, boolean approved)
248                    throws Exception {
249    
250                    return addArticle(
251                            groupId, folderId, title, title, content, defaultLocale,
252                            workflowEnabled, approved);
253            }
254    
255            public static JournalArticle addArticle(
256                            long groupId, long folderId, String title, String description,
257                            String content, Locale defaultLocale, boolean workflowEnabled,
258                            boolean approved)
259                    throws Exception {
260    
261                    ServiceContext serviceContext =
262                            ServiceContextTestUtil.getServiceContext();
263    
264                    serviceContext.setCommand(Constants.ADD);
265                    serviceContext.setLayoutFullURL("http://localhost");
266    
267                    return addArticle(
268                            groupId, folderId, JournalArticleConstants.CLASSNAME_ID_DEFAULT,
269                            title, description, content, defaultLocale, workflowEnabled,
270                            approved, serviceContext);
271            }
272    
273            public static JournalArticle addArticle(
274                            long groupId, String title, String content)
275                    throws Exception {
276    
277                    return addArticle(
278                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, title,
279                            title, content, LocaleUtil.getSiteDefault(), false, false);
280            }
281    
282            public static JournalArticle addArticle(
283                            long groupId, String title, String content, Date expirationDate,
284                            ServiceContext serviceContext)
285                    throws Exception {
286    
287                    return addArticle(
288                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
289                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, title, title, content,
290                            LocaleUtil.getSiteDefault(), expirationDate, false, false,
291                            serviceContext);
292            }
293    
294            public static JournalArticle addArticle(
295                            long groupId, String title, String content, Locale defaultLocale)
296                    throws Exception {
297    
298                    return addArticle(
299                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, title,
300                            title, content, defaultLocale, false, false);
301            }
302    
303            public static JournalArticle addArticle(
304                            long groupId, String title, String content,
305                            ServiceContext serviceContext)
306                    throws Exception {
307    
308                    return addArticle(
309                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
310                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, title, title, content,
311                            LocaleUtil.getSiteDefault(), false, false, serviceContext);
312            }
313    
314            public static JournalArticle addArticleWithWorkflow(
315                            long groupId, boolean approved)
316                    throws Exception {
317    
318                    return addArticleWithWorkflow(
319                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, approved);
320            }
321    
322            public static JournalArticle addArticleWithWorkflow(
323                            long groupId, long folderId, boolean approved)
324                    throws Exception {
325    
326                    return addArticleWithWorkflow(
327                            groupId, folderId, "title", "content", approved);
328            }
329    
330            public static JournalArticle addArticleWithWorkflow(
331                            long groupId, long folderId, Map<Locale, String> titleMap,
332                            Map<Locale, String> descriptionMap, Map<Locale, String> contentMap,
333                            boolean workflowEnabled, boolean approved)
334                    throws Exception {
335    
336                    ServiceContext serviceContext =
337                            ServiceContextTestUtil.getServiceContext(groupId);
338    
339                    serviceContext.setCommand(Constants.ADD);
340                    serviceContext.setLayoutFullURL("http://localhost");
341    
342                    return addArticleWithWorkflow(
343                            groupId, folderId, titleMap, descriptionMap, contentMap,
344                            workflowEnabled, approved, serviceContext);
345            }
346    
347            public static JournalArticle addArticleWithWorkflow(
348                            long groupId, long folderId, Map<Locale, String> titleMap,
349                            Map<Locale, String> descriptionMap, Map<Locale, String> contentMap,
350                            boolean workflowEnabled, boolean approved,
351                            ServiceContext serviceContext)
352                    throws Exception {
353    
354                    return addArticle(
355                            groupId, folderId, JournalArticleConstants.CLASSNAME_ID_DEFAULT,
356                            titleMap, descriptionMap, contentMap, LocaleUtil.getSiteDefault(),
357                            workflowEnabled, approved, serviceContext);
358            }
359    
360            public static JournalArticle addArticleWithWorkflow(
361                            long groupId, long folderId, String title, String content,
362                            boolean approved)
363                    throws Exception {
364    
365                    ServiceContext serviceContext =
366                            ServiceContextTestUtil.getServiceContext(groupId);
367    
368                    serviceContext.setCommand(Constants.ADD);
369                    serviceContext.setLayoutFullURL("http://localhost");
370    
371                    return addArticleWithWorkflow(
372                            groupId, folderId, title, content, approved, serviceContext);
373            }
374    
375            public static JournalArticle addArticleWithWorkflow(
376                            long groupId, long folderId, String title, String content,
377                            boolean approved, ServiceContext serviceContext)
378                    throws Exception {
379    
380                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
381    
382                    for (Locale locale : _locales) {
383                            titleMap.put(locale, title);
384                    }
385    
386                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
387    
388                    for (Locale locale : _locales) {
389                            descriptionMap.put(locale, RandomTestUtil.randomString(50));
390                    }
391    
392                    Map<Locale, String> contentMap = new HashMap<Locale, String>();
393    
394                    for (Locale locale : _locales) {
395                            contentMap.put(locale, content);
396                    }
397    
398                    return addArticleWithWorkflow(
399                            groupId, folderId, titleMap, descriptionMap, contentMap, true,
400                            approved, serviceContext);
401            }
402    
403            public static JournalArticle addArticleWithWorkflow(
404                            long groupId, Map<Locale, String> titleMap,
405                            Map<Locale, String> descriptionMap, Map<Locale, String> contentMap,
406                            boolean approved)
407                    throws Exception {
408    
409                    return addArticleWithWorkflow(
410                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, titleMap,
411                            descriptionMap, contentMap, true, approved);
412            }
413    
414            public static JournalArticle addArticleWithXMLContent(
415                            long groupId, long folderId, long classNameId, String xml,
416                            String ddmStructureKey, String ddmTemplateKey)
417                    throws Exception {
418    
419                    return addArticleWithXMLContent(
420                            groupId, folderId, classNameId, xml, ddmStructureKey,
421                            ddmTemplateKey, LocaleUtil.getSiteDefault());
422            }
423    
424            public static JournalArticle addArticleWithXMLContent(
425                            long groupId, long folderId, long classNameId, String xml,
426                            String ddmStructureKey, String ddmTemplateKey, Locale defaultLocale)
427                    throws Exception {
428    
429                    ServiceContext serviceContext =
430                            ServiceContextTestUtil.getServiceContext(groupId);
431    
432                    return addArticleWithXMLContent(
433                            folderId, classNameId, xml, ddmStructureKey, ddmTemplateKey,
434                            defaultLocale, null, serviceContext);
435            }
436    
437            public static JournalArticle addArticleWithXMLContent(
438                            long folderId, long classNameId, String xml, String ddmStructureKey,
439                            String ddmTemplateKey, Locale defaultLocale,
440                            Map<String, byte[]> images, ServiceContext serviceContext)
441                    throws Exception {
442    
443                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
444    
445                    titleMap.put(defaultLocale, "Test Article");
446    
447                    return JournalArticleLocalServiceUtil.addArticle(
448                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
449                            folderId, classNameId, 0, StringPool.BLANK, true, 0, titleMap, null,
450                            xml, ddmStructureKey, ddmTemplateKey, null, 1, 1, 1965, 0, 0, 0, 0,
451                            0, 0, 0, true, 0, 0, 0, 0, 0, true, true, false, null, null, images,
452                            null, serviceContext);
453            }
454    
455            public static JournalArticle addArticleWithXMLContent(
456                            long folderId, long classNameId, String xml, String ddmStructureKey,
457                            String ddmTemplateKey, Locale defaultLocale,
458                            ServiceContext serviceContext)
459                    throws Exception {
460    
461                    return addArticleWithXMLContent(
462                            folderId, classNameId, xml, ddmStructureKey, ddmTemplateKey,
463                            defaultLocale, null, serviceContext);
464            }
465    
466            public static JournalArticle addArticleWithXMLContent(
467                            long groupId, String xml, String ddmStructureKey,
468                            String ddmTemplateKey)
469                    throws Exception {
470    
471                    return addArticleWithXMLContent(
472                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
473                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml, ddmStructureKey,
474                            ddmTemplateKey, LocaleUtil.getSiteDefault());
475            }
476    
477            public static JournalArticle addArticleWithXMLContent(
478                            long parentFolderId, String xml, String ddmStructureKey,
479                            String ddmTemplateKey, Map<String, byte[]> images,
480                            ServiceContext serviceContext)
481                    throws Exception {
482    
483                    return addArticleWithXMLContent(
484                            parentFolderId, JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml,
485                            ddmStructureKey, ddmTemplateKey, LocaleUtil.getSiteDefault(),
486                            images, serviceContext);
487            }
488    
489            public static JournalArticle addArticleWithXMLContent(
490                            long parentFolderId, String xml, String ddmStructureKey,
491                            String ddmTemplateKey, ServiceContext serviceContext)
492                    throws Exception {
493    
494                    return addArticleWithXMLContent(
495                            serviceContext.getScopeGroupId(), parentFolderId,
496                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml, ddmStructureKey,
497                            ddmTemplateKey, LocaleUtil.getSiteDefault());
498            }
499    
500            public static JournalArticle addArticleWithXMLContent(
501                            String xml, String ddmStructureKey, String ddmTemplateKey)
502                    throws Exception {
503    
504                    return addArticleWithXMLContent(
505                            TestPropsValues.getGroupId(),
506                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
507                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml, ddmStructureKey,
508                            ddmTemplateKey, LocaleUtil.getSiteDefault());
509            }
510    
511            public static JournalArticle addArticleWithXMLContent(
512                            String xml, String ddmStructureKey, String ddmTemplateKey,
513                            Locale defaultLocale)
514                    throws Exception {
515    
516                    return addArticleWithXMLContent(
517                            TestPropsValues.getGroupId(),
518                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
519                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml, ddmStructureKey,
520                            ddmTemplateKey, defaultLocale);
521            }
522    
523            public static JournalArticle addArticleWithXMLContent(
524                            String xml, String ddmStructureKey, String ddmTemplateKey,
525                            ServiceContext serviceContext)
526                    throws Exception {
527    
528                    return addArticleWithXMLContent(
529                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, xml,
530                            ddmStructureKey, ddmTemplateKey, serviceContext);
531            }
532    
533            public static Element addDynamicElementElement(
534                    Element element, String type, String name) {
535    
536                    Element dynamicElementElement = element.addElement("dynamic-element");
537    
538                    dynamicElementElement.addAttribute("name", name);
539                    dynamicElementElement.addAttribute("type", type);
540    
541                    return dynamicElementElement;
542            }
543    
544            public static JournalFeed addFeed(
545                            long groupId, long plid, String name, String ddmStructureKey,
546                            String ddmTemplateKey, String rendererTemplateKey)
547                    throws Exception {
548    
549                    long userId = TestPropsValues.getUserId();
550                    String feedId = StringPool.BLANK;
551                    boolean autoFeedId = true;
552                    String description = StringPool.BLANK;
553                    int delta = 0;
554                    String orderByCol = "modified-date";
555                    String orderByType = "asc";
556                    String friendlyURL = _getFeedFriendlyURL(groupId, plid);
557                    String targetPortletId = StringPool.BLANK;
558                    String contentField = JournalFeedConstants.WEB_CONTENT_DESCRIPTION;
559                    String feedFormat = RSSUtil.getFeedTypeFormat(
560                            RSSUtil.FEED_TYPE_DEFAULT);
561                    double feedVersion = RSSUtil.getFeedTypeVersion(
562                            RSSUtil.FEED_TYPE_DEFAULT);
563    
564                    ServiceContext serviceContext =
565                            ServiceContextTestUtil.getServiceContext(groupId);
566    
567                    return JournalFeedLocalServiceUtil.addFeed(
568                            userId, groupId, feedId, autoFeedId, name, description,
569                            ddmStructureKey, ddmTemplateKey, rendererTemplateKey, delta,
570                            orderByCol, orderByType, friendlyURL, targetPortletId, contentField,
571                            feedFormat, feedVersion, serviceContext);
572            }
573    
574            public static JournalFolder addFolder(
575                            long groupId, long parentFolderId, String name)
576                    throws Exception {
577    
578                    ServiceContext serviceContext =
579                            ServiceContextTestUtil.getServiceContext(groupId);
580    
581                    return addFolder(parentFolderId, name, serviceContext);
582            }
583    
584            public static JournalFolder addFolder(long groupId, String name)
585                    throws Exception {
586    
587                    return addFolder(
588                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, name);
589            }
590    
591            public static JournalFolder addFolder(
592                            long parentFolderId, String name, ServiceContext serviceContext)
593                    throws Exception {
594    
595                    JournalFolder folder = JournalFolderLocalServiceUtil.fetchFolder(
596                            serviceContext.getScopeGroupId(), parentFolderId, name);
597    
598                    if (folder != null) {
599                            return folder;
600                    }
601    
602                    return JournalFolderLocalServiceUtil.addFolder(
603                            TestPropsValues.getUserId(), serviceContext.getScopeGroupId(),
604                            parentFolderId, name, "This is a test folder.", serviceContext);
605            }
606    
607            public static Element addMetadataElement(
608                    Element element, String locale, String label) {
609    
610                    Element metadataElement = element.addElement("meta-data");
611    
612                    metadataElement.addAttribute("locale", locale);
613    
614                    Element entryElement = metadataElement.addElement("entry");
615    
616                    entryElement.addAttribute("name", "label");
617    
618                    entryElement.addCDATA(label);
619    
620                    return entryElement;
621            }
622    
623            public static void expireArticle(long groupId, JournalArticle article)
624                    throws PortalException {
625    
626                    JournalArticleLocalServiceUtil.expireArticle(
627                            article.getUserId(), article.getGroupId(), article.getArticleId(),
628                            null, ServiceContextTestUtil.getServiceContext(groupId));
629            }
630    
631            public static JournalArticle expireArticle(
632                            long groupId, JournalArticle article, double version)
633                    throws PortalException {
634    
635                    return JournalArticleLocalServiceUtil.expireArticle(
636                            article.getUserId(), article.getGroupId(), article.getArticleId(),
637                            version, null, ServiceContextTestUtil.getServiceContext(groupId));
638            }
639    
640            public static String getSampleTemplateXSL() {
641                    return "$name.getData()";
642            }
643    
644            public static int getSearchArticlesCount(long companyId, long groupId)
645                    throws Exception {
646    
647                    Indexer indexer = IndexerRegistryUtil.getIndexer(JournalArticle.class);
648    
649                    SearchContext searchContext = new SearchContext();
650    
651                    searchContext.setCompanyId(companyId);
652                    searchContext.setGroupIds(new long[] {groupId});
653                    searchContext.setKeywords(StringPool.BLANK);
654    
655                    QueryConfig queryConfig = new QueryConfig();
656    
657                    searchContext.setQueryConfig(queryConfig);
658    
659                    Hits results = indexer.search(searchContext);
660    
661                    return results.getLength();
662            }
663    
664            public static Map<String, Map<String, String>> getXsdMap(String xsd)
665                    throws Exception {
666    
667                    Map<String, Map<String, String>> map =
668                            new HashMap<String, Map<String, String>>();
669    
670                    Document document = SAXReaderUtil.read(xsd);
671    
672                    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element");
673    
674                    List<Node> nodes = xPathSelector.selectNodes(document);
675    
676                    for (Node node : nodes) {
677                            Element dynamicElementElement = (Element)node;
678    
679                            String type = dynamicElementElement.attributeValue("type");
680    
681                            if (Validator.equals(type, "selection_break")) {
682                                    continue;
683                            }
684    
685                            String name = dynamicElementElement.attributeValue("name");
686    
687                            map.put(name, _getMap(dynamicElementElement));
688                    }
689    
690                    return map;
691            }
692    
693            public static JournalArticle updateArticle(JournalArticle article)
694                    throws Exception {
695    
696                    return updateArticle(article, RandomTestUtil.randomString());
697            }
698    
699            public static JournalArticle updateArticle(
700                            JournalArticle article, String title)
701                    throws Exception {
702    
703                    return updateArticle(
704                            article, title, article.getContent(), false, false,
705                            ServiceContextTestUtil.getServiceContext());
706            }
707    
708            public static JournalArticle updateArticle(
709                            JournalArticle article, String title, String content)
710                    throws Exception {
711    
712                    return updateArticle(
713                            article, title, content, false, false,
714                            ServiceContextTestUtil.getServiceContext());
715            }
716    
717            public static JournalArticle updateArticle(
718                            JournalArticle article, String title, String content,
719                            boolean workflowEnabled, boolean approved,
720                            ServiceContext serviceContext)
721                    throws Exception {
722    
723                    if (workflowEnabled) {
724                            serviceContext = (ServiceContext)serviceContext.clone();
725    
726                            if (approved) {
727                                    serviceContext.setWorkflowAction(
728                                            WorkflowConstants.ACTION_PUBLISH);
729                            }
730                            else {
731                                    serviceContext.setWorkflowAction(
732                                            WorkflowConstants.ACTION_SAVE_DRAFT);
733                            }
734                    }
735    
736                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
737    
738                    for (Locale locale : _locales) {
739                            titleMap.put(locale, title);
740                    }
741    
742                    Date displayDate = article.getDisplayDate();
743    
744                    int displayDateMonth = 0;
745                    int displayDateDay = 0;
746                    int displayDateYear = 0;
747                    int displayDateHour = 0;
748                    int displayDateMinute = 0;
749    
750                    if (displayDate != null) {
751                            Calendar displayCal = CalendarFactoryUtil.getCalendar(
752                                    TestPropsValues.getUser().getTimeZone());
753    
754                            displayCal.setTime(displayDate);
755    
756                            displayDateMonth = displayCal.get(Calendar.MONTH);
757                            displayDateDay = displayCal.get(Calendar.DATE);
758                            displayDateYear = displayCal.get(Calendar.YEAR);
759                            displayDateHour = displayCal.get(Calendar.HOUR_OF_DAY);
760                            displayDateMinute = displayCal.get(Calendar.MINUTE);
761                    }
762    
763                    serviceContext.setCommand(Constants.UPDATE);
764                    serviceContext.setLayoutFullURL("http://localhost");
765    
766                    return JournalArticleLocalServiceUtil.updateArticle(
767                            article.getUserId(), article.getGroupId(), article.getFolderId(),
768                            article.getArticleId(), article.getVersion(), titleMap,
769                            article.getDescriptionMap(), content, article.getDDMStructureKey(),
770                            article.getDDMTemplateKey(), article.getLayoutUuid(),
771                            displayDateMonth, displayDateDay, displayDateYear, displayDateHour,
772                            displayDateMinute, 0, 0, 0, 0, 0, true, 0, 0, 0, 0, 0, true,
773                            article.getIndexable(), article.isSmallImage(),
774                            article.getSmallImageURL(), null, null, null, serviceContext);
775            }
776    
777            public static JournalArticle updateArticleWithWorkflow(
778                            JournalArticle article, boolean approved)
779                    throws Exception {
780    
781                    return updateArticle(
782                            article, RandomTestUtil.randomString(), article.getContent(), false,
783                            approved, ServiceContextTestUtil.getServiceContext());
784            }
785    
786            private static String _getFeedFriendlyURL(long groupId, long plid)
787                    throws Exception {
788    
789                    String friendlyURL = StringPool.BLANK;
790    
791                    Group group = GroupLocalServiceUtil.getGroup(groupId);
792                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
793    
794                    if (layout.isPrivateLayout()) {
795                            if (group.isUser()) {
796                                    friendlyURL = friendlyURL.concat(
797                                            PortalUtil.getPathFriendlyURLPrivateUser());
798                            }
799                            else {
800                                    friendlyURL = friendlyURL.concat(
801                                            PortalUtil.getPathFriendlyURLPrivateGroup());
802                            }
803                    }
804                    else {
805                            friendlyURL = friendlyURL.concat(
806                                    PortalUtil.getPathFriendlyURLPublic());
807                    }
808    
809                    friendlyURL = friendlyURL.concat(group.getFriendlyURL());
810                    friendlyURL = friendlyURL.concat(layout.getFriendlyURL());
811    
812                    return friendlyURL;
813            }
814    
815            private static Map<String, String> _getMap(Element dynamicElementElement) {
816                    Map<String, String> map = new HashMap<String, String>();
817    
818                    Element parentElement = dynamicElementElement.getParent();
819    
820                    String parentType = parentElement.attributeValue("type");
821    
822                    // Attributes
823    
824                    for (Attribute attribute : dynamicElementElement.attributes()) {
825    
826                            // Option element should not contain index type atribute
827    
828                            if ((Validator.equals(parentType, "list") ||
829                                     Validator.equals(parentType, "multi-list")) &&
830                                    Validator.equals(attribute.getName(), "index-type")) {
831    
832                                    continue;
833                            }
834    
835                            map.put(attribute.getName(), attribute.getValue());
836                    }
837    
838                    // Metadata
839    
840                    Element metadadataElement = dynamicElementElement.element("meta-data");
841    
842                    if (metadadataElement == null) {
843                            return map;
844                    }
845    
846                    List<Element> entryElements = metadadataElement.elements("entry");
847    
848                    for (Element entryElement : entryElements) {
849                            map.put(
850                                    entryElement.attributeValue("name"), entryElement.getText());
851                    }
852    
853                    return map;
854            }
855    
856            private static final Locale[] _locales = {
857                    LocaleUtil.US, LocaleUtil.GERMANY, LocaleUtil.SPAIN
858            };
859    
860    }