Showing posts with label Liferay Service Builder. Show all posts
Showing posts with label Liferay Service Builder. Show all posts

Friday, November 29, 2013

Kaleo Workflow Configuration for Custom Portlet in Liferay 6.1

1. Make sure you have deployed kaleo workflow war in your liferay portal.

2. Create the service.xml


3. Build the service

4. Edit the FeedbackLocalServiceImpl class and add the following methods:



5. Make the following entries in liferay-portlet.xml file



6. create the FeedbackAssetRendererFactory class



7. create the FeedbackAssetRenderer class-



8. create the /html/feedback.jsp file.


9. create the FeedbackWorkflowHandler class -



10. We are done with all the changes now, to see our portlet in action go to control panel → Workflow Configuration and set the workflow for your custom portlet.

Sunday, September 1, 2013

Connecting Liferay with Another Database

In the following post we will connect liferay with some third party database (legacy database):

Method 1:

1. Create Service.xml

2. Create ext-spring.xml file as /WEB-INF/src/META-INF/ext-spring.xml

You wont believe but yes we are done!!


Method 2:

1. Make an entry in portal-ext.properties file for another database:

2. create service.xml:

3. create ext-spring.xml file:


done!


NOTE: In both the above methods, we have to create the table manually in the database.

Thursday, May 23, 2013

Spring MVC Portlet Validations

In the previous post we have implemented spring mvc portlet using Liferay Portal. In the following post we are going to implement input validation for adding new student form. We will use Spring’s Validator framework for validating data entered during adding new student.

1. First of all we will create StudentValidator class which implements Spring’s Validator interface.


[code language="java"]
@Component("studentValidator")
public class StudentValidator implements Validator{

@Override
public boolean supports(Class clazz) {
return Student.class.isAssignableFrom(clazz);
}

@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name", "Field name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "subject", "required.subject", "Field subject is required.");
}
}
[/code]

2. Now we will make changes in StudentController class

a. The @Autowired annotation is used to instruct the Spring container to inject the dependency. We will use this to inject StudentValidator in our StudentController class:

[code language="java"]
@Autowired
StudentValidator studentValidator;
[/code]

b. Modify the addStudent method to insert validation code -

[code language="java"]
@ActionMapping(params = "myaction=addStudent")
public void addStudent(@ModelAttribute("student") StudentImpl student, BindingResult result, ActionResponse actionResponse)
{
studentValidator.validate(student, result);
if(result.hasErrors())
{
actionResponse.setRenderParameter("myaction", "addStudentForm");
}
else
{
try {
long studentId = CounterLocalServiceUtil.increment(Student.class.getName());
student.setPrimaryKey(studentId);
StudentLocalServiceUtil.addStudent(student);
} catch (SystemException e) {
e.printStackTrace();
}
}
}
[/code]

c. modify the showAddStudentForm method -

[code language="java"]
@RenderMapping(params = "myaction=addStudentForm")
public String showAddStudentForm(Model model) throws SystemException
{
System.out.println("showAddStudentForm Called !!!!!!!!!!!");
return "addStudent";
}
[/code]

d. Add the following new method -

[code language="java"]
@ModelAttribute("student")
public Student getCommandObject() {
return new StudentImpl();
}
[/code]

3. make changes in addStudent.jsp file to display error messages-

[code language="html"]
<form:form action="<%=AddStudentURL.toString()%>" method="post" commandName="student">
<table>
<form:errors path="*" />
<tr><td>Name :</td> <td><form:input path="name" /></td></tr>
<tr><td colspan="2"><form:errors path="name"></form:errors></td></tr>
<tr><td>Subject:</td> <td><form:input path="subject" /></td></tr>
<tr><td colspan="2"><form:errors path="subject"></form:errors></td></tr>
<tr><td colspan="2"><input type="submit" value="Save" /></td></tr>
</table>
</form:form>
[/code]

4. Add the following snippet to myContext.xml file

[code language="xml"]
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>content.Language_en</value>
</list>
</property>
</bean>
[/code]

5. Add an entry to portlet.xml file -

