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.StringPool;
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.AssetVocabulary;
027    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
028    import com.liferay.portlet.asset.service.persistence.AssetVocabularyUtil;
029    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
030    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
031    import com.liferay.portlet.exportimport.lar.PortletDataContext;
032    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
033    
034    import java.util.HashMap;
035    import java.util.List;
036    import java.util.Locale;
037    import java.util.Map;
038    
039    /**
040     * @author Zsolt Berentey
041     * @author Gergely Mathe
042     * @author Mate Thurzo
043     */
044    @OSGiBeanProperties
045    public class AssetVocabularyStagedModelDataHandler
046            extends BaseStagedModelDataHandler<AssetVocabulary> {
047    
048            public static final String[] CLASS_NAMES =
049                    {AssetVocabulary.class.getName()};
050    
051            @Override
052            public void deleteStagedModel(AssetVocabulary vocabulary)
053                    throws PortalException {
054    
055                    AssetVocabularyLocalServiceUtil.deleteVocabulary(vocabulary);
056            }
057    
058            @Override
059            public void deleteStagedModel(
060                            String uuid, long groupId, String className, String extraData)
061                    throws PortalException {
062    
063                    AssetVocabulary vocabulary = fetchStagedModelByUuidAndGroupId(
064                            uuid, groupId);
065    
066                    if (vocabulary != null) {
067                            deleteStagedModel(vocabulary);
068                    }
069            }
070    
071            @Override
072            public AssetVocabulary fetchStagedModelByUuidAndGroupId(
073                    String uuid, long groupId) {
074    
075                    return AssetVocabularyLocalServiceUtil.
076                            fetchAssetVocabularyByUuidAndGroupId(uuid, groupId);
077            }
078    
079            @Override
080            public List<AssetVocabulary> fetchStagedModelsByUuidAndCompanyId(
081                    String uuid, long companyId) {
082    
083                    return AssetVocabularyLocalServiceUtil.
084                            getAssetVocabulariesByUuidAndCompanyId(
085                                    uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
086                                    new StagedModelModifiedDateComparator<AssetVocabulary>());
087            }
088    
089            @Override
090            public String[] getClassNames() {
091                    return CLASS_NAMES;
092            }
093    
094            @Override
095            public String getDisplayName(AssetVocabulary vocabulary) {
096                    return vocabulary.getTitleCurrentValue();
097            }
098    
099            protected ServiceContext createServiceContext(
100                    PortletDataContext portletDataContext, AssetVocabulary vocabulary) {
101    
102                    ServiceContext serviceContext = new ServiceContext();
103    
104                    serviceContext.setAddGroupPermissions(true);
105                    serviceContext.setAddGuestPermissions(true);
106                    serviceContext.setCreateDate(vocabulary.getCreateDate());
107                    serviceContext.setModifiedDate(vocabulary.getModifiedDate());
108                    serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());
109    
110                    return serviceContext;
111            }
112    
113            @Override
114            protected void doExportStagedModel(
115                            PortletDataContext portletDataContext, AssetVocabulary vocabulary)
116                    throws Exception {
117    
118                    Element vocabularyElement = portletDataContext.getExportDataElement(
119                            vocabulary);
120    
121                    String vocabularyPath = ExportImportPathUtil.getModelPath(vocabulary);
122    
123                    vocabularyElement.addAttribute("path", vocabularyPath);
124    
125                    vocabulary.setUserUuid(vocabulary.getUserUuid());
126    
127                    portletDataContext.addReferenceElement(
128                            vocabulary, vocabularyElement, vocabulary,
129                            PortletDataContext.REFERENCE_TYPE_DEPENDENCY, false);
130    
131                    portletDataContext.addPermissions(
132                            AssetVocabulary.class, vocabulary.getVocabularyId());
133    
134                    portletDataContext.addZipEntry(vocabularyPath, vocabulary);
135            }
136    
137            @Override
138            protected void doImportMissingReference(
139                            PortletDataContext portletDataContext, String uuid, long groupId,
140                            long vocabularyId)
141                    throws Exception {
142    
143                    AssetVocabulary existingVocabulary = fetchMissingReference(
144                            uuid, groupId);
145    
146                    Map<Long, Long> vocabularyIds =
147                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
148                                    AssetVocabulary.class);
149    
150                    vocabularyIds.put(vocabularyId, existingVocabulary.getVocabularyId());
151            }
152    
153            @Override
154            protected void doImportStagedModel(
155                            PortletDataContext portletDataContext, AssetVocabulary vocabulary)
156                    throws Exception {
157    
158                    long userId = portletDataContext.getUserId(vocabulary.getUserUuid());
159    
160                    ServiceContext serviceContext = createServiceContext(
161                            portletDataContext, vocabulary);
162    
163                    AssetVocabulary importedVocabulary = null;
164    
165                    AssetVocabulary existingVocabulary = fetchStagedModelByUuidAndGroupId(
166                            vocabulary.getUuid(), portletDataContext.getScopeGroupId());
167    
168                    if (existingVocabulary == null) {
169                            String name = getVocabularyName(
170                                    null, portletDataContext.getScopeGroupId(),
171                                    vocabulary.getName(), 2);
172    
173                            serviceContext.setUuid(vocabulary.getUuid());
174    
175                            importedVocabulary = AssetVocabularyLocalServiceUtil.addVocabulary(
176                                    userId, portletDataContext.getScopeGroupId(), StringPool.BLANK,
177                                    getVocabularyTitleMap(
178                                            portletDataContext.getScopeGroupId(), vocabulary, name),
179                                    vocabulary.getDescriptionMap(), vocabulary.getSettings(),
180                                    serviceContext);
181                    }
182                    else {
183                            String name = getVocabularyName(
184                                    vocabulary.getUuid(), portletDataContext.getScopeGroupId(),
185                                    vocabulary.getName(), 2);
186    
187                            importedVocabulary =
188                                    AssetVocabularyLocalServiceUtil.updateVocabulary(
189                                            existingVocabulary.getVocabularyId(), StringPool.BLANK,
190                                            getVocabularyTitleMap(
191                                                    portletDataContext.getScopeGroupId(), vocabulary, name),
192                                            vocabulary.getDescriptionMap(), vocabulary.getSettings(),
193                                            serviceContext);
194                    }
195    
196                    Map<Long, Long> vocabularyIds =
197                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
198                                    AssetVocabulary.class);
199    
200                    vocabularyIds.put(
201                            vocabulary.getVocabularyId(), importedVocabulary.getVocabularyId());
202    
203                    portletDataContext.importPermissions(
204                            AssetVocabulary.class, vocabulary.getVocabularyId(),
205                            importedVocabulary.getVocabularyId());
206            }
207    
208            protected String getVocabularyName(
209                            String uuid, long groupId, String name, int count)
210                    throws Exception {
211    
212                    AssetVocabulary vocabulary = AssetVocabularyUtil.fetchByG_N(
213                            groupId, name);
214    
215                    if (vocabulary == null) {
216                            return name;
217                    }
218    
219                    if (Validator.isNotNull(uuid) && uuid.equals(vocabulary.getUuid())) {
220                            return name;
221                    }
222    
223                    name = StringUtil.appendParentheticalSuffix(name, count);
224    
225                    return getVocabularyName(uuid, groupId, name, ++count);
226            }
227    
228            protected Map<Locale, String> getVocabularyTitleMap(
229                            long groupId, AssetVocabulary vocabulary, String name)
230                    throws PortalException {
231    
232                    Map<Locale, String> titleMap = vocabulary.getTitleMap();
233    
234                    if (titleMap == null) {
235                            titleMap = new HashMap<>();
236                    }
237    
238                    titleMap.put(PortalUtil.getSiteDefaultLocale(groupId), name);
239    
240                    return titleMap;
241            }
242    
243    }