@Transactional
  @Override
  @CacheEvict(value = "hooks", allEntries = true)
  public CommandProcessingResult updateHook(final Long hookId, final JsonCommand command) {

    try {
      this.context.authenticatedUser();

      this.fromApiJsonDeserializer.validateForUpdate(command.json());

      final Hook hook = retrieveHookBy(hookId);
      final HookTemplate template = hook.getHookTemplate();
      final Map<String, Object> changes = hook.update(command);

      if (!changes.isEmpty()) {

        if (changes.containsKey(templateIdParamName)) {
          final Long ugdTemplateId = command.longValueOfParameterNamed(templateIdParamName);
          final Template ugdTemplate = this.ugdTemplateRepository.findOne(ugdTemplateId);
          if (ugdTemplate == null) {
            changes.remove(templateIdParamName);
            throw new TemplateNotFoundException(ugdTemplateId);
          }
          hook.updateUgdTemplate(ugdTemplate);
        }

        if (changes.containsKey(eventsParamName)) {
          final Set<HookResource> events =
              assembleSetOfEvents(command.arrayOfParameterNamed(eventsParamName));
          final boolean updated = hook.updateEvents(events);
          if (!updated) {
            changes.remove(eventsParamName);
          }
        }

        if (changes.containsKey(configParamName)) {
          final String configJson = command.jsonFragment(configParamName);
          final Set<HookConfiguration> config =
              assembleConfig(command.mapValueOfParameterNamed(configJson), template);
          final boolean updated = hook.updateConfig(config);
          if (!updated) {
            changes.remove(configParamName);
          }
        }

        this.hookRepository.saveAndFlush(hook);
      }

      return new CommandProcessingResultBuilder() //
          .withCommandId(command.commandId()) //
          .withEntityId(hookId) //
          .with(changes) //
          .build();
    } catch (final DataIntegrityViolationException dve) {
      handleHookDataIntegrityIssues(command, dve);
      return null;
    }
  }
  @Transactional
  @Override
  @CacheEvict(value = "hooks", allEntries = true)
  public CommandProcessingResult createHook(final JsonCommand command) {

    try {
      this.context.authenticatedUser();

      this.fromApiJsonDeserializer.validateForCreate(command.json());

      final HookTemplate template =
          retrieveHookTemplateBy(command.stringValueOfParameterNamed(nameParamName));
      final String configJson = command.jsonFragment(configParamName);
      final Set<HookConfiguration> config =
          assembleConfig(command.mapValueOfParameterNamed(configJson), template);
      final JsonArray events = command.arrayOfParameterNamed(eventsParamName);
      final Set<HookResource> allEvents = assembleSetOfEvents(events);
      Template ugdTemplate = null;
      if (command.hasParameter(templateIdParamName)) {
        final Long ugdTemplateId = command.longValueOfParameterNamed(templateIdParamName);
        ugdTemplate = this.ugdTemplateRepository.findOne(ugdTemplateId);
        if (ugdTemplate == null) {
          throw new TemplateNotFoundException(ugdTemplateId);
        }
      }
      final Hook hook = Hook.fromJson(command, template, config, allEvents, ugdTemplate);

      validateHookRules(template, config, allEvents);

      this.hookRepository.save(hook);

      return new CommandProcessingResultBuilder()
          .withCommandId(command.commandId())
          .withEntityId(hook.getId())
          .build();
    } catch (final DataIntegrityViolationException dve) {
      handleHookDataIntegrityIssues(command, dve);
      return CommandProcessingResult.empty();
    }
  }
