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