--- /dev/null
+/*******************************************************************************
+ * Copyright © Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.sample.jsp.portlet;
+
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
+
+import javax.portlet.RenderRequest;
+
+import org.joda.time.Days;
+import org.joda.time.LocalDate;
+
+import com.google.ical.compat.jodatime.LocalDateIterable;
+import com.google.ical.compat.jodatime.LocalDateIteratorFactory;
+import com.liferay.portal.kernel.json.JSONFactoryUtil;
+import com.liferay.portal.kernel.json.JSONObject;
+import com.liferay.portal.kernel.util.ParamUtil;
+import com.liferay.portal.model.User;
+import com.liferay.portal.util.PortalUtil;
+import com.pentila.entSavoie.agenda.model.EventAgenda;
+import com.pentila.entSavoie.agenda.service.EventAgendaLocalServiceUtil;
+import com.pentila.entSavoie.agenda.service.RessourceLocalServiceUtil;
+
+public class ProxyEventAgenda {
+
+ static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+ static SimpleDateFormat localdf = new SimpleDateFormat("yyyy-MM-dd");
+
+ static public JSONObject getEventAsJSon(EventAgenda evt, boolean concurrence){
+ JSONObject res = JSONFactoryUtil.createJSONObject();
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+
+ res.put("id", evt.getEvtId());
+ res.put("cid", evt.getCalId());
+ res.put("title", evt.getTitle());
+ res.put("start", df.format(evt.getStartEvt()));
+ res.put("end", df.format(evt.getEndEvt()));
+ res.put("loc", evt.getLocation());
+ res.put("notes", evt.getNotes());
+ res.put("url", evt.getUrl());
+ res.put("ad", evt.getIsAllDay());
+ res.put("visible", evt.getIsVisible());
+ res.put("rem", evt.getRemind());
+ res.put("isEditable", true); // on me true par defaut car on est soit sur une creation soit un update
+ res.put("author", ""); // l'auteur est forcement soit meme donc inutile de preciser car pas affiché dans ce cas!
+ res.put("recur_rule", evt.getRepeatEvtRRule());
+ res.put("recurrenceId", evt.getRepeatEvtId());
+ res.put("recurrenceScope", "one"); // par defaut on ne modifie que l'evt
+
+ res.put("concurrence", concurrence); // par defaut on ne modifie que l'evt
+ return res;
+ }
+
+
+ static public void deleteEvent(RenderRequest renderRequest) throws Exception {
+
+ long evtId = ParamUtil.getLong(renderRequest, "data", 0);
+ String deleteScope = ParamUtil.getString(renderRequest, "recurrenceScope", "one");
+
+ EventAgenda evtObj = EventAgendaLocalServiceUtil.getEventAgenda(evtId);
+ if (deleteScope.equals("one")) {
+ EventAgendaLocalServiceUtil.deleteEvent(evtObj);
+ }
+ else if (deleteScope.equals("all")) {
+ EventAgendaLocalServiceUtil.deleteEventAgendaLinked(evtObj);
+ }
+ else if (deleteScope.equals("after")) {
+ EventAgendaLocalServiceUtil.deleteEventAgendaLinkedAfter(evtObj);
+ }
+ }
+
+ static public boolean updateEvent(RenderRequest renderRequest) throws Exception {
+
+ JSONObject data = JSONFactoryUtil.createJSONObject(ParamUtil.getString(renderRequest, "data"));
+
+ boolean concurrence = false;
+
+ EventAgenda evt = EventAgendaLocalServiceUtil.getEventAgenda(data.getLong("id"));
+ String updateScope = ParamUtil.getString(renderRequest, "recurrenceScope", "one");
+
+ //System.out.println("UpdateScope: " + updateScope);
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+
+ String startTime = null;
+ String endTime = null;
+ int startDeltaDays = 0;
+ int endDeltaDays = 0;
+
+
+ if(!data.isNull("start")){
+ // on change la startDate -->
+ // il faut le delta days + le nouveau temps
+
+ Date oldStart = evt.getStartEvt();
+ startTime = data.getString("start").substring(11);
+
+ //System.out.println("startDate: " + data.getString("start"));
+ //System.out.println("startTime: " + startTime);
+ LocalDate newlStart = new LocalDate(data.getString("start").substring(0, 10));
+ //System.out.println("newlStart :" + data.getString("start").substring(0, 10));
+ LocalDate oldlStart = new LocalDate(localdf.format(oldStart));
+ startDeltaDays = Days.daysBetween(oldlStart, newlStart).getDays();
+
+ //System.out.println("startDeltaDays: " + startDeltaDays);
+
+ evt.setStartEvt(df.parse(data.getString("start")));
+ }
+ if(!data.isNull("end")){
+ // on change la endDate -->
+ // il faut le delta days + le nouveau temps
+ Date oldEnd = evt.getEndEvt();
+ endTime = data.getString("end").substring(11);
+
+ LocalDate newlEnd = new LocalDate(data.getString("end").substring(0, 10));
+ LocalDate oldlEnd = new LocalDate(localdf.format(oldEnd));
+ endDeltaDays = Days.daysBetween(oldlEnd, newlEnd).getDays();
+
+ evt.setEndEvt(df.parse(data.getString("end")));
+ }
+ if(!data.isNull("ad")){
+ evt.setIsAllDay(data.getBoolean("ad"));
+ }
+ if(!data.isNull("visible")){
+ evt.setIsVisible(data.getBoolean("visible"));
+ }
+ if(!data.isNull("cid")){
+ evt.setCalId(data.getLong("cid"));
+ }
+ if(!data.isNull("title")){
+ evt.setTitle(data.getString("title"));
+ }
+ if(!data.isNull("loc")){
+ evt.setLocation(data.getString("loc"));
+ }
+ if(!data.isNull("notes")){
+ evt.setNotes(data.getString("notes"));
+ }
+ if(!data.isNull("url")){
+ evt.setUrl(data.getString("url"));
+ }
+ if(!data.isNull("rem")){
+ evt.setRemind(data.getString("rem"));
+ }
+
+ if (updateScope.equals("one")) {
+ // on le sort de la reccurence simplement
+ evt.setRepeatEvtId(0);
+ evt.setRepeatEvtRRule("");
+ }
+
+ boolean isRessourceCalendar = false;
+ isRessourceCalendar = RessourceLocalServiceUtil.getRessource(evt.getCalId()).getRessource();
+ if (isRessourceCalendar) {
+ concurrence = concurrence || EventAgendaLocalServiceUtil.hasConcurrenceEvent(evt.getEvtId(), evt.getCalId(), evt.getStartEvt(), evt.getEndEvt());
+ }
+
+
+ EventAgendaLocalServiceUtil.updateEventAgendaAndIndex(evt);
+
+ List<EventAgenda> evts = new ArrayList<EventAgenda>();
+ if (updateScope.equals("after")) {
+ evts = EventAgendaLocalServiceUtil.getEventAgendaLinkedAfter(evt);
+ }
+ else if (updateScope.equals("all")) {
+ evts = EventAgendaLocalServiceUtil.getEventAgendaLinked(evt);
+ }
+
+ for (EventAgenda e: evts){
+ if (e.getEvtId()==evt.getEvtId()) {
+ // on ne fait pas 2 fois le meme!
+ continue;
+ }
+ // on met à jours les evts
+ if(!data.isNull("start")){
+ // on change la startDate
+ Date oldStart = e.getStartEvt();
+ LocalDate lStart = new LocalDate(localdf.format(oldStart));
+
+ //System.out.println("lStart 1: " + lStart);
+ //System.out.println("startDeltaDays 1: " + startDeltaDays);
+ lStart = lStart.plusDays(startDeltaDays);
+ //System.out.println("lStart 2: " + lStart);
+
+ String newSStart = lStart.toString() + "T" + startTime;
+ //System.out.println("newSStart: " + newSStart);
+ e.setStartEvt(df.parse(newSStart));
+ }
+ if(!data.isNull("end")){
+ // on change la endDate
+ Date oldEnd = e.getEndEvt();
+ LocalDate lEnd = new LocalDate(localdf.format(oldEnd));
+
+ //System.out.println("lEnd 1: " + lEnd);
+ //System.out.println("endDeltaDays 1: " + startDeltaDays);
+
+ lEnd = lEnd.plusDays(endDeltaDays);
+ String newSEnd = lEnd.toString() + "T" + endTime;
+
+ e.setEndEvt(df.parse(newSEnd));
+ }
+
+
+ if(!data.isNull("ad")){
+ e.setIsAllDay(data.getBoolean("ad"));
+ }
+ if(!data.isNull("visible")){
+ evt.setIsVisible(data.getBoolean("visible"));
+ }
+ if(!data.isNull("cid")){
+ e.setCalId(data.getLong("cid"));
+ }
+ if(!data.isNull("title")){
+ e.setTitle(data.getString("title"));
+ }
+ if(!data.isNull("loc")){
+ e.setLocation(data.getString("loc"));
+ }
+ if(!data.isNull("notes")){
+ e.setNotes(data.getString("notes"));
+ }
+ if(!data.isNull("url")){
+ e.setUrl(data.getString("url"));
+ }
+ if(!data.isNull("rem")){
+ e.setRemind(data.getString("rem"));
+ }
+ // si on est sur un change after on revoit le evtRpeatId
+ if (updateScope.equals("after")) {
+ e.setRepeatEvtId(evt.getRepeatEvtId());
+ }
+
+ if (isRessourceCalendar) {
+ concurrence = concurrence || EventAgendaLocalServiceUtil.hasConcurrenceEvent(e.getEvtId(), e.getCalId(), e.getStartEvt(), e.getEndEvt());
+ }
+
+ EventAgendaLocalServiceUtil.updateEventAgendaAndIndex(e);
+ }
+ return concurrence;
+ }
+
+
+ static public JSONObject createEvent(RenderRequest renderRequest) throws Exception {
+
+ JSONObject data = JSONFactoryUtil.createJSONObject(ParamUtil.getString(renderRequest, "data"));
+
+ boolean concurrence = false;
+
+ User user = PortalUtil.getUser(renderRequest);
+
+ Date start = df.parse(data.getString("start"));
+ Calendar calendarStart = Calendar.getInstance();
+ calendarStart.setTime(start);
+
+ Date end = df.parse(data.getString("end"));
+ Calendar calendarEnd = GregorianCalendar.getInstance();
+ calendarEnd.setTime(end);
+
+ Long calId = data.getLong("cid");
+
+ String rRule = "";
+ if (!data.getString("recur_rule").isEmpty()) {
+ rRule = "RRULE:" + data.getString("recur_rule");
+ }
+
+ boolean isRessourceCalendar = false;
+ isRessourceCalendar = RessourceLocalServiceUtil.getRessource(calId).getRessource();
+ if (isRessourceCalendar) {
+ concurrence = concurrence || EventAgendaLocalServiceUtil.hasConcurrenceEvent(0, calId, start, end);
+ }
+
+
+ // on passe le repeatEvtId a 0 car si on a un recurRule on traite cela dans le impl pour le permier de la serie!
+ EventAgenda evt = EventAgendaLocalServiceUtil.addEventAgenda(
+ user.getCompanyId(), user.getUserId(), calId, start,
+ end, data.getString("title"),
+ data.getString("loc"), data.getString("notes"),
+ data.getString("url"), data.getBoolean("ad"), data.getString("rem"), rRule, 0,"", data.getBoolean("visible"));
+
+ // As t on un rRule??
+ if (rRule!=null && !rRule.isEmpty()) {
+ long repeatEvtId = evt.getEvtId();
+ LocalDate lStart = new LocalDate(localdf.format(start));
+ LocalDate lEnd = new LocalDate(localdf.format(end));
+
+ Days d = Days.daysBetween(lStart, lEnd);
+ int deltaDays = d.getDays();
+
+ LocalDateIterable ldi = LocalDateIteratorFactory.createLocalDateIterable(rRule, lStart, true);
+ for (LocalDate ldate: ldi) {
+ // pour ne pas créer en double la première!!!
+ if (ldate.equals(lStart)) {
+ // on crée tjs sur des dates differentes...
+ continue;
+ }
+ // on créer les réccurences
+ // date de debut
+ Date rStart = ldate.toDate();
+ rStart.setHours(calendarStart.get(Calendar.HOUR_OF_DAY));
+ rStart.setMinutes(calendarStart.get(Calendar.MINUTE));
+
+ //date de fin
+ ldate.plusDays(deltaDays);
+ Date rEnd = ldate.toDate();
+ rEnd.setHours(calendarEnd.get(Calendar.HOUR_OF_DAY));
+ rEnd.setMinutes(calendarEnd.get(Calendar.MINUTE));
+
+ if (isRessourceCalendar) {
+ concurrence = concurrence || EventAgendaLocalServiceUtil.hasConcurrenceEvent(0, calId, rStart, rEnd);
+ }
+
+
+ EventAgenda rEvt = EventAgendaLocalServiceUtil.addEventAgenda(
+ user.getCompanyId(), user.getUserId(), calId, rStart,
+ rEnd, data.getString("title"),
+ data.getString("loc"), data.getString("notes"),
+ data.getString("url"), data.getBoolean("ad"), data.getString("rem"), rRule, repeatEvtId, "", data.getBoolean("visible"));
+
+ }
+ }
+ return ProxyEventAgenda.getEventAsJSon(evt, concurrence);
+ }
+}