001
014
015 package com.liferay.portlet.documentlibrary.util.test;
016
017 import com.liferay.portal.kernel.repository.model.FileEntry;
018 import com.liferay.portal.kernel.repository.model.Folder;
019 import com.liferay.portal.kernel.util.Constants;
020 import com.liferay.portal.kernel.util.ContentTypes;
021 import com.liferay.portal.kernel.util.ListUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.UnicodeProperties;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.kernel.workflow.WorkflowConstants;
026 import com.liferay.portal.model.Repository;
027 import com.liferay.portal.model.RepositoryEntry;
028 import com.liferay.portal.service.RepositoryEntryLocalServiceUtil;
029 import com.liferay.portal.service.RepositoryLocalServiceUtil;
030 import com.liferay.portal.service.ServiceContext;
031 import com.liferay.portal.util.PortalUtil;
032 import com.liferay.portal.util.PortletKeys;
033 import com.liferay.portal.util.test.RandomTestUtil;
034 import com.liferay.portal.util.test.ServiceContextTestUtil;
035 import com.liferay.portal.util.test.TestPropsValues;
036 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
037 import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
038 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
039 import com.liferay.portlet.documentlibrary.model.DLFileRank;
040 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
041 import com.liferay.portlet.documentlibrary.model.DLFolder;
042 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
043 import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
044 import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
045 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
046 import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
047 import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
048 import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
049
050 import java.io.Serializable;
051
052 import java.util.HashMap;
053 import java.util.Map;
054
055
058 public abstract class DLAppTestUtil {
059
060 public static DLFileEntryType addDLFileEntryType(
061 long groupId, long ddmStructureId)
062 throws Exception {
063
064 long userId = TestPropsValues.getUserId();
065 String name = RandomTestUtil.randomString();
066 String description = RandomTestUtil.randomString();
067
068 ServiceContext serviceContext =
069 ServiceContextTestUtil.getServiceContext(groupId);
070
071 return addDLFileEntryType(
072 userId, groupId, name, description, new long[] {ddmStructureId},
073 serviceContext);
074 }
075
076 public static DLFileEntryType addDLFileEntryType(
077 long userId, long groupId, String name, String description,
078 long[] ddmStructureIds, ServiceContext serviceContext)
079 throws Exception {
080
081 return DLFileEntryTypeLocalServiceUtil.addFileEntryType(
082 userId, groupId, name, description, ddmStructureIds,
083 serviceContext);
084 }
085
086 public static DLFileRank addDLFileRank(long groupId, long fileEntryId)
087 throws Exception {
088
089 ServiceContext serviceContext =
090 ServiceContextTestUtil.getServiceContext(groupId);
091
092 return DLAppLocalServiceUtil.addFileRank(
093 groupId, TestPropsValues.getCompanyId(),
094 TestPropsValues.getUserId(), fileEntryId, serviceContext);
095 }
096
097 public static DLFileShortcut addDLFileShortcut(
098 FileEntry fileEntry, long groupId, long folderId)
099 throws Exception {
100
101 ServiceContext serviceContext =
102 ServiceContextTestUtil.getServiceContext(groupId);
103
104 return DLAppServiceUtil.addFileShortcut(
105 groupId, folderId, fileEntry.getFileEntryId(), serviceContext);
106 }
107
108 public static DLFileShortcut addDLFileShortcut(
109 long groupId, FileEntry fileEntry)
110 throws Exception {
111
112 return addDLFileShortcut(fileEntry, groupId, fileEntry.getFolderId());
113 }
114
115 public static FileEntry addFileEntry(
116 long groupId, long parentFolderId, boolean rootFolder,
117 String fileName)
118 throws Exception {
119
120 return addFileEntry(
121 groupId, parentFolderId, rootFolder, fileName, fileName);
122 }
123
124 public static FileEntry addFileEntry(
125 long groupId, long parentFolderId, boolean rootFolder,
126 String sourceFileName, String title)
127 throws Exception {
128
129 long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
130
131 if (!rootFolder) {
132 folderId = parentFolderId;
133 }
134
135 return addFileEntry(groupId, folderId, sourceFileName, title);
136 }
137
138 public static FileEntry addFileEntry(
139 long groupId, long repositoryId, long folderId)
140 throws Exception {
141
142 return addFileEntry(
143 groupId, repositoryId, folderId, RandomTestUtil.randomString(),
144 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL, false, true);
145 }
146
147 public static FileEntry addFileEntry(
148 long groupId, long repositoryId, long folderId,
149 String sourceFileName, long fileEntryTypeId,
150 boolean workflowEnabled, boolean approved)
151 throws Exception {
152
153 ServiceContext serviceContext =
154 ServiceContextTestUtil.getServiceContext(groupId);
155
156 if (fileEntryTypeId !=
157 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL) {
158
159 serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);
160 }
161
162 serviceContext.setCommand(Constants.ADD);
163 serviceContext.setLayoutFullURL("http:
164
165 if (workflowEnabled && !approved) {
166 serviceContext.setWorkflowAction(
167 WorkflowConstants.ACTION_SAVE_DRAFT);
168 }
169 else {
170 serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
171 }
172
173 FileEntry fileEntry = addFileEntry(
174 repositoryId, folderId, sourceFileName, ContentTypes.TEXT_PLAIN,
175 sourceFileName, null, serviceContext.getWorkflowAction(),
176 serviceContext);
177
178 if (workflowEnabled && approved) {
179 updateStatus(fileEntry, serviceContext);
180 }
181
182 return fileEntry;
183 }
184
185 public static FileEntry addFileEntry(
186 long groupId, long repositoryId, long folderId,
187 String sourceFileName, String mimeType, String title, byte[] bytes,
188 int workflowAction)
189 throws Exception {
190
191 long fileEntryTypeId =
192 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT;
193
194 return addFileEntry(
195 groupId, repositoryId, folderId, sourceFileName, mimeType, title,
196 bytes, fileEntryTypeId, workflowAction);
197 }
198
199 public static FileEntry addFileEntry(
200 long groupId, long repositoryId, long folderId,
201 String sourceFileName, String mimeType, String title, byte[] bytes,
202 long fileEntryTypeId, int workflowAction)
203 throws Exception {
204
205 ServiceContext serviceContext =
206 ServiceContextTestUtil.getServiceContext(groupId);
207
208 serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);
209 serviceContext.setCommand(Constants.ADD);
210 serviceContext.setLayoutFullURL("http:
211
212 return addFileEntry(
213 repositoryId, folderId, sourceFileName, mimeType, title, bytes,
214 workflowAction, serviceContext);
215 }
216
217 public static FileEntry addFileEntry(
218 long groupId, long folderId, String sourceFileName)
219 throws Exception {
220
221 return addFileEntry(groupId, folderId, sourceFileName, sourceFileName);
222 }
223
224 public static FileEntry addFileEntry(
225 long groupId, long folderId, String sourceFileName,
226 long fileEntryTypeId)
227 throws Exception {
228
229 return addFileEntry(
230 groupId, groupId, folderId, sourceFileName, fileEntryTypeId, true,
231 true);
232 }
233
234 public static FileEntry addFileEntry(
235 long groupId, long folderId, String sourceFileName, String title)
236 throws Exception {
237
238 return addFileEntry(
239 groupId, folderId, sourceFileName, title, null,
240 WorkflowConstants.ACTION_PUBLISH);
241 }
242
243 public static FileEntry addFileEntry(
244 long groupId, long folderId, String sourceFileName, String title,
245 boolean approved)
246 throws Exception {
247
248 int workflowAction = WorkflowConstants.ACTION_SAVE_DRAFT;
249
250 if (approved) {
251 workflowAction = WorkflowConstants.ACTION_PUBLISH;
252 }
253
254 return addFileEntry(
255 groupId, folderId, sourceFileName, title, null, workflowAction);
256 }
257
258 public static FileEntry addFileEntry(
259 long groupId, long folderId, String sourceFileName, String title,
260 byte[] bytes)
261 throws Exception {
262
263 return addFileEntry(
264 groupId, folderId, sourceFileName, title, bytes,
265 WorkflowConstants.ACTION_PUBLISH);
266 }
267
268 public static FileEntry addFileEntry(
269 long groupId, long folderId, String sourceFileName, String title,
270 byte[] bytes, int workflowAction)
271 throws Exception {
272
273 return addFileEntry(
274 groupId, folderId, sourceFileName, ContentTypes.TEXT_PLAIN, title,
275 bytes, workflowAction);
276 }
277
278 public static FileEntry addFileEntry(
279 long groupId, long folderId, String sourceFileName, String mimeType,
280 String title)
281 throws Exception {
282
283 return addFileEntry(
284 groupId, folderId, sourceFileName, mimeType, title, null,
285 WorkflowConstants.ACTION_PUBLISH);
286 }
287
288 public static FileEntry addFileEntry(
289 long groupId, long folderId, String sourceFileName, String mimeType,
290 String title, byte[] bytes, int workflowAction)
291 throws Exception {
292
293 long fileEntryTypeId =
294 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT;
295
296 return addFileEntry(
297 groupId, folderId, sourceFileName, mimeType, title, bytes,
298 fileEntryTypeId, workflowAction);
299 }
300
301 public static FileEntry addFileEntry(
302 long repositoryId, long folderId, String sourceFileName,
303 String mimeType, String title, byte[] bytes, int workflowAction,
304 ServiceContext serviceContext)
305 throws Exception {
306
307 if ((bytes == null) && Validator.isNotNull(sourceFileName)) {
308 bytes = _CONTENT.getBytes();
309 }
310
311 serviceContext = (ServiceContext)serviceContext.clone();
312
313 serviceContext.setWorkflowAction(workflowAction);
314
315 return DLAppServiceUtil.addFileEntry(
316 repositoryId, folderId, sourceFileName, mimeType, title,
317 StringPool.BLANK, StringPool.BLANK, bytes, serviceContext);
318 }
319
320 public static FileEntry addFileEntry(
321 long groupId, long folderId, String sourceFileName, String mimeType,
322 String title, byte[] bytes, long fileEntryTypeId,
323 int workflowAction)
324 throws Exception {
325
326 return addFileEntry(
327 groupId, groupId, folderId, sourceFileName, mimeType, title, bytes,
328 fileEntryTypeId, workflowAction);
329 }
330
331 public static FileEntry addFileEntry(
332 long folderId, String sourceFileName, String title,
333 boolean approved, ServiceContext serviceContext)
334 throws Exception {
335
336 int workflowAction = WorkflowConstants.ACTION_SAVE_DRAFT;
337
338 if (approved) {
339 workflowAction = WorkflowConstants.ACTION_PUBLISH;
340 }
341
342 return addFileEntry(
343 serviceContext.getScopeGroupId(), folderId, sourceFileName,
344 ContentTypes.TEXT_PLAIN, title, null, workflowAction,
345 serviceContext);
346 }
347
348 public static FileEntry addFileEntryWithWorkflow(
349 long groupId, long repositoryId, long folderId, boolean approved)
350 throws Exception {
351
352 return addFileEntry(
353 groupId, repositoryId, folderId, RandomTestUtil.randomString(),
354 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL, true, approved);
355 }
356
357 public static Folder addFolder(long groupId, long parentFolderId)
358 throws Exception {
359
360 return addFolder(
361 groupId, parentFolderId, RandomTestUtil.randomString(), false);
362 }
363
364 public static Folder addFolder(
365 long groupId, long repositoryId, long parentFolderId, String name)
366 throws Exception {
367
368 ServiceContext serviceContext =
369 ServiceContextTestUtil.getServiceContext(groupId);
370
371 return addFolder(
372 repositoryId, parentFolderId, name, false, serviceContext);
373 }
374
375 public static Folder addFolder(
376 long groupId, long parentFolderId, String name)
377 throws Exception {
378
379 return addFolder(groupId, parentFolderId, name, false);
380 }
381
382 public static Folder addFolder(
383 long groupId, long parentFolderId, String name,
384 boolean deleteExisting)
385 throws Exception {
386
387 ServiceContext serviceContext =
388 ServiceContextTestUtil.getServiceContext(groupId);
389
390 return addFolder(parentFolderId, name, deleteExisting, serviceContext);
391 }
392
393 public static Folder addFolder(
394 long repositoryId, long parentFolderId, String name,
395 boolean deleteExisting, ServiceContext serviceContext)
396 throws Exception {
397
398 String description = StringPool.BLANK;
399
400 if (deleteExisting) {
401 try {
402 DLAppServiceUtil.deleteFolder(
403 serviceContext.getScopeGroupId(), parentFolderId, name);
404 }
405 catch (NoSuchFolderException nsfe) {
406 }
407 }
408
409 return DLAppServiceUtil.addFolder(
410 repositoryId, parentFolderId, name, description, serviceContext);
411 }
412
413 public static Folder addFolder(
414 long parentFolderId, String name, boolean deleteExisting,
415 ServiceContext serviceContext)
416 throws Exception {
417
418 return addFolder(
419 serviceContext.getScopeGroupId(), parentFolderId, name,
420 deleteExisting, serviceContext);
421 }
422
423 public static Folder addFolder(
424 long parentFolderId, String name, ServiceContext serviceContext)
425 throws Exception {
426
427 return addFolder(parentFolderId, name, false, serviceContext);
428 }
429
430 public static Repository addRepository(long groupId) throws Exception {
431 long classNameId = PortalUtil.getClassNameId(
432 "com.liferay.portal.repository.liferayrepository." +
433 "LiferayRepository");
434
435 return addRepository(groupId, classNameId);
436 }
437
438 public static Repository addRepository(long groupId, long classNameId)
439 throws Exception {
440
441 long userId = TestPropsValues.getUserId();
442
443 Folder folder = addFolder(
444 groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
445 RandomTestUtil.randomString());
446
447 long parentFolderId = folder.getFolderId();
448
449 String name = RandomTestUtil.randomString();
450 String description = RandomTestUtil.randomString();
451 String portletId = PortletKeys.DOCUMENT_LIBRARY;
452 UnicodeProperties typeSettingsProperties = new UnicodeProperties();
453 boolean hidden = false;
454
455 ServiceContext serviceContext =
456 ServiceContextTestUtil.getServiceContext(groupId);
457
458 return addRepository(
459 userId, groupId, classNameId, parentFolderId, name, description,
460 portletId, typeSettingsProperties, hidden, serviceContext);
461 }
462
463 public static Repository addRepository(
464 long userId, long groupId, long classNameId, long parentFolderId,
465 String name, String description, String portletId,
466 UnicodeProperties typeSettingsProperties, boolean hidden,
467 ServiceContext serviceContext)
468 throws Exception {
469
470 return RepositoryLocalServiceUtil.addRepository(
471 userId, groupId, classNameId, parentFolderId, name, description,
472 portletId, typeSettingsProperties, hidden, serviceContext);
473 }
474
475 public static RepositoryEntry addRepositoryEntry(
476 long groupId, long repositoryId)
477 throws Exception {
478
479 long userId = TestPropsValues.getUserId();
480 String mappedId = RandomTestUtil.randomString();
481
482 ServiceContext serviceContext =
483 ServiceContextTestUtil.getServiceContext(groupId);
484
485 return addRepositoryEntry(
486 userId, groupId, repositoryId, mappedId, serviceContext);
487 }
488
489 public static RepositoryEntry addRepositoryEntry(
490 long userId, long groupId, long repositoryId, String mappedId,
491 ServiceContext serviceContext)
492 throws Exception {
493
494 return RepositoryEntryLocalServiceUtil.addRepositoryEntry(
495 userId, groupId, repositoryId, mappedId, serviceContext);
496 }
497
498 public static FileEntry updateFileEntry(
499 long groupId, long fileEntryId, boolean majorVersion)
500 throws Exception {
501
502 return updateFileEntry(
503 groupId, fileEntryId, RandomTestUtil.randomString(),
504 RandomTestUtil.randomString(), majorVersion, true, true);
505 }
506
507 public static FileEntry updateFileEntry(
508 long groupId, long fileEntryId, String sourceFileName, String title)
509 throws Exception {
510
511 return updateFileEntry(
512 groupId, fileEntryId, sourceFileName, title, false, false, false);
513 }
514
515 public static FileEntry updateFileEntry(
516 long groupId, long fileEntryId, String sourceFileName, String title,
517 boolean majorVersion, boolean workflowEnabled, boolean approved)
518 throws Exception {
519
520 ServiceContext serviceContext =
521 ServiceContextTestUtil.getServiceContext(groupId);
522
523 serviceContext.setCommand(Constants.UPDATE);
524 serviceContext.setLayoutFullURL("http:
525
526 return updateFileEntry(
527 groupId, fileEntryId, sourceFileName, ContentTypes.TEXT_PLAIN,
528 title, majorVersion, workflowEnabled, approved, serviceContext);
529 }
530
531 public static FileEntry updateFileEntry(
532 long groupId, long fileEntryId, String sourceFileName,
533 String mimeType, String title, boolean majorVersion,
534 boolean workflowEnabled, boolean approved,
535 ServiceContext serviceContext)
536 throws Exception {
537
538 String description = StringPool.BLANK;
539 String changeLog = StringPool.BLANK;
540
541 byte[] bytes = null;
542
543 if (Validator.isNotNull(sourceFileName)) {
544 String newContent = _CONTENT + "\n" + System.currentTimeMillis();
545
546 bytes = newContent.getBytes();
547 }
548
549 serviceContext = (ServiceContext)serviceContext.clone();
550
551 serviceContext.setAddGroupPermissions(true);
552 serviceContext.setAddGuestPermissions(true);
553 serviceContext.setScopeGroupId(groupId);
554
555 if (workflowEnabled && !approved) {
556 serviceContext.setWorkflowAction(
557 WorkflowConstants.ACTION_SAVE_DRAFT);
558 }
559 else {
560 serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
561 }
562
563 FileEntry fileEntry = DLAppServiceUtil.updateFileEntry(
564 fileEntryId, sourceFileName, mimeType, title, description,
565 changeLog, majorVersion, bytes, serviceContext);
566
567 if (workflowEnabled && approved) {
568 updateStatus(fileEntry, serviceContext);
569 }
570
571 return fileEntry;
572 }
573
574 public static FileEntry updateFileEntry(
575 long groupId, long fileEntryId, String sourceFileName,
576 String mimeType, String title, boolean majorVersion,
577 ServiceContext serviceContext)
578 throws Exception {
579
580 String description = StringPool.BLANK;
581 String changeLog = StringPool.BLANK;
582
583 byte[] bytes = null;
584
585 if (Validator.isNotNull(sourceFileName)) {
586 String newContent = _CONTENT + "\n" + System.currentTimeMillis();
587
588 bytes = newContent.getBytes();
589 }
590
591 serviceContext.setAddGroupPermissions(true);
592 serviceContext.setAddGuestPermissions(true);
593 serviceContext.setScopeGroupId(groupId);
594
595 return DLAppServiceUtil.updateFileEntry(
596 fileEntryId, sourceFileName, mimeType, title, description,
597 changeLog, majorVersion, bytes, serviceContext);
598 }
599
600 public static FileEntry updateFileEntryWithWorkflow(
601 long groupId, long fileEntryId, boolean majorVersion,
602 boolean approved)
603 throws Exception {
604
605 ServiceContext serviceContext =
606 ServiceContextTestUtil.getServiceContext(groupId);
607
608 serviceContext.setCommand(Constants.UPDATE);
609 serviceContext.setLayoutFullURL("http:
610
611 return updateFileEntry(
612 groupId, fileEntryId, RandomTestUtil.randomString(),
613 ContentTypes.TEXT_PLAIN, RandomTestUtil.randomString(),
614 majorVersion, true, approved, serviceContext);
615 }
616
617 public static void updateFolderFileEntryType(
618 Folder folder, long fileEntryTypeId)
619 throws Exception {
620
621 updateFolderFileEntryTypes(
622 folder, fileEntryTypeId, new long[] {fileEntryTypeId});
623 }
624
625 public static void updateFolderFileEntryTypes(
626 Folder folder, long defaultFileEntryTypeId, long[] fileEntryTypeIds)
627 throws Exception {
628
629 DLFolder dlFolder = (DLFolder)folder.getModel();
630
631 dlFolder.setDefaultFileEntryTypeId(defaultFileEntryTypeId);
632 dlFolder.setRestrictionType(
633 DLFolderConstants.RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW);
634
635 DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
636
637 ServiceContext serviceContext =
638 ServiceContextTestUtil.getServiceContext(folder.getGroupId());
639
640 DLFileEntryTypeLocalServiceUtil.updateFolderFileEntryTypes(
641 dlFolder, ListUtil.toList(fileEntryTypeIds), defaultFileEntryTypeId,
642 serviceContext);
643 }
644
645 protected static void updateStatus(
646 FileEntry fileEntry, ServiceContext serviceContext)
647 throws Exception {
648
649 Map<String, Serializable> workflowContext =
650 new HashMap<String, Serializable>();
651
652 workflowContext.put(WorkflowConstants.CONTEXT_URL, "http:
653 workflowContext.put("event", DLSyncConstants.EVENT_ADD);
654
655 DLAppHelperLocalServiceUtil.updateStatus(
656 TestPropsValues.getUserId(), fileEntry,
657 fileEntry.getLatestFileVersion(), WorkflowConstants.STATUS_PENDING,
658 WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
659 }
660
661 private static final String _CONTENT =
662 "Content: Enterprise. Open Source. For Life.";
663
664 }