Monday, December 19, 2016

Get Liferay Server folder path in liferay

To get the exact path of your server folder in Liferay , then the below code will be help you inspite of any Operating System.

import java.util.Properties;

Properties properties = PortalUtil.getPortalProperties();
String serverHome = properties.getProperty("liferay.home");

Thursday, December 15, 2016

Custom entity Indexer in Liferay

Planning to create the Indexer for your custom entity then here is an solution for that.

To search the custom table content through the search portlet of  Liferay, then we will create the Indexer(which is used for search).

Step 1 : Create your custom entity in service.xml

<entity name="myDB" local-service="true" remote-service="false" cache-enabled="true" >
<column name="manageentryID" type="long" primary="true" />
<column name="groupId" type="long" />
<column name="companyId" type="long" />
<column name="userId" type="long" />
<column name="lastModifiedDate" type="Date" />
<column name="myID" type="String" />
<column name="myName" type="String" />

<finder name="myID" return-type="myDB">
        <finder-column name="indexIsin" />
    </finder>
</entity>


Step 2 :- Create Indexer class in your portlet which will extends the BaseIndexer

ex:-

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.search.BaseIndexer;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.DocumentImpl;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchEngineUtil;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.util.GetterUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.portlet.PortletURL;

import org.apache.log4j.Logger;

public class Indexer extends BaseIndexer{

private static final Logger log = Logger.getLogger(Indexer.class);
public static final String[] CLASS_NAMES = { MyDB.class.getName() };
public static final String PORTLET_ID = "myportlet_WAR_myportlet";
public static int COUNT = getCount();

public static int getCount() {
int count = 1;
try {
count = MyDBLocalServiceUtil.getMyDBsCount();
} catch (SystemException e) {
e.printStackTrace();
}
return count;
}

@Override
public String[] getClassNames() {
// TODO Auto-generated method stub
return CLASS_NAMES;
}

@Override
public String getPortletId() {
// TODO Auto-generated method stub
return PORTLET_ID;
}

@Override
protected void doDelete(Object obj) throws Exception {
// TODO Auto-generated method stub

}

protected Document getDocumentUpdate(Object obj) throws Exception {

MyDB myDB = (MyDB) obj;
long groupId = getParentGroupId(myDB.getGroupId());
long scopeGroupId = myDB.getGroupId();
long companyId = myDB.getCompanyId();
String myID = myDB.getmyID();
String title = myDB.getmyName();

Document document = getBaseModelDocument(PORTLET_ID, MyDB);
document.addKeyword(Field.COMPANY_ID, companyId);
document.addKeyword(Field.GROUP_ID, groupId);
document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);

document.addKeyword("myID", myID);
document.addText(Field.TITLE, title);
return document;
}

@Override
protected Document doGetDocument(Object obj) throws Exception {
// TODO Auto-generated method stub
MyDB myDB = (MyDB) obj;
log.info("-----------  Adding the Document ------------");
long groupId = myDB.getGroupId();
long scopeGroupId = myDB.getGroupId();
long companyId = myDB.getCompanyId();
long userId = myDB.getUserId();
long manageentryID = myDB.getManageentryID();
Date modifiedDate = myDB.getLastModifiedDate();
String myID = myDB.getmyID();
String title = myDB.getmyName();
log.info("----------- Document ------------" + myDB.getManageentryID());
Document document = new DocumentImpl();//Way 2 usefull for both common jar or jar in same war
//Document document = getBaseModelDocument(PORTLET_ID, MyDB);//Way 1 It throws an error if your service jar is not available in same war
document.addUID(PORTLET_ID, manageentryID);// Mandatory for Way 2
document.addModifiedDate(modifiedDate);
document.addKeyword(Field.COMPANY_ID, companyId);//Mandatory for Way 1 or 2
document.addKeyword(Field.PORTLET_ID, PORTLET_ID);// Mandatory for Way 2
document.addKeyword(Field.GROUP_ID, groupId);//Mandatory for Way 1 or 2
document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);//Mandatory for Way 1 or 2
document.addKeyword(Field.USER_ID, userId); // Mandatory for Way 2
document.addKeyword(Field.ENTRY_CLASS_NAME, MyDB.class.getName());// Mandatory for Way 2
document.addKeyword(Field.ENTRY_CLASS_PK, manageentryID);// Mandatory for Way 2
document.addText(Field.TITLE, title); //Mandatory for Way 1 or 2
document.addKeyword("myID", myID);
document.addText(Field.TITLE, title); //Mandatory for Way 1 or 2
log.info("----------- myID IN DOCUMENT ------------" + document.get("myID"));
return document;
}

