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.asset.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
020    import com.liferay.portal.kernel.util.MapUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portlet.asset.model.AssetCategory;
027    import com.liferay.portlet.asset.model.AssetCategoryConstants;
028    import com.liferay.portlet.asset.model.AssetCategoryProperty;
029    import com.liferay.portlet.asset.model.AssetVocabulary;
030    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
031    import com.liferay.portlet.asset.service.AssetCategoryPropertyLocalServiceUtil;
032    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
033    import com.liferay.portlet.asset.service.persistence.AssetCategoryUtil;
034    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
035    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
036    import com.liferay.portlet.exportimport.lar.PortletDataContext;
037    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
038    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
039    
040    import java.util.HashMap;
041    import java.util.List;
042    import java.util.Locale;
043    import java.util.Map;
044    
045    /**
046     * @author Zsolt Berentey
047     * @author Gergely Mathe
048     * @author Mate Thurzo
049     */
050    @OSGiBeanProperties
051    public class AssetCategoryStagedModelDataHandler
052            extends BaseStagedModelDataHandler<AssetCategory> {
053    
054            public static final String[] CLASS_NAMES = {AssetCategory.class.getName()};
055    
056            @Override
057            public void deleteStagedModel(AssetCategory category)
058                    throws PortalException {
059    
060                    AssetCategoryLocalServiceUtil.deleteCategory(category);
061            }
062    
063            @Override
064            public void deleteStagedModel(
065                            String uuid, long groupId, String className, String extraData)
066                    throws PortalException {
067    
068                    AssetCategory category = fetchStagedModelByUuidAndGroupId(
069                            uuid, groupId);
070    
071                    if (category != null) {
072                            deleteStagedModel(category);
073                    }
074            }
075    
076            @Override
077            public AssetCategory fetchStagedModelByUuidAndGroupId(
078                    String uuid, long groupId) {
079    
080                    return AssetCategoryLocalServiceUtil.fetchAssetCategoryByUuidAndGroupId(
081                            uuid, groupId);
082            }
083    
084            @Override
085            public List<AssetCategory> fetchStagedModelsByUuidAndCompanyId(
086                    String uuid, long companyId) {
087    
088                    return AssetCategoryLocalServiceUtil.
089                            getAssetCategoriesByUuidAndCompanyId(
090                                    uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
091                                    new StagedModelModifiedDateComparator<AssetCategory>());
092            }
093    
094            @Override
095            public String[] getClassNames() {
096                    return CLASS_NAMES;
097            }
098    
099            @Override
100            public String getDisplayName(AssetCategory category) {
101                    return category.getTitleCurrentValue();
102            }
103    
104            protected ServiceContext createServiceContext(
105                    PortletDataContext portletDataContext, AssetCategory category) {
106    
107                    ServiceContext serviceContext = new ServiceContext();
108    
109                    serviceContext.setAddGroupPermissions(true);
110                    serviceContext.setAddGuestPermissions(true);
111                    serviceContext.setCreateDate(category.getCreateDate());
112                    serviceContext.setModifiedDate(category.getModifiedDate());
113                    serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());
114    
115                    return serviceContext;
116            }
117    
118            @Override
119            protected void doExportStagedModel(
120                            PortletDataContext portletDataContext, AssetCategory category)
121                    throws Exception {
122    
123                    if (category.getParentCategoryId() !=
124                                    AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
125    
126                            AssetCategory parentCategory =
127                                    AssetCategoryLocalServiceUtil.fetchAssetCategory(
128                                            category.getParentCategoryId());
129    
130                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
131                                    portletDataContext, category, parentCategory,
132                                    PortletDataContext.REFERENCE_TYPE_PARENT);
133                    }
134                    else {
135                            AssetVocabulary vocabulary =
136                                    AssetVocabularyLocalServiceUtil.fetchAssetVocabulary(
137                                            category.getVocabularyId());
138    
139                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
140                                    portletDataContext, category, vocabulary,
141                                    PortletDataContext.REFERENCE_TYPE_PARENT);
142                    }
143    
144                    Element categoryElement = portletDataContext.getExportDataElement(
145                            category);
146    
147                    category.setUserUuid(category.getUserUuid());
148    
149                    List<AssetCategoryProperty> categoryProperties =
150                            AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(
151                                    category.getCategoryId());
152    
153                    for (AssetCategoryProperty categoryProperty : categoryProperties) {
154                            Element propertyElement = categoryElement.addElement("property");
155    
156                            propertyElement.addAttribute(
157                                    "userUuid", categoryProperty.getUserUuid());
158                            propertyElement.addAttribute("key", categoryProperty.getKey());
159                            propertyElement.addAttribute("value", categoryProperty.getValue());
160                    }
161    
162                    String categoryPath = ExportImportPathUtil.getModelPath(category);
163    
164                    categoryElement.addAttribute("path", categoryPath);
165    
166                    portletDataContext.addReferenceElement(
167                            category, categoryElement, category,
168                            PortletDataContext.REFERENCE_TYPE_DEPENDENCY, false);
169    
170                    portletDataContext.addPermissions(
171                            AssetCategory.class, category.getCategoryId());
172    
173                    portletDataContext.addZipEntry(categoryPath, category);
174            }
175    
176            @Override
177            protected void doImportMissingReference(
178                            PortletDataContext portletDataContext, String uuid, long groupId,
179                            long categoryId)
180                    throws Exception {
181    
182                    AssetCategory existingCategory = fetchMissingReference(uuid, groupId);
183    
184                    Map<Long, Long> categoryIds =
185                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
186                                    AssetCategory.class);
187    
188                    categoryIds.put(categoryId, existingCategory.getCategoryId());
189            }
190    
191            @Override
192            protected void doImportStagedModel(
193                            PortletDataContext portletDataContext, AssetCategory category)
194                    throws Exception {
195    
196                    long userId = portletDataContext.getUserId(category.getUserUuid());
197    
198                    Map<Long, Long> vocabularyIds =
199                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
200                                    AssetVocabulary.class);
201    
202                    long vocabularyId = MapUtil.getLong(
203                            vocabularyIds, category.getVocabularyId(),
204                            category.getVocabularyId());
205    
206                    Map<Long, Long> categoryIds =
207                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
208                                    AssetCategory.class);
209    
210                    long parentCategoryId = MapUtil.getLong(
211                            categoryIds, category.getParentCategoryId(),
212                            category.getParentCategoryId());
213    
214                    Element categoryElement = portletDataContext.getImportDataElement(
215                            category);
216    
217                    List<Element> propertyElements = categoryElement.elements("property");
218    
219                    String[] properties = new String[propertyElements.size()];
220    
221                    for (int i = 0; i < propertyElements.size(); i++) {
222                            Element propertyElement = propertyElements.get(i);
223    
224                            String key = propertyElement.attributeValue("key");
225                            String value = propertyElement.attributeValue("value");
226    
227                            properties[i] = key.concat(
228                                    AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR).concat(
229                                            value);
230                    }
231    
232                    ServiceContext serviceContext = createServiceContext(
233                            portletDataContext, category);
234    
235                    AssetCategory importedCategory = null;
236    
237                    AssetCategory existingCategory = fetchStagedModelByUuidAndGroupId(
238                            category.getUuid(), portletDataContext.getScopeGroupId());
239    
240                    if (existingCategory == null) {
241                            String name = getCategoryName(
242                                    null, portletDataContext.getScopeGroupId(), parentCategoryId,
243                                    category.getName(), vocabularyId, 2);
244    
245                            serviceContext.setUuid(category.getUuid());
246    
247                            importedCategory = AssetCategoryLocalServiceUtil.addCategory(
248                                    userId, portletDataContext.getScopeGroupId(), parentCategoryId,
249                                    getCategoryTitleMap(
250                                            portletDataContext.getScopeGroupId(), category, name),
251                                    category.getDescriptionMap(), vocabularyId, properties,
252                                    serviceContext);
253                    }
254                    else {
255                            String name = getCategoryName(
256                                    category.getUuid(), portletDataContext.getScopeGroupId(),
257                                    parentCategoryId, category.getName(), vocabularyId, 2);
258    
259                            importedCategory = AssetCategoryLocalServiceUtil.updateCategory(
260                                    userId, existingCategory.getCategoryId(), parentCategoryId,
261                                    getCategoryTitleMap(
262                                            portletDataContext.getScopeGroupId(), category, name),
263                                    category.getDescriptionMap(), vocabularyId, properties,
264                                    serviceContext);
265                    }
266    
267                    categoryIds.put(
268                            category.getCategoryId(), importedCategory.getCategoryId());
269    
270                    Map<String, String> categoryUuids =
271                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
272                                    AssetCategory.class + ".uuid");
273    
274                    categoryUuids.put(category.getUuid(), importedCategory.getUuid());
275    
276                    portletDataContext.importPermissions(
277                            AssetCategory.class, category.getCategoryId(),
278                            importedCategory.getCategoryId());
279            }
280    
281            protected String getCategoryName(
282                            String uuid, long groupId, long parentCategoryId, String name,
283                            long vocabularyId, int count)
284                    throws Exception {
285    
286                    AssetCategory category = AssetCategoryUtil.fetchByG_P_N_V_First(
287                            groupId, parentCategoryId, name, vocabularyId, null);
288    
289                    if ((category == null) ||
290                            (Validator.isNotNull(uuid) && uuid.equals(category.getUuid()))) {
291    
292                            return name;
293                    }
294    
295                    name = StringUtil.appendParentheticalSuffix(name, count);
296    
297                    return getCategoryName(
298                            uuid, groupId, parentCategoryId, name, vocabularyId, ++count);
299            }
300    
301            protected Map<Locale, String> getCategoryTitleMap(
302                            long groupId, AssetCategory category, String name)
303                    throws PortalException {
304    
305                    Map<Locale, String> titleMap = category.getTitleMap();
306    
307                    if (titleMap == null) {
308                            titleMap = new HashMap<>();
309                    }
310    
311                    titleMap.put(PortalUtil.getSiteDefaultLocale(groupId), name);
312    
313                    return titleMap;
314            }
315    
316    }