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