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.util.StringPool;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portlet.wiki.model.WikiNode;
027    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
028    
029    /**
030     * @author Zsolt Berentey
031     */
032    public class WikiNodeStagedModelDataHandler
033            extends BaseStagedModelDataHandler<WikiNode> {
034    
035            public static final String[] CLASS_NAMES = {WikiNode.class.getName()};
036    
037            @Override
038            public void deleteStagedModel(
039                            String uuid, long groupId, String className, String extraData)
040                    throws PortalException, SystemException {
041    
042                    WikiNode wikiNode =
043                            WikiNodeLocalServiceUtil.fetchWikiNodeByUuidAndGroupId(
044                                    uuid, groupId);
045    
046                    if (wikiNode != null) {
047                            WikiNodeLocalServiceUtil.deleteNode(wikiNode);
048                    }
049            }
050    
051            @Override
052            public String[] getClassNames() {
053                    return CLASS_NAMES;
054            }
055    
056            @Override
057            protected void doExportStagedModel(
058                            PortletDataContext portletDataContext, WikiNode node)
059                    throws Exception {
060    
061                    Element nodeElement = portletDataContext.getExportDataElement(node);
062    
063                    portletDataContext.addClassedModel(
064                            nodeElement, ExportImportPathUtil.getModelPath(node), node,
065                            WikiPortletDataHandler.NAMESPACE);
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, WikiPortletDataHandler.NAMESPACE);
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(
131                            node, importedNode, WikiPortletDataHandler.NAMESPACE);
132            }
133    
134            protected String getNodeName(
135                            PortletDataContext portletDataContext, WikiNode node, String name,
136                            int count)
137                    throws Exception {
138    
139                    WikiNode existingNode = WikiNodeLocalServiceUtil.fetchNode(
140                            portletDataContext.getScopeGroupId(), name);
141    
142                    if (existingNode == null) {
143                            return name;
144                    }
145    
146                    String nodeName = node.getName();
147    
148                    return getNodeName(
149                            portletDataContext, node,
150                            nodeName.concat(StringPool.SPACE).concat(String.valueOf(count)),
151                            ++count);
152            }
153    
154            @Override
155            protected boolean validateMissingReference(
156                            String uuid, long companyId, long groupId)
157                    throws Exception {
158    
159                    WikiNode node = WikiNodeLocalServiceUtil.fetchNodeByUuidAndGroupId(
160                            uuid, groupId);
161    
162                    if (node == null) {
163                            return false;
164                    }
165    
166                    return true;
167            }
168    
169    }