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.wiki.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
019    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.trash.TrashHandler;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.wiki.model.WikiNode;
028    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
029    
030    import java.util.List;
031    import java.util.Map;
032    
033    /**
034     * @author Zsolt Berentey
035     */
036    public class WikiNodeStagedModelDataHandler
037            extends BaseStagedModelDataHandler<WikiNode> {
038    
039            public static final String[] CLASS_NAMES = {WikiNode.class.getName()};
040    
041            @Override
042            public void deleteStagedModel(
043                            String uuid, long groupId, String className, String extraData)
044                    throws PortalException {
045    
046                    WikiNode wikiNode = fetchStagedModelByUuidAndGroupId(uuid, groupId);
047    
048                    if (wikiNode != null) {
049                            WikiNodeLocalServiceUtil.deleteNode(wikiNode);
050                    }
051            }
052    
053            @Override
054            public WikiNode fetchStagedModelByUuidAndCompanyId(
055                    String uuid, long companyId) {
056    
057                    List<WikiNode> wikiNodes =
058                            WikiNodeLocalServiceUtil.getWikiNodesByUuidAndCompanyId(
059                                    uuid, companyId);
060    
061                    if (ListUtil.isEmpty(wikiNodes)) {
062                            return null;
063                    }
064    
065                    return wikiNodes.get(0);
066            }
067    
068            @Override
069            public WikiNode fetchStagedModelByUuidAndGroupId(
070                    String uuid, long groupId) {
071    
072                    return WikiNodeLocalServiceUtil.fetchWikiNodeByUuidAndGroupId(
073                            uuid, groupId);
074            }
075    
076            @Override
077            public String[] getClassNames() {
078                    return CLASS_NAMES;
079            }
080    
081            @Override
082            protected void doExportStagedModel(
083                            PortletDataContext portletDataContext, WikiNode node)
084                    throws Exception {
085    
086                    Element nodeElement = portletDataContext.getExportDataElement(node);
087    
088                    portletDataContext.addClassedModel(
089                            nodeElement, ExportImportPathUtil.getModelPath(node), node);
090            }
091    
092            @Override
093            protected void doImportMissingReference(
094                            PortletDataContext portletDataContext, String uuid, long groupId,
095                            long nodeId)
096                    throws Exception {
097    
098                    WikiNode existingNode = fetchMissingReference(uuid, groupId);
099    
100                    Map<Long, Long> nodeIds =
101                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
102                                    WikiNode.class);
103    
104                    nodeIds.put(nodeId, existingNode.getNodeId());
105            }
106    
107            @Override
108            protected void doImportStagedModel(
109                            PortletDataContext portletDataContext, WikiNode node)
110                    throws Exception {
111    
112                    long userId = portletDataContext.getUserId(node.getUserUuid());
113    
114                    ServiceContext serviceContext = portletDataContext.createServiceContext(
115                            node);
116    
117                    WikiNode importedNode = null;
118    
119                    if (portletDataContext.isDataStrategyMirror()) {
120                            WikiNode existingNode = fetchStagedModelByUuidAndGroupId(
121                                    node.getUuid(), portletDataContext.getScopeGroupId());
122    
123                            String initialNodeName = PropsValues.WIKI_INITIAL_NODE_NAME;
124    
125                            if ((existingNode == null) &&
126                                    initialNodeName.equals(node.getName())) {
127    
128                                    WikiNode initialNode = WikiNodeLocalServiceUtil.fetchNode(
129                                            portletDataContext.getScopeGroupId(), node.getName());
130    
131                                    if (initialNode != null) {
132                                            WikiNodeLocalServiceUtil.deleteWikiNode(initialNode);
133                                    }
134                            }
135    
136                            if (existingNode == null) {
137                                    serviceContext.setUuid(node.getUuid());
138    
139                                    importedNode = WikiNodeLocalServiceUtil.addNode(
140                                            userId, node.getName(), node.getDescription(),
141                                            serviceContext);
142                            }
143                            else {
144                                    importedNode = WikiNodeLocalServiceUtil.updateNode(
145                                            existingNode.getNodeId(), node.getName(),
146                                            node.getDescription(), serviceContext);
147                            }
148                    }
149                    else {
150                            String initialNodeName = PropsValues.WIKI_INITIAL_NODE_NAME;
151    
152                            if (initialNodeName.equals(node.getName())) {
153                                    WikiNode initialNode = WikiNodeLocalServiceUtil.fetchNode(
154                                            portletDataContext.getScopeGroupId(), node.getName());
155    
156                                    if (initialNode != null) {
157                                            WikiNodeLocalServiceUtil.deleteWikiNode(initialNode);
158                                    }
159                            }
160    
161                            String nodeName = getNodeName(
162                                    portletDataContext, node, node.getName(), 2);
163    
164                            importedNode = WikiNodeLocalServiceUtil.addNode(
165                                    userId, nodeName, node.getDescription(), serviceContext);
166                    }
167    
168                    portletDataContext.importClassedModel(node, importedNode);
169            }
170    
171            @Override
172            protected void doRestoreStagedModel(
173                            PortletDataContext portletDataContext, WikiNode node)
174                    throws Exception {
175    
176                    long userId = portletDataContext.getUserId(node.getUserUuid());
177    
178                    WikiNode existingNode = fetchStagedModelByUuidAndGroupId(
179                            node.getUuid(), portletDataContext.getScopeGroupId());
180    
181                    if ((existingNode == null) || !existingNode.isInTrash()) {
182                            return;
183                    }
184    
185                    TrashHandler trashHandler = existingNode.getTrashHandler();
186    
187                    if (trashHandler.isRestorable(existingNode.getNodeId())) {
188                            trashHandler.restoreTrashEntry(userId, existingNode.getNodeId());
189                    }
190            }
191    
192            protected String getNodeName(
193                            PortletDataContext portletDataContext, WikiNode node, String name,
194                            int count)
195                    throws Exception {
196    
197                    WikiNode existingNode = WikiNodeLocalServiceUtil.fetchNode(
198                            portletDataContext.getScopeGroupId(), name);
199    
200                    if (existingNode == null) {
201                            return name;
202                    }
203    
204                    String nodeName = node.getName();
205    
206                    return getNodeName(
207                            portletDataContext, node,
208                            nodeName.concat(StringPool.SPACE).concat(String.valueOf(count)),
209                            ++count);
210            }
211    
212    }