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