Wednesday 30 August 2017

Liferay integration with solr 



Configure solr with Tomcat 7 


Step -1 
-  Download Solr from apache website. Here we are using solr 4.*.*.(solr-4.3.1.tgz)

-  Download tomcat from apache website(apache-tomcat-7.0.75.zip).

-  Create a  folder called "solr".

-  Unzip both files on solr folder.(  tar -xvf yourfile.tar)

-  Copy solr<version>.war from <extracted_solr>\dist 
    to <extracted_tomcat>\webapps and rename solr<version>.war to solr.war.

-  Copy all jars in <extracted_solr>\example\lib\ext  to <extracted_tomcat>\lib.
      Change the port to 8006

         <Server port="8005" shutdown="SHUTDOWN">
             Change ajp port to 8008
         <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
-  Change the port to 8983  in server.xml file of tomcat . (in different instance no need for this step)

-  Start the tomcat.
-  Check New folder names solr would be available in webapps.

-  Shutdown the tomcat.

-  Modify web.xml located at <extracted_tomcat>\webapps\solr\WEB-INF
         Uncomment below entry and update the path for solr\home.

      <env-entry>  <env-entry-name>solr/home</env-entry-name>   <env-entry-value>    <extracted_solr>C:/Solr/solr-4.3.1/example/solr</env-entry-value> <env-entry-type>java.lang.String</env-entry-type></env-entry>

- Start the tomcat and access the below URL.
      http://[IP ADDRESS]:[PORT]/solr or  http://localhost:[PORT]/solr

Step-2:

Setup solr-web plugins in liferay :

*  Download the solr web from liferay market place (Liferay Solr 4 Search Engine.lpkg) .
*  Copy the solr-web war file to deploy folder of liferay-server
*  After solr-web  plugin is deployed in Liferay, stop Liferay Tomcat server.
-  Go to <Liferay>/tomcat-7.0.27/webapps/solr-web/WEB-INF/conf directory.
-  Copy and replace schema.xml file to <extracted_solr>example/solr/collection1/conf directory on         solr server.

* Go to < liferay>/tomcat-7.0.42/webapps/solr4-web/WEB-INF/classes/ 
    Edit solr.properties file to update  with property server.http.url
    And value should be< solr-ip>:8983/solr


* Edit the solrconfig.xml file and update the values as below 
     <extracted_solr>example/solr/collection1/conf directory on Solr Server.
        <autoCommit> 
             <maxTime>10000</maxTime> 
            <openSearcher>true</openSearcher> 
     </autoCommit>

*  Creare the log4j.properties file in C:\Solr\apache-tomcat-7.0.75\lib and add the below properties

    #  Logging level
       log4j.rootLogger=INFO, file, CONSOLE

       log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

   #log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
     log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n

   #- size rotation with log cleanup.
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.MaxFileSize=4MB
    log4j.appender.file.MaxBackupIndex=9

   #- File to log to and log format
    log4j.appender.file.File=C:\\Solr\\apache-tomcat-7.0.75\\logs\\solr.log
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n

    log4j.logger.org.apache.zookeeper=WARN

- After above changes are done, first start the Solr Tomcat Server after start Liferay Tomcat server.
- When Liferay Server starts successfully, follow below steps,
     -> Login with admin user
     -> Go to the Control Panel > Server > Server Administration.
     -> Click on Execute Button beside Reindex all search indexes.

Tuesday 21 February 2017

Update Document and Media MetaData Field values : 
=========================================


I want to store documents in specific User own document type.

Goto Content -> Document and Media -> click Manage -> Click Add Document Types

Name : SSC Document

Add the 3 MetaData fields for SSC Document like Expire Date ,Date and Language.

