001    /**
002     * Copyright (c) 2000-2012 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.portal.lar;
016    
017    import com.liferay.portal.LayoutImportException;
018    import com.liferay.portal.NoSuchPortletPreferencesException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.language.LanguageUtil;
022    import com.liferay.portal.kernel.lar.ImportExportThreadLocal;
023    import com.liferay.portal.kernel.lar.PortletDataContext;
024    import com.liferay.portal.kernel.lar.PortletDataHandler;
025    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.util.CharPool;
029    import com.liferay.portal.kernel.util.FileUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.MapUtil;
032    import com.liferay.portal.kernel.util.ReleaseInfo;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Time;
037    import com.liferay.portal.kernel.util.UnicodeProperties;
038    import com.liferay.portal.kernel.util.Validator;
039    import com.liferay.portal.kernel.xml.Document;
040    import com.liferay.portal.kernel.xml.Element;
041    import com.liferay.portal.kernel.xml.Node;
042    import com.liferay.portal.kernel.xml.SAXReaderUtil;
043    import com.liferay.portal.kernel.zip.ZipWriter;
044    import com.liferay.portal.kernel.zip.ZipWriterFactoryUtil;
045    import com.liferay.portal.model.Company;
046    import com.liferay.portal.model.Group;
047    import com.liferay.portal.model.Layout;
048    import com.liferay.portal.model.LayoutConstants;
049    import com.liferay.portal.model.LayoutTypePortlet;
050    import com.liferay.portal.model.Lock;
051    import com.liferay.portal.model.Portlet;
052    import com.liferay.portal.model.PortletConstants;
053    import com.liferay.portal.model.PortletItem;
054    import com.liferay.portal.model.PortletPreferences;
055    import com.liferay.portal.model.User;
056    import com.liferay.portal.service.CompanyLocalServiceUtil;
057    import com.liferay.portal.service.GroupLocalServiceUtil;
058    import com.liferay.portal.service.LayoutLocalServiceUtil;
059    import com.liferay.portal.service.PortletItemLocalServiceUtil;
060    import com.liferay.portal.service.PortletLocalServiceUtil;
061    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
062    import com.liferay.portal.service.UserLocalServiceUtil;
063    import com.liferay.portal.util.PortalUtil;
064    import com.liferay.portal.util.PortletKeys;
065    import com.liferay.portlet.PortletPreferencesFactoryUtil;
066    import com.liferay.portlet.asset.model.AssetCategory;
067    import com.liferay.portlet.asset.model.AssetCategoryConstants;
068    import com.liferay.portlet.asset.model.AssetCategoryProperty;
069    import com.liferay.portlet.asset.model.AssetTag;
070    import com.liferay.portlet.asset.model.AssetTagProperty;
071    import com.liferay.portlet.asset.model.AssetVocabulary;
072    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
073    import com.liferay.portlet.asset.service.AssetCategoryPropertyLocalServiceUtil;
074    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
075    import com.liferay.portlet.asset.service.AssetTagPropertyLocalServiceUtil;
076    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
077    import com.liferay.portlet.asset.service.persistence.AssetCategoryUtil;
078    import com.liferay.portlet.asset.service.persistence.AssetVocabularyUtil;
079    import com.liferay.portlet.expando.model.ExpandoColumn;
080    import com.liferay.portlet.journal.model.JournalStructure;
081    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
082    import com.liferay.portlet.messageboards.model.MBMessage;
083    import com.liferay.portlet.ratings.model.RatingsEntry;
084    import com.liferay.util.xml.DocUtil;
085    
086    import java.io.File;
087    import java.io.IOException;
088    
089    import java.util.Date;
090    import java.util.Enumeration;
091    import java.util.HashSet;
092    import java.util.List;
093    import java.util.Map;
094    
095    import org.apache.commons.lang.time.StopWatch;
096    
097    /**
098     * @author Brian Wing Shun Chan
099     * @author Joel Kozikowski
100     * @author Charles May
101     * @author Raymond Augé
102     * @author Jorge Ferrer
103     * @author Bruno Farache
104     * @author Zsigmond Rab
105     * @author Douglas Wong
106     * @author Mate Thurzo
107     */
108    public class PortletExporter {
109    
110            @Deprecated
111            public byte[] exportPortletInfo(
112                            long plid, long groupId, String portletId,
113                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
114                    throws Exception {
115    
116                    File file = exportPortletInfoAsFile(
117                            plid, groupId, portletId, parameterMap, startDate, endDate);
118    
119                    try {
120                            return FileUtil.getBytes(file);
121                    }
122                    catch (IOException ioe) {
123                            throw new SystemException(ioe);
124                    }
125                    finally {
126                            file.delete();
127                    }
128            }
129    
130            public File exportPortletInfoAsFile(
131                            long plid, long groupId, String portletId,
132                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
133                    throws Exception {
134    
135                    try {
136                            ImportExportThreadLocal.setPortletExportInProcess(true);
137    
138                            return doExportPortletInfoAsFile(
139                                    plid, groupId, portletId, parameterMap, startDate, endDate);
140                    }
141                    finally {
142                            ImportExportThreadLocal.setPortletExportInProcess(false);
143                    }
144            }
145    
146            protected File doExportPortletInfoAsFile(
147                            long plid, long groupId, String portletId,
148                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
149                    throws Exception {
150    
151                    boolean exportCategories = MapUtil.getBoolean(
152                            parameterMap, PortletDataHandlerKeys.CATEGORIES);
153                    boolean exportPermissions = MapUtil.getBoolean(
154                            parameterMap, PortletDataHandlerKeys.PERMISSIONS);
155                    boolean exportPortletArchivedSetups = MapUtil.getBoolean(
156                            parameterMap, PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS);
157    
158                    boolean exportPortletData = false;
159    
160                    if (parameterMap.containsKey(
161                                    PortletDataHandlerKeys.PORTLET_DATA + "_" +
162                                            PortletConstants.getRootPortletId(portletId))) {
163    
164                            exportPortletData = MapUtil.getBoolean(
165                                    parameterMap,
166                                    PortletDataHandlerKeys.PORTLET_DATA + "_" +
167                                            PortletConstants.getRootPortletId(portletId));
168                    }
169                    else {
170                            exportPortletData = MapUtil.getBoolean(
171                                    parameterMap, PortletDataHandlerKeys.PORTLET_DATA);
172                    }
173    
174                    boolean exportPortletDataAll = MapUtil.getBoolean(
175                            parameterMap, PortletDataHandlerKeys.PORTLET_DATA_ALL);
176                    boolean exportPortletSetup = MapUtil.getBoolean(
177                            parameterMap, PortletDataHandlerKeys.PORTLET_SETUP);
178                    boolean exportPortletUserPreferences = MapUtil.getBoolean(
179                            parameterMap, PortletDataHandlerKeys.PORTLET_USER_PREFERENCES);
180    
181                    if (_log.isDebugEnabled()) {
182                            _log.debug("Export categories " + exportCategories);
183                            _log.debug("Export permissions " + exportPermissions);
184                            _log.debug(
185                                    "Export portlet archived setups " +
186                                            exportPortletArchivedSetups);
187                            _log.debug("Export portlet data " + exportPortletData);
188                            _log.debug("Export all portlet data " + exportPortletDataAll);
189                            _log.debug("Export portlet setup " + exportPortletSetup);
190                            _log.debug(
191                                    "Export portlet user preferences " +
192                                            exportPortletUserPreferences);
193                    }
194    
195                    if (exportPortletDataAll) {
196                            exportPortletData = true;
197                    }
198    
199                    StopWatch stopWatch = null;
200    
201                    if (_log.isInfoEnabled()) {
202                            stopWatch = new StopWatch();
203    
204                            stopWatch.start();
205                    }
206    
207                    LayoutCache layoutCache = new LayoutCache();
208    
209                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
210    
211                    if (!layout.isTypeControlPanel() && !layout.isTypePanel() &&
212                            !layout.isTypePortlet()) {
213    
214                            throw new LayoutImportException(
215                                    "Layout type " + layout.getType() + " is not valid");
216                    }
217    
218                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
219                            layout.getCompanyId());
220    
221                    ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();
222    
223                    long scopeGroupId = groupId;
224    
225                    javax.portlet.PortletPreferences jxPreferences =
226                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
227                                    layout, portletId);
228    
229                    String scopeType = GetterUtil.getString(
230                            jxPreferences.getValue("lfrScopeType", null));
231                    String scopeLayoutUuid = GetterUtil.getString(
232                            jxPreferences.getValue("lfrScopeLayoutUuid", null));
233    
234                    if (Validator.isNotNull(scopeType)) {
235                            Group scopeGroup = null;
236    
237                            if (scopeType.equals("company")) {
238                                    scopeGroup = GroupLocalServiceUtil.getCompanyGroup(
239                                            layout.getCompanyId());
240                            }
241                            else if (Validator.isNotNull(scopeLayoutUuid)) {
242                                    scopeGroup = layout.getScopeGroup();
243                            }
244    
245                            if (scopeGroup != null) {
246                                    scopeGroupId = scopeGroup.getGroupId();
247                            }
248                    }
249    
250                    PortletDataContext portletDataContext = new PortletDataContextImpl(
251                            layout.getCompanyId(), scopeGroupId, parameterMap,
252                            new HashSet<String>(), startDate, endDate, zipWriter);
253    
254                    portletDataContext.setPortetDataContextListener(
255                            new PortletDataContextListenerImpl(portletDataContext));
256    
257                    portletDataContext.setPlid(plid);
258                    portletDataContext.setOldPlid(plid);
259                    portletDataContext.setScopeType(scopeType);
260                    portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
261    
262                    Document document = SAXReaderUtil.createDocument();
263    
264                    Element rootElement = document.addElement("root");
265    
266                    Element headerElement = rootElement.addElement("header");
267    
268                    headerElement.addAttribute(
269                            "available-locales",
270                            StringUtil.merge(LanguageUtil.getAvailableLocales()));
271                    headerElement.addAttribute(
272                            "build-number", String.valueOf(ReleaseInfo.getBuildNumber()));
273                    headerElement.addAttribute("export-date", Time.getRFC822());
274    
275                    if (portletDataContext.hasDateRange()) {
276                            headerElement.addAttribute(
277                                    "start-date",
278                                    String.valueOf(portletDataContext.getStartDate()));
279                            headerElement.addAttribute(
280                                    "end-date", String.valueOf(portletDataContext.getEndDate()));
281                    }
282    
283                    headerElement.addAttribute("type", "portlet");
284                    headerElement.addAttribute("group-id", String.valueOf(scopeGroupId));
285                    headerElement.addAttribute(
286                            "private-layout", String.valueOf(layout.isPrivateLayout()));
287                    headerElement.addAttribute(
288                            "root-portlet-id", PortletConstants.getRootPortletId(portletId));
289    
290                    exportPortlet(
291                            portletDataContext, layoutCache, portletId, layout, rootElement,
292                            defaultUserId, exportPermissions, exportPortletArchivedSetups,
293                            exportPortletData, exportPortletSetup,
294                            exportPortletUserPreferences);
295    
296                    if (exportCategories) {
297                            exportAssetCategories(portletDataContext);
298                    }
299    
300                    exportAssetLinks(portletDataContext);
301                    exportAssetTags(portletDataContext);
302                    exportComments(portletDataContext);
303                    exportExpandoTables(portletDataContext);
304                    exportLocks(portletDataContext);
305    
306                    if (exportPermissions) {
307                            _permissionExporter.exportPortletDataPermissions(
308                                    portletDataContext);
309                    }
310    
311                    exportRatingsEntries(portletDataContext, rootElement);
312    
313                    if (_log.isInfoEnabled()) {
314                            _log.info("Exporting portlet took " + stopWatch.getTime() + " ms");
315                    }
316    
317                    try {
318                            portletDataContext.addZipEntry(
319                                    "/manifest.xml", document.formattedString());
320                    }
321                    catch (IOException ioe) {
322                            throw new SystemException(ioe);
323                    }
324    
325                    return zipWriter.getFile();
326            }
327    
328            protected void exportAssetCategories(PortletDataContext portletDataContext)
329                    throws Exception {
330    
331                    Document document = SAXReaderUtil.createDocument();
332    
333                    Element rootElement = document.addElement("categories-hierarchy");
334    
335                    exportAssetCategories(portletDataContext, rootElement);
336    
337                    portletDataContext.addZipEntry(
338                            portletDataContext.getRootPath() + "/categories-hierarchy.xml",
339                            document.formattedString());
340            }
341    
342            protected void exportAssetCategories(
343                            PortletDataContext portletDataContext, Element rootElement)
344                    throws Exception {
345    
346                    Element assetsElement = rootElement.element("assets");
347    
348                    if (assetsElement == null) {
349                            assetsElement = rootElement.addElement("assets");
350                    }
351    
352                    Element assetCategoriesElement = rootElement.element("categories");
353    
354                    if (assetCategoriesElement == null) {
355                            assetCategoriesElement = rootElement.addElement("categories");
356                    }
357    
358                    Element assetVocabulariesElement = rootElement.element("vocabularies");
359    
360                    if (assetVocabulariesElement == null) {
361                            assetVocabulariesElement = rootElement.addElement("vocabularies");
362                    }
363    
364                    Map<String, String[]> assetCategoryUuidsMap =
365                            portletDataContext.getAssetCategoryUuidsMap();
366    
367                    for (Map.Entry<String, String[]> entry :
368                                    assetCategoryUuidsMap.entrySet()) {
369    
370                            String[] assetCategoryEntryParts = StringUtil.split(
371                                    entry.getKey(), CharPool.POUND);
372    
373                            String className = assetCategoryEntryParts[0];
374                            String classPK = assetCategoryEntryParts[1];
375    
376                            Element assetElement = assetsElement.addElement("asset");
377    
378                            assetElement.addAttribute("class-name", className);
379                            assetElement.addAttribute("class-pk", classPK);
380                            assetElement.addAttribute(
381                                    "category-uuids", StringUtil.merge(entry.getValue()));
382    
383                            List<AssetCategory> assetCategories =
384                                    AssetCategoryLocalServiceUtil.getCategories(
385                                            className, GetterUtil.getLong(classPK));
386    
387                            for (AssetCategory assestCategory : assetCategories) {
388                                    exportAssetCategory(
389                                            portletDataContext, assetVocabulariesElement,
390                                            assetCategoriesElement, assestCategory);
391                            }
392                    }
393            }
394    
395            protected void exportAssetCategory(
396                            PortletDataContext portletDataContext,
397                            Element assetVocabulariesElement, Element assetCategoriesElement,
398                            AssetCategory assetCategory)
399                    throws Exception {
400    
401                    exportAssetVocabulary(
402                            portletDataContext, assetVocabulariesElement,
403                            assetCategory.getVocabularyId());
404    
405                    if (assetCategory.getParentCategoryId() !=
406                                    AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
407    
408                            exportAssetCategory(
409                                    portletDataContext, assetVocabulariesElement,
410                                    assetCategoriesElement, assetCategory.getParentCategoryId());
411                    }
412    
413                    String path = getAssetCategoryPath(
414                            portletDataContext, assetCategory.getCategoryId());
415    
416                    if (!portletDataContext.isPathNotProcessed(path)) {
417                            return;
418                    }
419    
420                    Element assetCategoryElement = assetCategoriesElement.addElement(
421                            "category");
422    
423                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
424                            assetCategory.getCompanyId());
425    
426                    if (assetCategory.getGroupId() == companyGroup.getGroupId()) {
427                            assetCategoryElement.addAttribute("global", "true");
428                    }
429    
430                    assetCategoryElement.addAttribute("path", path);
431    
432                    assetCategory.setUserUuid(assetCategory.getUserUuid());
433    
434                    portletDataContext.addZipEntry(path, assetCategory);
435    
436                    List<AssetCategoryProperty> assetCategoryProperties =
437                            AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(
438                                    assetCategory.getCategoryId());
439    
440                    for (AssetCategoryProperty assetCategoryProperty :
441                                    assetCategoryProperties) {
442    
443                            Element propertyElement = assetCategoryElement.addElement(
444                                    "property");
445    
446                            propertyElement.addAttribute(
447                                    "userUuid", assetCategoryProperty.getUserUuid());
448                            propertyElement.addAttribute("key", assetCategoryProperty.getKey());
449                            propertyElement.addAttribute(
450                                    "value", assetCategoryProperty.getValue());
451                    }
452    
453                    portletDataContext.addPermissions(
454                            AssetCategory.class, assetCategory.getCategoryId());
455            }
456    
457            protected void exportAssetCategory(
458                            PortletDataContext portletDataContext,
459                            Element assetVocabulariesElement, Element assetCategoriesElement,
460                            long assetCategoryId)
461                    throws Exception {
462    
463                    AssetCategory assetCategory = AssetCategoryUtil.fetchByPrimaryKey(
464                            assetCategoryId);
465    
466                    if (assetCategory != null) {
467                            exportAssetCategory(
468                                    portletDataContext, assetVocabulariesElement,
469                                    assetCategoriesElement, assetCategory);
470                    }
471            }
472    
473            protected void exportAssetLinks(PortletDataContext portletDataContext)
474                    throws Exception {
475    
476                    Document document = SAXReaderUtil.createDocument();
477    
478                    Element rootElement = document.addElement("links");
479    
480                    Map<String, String[]> assetLinkUuidsMap =
481                            portletDataContext.getAssetLinkUuidsMap();
482    
483                    for (Map.Entry<String, String[]> entry : assetLinkUuidsMap.entrySet()) {
484                            String[] assetLinkNameParts = StringUtil.split(
485                                    entry.getKey(), CharPool.POUND);
486                            String[] targetAssetEntryUuids = entry.getValue();
487    
488                            String sourceAssetEntryUuid = assetLinkNameParts[0];
489                            String assetLinkType = assetLinkNameParts[1];
490    
491                            Element assetElement = rootElement.addElement("asset-link");
492    
493                            assetElement.addAttribute("source-uuid", sourceAssetEntryUuid);
494                            assetElement.addAttribute(
495                                    "target-uuids", StringUtil.merge(targetAssetEntryUuids));
496                            assetElement.addAttribute("type", assetLinkType);
497                    }
498    
499                    portletDataContext.addZipEntry(
500                            portletDataContext.getRootPath() + "/links.xml",
501                            document.formattedString());
502            }
503    
504            protected void exportAssetTag(
505                            PortletDataContext portletDataContext, AssetTag assetTag,
506                            Element assetTagsElement)
507                    throws PortalException, SystemException {
508    
509                    String path = getAssetTagPath(portletDataContext, assetTag.getTagId());
510    
511                    if (!portletDataContext.isPathNotProcessed(path)) {
512                            return;
513                    }
514    
515                    Element assetTagElement = assetTagsElement.addElement("tag");
516    
517                    assetTagElement.addAttribute("path", path);
518    
519                    assetTag.setUserUuid(assetTag.getUserUuid());
520    
521                    portletDataContext.addZipEntry(path, assetTag);
522    
523                    List<AssetTagProperty> assetTagProperties =
524                            AssetTagPropertyLocalServiceUtil.getTagProperties(
525                                    assetTag.getTagId());
526    
527                    for (AssetTagProperty assetTagProperty : assetTagProperties) {
528                            Element propertyElement = assetTagElement.addElement("property");
529    
530                            propertyElement.addAttribute("key", assetTagProperty.getKey());
531                            propertyElement.addAttribute("value", assetTagProperty.getValue());
532                    }
533    
534                    portletDataContext.addPermissions(AssetTag.class, assetTag.getTagId());
535            }
536    
537            protected void exportAssetTags(PortletDataContext portletDataContext)
538                    throws Exception {
539    
540                    Document document = SAXReaderUtil.createDocument();
541    
542                    Element rootElement = document.addElement("tags");
543    
544                    Map<String, String[]> assetTagNamesMap =
545                            portletDataContext.getAssetTagNamesMap();
546    
547                    if (assetTagNamesMap.isEmpty()) {
548                            return;
549                    }
550    
551                    for (Map.Entry<String, String[]> entry : assetTagNamesMap.entrySet()) {
552                            String[] assetTagNameParts = StringUtil.split(
553                                    entry.getKey(), CharPool.POUND);
554    
555                            String className = assetTagNameParts[0];
556                            String classPK = assetTagNameParts[1];
557    
558                            Element assetElement = rootElement.addElement("asset");
559    
560                            assetElement.addAttribute("class-name", className);
561                            assetElement.addAttribute("class-pk", classPK);
562                            assetElement.addAttribute(
563                                    "tags", StringUtil.merge(entry.getValue()));
564    
565                            List<AssetTag> assetTags = AssetTagLocalServiceUtil.getTags(
566                                    className, GetterUtil.getLong(classPK));
567    
568                            for (AssetTag assetTag : assetTags) {
569                                    exportAssetTag(portletDataContext, assetTag, rootElement);
570                            }
571                    }
572    
573                    portletDataContext.addZipEntry(
574                            portletDataContext.getRootPath() + "/tags.xml",
575                            document.formattedString());
576            }
577    
578            protected void exportAssetVocabulary(
579                            PortletDataContext portletDataContext,
580                            Element assetVocabulariesElement, AssetVocabulary assetVocabulary)
581                    throws Exception {
582    
583                    String path = getAssetVocabulariesPath(
584                            portletDataContext, assetVocabulary.getVocabularyId());
585    
586                    if (!portletDataContext.isPathNotProcessed(path)) {
587                            return;
588                    }
589    
590                    Element assetVocabularyElement = assetVocabulariesElement.addElement(
591                            "vocabulary");
592    
593                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
594                            assetVocabulary.getCompanyId());
595    
596                    if (assetVocabulary.getGroupId() == companyGroup.getGroupId()) {
597                            assetVocabularyElement.addAttribute("global", "true");
598                    }
599    
600                    assetVocabularyElement.addAttribute("path", path);
601    
602                    assetVocabulary.setUserUuid(assetVocabulary.getUserUuid());
603    
604                    portletDataContext.addZipEntry(path, assetVocabulary);
605    
606                    portletDataContext.addPermissions(
607                            AssetVocabulary.class, assetVocabulary.getVocabularyId());
608            }
609    
610            protected void exportAssetVocabulary(
611                            PortletDataContext portletDataContext,
612                            Element assetVocabulariesElement, long assetVocabularyId)
613                    throws Exception {
614    
615                    AssetVocabulary assetVocabulary = AssetVocabularyUtil.findByPrimaryKey(
616                            assetVocabularyId);
617    
618                    exportAssetVocabulary(
619                            portletDataContext, assetVocabulariesElement, assetVocabulary);
620            }
621    
622            protected void exportComments(PortletDataContext portletDataContext)
623                    throws Exception {
624    
625                    Document document = SAXReaderUtil.createDocument();
626    
627                    Element rootElement = document.addElement("comments");
628    
629                    Map<String, List<MBMessage>> commentsMap =
630                            portletDataContext.getComments();
631    
632                    for (Map.Entry<String, List<MBMessage>> entry :
633                                    commentsMap.entrySet()) {
634    
635                            String[] commentParts = StringUtil.split(
636                                    entry.getKey(), CharPool.POUND);
637    
638                            String className = commentParts[0];
639                            String classPK = commentParts[1];
640    
641                            String commentsPath = getCommentsPath(
642                                    portletDataContext, className, classPK);
643    
644                            Element assetElement = rootElement.addElement("asset");
645    
646                            assetElement.addAttribute("path", commentsPath);
647                            assetElement.addAttribute("class-name", className);
648                            assetElement.addAttribute("class-pk", classPK);
649    
650                            List<MBMessage> mbMessages = entry.getValue();
651    
652                            for (MBMessage mbMessage : mbMessages) {
653                                    String commentPath = getCommentPath(
654                                            portletDataContext, className, classPK, mbMessage);
655    
656                                    if (portletDataContext.isPathNotProcessed(commentPath)) {
657                                            portletDataContext.addZipEntry(commentPath, mbMessage);
658                                    }
659                            }
660                    }
661    
662                    portletDataContext.addZipEntry(
663                            portletDataContext.getRootPath() + "/comments.xml",
664                            document.formattedString());
665            }
666    
667            protected void exportExpandoTables(PortletDataContext portletDataContext)
668                    throws Exception {
669    
670                    Document document = SAXReaderUtil.createDocument();
671    
672                    Element rootElement = document.addElement("expando-tables");
673    
674                    Map<String, List<ExpandoColumn>> expandoColumnsMap =
675                            portletDataContext.getExpandoColumns();
676    
677                    for (Map.Entry<String, List<ExpandoColumn>> entry :
678                                    expandoColumnsMap.entrySet()) {
679    
680                            String className = entry.getKey();
681    
682                            Element expandoTableElement = rootElement.addElement(
683                                    "expando-table");
684    
685                            expandoTableElement.addAttribute("class-name", className);
686    
687                            List<ExpandoColumn> expandoColumns = entry.getValue();
688    
689                            for (ExpandoColumn expandoColumn : expandoColumns) {
690                                    Element expandoColumnElement = expandoTableElement.addElement(
691                                            "expando-column");
692    
693                                    expandoColumnElement.addAttribute(
694                                            "column-id", String.valueOf(expandoColumn.getColumnId()));
695                                    expandoColumnElement.addAttribute(
696                                            "name", expandoColumn.getName());
697                                    expandoColumnElement.addAttribute(
698                                            "type", String.valueOf(expandoColumn.getType()));
699    
700                                    DocUtil.add(
701                                            expandoColumnElement, "default-data",
702                                            expandoColumn.getDefaultData());
703    
704                                    Element typeSettingsElement = expandoColumnElement.addElement(
705                                            "type-settings");
706    
707                                    UnicodeProperties typeSettingsProperties =
708                                            expandoColumn.getTypeSettingsProperties();
709    
710                                    typeSettingsElement.addCDATA(typeSettingsProperties.toString());
711                            }
712                    }
713    
714                    portletDataContext.addZipEntry(
715                            portletDataContext.getRootPath() + "/expando-tables.xml",
716                            document.formattedString());
717            }
718    
719            protected void exportLocks(PortletDataContext portletDataContext)
720                    throws Exception {
721    
722                    Document document = SAXReaderUtil.createDocument();
723    
724                    Element rootElement = document.addElement("locks");
725    
726                    Map<String, Lock> locksMap = portletDataContext.getLocks();
727    
728                    for (Map.Entry<String, Lock> entry : locksMap.entrySet()) {
729                            Lock lock = entry.getValue();
730    
731                            String entryKey = entry.getKey();
732    
733                            int pos = entryKey.indexOf(CharPool.POUND);
734    
735                            String className = entryKey.substring(0, pos);
736                            String key = entryKey.substring(pos + 1);
737    
738                            String path = getLockPath(portletDataContext, className, key, lock);
739    
740                            Element assetElement = rootElement.addElement("asset");
741    
742                            assetElement.addAttribute("path", path);
743                            assetElement.addAttribute("class-name", className);
744                            assetElement.addAttribute("key", key);
745    
746                            if (portletDataContext.isPathNotProcessed(path)) {
747                                    portletDataContext.addZipEntry(path, lock);
748                            }
749                    }
750    
751                    portletDataContext.addZipEntry(
752                            portletDataContext.getRootPath() + "/locks.xml",
753                            document.formattedString());
754            }
755    
756            protected void exportPortlet(
757                            PortletDataContext portletDataContext, LayoutCache layoutCache,
758                            String portletId, Layout layout, Element parentElement,
759                            long defaultUserId, boolean exportPermissions,
760                            boolean exportPortletArchivedSetups, boolean exportPortletData,
761                            boolean exportPortletSetup, boolean exportPortletUserPreferences)
762                    throws Exception {
763    
764                    long plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;
765                    long layoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
766    
767                    if (layout != null) {
768                            plid = layout.getPlid();
769                            layoutId = layout.getLayoutId();
770                    }
771    
772                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
773                            portletDataContext.getCompanyId(), portletId);
774    
775                    if (portlet == null) {
776                            if (_log.isDebugEnabled()) {
777                                    _log.debug(
778                                            "Do not export portlet " + portletId +
779                                                    " because the portlet does not exist");
780                            }
781    
782                            return;
783                    }
784    
785                    if (!portlet.isInstanceable() &&
786                            !portlet.isPreferencesUniquePerLayout() &&
787                            portletDataContext.hasNotUniquePerLayout(portletId)) {
788    
789                            return;
790                    }
791    
792                    Document document = SAXReaderUtil.createDocument();
793    
794                    Element portletElement = document.addElement("portlet");
795    
796                    portletElement.addAttribute("portlet-id", portletId);
797                    portletElement.addAttribute(
798                            "root-portlet-id", PortletConstants.getRootPortletId(portletId));
799                    portletElement.addAttribute("old-plid", String.valueOf(plid));
800                    portletElement.addAttribute(
801                            "scope-layout-type", portletDataContext.getScopeType());
802                    portletElement.addAttribute(
803                            "scope-layout-uuid", portletDataContext.getScopeLayoutUuid());
804    
805                    // Data
806    
807                    if (exportPortletData) {
808                            javax.portlet.PortletPreferences jxPreferences =
809                                    PortletPreferencesFactoryUtil.getStrictPortletSetup(
810                                            layout, portletId);
811    
812                            if (!portlet.isPreferencesUniquePerLayout()) {
813                                    StringBundler sb = new StringBundler(5);
814    
815                                    sb.append(portletId);
816                                    sb.append(StringPool.AT);
817                                    sb.append(portletDataContext.getScopeType());
818                                    sb.append(StringPool.AT);
819                                    sb.append(portletDataContext.getScopeLayoutUuid());
820    
821                                    String dataKey = sb.toString();
822    
823                                    if (!portletDataContext.hasNotUniquePerLayout(dataKey)) {
824                                            portletDataContext.putNotUniquePerLayout(dataKey);
825    
826                                            exportPortletData(
827                                                    portletDataContext, portlet, layout, jxPreferences,
828                                                    portletElement);
829                                    }
830                            }
831                            else {
832                                    exportPortletData(
833                                            portletDataContext, portlet, layout, jxPreferences,
834                                            portletElement);
835                            }
836                    }
837    
838                    // Portlet preferences
839    
840                    if (exportPortletSetup) {
841                            exportPortletPreferences(
842                                    portletDataContext, PortletKeys.PREFS_OWNER_ID_DEFAULT,
843                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, false, layout, plid,
844                                    portletId, portletElement);
845    
846                            exportPortletPreferences(
847                                    portletDataContext, portletDataContext.getScopeGroupId(),
848                                    PortletKeys.PREFS_OWNER_TYPE_GROUP, false, layout, plid,
849                                    portletId, portletElement);
850    
851                            exportPortletPreferences(
852                                    portletDataContext, portletDataContext.getCompanyId(),
853                                    PortletKeys.PREFS_OWNER_TYPE_COMPANY, false, layout, plid,
854                                    portletId, portletElement);
855                    }
856    
857                    // Portlet preferences
858    
859                    if (exportPortletUserPreferences) {
860                            List<PortletPreferences> portletPreferencesList =
861                                    PortletPreferencesLocalServiceUtil.getPortletPreferences(
862                                            PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletId);
863    
864                            for (PortletPreferences portletPreferences :
865                                            portletPreferencesList) {
866    
867                                    boolean defaultUser = false;
868    
869                                    if (portletPreferences.getOwnerId() ==
870                                                    PortletKeys.PREFS_OWNER_ID_DEFAULT) {
871    
872                                            defaultUser = true;
873                                    }
874    
875                                    exportPortletPreferences(
876                                            portletDataContext, portletPreferences.getOwnerId(),
877                                            PortletKeys.PREFS_OWNER_TYPE_USER, defaultUser, layout,
878                                            plid, portletId, portletElement);
879                            }
880    
881                            try {
882                                    PortletPreferences groupPortletPreferences =
883                                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
884                                                    portletDataContext.getScopeGroupId(),
885                                                    PortletKeys.PREFS_OWNER_TYPE_GROUP,
886                                                    PortletKeys.PREFS_PLID_SHARED, portletId);
887    
888                                    exportPortletPreference(
889                                            portletDataContext, portletDataContext.getScopeGroupId(),
890                                            PortletKeys.PREFS_OWNER_TYPE_GROUP, false,
891                                            groupPortletPreferences, portletId,
892                                            PortletKeys.PREFS_PLID_SHARED, portletElement);
893                            }
894                            catch (NoSuchPortletPreferencesException nsppe) {
895                            }
896                    }
897    
898                    // Archived setups
899    
900                    if (exportPortletArchivedSetups) {
901                            String rootPortletId = PortletConstants.getRootPortletId(portletId);
902    
903                            List<PortletItem> portletItems =
904                                    PortletItemLocalServiceUtil.getPortletItems(
905                                            portletDataContext.getGroupId(), rootPortletId,
906                                            PortletPreferences.class.getName());
907    
908                            for (PortletItem portletItem : portletItems) {
909                                    exportPortletPreferences(
910                                            portletDataContext, portletItem.getPortletItemId(),
911                                            PortletKeys.PREFS_OWNER_TYPE_ARCHIVED, false, null, plid,
912                                            portletItem.getPortletId(), portletElement);
913                            }
914                    }
915    
916                    // Permissions
917    
918                    if (exportPermissions) {
919                            _permissionExporter.exportPortletPermissions(
920                                    portletDataContext, layoutCache, portletId, layout,
921                                    portletElement);
922                    }
923    
924                    // Zip
925    
926                    StringBundler sb = new StringBundler(4);
927    
928                    sb.append(portletDataContext.getPortletPath(portletId));
929                    sb.append(StringPool.SLASH);
930                    sb.append(plid);
931                    sb.append("/portlet.xml");
932    
933                    String path = sb.toString();
934    
935                    Element element = parentElement.addElement("portlet");
936    
937                    element.addAttribute("portlet-id", portletId);
938                    element.addAttribute("layout-id", String.valueOf(layoutId));
939                    element.addAttribute("path", path);
940    
941                    if (portletDataContext.isPathNotProcessed(path)) {
942                            try {
943                                    portletDataContext.addZipEntry(
944                                            path, document.formattedString());
945                            }
946                            catch (IOException ioe) {
947                                    if (_log.isWarnEnabled()) {
948                                            _log.warn(ioe.getMessage());
949                                    }
950                            }
951    
952                            portletDataContext.addPrimaryKey(String.class, path);
953                    }
954            }
955    
956            protected void exportPortletData(
957                            PortletDataContext portletDataContext, Portlet portlet,
958                            Layout layout, javax.portlet.PortletPreferences jxPreferences,
959                            Element parentElement)
960                    throws Exception {
961    
962                    if (portlet == null) {
963                            return;
964                    }
965    
966                    PortletDataHandler portletDataHandler =
967                            portlet.getPortletDataHandlerInstance();
968    
969                    if (portletDataHandler == null) {
970                            return;
971                    }
972    
973                    String portletId = portlet.getPortletId();
974    
975                    Group liveGroup = layout.getGroup();
976    
977                    if (liveGroup.isStagingGroup()) {
978                            liveGroup = liveGroup.getLiveGroup();
979                    }
980    
981                    boolean staged = liveGroup.isStagedPortlet(portlet.getRootPortletId());
982    
983                    if (!staged && ImportExportThreadLocal.isLayoutExportInProcess()) {
984                            if (_log.isDebugEnabled()) {
985                                    _log.debug(
986                                            "Not exporting data for " + portletId +
987                                                    " because it is configured not to be staged");
988                            }
989    
990                            return;
991                    }
992    
993                    if (_log.isDebugEnabled()) {
994                            _log.debug("Exporting data for " + portletId);
995                    }
996    
997                    StringBundler sb = new StringBundler(4);
998    
999                    sb.append(portletDataContext.getPortletPath(portletId));
1000                    sb.append(StringPool.SLASH);
1001    
1002                    if (portlet.isPreferencesUniquePerLayout()) {
1003                            sb.append(layout.getPlid());
1004                    }
1005                    else {
1006                            sb.append(portletDataContext.getScopeGroupId());
1007                    }
1008    
1009                    sb.append("/portlet-data.xml");
1010    
1011                    String path = sb.toString();
1012    
1013                    if (!portletDataContext.isPathNotProcessed(path)) {
1014                            return;
1015                    }
1016    
1017                    long lastPublishDate = GetterUtil.getLong(
1018                            jxPreferences.getValue("last-publish-date", StringPool.BLANK));
1019    
1020                    Date startDate = portletDataContext.getStartDate();
1021    
1022                    if ((lastPublishDate > 0) && (startDate != null) &&
1023                            (lastPublishDate < startDate.getTime())) {
1024    
1025                            portletDataContext.setStartDate(new Date(lastPublishDate));
1026                    }
1027    
1028                    String data = null;
1029    
1030                    long groupId = portletDataContext.getGroupId();
1031    
1032                    portletDataContext.setGroupId(portletDataContext.getScopeGroupId());
1033    
1034                    try {
1035                            data = portletDataHandler.exportData(
1036                                    portletDataContext, portletId, jxPreferences);
1037                    }
1038                    catch (Exception e) {
1039                            throw new SystemException(e);
1040                    }
1041                    finally {
1042                            portletDataContext.setGroupId(groupId);
1043                            portletDataContext.setStartDate(startDate);
1044                    }
1045    
1046                    if (Validator.isNull(data)) {
1047                            if (_log.isDebugEnabled()) {
1048                                    _log.debug(
1049                                            "Not exporting data for " + portletId +
1050                                                    " because null data was returned");
1051                            }
1052    
1053                            return;
1054                    }
1055    
1056                    Element portletDataElement = parentElement.addElement("portlet-data");
1057    
1058                    portletDataElement.addAttribute("path", path);
1059    
1060                    portletDataContext.addZipEntry(path, data);
1061    
1062                    Date endDate = portletDataContext.getEndDate();
1063    
1064                    if (endDate != null) {
1065                            try {
1066                                    jxPreferences.setValue(
1067                                            "last-publish-date", String.valueOf(endDate.getTime()));
1068    
1069                                    jxPreferences.store();
1070                            }
1071                            catch (UnsupportedOperationException uoe) {
1072                                    if (_log.isDebugEnabled()) {
1073                                            _log.debug(
1074                                                    "Not updating the portlet setup for " + portletId +
1075                                                            " because no setup was returned for the current " +
1076                                                                    "page");
1077                                    }
1078                            }
1079                            catch (Exception e) {
1080                                    _log.error(e, e);
1081                            }
1082                    }
1083            }
1084    
1085            protected void exportPortletPreference(
1086                            PortletDataContext portletDataContext, long ownerId, int ownerType,
1087                            boolean defaultUser, PortletPreferences portletPreferences,
1088                            String portletId, long plid, Element parentElement)
1089                    throws Exception {
1090    
1091                    String preferencesXML = portletPreferences.getPreferences();
1092    
1093                    if (Validator.isNull(preferencesXML)) {
1094                            preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
1095                    }
1096    
1097                    String rootPotletId = PortletConstants.getRootPortletId(portletId);
1098    
1099                    if (rootPotletId.equals(PortletKeys.ASSET_PUBLISHER)) {
1100                            preferencesXML = updateAssetPublisherPortletPreferences(
1101                                    preferencesXML, plid);
1102                    }
1103                    else if (rootPotletId.equals(PortletKeys.TAGS_CATEGORIES_NAVIGATION)) {
1104                            preferencesXML = updateAssetCategoriesNavigationPortletPreferences(
1105                                    preferencesXML, plid);
1106                    }
1107    
1108                    Document document = SAXReaderUtil.read(preferencesXML);
1109    
1110                    Element rootElement = document.getRootElement();
1111    
1112                    rootElement.addAttribute("owner-id", String.valueOf(ownerId));
1113                    rootElement.addAttribute("owner-type", String.valueOf(ownerType));
1114                    rootElement.addAttribute("default-user", String.valueOf(defaultUser));
1115                    rootElement.addAttribute("plid", String.valueOf(plid));
1116                    rootElement.addAttribute("portlet-id", portletId);
1117    
1118                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1119                            PortletItem portletItem =
1120                                    PortletItemLocalServiceUtil.getPortletItem(ownerId);
1121    
1122                            rootElement.addAttribute(
1123                                    "archive-user-uuid", portletItem.getUserUuid());
1124                            rootElement.addAttribute("archive-name", portletItem.getName());
1125                    }
1126                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1127                            User user = UserLocalServiceUtil.fetchUserById(ownerId);
1128    
1129                            if (user == null) {
1130                                    return;
1131                            }
1132    
1133                            rootElement.addAttribute("user-uuid", user.getUserUuid());
1134                    }
1135    
1136                    List<Node> nodes = document.selectNodes(
1137                            "/portlet-preferences/preference[name/text() = " +
1138                                    "'last-publish-date']");
1139    
1140                    for (Node node : nodes) {
1141                            document.remove(node);
1142                    }
1143    
1144                    String path = getPortletPreferencesPath(
1145                            portletDataContext, portletId, ownerId, ownerType, plid);
1146    
1147                    Element portletPreferencesElement = parentElement.addElement(
1148                            "portlet-preferences");
1149    
1150                    portletPreferencesElement.addAttribute("path", path);
1151    
1152                    if (portletDataContext.isPathNotProcessed(path)) {
1153                            portletDataContext.addZipEntry(
1154                                    path, document.formattedString(StringPool.TAB, false, false));
1155                    }
1156            }
1157    
1158            protected void exportPortletPreferences(
1159                            PortletDataContext portletDataContext, long ownerId, int ownerType,
1160                            boolean defaultUser, Layout layout, long plid, String portletId,
1161                            Element parentElement)
1162                    throws Exception {
1163    
1164                    PortletPreferences portletPreferences = null;
1165    
1166                    if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) ||
1167                            (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) ||
1168                            (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED)) {
1169    
1170                            plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;
1171                    }
1172    
1173                    try {
1174                            portletPreferences =
1175                                    PortletPreferencesLocalServiceUtil.getPortletPreferences(
1176                                            ownerId, ownerType, plid, portletId);
1177                    }
1178                    catch (NoSuchPortletPreferencesException nsppe) {
1179                            return;
1180                    }
1181    
1182                    LayoutTypePortlet layoutTypePortlet = null;
1183    
1184                    if (layout != null) {
1185                            layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
1186                    }
1187    
1188                    if ((layoutTypePortlet == null) ||
1189                            layoutTypePortlet.hasPortletId(portletId)) {
1190    
1191                            exportPortletPreference(
1192                                    portletDataContext, ownerId, ownerType, defaultUser,
1193                                    portletPreferences, portletId, plid, parentElement);
1194                    }
1195            }
1196    
1197            protected void exportRatingsEntries(
1198                            PortletDataContext portletDataContext, Element parentElement)
1199                    throws Exception {
1200    
1201                    Document document = SAXReaderUtil.createDocument();
1202    
1203                    Element rootElement = document.addElement("ratings");
1204    
1205                    Map<String, List<RatingsEntry>> ratingsEntriesMap =
1206                            portletDataContext.getRatingsEntries();
1207    
1208                    for (Map.Entry<String, List<RatingsEntry>> entry :
1209                                    ratingsEntriesMap.entrySet()) {
1210    
1211                            String[] ratingsEntryParts = StringUtil.split(
1212                                    entry.getKey(), CharPool.POUND);
1213    
1214                            String className = ratingsEntryParts[0];
1215                            String classPK = ratingsEntryParts[1];
1216    
1217                            String ratingsEntriesPath = getRatingsEntriesPath(
1218                                    portletDataContext, className, classPK);
1219    
1220                            Element assetElement = rootElement.addElement("asset");
1221    
1222                            assetElement.addAttribute("path", ratingsEntriesPath);
1223                            assetElement.addAttribute("class-name", className);
1224                            assetElement.addAttribute("class-pk", classPK);
1225    
1226                            List<RatingsEntry> ratingsEntries = entry.getValue();
1227    
1228                            for (RatingsEntry ratingsEntry : ratingsEntries) {
1229                                    String ratingsEntryPath = getRatingsEntryPath(
1230                                            portletDataContext, className, classPK, ratingsEntry);
1231    
1232                                    portletDataContext.addZipEntry(ratingsEntryPath, ratingsEntry);
1233                            }
1234                    }
1235    
1236                    portletDataContext.addZipEntry(
1237                            portletDataContext.getRootPath() + "/ratings.xml",
1238                            document.formattedString());
1239            }
1240    
1241            protected String getAssetCategoryPath(
1242                    PortletDataContext portletDataContext, long assetCategoryId) {
1243    
1244                    StringBundler sb = new StringBundler(6);
1245    
1246                    sb.append(portletDataContext.getRootPath());
1247                    sb.append("/categories/");
1248                    sb.append(assetCategoryId);
1249                    sb.append(".xml");
1250    
1251                    return sb.toString();
1252            }
1253    
1254            protected String getAssetTagPath(
1255                    PortletDataContext portletDataContext, long assetCategoryId) {
1256    
1257                    StringBundler sb = new StringBundler(4);
1258    
1259                    sb.append(portletDataContext.getRootPath());
1260                    sb.append("/tags/");
1261                    sb.append(assetCategoryId);
1262                    sb.append(".xml");
1263    
1264                    return sb.toString();
1265            }
1266    
1267            protected String getAssetVocabulariesPath(
1268                    PortletDataContext portletDataContext, long assetVocabularyId) {
1269    
1270                    StringBundler sb = new StringBundler(8);
1271    
1272                    sb.append(portletDataContext.getRootPath());
1273                    sb.append("/vocabularies/");
1274                    sb.append(assetVocabularyId);
1275                    sb.append(".xml");
1276    
1277                    return sb.toString();
1278            }
1279    
1280            protected String getCommentPath(
1281                    PortletDataContext portletDataContext, String className, String classPK,
1282                    MBMessage mbMessage) {
1283    
1284                    StringBundler sb = new StringBundler(8);
1285    
1286                    sb.append(portletDataContext.getRootPath());
1287                    sb.append("/comments/");
1288                    sb.append(PortalUtil.getClassNameId(className));
1289                    sb.append(CharPool.FORWARD_SLASH);
1290                    sb.append(classPK);
1291                    sb.append(CharPool.FORWARD_SLASH);
1292                    sb.append(mbMessage.getMessageId());
1293                    sb.append(".xml");
1294    
1295                    return sb.toString();
1296            }
1297    
1298            protected String getCommentsPath(
1299                    PortletDataContext portletDataContext, String className,
1300                    String classPK) {
1301    
1302                    StringBundler sb = new StringBundler(6);
1303    
1304                    sb.append(portletDataContext.getRootPath());
1305                    sb.append("/comments/");
1306                    sb.append(PortalUtil.getClassNameId(className));
1307                    sb.append(CharPool.FORWARD_SLASH);
1308                    sb.append(classPK);
1309                    sb.append(CharPool.FORWARD_SLASH);
1310    
1311                    return sb.toString();
1312            }
1313    
1314            protected String getLockPath(
1315                    PortletDataContext portletDataContext, String className, String key,
1316                    Lock lock) {
1317    
1318                    StringBundler sb = new StringBundler(8);
1319    
1320                    sb.append(portletDataContext.getRootPath());
1321                    sb.append("/locks/");
1322                    sb.append(PortalUtil.getClassNameId(className));
1323                    sb.append(CharPool.FORWARD_SLASH);
1324                    sb.append(key);
1325                    sb.append(CharPool.FORWARD_SLASH);
1326                    sb.append(lock.getLockId());
1327                    sb.append(".xml");
1328    
1329                    return sb.toString();
1330            }
1331    
1332            protected String getPortletDataPath(
1333                    PortletDataContext portletDataContext, String portletId) {
1334    
1335                    return portletDataContext.getPortletPath(portletId) +
1336                            "/portlet-data.xml";
1337            }
1338    
1339            protected String getPortletPreferencesPath(
1340                    PortletDataContext portletDataContext, String portletId, long ownerId,
1341                    int ownerType, long plid) {
1342    
1343                    StringBundler sb = new StringBundler(8);
1344    
1345                    sb.append(portletDataContext.getPortletPath(portletId));
1346                    sb.append("/preferences/");
1347    
1348                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
1349                            sb.append("company/");
1350                    }
1351                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
1352                            sb.append("group/");
1353                    }
1354                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT) {
1355                            sb.append("layout/");
1356                    }
1357                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1358                            sb.append("user/");
1359                    }
1360                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1361                            sb.append("archived/");
1362                    }
1363    
1364                    sb.append(ownerId);
1365                    sb.append(CharPool.FORWARD_SLASH);
1366                    sb.append(plid);
1367                    sb.append(CharPool.FORWARD_SLASH);
1368                    sb.append("portlet-preferences.xml");
1369    
1370                    return sb.toString();
1371            }
1372    
1373            protected String getRatingsEntriesPath(
1374                    PortletDataContext portletDataContext, String className,
1375                    String classPK) {
1376    
1377                    StringBundler sb = new StringBundler(6);
1378    
1379                    sb.append(portletDataContext.getRootPath());
1380                    sb.append("/ratings/");
1381                    sb.append(PortalUtil.getClassNameId(className));
1382                    sb.append(CharPool.FORWARD_SLASH);
1383                    sb.append(classPK);
1384                    sb.append(CharPool.FORWARD_SLASH);
1385    
1386                    return sb.toString();
1387            }
1388    
1389            protected String getRatingsEntryPath(
1390                    PortletDataContext portletDataContext, String className, String classPK,
1391                    RatingsEntry ratingsEntry) {
1392    
1393                    StringBundler sb = new StringBundler(8);
1394    
1395                    sb.append(portletDataContext.getRootPath());
1396                    sb.append("/ratings/");
1397                    sb.append(PortalUtil.getClassNameId(className));
1398                    sb.append(CharPool.FORWARD_SLASH);
1399                    sb.append(classPK);
1400                    sb.append(CharPool.FORWARD_SLASH);
1401                    sb.append(ratingsEntry.getEntryId());
1402                    sb.append(".xml");
1403    
1404                    return sb.toString();
1405            }
1406    
1407            protected String updateAssetCategoriesNavigationPortletPreferences(
1408                            String xml, long plid)
1409                    throws Exception {
1410    
1411                    javax.portlet.PortletPreferences jxPreferences =
1412                            PortletPreferencesFactoryUtil.fromDefaultXML(xml);
1413    
1414                    Enumeration<String> enu = jxPreferences.getNames();
1415    
1416                    while (enu.hasMoreElements()) {
1417                            String name = enu.nextElement();
1418    
1419                            if (name.equals("assetVocabularyIds")) {
1420                                    updatePreferencesClassPKs(
1421                                            jxPreferences, name, AssetVocabulary.class.getName());
1422                            }
1423                    }
1424    
1425                    return PortletPreferencesFactoryUtil.toXML(jxPreferences);
1426            }
1427    
1428            protected void updateAssetPublisherClassNameIds(
1429                            javax.portlet.PortletPreferences jxPreferences, String key)
1430                    throws Exception {
1431    
1432                    String[] oldValues = jxPreferences.getValues(key, null);
1433    
1434                    if (oldValues == null) {
1435                            return;
1436                    }
1437    
1438                    String[] newValues = new String[oldValues.length];
1439    
1440                    int i = 0;
1441    
1442                    for (String oldValue : oldValues) {
1443                            if (key.equals("anyAssetType") &&
1444                                    (oldValue.equals("false") || oldValue.equals("true"))) {
1445    
1446                                    newValues[i++] = oldValue;
1447    
1448                                    continue;
1449                            }
1450    
1451                            try {
1452                                    long classNameId = Long.parseLong(oldValue);
1453    
1454                                    String className = PortalUtil.getClassName(classNameId);
1455    
1456                                    newValues[i++] = className;
1457                            }
1458                            catch (Exception e) {
1459                                    if (_log.isWarnEnabled()) {
1460                                            _log.warn(
1461                                                    "Unable to find class name ID for class name " +
1462                                                            oldValue);
1463                                    }
1464                            }
1465                    }
1466    
1467                    jxPreferences.setValues(key, newValues);
1468            }
1469    
1470            protected void updateAssetPublisherGlobalScopeId(
1471                            javax.portlet.PortletPreferences jxPreferences, String key,
1472                            long plid)
1473                    throws Exception {
1474    
1475                    String[] oldValues = jxPreferences.getValues(key, null);
1476    
1477                    if (oldValues == null) {
1478                            return;
1479                    }
1480    
1481                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1482    
1483                    Company company = CompanyLocalServiceUtil.getCompany(
1484                            layout.getCompanyId());
1485    
1486                    Group companyGroup = company.getGroup();
1487    
1488                    String[] newValues = new String[oldValues.length];
1489    
1490                    for (int i = 0; i < oldValues.length; i++) {
1491                            String oldValue = oldValues[i];
1492    
1493                            newValues[i] = StringUtil.replace(
1494                                    oldValue, "Group_" + companyGroup.getGroupId(),
1495                                    "Group_Company");
1496                    }
1497    
1498                    jxPreferences.setValues(key, newValues);
1499            }
1500    
1501            protected String updateAssetPublisherPortletPreferences(
1502                            String xml, long plid)
1503                    throws Exception {
1504    
1505                    javax.portlet.PortletPreferences jxPreferences =
1506                            PortletPreferencesFactoryUtil.fromDefaultXML(xml);
1507    
1508                    Enumeration<String> enu = jxPreferences.getNames();
1509    
1510                    while (enu.hasMoreElements()) {
1511                            String name = enu.nextElement();
1512    
1513                            String value = GetterUtil.getString(
1514                                    jxPreferences.getValue(name, null));
1515    
1516                            if (name.equals(
1517                                            "anyClassTypeJournalArticleAssetRendererFactory") ||
1518                                    name.equals(
1519                                            "classTypeIdsJournalArticleAssetRendererFactory") ||
1520                                    name.equals("classTypeIds")) {
1521    
1522                                    updatePreferencesClassPKs(
1523                                            jxPreferences, name, JournalStructure.class.getName());
1524                            }
1525                            else if (name.equals("anyAssetType") ||
1526                                             name.equals("classNameIds")) {
1527    
1528                                    updateAssetPublisherClassNameIds(jxPreferences, name);
1529                            }
1530                            else if (name.equals("defaultScope") || name.equals("scopeIds")) {
1531                                    updateAssetPublisherGlobalScopeId(jxPreferences, name, plid);
1532                            }
1533                            else if (name.startsWith("queryName") &&
1534                                             value.equalsIgnoreCase("assetCategories")) {
1535    
1536                                    String index = name.substring(9);
1537    
1538                                    updatePreferencesClassPKs(
1539                                            jxPreferences, "queryValues" + index,
1540                                            AssetCategory.class.getName());
1541                            }
1542                    }
1543    
1544                    return PortletPreferencesFactoryUtil.toXML(jxPreferences);
1545            }
1546    
1547            protected void updatePreferencesClassPKs(
1548                            javax.portlet.PortletPreferences jxPreferences, String key,
1549                            String className)
1550                    throws Exception {
1551    
1552                    String[] oldValues = jxPreferences.getValues(key, null);
1553    
1554                    if (oldValues == null) {
1555                            return;
1556                    }
1557    
1558                    String[] newValues = new String[oldValues.length];
1559    
1560                    for (int i = 0; i < oldValues.length; i++) {
1561                            String oldValue = oldValues[i];
1562    
1563                            String newValue = oldValue;
1564    
1565                            String[] primaryKeys = StringUtil.split(oldValue);
1566    
1567                            for (String primaryKey : primaryKeys) {
1568                                    if (!Validator.isNumber(primaryKey)) {
1569                                            break;
1570                                    }
1571    
1572                                    long primaryKeyLong = GetterUtil.getLong(primaryKey);
1573    
1574                                    String uuid = null;
1575    
1576                                    if (className.equals(AssetCategory.class.getName())) {
1577                                            AssetCategory assetCategory =
1578                                                    AssetCategoryLocalServiceUtil.fetchCategory(
1579                                                            primaryKeyLong);
1580    
1581                                            if (assetCategory != null) {
1582                                                    uuid = assetCategory.getUuid();
1583                                            }
1584                                    }
1585                                    else if (className.equals(AssetVocabulary.class.getName())) {
1586                                            AssetVocabulary assetVocabulary =
1587                                                    AssetVocabularyLocalServiceUtil.fetchAssetVocabulary(
1588                                                            primaryKeyLong);
1589    
1590                                            if (assetVocabulary != null) {
1591                                                    uuid = assetVocabulary.getUuid();
1592                                            }
1593                                    }
1594                                    else if (className.equals(JournalStructure.class.getName())) {
1595                                            JournalStructure journalStructure =
1596                                                    JournalStructureLocalServiceUtil.fetchJournalStructure(
1597                                                            primaryKeyLong);
1598    
1599                                            if (journalStructure != null) {
1600                                                    uuid = journalStructure.getUuid();
1601                                            }
1602                                    }
1603    
1604                                    if (Validator.isNull(uuid)) {
1605                                            if (_log.isWarnEnabled()) {
1606                                                    _log.warn(
1607                                                            "Unable to get UUID for class " + className +
1608                                                                    " with primary key " + primaryKeyLong);
1609                                            }
1610    
1611                                            continue;
1612                                    }
1613    
1614                                    newValue = StringUtil.replace(newValue, primaryKey, uuid);
1615                            }
1616    
1617                            newValues[i] = newValue;
1618                    }
1619    
1620                    jxPreferences.setValues(key, newValues);
1621            }
1622    
1623            private static Log _log = LogFactoryUtil.getLog(PortletExporter.class);
1624    
1625            private PermissionExporter _permissionExporter = new PermissionExporter();
1626    
1627    }