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