<root available-locales="en_US" default-locale="en_US">
<dynamic-element dataType="string" indexType="keyword" localizable="true" multiple="false" name="select" readOnly="false" repeatable="false" required="false" showLabel="true" type="select" width="">
<dynamic-element name="option3863" type="option" value="English">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[English]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element name="option5214" type="option" value="French">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[French]]>
</entry>
</meta-data>
</dynamic-element>
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Select Language]]>
</entry>
<entry name="predefinedValue">
<![CDATA[[""]]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element dataType="date" fieldNamespace="ddm" indexType="keyword" localizable="true" name="expiryDate" readOnly="false" repeatable="false" required="false" showLabel="true" type="ddm-date" width="small">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Expiry Date]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element dataType="string" indexType="keyword" localizable="true" multiple="false" name="role" readOnly="false" repeatable="false" required="false" showLabel="true" type="select" width="">
<dynamic-element name="option4347" type="option" value="Sales Document Approver">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Sales Document Approver]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element name="option4348" type="option" value="Parts Document Approver">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Parts Document Approver]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element name="option4349" type="option" value="Service Document Approver">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Service Document Approver]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element name="option4350" type="option" value="Marketing Document Approver">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Marketing Document Approver]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element name="option4351" type="option" value="Management Document Approver">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Management Document Approver]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element name="option4352" type="option" value="Accounting Document Approver">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Accounting Document Approver]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element name="option3746" type="option" value="Portal Admin Document Approver">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Portal Admin Document Approver]]>
</entry>
</meta-data>
</dynamic-element>
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Approved By]]>
</entry>
<entry name="predefinedValue">
<![CDATA[[""]]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
</root>


While adding the documents in Document and Media.

Goto Content -> Document and Media -> Click Add section and Select SSC Document

Add the document and give to fields values like Expire Date ,Date and Language

Save the document.

Update the ExpiryDate MetaData field value programatically:

Step 1 : 

get the SSC Document structure id value
DDMStructure agcoStructure = null;
DLFileEntry dlFileEntry = null;
List<DDMStructure> ddmStructure = DDMStructureLocalServiceUtil.getDDMStructures(0,DDMStructureLocalServiceUtil.getDDMStructuresCount());

for (DDMStructure structure : ddmStructure) {
if (structure.getName(Locale.getDefault()).equalsIgnoreCase(DocumentUpdaterConstants.AGCO_DOCUMENT_TYPE)) {
agcoStructure = structure;
break;
}
}

Step 2 :

Get the dlfileentry Id

ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);
long companyId = themeDisplay.getCompanyId();
String fileEntryUUId = "d5254c81-8924-4d24-bd5e-c6f3fdce1113";
dlFileEntry = DLFileEntryLocalServiceUtil.getDLFileEntryByUuidAndCompanyId(fileEntryUUId, companyId);

long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();
long version = dlFileEntry.getFileVersion().getFileVersionId();
long dlfileEntryId = dlFileEntry.getFileEntryId();

String expirationDate = "6/28/2016";

Step 3 :

Convert string to date and Make sure every date value will be store in string value so,convert date to Serializable
Serializable date = null;
SimpleDateFormat simpleDateFomat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedExpirationDate = null;
convertedExpirationDate = simpleDateFomat.parse(expirationDate);
date = String.valueOf(convertedExpirationDate.getTime());

Step 4:

Get the MetaData filed value
Field fieldExpiryDate = new Field("expiryDate", date);  // expiryDate field value identify in DDMConent table

Step5 : 

Create the object of field class

Fields fields = new Fields();
fields.put(fieldExpiryDate);

Step 6 :

Create the map object generic classes of fields and pass the Structure key
Map<String, Fields> fieldsMapFinal = new HashMap<String, Fields>();
fieldsMapFinal.put(agcoStructure.getStructureKey(),fields);
DLFileEntryMetadataLocalServiceUtil.updateFileEntryMetadata(fileEntryTypeId,dlfileEntryId, version, fieldsMapFinal,serviceContextDLFile);

update success

Step 7:

When ever we can modify or update the Document new version will be created
In the above case new version couldn't create, follow below it will be update new version

ServiceContext serviceContext = new ServiceContext();
String sourceFileName = dlFileEntry.getTitle();
serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);
FileEntry fileEntry = DLAppServiceUtil.getFileEntry(themeDisplay.getSiteGroupId(), dlFileEntry.getFolderId(),dlFileEntry.getTitle());
InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(themeDisplay.getUserId(), dlfileEntryId,fileEntry.getVersion(), false);
File file = DLFileEntryLocalServiceUtil.getFile(themeDisplay.getUserId(), dlfileEntryId,fileEntry.getVersion(), false);
DLAppServiceUtil.updateFileEntry(dlfileEntryId, sourceFileName,MimeTypesUtil.getContentType(dlFileEntry.getTitle()),dlFileEntry.getTitle(),
dlFileEntry.getDescription().concat(StringPool.SPACE),StringPool.BLANK, Boolean.FALSE, is, file.length(),serviceContext);

  Inter Portlet Communication (IPC) in Liferay 7.4 Public Render Parameter IPC: 1) Create a new module project -> SenderPortlet 2) In  Se...