[code language="xml"]
<resource-bundle>content.Language_en</resource-bundle>
[/code]

6. create new file named Language_en.properties with following content in src/content folder -

[code language="java"]
required.name = name is required!
required.subject = subject is required!
[/code]

We are done with all the changes required!

Thursday, August 23, 2012

Liferay Custom JSON Web Service Development



Following is the step by step description of generating a custom liferay plugin service and exposing it as JSON Web Service.



STEP:1 Create a liferay plugin project and create new service


Here is the sample service.xml



Make sure remote-service=”true” in entity tag declaration.



STEP:2  Build the service.



STEP:3  Add your custom method in SampleLocalServiceImpl class



STEP:4  Build the service.



STEP:5  Add the method definition to SampleServiceImpl class



STEP:6  build the service.



STEP:7  Add the following <servlet> and <servlet-mapping> Entries to portlet's web.xml file -



STEP:8  deploy the portlet.


STEP:9  To access your json web services enter the following url -

http://localhost:8080/<<portlet-context>>/api/jsonws





and Its Done!!!





Thursday, June 9, 2011

Liferay Service Builder : Part 1 (Finder methods in liferay portal)

In liferay portal if we want to find records from table then we need finder methods. To create finder method we need to specify the finder tag in the service.xml file.

Assume that we have a database table named customer having fields like name, city, age etc. We want to find all the customers belonging to a particular city.

Following are the steps to create finder method for the above requirement-

1.  In the service.xml file add the following line after database columns declarations,

<finder name="City" return-type="Collection">
<finder-column name="city"></finder-column>
</finder>


Name – method name , a method named findByCity will be generated

Collection – return type

Finder column name – name of the corresponding column in the customer table

2.  Run the ant target "ant build-service". In the SampleUtil.java you will find the following method –

public static java.util.List<com.sample.model.Sample> findByCity(
java.lang.String city) throws com.liferay.portal.SystemException {
return getPersistence().findByCity(city);
}


3.  Copy the finder method from this class and paste it inside the SampleLocalServiceImpl class and modify the method definition as following

-       Remove the static modifier

-       Replace getPersistence() with SampleUtil.

4.  Run the target "ant build-service" again. After build finder will be available in your SampleLocalServiceUtil class.

List<Sample> customerList = SampleLocalServiceUtil.findByCity("cityname");

NOTE: Replace Sample with your EntityName.

Wednesday, February 23, 2011

Custom Sql in Liferay

1. Create a folder named custom-sql under the src folder.

2. Create a default.xml file in the custom-sql folder.

3. Create another xml file in the custom-sql folder, this file contains the custom sql query.

4. Create a class say SampleFinderImpl in the persistent package. This class will extends the BasePersistenceImpl<Sample> and implements SampleFinder.

5. Now build the service, it will generate the SampleFinder Interface.

6. Now define methods to execute custom sql in the SampleFinderImpl class.

7. Build the service; it will generate the SampleFinderUtil class with the method defined in the SampleFinderImpl class.

8. Copy the generated method definition from SampleFinderUtil and place it in the SampleLocalServiceImpl.

9. Build the service; it will place the methods definition in the SampleLocalServiceUtil from EntryLocalServiceImpl.



1. Create a folder named custom-sql under the src folder.


2. Create a default.xml file in the custom-sql folder.


3. Create another xml file in the custom-sql folder, this file contains the custom sql query.


4. Create a class named (EntryFinderImpl) in the persistent package. This class will extends the BasePersistenceImpl<Entry> and implements EntryFinder.


5. Now build the service, it will generate the EntryFinder Interface.


6. Now define methods to execute custom sql in the EntryFinderImpl class.


7. Build the service; it will generate the EntryFinderUtil class with the method defined in the EntryFinderImpl class.


8. Copy the generated method definition from EntryFinderUtil and place it in the EntryLocalServiceImpl.


9. Build the service; it will place the methods definition in the EntryLocalServiceUtil from EntryLocalServiceImpl.


10. Now the method is available to call from any portlet class.