public static List<org.exoplatform.calendar.service.Calendar> getAllOfCalendars(String username)
      throws Exception {
    List<org.exoplatform.calendar.service.Calendar> calendars =
        new ArrayList<org.exoplatform.calendar.service.Calendar>();
    CalendarService calendarService = getCalendarService();
    /*---- get private calendars ----*/
    List<GroupCalendarData> groupCalendars;
    for (org.exoplatform.calendar.service.Calendar calendar :
        calendarService.getUserCalendars(username, true)) {
      calendars.add(calendar);
    }

    /*---- get public calendars ----*/
    String[] groups = CalendarUtils.getUserGroups(username);
    groupCalendars =
        calendarService.getGroupCalendars(groups, true, CalendarUtils.getCurrentUser());
    Map<String, org.exoplatform.calendar.service.Calendar> mapCal =
        new HashMap<String, org.exoplatform.calendar.service.Calendar>();
    for (GroupCalendarData group : groupCalendars) {
      for (org.exoplatform.calendar.service.Calendar cal : group.getCalendars()) {
        mapCal.put(cal.getId(), cal);
      }
    }
    calendars.addAll(mapCal.values());

    /*---- get shared calendars ----*/
    GroupCalendarData groupCalendar = calendarService.getSharedCalendars(username, true);
    if (groupCalendar != null) {
      for (org.exoplatform.calendar.service.Calendar calendar : groupCalendar.getCalendars()) {
        calendars.add(calendar);
      }
    }
    return calendars;
  }
  private org.exoplatform.calendar.model.Calendar persist(
      org.exoplatform.calendar.model.Calendar cal, boolean isNew) {
    Calendar o;
    if (cal instanceof Calendar) {
      o = (Calendar) cal;
    } else {
      o = Calendar.build(cal);
    }

    int type = o.getCalType();

    if (type == Calendar.Type.PERSONAL.type()) {
      try {
        dataStorage.saveUserCalendar(o.getCalendarOwner(), o, isNew);
      } catch (Exception ex) {
        LOG.error(ex);
      }
    } else if (type == Calendar.Type.GROUP.type()) {
      try {
        dataStorage.savePublicCalendar(o, isNew, null);
      } catch (Exception ex) {
        LOG.error(ex);
      }
    } else {
      throw new UnsupportedOperationException(
          "Save calendar with type '" + type + "' is not supported");
    }

    return cal;
  }
 public static List<org.exoplatform.calendar.service.Calendar> getAllOfCurrentUserCalendars()
     throws Exception {
   List<org.exoplatform.calendar.service.Calendar> list =
       new ArrayList<org.exoplatform.calendar.service.Calendar>();
   CalendarService calendarService = getCalendarService();
   String username = getCurrentUser();
   List<org.exoplatform.calendar.service.Calendar> calendars =
       calendarService.getUserCalendars(username, true);
   for (org.exoplatform.calendar.service.Calendar c : calendars) {
     list.add(c);
   }
   GroupCalendarData gcd = calendarService.getSharedCalendars(username, true);
   if (gcd != null) {
     for (org.exoplatform.calendar.service.Calendar c : gcd.getCalendars()) {
       if (Utils.hasPermission(Utils.getEditPerUsers(c))) {
         list.add(c);
       }
     }
   }
   List<GroupCalendarData> lgcd =
       calendarService.getGroupCalendars(CalendarUtils.getUserGroups(username), true, username);
   if (lgcd != null) {
     for (GroupCalendarData g : lgcd) {
       for (org.exoplatform.calendar.service.Calendar c : g.getCalendars()) {
         if (Utils.hasPermission(c.getEditPermission())) {
           list.add(c);
         }
       }
     }
   }
   return list;
 }
 @Override
 public Calendar getById(String id) {
   try {
     Calendar cal = dataStorage.getCalendarById(id);
     if (cal != null) {
       cal.setDS(JCRStorage.JCR_STORAGE);
     }
     return cal;
   } catch (Exception ex) {
     LOG.error("Exception while loading calendar by ID", ex);
     return null;
   }
 }
  @Override
  public Calendar remove(String id) {
    Calendar calendar = getById(id);
    if (calendar == null) {
      return null;
    }

    if (calendar.getCalType() == Calendar.Type.PERSONAL.type()) {
      try {
        dataStorage.removeUserCalendar(calendar.getCalendarOwner(), id);
      } catch (Exception ex) {
        LOG.error(ex);
      }
    } else if (calendar.getCalType() == Calendar.Type.GROUP.type()) {
      try {
        dataStorage.removeGroupCalendar(id);
      } catch (Exception ex) {
        LOG.error(ex);
      }
    }
    return calendar;
  }
  private void initPrivateCalendar() throws Exception {
    log.info("Inject private datas ....");
    // save setting
    try {
      setting = calService.getCalendarSetting(currentUser);
      log.info(String.format("Save calendar setting for user %s ....", currentUser));
    } catch (Exception e) {
      setting = newCalendarSetting();
      calService.saveCalendarSetting(currentUser, setting);
    }
    long t = System.currentTimeMillis(), t1 = t;

    t = System.currentTimeMillis();
    // save EventCategoy
    List<EventCategory> eventCategories = findEventCategorys();
    for (EventCategory evCat : eventCategories) {
      calService.saveEventCategory(currentUser, evCat, true);
      eventCategory.add(evCat);
      eventCategorys.add(evCat.getId());
    }
    log.info(
        String.format(
            "Saved %s eventCategories in %sms",
            eventCategories.size(), (System.currentTimeMillis() - t)));
    // save calendar
    List<Calendar> calendars = findCalendars(false);
    List<CalendarEvent> events;
    int index = 0, size = calendars.size(), evsCal, evs = 0, tas = 0;
    for (Calendar calendar : calendars) {
      t = System.currentTimeMillis();
      calService.saveUserCalendar(currentUser, calendar, true);
      privateCalendar.add(calendar.getId());
      // save Event
      events = findCalendarEvent(calendar.getId(), "0", CalendarEvent.TYPE_EVENT, false);
      for (CalendarEvent event : events) {
        calService.saveUserEvent(currentUser, calendar.getId(), event, true);
      }
      evsCal = events.size();
      evs += evsCal;
      // save Task
      events = findCalendarEvent(calendar.getId(), "0", CalendarEvent.TYPE_TASK, false);
      tas += events.size();
      for (CalendarEvent event : events) {
        calService.saveUserEvent(currentUser, calendar.getId(), event, true);
      }
      log.info(
          String.format(
              "Saved Calendar %s/%s with %s Events and %s Tasks in %sms",
              (++index), size, evsCal, events.size(), (System.currentTimeMillis()) - t));
    }
    log.info(
        String.format(
            "INITIALIZED EventCategorys=%s / Calendars=%s / Events=%s / Tasks=%s in %sms",
            eventCategories.size(), calendars.size(), evs, tas, (System.currentTimeMillis() - t1)));
    saveHistoryInject();
  }
  @Override
  public List<org.exoplatform.calendar.model.Calendar> findCalendars(CalendarQuery query) {
    List<org.exoplatform.calendar.model.Calendar> calendars =
        new LinkedList<org.exoplatform.calendar.model.Calendar>();
    List<String> excludes = Collections.emptyList();
    String[] excludesId = query.getExclusions();
    if (excludesId != null) {
      excludes = Arrays.asList(excludesId);
    }

    try {
      Identity identity = query.getIdentity();
      List<Calendar> cals = dataStorage.getUserCalendars(identity.getUserId(), true);
      if (cals != null && cals.size() > 0) {
        for (Calendar c : cals) {
          if (!excludes.contains(c.getId())) {
            calendars.add(c);
          }
        }
      }

      GroupCalendarData data = dataStorage.getSharedCalendars(identity.getUserId(), true);
      if (data != null && data.getCalendars().size() > 0) {
        for (Calendar c : data.getCalendars()) {
          if (!excludes.contains(c.getId())) {
            calendars.add(c);
          }
        }
      }

      List<GroupCalendarData> datas =
          dataStorage.getGroupCalendars(
              identity.getGroups().toArray(new String[0]), true, identity.getUserId());
      if (datas != null && datas.size() > 0) {
        for (GroupCalendarData d : datas) {
          for (Calendar c : d.getCalendars()) {
            if (!excludes.contains(c.getId())) {
              calendars.add(c);
            }
          }
        }
      }

      for (org.exoplatform.calendar.model.Calendar cal : calendars) {
        cal.setDS(JCRStorage.JCR_STORAGE);
      }

      return calendars;
    } catch (Exception ex) {
      LOG.error(ex);
    }
    return null;
  }
 private void initPublicCalendar() throws Exception {
   // save public calendar
   List<Calendar> calendars = findCalendars(true);
   log.info("Inject public datas ....");
   int index = 0, size = calendars.size(), evsCal, tasCal, evs = 0, tas = 0;
   long t, t1 = System.currentTimeMillis();
   for (Calendar calendar : calendars) {
     t = System.currentTimeMillis();
     evsCal = tasCal = 0;
     calService.savePublicCalendar(calendar, true);
     publicCalendar.add(calendar.getId());
     // save event in public calendar
     for (CalendarEvent event :
         findCalendarEvent(calendar.getId(), "2", CalendarEvent.TYPE_EVENT, true)) {
       calService.savePublicEvent(calendar.getId(), event, true);
       evsCal++;
     }
     // save task in public calendar
     for (CalendarEvent event :
         findCalendarEvent(calendar.getId(), "2", CalendarEvent.TYPE_TASK, true)) {
       calService.savePublicEvent(calendar.getId(), event, true);
       tasCal++;
     }
     log.info(
         String.format(
             "Saved Calendar %s/%s with %s Events and %s Tasks in %sms",
             (++index), size, evsCal, tasCal, (System.currentTimeMillis()) - t));
     evs += evsCal;
     tas += tasCal;
   }
   log.info(
       String.format(
           "INITIALIZED: Calendars=%s / Events=%s / Tasks=%s in %sms",
           publicCalendar.size(), evs, tas, (System.currentTimeMillis() - t1)));
   saveHistoryInject();
 }
 @Override
 public Calendar newInstance() {
   Calendar c = new Calendar();
   c.setDS(JCRStorage.JCR_STORAGE);
   return c;
 }
 private Calendar newPublicCalendar() {
   Calendar calendar = new Calendar();
   calendar.setCalendarOwner(currentUser);
   calendar.setDataInit(true);
   calendar.setName(calRandomWords(5));
   calendar.setDescription(randomWords(20));
   calendar.setCalendarColor(getRandomColor());
   calendar.setEditPermission(groupShare);
   calendar.setGroups(groups);
   calendar.setViewPermission(new String[] {"*.*"});
   calendar.setPrivateUrl(EMPTY);
   calendar.setPublicUrl(EMPTY);
   calendar.setPublic(true);
   calendar.setLocale("VNM");
   calendar.setTimeZone("Asia/Ho_Chi_Minh");
   return calendar;
 }
 private Calendar newPrivateCalendar() {
   Calendar calendar = new Calendar();
   calendar.setCalendarOwner(currentUser);
   calendar.setDataInit(true);
   calendar.setName(calRandomWords(5));
   calendar.setDescription(randomWords(20));
   calendar.setCalendarColor(getRandomColor());
   calendar.setEditPermission(new String[] {});
   calendar.setGroups(new String[] {});
   calendar.setViewPermission(new String[] {});
   calendar.setPrivateUrl(EMPTY);
   calendar.setPublicUrl(EMPTY);
   calendar.setPublic(false);
   calendar.setLocale(DEFAULT_LOCATION);
   calendar.setTimeZone(setting.getTimeZone());
   return calendar;
 }
