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