001
014
015 package com.liferay.portlet.documentlibrary.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.ArrayUtil;
020 import com.liferay.portal.kernel.util.LocaleUtil;
021 import com.liferay.portal.kernel.util.LocalizationUtil;
022 import com.liferay.portal.kernel.util.OrderByComparator;
023 import com.liferay.portal.kernel.util.ParamUtil;
024 import com.liferay.portal.kernel.util.SortedArrayList;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portal.util.PortalUtil;
030 import com.liferay.portlet.documentlibrary.DuplicateFileEntryTypeException;
031 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
032 import com.liferay.portlet.documentlibrary.NoSuchMetadataSetException;
033 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034 import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
035 import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
036 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
037 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
038 import com.liferay.portlet.documentlibrary.model.DLFolder;
039 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
040 import com.liferay.portlet.documentlibrary.service.base.DLFileEntryTypeLocalServiceBaseImpl;
041 import com.liferay.portlet.documentlibrary.util.DLUtil;
042 import com.liferay.portlet.dynamicdatamapping.StructureXsdException;
043 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
044 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
045
046 import java.util.ArrayList;
047 import java.util.Date;
048 import java.util.HashMap;
049 import java.util.List;
050 import java.util.Locale;
051 import java.util.Map;
052
053
057 public class DLFileEntryTypeLocalServiceImpl
058 extends DLFileEntryTypeLocalServiceBaseImpl {
059
060 public DLFileEntryType addFileEntryType(
061 long userId, long groupId, String name, String description,
062 long[] ddmStructureIds, ServiceContext serviceContext)
063 throws PortalException, SystemException {
064
065 User user = userPersistence.findByPrimaryKey(userId);
066
067 String fileEntryTypeUuid = serviceContext.getUuid();
068
069 if (Validator.isNull(fileEntryTypeUuid)) {
070 fileEntryTypeUuid = PortalUUIDUtil.generate();
071 }
072
073 long fileEntryTypeId = counterLocalService.increment();
074
075 long ddmStructureId = updateDDMStructure(
076 userId, fileEntryTypeUuid, fileEntryTypeId, groupId, name,
077 description, serviceContext);
078
079 if (ddmStructureId > 0) {
080 ddmStructureIds = ArrayUtil.append(ddmStructureIds, ddmStructureId);
081 }
082
083 Date now = new Date();
084
085 validate(fileEntryTypeId, groupId, name, ddmStructureIds);
086
087 DLFileEntryType dlFileEntryType = dlFileEntryTypePersistence.create(
088 fileEntryTypeId);
089
090 dlFileEntryType.setUuid(fileEntryTypeUuid);
091 dlFileEntryType.setGroupId(groupId);
092 dlFileEntryType.setCompanyId(user.getCompanyId());
093 dlFileEntryType.setUserId(user.getUserId());
094 dlFileEntryType.setUserName(user.getFullName());
095 dlFileEntryType.setCreateDate(serviceContext.getCreateDate(now));
096 dlFileEntryType.setModifiedDate(serviceContext.getModifiedDate(now));
097 dlFileEntryType.setName(name);
098 dlFileEntryType.setDescription(description);
099
100 dlFileEntryTypePersistence.update(dlFileEntryType);
101
102 dlFileEntryTypePersistence.addDDMStructures(
103 fileEntryTypeId, ddmStructureIds);
104
105 if (serviceContext.isAddGroupPermissions() ||
106 serviceContext.isAddGuestPermissions()) {
107
108 addFileEntryTypeResources(
109 dlFileEntryType, serviceContext.isAddGroupPermissions(),
110 serviceContext.isAddGuestPermissions());
111 }
112 else {
113 addFileEntryTypeResources(
114 dlFileEntryType, serviceContext.getGroupPermissions(),
115 serviceContext.getGuestPermissions());
116 }
117
118 return dlFileEntryType;
119 }
120
121 public void cascadeFileEntryTypes(long userId, DLFolder dlFolder)
122 throws PortalException, SystemException {
123
124 List<DLFileEntryType> dlFileEntryTypes = getFolderFileEntryTypes(
125 new long[] {dlFolder.getGroupId()}, dlFolder.getFolderId(), true);
126
127 List<Long> fileEntryTypeIds = getFileEntryTypeIds(dlFileEntryTypes);
128
129 long defaultFileEntryTypeId = getDefaultFileEntryTypeId(
130 dlFolder.getFolderId());
131
132 ServiceContext serviceContext = new ServiceContext();
133
134 serviceContext.setCompanyId(dlFolder.getCompanyId());
135 serviceContext.setScopeGroupId(dlFolder.getGroupId());
136 serviceContext.setUserId(userId);
137
138 cascadeFileEntryTypes(
139 dlFolder.getGroupId(), dlFolder.getFolderId(),
140 defaultFileEntryTypeId, fileEntryTypeIds, serviceContext);
141 }
142
143 public void deleteFileEntryType(DLFileEntryType dlFileEntryType)
144 throws PortalException, SystemException {
145
146 DDMStructure ddmStructure = ddmStructureLocalService.fetchStructure(
147 dlFileEntryType.getGroupId(),
148 PortalUtil.getClassNameId(DLFileEntry.class),
149 DLUtil.getDDMStructureKey(dlFileEntryType));
150
151 if (ddmStructure == null) {
152 ddmStructure = ddmStructureLocalService.fetchStructure(
153 dlFileEntryType.getGroupId(),
154 PortalUtil.getClassNameId(DLFileEntry.class),
155 DLUtil.getDeprecatedDDMStructureKey(dlFileEntryType));
156 }
157
158 if (ddmStructure != null) {
159 ddmStructureLocalService.deleteStructure(
160 ddmStructure.getStructureId());
161 }
162
163 dlFileEntryTypePersistence.remove(dlFileEntryType);
164 }
165
166 public void deleteFileEntryType(long fileEntryTypeId)
167 throws PortalException, SystemException {
168
169 DLFileEntryType dlFileEntryType =
170 dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
171
172 deleteFileEntryType(dlFileEntryType);
173 }
174
175 public void deleteFileEntryTypes(long groupId)
176 throws PortalException, SystemException {
177
178 List<DLFileEntryType> dlFileEntryTypes =
179 dlFileEntryTypePersistence.findByGroupId(groupId);
180
181 for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
182 deleteFileEntryType(dlFileEntryType);
183 }
184 }
185
186 public DLFileEntryType fetchFileEntryType(long fileEntryTypeId)
187 throws SystemException {
188
189 return dlFileEntryTypePersistence.fetchByPrimaryKey(fileEntryTypeId);
190 }
191
192 public long getDefaultFileEntryTypeId(long folderId)
193 throws PortalException, SystemException {
194
195 folderId = getFileEntryTypesPrimaryFolderId(folderId);
196
197 if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
198 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
199
200 return dlFolder.getDefaultFileEntryTypeId();
201 }
202 else {
203 return 0;
204 }
205 }
206
207 public DLFileEntryType getFileEntryType(long fileEntryTypeId)
208 throws PortalException, SystemException {
209
210 return dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
211 }
212
213 public DLFileEntryType getFileEntryType(long groupId, String name)
214 throws PortalException, SystemException {
215
216 return dlFileEntryTypePersistence.findByG_N(groupId, name);
217 }
218
219 public List<DLFileEntryType> getFileEntryTypes(long[] groupIds)
220 throws SystemException {
221
222 return dlFileEntryTypePersistence.findByGroupId(groupIds);
223 }
224
225 public List<DLFileEntryType> getFolderFileEntryTypes(
226 long[] groupIds, long folderId, boolean inherited)
227 throws PortalException, SystemException {
228
229 if (!inherited) {
230 return dlFolderPersistence.getDLFileEntryTypes(folderId);
231 }
232
233 List<DLFileEntryType> dlFileEntryTypes = null;
234
235 folderId = getFileEntryTypesPrimaryFolderId(folderId);
236
237 if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
238 dlFileEntryTypes = dlFolderPersistence.getDLFileEntryTypes(
239 folderId);
240 }
241
242 if ((dlFileEntryTypes == null) || dlFileEntryTypes.isEmpty()) {
243 dlFileEntryTypes = new ArrayList<DLFileEntryType>(
244 getFileEntryTypes(groupIds));
245
246 DLFileEntryType dlFileEntryType =
247 dlFileEntryTypePersistence.fetchByPrimaryKey(
248 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);
249
250 dlFileEntryTypes.add(0, dlFileEntryType);
251 }
252
253 return dlFileEntryTypes;
254 }
255
256 public List<DLFileEntryType> search(
257 long companyId, long[] groupIds, String keywords,
258 boolean includeBasicFileEntryType, int start, int end,
259 OrderByComparator orderByComparator)
260 throws SystemException {
261
262 return dlFileEntryTypeFinder.findByKeywords(
263 companyId, groupIds, keywords, includeBasicFileEntryType, start,
264 end, orderByComparator);
265 }
266
267 public int searchCount(
268 long companyId, long[] groupIds, String keywords,
269 boolean includeBasicFileEntryType)
270 throws SystemException {
271
272 return dlFileEntryTypeFinder.countByKeywords(
273 companyId, groupIds, keywords, includeBasicFileEntryType);
274 }
275
276 public void unsetFolderFileEntryTypes(long folderId)
277 throws SystemException {
278
279 List<DLFileEntryType> dlFileEntryTypes =
280 dlFolderPersistence.getDLFileEntryTypes(folderId);
281
282 for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
283 dlFolderPersistence.removeDLFileEntryType(
284 folderId, dlFileEntryType);
285 }
286 }
287
288 public DLFileEntry updateFileEntryFileEntryType(
289 DLFileEntry dlFileEntry, ServiceContext serviceContext)
290 throws PortalException, SystemException {
291
292 long groupId = serviceContext.getScopeGroupId();
293 long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
294
295 DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(
296 dlFileEntry.getFolderId());
297
298 if (dlFolder != null) {
299 groupId = dlFolder.getGroupId();
300 folderId = dlFolder.getFolderId();
301 }
302
303 List<DLFileEntryType> dlFileEntryTypes = getFolderFileEntryTypes(
304 PortalUtil.getSiteAndCompanyGroupIds(groupId), folderId, true);
305
306 List<Long> fileEntryTypeIds = getFileEntryTypeIds(dlFileEntryTypes);
307
308 if (fileEntryTypeIds.contains(dlFileEntry.getFileEntryTypeId())) {
309 return dlFileEntry;
310 }
311
312 long defaultFileEntryTypeId = getDefaultFileEntryTypeId(folderId);
313
314 DLFileVersion dlFileVersion =
315 dlFileVersionLocalService.getLatestFileVersion(
316 dlFileEntry.getFileEntryId(), true);
317
318 if (dlFileVersion.isPending()) {
319 workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
320 dlFileVersion.getCompanyId(), dlFileEntry.getGroupId(),
321 DLFileEntry.class.getName(), dlFileVersion.getFileVersionId());
322 }
323
324 return dlFileEntryLocalService.updateFileEntry(
325 serviceContext.getUserId(), dlFileEntry.getFileEntryId(), null,
326 null, null, null, null, false, defaultFileEntryTypeId, null, null,
327 null, 0, serviceContext);
328 }
329
330 public void updateFileEntryType(
331 long userId, long fileEntryTypeId, String name, String description,
332 long[] ddmStructureIds, ServiceContext serviceContext)
333 throws PortalException, SystemException {
334
335 DLFileEntryType dlFileEntryType =
336 dlFileEntryTypePersistence.findByPrimaryKey(fileEntryTypeId);
337
338 long ddmStructureId = updateDDMStructure(
339 userId, dlFileEntryType.getUuid(), fileEntryTypeId,
340 dlFileEntryType.getGroupId(), name, description, serviceContext);
341
342 if (ddmStructureId > 0) {
343 ddmStructureIds = ArrayUtil.append(ddmStructureIds, ddmStructureId);
344 }
345
346 validate(
347 fileEntryTypeId, dlFileEntryType.getGroupId(), name,
348 ddmStructureIds);
349
350 dlFileEntryType.setModifiedDate(serviceContext.getModifiedDate(null));
351 dlFileEntryType.setName(name);
352 dlFileEntryType.setDescription(description);
353
354 dlFileEntryTypePersistence.update(dlFileEntryType);
355
356 dlFileEntryTypePersistence.setDDMStructures(
357 fileEntryTypeId, ddmStructureIds);
358 }
359
360 public void updateFolderFileEntryTypes(
361 DLFolder dlFolder, List<Long> fileEntryTypeIds,
362 long defaultFileEntryTypeId, ServiceContext serviceContext)
363 throws PortalException, SystemException {
364
365 List<Long> originalFileEntryTypeIds = getFileEntryTypeIds(
366 dlFolderPersistence.getDLFileEntryTypes(dlFolder.getFolderId()));
367
368 if (fileEntryTypeIds.equals(originalFileEntryTypeIds)) {
369 return;
370 }
371
372 for (Long fileEntryTypeId : fileEntryTypeIds) {
373 if (!originalFileEntryTypeIds.contains(fileEntryTypeId)) {
374 dlFolderPersistence.addDLFileEntryType(
375 dlFolder.getFolderId(), fileEntryTypeId);
376 }
377 }
378
379 for (Long originalFileEntryTypeId : originalFileEntryTypeIds) {
380 if (!fileEntryTypeIds.contains(originalFileEntryTypeId)) {
381 dlFolderPersistence.removeDLFileEntryType(
382 dlFolder.getFolderId(), originalFileEntryTypeId);
383
384 workflowDefinitionLinkLocalService.deleteWorkflowDefinitionLink(
385 dlFolder.getCompanyId(), dlFolder.getGroupId(),
386 DLFolder.class.getName(), dlFolder.getFolderId(),
387 originalFileEntryTypeId);
388 }
389 }
390 }
391
392 protected void addFileEntryTypeResources(
393 DLFileEntryType dlFileEntryType, boolean addGroupPermissions,
394 boolean addGuestPermissions)
395 throws PortalException, SystemException {
396
397 resourceLocalService.addResources(
398 dlFileEntryType.getCompanyId(), dlFileEntryType.getGroupId(),
399 dlFileEntryType.getUserId(), DLFileEntryType.class.getName(),
400 dlFileEntryType.getFileEntryTypeId(), false, addGroupPermissions,
401 addGuestPermissions);
402 }
403
404 protected void addFileEntryTypeResources(
405 DLFileEntryType dlFileEntryType, String[] groupPermissions,
406 String[] guestPermissions)
407 throws PortalException, SystemException {
408
409 resourceLocalService.addModelResources(
410 dlFileEntryType.getCompanyId(), dlFileEntryType.getGroupId(),
411 dlFileEntryType.getUserId(), DLFileEntryType.class.getName(),
412 dlFileEntryType.getFileEntryTypeId(), groupPermissions,
413 guestPermissions);
414 }
415
416 protected void cascadeFileEntryTypes(
417 long groupId, long folderId, long defaultFileEntryTypeId,
418 List<Long> fileEntryTypeIds, ServiceContext serviceContext)
419 throws PortalException, SystemException {
420
421 List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(
422 groupId, folderId);
423
424 for (DLFileEntry dlFileEntry : dlFileEntries) {
425 Long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();
426
427 if (fileEntryTypeIds.contains(fileEntryTypeId)) {
428 continue;
429 }
430
431 DLFileVersion dlFileVersion =
432 dlFileVersionLocalService.getLatestFileVersion(
433 dlFileEntry.getFileEntryId(), true);
434
435 if (dlFileVersion.isPending()) {
436 workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
437 dlFileVersion.getCompanyId(), groupId,
438 DLFileEntry.class.getName(),
439 dlFileVersion.getFileVersionId());
440 }
441
442 dlFileEntryService.updateFileEntry(
443 dlFileEntry.getFileEntryId(), null, null, null, null, null,
444 false, defaultFileEntryTypeId, null, null, null, 0,
445 serviceContext);
446 }
447
448 List<DLFolder> subFolders = dlFolderPersistence.findByG_M_P_H(
449 groupId, false, folderId, false);
450
451 for (DLFolder subFolder : subFolders) {
452 long subFolderId = subFolder.getFolderId();
453
454 if (subFolder.isOverrideFileEntryTypes()) {
455 continue;
456 }
457
458 cascadeFileEntryTypes(
459 groupId, subFolderId, defaultFileEntryTypeId, fileEntryTypeIds,
460 serviceContext);
461 }
462 }
463
464 protected void fixDDMStructureKey(
465 String fileEntryTypeUuid, long fileEntryTypeId, long groupId)
466 throws SystemException {
467
468 DDMStructure ddmStructure = ddmStructureLocalService.fetchStructure(
469 groupId, PortalUtil.getClassNameId(DLFileEntry.class),
470 DLUtil.getDeprecatedDDMStructureKey(fileEntryTypeId));
471
472 if (ddmStructure != null) {
473 ddmStructure.setStructureKey(
474 DLUtil.getDDMStructureKey(fileEntryTypeUuid));
475
476 ddmStructureLocalService.updateDDMStructure(ddmStructure);
477 }
478 }
479
480 protected List<Long> getFileEntryTypeIds(
481 List<DLFileEntryType> dlFileEntryTypes) {
482
483 List<Long> fileEntryTypeIds = new SortedArrayList<Long>();
484
485 for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
486 fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
487 }
488
489 return fileEntryTypeIds;
490 }
491
492 protected long getFileEntryTypesPrimaryFolderId(long folderId)
493 throws NoSuchFolderException, SystemException {
494
495 while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
496 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
497
498 if (dlFolder.isOverrideFileEntryTypes()) {
499 break;
500 }
501
502 folderId = dlFolder.getParentFolderId();
503 }
504
505 return folderId;
506 }
507
508 protected long updateDDMStructure(
509 long userId, String fileEntryTypeUuid, long fileEntryTypeId,
510 long groupId, String name, String description,
511 ServiceContext serviceContext)
512 throws PortalException, SystemException {
513
514 fixDDMStructureKey(fileEntryTypeUuid, fileEntryTypeId, groupId);
515
516 String ddmStructureKey = DLUtil.getDDMStructureKey(fileEntryTypeUuid);
517
518 Map<Locale, String> nameMap = new HashMap<Locale, String>();
519
520 Locale locale = serviceContext.getLocale();
521
522 nameMap.put(locale, name);
523
524 Locale defaultLocale = LocaleUtil.getDefault();
525
526 nameMap.put(defaultLocale, name);
527
528 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
529
530 descriptionMap.put(locale, description);
531 descriptionMap.put(defaultLocale, description);
532
533 String xsd = ParamUtil.getString(serviceContext, "xsd");
534
535 DDMStructure ddmStructure = ddmStructureLocalService.fetchStructure(
536 groupId, PortalUtil.getClassNameId(DLFileEntryMetadata.class),
537 ddmStructureKey);
538
539 if ((ddmStructure != null) && Validator.isNull(xsd)) {
540 xsd = ddmStructure.getXsd();
541 }
542
543 Locale[] contentAvailableLocales = LocaleUtil.fromLanguageIds(
544 LocalizationUtil.getAvailableLocales(xsd));
545
546 for (Locale contentAvailableLocale : contentAvailableLocales) {
547 nameMap.put(contentAvailableLocale, name);
548
549 descriptionMap.put(contentAvailableLocale, description);
550 }
551
552 try {
553 if (ddmStructure == null) {
554 ddmStructure = ddmStructureLocalService.addStructure(
555 userId, groupId,
556 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
557 PortalUtil.getClassNameId(DLFileEntryMetadata.class),
558 ddmStructureKey, nameMap, descriptionMap, xsd, "xml",
559 DDMStructureConstants.TYPE_AUTO, serviceContext);
560 }
561 else {
562 ddmStructure = ddmStructureLocalService.updateStructure(
563 ddmStructure.getStructureId(),
564 ddmStructure.getParentStructureId(), nameMap,
565 descriptionMap, xsd, serviceContext);
566 }
567
568 return ddmStructure.getStructureId();
569 }
570 catch (StructureXsdException sxe) {
571 if (ddmStructure != null) {
572 ddmStructureLocalService.deleteStructure(
573 ddmStructure.getStructureId());
574 }
575 }
576
577 return 0;
578 }
579
580 protected void validate(
581 long fileEntryTypeId, long groupId, String fileEntryTypeName,
582 long[] ddmStructureIds)
583 throws PortalException, SystemException {
584
585 DLFileEntryType dlFileEntryType = dlFileEntryTypePersistence.fetchByG_N(
586 groupId, fileEntryTypeName);
587
588 if ((dlFileEntryType != null) &&
589 (dlFileEntryType.getFileEntryTypeId() != fileEntryTypeId)) {
590
591 throw new DuplicateFileEntryTypeException(fileEntryTypeName);
592 }
593
594 if (ddmStructureIds.length == 0) {
595 throw new NoSuchMetadataSetException();
596 }
597
598 for (long ddmStructureId : ddmStructureIds) {
599 DDMStructure ddmStructure =
600 ddmStructurePersistence.fetchByPrimaryKey(ddmStructureId);
601
602 if (ddmStructure == null) {
603 throw new NoSuchMetadataSetException();
604 }
605 }
606 }
607
608 }