Sunday, 18 May 2025

Polls in liferay dxp

Polls in Liferay DXP

Liferay DXP provides a way to create and manage polls either using its classic built-in Polls portlet or by developing your own custom Poll feature using Service Builder. In this blog, we’ll explore both approaches.

✅ Option 1: Using Built-In Polls (If Available)

Liferay used to ship with a Polls portlet in older versions, which may still be available in some distributions via Marketplace. You can:

  • Deploy the Polls portlet from Liferay Marketplace.
  • Add the Polls widget to a site page.
  • Create a poll question, define choices, and track responses.

🛠 Option 2: Custom Poll Portlet with Service Builder

If you want full control or Polls is not available, you can create your own:

Step 1: Create Modules

  • poll-api – Contains service interfaces and models
  • poll-service – Contains service implementations
  • poll-web – Contains portlet UI

Step 2: Define Entities in Service Builder XML

<entity name="Poll" local-service="true" remote-service="false">
    <column name="pollId" type="long" primary="true" />
    <column name="question" type="String" />
    <column name="createDate" type="Date" />
    <column name="modifiedDate" type="Date" />
</entity>

<entity name="PollOption" local-service="true" remote-service="false">
    <column name="optionId" type="long" primary="true" />
    <column name="pollId" type="long" />
    <column name="optionText" type="String" />
    <column name="votes" type="int" />
</entity>

Step 3: Run Service Builder

Run the command:

./gradlew buildService

Step 4: Add Service Methods

In `PollLocalServiceImpl`, add methods like:

public Poll addPoll(String question, ServiceContext serviceContext) {
    long pollId = counterLocalService.increment(Poll.class.getName());

    Poll poll = pollPersistence.create(pollId);
    poll.setQuestion(question);
    poll.setCreateDate(new Date());

    return pollPersistence.update(poll);
}

Step 5: Create the UI (poll-web module)

Use JSP or React to display:

  • The poll question
  • Options as radio buttons
  • Submit button to vote
  • Vote count after submission

Example Form Snippet

<form action="<portlet:actionURL name='submitVote' />" method="post">
    <label>What is your favorite programming language?</label><br/>
    <input type="radio" name="optionId" value="1" /> Java<br/>
    <input type="radio" name="optionId" value="2" /> Python<br/>
    <input type="radio" name="optionId" value="3" /> JavaScript<br/>
    <button type="submit">Vote</button>
</form>

📊 Viewing Results

You can display a summary below the poll form with vote counts using a method in your service layer like:

public Map<String, Integer> getResults(long pollId) {
    List<PollOption> options = pollOptionPersistence.findByPollId(pollId);
    Map<String, Integer> results = new HashMap<>();
    for (PollOption option : options) {
        results.put(option.getOptionText(), option.getVotes());
    }
    return results;
}

🔒 Permissions

Use Liferay’s permission framework to restrict who can vote or view poll results.

📦 Deploy and Test

Deploy the API, Service, and Web modules using Gradle. Add the portlet to a page and verify everything works as expected.

🧠 Conclusion

Polls in Liferay can be simple or complex depending on your needs. If built-in support is not present, creating a custom poll feature with Service Builder gives you full flexibility over functionality, UI, and data handling.

📚 References

No comments:

Post a Comment