@Override
  public void updateAsset(
      long userId,
      CalendarResource calendarResource,
      long[] assetCategoryIds,
      String[] assetTagNames)
      throws PortalException {

    assetEntryLocalService.updateEntry(
        userId,
        calendarResource.getGroupId(),
        calendarResource.getCreateDate(),
        calendarResource.getModifiedDate(),
        CalendarResource.class.getName(),
        calendarResource.getCalendarResourceId(),
        calendarResource.getUuid(),
        0,
        assetCategoryIds,
        assetTagNames,
        true,
        null,
        null,
        null,
        ContentTypes.TEXT,
        calendarResource.getName(),
        calendarResource.getDescription(),
        null,
        null,
        null,
        0,
        0,
        null,
        false);
  }
  protected void updateCalendars(
      PortletDataContext portletDataContext,
      CalendarResource calendarResource,
      CalendarResource importedCalendarResource)
      throws SystemException {

    Map<Long, Long> calendarIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Calendar.class);

    List<Element> referenceElements =
        portletDataContext.getReferenceElements(calendarResource, Calendar.class);

    for (Element referenceElement : referenceElements) {
      long calendarId = GetterUtil.getLong(referenceElement.attributeValue("class-pk"));

      Calendar calendar =
          CalendarLocalServiceUtil.fetchCalendar(MapUtil.getLong(calendarIds, calendarId));

      if (calendar != null) {
        calendar.setCalendarResourceId(importedCalendarResource.getCalendarResourceId());

        CalendarLocalServiceUtil.updateCalendar(calendar);
      }
    }
  }
  @Override
  @SystemEvent(action = SystemEventConstants.ACTION_SKIP, type = SystemEventConstants.TYPE_DELETE)
  public CalendarResource deleteCalendarResource(CalendarResource calendarResource)
      throws PortalException {

    // Calendar resource

    calendarResourcePersistence.remove(calendarResource);

    // Resources

    resourceLocalService.deleteResource(calendarResource, ResourceConstants.SCOPE_INDIVIDUAL);

    // Calendars

    List<Calendar> calendars =
        calendarPersistence.findByG_C(
            calendarResource.getGroupId(), calendarResource.getCalendarResourceId());

    for (Calendar calendar : calendars) {
      calendarPersistence.remove(calendar);

      resourceLocalService.deleteResource(calendar, ResourceConstants.SCOPE_INDIVIDUAL);

      calendarNotificationTemplateLocalService.deleteCalendarNotificationTemplates(
          calendar.getCalendarId());
    }

    // Calendar bookings

    List<CalendarBooking> calendarBookings =
        calendarBookingPersistence.findByCalendarResourceId(
            calendarResource.getCalendarResourceId());

    for (CalendarBooking calendarBooking : calendarBookings) {
      calendarBookingLocalService.deleteCalendarBooking(calendarBooking);
    }

    return calendarResource;
  }
