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;
  }
 @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 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;
  }
  @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;
  }