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.messageboards.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.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.util.MapUtil;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.messageboards.model.MBCategory;
027    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
028    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
029    
030    import java.util.Map;
031    
032    /**
033     * @author Daniel Kocsis
034     */
035    public class MBCategoryStagedModelDataHandler
036            extends BaseStagedModelDataHandler<MBCategory> {
037    
038            public static final String[] CLASS_NAMES = {MBCategory.class.getName()};
039    
040            @Override
041            public void deleteStagedModel(
042                            String uuid, long groupId, String className, String extraData)
043                    throws PortalException, SystemException {
044    
045                    MBCategory category =
046                            MBCategoryLocalServiceUtil.fetchMBCategoryByUuidAndGroupId(
047                                    uuid, groupId);
048    
049                    if (category != null) {
050                            MBCategoryLocalServiceUtil.deleteCategory(category);
051                    }
052            }
053    
054            @Override
055            public String[] getClassNames() {
056                    return CLASS_NAMES;
057            }
058    
059            @Override
060            public String getDisplayName(MBCategory category) {
061                    return category.getName();
062            }
063    
064            @Override
065            protected void doExportStagedModel(
066                            PortletDataContext portletDataContext, MBCategory category)
067                    throws Exception {
068    
069                    if ((category.getCategoryId() ==
070                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
071                            (category.getCategoryId() ==
072                                    MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
073    
074                            return;
075                    }
076    
077                    if (category.getParentCategory() != null) {
078                            StagedModelDataHandlerUtil.exportStagedModel(
079                                    portletDataContext, category.getParentCategory());
080                    }
081    
082                    Element categoryElement = portletDataContext.getExportDataElement(
083                            category);
084    
085                    portletDataContext.addClassedModel(
086                            categoryElement, ExportImportPathUtil.getModelPath(category),
087                            category, MBPortletDataHandler.NAMESPACE);
088            }
089    
090            @Override
091            protected void doImportStagedModel(
092                            PortletDataContext portletDataContext, MBCategory category)
093                    throws Exception {
094    
095                    long userId = portletDataContext.getUserId(category.getUserUuid());
096    
097                    Map<Long, Long> categoryIds =
098                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
099                                    MBCategory.class);
100    
101                    long parentCategoryId = MapUtil.getLong(
102                            categoryIds, category.getParentCategoryId(),
103                            category.getParentCategoryId());
104    
105                    String emailAddress = null;
106                    String inProtocol = null;
107                    String inServerName = null;
108                    int inServerPort = 0;
109                    boolean inUseSSL = false;
110                    String inUserName = null;
111                    String inPassword = null;
112                    int inReadInterval = 0;
113                    String outEmailAddress = null;
114                    boolean outCustom = false;
115                    String outServerName = null;
116                    int outServerPort = 0;
117                    boolean outUseSSL = false;
118                    String outUserName = null;
119                    String outPassword = null;
120                    boolean allowAnonymous = false;
121                    boolean mailingListActive = false;
122    
123                    // Parent category
124    
125                    if ((parentCategoryId !=
126                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
127                            (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID) &&
128                            (parentCategoryId == category.getParentCategoryId())) {
129    
130                            String parentCategoryPath = ExportImportPathUtil.getModelPath(
131                                    portletDataContext, MBCategory.class.getName(),
132                                    parentCategoryId);
133    
134                            MBCategory parentCategory =
135                                    (MBCategory)portletDataContext.getZipEntryAsObject(
136                                            parentCategoryPath);
137    
138                            StagedModelDataHandlerUtil.importStagedModel(
139                                    portletDataContext, parentCategory);
140    
141                            parentCategoryId = MapUtil.getLong(
142                                    categoryIds, category.getParentCategoryId(),
143                                    category.getParentCategoryId());
144                    }
145    
146                    ServiceContext serviceContext = portletDataContext.createServiceContext(
147                            category, MBPortletDataHandler.NAMESPACE);
148    
149                    MBCategory importedCategory = null;
150    
151                    if (portletDataContext.isDataStrategyMirror()) {
152                            MBCategory existingCategory =
153                                    MBCategoryLocalServiceUtil.fetchMBCategoryByUuidAndGroupId(
154                                            category.getUuid(), portletDataContext.getScopeGroupId());
155    
156                            if (existingCategory == null) {
157                                    serviceContext.setUuid(category.getUuid());
158    
159                                    importedCategory = MBCategoryLocalServiceUtil.addCategory(
160                                            userId, parentCategoryId, category.getName(),
161                                            category.getDescription(), category.getDisplayStyle(),
162                                            emailAddress, inProtocol, inServerName, inServerPort,
163                                            inUseSSL, inUserName, inPassword, inReadInterval,
164                                            outEmailAddress, outCustom, outServerName, outServerPort,
165                                            outUseSSL, outUserName, outPassword, allowAnonymous,
166                                            mailingListActive, serviceContext);
167                            }
168                            else {
169                                    importedCategory = MBCategoryLocalServiceUtil.updateCategory(
170                                            existingCategory.getCategoryId(), parentCategoryId,
171                                            category.getName(), category.getDescription(),
172                                            category.getDisplayStyle(), emailAddress, inProtocol,
173                                            inServerName, inServerPort, inUseSSL, inUserName,
174                                            inPassword, inReadInterval, outEmailAddress, outCustom,
175                                            outServerName, outServerPort, outUseSSL, outUserName,
176                                            outPassword, allowAnonymous, mailingListActive, false,
177                                            serviceContext);
178                            }
179                    }
180                    else {
181                            importedCategory = MBCategoryLocalServiceUtil.addCategory(
182                                    userId, parentCategoryId, category.getName(),
183                                    category.getDescription(), category.getDisplayStyle(),
184                                    emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
185                                    inUserName, inPassword, inReadInterval, outEmailAddress,
186                                    outCustom, outServerName, outServerPort, outUseSSL, outUserName,
187                                    outPassword, allowAnonymous, mailingListActive, serviceContext);
188                    }
189    
190                    portletDataContext.importClassedModel(
191                            category, importedCategory, MBPortletDataHandler.NAMESPACE);
192            }
193    
194    }