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, "general",
186                            ddmStructure.getStructureKey(), ddmTemplate.getTemplateKey(),
187                            layoutUuid, displayDateMonth, displayDateDay, displayDateYear,
188                            displayDateHour, displayDateMinute, expirationDateMonth,
189                            expirationDateDay, expirationDateYear, expirationDateHour,
190                            expirationDateMinute, neverExpire, 0, 0, 0, 0, 0, true, true, false,
191                            null, null, null, null, serviceContext);
192            }
193    
194            public static JournalArticle addArticle(
195                            long groupId, long folderId, long classNameId, String title,
196                            String description, String content, Locale defaultLocale,
197                            boolean workflowEnabled, boolean approved,
198                            ServiceContext serviceContext)
199                    throws Exception {
200    
201                    return addArticle(
202                            groupId, folderId, classNameId, title, description, content,
203                            defaultLocale, null, workflowEnabled, approved, serviceContext);
204            }
205    
206            public static JournalArticle addArticle(
207                            long groupId, long folderId, long classNameId, String title,
208                            String description, String content, Locale defaultLocale,
209                            Date expirationDate, boolean workflowEnabled, boolean approved,
210                            ServiceContext serviceContext)
211                    throws Exception {
212    
213                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
214    
215                    for (Locale locale : _locales) {
216                            titleMap.put(locale, title);
217                    }
218    
219                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
220    
221                    for (Locale locale : _locales) {
222                            descriptionMap.put(locale, description);
223                    }
224    
225                    Map<Locale, String> contentMap = new HashMap<Locale, String>();
226    
227                    for (Locale locale : _locales) {
228                            contentMap.put(locale, content);
229                    }
230    
231                    return addArticle(
232                            groupId, folderId, classNameId, titleMap, descriptionMap,
233                            contentMap, defaultLocale, expirationDate, workflowEnabled,
234                            approved, serviceContext);
235            }
236    
237            public static JournalArticle addArticle(
238                            long groupId, long folderId, String title, String content)
239                    throws Exception {
240    
241                    return addArticle(
242                            groupId, folderId, title, title, content,
243                            LocaleUtil.getSiteDefault(), false, false);
244            }
245    
246            public static JournalArticle addArticle(
247                            long groupId, long folderId, String title, String content,
248                            Locale defaultLocale, boolean workflowEnabled, boolean approved)
249                    throws Exception {
250    
251                    return addArticle(
252                            groupId, folderId, title, title, content, defaultLocale,
253                            workflowEnabled, approved);
254            }
255    
256            public static JournalArticle addArticle(
257                            long groupId, long folderId, String title, String description,
258                            String content, Locale defaultLocale, boolean workflowEnabled,
259                            boolean approved)
260                    throws Exception {
261    
262                    ServiceContext serviceContext =
263                            ServiceContextTestUtil.getServiceContext();
264    
265                    serviceContext.setCommand(Constants.ADD);
266                    serviceContext.setLayoutFullURL("http://localhost");
267    
268                    return addArticle(
269                            groupId, folderId, JournalArticleConstants.CLASSNAME_ID_DEFAULT,
270                            title, description, content, defaultLocale, workflowEnabled,
271                            approved, serviceContext);
272            }
273    
274            public static JournalArticle addArticle(
275                            long groupId, String title, String content)
276                    throws Exception {
277    
278                    return addArticle(
279                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, title,
280                            title, content, LocaleUtil.getSiteDefault(), false, false);
281            }
282    
283            public static JournalArticle addArticle(
284                            long groupId, String title, String content, Date expirationDate,
285                            ServiceContext serviceContext)
286                    throws Exception {
287    
288                    return addArticle(
289                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
290                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, title, title, content,
291                            LocaleUtil.getSiteDefault(), expirationDate, false, false,
292                            serviceContext);
293            }
294    
295            public static JournalArticle addArticle(
296                            long groupId, String title, String content, Locale defaultLocale)
297                    throws Exception {
298    
299                    return addArticle(
300                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, title,
301                            title, content, defaultLocale, false, false);
302            }
303    
304            public static JournalArticle addArticle(
305                            long groupId, String title, String content,
306                            ServiceContext serviceContext)
307                    throws Exception {
308    
309                    return addArticle(
310                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
311                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, title, title, content,
312                            LocaleUtil.getSiteDefault(), false, false, serviceContext);
313            }
314    
315            public static JournalArticle addArticleWithWorkflow(
316                            long groupId, boolean approved)
317                    throws Exception {
318    
319                    return addArticleWithWorkflow(
320                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, approved);
321            }
322    
323            public static JournalArticle addArticleWithWorkflow(
324                            long groupId, long folderId, boolean approved)
325                    throws Exception {
326    
327                    return addArticleWithWorkflow(
328                            groupId, folderId, "title", "content", approved);
329            }
330    
331            public static JournalArticle addArticleWithWorkflow(
332                            long groupId, long folderId, Map<Locale, String> titleMap,
333                            Map<Locale, String> descriptionMap, Map<Locale, String> contentMap,
334                            boolean workflowEnabled, boolean approved)
335                    throws Exception {
336    
337                    ServiceContext serviceContext =
338                            ServiceContextTestUtil.getServiceContext(groupId);
339    
340                    serviceContext.setCommand(Constants.ADD);
341                    serviceContext.setLayoutFullURL("http://localhost");
342    
343                    return addArticleWithWorkflow(
344                            groupId, folderId, titleMap, descriptionMap, contentMap,
345                            workflowEnabled, approved, serviceContext);
346            }
347    
348            public static JournalArticle addArticleWithWorkflow(
349                            long groupId, long folderId, Map<Locale, String> titleMap,
350                            Map<Locale, String> descriptionMap, Map<Locale, String> contentMap,
351                            boolean workflowEnabled, boolean approved,
352                            ServiceContext serviceContext)
353                    throws Exception {
354    
355                    return addArticle(
356                            groupId, folderId, JournalArticleConstants.CLASSNAME_ID_DEFAULT,
357                            titleMap, descriptionMap, contentMap, LocaleUtil.getSiteDefault(),
358                            workflowEnabled, approved, serviceContext);
359            }
360    
361            public static JournalArticle addArticleWithWorkflow(
362                            long groupId, long folderId, String title, String content,
363                            boolean approved)
364                    throws Exception {
365    
366                    ServiceContext serviceContext =
367                            ServiceContextTestUtil.getServiceContext(groupId);
368    
369                    serviceContext.setCommand(Constants.ADD);
370                    serviceContext.setLayoutFullURL("http://localhost");
371    
372                    return addArticleWithWorkflow(
373                            groupId, folderId, title, content, approved, serviceContext);
374            }
375    
376            public static JournalArticle addArticleWithWorkflow(
377                            long groupId, long folderId, String title, String content,
378                            boolean approved, ServiceContext serviceContext)
379                    throws Exception {
380    
381                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
382    
383                    for (Locale locale : _locales) {
384                            titleMap.put(locale, title);
385                    }
386    
387                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
388    
389                    for (Locale locale : _locales) {
390                            descriptionMap.put(locale, RandomTestUtil.randomString(50));
391                    }
392    
393                    Map<Locale, String> contentMap = new HashMap<Locale, String>();
394    
395                    for (Locale locale : _locales) {
396                            contentMap.put(locale, content);
397                    }
398    
399                    return addArticleWithWorkflow(
400                            groupId, folderId, titleMap, descriptionMap, contentMap, true,
401                            approved, serviceContext);
402            }
403    
404            public static JournalArticle addArticleWithWorkflow(
405                            long groupId, Map<Locale, String> titleMap,
406                            Map<Locale, String> descriptionMap, Map<Locale, String> contentMap,
407                            boolean approved)
408                    throws Exception {
409    
410                    return addArticleWithWorkflow(
411                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, titleMap,
412                            descriptionMap, contentMap, true, approved);
413            }
414    
415            public static JournalArticle addArticleWithXMLContent(
416                            long groupId, long folderId, long classNameId, String xml,
417                            String ddmStructureKey, String ddmTemplateKey)
418                    throws Exception {
419    
420                    return addArticleWithXMLContent(
421                            groupId, folderId, classNameId, xml, ddmStructureKey,
422                            ddmTemplateKey, LocaleUtil.getSiteDefault());
423            }
424    
425            public static JournalArticle addArticleWithXMLContent(
426                            long groupId, long folderId, long classNameId, String xml,
427                            String ddmStructureKey, String ddmTemplateKey, Locale defaultLocale)
428                    throws Exception {
429    
430                    ServiceContext serviceContext =
431                            ServiceContextTestUtil.getServiceContext(groupId);
432    
433                    return addArticleWithXMLContent(
434                            folderId, classNameId, xml, ddmStructureKey, ddmTemplateKey,
435                            defaultLocale, null, serviceContext);
436            }
437    
438            public static JournalArticle addArticleWithXMLContent(
439                            long folderId, long classNameId, String xml, String ddmStructureKey,
440                            String ddmTemplateKey, Locale defaultLocale,
441                            Map<String, byte[]> images, ServiceContext serviceContext)
442                    throws Exception {
443    
444                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
445    
446                    titleMap.put(defaultLocale, "Test Article");
447    
448                    return JournalArticleLocalServiceUtil.addArticle(
449                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
450                            folderId, classNameId, 0, StringPool.BLANK, true, 0, titleMap, null,
451                            xml, "general", ddmStructureKey, ddmTemplateKey, null, 1, 1, 1965,
452                            0, 0, 0, 0, 0, 0, 0, true, 0, 0, 0, 0, 0, true, true, false, null,
453                            null, images, null, serviceContext);
454            }
455    
456            public static JournalArticle addArticleWithXMLContent(
457                            long folderId, long classNameId, String xml, String ddmStructureKey,
458                            String ddmTemplateKey, Locale defaultLocale,
459                            ServiceContext serviceContext)
460                    throws Exception {
461    
462                    return addArticleWithXMLContent(
463                            folderId, classNameId, xml, ddmStructureKey, ddmTemplateKey,
464                            defaultLocale, null, serviceContext);
465            }
466    
467            public static JournalArticle addArticleWithXMLContent(
468                            long groupId, String xml, String ddmStructureKey,
469                            String ddmTemplateKey)
470                    throws Exception {
471    
472                    return addArticleWithXMLContent(
473                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
474                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml, ddmStructureKey,
475                            ddmTemplateKey, LocaleUtil.getSiteDefault());
476            }
477    
478            public static JournalArticle addArticleWithXMLContent(
479                            long parentFolderId, String xml, String ddmStructureKey,
480                            String ddmTemplateKey, Map<String, byte[]> images,
481                            ServiceContext serviceContext)
482                    throws Exception {
483    
484                    return addArticleWithXMLContent(
485                            parentFolderId, JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml,
486                            ddmStructureKey, ddmTemplateKey, LocaleUtil.getSiteDefault(),
487                            images, serviceContext);
488            }
489    
490            public static JournalArticle addArticleWithXMLContent(
491                            long parentFolderId, String xml, String ddmStructureKey,
492                            String ddmTemplateKey, ServiceContext serviceContext)
493                    throws Exception {
494    
495                    return addArticleWithXMLContent(
496                            serviceContext.getScopeGroupId(), parentFolderId,
497                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml, ddmStructureKey,
498                            ddmTemplateKey, LocaleUtil.getSiteDefault());
499            }
500    
501            public static JournalArticle addArticleWithXMLContent(
502                            String xml, String ddmStructureKey, String ddmTemplateKey)
503                    throws Exception {
504    
505                    return addArticleWithXMLContent(
506                            TestPropsValues.getGroupId(),
507                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
508                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml, ddmStructureKey,
509                            ddmTemplateKey, LocaleUtil.getSiteDefault());
510            }
511    
512            public static JournalArticle addArticleWithXMLContent(
513                            String xml, String ddmStructureKey, String ddmTemplateKey,
514                            Locale defaultLocale)
515                    throws Exception {
516    
517                    return addArticleWithXMLContent(
518                            TestPropsValues.getGroupId(),
519                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
520                            JournalArticleConstants.CLASSNAME_ID_DEFAULT, xml, ddmStructureKey,
521                            ddmTemplateKey, defaultLocale);
522            }
523    
524            public static JournalArticle addArticleWithXMLContent(
525                            String xml, String ddmStructureKey, String ddmTemplateKey,
526                            ServiceContext serviceContext)
527                    throws Exception {
528    
529                    return addArticleWithXMLContent(
530                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, xml,
531                            ddmStructureKey, ddmTemplateKey, serviceContext);
532            }
533    
534            public static Element addDynamicElementElement(
535                    Element element, String type, String name) {
536    
537                    Element dynamicElementElement = element.addElement("dynamic-element");
538    
539                    dynamicElementElement.addAttribute("name", name);
540                    dynamicElementElement.addAttribute("type", type);
541    
542                    return dynamicElementElement;
543            }
544    
545            public static JournalFeed addFeed(
546                            long groupId, long plid, String name, String ddmStructureKey,
547                            String ddmTemplateKey, String rendererTemplateKey)
548                    throws Exception {
549    
550                    long userId = TestPropsValues.getUserId();
551                    String feedId = StringPool.BLANK;
552                    boolean autoFeedId = true;
553                    String description = StringPool.BLANK;
554                    String type = StringPool.BLANK;
555                    int delta = 0;
556                    String orderByCol = "modified-date";
557                    String orderByType = "asc";
558                    String friendlyURL = _getFeedFriendlyURL(groupId, plid);
559                    String targetPortletId = StringPool.BLANK;
560                    String contentField = JournalFeedConstants.WEB_CONTENT_DESCRIPTION;
561                    String feedFormat = RSSUtil.getFeedTypeFormat(
562                            RSSUtil.FEED_TYPE_DEFAULT);
563                    double feedVersion = RSSUtil.getFeedTypeVersion(
564                            RSSUtil.FEED_TYPE_DEFAULT);
565    
566                    ServiceContext serviceContext =
567                            ServiceContextTestUtil.getServiceContext(groupId);
568    
569                    return JournalFeedLocalServiceUtil.addFeed(
570                            userId, groupId, feedId, autoFeedId, name, description, type,
571                            ddmStructureKey, ddmTemplateKey, rendererTemplateKey, delta,
572                            orderByCol, orderByType, friendlyURL, targetPortletId, contentField,
573                            feedFormat, feedVersion, serviceContext);
574            }
575    
576            public static JournalFolder addFolder(
577                            long groupId, long parentFolderId, String name)
578                    throws Exception {
579    
580                    ServiceContext serviceContext =
581                            ServiceContextTestUtil.getServiceContext(groupId);
582    
583                    return addFolder(parentFolderId, name, serviceContext);
584            }
585    
586            public static JournalFolder addFolder(long groupId, String name)
587                    throws Exception {
588    
589                    return addFolder(
590                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, name);
591            }
592    
593            public static JournalFolder addFolder(
594                            long parentFolderId, String name, ServiceContext serviceContext)
595                    throws Exception {
596    
597                    JournalFolder folder = JournalFolderLocalServiceUtil.fetchFolder(
598                            serviceContext.getScopeGroupId(), parentFolderId, name);
599    
600                    if (folder != null) {
601                            return folder;
602                    }
603    
604                    return JournalFolderLocalServiceUtil.addFolder(
605                            TestPropsValues.getUserId(), serviceContext.getScopeGroupId(),
606                            parentFolderId, name, "This is a test folder.", serviceContext);
607            }
608    
609            public static Element addMetadataElement(
610                    Element element, String locale, String label) {
611    
612                    Element metadataElement = element.addElement("meta-data");
613    
614                    metadataElement.addAttribute("locale", locale);
615    
616                    Element entryElement = metadataElement.addElement("entry");
617    
618                    entryElement.addAttribute("name", "label");
619    
620                    entryElement.addCDATA(label);
621    
622                    return entryElement;
623            }
624    
625            public static void expireArticle(long groupId, JournalArticle article)
626                    throws PortalException {
627    
628                    JournalArticleLocalServiceUtil.expireArticle(
629                            article.getUserId(), article.getGroupId(), article.getArticleId(),
630                            null, ServiceContextTestUtil.getServiceContext(groupId));
631            }
632    
633            public static JournalArticle expireArticle(
634                            long groupId, JournalArticle article, double version)
635                    throws PortalException {
636    
637                    return JournalArticleLocalServiceUtil.expireArticle(
638                            article.getUserId(), article.getGroupId(), article.getArticleId(),
639                            version, null, ServiceContextTestUtil.getServiceContext(groupId));
640            }
641    
642            public static String getSampleTemplateXSL() {
643                    return "$name.getData()";
644            }
645    
646            public static int getSearchArticlesCount(long companyId, long groupId)
647                    throws Exception {
648    
649                    Indexer indexer = IndexerRegistryUtil.getIndexer(JournalArticle.class);
650    
651                    SearchContext searchContext = new SearchContext();
652    
653                    searchContext.setCompanyId(companyId);
654                    searchContext.setGroupIds(new long[] {groupId});
655                    searchContext.setKeywords(StringPool.BLANK);
656    
657                    QueryConfig queryConfig = new QueryConfig();
658    
659                    searchContext.setQueryConfig(queryConfig);
660    
661                    Hits results = indexer.search(searchContext);
662    
663                    return results.getLength();
664            }
665    
666            public static Map<String, Map<String, String>> getXsdMap(String xsd)
667                    throws Exception {
668    
669                    Map<String, Map<String, String>> map =
670                            new HashMap<String, Map<String, String>>();
671    
672                    Document document = SAXReaderUtil.read(xsd);
673    
674                    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element");
675    
676                    List<Node> nodes = xPathSelector.selectNodes(document);
677    
678                    for (Node node : nodes) {
679                            Element dynamicElementElement = (Element)node;
680    
681                            String type = dynamicElementElement.attributeValue("type");
682    
683                            if (Validator.equals(type, "selection_break")) {
684                                    continue;
685                            }
686    
687                            String name = dynamicElementElement.attributeValue("name");
688    
689                            map.put(name, _getMap(dynamicElementElement));
690                    }
691    
692                    return map;
693            }
694    
695            public static JournalArticle updateArticle(JournalArticle article)
696                    throws Exception {
697    
698                    return updateArticle(article, RandomTestUtil.randomString());
699            }
700    
701            public static JournalArticle updateArticle(
702                            JournalArticle article, String title)
703                    throws Exception {
704    
705                    return updateArticle(
706                            article, title, article.getContent(), false, false,
707                            ServiceContextTestUtil.getServiceContext());
708            }
709    
710            public static JournalArticle updateArticle(
711                            JournalArticle article, String title, String content)
712                    throws Exception {
713    
714                    return updateArticle(
715                            article, title, content, false, false,
716                            ServiceContextTestUtil.getServiceContext());
717            }
718    
719            public static JournalArticle updateArticle(
720                            JournalArticle article, String title, String content,
721                            boolean workflowEnabled, boolean approved,
722                            ServiceContext serviceContext)
723                    throws Exception {
724    
725                    if (workflowEnabled) {
726                            serviceContext = (ServiceContext)serviceContext.clone();
727    
728                            if (approved) {
729                                    serviceContext.setWorkflowAction(
730                                            WorkflowConstants.ACTION_PUBLISH);
731                            }
732                            else {
733                                    serviceContext.setWorkflowAction(
734                                            WorkflowConstants.ACTION_SAVE_DRAFT);
735                            }
736                    }
737    
738                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
739    
740                    for (Locale locale : _locales) {
741                            titleMap.put(locale, title);
742                    }
743    
744                    Date displayDate = article.getDisplayDate();
745    
746                    int displayDateMonth = 0;
747                    int displayDateDay = 0;
748                    int displayDateYear = 0;
749                    int displayDateHour = 0;
750                    int displayDateMinute = 0;
751    
752                    if (displayDate != null) {
753                            Calendar displayCal = CalendarFactoryUtil.getCalendar(
754                                    TestPropsValues.getUser().getTimeZone());
755    
756                            displayCal.setTime(displayDate);
757    
758                            displayDateMonth = displayCal.get(Calendar.MONTH);
759                            displayDateDay = displayCal.get(Calendar.DATE);
760                            displayDateYear = displayCal.get(Calendar.YEAR);
761                            displayDateHour = displayCal.get(Calendar.HOUR_OF_DAY);
762                            displayDateMinute = displayCal.get(Calendar.MINUTE);
763                    }
764    
765                    serviceContext.setCommand(Constants.UPDATE);
766                    serviceContext.setLayoutFullURL("http://localhost");
767    
768                    return JournalArticleLocalServiceUtil.updateArticle(
769                            article.getUserId(), article.getGroupId(), article.getFolderId(),
770                            article.getArticleId(), article.getVersion(), titleMap,
771                            article.getDescriptionMap(), content, article.getType(),
772                            article.getStructureId(), article.getTemplateId(),
773                            article.getLayoutUuid(), displayDateMonth, displayDateDay,
774                            displayDateYear, displayDateHour, displayDateMinute, 0, 0, 0, 0, 0,
775                            true, 0, 0, 0, 0, 0, true, article.getIndexable(),
776                            article.isSmallImage(), article.getSmallImageURL(), null, null,
777                            null, serviceContext);
778            }
779    
780            public static JournalArticle updateArticleWithWorkflow(
781                            JournalArticle article, boolean approved)
782                    throws Exception {
783    
784                    return updateArticle(
785                            article, RandomTestUtil.randomString(), article.getContent(), false,
786                            approved, ServiceContextTestUtil.getServiceContext());
787            }
788    
789            private static String _getFeedFriendlyURL(long groupId, long plid)
790                    throws Exception {
791    
792                    String friendlyURL = StringPool.BLANK;
793    
794                    Group group = GroupLocalServiceUtil.getGroup(groupId);
795                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
796    
797                    if (layout.isPrivateLayout()) {
798                            if (group.isUser()) {
799                                    friendlyURL = friendlyURL.concat(
800                                            PortalUtil.getPathFriendlyURLPrivateUser());
801                            }
802                            else {
803                                    friendlyURL = friendlyURL.concat(
804                                            PortalUtil.getPathFriendlyURLPrivateGroup());
805                            }
806                    }
807                    else {
808                            friendlyURL = friendlyURL.concat(
809                                    PortalUtil.getPathFriendlyURLPublic());
810                    }
811    
812                    friendlyURL = friendlyURL.concat(group.getFriendlyURL());
813                    friendlyURL = friendlyURL.concat(layout.getFriendlyURL());
814    
815                    return friendlyURL;
816            }
817    
818            private static Map<String, String> _getMap(Element dynamicElementElement) {
819                    Map<String, String> map = new HashMap<String, String>();
820    
821                    Element parentElement = dynamicElementElement.getParent();
822    
823                    String parentType = parentElement.attributeValue("type");
824    
825                    // Attributes
826    
827                    for (Attribute attribute : dynamicElementElement.attributes()) {
828    
829                            // Option element should not contain index type atribute
830    
831                            if ((Validator.equals(parentType, "list") ||
832                                     Validator.equals(parentType, "multi-list")) &&
833                                    Validator.equals(attribute.getName(), "index-type")) {
834    
835                                    continue;
836                            }
837    
838                            map.put(attribute.getName(), attribute.getValue());
839                    }
840    
841                    // Metadata
842    
843                    Element metadadataElement = dynamicElementElement.element("meta-data");
844    
845                    if (metadadataElement == null) {
846                            return map;
847                    }
848    
849                    List<Element> entryElements = metadadataElement.elements("entry");
850    
851                    for (Element entryElement : entryElements) {
852                            map.put(
853                                    entryElement.attributeValue("name"), entryElement.getText());
854                    }
855    
856                    return map;
857            }
858    
859            private static final Locale[] _locales = {
860                    LocaleUtil.US, LocaleUtil.GERMANY, LocaleUtil.SPAIN
861            };
862    
863    }