Tuesday, April 5, 2011

New jQuery version into Liferay Portal

Copy the file "top_head.jsp" located in folder "liferay-portal-src-[version]\portal-web\docroot\html\common\themes\" into the EXT-folder 'liferay-portal-ext\ext-web\docroot\html\common\themes\'

>Open the copied file in your EXT
>Add the following lines:
<script  src="/html/js/jQuery1.3.2/jquery1.3.2.js" type="text/javascript"></script>
<script  src="/html/js/jQuery1.3.2/ui.jquery1.7.js" type="text/javascript"></script>
<script >="" src="/html/js/jQuery1.3.2/ui.stars.js" type="text/javascript"></script>

<script type="text/javascript">
var $132 = jQuery.noConflict(true);
 </script>
> Now you can access jQuery 1.3.2'.

Friday, January 14, 2011

Retrieve selected checkbox value in liferay 6 with aui:script and Liferay.provide

FYI: Everything inside of an aui:script block that contains a "use" attribute is called asynchronously, so it could fire right away (if the resources are on the page) or if the network resolution is taking a long time, it could execute later on.

Bellow is an example:


alert('not "using" any modules, Im fired right away');
alert('"using" aui-io, Im fired when its ready');


So we make sure never to define a function inside of an aui-script block that has a "use" attribute, unless we're creating another module to be used.

But then how do you define a function that uses a module?
Luckily, we can use Liferay.provide.

For simplicity here I have used two field which will indicated the checked and unchecked value of checkboks and can easily get this value on action class, like bellow:


<aui:input name="addCommIds" type="hidden" value="" />
<aui:input name="removeCommIds" type="hidden" value="" />


Bellow given the aui:script code:



Liferay.provide(
window,
'saveEntry',
function() {
document.fm.addCommIds.value = Liferay.Util.listCheckedExcept(document.fm, "allRowIds");
document.fm.removeCommIds.value = Liferay.Util.listUncheckedExcept(document.fm, "allRowIds");
submitForm(document.fm);
},
['liferay-util-list-fields']
);


NB: listCheckedExcept is a javascript function
we can simply call this function as bellow:


<aui:button onClick="<%= renderResponse.getNamespace() + "saveEntry();" %>"value="save" />


Source

Monday, January 3, 2011

Check Lucene directory failed for 0

If this type of error comes in development; actually I got this exception in liferay development and by googling found this occur for lucene storage error. So first what I have done: changed the lucene.store.type=file to lucene.store.type=jdbc in portal.properties. You may override it in portal-ext.properties of ext without modifying directly in liferay code.

After that exception may throw like bellows:

Check Lucene directory failed for 0
java.lang.NullPointerException
at org.apache.lucene.store.jdbc.support.JdbcTable.setName(JdbcTable.java:183)
at org.apache.lucene.store.jdbc.support.JdbcTable.<init>(JdbcTable.java:75)
at org.apache.lucene.store.jdbc.support.JdbcTable.<init>(JdbcTable.java:69)
at org.apache.lucene.store.jdbc.JdbcDirectory.<init>(JdbcDirectory.java:121)
at com.liferay.portal.search.lucene.IndexAccessorImpl._getLuceneDirJdbc(IndexAccessorImpl.java:318)
at com.liferay.portal.search.lucene.IndexAccessorImpl.getLuceneDir(IndexAccessorImpl.java:161)
at com.liferay.portal.search.lucene.IndexAccessorImpl._checkLuceneDir(IndexAccessorImpl.java:188)
at com.liferay.portal.search.lucene.IndexAccessorImpl.<init>(IndexAccessorImpl.java:77)
at com.liferay.portal.search.lucene.LuceneHelperImpl._getIndexAccessor(LuceneHelperImpl.java:287)
at com.liferay.portal.search.lucene.LuceneHelperImpl.deleteDocuments(LuceneHelperImpl.java:168)
at com.liferay.portal.search.lucene.LuceneHelperUtil.deleteDocuments(LuceneHelperUtil.java:165)
at com.liferay.portal.search.lucene.LuceneIndexWriterImpl.deletePortletDocuments(LuceneIndexWriterImpl.java:85)
at com.liferay.portal.kernel.search.messaging.SearchWriterMessageListener.doReceive(SearchWriterMessageListener.java:75)
at com.liferay.portal.kernel.search.messaging.SearchWriterMessageListener.receive(SearchWriterMessageListener.java:41)
at com.liferay.portal.kernel.messaging.InvokerMessageListener.receive(InvokerMessageListener.java:73)
at com.liferay.portal.kernel.messaging.ParallelDestination$1.run(ParallelDestination.java:68)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:650)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:675)
at java.lang.Thread.run(Thread.java:595)

Here you need to change little in database connection URL:

jdbc.default.url=jdbc:mysql://localhost/DBName?emulateLocators=true&useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false

Added emulateLocators=true in database URL; Also don't forget to clean database. now running ok for me :)

