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 userId, long groupId, long repositoryId, long folderId,
149 String sourceFileName, String mimeType, String title, byte[] bytes,
150 long fileEntryTypeId, int workflowAction)
151 throws Exception {
152
153 ServiceContext serviceContext =
154 ServiceContextTestUtil.getServiceContext(groupId);
155
156 serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);
157 serviceContext.setCommand(Constants.ADD);
158 serviceContext.setLayoutFullURL("http:
159
160 return addFileEntry(
161 userId, repositoryId, folderId, sourceFileName, mimeType, title,
162 bytes, workflowAction, serviceContext);
163 }
164
165 public static FileEntry addFileEntry(
166 long groupId, long repositoryId, long folderId,
167 String sourceFileName, long fileEntryTypeId,
168 boolean workflowEnabled, boolean approved)
169 throws Exception {
170
171 ServiceContext serviceContext =
172 ServiceContextTestUtil.getServiceContext(groupId);
173
174 if (fileEntryTypeId !=
175 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL) {
176
177 serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);
178 }
179
180 serviceContext.setCommand(Constants.ADD);
181 serviceContext.setLayoutFullURL("http:
182
183 if (workflowEnabled && !approved) {
184 serviceContext.setWorkflowAction(
185 WorkflowConstants.ACTION_SAVE_DRAFT);
186 }
187 else {
188 serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
189 }
190
191 FileEntry fileEntry = addFileEntry(
192 TestPropsValues.getUserId(), repositoryId, folderId, sourceFileName,
193 ContentTypes.TEXT_PLAIN, sourceFileName, null,
194 serviceContext.getWorkflowAction(), serviceContext);
195
196 if (workflowEnabled && approved) {
197 updateStatus(fileEntry, serviceContext);
198 }
199
200 return fileEntry;
201 }
202
203 public static FileEntry addFileEntry(
204 long groupId, long repositoryId, long folderId,
205 String sourceFileName, String mimeType, String title, byte[] bytes,
206 int workflowAction)
207 throws Exception {
208
209 long fileEntryTypeId =
210 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT;
211
212 return addFileEntry(
213 TestPropsValues.getUserId(), groupId, repositoryId, folderId,
214 sourceFileName, mimeType, title, bytes, fileEntryTypeId,
215 workflowAction);
216 }
217
218 public static FileEntry addFileEntry(
219 long userId, long repositoryId, long folderId,
220 String sourceFileName, String mimeType, String title, byte[] bytes,
221 int workflowAction, ServiceContext serviceContext)
222 throws Exception {
223
224 if ((bytes == null) && Validator.isNotNull(sourceFileName)) {
225 bytes = _CONTENT.getBytes();
226 }
227
228 serviceContext = (ServiceContext)serviceContext.clone();
229
230 serviceContext.setWorkflowAction(workflowAction);
231
232 return DLAppLocalServiceUtil.addFileEntry(
233 userId, repositoryId, folderId, sourceFileName, mimeType, title,
234 StringPool.BLANK, StringPool.BLANK, bytes, serviceContext);
235 }
236
237 public static FileEntry addFileEntry(
238 long userId, long groupId, long folderId, String sourceFileName,
239 String mimeType, String title, byte[] bytes, long fileEntryTypeId,
240 int workflowAction)
241 throws Exception {
242
243 return addFileEntry(
244 userId, groupId, groupId, folderId, sourceFileName, mimeType, title,
245 bytes, fileEntryTypeId, workflowAction);
246 }
247
248 public static FileEntry addFileEntry(
249 long groupId, long folderId, String sourceFileName)
250 throws Exception {
251
252 return addFileEntry(groupId, folderId, sourceFileName, sourceFileName);
253 }
254
255 public static FileEntry addFileEntry(
256 long groupId, long folderId, String sourceFileName,
257 long fileEntryTypeId)
258 throws Exception {
259
260 return addFileEntry(
261 groupId, groupId, folderId, sourceFileName, fileEntryTypeId, true,
262 true);
263 }
264
265 public static FileEntry addFileEntry(
266 long groupId, long folderId, String sourceFileName, String title)
267 throws Exception {
268
269 return addFileEntry(
270 groupId, folderId, sourceFileName, title, null,
271 WorkflowConstants.ACTION_PUBLISH);
272 }
273
274 public static FileEntry addFileEntry(
275 long groupId, long folderId, String sourceFileName, String title,
276 boolean approved)
277 throws Exception {
278
279 int workflowAction = WorkflowConstants.ACTION_SAVE_DRAFT;
280
281 if (approved) {
282 workflowAction = WorkflowConstants.ACTION_PUBLISH;
283 }
284
285 return addFileEntry(
286 groupId, folderId, sourceFileName, title, null, workflowAction);
287 }
288
289 public static FileEntry addFileEntry(
290 long groupId, long folderId, String sourceFileName, String title,
291 byte[] bytes)
292 throws Exception {
293
294 return addFileEntry(
295 groupId, folderId, sourceFileName, title, bytes,
296 WorkflowConstants.ACTION_PUBLISH);
297 }
298
299 public static FileEntry addFileEntry(
300 long groupId, long folderId, String sourceFileName, String title,
301 byte[] bytes, int workflowAction)
302 throws Exception {
303
304 return addFileEntry(
305 groupId, folderId, sourceFileName, ContentTypes.TEXT_PLAIN, title,
306 bytes, workflowAction);
307 }
308
309 public static FileEntry addFileEntry(
310 long groupId, long folderId, String sourceFileName, String mimeType,
311 String title)
312 throws Exception {
313
314 return addFileEntry(
315 groupId, folderId, sourceFileName, mimeType, title, null,
316 WorkflowConstants.ACTION_PUBLISH);
317 }
318
319 public static FileEntry addFileEntry(
320 long groupId, long folderId, String sourceFileName, String mimeType,
321 String title, byte[] bytes, int workflowAction)
322 throws Exception {
323
324 long fileEntryTypeId =
325 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT;
326
327 return addFileEntry(
328 TestPropsValues.getUserId(), groupId, folderId, sourceFileName,
329 mimeType, title, bytes, fileEntryTypeId, workflowAction);
330 }
331
332 public static FileEntry addFileEntry(
333 long folderId, String sourceFileName, String title,
334 boolean approved, ServiceContext serviceContext)
335 throws Exception {
336
337 int workflowAction = WorkflowConstants.ACTION_SAVE_DRAFT;
338
339 if (approved) {
340 workflowAction = WorkflowConstants.ACTION_PUBLISH;
341 }
342
343 return addFileEntry(
344 serviceContext.getUserId(), serviceContext.getScopeGroupId(),
345 folderId, sourceFileName, ContentTypes.TEXT_PLAIN, title, null,
346 workflowAction, serviceContext);
347 }
348
349 public static FileEntry addFileEntryWithWorkflow(
350 long groupId, long repositoryId, long folderId, boolean approved)
351 throws Exception {
352
353 return addFileEntry(
354 groupId, repositoryId, folderId, RandomTestUtil.randomString(),
355 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL, true, approved);
356 }
357
358 public static Folder addFolder(long groupId, long parentFolderId)
359 throws Exception {
360
361 return addFolder(
362 groupId, parentFolderId, RandomTestUtil.randomString(), false);
363 }
364
365 public static Folder addFolder(
366 long groupId, long repositoryId, long parentFolderId, String name)
367 throws Exception {
368
369 ServiceContext serviceContext =
370 ServiceContextTestUtil.getServiceContext(groupId);
371
372 return addFolder(
373 repositoryId, parentFolderId, name, false, serviceContext);
374 }
375
376 public static Folder addFolder(
377 long groupId, long parentFolderId, String name)
378 throws Exception {
379
380 return addFolder(groupId, parentFolderId, name, false);
381 }
382
383 public static Folder addFolder(
384 long groupId, long parentFolderId, String name,
385 boolean deleteExisting)
386 throws Exception {
387
388 ServiceContext serviceContext =
389 ServiceContextTestUtil.getServiceContext(groupId);
390
391 return addFolder(parentFolderId, name, deleteExisting, serviceContext);
392 }
393
394 public static Folder addFolder(
395 long repositoryId, long parentFolderId, String name,
396 boolean deleteExisting, ServiceContext serviceContext)
397 throws Exception {
398
399 String description = StringPool.BLANK;
400
401 if (deleteExisting) {
402 try {
403 DLAppServiceUtil.deleteFolder(
404 serviceContext.getScopeGroupId(), parentFolderId, name);
405 }
406 catch (NoSuchFolderException nsfe) {
407 }
408 }
409
410 return DLAppLocalServiceUtil.addFolder(
411 TestPropsValues.getUserId(), repositoryId, parentFolderId, name,
412 description, serviceContext);
413 }
414
415 public static Folder addFolder(
416 long parentFolderId, String name, boolean deleteExisting,
417 ServiceContext serviceContext)
418 throws Exception {
419
420 return addFolder(
421 serviceContext.getScopeGroupId(), parentFolderId, name,
422 deleteExisting, serviceContext);
423 }
424
425 public static Folder addFolder(
426 long parentFolderId, String name, ServiceContext serviceContext)
427 throws Exception {
428
429 return addFolder(parentFolderId, name, false, serviceContext);
430 }
431
432 public static Repository addRepository(long groupId) throws Exception {
433 long classNameId = PortalUtil.getClassNameId(
434 "com.liferay.portal.repository.liferayrepository." +
435 "LiferayRepository");
436
437 return addRepository(groupId, classNameId);
438 }
439
440 public static Repository addRepository(long groupId, long classNameId)
441 throws Exception {
442
443 long userId = TestPropsValues.getUserId();
444
445 Folder folder = addFolder(
446 groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
447 RandomTestUtil.randomString());
448
449 long parentFolderId = folder.getFolderId();
450
451 String name = RandomTestUtil.randomString();
452 String description = RandomTestUtil.randomString();
453 String portletId = PortletKeys.DOCUMENT_LIBRARY;
454 UnicodeProperties typeSettingsProperties = new UnicodeProperties();
455 boolean hidden = false;
456
457 ServiceContext serviceContext =
458 ServiceContextTestUtil.getServiceContext(groupId);
459
460 return addRepository(
461 userId, groupId, classNameId, parentFolderId, name, description,
462 portletId, typeSettingsProperties, hidden, serviceContext);
463 }
464
465 public static Repository addRepository(
466 long userId, long groupId, long classNameId, long parentFolderId,
467 String name, String description, String portletId,
468 UnicodeProperties typeSettingsProperties, boolean hidden,
469 ServiceContext serviceContext)
470 throws Exception {
471
472 return RepositoryLocalServiceUtil.addRepository(
473 userId, groupId, classNameId, parentFolderId, name, description,
474 portletId, typeSettingsProperties, hidden, serviceContext);
475 }
476
477 public static RepositoryEntry addRepositoryEntry(
478 long groupId, long repositoryId)
479 throws Exception {
480
481 long userId = TestPropsValues.getUserId();
482 String mappedId = RandomTestUtil.randomString();
483
484 ServiceContext serviceContext =
485 ServiceContextTestUtil.getServiceContext(groupId);
486
487 return addRepositoryEntry(
488 userId, groupId, repositoryId, mappedId, serviceContext);
489 }
490
491 public static RepositoryEntry addRepositoryEntry(
492 long userId, long groupId, long repositoryId, String mappedId,
493 ServiceContext serviceContext)
494 throws Exception {
495
496 return RepositoryEntryLocalServiceUtil.addRepositoryEntry(
497 userId, groupId, repositoryId, mappedId, serviceContext);
498 }
499
500 public static FileEntry updateFileEntry(
501 long groupId, long fileEntryId, boolean majorVersion)
502 throws Exception {
503
504 return updateFileEntry(
505 groupId, fileEntryId, RandomTestUtil.randomString(),
506 RandomTestUtil.randomString(), majorVersion, true, true);
507 }
508
509 public static FileEntry updateFileEntry(
510 long groupId, long fileEntryId, String sourceFileName, String title)
511 throws Exception {
512
513 return updateFileEntry(
514 groupId, fileEntryId, sourceFileName, title, false, false, false);
515 }
516
517 public static FileEntry updateFileEntry(
518 long groupId, long fileEntryId, String sourceFileName, String title,
519 boolean majorVersion, boolean workflowEnabled, boolean approved)
520 throws Exception {
521
522 ServiceContext serviceContext =
523 ServiceContextTestUtil.getServiceContext(groupId);
524
525 serviceContext.setCommand(Constants.UPDATE);
526 serviceContext.setLayoutFullURL("http:
527
528 return updateFileEntry(
529 groupId, fileEntryId, sourceFileName, ContentTypes.TEXT_PLAIN,
530 title, majorVersion, workflowEnabled, approved, serviceContext);
531 }
532
533 public static FileEntry updateFileEntry(
534 long groupId, long fileEntryId, String sourceFileName,
535 String mimeType, String title, boolean majorVersion,
536 boolean workflowEnabled, boolean approved,
537 ServiceContext serviceContext)
538 throws Exception {
539
540 String description = StringPool.BLANK;
541 String changeLog = StringPool.BLANK;
542
543 byte[] bytes = null;
544
545 if (Validator.isNotNull(sourceFileName)) {
546 String newContent = _CONTENT + "\n" + System.currentTimeMillis();
547
548 bytes = newContent.getBytes();
549 }
550
551 serviceContext = (ServiceContext)serviceContext.clone();
552
553 serviceContext.setAddGroupPermissions(true);
554 serviceContext.setAddGuestPermissions(true);
555 serviceContext.setScopeGroupId(groupId);
556
557 if (workflowEnabled && !approved) {
558 serviceContext.setWorkflowAction(
559 WorkflowConstants.ACTION_SAVE_DRAFT);
560 }
561 else {
562 serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
563 }
564
565 FileEntry fileEntry = DLAppServiceUtil.updateFileEntry(
566 fileEntryId, sourceFileName, mimeType, title, description,
567 changeLog, majorVersion, bytes, serviceContext);
568
569 if (workflowEnabled && approved) {
570 updateStatus(fileEntry, serviceContext);
571 }
572
573 return fileEntry;
574 }
575
576 public static FileEntry updateFileEntry(
577 long groupId, long fileEntryId, String sourceFileName,
578 String mimeType, String title, boolean majorVersion,
579 ServiceContext serviceContext)
580 throws Exception {
581
582 String description = StringPool.BLANK;
583 String changeLog = StringPool.BLANK;
584
585 byte[] bytes = null;
586
587 if (Validator.isNotNull(sourceFileName)) {
588 String newContent = _CONTENT + "\n" + System.currentTimeMillis();
589
590 bytes = newContent.getBytes();
591 }
592
593 serviceContext.setAddGroupPermissions(true);
594 serviceContext.setAddGuestPermissions(true);
595 serviceContext.setScopeGroupId(groupId);
596
597 return DLAppServiceUtil.updateFileEntry(
598 fileEntryId, sourceFileName, mimeType, title, description,
599 changeLog, majorVersion, bytes, serviceContext);
600 }
601
602 public static FileEntry updateFileEntryWithWorkflow(
603 long groupId, long fileEntryId, boolean majorVersion,
604 boolean approved)
605 throws Exception {
606
607 ServiceContext serviceContext =
608 ServiceContextTestUtil.getServiceContext(groupId);
609
610 serviceContext.setCommand(Constants.UPDATE);
611 serviceContext.setLayoutFullURL("http:
612
613 return updateFileEntry(
614 groupId, fileEntryId, RandomTestUtil.randomString(),
615 ContentTypes.TEXT_PLAIN, RandomTestUtil.randomString(),
616 majorVersion, true, approved, serviceContext);
617 }
618
619 public static void updateFolderFileEntryType(
620 Folder folder, long fileEntryTypeId)
621 throws Exception {
622
623 updateFolderFileEntryTypes(
624 folder, fileEntryTypeId, new long[] {fileEntryTypeId});
625 }
626
627 public static void updateFolderFileEntryTypes(
628 Folder folder, long defaultFileEntryTypeId, long[] fileEntryTypeIds)
629 throws Exception {
630
631 DLFolder dlFolder = (DLFolder)folder.getModel();
632
633 dlFolder.setDefaultFileEntryTypeId(defaultFileEntryTypeId);
634 dlFolder.setOverrideFileEntryTypes(true);
635
636 DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
637
638 ServiceContext serviceContext =
639 ServiceContextTestUtil.getServiceContext(folder.getGroupId());
640
641 DLFileEntryTypeLocalServiceUtil.updateFolderFileEntryTypes(
642 dlFolder, ListUtil.toList(fileEntryTypeIds), defaultFileEntryTypeId,
643 serviceContext);
644 }
645
646 protected static void updateStatus(
647 FileEntry fileEntry, ServiceContext serviceContext)
648 throws Exception {
649
650 Map<String, Serializable> workflowContext =
651 new HashMap<String, Serializable>();
652
653 workflowContext.put(WorkflowConstants.CONTEXT_URL, "http:
654 workflowContext.put("event", DLSyncConstants.EVENT_ADD);
655
656 DLAppHelperLocalServiceUtil.updateStatus(
657 TestPropsValues.getUserId(), fileEntry,
658 fileEntry.getLatestFileVersion(), WorkflowConstants.STATUS_PENDING,
659 WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
660 }
661
662 private static final String _CONTENT =
663 "Content: Enterprise. Open Source. For Life.";
664
665 }