Пример #3
0
  public static Template fromJson(final JsonCommand command) {
    final String name = command.stringValueOfParameterNamed("name");
    final String text = command.stringValueOfParameterNamed("text");
    final TemplateEntity entity =
        TemplateEntity.values()[command.integerValueSansLocaleOfParameterNamed("entity")];
    final TemplateType type =
        TemplateType.values()[command.integerValueSansLocaleOfParameterNamed("type")];

    final JsonArray array = command.arrayOfParameterNamed("mappers");

    final List<TemplateMapper> mappersList = new ArrayList<>();

    for (final JsonElement element : array) {
      mappersList.add(
          new TemplateMapper(
              element.getAsJsonObject().get("mappersorder").getAsInt(),
              element.getAsJsonObject().get("mapperskey").getAsString(),
              element.getAsJsonObject().get("mappersvalue").getAsString()));
    }

    return new Template(name, text, entity, type, mappersList);
  }
  @Override
  public CommandProcessingResult createMediaAsset(JsonCommand command) {

    try {

      this.context.authenticatedUser();
      this.fromApiJsonDeserializer.validateForCreate(command.json());
      MediaAsset mediaAsset = MediaAsset.fromJson(command);
      final JsonArray mediaassetAttributesArray =
          command.arrayOfParameterNamed("mediaassetAttributes").getAsJsonArray();
      String[] mediaassetAttributes = null;
      mediaassetAttributes = new String[mediaassetAttributesArray.size()];
      for (int i = 0; i < mediaassetAttributesArray.size(); i++) {
        mediaassetAttributes[i] = mediaassetAttributesArray.get(i).toString();
        // JsonObject temp = mediaassetAttributesArray.getAsJsonObject();

      }
      // For Media Attributes
      for (String mediaassetAttribute : mediaassetAttributes) {

        final JsonElement element = fromApiJsonHelper.parse(mediaassetAttribute);
        final String mediaAttributeType =
            fromApiJsonHelper.extractStringNamed("attributeType", element);
        final String mediaattributeName =
            fromApiJsonHelper.extractStringNamed("attributeName", element);
        final String mediaattributeValue =
            fromApiJsonHelper.extractStringNamed("attributevalue", element);
        final String mediaattributeNickname =
            fromApiJsonHelper.extractStringNamed("attributeNickname", element);
        final String mediaattributeImage =
            fromApiJsonHelper.extractStringNamed("attributeImage", element);
        MediaassetAttributes attributes =
            new MediaassetAttributes(
                mediaAttributeType,
                mediaattributeName,
                mediaattributeValue,
                mediaattributeNickname,
                mediaattributeImage);
        mediaAsset.add(attributes);
      }

      final JsonArray mediaassetLocationsArray =
          command.arrayOfParameterNamed("mediaAssetLocations").getAsJsonArray();
      String[] mediaassetLocations = null;
      mediaassetLocations = new String[mediaassetLocationsArray.size()];
      for (int i = 0; i < mediaassetLocationsArray.size(); i++) {

        mediaassetLocations[i] = mediaassetLocationsArray.get(i).toString();
      }
      // For Media Attributes
      for (String mediaassetLocationData : mediaassetLocations) {

        final JsonElement element = fromApiJsonHelper.parse(mediaassetLocationData);
        final Long languageId = fromApiJsonHelper.extractLongNamed("languageId", element);
        final String formatType = fromApiJsonHelper.extractStringNamed("formatType", element);
        final String location = fromApiJsonHelper.extractStringNamed("location", element);
        MediaassetLocation mediaassetLocation =
            new MediaassetLocation(languageId, formatType, location);
        mediaAsset.addMediaLocations(mediaassetLocation);
      }

      this.assetRepository.save(mediaAsset);
      return new CommandProcessingResult(mediaAsset.getId());

    } catch (DataIntegrityViolationException dve) {
      handleCodeDataIntegrityIssues(command, dve);
      return new CommandProcessingResult(Long.valueOf(-1));
    }
  }
  @Override
  public CommandProcessingResult updateGameMediaAsset(JsonCommand command) {

    MediaAsset mediaAssetMaster = null;
    try {
      this.context.authenticatedUser();
      final Long mediaId = command.entityId();
      mediaAssetMaster = this.assetRepository.findOne(mediaId);

      if (mediaAssetMaster == null) {
        throw new NoMoviesFoundException(
            "error.media.assets.game.not.found", "No Game Asset Found");
      }

      final Map<String, Object> changes = mediaAssetMaster.updateForGame(command);
      if (!changes.isEmpty()) {
        assetRepository.save(mediaAssetMaster);
      }

      final JsonArray gameData = command.arrayOfParameterNamed("gameMediaData").getAsJsonArray();
      for (int i = 0; i < gameData.size(); i++) {
        String currentData = gameData.get(i).toString();
        final JsonElement element = fromApiJsonHelper.parse(currentData);

        final Long settlementId = fromApiJsonHelper.extractLongNamed("settlementId", element);
        if (settlementId == null) {

          final BigDecimal amount =
              fromApiJsonHelper.extractBigDecimalWithLocaleNamed("amount", element);
          final String category = fromApiJsonHelper.extractStringNamed("category", element);
          final String mediaContentProvider =
              fromApiJsonHelper.extractStringNamed("mediaContentProvider", element);
          final String mt = fromApiJsonHelper.extractStringNamed("mediaType", element);
          final Character mediaType = mt.equals("Flat") ? 'F' : 'P';
          final Long sequence = fromApiJsonHelper.extractLongNamed("sequence", element);
          final String source = fromApiJsonHelper.extractStringNamed("source", element);

          final Settlement settlement =
              Settlement.fromJson(
                  mediaId, amount, category, mediaContentProvider, mediaType, sequence, source);
          settlementJpaRepository.save(settlement);
        } else {
          final Settlement settlement = settlementJpaRepository.findOne(settlementId);
          if (settlement == null) {
            throw new SettlementNotFoundException();
          }

          final Map<String, Object> settlementChanges =
              settlement.update(element, fromApiJsonHelper, mediaId);

          if (!settlementChanges.isEmpty()) {
            settlementJpaRepository.save(settlement);
          }
        }
      }

    } catch (DataIntegrityViolationException dve) {
      handleCodeDataIntegrityIssues(command, dve);
      return new CommandProcessingResult(Long.valueOf(-1));
    }
    return new CommandProcessingResult(mediaAssetMaster.getId());
  }