001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.test.util.RandomTestUtil;
019    import com.liferay.portal.kernel.test.util.TestPropsValues;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.test.randomizerbumpers.TikaSafeRandomizerBumper;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
028    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
029    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
031    
032    import java.io.Serializable;
033    
034    import java.util.HashMap;
035    import java.util.Map;
036    
037    /**
038     * @author Alexander Chow
039     */
040    public abstract class DLAppTestUtil {
041    
042            public static FileEntry addFileEntryWithWorkflow(
043                            long userId, long groupId, long folderId, String sourceFileName,
044                            String title, boolean approved, ServiceContext serviceContext)
045                    throws Exception {
046    
047                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
048    
049                    try {
050                            WorkflowThreadLocal.setEnabled(true);
051    
052                            serviceContext = (ServiceContext)serviceContext.clone();
053    
054                            serviceContext.setWorkflowAction(
055                                    WorkflowConstants.ACTION_SAVE_DRAFT);
056    
057                            FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(
058                                    userId, groupId, folderId, sourceFileName,
059                                    ContentTypes.TEXT_PLAIN, title, StringPool.BLANK,
060                                    StringPool.BLANK,
061                                    RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE),
062                                    serviceContext);
063    
064                            if (approved) {
065                                    return updateStatus(fileEntry, serviceContext);
066                            }
067    
068                            return fileEntry;
069                    }
070                    finally {
071                            WorkflowThreadLocal.setEnabled(workflowEnabled);
072                    }
073            }
074    
075            public static void populateNotificationsServiceContext(
076                            ServiceContext serviceContext, String command)
077                    throws Exception {
078    
079                    serviceContext.setAttribute("entryURL", "http://localhost");
080    
081                    if (Validator.isNotNull(command)) {
082                            serviceContext.setCommand(command);
083                    }
084    
085                    serviceContext.setLayoutFullURL("http://localhost");
086            }
087    
088            public static void populateServiceContext(
089                            ServiceContext serviceContext, long fileEntryTypeId)
090                    throws Exception {
091    
092                    if (fileEntryTypeId !=
093                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL) {
094    
095                            serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);
096                    }
097    
098                    serviceContext.setLayoutFullURL("http://localhost");
099            }
100    
101            protected static FileEntry updateStatus(
102                            FileEntry fileEntry, ServiceContext serviceContext)
103                    throws Exception {
104    
105                    Map<String, Serializable> workflowContext = new HashMap<>();
106    
107                    workflowContext.put(WorkflowConstants.CONTEXT_URL, "http://localhost");
108                    workflowContext.put("event", DLSyncConstants.EVENT_ADD);
109    
110                    DLFileEntryLocalServiceUtil.updateStatus(
111                            TestPropsValues.getUserId(),
112                            fileEntry.getFileVersion().getFileVersionId(),
113                            WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
114    
115                    return DLAppLocalServiceUtil.getFileEntry(fileEntry.getFileEntryId());
116            }
117    
118    }