掲示板

delete cal events by type

8年前 に juan meza によって更新されました。

delete cal events by type

Regular Member 投稿: 228 参加年月日: 14/01/06 最新の投稿
hi, i have liferay 6.2 GA4

i have a portlet where i add to the calendar all the users birthdays... as events.. i use this code for that:
// Start
		try {
			long scopeGroupId = themeDisplay.getScopeGroupId();
			ServiceContext serviceContext = new ServiceContext();
			serviceContext.setScopeGroupId(scopeGroupId);

			Calendar cal = Calendar.getInstance();   // GregorianCalendar
			cal.setTime(user.getBirthday());
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH);
			int day = cal.get(Calendar.DAY_OF_MONTH);
			
			//Create a Calendar Event
			CalEvent calEvent = CalEventLocalServiceUtil.addEvent(
					user.getUserId(), //userId
					"Happy birthday!", //title
					"Today is "+user.getFullName()+"'s birthday!", //description
					"´location", //location
					month, //int startDateMonth || 0 > Jan || 1 > Feb || 2 > Mar || 3 > Apr||.....|| 11 > Dec||
					day, //int startDateDay,
					2015, //int startDateYear,
					9, //int startDateHour,
					0, //int startDateMinute,
					1, //int durationHour,
					0, //int durationMinute,
					false, //boolean allDay,
					true, //boolean timeZoneSensitive,
					"Birthday", //String type,
					false, //boolean repeating,
					null, //TZSRecurrence recurrence,
					0, //int remindBy,
					0, //int firstReminder,
					0, // int secondReminder,
					serviceContext);

			//Import the event to Calendar Booking - Requires calendar-portlet-service.jar(tomcat-7.0.42\webapps\calendar-portlet\WEB-INF\lib)
			CalendarImporterLocalServiceUtil.importCalEvent(calEvent);

		} catch (com.liferay.portal.kernel.exception.PortalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (com.liferay.portal.kernel.exception.SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// End



but it adds repeted birthdays... so every time i run the process to add all birthdays, i want to delete them all first, so there are no repeated birthdays...
but i cant delete the events, and the events just keep acomulating...
im using this code to delete:
//delete all birthdays events, so they can be added again, and not be repeated
        DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(CalEvent.class);
		Conjunction primaryCriteria = RestrictionsFactoryUtil.conjunction();
		primaryCriteria.add(PropertyFactoryUtil.forName("type").eq("Birthday"));
		primaryCriteria.add(PropertyFactoryUtil.forName("groupId").eq(themeDisplay.getScopeGroupId()));
		dynamicQuery.add(primaryCriteria);

		List<calevent> calEvents = (List)CalEventLocalServiceUtil.dynamicQuery(dynamicQuery);
		for(CalEvent event:calEvents){
			System.out.println(event.getEventId());
			System.out.println(event.getType());
			
			CalEventLocalServiceUtil.deleteEvent(event);
			//CalEventLocalServiceUtil.deleteEvents(themeDisplay.getScopeGroupId());
		}</calevent>


what am i doing wrong? i belive this should delete them, but it doesnt work... they just keep piling up
how can i do this?

thank you!
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: delete cal events by type

Liferay Legend 投稿: 14914 参加年月日: 06/09/02 最新の投稿
So is it printing the event id and type for the events to be deleted?

Honestly I'm not sure what role the conjuction plays here as the default is to use 'and', so you should be able to just do:

dynamicQuery.add(PropertyFactoryUtil.forName("type").eq("Birthday"));
dynamicQuery.add(PropertyFactoryUtil.forName("groupId").eq(themeDisplay.getScopeGroupId()));

I also think your DQ usage is broken in that it's not using the appropriate constructor, the one that takes the class loader where the classes originate (the service portlet's class loader).
8年前 に juan meza によって更新されました。

RE: delete cal events by type

Regular Member 投稿: 228 参加年月日: 14/01/06 最新の投稿
it does print something...

i dont know what it is because they are repeated, the events... but it only prints them 1 time, so i dont know why that is...

it also prints the same if i delete them all in the calendar portlet... i dont know if they are in some sort of cache or something like that, so the DQ keeps finding them...

but in short, it does get the eventId, i tried deleting by eventId... but with same results
8年前 に juan meza によって更新されました。

RE: delete cal events by type (回答)

Regular Member 投稿: 228 参加年月日: 14/01/06 最新の投稿
managed to delete events by title, worked for me, here is how:

ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader("1_WAR_calendarportlet");
        DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(CalendarBooking.class, classLoader);
        
		Conjunction primaryCriteria = RestrictionsFactoryUtil.conjunction();
		primaryCriteria.add(PropertyFactoryUtil.forName("title").eq("Happy birthday!"));
		primaryCriteria.add(PropertyFactoryUtil.forName("groupId").eq(themeDisplay.getScopeGroupId()));
		dynamicQuery.add(primaryCriteria);

		List<calendarbooking> calendarBookings = (List)CalendarBookingLocalServiceUtil.dynamicQuery(dynamicQuery);
		for(CalendarBooking event:calendarBookings){
			CalendarBookingLocalServiceUtil.deleteCalendarBooking(event);
		}</calendarbooking>


all the events i wanted to delete had the same title, so this works for me
i tried with the type property, but the new CalendarBooking does not have it! (or i did not find it at least, i think now its a list or something like that)
hope it helps someone!