@Override
protected Summary doGetSummary(Document document, Locale locale,
String snippet, PortletURL portletURL) throws Exception {
String myID = document.get("myID");
MyDB myDB = null;
List totalRecord = new ArrayList();
if (myID.length() > 0) {
myDB = MyDBLocalServiceUtil.fetchBymyID(myID);
}
String title = document.get(Field.TITLE);
String content = snippet;
List indexList = null;

indexList = MyDBLocalServiceUtil.getMyDBs(0, -1);
for (MyDB tr : indexList) {
totalRecord.add(tr);
}
int pos = totalRecord.indexOf(myDB) + 1;
if (pos > 0) {
int page = pos / 10;
if (pos % 10 > 0) {
page = page + 1;
}
if (page == 0) {
page = 1;
}
portletURL.setParameter("cur", String.valueOf(page));
}
return new Summary(title, content, portletURL);
}


@Override
protected void doReindex(Object obj) throws Exception {
// TODO Auto-generated method stub
MyDB myDB = (MyDB) obj;
String searchEngineId = SearchEngineUtil.getDefaultSearchEngineId();
Document document = null;
try {
document = getDocument(MyDB);
SearchEngineUtil.updateDocument(searchEngineId,myDB.getCompanyId(), document);
} catch (Exception e) {
log.error("error while add document", e);
}
}

protected void doReindexUpdate(Object obj) throws Exception {
MyDB myDB = (MyDB) obj;
String searchEngineId = SearchEngineUtil.getDefaultSearchEngineId();
SearchEngineUtil.updateDocument(searchEngineId, myDB.getCompanyId(),
getDocumentUpdate(MyDB));
}

@Override
protected void doReindex(String className, long classPK) throws Exception {
// TODO Auto-generated method stub
MyDB myDB = MyDBLocalServiceUtil.getMyDB(classPK);
doReindex(MyDB);

}

@Override
protected void doReindex(String[] ids) throws Exception {
// TODO Auto-generated method stub
long companyId = GetterUtil.getLong(ids[0]);
doReindexAll(companyId);
}

private void doReindexAll(long companyId) throws Exception {
int count = MyDBLocalServiceUtil.getMyDBsCount();
int pages = count / Indexer.DEFAULT_INTERVAL;
for (int i = 0; i <= pages; i++) {
int start = (i * Indexer.DEFAULT_INTERVAL);
int end = start + Indexer.DEFAULT_INTERVAL;
reindexMyDB(companyId, start, end);
}
}

protected void reindexMyDB(long companyId, int start, int end)
throws Exception {
List myDBlist = MyDBLocalServiceUtil.getMyDBs(-1, -1);
if (myDBlist.isEmpty()) {
return;
}
Collection documents = new ArrayList();
COUNT = 1;
for (MyDB myDB : IndexDBlist) {
Document document = getDocument(myDB);
document.addText("count", String.valueOf(COUNT++));
documents.add(document);
}
String searchEngineId = SearchEngineUtil.getDefaultSearchEngineId();
SearchEngineUtil.updateDocuments(searchEngineId, companyId, documents);
}
@Override
protected String getPortletId(SearchContext searchContext) {
// TODO Auto-generated method stub
return PORTLET_ID;
}

}


Step 3 :- make an Indexer entry in your liferay-portlet.xml

<icon>/icon.png</icon>
<indexer-class>com.mypackage.Indexer</indexer-class>


Step 4 : call the Indexer while updating or inserting data into the data base myDB

public void createEntry(ThemeDisplay themeDisplay){
MyDB createdMyDBObject;
MyDB myDB = MyDBLocalServiceUtil.createMyDB("1234");
myDB.setGroupId(themeDisplay.getLayout().getGroupId());
myDB.setCompanyId(themeDisplay.getCompanyId());
myDB.setmyID("I786");
myDB.setUserId(themeDisplay.getUserId());
myDB.setmyName("I786");
try {
createdMyDBObject = MyDBLocalServiceUtil.updateMyDBFromCSV(myDB);
Indexer indexer = IndexerRegistryUtil.getIndexer(MyDB.class);
if(Validator.isNotNull(createdIndexDBObject))
indexer.reindex(createdIndexDBObject);
} catch (SystemException | SearchException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}




Liferay DXP JNDI Data Source Cofiguration

 This Blog will help us to learn about the JNDI Data Source Configuration in Liferay DXP. We have tested this with Liferay 7.3 with Tomcat. ...