Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Randy Parsons
Retrieving CalEvents based on Event Types
November 27, 2012 11:12 AM
Answer

Randy Parsons

Rank: Junior Member

Posts: 33

Join Date: January 4, 2011

Recent Posts

Can someone please help me understand why I get an exception error when attempting to retrieve CalEvent objects based on a List of String Event Types. I have reviewed the API documentation at it clearly indicates I can do this.

For testing, I have successfully retrieved the count of CalEvents based on a single Event Type (my case: "holiday") using the following code: (so I know I have all the required imports)
1Holiday Events Count: <%= CalEventLocalServiceUtil.getEventsCount(scopeGroupId, "holiday") %><br />

but, when I attempt the following I get an exception error:
1<%
2List<String> eventTypes = new ArrayList<String>();
3eventTypes.add(0, "holiday");
4eventTypes.add(1, "event");
5%<
6
7Filter Events Count: <%= CalEventLocalServiceUtil.getEventsCount(scopeGroupId, eventTypes) %><br />

Error I Receive: "The method getEventsCount(long, String) in the type CalEventLocalServiceUtil is not applicable for the arguments (Long, List<String>)

From the API:
static int getEventsCount(long groupId, String type)
static int getEventsCount(long groupId, String[] types)

Update:
======
I decided to try using Arrays instead of Lists (still learning so I thought [ ] implies arrays). Modified code still gives error - but a different one
Approach 2
1<%
2String[] eventTypes = new String[1];
3eventTypes[0]="holiday";
4eventTypes[1]="event";

New Error: "java.lang.ArrayIndexOutOfBoundsException: 1"

Any assistance would be greatly appreciated.
Randy Parsons
RE: Retrieving CalEvents based on Event Types
November 27, 2012 11:33 AM
Answer

Randy Parsons

Rank: Junior Member

Posts: 33

Join Date: January 4, 2011

Recent Posts

Well I found out my error. You must use a String Array and not a List<String>

Here is the code for anyone looking for to retrieve all CalEvents using a filter of Event Types. Of course, you would probably want to have the list dynamic.

 1//Initialize the Array:
 2String[] eventTypes = new String[] {
 3    "holiday","event"
 4};
 5
 6//Sets a calendar object of today based on user's local and timezone
 7Calendar curCal = CalendarFactoryUtil.getCalendar(timeZone, locale);
 8
 9//Initialize List of CalEvents
10List<CalEvent> todayEvents = new ArrayList<CalEvent>();
11
12//Get all events filtered using the eventTypes Array
13todayEvents.addAll(CalEventLocalServiceUtil.getEvents(scopeGroupId, curCal, eventTypes));


I hope that helps anyone.