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