Liferay Service Builder + 文档库 + Quartz Scheduler + Apache POI

Liferay Service Builder + Document Library + Quartz Scheduler + Apache POI

你好 Whosebug 社区,

要求: 我需要有关如何创建 Liferay(LR) 模块的建议和帮助,该模块将使用从数据库中提取的信息创建 XLS 文件,并在整个星期的预定时间内将它们存储到文档库 (DL) 中的文件夹位置。

解决方法: 我解决了使用LR的Service Builder+DL+Quartz Scheduler+Apache POI。下面的代码。

障碍: receive() 方法需要一个 RenderRequest 对象,以便可以创建 ThemeDisplay 和 ServiceContext 对象,DLAppServiceUtil 将使用它们在 DL 中创建文件。我该如何着手创建 RenderRequest 对象?

@Override
public void receive(Message message) throws MessageListenerException {
    _log.debug(">> receive()");

    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
    fileUploadByApp("folder-1", themeDisplay, renderRequest);

    _log.debug("<< receive()");
}

public void fileUploadByApp(String folderName, ThemeDisplay themeDisplay, RenderRequest renderRequest) {

    try {
        File file = new File("D:/liferay-portal-6.2-ce-ga6/temp/sample_" + getDateTimeBasedFilename() + ".txt");

        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
        writer.write("Something");
        writer.close();

        long repositoryId = themeDisplay.getScopeGroupId();
        String mimeType = MimeTypesUtil.getContentType(file);
        String title = file.getName();
        String description = "This file is added via programatically";
        String changeLog = "hi";
        Long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;

        Folder folder = DLAppServiceUtil.getFolder(themeDisplay.getScopeGroupId(), parentFolderId, folderName);
        ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(),
                renderRequest);
        InputStream is = new FileInputStream(file);
        DLAppServiceUtil.addFileEntry(repositoryId, folder.getFolderId(), file.getName(), mimeType, title,
                description, changeLog, is, file.length(), serviceContext);

    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }
}

Project repo, click here

首先,方法receive不需要RenderRequest对象,但是你的方法fileUploadByApp。如果你仔细看你的方法,你需要的 ThemeDisplay 就是 scopeGroupId。正如您提到的,RenderRequest 对象是创建 ServiceContext 所必需的,但您至少有 2 个解决方案。

  1. ServletContextPool 获取一些实例(如果可能的话)。
  2. 手动创建对象

在第二种情况下,您可以设置所需的内容,但我认为您只需要设置 scopeGroupId,它就会按您的意愿工作。

变化示例:

@Override
public void receive(Message message) throws MessageListenerException {
    _log.debug(">> receive()");

    ServiceContext serviceContext = new ServiceContext();
    serviceContext.setScopeGroupId(YOUR_SCOPE_GROUP_ID); //For example YOUR_SCOPE_GROUP_ID can be received from message.
    fileUploadByApp("folder-1", serviceContext);

    _log.debug("<< receive()");
}

public void fileUploadByApp(String folderName, ServiceContext serviceContext) {

    try {
        File file = new File("D:/liferay-portal-6.2-ce-ga6/temp/sample_" + getDateTimeBasedFilename() + ".txt");

        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
        writer.write("Something");
        writer.close();

        long repositoryId = serviceContext.getScopeGroupId();
        String mimeType = MimeTypesUtil.getContentType(file);
        String title = file.getName();
        String description = "This file is added via programatically";
        String changeLog = "hi";
        Long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;

        Folder folder = DLAppServiceUtil.getFolder(serviceContext.getScopeGroupId(), parentFolderId, folderName);

        InputStream is = new FileInputStream(file);
        DLAppServiceUtil.addFileEntry(repositoryId, folder.getFolderId(), file.getName(), mimeType, title,
                description, changeLog, is, file.length(), serviceContext);

    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }
}

谢谢 glw。提供的代码有助于连接点。除了您提供的代码段之外,我还使用了以下解决方案。

// Repository ID of the document library, figured is out by the output from
// _log.debug("repositoryId: " + repositoryId);
// in
// fileUploadByApp(folderName, themeDisplay, renderRequest).
// I found the entry in 'ddmcontent' table of LR's default DB
private long siteARepoId = ?????;
// Found this in 'company' table of LR's default DB
private long LRCompanyId = ?????;
private String sAdminRole = "Administrator";

@Override
public void receive(Message message) throws MessageListenerException {
    _log.debug(">> receive()");

    ServiceContext serviceContext = new ServiceContext();
    serviceContext.setScopeGroupId(siteARepoId);
    fileUploadByApp(DLFolderLocation, serviceContext);

    _log.debug("<< receive()");
}

public void fileUploadByApp(String folderName, ServiceContext serviceContext) {
    _log.debug(">> fileUploadByApp(folderName, serviceContext)");

    try {
        File file = new File(LRTempLocation + "excel_" + getDateTimeBasedFilename() + ".xlsx");

        createExcel(file);

        long repositoryId = serviceContext.getScopeGroupId();
        String mimeType = MimeTypesUtil.getContentType(file);
        String title = file.getName();
        String description = "This file is added via programatically";
        String changeLog = "hi";
        Long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;

        _log.debug("repositoryId: " + repositoryId);
        _log.debug("fileName: " + file.getName());
        _log.debug("mimeType: " + mimeType);
        _log.debug("title: " + title);
        _log.debug("description: " + description);
        _log.debug("changeLog: " + changeLog);
        _log.debug("fileLength: " + file.length());
        _log.debug("serviceContext: " + serviceContext);

        Folder folder = DLAppLocalServiceUtil.getFolder(repositoryId, parentFolderId, folderName);

        _log.debug("folderId: " + folder.getFolderId());

        InputStream is = new FileInputStream(file);

        _log.debug("inputStream: " + is);

        // Initializing PermissionChecker, without the below the exception
        // occurs
        // Caused by: com.liferay.portal.security.auth.PrincipalException:
        // PermissionChecker not initialized
        // Source:
        // http://www.open.gr/blog/2014/05/permissionchecker-not-initialised-scheduler-job
        Company companyqq = CompanyLocalServiceUtil.getCompanyById(LRCompanyId);
        Role adminRole = RoleLocalServiceUtil.getRole(companyqq.getCompanyId(), sAdminRole);
        List<User> adminUsers = UserLocalServiceUtil.getRoleUsers(adminRole.getRoleId());
        PrincipalThreadLocal.setName(adminUsers.get(0).getUserId());
        PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(adminUsers.get(0), true);
        PermissionThreadLocal.setPermissionChecker(permissionChecker);

        // Adding the file entry to the Document Library
        DLAppServiceUtil.addFileEntry(repositoryId, folder.getFolderId(), file.getName(), mimeType, title,
                description, changeLog, is, file.length(), serviceContext);

    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }

    _log.debug("<< fileUploadByApp(folderName, serviceContext)");
}

Project Link, click here