If error comes like bellow:

org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out:
at org.apache.lucene.store.Lock.obtain(Lock.java:70)
at org.apache.lucene.index.IndexWriter.init(IndexWriter.java:598)
at org.apache.lucene.index.IndexWriter.<init>(IndexWriter.java:410)
at com.liferay.portal.lucene.IndexWriterFactory.getWriter(IndexWriterFactory.java:182)
at com.liferay.portal.lucene.LuceneUtil.getWriter(LuceneUtil.java:285)
at com.liferay.portal.lucene.LuceneUtil.getWriter(LuceneUtil.java:279)
at com.liferay.portal.plugin.PluginPackageIndexer.addPluginPackage(PluginPackageIndexer.java:78)
at com.liferay.portal.plugin.PluginPackageIndexer.updatePluginPackage(PluginPackageIndexer.java:227)
at com.liferay.portal.plugin.PluginPackageUtil._indexPluginPackage(PluginPackageUtil.java:835)
at com.liferay.portal.plugin.PluginPackageUtil._parseRepositoryXml(PluginPackageUtil.java:987)
at com.liferay.portal.plugin.PluginPackageUtil._loadRepository(PluginPackageUtil.java:886)
at com.liferay.portal.plugin.PluginPackageUtil.reloadRepositories(PluginPackageUtil.java:612)
at com.liferay.portlet.admin.job.CheckRemoteRepositoriesJob.execute(CheckRemoteRepositoriesJob.java:47)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)



To solve this error delete your lucene directory, deploy all and restart server . Check your portal.properties or portal-ext.properties to figure out where this is, by default it is configured as:

lucene.dir=${liferay.home}/data/lucene/
liferay.home=${resource.repositories.root}
resource.repositories.root=${default.liferay.home}

Dynamic query in liferay 5.2.+

Liferay provides “LocalServiceUtil” to interact with database, we can do the common and basic functionality using these LocalServiceUtil classes. However sometimes we may need to run complex quires beside the common service like filtering out from result list or directly getting the data by certain criteria etc. Liferay provides several factory classes in this case to make our life easier, listed bellow:

DynamicQueryFactoryUtil
OrderFactoryUtil
ProjectionFactoryUtil
PropertyFactoryUtil
RestrictionsFactoryUtil

Here I will provide a example on how to use DynamicqueryFactoryUtil to make dynamic query.

ClassLoader cl = PortalClassLoaderUtil.getClassLoader();
DynamicQuery dqi = DynamicQueryFactoryUtil.forClass(
AnnouncementsEntry.class, cl);

Criterion crit = PropertyFactoryUtil.forName("title").eq(title);
dqi.add(crit);

crit = PropertyFactoryUtil.forName("displayDate").eq(displayDate);
dqi.add(crit);

try {
results = AnnouncementsEntryLocalServiceUtil.dynamicQuery(dqi);

} catch (SystemException e) {
e.printStackTrace();
}

classloader ensure that the implementation class can be loaded . Propertyfactoryutil may used for checking certain feature like equality, between, empty, nonempty etc ie. Projection/ restriction. Actually PropertyFactoryUtil return a Property instance that allows us to create Criterion objects and to assign them in DynamicQuery.

Portlet Preference of liferay 5.2.3

Load user specific preference:


com.liferay.portal.model.Layout layout = ((com.liferay.portal.theme.ThemeDisplay)themeDisplay).getLayout();
javax.portlet.PortletPreferences portletSetup = com.liferay.portlet.PortletPreferencesFactoryUtil
.getLayoutPortletSetup(layout, "portlet_id");

try {
portletSetup.setValue("key", "value");
portletSetup.getValue("key", "d
efault value");
} catch (ReadOnlyException e1) {
e1.printStackTrace();
}
here themeDisplay is user specific themeDispaly as a result it load user specific preference.

Load global preference:


javax.portlet.PortletPreferences portletSetup = actionRequest.getPreferences();

try {
portletSetup.setValue("key", "value");
portletSetup.getValue("key", "default value");
} catch (ReadOnlyException e1) {
e1.printStackTrace();
}
here actionRequest may be javax.portlet.RenderRequest or javax.portlet.ActionRequest or javax.portlet.PortletRequest

You need to set the following properties in liferay-portlet.xml or in case of overridden in ext liferay-portlet-ext.xml, if want to set company wise/ global preference for portlet:


<preferences-company-wide>true</preferences-company-wide>
<preferences-unique-per-layout>false</preferences-unique-per-layout>

TIPS: Using jquery in Liferay 6 through Alloy

We just cannot use alloy object with the jquery because Each jQuery object created with the jQuery() function contains a number of properties alongside with its methods. These properties allow us to inspect various attributes of the object. To solve this problem we can use getDom method of alloy to get dom and only then we can use them with jquery
Example:
var parentNode = node.get('parentNode');
jQuery(“.Class”,parentNode.getDOM())