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