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