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