留言板

upload csv files to document and media during a scheduler

Alex Man,修改在7 年前。

upload csv files to document and media during a scheduler

Junior Member 帖子: 70 加入日期: 16-2-8 最近的帖子
I have a cron job scheduler which run every 2 hours. During the scheduling i need to create csv files based on some incoming values, the csv files created are to be uploaded into the document and media. Here the challenge which I am facing is that for the method addFileEntry it requires ServiceContext object to be passed as a parameter.

But in the above scenario how can I get the ServiceContext object in service builder.

My Scenario seems a valid one I think

my code is as given below

bytes = getCSVOutputStream(headers, details).toByteArray();
ServiceContext serviceContext = new ServiceContext();  
String title = "Csv_"+new Date().getTime()+".csv";
String mimeType = "text/csv";
DLAppServiceUtil.addFileEntry(repositoryId, folderId, title, mimeType, title, title, StringPool.BLANK, bytes, serviceContext);


can anyone please help me on this
Nidhish Krishnan,修改在7 年前。

RE: upload csv files to document and media during a scheduler

New Member 帖子: 2 加入日期: 15-1-2 最近的帖子
Try using ServiceContextThreadLocal for getting ServiceContext in your service impl like as shown below

ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
thumbnail
Miroslav Ligas,修改在7 年前。

RE: upload csv files to document and media during a scheduler

New Member 帖子: 17 加入日期: 12-2-23 最近的帖子
Hi
Nidhish suggestion is a possibility but if you don't like the object returned you can create a new service context manually and configure it as you require. usually it sufficient to create the object and it's good to set the basic permissions.


ServiceContext serviceContext = new ServiceContext();
        serviceContext.setAddGroupPermissions(true);
        serviceContext.setAddGuestPermissions(false);


Especially in the scheduled task you might not get anything from the thread local as there have not been an user interaction in the thread.
Alex Man,修改在7 年前。

RE: upload csv files to document and media during a scheduler

Junior Member 帖子: 70 加入日期: 16-2-8 最近的帖子
Miroslav Ligas:
Hi
Nidhish suggestion is a possibility but if you don't like the object returned you can create a new service context manually and configure it as you require. usually it sufficient to create the object and it's good to set the basic permissions.


ServiceContext serviceContext = new ServiceContext();
        serviceContext.setAddGroupPermissions(true);
        serviceContext.setAddGuestPermissions(false);


Especially in the scheduled task you might not get anything from the thread local as there have not been an user interaction in the thread.


When when I tried with your code I got

PermissionChecker not initialized PortalException
thumbnail
David H Nebinger,修改在7 年前。

RE: upload csv files to document and media during a scheduler

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
You may have to take things a step farther and initialize more stuff including the principal thread local, etc.

When I need to do this kind of thing I'll look up an administrator user and set things up as that user.

I don't like to "hard code" for a specific admin user unless, of course, I have created a specific admin user for this task.
Alex Man,修改在7 年前。

RE: upload csv files to document and media during a scheduler

Junior Member 帖子: 70 加入日期: 16-2-8 最近的帖子
David H Nebinger:
You may have to take things a step farther and initialize more stuff including the principal thread local, etc.

When I need to do this kind of thing I'll look up an administrator user and set things up as that user.

I don't like to "hard code" for a specific admin user unless, of course, I have created a specific admin user for this task.


Did'nt get much...it would be greatful if you can you please explain with an example