Foros de discusión

Generate Link to a liferay document from scheduler class

Rupal Chatterjee, modificado hace 11 años.

Generate Link to a liferay document from scheduler class

New Member Mensajes: 6 Fecha de incorporación: 21/01/13 Mensajes recientes
Hello all,

I am quite new to liferay. Need help.

I am developing a custom search portlet with a store search and email notification facility. When a user store a search then my application email the new result for that search string everyday. There are two scenarios
1. When user search a string using the portlet, the application generate link to all the found result items. We need to generate links as they are not provided by the search engine. Here we are using,
PortletURL portletURL = PortletURLFactoryUtil.create(request, portletId, plid, PortletRequest.RENDER_PHASE);
This is working fine.

2. Here an user store a search string and opt for notification via email. The application is running a scheduler process every day which searches for the stored string and send an email with the result links.
This is not working. I am unable to generate link because the receive method of the scheduler class donot have any request object

public class EmailNotificationScheduler implements MessageListener{
@Override
public void receive(Message msg) throws MessageListenerException {
-----------Logic-------------
}
}

I tried another approach,
- Created a createLink.jsp file inline of view.jsp.
- Added all parameter to generate links as get paramters to createLinks. Now the url looks like this,
http://localhost:8081/Test-portlet/html/search/createDynamicLink.jsp?groupId=10179&scopeGroupId=10179&portletId=15&articleId=10689&entryClassName=com.liferay.portlet.journal.model.JournalArticle&classPK=10691&resourcePrimKey=10691
- I thought that, when user clicks on the above link, the actual article link should be created dynamically.
- In createLink.jsp, I tried to generate link but it failed because the request is not a portlet scope request.


PLEASE HELP!!!!

Thanks
Rupal Chatterjee
Rupal Chatterjee, modificado hace 10 años.

RE: Generate Link to a liferay document from scheduler class

New Member Mensajes: 6 Fecha de incorporación: 21/01/13 Mensajes recientes
Hey All,

Finally, I have SOLVED the issue. I thought i should share the same with the community.

To achieve the same, we need to changed my approach a little bit. Now we need to store the layoutId with each searchstring in the database.
Use the below code to get layout id,

ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
long layoutID = themeDisplay.getLayout().getPlid();


Now from the scheduler class, we need to fetch all the search string & respective layoutId from the database for current time and execute solr search on each of them in a loop.
Use the below code to execute Solr search,

// Connect to Solr server
CommonsHttpSolrServer solrServer = new CommonsHttpSolrServer("http://localhost:8080/solr");

// Define all solr search parameter
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", searchKeyword);
params.set("start", 0);
params.set("rows",0);

// Execute query and get result in SolrDocumentList arraylist
QueryResponse queryResponse = solrServer.query(params);
SolrDocumentList results = queryResponse.getResults();

// loop through the search result and search result list
for (int index = 0; index < results.size(); index++) {
document = results.get(index);

// Get all values from the SolrDocument
Object entryClassName = document.getFirstValue(Field.ENTRY_CLASS_NAME);
Object titleObj = document.getFirstValue(Field.TITLE);
Object contentObj = document.getFirstValue(Field.CONTENT);
Object creationDate = document.getFirstValue(Field.CREATE_DATE);
Object modificationDate = document.getFirstValue(Field.MODIFIED_DATE);
long groupId = GetterUtil.getLong(document.getFirstValue(Field.GROUP_ID));
long scopeGroupId = GetterUtil.getLong(document.getFirstValue(Field.SCOPE_GROUP_ID));
String portletId = String.valueOf(document.getFirstValue(Field.PORTLET_ID));
Object articleID = document.getFirstValue(ApplicationGlobals.SOLR_ARTICLE_ID);
long classPK = GetterUtil.getLong(document.getFirstValue(Field.ENTRY_CLASS_PK));
long resourcePrimKey = GetterUtil.getLong(document.getFirstValue(Field.ROOT_ENTRY_CLASS_PK));
String userName = String.valueOf(document.getFirstValue(Field.USER_NAME));

String dynamicLinkURL = createDynamicLinkURL(groupId, scopeGroupId, portletId, articleID, entryClassName, classPK, resourcePrimKey, layoutId);

}


Now createDynamicLinkURL method is responsible to generate external link to a liferay item. Below is the code,

private String createDynamicLinkURL(long groupId, long scopeGroupId, String portletId, Object articleID,
Object entryClassName, long classPK, long resourcePrimKey, long layoutId){

static final String LIFERAY_SERVER_URL = "http://localhost:8081";
static final String PORTLET_ID = "test_WAR_TestSearchportlet";

StringBuffer dynamicLinkURL = new StringBuffer();

// Liferay server url, we need to pass this statically as we dont have a request object
dynamicLinkURL.append(LIFERAY_SERVER_URL);
dynamicLinkURL.append("/c/portal/layout");
dynamicLinkURL.append("?");
dynamicLinkURL.append("p_l_id");
dynamicLinkURL.append("=");
dynamicLinkURL.append(layoutId);
dynamicLinkURL.append("&");
dynamicLinkURL.append("p_v_l_s_g_id=0");
dynamicLinkURL.append("&"); dynamicLinkURL.append("p_p_id="+PORTLET_ID+"&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=2&_"+PORTLET_ID+"_jspPage=%2Fhtml%2Fsearch%2Futil%2FcreateDynamicLink.jsp");


dynamicLinkURL.append("&");

dynamicLinkURL.append("groupId");
dynamicLinkURL.append("=");
dynamicLinkURL.append(groupId);

dynamicLinkURL.append("&");

dynamicLinkURL.append("scopeGroupId");
dynamicLinkURL.append("=");
dynamicLinkURL.append(scopeGroupId);

dynamicLinkURL.append("&");

dynamicLinkURL.append("portletId");
dynamicLinkURL.append("=");
dynamicLinkURL.append(portletId);

dynamicLinkURL.append("&");

dynamicLinkURL.append("articleId");
dynamicLinkURL.append("=");
dynamicLinkURL.append(articleID);

dynamicLinkURL.append("&");

dynamicLinkURL.append("entryClassName");
dynamicLinkURL.append("=");
dynamicLinkURL.append(entryClassName);

dynamicLinkURL.append("&");

dynamicLinkURL.append("classPK");
dynamicLinkURL.append("=");
dynamicLinkURL.append(classPK);

dynamicLinkURL.append("&");

dynamicLinkURL.append("resourcePrimKey");
dynamicLinkURL.append("=");
dynamicLinkURL.append(resourcePrimKey);

return dynamicLinkURL.toString();
}


Hope this helps... Cheers...

Happy Coding...

Rupal Chatterjee
Software Engineer, Mindfire Solutions
www.mindfiresolutions.com