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