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