29 August 2012
XML
XPL http://www.w3.org/Submission/xpl/
Exist DB http://exist-db.org/exist/index.xml
XPath http://www.w3.org/TR/xpath20/
Xquery http://www.w3.org/TR/xquery/
XSLT http://www.w3.org/TR/xslt
Exist DB http://exist-db.org/exist/index.xml
XPath http://www.w3.org/TR/xpath20/
Xquery http://www.w3.org/TR/xquery/
XSLT http://www.w3.org/TR/xslt
Working with alloyui events and elements
http://www.liferay.com/web/nathan.cavanaugh/blog/-/blogs/alloyui-working-with-elements-and-events
Alloy UI Loading Mask
AlloyUI Loading mask
if (A.one('#wrapper').loadingmask == null)
A.one('#wrapper').plug(A.LoadingMask, { background: '#000' });
A.one('#wrapper').loadingmask.toggle();
if (A.one('#wrapper').loadingmask == null)
A.one('#wrapper').plug(A.LoadingMask, { background: '#000' });
A.one('#wrapper').loadingmask.toggle();
Changing Datatype in liferay service layer
your-portlet\docroot\WEB-INF\classes\META-INF
portlet-model-hints.xml
<model-hints>
<model name="com.abc.portal.community.database.model.TwitterAccount">
<field name="accountId" type="long" />
<field name="twitterUserId" type="long" />
<field name="imageUrl" type="String">
<hint-collection name="TEXTAREA" />
</field>
<field name="twitterScreenName" type="String" />
<field name="createDate" type="Date" />
<field name="modifiedDate" type="Date" />
<field name="lastStatusId" type="long" />
</model>
<model name="com.abc.portal.community.database.model.TwitterFeed">
<field name="feedId" type="long" />
<field name="accountId" type="long" />
<field name="feedMessage" type="String">
<hint-collection name="TEXTAREA" />
</field>
<field name="createDate" type="Date" />
</model>
</model-hints>
Display Twitter feeds in liferay
In this post im going how to fetch twitter feeds in liferay portlet.
Add Message Listener in liferay port let.xml
<scheduler-entry>
<scheduler-event-listener-class>com.abc.portal.community.messaging.SynchronizeTwitterMessageListener</scheduler-event-listener-class>
<trigger>
<simple>
<property-key>twitter.synchronization.interval</property-key>
<time-unit>minute</time-unit>
</simple>
</trigger>
</scheduler-entry>
In Scheduler class
com.abc.portal.community.messaging.SynchronizeTwitterMessageListener
public class SynchronizeTwitterMessageListener implements MessageListener {
@Override
public void receive(Message message) throws MessageListenerException {
try {
doReceive(message);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doReceive(Message message) throws Exception {
TwitterFeedLocalServiceUtil.updateFeeds();
_log.info(" ========= Successfully called SynchronizeTwitterMessageListener =========");
}
private static Log _log = LogFactoryUtil.getLog(
SynchronizeTwitterMessageListener.class);
}
protected void updateFeed(String twitterurl) throws PortalException,
SystemException {
String twitterScreenName = twitterurl;
Date now = new Date();
if (Validator.isNull(twitterScreenName)) {
throw new FeedTwitterScreenNameException();
}
TwitterAccount twitterAccount = twitterAccountPersistence
.fetchByTwitterScreenName(twitterScreenName);
JSONArray jsonArray = null;
String ImageURL = null;
if (twitterAccount == null) {
jsonArray = getUserTimelineJSONArray(twitterScreenName, 0);
long feedId = counterLocalService.increment();
twitterAccount = twitterAccountPersistence.create(feedId);
twitterAccount.setTwitterScreenName(twitterScreenName);
twitterAccount.setCreateDate(now);
twitterAccount.setModifiedDate(now);
twitterAccountPersistence.update(twitterAccount, false);
if (jsonArray == null) {
return;
}
}
if (jsonArray == null) {
jsonArray = getUserTimelineJSONArray(twitterScreenName,
twitterAccount.getLastStatusId());
}
if ((jsonArray == null) || (jsonArray.length() == 0)) {
return;
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject statusJSON = jsonArray.getJSONObject(i);
SimpleDateFormat sdf = new SimpleDateFormat(
"EEE MMM d hh:mm:ss Z yyyy");
Date createDate = null;
try {
createDate = sdf.parse(statusJSON.getString("created_at"));
} catch (ParseException pe) {
throw new SystemException(pe);
}
long statusId = statusJSON.getLong("id");
String text = statusJSON.getString("text");
JSONObject userJSON = statusJSON.getJSONObject("user");
ImageURL = userJSON.getString("profile_image_url");
if (twitterAccount.getTwitterUserId() <= 0) {
userJSON = statusJSON.getJSONObject("user");
twitterAccount.setTwitterUserId(userJSON.getLong("id"));
ImageURL = userJSON.getString("profile_image_url");
twitterAccount.setImageUrl(ImageURL);
}
if (twitterAccount.getLastStatusId() < statusId) {
twitterAccount.setLastStatusId(statusId);
twitterAccount.setImageUrl(ImageURL);
twitterAccountPersistence.update(twitterAccount, false);
}
JSONObject extraData = JSONFactoryUtil.createJSONObject();
extraData.put("text", text);
TwitterFeed twitterFeed = TwitterFeedLocalServiceUtil
.createTwitterFeed(CounterLocalServiceUtil
.increment(TwitterFeed.class.getName()));
twitterFeed.setAccountId(twitterAccount.getAccountId());
twitterFeed.setCreateDate(createDate);
System.out.println("text===============" + text);
twitterFeed.setFeedMessage(text);
Date today = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(today);
cal.add(Calendar.DAY_OF_MONTH, -30);
SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date thirtydaysold = null;
try {
String date = sdfd.format(cal.getTime());
createDate = sdfd.parse(date);
} catch (ParseException pe) {
}
/*
* List<TwitterFeed> twitterFeedsold = TwitterFeedLocalServiceUtil
* .getOldTwitterFeeds(twitterAccount.getAccountId(),
* thirtydaysold);
* TwitterFeedLocalServiceUtil.deleteoldfeeds(twitterFeedsold);
*/
/*
* SocialActivityLocalServiceUtil.addActivity( user.getUserId(), 0,
* createDate, Feed.class.getName(), statusId,
* TwitterActivityKeys.ADD_STATUS, extraData.toString(), 0);
*/
TwitterFeedLocalServiceUtil.addTwitterFeed(twitterFeed);
// System.out.println("\n\n\n\n\ntwitterFeed>>>>>>>>" + twitterFeed
// +"\n\n\n\n\n");
}
twitterAccount.setModifiedDate(now);
twitterAccount.setImageUrl(ImageURL);
twitterAccountPersistence.update(twitterAccount, false);
}
protected JSONArray getUserTimelineJSONArray(String twitterScreenName,
long sinceId) {
// System.out.println("sinceId>>>#######################################################>>>>>>>>>>>"
// + sinceId);
try {
String url = _URL + twitterScreenName + ".json";
if (sinceId > 0) {
url += "?since_id=" + sinceId;
}
return JSONFactoryUtil.createJSONArray(HttpUtil.URLtoString(url));
} catch (Exception e) {
if (_log.isWarnEnabled()) {
_log.warn(e, e);
}
return null;
}
}
Add Message Listener in liferay port let.xml
<scheduler-entry>
<scheduler-event-listener-class>com.abc.portal.community.messaging.SynchronizeTwitterMessageListener</scheduler-event-listener-class>
<trigger>
<simple>
<property-key>twitter.synchronization.interval</property-key>
<time-unit>minute</time-unit>
</simple>
</trigger>
</scheduler-entry>
In Scheduler class
com.abc.portal.community.messaging.SynchronizeTwitterMessageListener
public class SynchronizeTwitterMessageListener implements MessageListener {
@Override
public void receive(Message message) throws MessageListenerException {
try {
doReceive(message);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doReceive(Message message) throws Exception {
TwitterFeedLocalServiceUtil.updateFeeds();
_log.info(" ========= Successfully called SynchronizeTwitterMessageListener =========");
}
private static Log _log = LogFactoryUtil.getLog(
SynchronizeTwitterMessageListener.class);
}
protected void updateFeed(String twitterurl) throws PortalException,
SystemException {
String twitterScreenName = twitterurl;
Date now = new Date();
if (Validator.isNull(twitterScreenName)) {
throw new FeedTwitterScreenNameException();
}
TwitterAccount twitterAccount = twitterAccountPersistence
.fetchByTwitterScreenName(twitterScreenName);
JSONArray jsonArray = null;
String ImageURL = null;
if (twitterAccount == null) {
jsonArray = getUserTimelineJSONArray(twitterScreenName, 0);
long feedId = counterLocalService.increment();
twitterAccount = twitterAccountPersistence.create(feedId);
twitterAccount.setTwitterScreenName(twitterScreenName);
twitterAccount.setCreateDate(now);
twitterAccount.setModifiedDate(now);
twitterAccountPersistence.update(twitterAccount, false);
if (jsonArray == null) {
return;
}
}
if (jsonArray == null) {
jsonArray = getUserTimelineJSONArray(twitterScreenName,
twitterAccount.getLastStatusId());
}
if ((jsonArray == null) || (jsonArray.length() == 0)) {
return;
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject statusJSON = jsonArray.getJSONObject(i);
SimpleDateFormat sdf = new SimpleDateFormat(
"EEE MMM d hh:mm:ss Z yyyy");
Date createDate = null;
try {
createDate = sdf.parse(statusJSON.getString("created_at"));
} catch (ParseException pe) {
throw new SystemException(pe);
}
long statusId = statusJSON.getLong("id");
String text = statusJSON.getString("text");
JSONObject userJSON = statusJSON.getJSONObject("user");
ImageURL = userJSON.getString("profile_image_url");
if (twitterAccount.getTwitterUserId() <= 0) {
userJSON = statusJSON.getJSONObject("user");
twitterAccount.setTwitterUserId(userJSON.getLong("id"));
ImageURL = userJSON.getString("profile_image_url");
twitterAccount.setImageUrl(ImageURL);
}
if (twitterAccount.getLastStatusId() < statusId) {
twitterAccount.setLastStatusId(statusId);
twitterAccount.setImageUrl(ImageURL);
twitterAccountPersistence.update(twitterAccount, false);
}
JSONObject extraData = JSONFactoryUtil.createJSONObject();
extraData.put("text", text);
TwitterFeed twitterFeed = TwitterFeedLocalServiceUtil
.createTwitterFeed(CounterLocalServiceUtil
.increment(TwitterFeed.class.getName()));
twitterFeed.setAccountId(twitterAccount.getAccountId());
twitterFeed.setCreateDate(createDate);
System.out.println("text===============" + text);
twitterFeed.setFeedMessage(text);
Date today = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(today);
cal.add(Calendar.DAY_OF_MONTH, -30);
SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date thirtydaysold = null;
try {
String date = sdfd.format(cal.getTime());
createDate = sdfd.parse(date);
} catch (ParseException pe) {
}
/*
* List<TwitterFeed> twitterFeedsold = TwitterFeedLocalServiceUtil
* .getOldTwitterFeeds(twitterAccount.getAccountId(),
* thirtydaysold);
* TwitterFeedLocalServiceUtil.deleteoldfeeds(twitterFeedsold);
*/
/*
* SocialActivityLocalServiceUtil.addActivity( user.getUserId(), 0,
* createDate, Feed.class.getName(), statusId,
* TwitterActivityKeys.ADD_STATUS, extraData.toString(), 0);
*/
TwitterFeedLocalServiceUtil.addTwitterFeed(twitterFeed);
// System.out.println("\n\n\n\n\ntwitterFeed>>>>>>>>" + twitterFeed
// +"\n\n\n\n\n");
}
twitterAccount.setModifiedDate(now);
twitterAccount.setImageUrl(ImageURL);
twitterAccountPersistence.update(twitterAccount, false);
}
protected JSONArray getUserTimelineJSONArray(String twitterScreenName,
long sinceId) {
// System.out.println("sinceId>>>#######################################################>>>>>>>>>>>"
// + sinceId);
try {
String url = _URL + twitterScreenName + ".json";
if (sinceId > 0) {
url += "?since_id=" + sinceId;
}
return JSONFactoryUtil.createJSONArray(HttpUtil.URLtoString(url));
} catch (Exception e) {
if (_log.isWarnEnabled()) {
_log.warn(e, e);
}
return null;
}
}
Remove portlet from page
To remove portlet from Page when portlet is empty.
renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.FALSE)
renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.FALSE)
07 August 2012
Subscribe to:
Posts (Atom)