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.portlet.documentlibrary.lar;
016    
017    import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
018    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.util.MapUtil;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.model.Repository;
023    import com.liferay.portal.model.RepositoryEntry;
024    import com.liferay.portal.service.RepositoryEntryLocalServiceUtil;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.persistence.RepositoryEntryUtil;
027    
028    import java.util.Map;
029    
030    /**
031     * @author Mate Thurzo
032     */
033    public class RepositoryEntryStagedModelDataHandler
034            extends BaseStagedModelDataHandler<RepositoryEntry> {
035    
036            public static final String[] CLASS_NAMES =
037                    {RepositoryEntry.class.getName()};
038    
039            @Override
040            public String[] getClassNames() {
041                    return CLASS_NAMES;
042            }
043    
044            @Override
045            protected void doExportStagedModel(
046                            PortletDataContext portletDataContext,
047                            RepositoryEntry repositoryEntry)
048                    throws Exception {
049    
050                    Element repositoryEntryElement =
051                            portletDataContext.getExportDataStagedModelElement(repositoryEntry);
052    
053                    portletDataContext.addClassedModel(
054                            repositoryEntryElement,
055                            ExportImportPathUtil.getModelPath(repositoryEntry), repositoryEntry,
056                            DLPortletDataHandler.NAMESPACE);
057            }
058    
059            @Override
060            protected void doImportStagedModel(
061                            PortletDataContext portletDataContext,
062                            RepositoryEntry repositoryEntry)
063                    throws Exception {
064    
065                    long userId = portletDataContext.getUserId(
066                            repositoryEntry.getUserUuid());
067    
068                    Map<Long, Long> repositoryIds =
069                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
070                                    Repository.class);
071    
072                    long repositoryId = MapUtil.getLong(
073                            repositoryIds, repositoryEntry.getRepositoryId(),
074                            repositoryEntry.getRepositoryId());
075    
076                    ServiceContext serviceContext = portletDataContext.createServiceContext(
077                            repositoryEntry, DLPortletDataHandler.NAMESPACE);
078    
079                    RepositoryEntry importedRepositoryEntry = null;
080    
081                    if (portletDataContext.isDataStrategyMirror()) {
082                            RepositoryEntry existingRepositoryEntry =
083                                    RepositoryEntryUtil.fetchByUUID_G(
084                                            repositoryEntry.getUuid(),
085                                            portletDataContext.getScopeGroupId());
086    
087                            if (existingRepositoryEntry == null) {
088                                    serviceContext.setUuid(repositoryEntry.getUuid());
089    
090                                    importedRepositoryEntry =
091                                            RepositoryEntryLocalServiceUtil.addRepositoryEntry(
092                                                    userId, portletDataContext.getScopeGroupId(),
093                                                    repositoryId, repositoryEntry.getMappedId(),
094                                                    serviceContext);
095                            }
096                            else {
097                                    importedRepositoryEntry =
098                                            RepositoryEntryLocalServiceUtil.updateRepositoryEntry(
099                                                    existingRepositoryEntry.getRepositoryEntryId(),
100                                                    repositoryEntry.getMappedId());
101                            }
102                    }
103                    else {
104                            importedRepositoryEntry =
105                                    RepositoryEntryLocalServiceUtil.addRepositoryEntry(
106                                            userId, portletDataContext.getScopeGroupId(), repositoryId,
107                                            repositoryEntry.getMappedId(), serviceContext);
108                    }
109    
110                    portletDataContext.importClassedModel(
111                            repositoryEntry, importedRepositoryEntry,
112                            DLPortletDataHandler.NAMESPACE);
113            }
114    
115    }