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();
}
}
First of all, method receive
doesn't require RenderRequest
object, but your method fileUploadByApp
. If you look carefully in your method, all what you need from ThemeDisplay
is scopeGroupId
. RenderRequest
object, as you mentioned, is required for creating ServiceContext
, but you have at least 2 solutions for this.
ServletContextPool
(if its possible). In second case you can setup what you need, but I think you need setup only scopeGroupId
and it will work as you want.
Example of changes:
@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();
}
}