Fórum

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

thumbnail
Mohammed Rawoof Shaik, modificado 7 Anos atrás.

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

Junior Member Postagens: 37 Data de Entrada: 25/09/16 Postagens Recentes
Hello LR Community,

Requirement:
I need advice and help on going about how to create a Liferay(LR) module that will create XLS files with information pulled from a DB, and store them to a folder location in Document Library(DL), during scheduled times throughout the week.

Solution:
I solutionized to use LR's Service Builder+DL+Quartz Scheduler+Apache POI. Code below.

Roadblock:
The receive() method requires a RenderRequest object so that the ThemeDisplay and ServiceContext object can be created, which will be used by the DLAppServiceUtil to create the file in DL. How do I go about creating a RenderRequest object?

Code:
@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 {
// Sample file
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
thumbnail
David H Nebinger, modificado 7 Anos atrás.

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

Liferay Legend Postagens: 14919 Data de Entrada: 02/09/06 Postagens Recentes
Theme display has no context as a scheduled task, there is no theme, group, page, etc to take advantage of.

A service context can be created at any time. As a scheduled task you'll have to figure out an appropriate user to use. Often times it's easiest to find an admin user as it gets you by any permissions issues.

You don't have to go through some sort of "file upload", there's no user uploading anything. There are other DL apis that you can use to store a file programmatically in the doc lib.
thumbnail
Mohammed Rawoof Shaik, modificado 7 Anos atrás.

RE: Liferay Service Builder+Document Library+Quartz Scheduler+Apache POI (Resposta)

Junior Member Postagens: 37 Data de Entrada: 25/09/16 Postagens Recentes
Thanks David for the advice. I looked into the DL API and have posted the solution below,
Solution here