Hello StackOverflow 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?
@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();
}
}