Exemple #4
0
  public static List<Calendar> getCalendarResourceCalendars(CalendarResource calendarResource)
      throws PortalException, SystemException {

    return CalendarServiceUtil.search(
        calendarResource.getCompanyId(),
        new long[] {calendarResource.getGroupId()},
        new long[] {calendarResource.getCalendarResourceId()},
        null,
        false,
        QueryUtil.ALL_POS,
        QueryUtil.ALL_POS,
        new CalendarNameComparator(true));
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    CalendarResource newCalendarResource = addCalendarResource();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(CalendarResource.class, _dynamicQueryClassLoader);

    dynamicQuery.add(
        RestrictionsFactoryUtil.eq(
            "calendarResourceId", newCalendarResource.getCalendarResourceId()));

    List<CalendarResource> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    CalendarResource existingCalendarResource = result.get(0);

    Assert.assertEquals(existingCalendarResource, newCalendarResource);
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    CalendarResource newCalendarResource = addCalendarResource();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(CalendarResource.class, _dynamicQueryClassLoader);

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("calendarResourceId"));

    Object newCalendarResourceId = newCalendarResource.getCalendarResourceId();

    dynamicQuery.add(
        RestrictionsFactoryUtil.in("calendarResourceId", new Object[] {newCalendarResourceId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Object existingCalendarResourceId = result.get(0);

    Assert.assertEquals(existingCalendarResourceId, newCalendarResourceId);
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    CalendarResource newCalendarResource = _persistence.create(pk);

    newCalendarResource.setUuid(RandomTestUtil.randomString());

    newCalendarResource.setGroupId(RandomTestUtil.nextLong());

    newCalendarResource.setCompanyId(RandomTestUtil.nextLong());

    newCalendarResource.setUserId(RandomTestUtil.nextLong());

    newCalendarResource.setUserName(RandomTestUtil.randomString());

    newCalendarResource.setCreateDate(RandomTestUtil.nextDate());

    newCalendarResource.setModifiedDate(RandomTestUtil.nextDate());

    newCalendarResource.setResourceBlockId(RandomTestUtil.nextLong());

    newCalendarResource.setClassNameId(RandomTestUtil.nextLong());

    newCalendarResource.setClassPK(RandomTestUtil.nextLong());

    newCalendarResource.setClassUuid(RandomTestUtil.randomString());

    newCalendarResource.setCode(RandomTestUtil.randomString());

    newCalendarResource.setName(RandomTestUtil.randomString());

    newCalendarResource.setDescription(RandomTestUtil.randomString());

    newCalendarResource.setActive(RandomTestUtil.randomBoolean());

    newCalendarResource.setLastPublishDate(RandomTestUtil.nextDate());

    _calendarResources.add(_persistence.update(newCalendarResource));

    CalendarResource existingCalendarResource =
        _persistence.findByPrimaryKey(newCalendarResource.getPrimaryKey());

    Assert.assertEquals(existingCalendarResource.getUuid(), newCalendarResource.getUuid());
    Assert.assertEquals(
        existingCalendarResource.getCalendarResourceId(),
        newCalendarResource.getCalendarResourceId());
    Assert.assertEquals(existingCalendarResource.getGroupId(), newCalendarResource.getGroupId());
    Assert.assertEquals(
        existingCalendarResource.getCompanyId(), newCalendarResource.getCompanyId());
    Assert.assertEquals(existingCalendarResource.getUserId(), newCalendarResource.getUserId());
    Assert.assertEquals(existingCalendarResource.getUserName(), newCalendarResource.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingCalendarResource.getCreateDate()),
        Time.getShortTimestamp(newCalendarResource.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingCalendarResource.getModifiedDate()),
        Time.getShortTimestamp(newCalendarResource.getModifiedDate()));
    Assert.assertEquals(
        existingCalendarResource.getResourceBlockId(), newCalendarResource.getResourceBlockId());
    Assert.assertEquals(
        existingCalendarResource.getClassNameId(), newCalendarResource.getClassNameId());
    Assert.assertEquals(existingCalendarResource.getClassPK(), newCalendarResource.getClassPK());
    Assert.assertEquals(
        existingCalendarResource.getClassUuid(), newCalendarResource.getClassUuid());
    Assert.assertEquals(existingCalendarResource.getCode(), newCalendarResource.getCode());
    Assert.assertEquals(existingCalendarResource.getName(), newCalendarResource.getName());
    Assert.assertEquals(
        existingCalendarResource.getDescription(), newCalendarResource.getDescription());
    Assert.assertEquals(existingCalendarResource.getActive(), newCalendarResource.getActive());
    Assert.assertEquals(
        Time.getShortTimestamp(existingCalendarResource.getLastPublishDate()),
        Time.getShortTimestamp(newCalendarResource.getLastPublishDate()));
  }
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, CalendarResource calendarResource) throws Exception {

    prepareLanguagesForImport(calendarResource);

    long userId = portletDataContext.getUserId(calendarResource.getUserUuid());

    StagedModelDataHandlerUtil.importReferenceStagedModels(
        portletDataContext, calendarResource, Calendar.class);

    long classPK = getClassPK(portletDataContext, calendarResource, userId);
    Map<Locale, String> calendarResourceNameMap =
        getCalendarResourceNameMap(portletDataContext, calendarResource);

    ServiceContext serviceContext = portletDataContext.createServiceContext(calendarResource);

    CalendarResource importedCalendarResource = null;

    if (portletDataContext.isDataStrategyMirror()) {
      CalendarResource existingCalendarResource =
          CalendarResourceLocalServiceUtil.fetchCalendarResourceByUuidAndGroupId(
              calendarResource.getUuid(), portletDataContext.getScopeGroupId());

      if (existingCalendarResource == null) {
        existingCalendarResource =
            CalendarResourceLocalServiceUtil.fetchCalendarResource(
                calendarResource.getClassNameId(), classPK);
      }

      if (existingCalendarResource == null) {
        serviceContext.setUuid(calendarResource.getUuid());

        importedCalendarResource =
            CalendarResourceLocalServiceUtil.addCalendarResource(
                userId,
                portletDataContext.getScopeGroupId(),
                calendarResource.getClassNameId(),
                classPK,
                calendarResource.getClassUuid(),
                calendarResource.getCode(),
                calendarResourceNameMap,
                calendarResource.getDescriptionMap(),
                calendarResource.isActive(),
                serviceContext);
      } else {
        importedCalendarResource =
            CalendarResourceLocalServiceUtil.updateCalendarResource(
                existingCalendarResource.getCalendarResourceId(),
                calendarResource.getNameMap(),
                calendarResource.getDescriptionMap(),
                calendarResource.isActive(),
                serviceContext);
      }
    } else {
      importedCalendarResource =
          CalendarResourceLocalServiceUtil.addCalendarResource(
              userId,
              portletDataContext.getScopeGroupId(),
              calendarResource.getClassNameId(),
              classPK,
              calendarResource.getClassUuid(),
              calendarResource.getCode(),
              calendarResourceNameMap,
              calendarResource.getDescriptionMap(),
              calendarResource.isActive(),
              serviceContext);
    }

    updateCalendars(portletDataContext, calendarResource, importedCalendarResource);

    portletDataContext.importClassedModel(calendarResource, importedCalendarResource);
  }
  @Override
  public void importCalEvent(CalEvent calEvent) throws PortalException, SystemException {

    // Calendar event

    if (isImported(calEvent)) {
      return;
    }

    long calendarBookingId = counterLocalService.increment();

    CalendarResource calendarResource =
        getCalendarResource(calEvent.getCompanyId(), calEvent.getGroupId());

    Date startDate = calEvent.getStartDate();

    long startTime = startDate.getTime();

    long endTime =
        startTime
            + calEvent.getDurationHour() * Time.HOUR
            + calEvent.getDurationMinute() * Time.MINUTE;

    if (calEvent.isAllDay()) {
      endTime = endTime - 1;
    }

    String recurrence = getRecurrence(calEvent.getRecurrenceObj());

    addCalendarBooking(
        calEvent.getUuid(),
        calendarBookingId,
        calEvent.getCompanyId(),
        calendarResource.getGroupId(),
        calEvent.getUserId(),
        calEvent.getUserName(),
        calEvent.getCreateDate(),
        calEvent.getModifiedDate(),
        calendarResource.getDefaultCalendarId(),
        calendarResource.getCalendarResourceId(),
        calEvent.getTitle(),
        calEvent.getDescription(),
        calEvent.getLocation(),
        startTime,
        endTime,
        calEvent.getAllDay(),
        recurrence,
        calEvent.getFirstReminder(),
        NotificationType.EMAIL,
        calEvent.getSecondReminder(),
        NotificationType.EMAIL);

    // Resources

    importCalendarBookingResourcePermissions(calEvent, calendarBookingId);

    // Subscriptions

    importSubscriptions(calEvent, calendarBookingId);

    // Asset

    importAssets(calEvent, calendarBookingId);

    // Message boards

    importMBDiscussion(calEvent, calendarBookingId);

    // Social

    importSocialActivities(calEvent, calendarBookingId);

    // Ratings

    importRatings(
        classNameLocalService.getClassNameId(CalEvent.class.getName()),
        calEvent.getEventId(),
        classNameLocalService.getClassNameId(CalendarBooking.class.getName()),
        calendarBookingId);
  }