private void sendRecords(Cache cache) {

    Long groupId = LiferayServerContext.getGroupId();
    List<DDLRecordCache> records = getLatestRecordsToSync(cache);

    DDLRecordService recordService =
        new DDLRecordService(SessionContext.createSessionFromCurrentSession());

    for (DDLRecordCache cachedRecord : records) {
      try {
        Record record = cachedRecord.getRecord();
        record.setCreatorUserId(SessionContext.getLoggedUser().getId());
        final JSONObject serviceContextAttributes = new JSONObject();
        serviceContextAttributes.put("userId", record.getCreatorUserId());
        serviceContextAttributes.put("scopeGroupId", groupId);
        JSONObjectWrapper serviceContextWrapper = new JSONObjectWrapper(serviceContextAttributes);
        JSONObject jsonContent = cachedRecord.getJSONContent();

        JSONObject jsonObject =
            saveOrUpdate(recordService, record, groupId, serviceContextWrapper, jsonContent);

        cachedRecord.setDirty(false);
        cachedRecord.setSyncDate(new Date());
        cache.set(cachedRecord);
      } catch (Exception e) {
        LiferayLogger.e("Error syncing a record", e);
      }
    }
  }
 private JSONObject saveOrUpdate(
     DDLRecordService recordService,
     Record record,
     long groupId,
     JSONObjectWrapper serviceContextWrapper,
     JSONObject jsonContent)
     throws Exception {
   if (record.getRecordId() == 0) {
     return recordService.addRecord(
         groupId, record.getRecordSetId(), 0, jsonContent, serviceContextWrapper);
   } else {
     return recordService.updateRecord(
         record.getRecordId(), 0, jsonContent, true, serviceContextWrapper);
   }
 }