Exemple #12
0
 @Override
 public void execute(Event<UIWeekView> event) throws Exception {
   UIWeekView calendarview = event.getSource();
   String eventId = event.getRequestContext().getRequestParameter(OBJECTID);
   String calendarId = event.getRequestContext().getRequestParameter(eventId + CALENDARID);
   String calType = event.getRequestContext().getRequestParameter(eventId + CALTYPE);
   String startTime = event.getRequestContext().getRequestParameter(eventId + START_TIME);
   String finishTime = event.getRequestContext().getRequestParameter(eventId + FINISH_TIME);
   Boolean isOccur = false;
   if (!Utils.isEmpty(event.getRequestContext().getRequestParameter(eventId + ISOCCUR))) {
     isOccur =
         Boolean.parseBoolean(event.getRequestContext().getRequestParameter(eventId + ISOCCUR));
   }
   String recurId = null;
   if (isOccur) recurId = event.getRequestContext().getRequestParameter(eventId + RECURID);
   try {
     String username = CalendarUtils.getCurrentUser();
     CalendarEvent eventCalendar = null;
     if (isOccur && !Utils.isEmpty(recurId)) {
       eventCalendar = calendarview.getRecurrenceMap().get(eventId).get(recurId);
     } else {
       eventCalendar = calendarview.getDataMap().get(eventId);
     }
     if (eventCalendar != null) {
       CalendarService calendarService = CalendarUtils.getCalendarService();
       Calendar calBegin = calendarview.getInstanceTempCalendar();
       Calendar calEnd = calendarview.getInstanceTempCalendar();
       long unit = 15 * 60 * 1000;
       calBegin.setTimeInMillis((Long.parseLong(startTime) / unit) * unit);
       eventCalendar.setFromDateTime(calBegin.getTime());
       calEnd.setTimeInMillis((Long.parseLong(finishTime) / unit) * unit);
       eventCalendar.setToDateTime(calEnd.getTime());
       if (eventCalendar.getToDateTime().before(eventCalendar.getFromDateTime())) {
         return;
       }
       org.exoplatform.calendar.service.Calendar calendar = null;
       if (CalendarUtils.PRIVATE_TYPE.equals(calType)) {
         calendar = calendarService.getUserCalendar(username, calendarId);
       } else if (CalendarUtils.SHARED_TYPE.equals(calType)) {
         if (calendarService.getSharedCalendars(username, true) != null)
           calendar =
               calendarService.getSharedCalendars(username, true).getCalendarById(calendarId);
       } else if (CalendarUtils.PUBLIC_TYPE.equals(calType)) {
         calendar = calendarService.getGroupCalendar(calendarId);
       }
       if (calendar == null) {
         event
             .getRequestContext()
             .getUIApplication()
             .addMessage(new ApplicationMessage("UICalendars.msg.have-no-calendar", null, 1));
       } else {
         if ((CalendarUtils.SHARED_TYPE.equals(calType)
                 && !CalendarUtils.canEdit(
                     calendarview.getApplicationComponent(OrganizationService.class),
                     Utils.getEditPerUsers(calendar),
                     username))
             || (CalendarUtils.PUBLIC_TYPE.equals(calType)
                 && !CalendarUtils.canEdit(
                     calendarview.getApplicationComponent(OrganizationService.class),
                     calendar.getEditPermission(),
                     username))) {
           event
               .getRequestContext()
               .getUIApplication()
               .addMessage(
                   new ApplicationMessage(
                       "UICalendars.msg.have-no-permission-to-edit-event", null, 1));
           calendarview.refresh();
           event.getRequestContext().addUIComponentToUpdateByAjax(calendarview.getParent());
           return;
         }
         // if it's a 'virtual' occurrence
         if (isOccur && !Utils.isEmpty(recurId)) {
           List<CalendarEvent> listEvent = new ArrayList<CalendarEvent>();
           listEvent.add(eventCalendar);
           calendarService.updateOccurrenceEvent(
               calendarId, calendarId, calType, calType, listEvent, username);
         } else {
           if (calType.equals(CalendarUtils.PRIVATE_TYPE)) {
             calendarService.saveUserEvent(username, calendarId, eventCalendar, false);
           } else if (calType.equals(CalendarUtils.SHARED_TYPE)) {
             calendarService.saveEventToSharedCalendar(
                 username, calendarId, eventCalendar, false);
           } else if (calType.equals(CalendarUtils.PUBLIC_TYPE)) {
             calendarService.savePublicEvent(calendarId, eventCalendar, false);
           }
         }
         calendarview.setLastUpdatedEventId(eventId);
         calendarview.refresh();
         UIMiniCalendar uiMiniCalendar =
             calendarview
                 .getAncestorOfType(UICalendarPortlet.class)
                 .findFirstComponentOfType(UIMiniCalendar.class);
         event.getRequestContext().addUIComponentToUpdateByAjax(uiMiniCalendar);
         event.getRequestContext().addUIComponentToUpdateByAjax(calendarview.getParent());
       }
     }
   } catch (Exception e) {
     if (log.isDebugEnabled()) {
       log.debug("Fail to save the event to the calendar", e);
     }
     return;
   }
 }
  /**
   * get all users calendars (private, public, shared)
   *
   * @return
   * @throws Exception
   */
  public static List<SelectItem> getCalendarOption() throws Exception {
    List<SelectItem> options = new ArrayList<SelectItem>();
    CalendarService calendarService = getCalendarService();
    String username = getCurrentUser();
    /*
     * hash map to check existence of a calendar in the list.
     */
    Map<String, String> hash = new HashMap<String, String>();
    /*
     * Modified by Philippe ([email protected])
     * Uses SelectItemOptionGroup to differienciate private, shared and public groups
     */

    // private calendars group
    SelectOptionGroup privGrp = new SelectOptionGroup(CalendarUtils.PRIVATE_CALENDARS);
    List<org.exoplatform.calendar.service.Calendar> calendars =
        calendarService.getUserCalendars(username, true);
    for (org.exoplatform.calendar.service.Calendar c : calendars) {
      if (!hash.containsKey(c.getId())) {
        hash.put(c.getId(), "");
        privGrp.addOption(
            new SelectOption(
                c.getName(), CalendarUtils.PRIVATE_TYPE + CalendarUtils.COLON + c.getId()));
      }
    }
    if (privGrp.getOptions().size() > 0) options.add(privGrp);
    // shared calendars group
    GroupCalendarData gcd = calendarService.getSharedCalendars(username, true);
    if (gcd != null) {
      SelectOptionGroup sharedGrp = new SelectOptionGroup(CalendarUtils.SHARED_CALENDARS);
      for (org.exoplatform.calendar.service.Calendar c : gcd.getCalendars()) {
        if (Utils.hasPermission(Utils.getEditPerUsers(c))) {
          if (!hash.containsKey(c.getId())) {
            hash.put(c.getId(), "");
            sharedGrp.addOption(
                new SelectOption(
                    c.getName(), CalendarUtils.SHARED_TYPE + CalendarUtils.COLON + c.getId()));
          }
        }
      }
      if (sharedGrp.getOptions().size() > 0) options.add(sharedGrp);
    }
    // public calendars group
    List<GroupCalendarData> lgcd =
        calendarService.getGroupCalendars(CalendarUtils.getUserGroups(username), true, username);

    if (lgcd != null) {
      SelectOptionGroup pubGrp = new SelectOptionGroup(CalendarUtils.PUBLIC_CALENDARS);
      for (GroupCalendarData g : lgcd) {
        for (org.exoplatform.calendar.service.Calendar c : g.getCalendars()) {
          if (Utils.hasPermission(c.getEditPermission())) {
            if (!hash.containsKey(c.getId())) {
              hash.put(c.getId(), "");
              pubGrp.addOption(
                  new SelectOption(
                      c.getName(), CalendarUtils.PUBLIC_TYPE + CalendarUtils.COLON + c.getId()));
            }
          }
        }
      }
      if (pubGrp.getOptions().size() > 0) options.add(pubGrp);
    }
    return options;
  }