001    /**
002     * Copyright (c) 2000-present 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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.util.MapUtil;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.model.Repository;
021    import com.liferay.portal.model.RepositoryEntry;
022    import com.liferay.portal.service.RepositoryEntryLocalServiceUtil;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
025    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
026    import com.liferay.portlet.exportimport.lar.PortletDataContext;
027    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
028    
029    import java.util.List;
030    import java.util.Map;
031    
032    /**
033     * @author Mate Thurzo
034     */
035    public class RepositoryEntryStagedModelDataHandler
036            extends BaseStagedModelDataHandler<RepositoryEntry> {
037    
038            public static final String[] CLASS_NAMES =
039                    {RepositoryEntry.class.getName()};
040    
041            @Override
042            public void deleteStagedModel(RepositoryEntry repositoryEntry) {
043                    RepositoryEntryLocalServiceUtil.deleteRepositoryEntry(repositoryEntry);
044            }
045    
046            @Override
047            public void deleteStagedModel(
048                    String uuid, long groupId, String className, String extraData) {
049    
050                    RepositoryEntry repositoryEntry = fetchStagedModelByUuidAndGroupId(
051                            uuid, groupId);
052    
053                    if (repositoryEntry != null) {
054                            deleteStagedModel(repositoryEntry);
055                    }
056            }
057    
058            @Override
059            public RepositoryEntry fetchStagedModelByUuidAndGroupId(
060                    String uuid, long groupId) {
061    
062                    return RepositoryEntryLocalServiceUtil.
063                            fetchRepositoryEntryByUuidAndGroupId(uuid, groupId);
064            }
065    
066            @Override
067            public List<RepositoryEntry> fetchStagedModelsByUuidAndCompanyId(
068                    String uuid, long companyId) {
069    
070                    return RepositoryEntryLocalServiceUtil.
071                            getRepositoryEntriesByUuidAndCompanyId(
072                                    uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
073                                    new StagedModelModifiedDateComparator<RepositoryEntry>());
074            }
075    
076            @Override
077            public String[] getClassNames() {
078                    return CLASS_NAMES;
079            }
080    
081            @Override
082            protected void doExportStagedModel(
083                            PortletDataContext portletDataContext,
084                            RepositoryEntry repositoryEntry)
085                    throws Exception {
086    
087                    Element repositoryEntryElement =
088                            portletDataContext.getExportDataElement(repositoryEntry);
089    
090                    portletDataContext.addClassedModel(
091                            repositoryEntryElement,
092                            ExportImportPathUtil.getModelPath(repositoryEntry),
093                            repositoryEntry);
094            }
095    
096            @Override
097            protected void doImportStagedModel(
098                            PortletDataContext portletDataContext,
099                            RepositoryEntry repositoryEntry)
100                    throws Exception {
101    
102                    long userId = portletDataContext.getUserId(
103                            repositoryEntry.getUserUuid());
104    
105                    Map<Long, Long> repositoryIds =
106                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
107                                    Repository.class);
108    
109                    long repositoryId = MapUtil.getLong(
110                            repositoryIds, repositoryEntry.getRepositoryId(),
111                            repositoryEntry.getRepositoryId());
112    
113                    ServiceContext serviceContext = portletDataContext.createServiceContext(
114                            repositoryEntry);
115    
116                    RepositoryEntry importedRepositoryEntry = null;
117    
118                    if (portletDataContext.isDataStrategyMirror()) {
119                            RepositoryEntry existingRepositoryEntry =
120                                    fetchStagedModelByUuidAndGroupId(
121                                            repositoryEntry.getUuid(),
122                                            portletDataContext.getScopeGroupId());
123    
124                            if (existingRepositoryEntry == null) {
125                                    serviceContext.setUuid(repositoryEntry.getUuid());
126    
127                                    importedRepositoryEntry =
128                                            RepositoryEntryLocalServiceUtil.addRepositoryEntry(
129                                                    userId, portletDataContext.getScopeGroupId(),
130                                                    repositoryId, repositoryEntry.getMappedId(),
131                                                    serviceContext);
132                            }
133                            else {
134                                    importedRepositoryEntry =
135                                            RepositoryEntryLocalServiceUtil.updateRepositoryEntry(
136                                                    existingRepositoryEntry.getRepositoryEntryId(),
137                                                    repositoryEntry.getMappedId());
138                            }
139                    }
140                    else {
141                            importedRepositoryEntry =
142                                    RepositoryEntryLocalServiceUtil.addRepositoryEntry(
143                                            userId, portletDataContext.getScopeGroupId(), repositoryId,
144                                            repositoryEntry.getMappedId(), serviceContext);
145                    }
146    
147                    portletDataContext.importClassedModel(
148                            repositoryEntry, importedRepositoryEntry);
149            }
150    
151    }