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.portlet.documentlibrary.model.DLFileEntryTypeConstants;
027    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
028    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
030    
031    import java.io.Serializable;
032    
033    import java.util.HashMap;
034    import java.util.Map;
035    
036    /**
037     * @author Alexander Chow
038     */
039    public abstract class DLAppTestUtil {
040    
041            public static FileEntry addFileEntryWithWorkflow(
042                            long userId, long groupId, long folderId, String sourceFileName,
043                            String title, boolean approved, ServiceContext serviceContext)
044                    throws Exception {
045    
046                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
047    
048                    try {
049                            WorkflowThreadLocal.setEnabled(true);
050    
051                            serviceContext = (ServiceContext)serviceContext.clone();
052    
053                            serviceContext.setWorkflowAction(
054                                    WorkflowConstants.ACTION_SAVE_DRAFT);
055    
056                            FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(
057                                    userId, groupId, folderId, sourceFileName,
058                                    ContentTypes.TEXT_PLAIN, title, StringPool.BLANK,
059                                    StringPool.BLANK, RandomTestUtil.randomBytes(), serviceContext);
060    
061                            if (approved) {
062                                    return updateStatus(fileEntry, serviceContext);
063                            }
064    
065                            return fileEntry;
066                    }
067                    finally {
068                            WorkflowThreadLocal.setEnabled(workflowEnabled);
069                    }
070            }
071    
072            public static void populateNotificationsServiceContext(
073                            ServiceContext serviceContext, String command)
074                    throws Exception {
075    
076                    serviceContext.setAttribute("entryURL", "http://localhost");
077    
078                    if (Validator.isNotNull(command)) {
079                            serviceContext.setCommand(command);
080                    }
081    
082                    serviceContext.setLayoutFullURL("http://localhost");
083            }
084    
085            public static void populateServiceContext(
086                            ServiceContext serviceContext, long fileEntryTypeId)
087                    throws Exception {
088    
089                    if (fileEntryTypeId !=
090                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL) {
091    
092                            serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);
093                    }
094    
095                    serviceContext.setLayoutFullURL("http://localhost");
096            }
097    
098            protected static FileEntry updateStatus(
099                            FileEntry fileEntry, ServiceContext serviceContext)
100                    throws Exception {
101    
102                    Map<String, Serializable> workflowContext = new HashMap<>();
103    
104                    workflowContext.put(WorkflowConstants.CONTEXT_URL, "http://localhost");
105                    workflowContext.put("event", DLSyncConstants.EVENT_ADD);
106    
107                    DLFileEntryLocalServiceUtil.updateStatus(
108                            TestPropsValues.getUserId(),
109                            fileEntry.getFileVersion().getFileVersionId(),
110                            WorkflowConstants.STATUS_APPROVED, serviceContext, workflowContext);
111    
112                    return DLAppLocalServiceUtil.getFileEntry(fileEntry.getFileEntryId());
113            }
114    
115    }